@nocloud/sdk 0.1.13 → 0.1.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +1 -1
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var{defineProperty:W,getOwnPropertyNames:z,getOwnPropertyDescriptor:M}=Object,R=Object.prototype.hasOwnProperty;var
|
|
1
|
+
var{defineProperty:W,getOwnPropertyNames:z,getOwnPropertyDescriptor:M}=Object,R=Object.prototype.hasOwnProperty;var w=new WeakMap,S=(h)=>{var f=w.get(h),x;if(f)return f;if(f=W({},"__esModule",{value:!0}),h&&typeof h==="object"||typeof h==="function")z(h).map((F)=>!R.call(f,F)&&W(f,F,{get:()=>h[F],enumerable:!(x=M(h,F))||x.enumerable}));return w.set(h,f),f};var c=(h,f)=>{for(var x in f)W(h,x,{get:f[x],enumerable:!0,configurable:!0,set:(F)=>f[x]=()=>F})};var A={};c(A,{default:()=>P,NoCloudError:()=>q,NoCloudAPIError:()=>O,NoCloud:()=>V});module.exports=S(A);var G="https://api.nonefivem.com",L="/cloud";var q;((D)=>{D.INVALID_API_KEY="INVALID_API_KEY";D.BAD_REQUEST="BAD_REQUEST";D.RATE_LIMIT_EXCEEDED="RATE_LIMIT_EXCEEDED";D.RESOURCE_NOT_FOUND="RESOURCE_NOT_FOUND";D.INTERNAL_SERVER_ERROR="INTERNAL_SERVER_ERROR";D.UNKNOWN_ERROR="UNKNOWN_ERROR"})(q||={});class O extends Error{status;code;constructor(h,f,x){super(h);this.status=f;this.code=x;this.name="NoCloudAPIError",this.code=x}static isError(h,f){if(!(h instanceof O))return!1;if(f&&h.code!==f)return!1;return!0}static fromCode(h,f){switch(h){case"BAD_REQUEST":return new O(f,400,h);case"INVALID_API_KEY":return new O(f,401,h);case"RATE_LIMIT_EXCEEDED":return new O(f,429,h);case"RESOURCE_NOT_FOUND":return new O(f,404,h);case"INTERNAL_SERVER_ERROR":case"UNKNOWN_ERROR":return new O(f,500,h);default:return new O(f,500,"INTERNAL_SERVER_ERROR")}}static fromStatus(h,f){switch(h){case 400:return new O(f,h,"BAD_REQUEST");case 401:return new O(f,h,"INVALID_API_KEY");case 429:return new O(f,h,"RATE_LIMIT_EXCEEDED");case 404:return new O(f,h,"RESOURCE_NOT_FOUND");case 500:return new O(f,h,"INTERNAL_SERVER_ERROR");default:return new O(f,h,"UNKNOWN_ERROR")}}}function y(h){return new Promise((f)=>setTimeout(f,h))}async function B(h,f=3,x=1000){let F;for(let $=0;$<=f;$++)try{return await h()}catch(_){if(F=_,$<f)await y(x)}throw F}var b={iVBORw0KGgo:"image/png","/9j/":"image/jpeg",R0lGOD:"image/gif",UklGR:"image/webp",AAAA:"video/mp4",JVBERi0:"application/pdf",UEsDB:"application/zip",PD94bWw:"application/xml",PHN2Zw:"image/svg+xml"};function k(h){let f=h.match(/^data:([^;,]+)/);if(f?.[1])return f[1];for(let[x,F]of Object.entries(b))if(h.startsWith(x))return F;return null}function Z(h){let f=h.match(/^data:[^;,]+;base64,(.+)$/);if(f?.[1])return f[1];return h}function v(h){let f=atob(h),x=new Uint8Array(f.length);for(let F=0;F<f.length;F++)x[F]=f.charCodeAt(F);return x}function H(h){return h.split(";")[0]?.trim()??h}class X{baseUrl;basePath;apiKey;retryCount;retryDelayMs;constructor(h){if(this.apiKey=h.apiKey,this.baseUrl=(h.baseUrl||G).replace(/\/+$/,""),this.basePath=(h.basePath??L).replace(/^\/+|\/+$/g,""),this.retryCount=h.retries??3,this.retryDelayMs=h.retryDelayMs??1000,!this.apiKey)throw Error("API key is required")}buildUrl(h){let f=this.basePath?`${this.basePath}/${h}`:h;return`${this.baseUrl}/${f}`}fetch(h,f={}){let x=this.buildUrl(h);return B(()=>fetch(x,{...f,headers:{Authorization:`Bearer ${this.apiKey}`,...f.headers||{}}}),f.retries??this.retryCount,f.retryDelayMs??this.retryDelayMs)}}async function Q(h){if(!h.ok){let f=await h.json().catch(()=>({}));throw O.fromStatus(h.status,f.message||"Unknown error")}return await h.json()}class Y{fetcher;constructor(h){this.fetcher=h}fetch(h,f){return this.fetcher.fetch(h,f)}}class j extends Y{async generateSignedUrl(h,f,x){let F=new URLSearchParams;if(F.append("contentType",h),F.append("size",f.toString()),x)F.append("metadata",JSON.stringify(x));let $=await this.fetch(`storage/signed-url?${F.toString()}`);return Q($)}getBodyInfo(h){if(typeof Blob<"u"&&h instanceof Blob)return{contentType:H(h.type)||"application/octet-stream",size:h.size,normalizedBody:h};if(typeof ArrayBuffer<"u"&&h instanceof ArrayBuffer)return{contentType:"application/octet-stream",size:h.byteLength,normalizedBody:h};if(typeof h==="string"){let f=k(h);if(f){let F=Z(h),$=v(F);return{contentType:f,size:$.byteLength,normalizedBody:new Blob([$.buffer])}}let x=new TextEncoder().encode(h);return{contentType:"text/plain",size:x.length,normalizedBody:new Blob([x.buffer])}}throw O.fromStatus(400,"Unsupported body type")}async upload(h,f){let{contentType:x,size:F,normalizedBody:$}=this.getBodyInfo(h),{url:_,mediaUrl:D,mediaId:K}=await this.generateSignedUrl(x,F,f),U=await fetch(_,{method:"PUT",headers:{"Content-Length":F.toString()},body:$});if(!U.ok){let J=await U.text().catch(()=>U.statusText);throw O.fromStatus(U.status,`Failed to upload file to R2: ${J}`)}return{id:K,url:D}}async uploadStream(h,f,x,F){let{url:$,mediaUrl:_,mediaId:D}=await this.generateSignedUrl(f,x,F),K=await fetch($,{method:"PUT",headers:{"Content-Length":x.toString()},body:h,duplex:"half"});if(!K.ok){let U=await K.text().catch(()=>K.statusText);throw O.fromStatus(K.status,`Failed to upload stream to R2: ${U}`)}return{id:D,url:_}}async delete(h){if(!Array.isArray(h)){let F=await this.fetch(`storage/${h}`,{method:"DELETE"});await Q(F);return}if(h.length===0)return;if(h.length===1)return this.delete(h[0]);let f=100,x=Math.ceil(h.length/f);for(let F=0;F<x;F++){let $=F*f,_=Math.min($+f,h.length),D=h.slice($,_),K=await this.fetch("storage/bulk",{method:"DELETE",headers:{"Content-Type":"application/json"},body:JSON.stringify({ids:D})});await Q(K)}}}class V{fetcher;constructor(h){if(typeof h==="string")h={apiKey:h};this.fetcher=new X({apiKey:h.apiKey,baseUrl:h.baseUrl,basePath:h.basePath,retries:h.retries,retryDelayMs:h.retryDelayMs})}_storage;get storage(){return this._storage??=new j(this.fetcher)}}var P=V;
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var j="https://api.nonefivem.com",
|
|
1
|
+
var j="https://api.nonefivem.com",w="/cloud";var v;((D)=>{D.INVALID_API_KEY="INVALID_API_KEY";D.BAD_REQUEST="BAD_REQUEST";D.RATE_LIMIT_EXCEEDED="RATE_LIMIT_EXCEEDED";D.RESOURCE_NOT_FOUND="RESOURCE_NOT_FOUND";D.INTERNAL_SERVER_ERROR="INTERNAL_SERVER_ERROR";D.UNKNOWN_ERROR="UNKNOWN_ERROR"})(v||={});class O extends Error{status;code;constructor(h,f,x){super(h);this.status=f;this.code=x;this.name="NoCloudAPIError",this.code=x}static isError(h,f){if(!(h instanceof O))return!1;if(f&&h.code!==f)return!1;return!0}static fromCode(h,f){switch(h){case"BAD_REQUEST":return new O(f,400,h);case"INVALID_API_KEY":return new O(f,401,h);case"RATE_LIMIT_EXCEEDED":return new O(f,429,h);case"RESOURCE_NOT_FOUND":return new O(f,404,h);case"INTERNAL_SERVER_ERROR":case"UNKNOWN_ERROR":return new O(f,500,h);default:return new O(f,500,"INTERNAL_SERVER_ERROR")}}static fromStatus(h,f){switch(h){case 400:return new O(f,h,"BAD_REQUEST");case 401:return new O(f,h,"INVALID_API_KEY");case 429:return new O(f,h,"RATE_LIMIT_EXCEEDED");case 404:return new O(f,h,"RESOURCE_NOT_FOUND");case 500:return new O(f,h,"INTERNAL_SERVER_ERROR");default:return new O(f,h,"UNKNOWN_ERROR")}}}function H(h){return new Promise((f)=>setTimeout(f,h))}async function G(h,f=3,x=1000){let F;for(let $=0;$<=f;$++)try{return await h()}catch(_){if(F=_,$<f)await H(x)}throw F}var J={iVBORw0KGgo:"image/png","/9j/":"image/jpeg",R0lGOD:"image/gif",UklGR:"image/webp",AAAA:"video/mp4",JVBERi0:"application/pdf",UEsDB:"application/zip",PD94bWw:"application/xml",PHN2Zw:"image/svg+xml"};function L(h){let f=h.match(/^data:([^;,]+)/);if(f?.[1])return f[1];for(let[x,F]of Object.entries(J))if(h.startsWith(x))return F;return null}function q(h){let f=h.match(/^data:[^;,]+;base64,(.+)$/);if(f?.[1])return f[1];return h}function B(h){let f=atob(h),x=new Uint8Array(f.length);for(let F=0;F<f.length;F++)x[F]=f.charCodeAt(F);return x}function k(h){return h.split(";")[0]?.trim()??h}class V{baseUrl;basePath;apiKey;retryCount;retryDelayMs;constructor(h){if(this.apiKey=h.apiKey,this.baseUrl=(h.baseUrl||j).replace(/\/+$/,""),this.basePath=(h.basePath??w).replace(/^\/+|\/+$/g,""),this.retryCount=h.retries??3,this.retryDelayMs=h.retryDelayMs??1000,!this.apiKey)throw Error("API key is required")}buildUrl(h){let f=this.basePath?`${this.basePath}/${h}`:h;return`${this.baseUrl}/${f}`}fetch(h,f={}){let x=this.buildUrl(h);return G(()=>fetch(x,{...f,headers:{Authorization:`Bearer ${this.apiKey}`,...f.headers||{}}}),f.retries??this.retryCount,f.retryDelayMs??this.retryDelayMs)}}async function Q(h){if(!h.ok){let f=await h.json().catch(()=>({}));throw O.fromStatus(h.status,f.message||"Unknown error")}return await h.json()}class W{fetcher;constructor(h){this.fetcher=h}fetch(h,f){return this.fetcher.fetch(h,f)}}class X extends W{async generateSignedUrl(h,f,x){let F=new URLSearchParams;if(F.append("contentType",h),F.append("size",f.toString()),x)F.append("metadata",JSON.stringify(x));let $=await this.fetch(`storage/signed-url?${F.toString()}`);return Q($)}getBodyInfo(h){if(typeof Blob<"u"&&h instanceof Blob)return{contentType:k(h.type)||"application/octet-stream",size:h.size,normalizedBody:h};if(typeof ArrayBuffer<"u"&&h instanceof ArrayBuffer)return{contentType:"application/octet-stream",size:h.byteLength,normalizedBody:h};if(typeof h==="string"){let f=L(h);if(f){let F=q(h),$=B(F);return{contentType:f,size:$.byteLength,normalizedBody:new Blob([$.buffer])}}let x=new TextEncoder().encode(h);return{contentType:"text/plain",size:x.length,normalizedBody:new Blob([x.buffer])}}throw O.fromStatus(400,"Unsupported body type")}async upload(h,f){let{contentType:x,size:F,normalizedBody:$}=this.getBodyInfo(h),{url:_,mediaUrl:D,mediaId:K}=await this.generateSignedUrl(x,F,f),U=await fetch(_,{method:"PUT",headers:{"Content-Length":F.toString()},body:$});if(!U.ok){let Z=await U.text().catch(()=>U.statusText);throw O.fromStatus(U.status,`Failed to upload file to R2: ${Z}`)}return{id:K,url:D}}async uploadStream(h,f,x,F){let{url:$,mediaUrl:_,mediaId:D}=await this.generateSignedUrl(f,x,F),K=await fetch($,{method:"PUT",headers:{"Content-Length":x.toString()},body:h,duplex:"half"});if(!K.ok){let U=await K.text().catch(()=>K.statusText);throw O.fromStatus(K.status,`Failed to upload stream to R2: ${U}`)}return{id:D,url:_}}async delete(h){if(!Array.isArray(h)){let F=await this.fetch(`storage/${h}`,{method:"DELETE"});await Q(F);return}if(h.length===0)return;if(h.length===1)return this.delete(h[0]);let f=100,x=Math.ceil(h.length/f);for(let F=0;F<x;F++){let $=F*f,_=Math.min($+f,h.length),D=h.slice($,_),K=await this.fetch("storage/bulk",{method:"DELETE",headers:{"Content-Type":"application/json"},body:JSON.stringify({ids:D})});await Q(K)}}}class Y{fetcher;constructor(h){if(typeof h==="string")h={apiKey:h};this.fetcher=new V({apiKey:h.apiKey,baseUrl:h.baseUrl,basePath:h.basePath,retries:h.retries,retryDelayMs:h.retryDelayMs})}_storage;get storage(){return this._storage??=new X(this.fetcher)}}var fh=Y;export{fh as default,v as NoCloudError,O as NoCloudAPIError,Y as NoCloud};
|