UNPKG

clodynix

Version:

Clodynix is a modern, efficient, and developer-friendly CDN package offering seamless content delivery with an intuitive dashboard and comprehensive documentation. Simplify your content delivery with Clodynix's powerful and scalable platform.

179 lines (178 loc) 4.51 kB
// src/functions/clodynix.ts import axios from "axios"; var Clodynix = class { apiKey; origin; secret; constructor({ apiKey, origin, secret }) { this.apiKey = apiKey; this.origin = origin; this.secret = secret; } getAuthToken() { return this.apiKey; } getOriginLink() { return this.origin; } getSecret() { return this.secret; } async checkFileInfo(fileId, token) { try { const res = await axios.get(`${this.getOriginLink()}/v2/file/info/client`, { headers: { Authorization: this.getAuthToken(), Secret: this.getSecret() }, data: { fileId, token } }); if (res.status !== 200) throw new Error(res.data); return res.data.data; } catch (error) { throw new Error(error.message); } } async registerFile(token) { try { const res = await axios.post( `${this.getOriginLink()}/v2/file/register`, { token }, { headers: { Authorization: this.getAuthToken(), Secret: this.getSecret() } } ); if (res.status !== 200) throw new Error(res.data); return res.data.data; } catch (error) { throw new Error(error.message); } } async uploadByLink(link) { try { const res = await axios.put( `${this.getOriginLink()}/v2/file/upload/link`, { link }, { headers: { Authorization: this.getAuthToken(), Secret: this.getSecret() } } ); if (res.status !== 200) throw new Error(res.data); return res.data.data; } catch (error) { throw new Error(error.message); } } async createUploadToken(totalChunks, fileData) { try { const res = await axios.post( `${this.getOriginLink()}/v2/file/upload/token/create`, { totalChunks, fileData }, { headers: { Authorization: this.getAuthToken(), Secret: this.getSecret() } } ); if (res.status !== 200) throw new Error(res.data); return res.data.data; } catch (error) { throw new Error(error.message); } } async deleteFile(fileId) { try { const res = await axios.delete(`${this.getOriginLink()}/v2/file/delete`, { headers: { Authorization: this.getAuthToken(), Secret: this.getSecret() }, data: { fileId } }); if (res.status !== 200) throw new Error(res.data); return res.data.data; } catch (error) { throw new Error(error.message); } } async generateLink(fileId) { try { const res = await axios.post( `${this.getOriginLink()}/v2/link/single/create`, { fileId }, { headers: { Authorization: this.getAuthToken(), Secret: this.getSecret() } } ); if (res.status !== 200) throw new Error(res.data); return res.data.data; } catch (error) { throw new Error(error.message); } } async generateLinksBatch(fileIds) { try { const res = await axios.post( `${this.getOriginLink()}/v2/link/batch/create`, { fileIds }, { headers: { Authorization: this.getAuthToken(), Secret: this.getSecret() } } ); if (res.status !== 200) throw new Error(res.data); return res.data.data; } catch (error) { throw new Error(error.message); } } async uploadDirect(formdata) { try { const file = formdata.get("file"); const fileBuffer = await file.arrayBuffer(); const blob = new Blob([fileBuffer]); const form = new FormData(); const fileData = { name: file.name.split(".").shift() || "", extention: file.name.split(".").pop() || "", size: file.size }; form.append("fileData", JSON.stringify(fileData)); form.append("file", blob); const res = await axios.put( `${this.getOriginLink()}/v2/file/upload/direct`, form, { headers: { Authorization: this.getAuthToken(), Secret: this.getSecret() } } ); if (res.status !== 200) throw new Error(res.data); return res.data.data; } catch (error) { throw new Error(error.message); } } }; var clodynix_default = Clodynix; export { clodynix_default as Clodynix };