@nocloud/sdk 0.1.11 → 0.1.12
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.d.ts +2 -2
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var{defineProperty:
|
|
1
|
+
var{defineProperty:A,getOwnPropertyNames:L,getOwnPropertyDescriptor:B}=Object,C=Object.prototype.hasOwnProperty;var Q=new WeakMap,H=(h)=>{var f=Q.get(h),x;if(f)return f;if(f=A({},"__esModule",{value:!0}),h&&typeof h==="object"||typeof h==="function")L(h).map((F)=>!C.call(f,F)&&A(f,F,{get:()=>h[F],enumerable:!(x=B(h,F))||x.enumerable}));return Q.set(h,f),f};var k=(h,f)=>{for(var x in f)A(h,x,{get:f[x],enumerable:!0,configurable:!0,set:(F)=>f[x]=()=>F})};var E={};k(E,{default:()=>J,NoCloudAPIError:()=>$,NoCloud:()=>_});module.exports=H(E);var V="https://api.nonefivem.com",W="/cloud";class $ extends Error{status;constructor(h,f){super(h);this.status=f;this.name="NoCloudAPIError"}}function Z(h){return new Promise((f)=>setTimeout(f,h))}async function X(h,f=3,x=1000){let F;for(let r=0;r<=f;r++)try{return await h()}catch(c){if(F=c,r<f)await Z(x)}throw F}var g={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 Y(h){let f=h.match(/^data:([^;,]+)/);if(f?.[1])return f[1];for(let[x,F]of Object.entries(g))if(h.startsWith(x))return F;return null}function j(h){let f=h.match(/^data:[^;,]+;base64,(.+)$/);if(f?.[1])return f[1];return h}function q(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 w(h){return h.split(";")[0]?.trim()??h}class U{baseUrl;basePath;apiKey;retryCount;retryDelayMs;constructor(h){if(this.apiKey=h.apiKey,this.baseUrl=(h.baseUrl||V).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 X(()=>fetch(x,{...f,headers:{Authorization:`Bearer ${this.apiKey}`,...f.headers||{}}}),f.retries??this.retryCount,f.retryDelayMs??this.retryDelayMs)}}async function P(h){if(!h.ok){let f=await h.json().catch(()=>({}));throw new $(f.message||"Unknown error",h.status)}return await h.json()}class u{fetcher;constructor(h){this.fetcher=h}fetch(h,f){return this.fetcher.fetch(h,f)}}class v extends u{async generateSignedUrl(h,f,x){let F=new URLSearchParams;F.append("contentType",h),F.append("size",f.toString());let r=await this.fetch(`storage/signed-url?${F.toString()}`);return P(r)}getBodyInfo(h){if(typeof Blob<"u"&&h instanceof Blob)return{contentType:w(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=Y(h);if(f){let F=j(h),r=q(F);return{contentType:f,size:r.byteLength,normalizedBody:new Blob([r.buffer])}}let x=new TextEncoder().encode(h);return{contentType:"text/plain",size:x.length,normalizedBody:new Blob([x.buffer])}}throw new $("Unsupported body type",400)}async upload(h,f){let{contentType:x,size:F,normalizedBody:r}=this.getBodyInfo(h),{url:c,mediaUrl:K,mediaId:O}=await this.generateSignedUrl(x,F,f),D=await fetch(c,{method:"PUT",headers:{"Content-Length":F.toString()},body:r});if(!D.ok){let G=await D.text().catch(()=>D.statusText);throw new $(`Failed to upload file to R2: ${G}`,D.status)}return{id:O,url:K}}async uploadStream(h,f,x,F){let{url:r,mediaUrl:c,mediaId:K}=await this.generateSignedUrl(f,x,F),O=await fetch(r,{method:"PUT",headers:{"Content-Length":x.toString()},body:h,duplex:"half"});if(!O.ok){let D=await O.text().catch(()=>O.statusText);throw new $(`Failed to upload stream to R2: ${D}`,O.status)}return{id:K,url:c}}async delete(h){if(!Array.isArray(h)){let F=await this.fetch(`storage/${h}`,{method:"DELETE"});await P(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 r=F*f,c=Math.min(r+f,h.length),K=h.slice(r,c),O=await this.fetch("storage/bulk",{method:"DELETE",headers:{"Content-Type":"application/json"},body:JSON.stringify({ids:K})});await P(O)}}}class _{fetcher;constructor(h){if(typeof h==="string")h={apiKey:h};this.fetcher=new U({apiKey:h.apiKey,baseUrl:h.baseUrl,basePath:h.basePath,retries:h.retries,retryDelayMs:h.retryDelayMs})}_storage;get storage(){return this._storage??=new v(this.fetcher)}}var J=_;
|
package/dist/index.d.ts
CHANGED
|
@@ -107,11 +107,11 @@ declare class Storage$1 extends SDKModule {
|
|
|
107
107
|
uploadStream(stream: ReadableStream, contentType: string, contentLength: number, metadata?: FileMetadata): Promise<UploadResponse>;
|
|
108
108
|
/**
|
|
109
109
|
* Deletes a media file from the storage.
|
|
110
|
-
* @param mediaId The ID of the media
|
|
110
|
+
* @param mediaId - The ID of the media to delete, or an array of IDs for batch deletion (max 100 per batch).
|
|
111
111
|
* @returns {Promise<void>} A promise that resolves when the deletion is complete.
|
|
112
112
|
* @throws {NoCloudAPIError} If the deletion fails.
|
|
113
113
|
*/
|
|
114
|
-
delete(mediaId: string): Promise<void>;
|
|
114
|
+
delete(mediaId: string | string[]): Promise<void>;
|
|
115
115
|
}
|
|
116
116
|
export interface NoCloudOptions {
|
|
117
117
|
/**
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
1
|
+
var v="https://api.nonefivem.com",Q="/cloud";class c extends Error{status;constructor(h,f){super(h);this.status=f;this.name="NoCloudAPIError"}}function w(h){return new Promise((f)=>setTimeout(f,h))}async function V(h,f=3,x=1000){let F;for(let r=0;r<=f;r++)try{return await h()}catch($){if(F=$,r<f)await w(x)}throw F}var G={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 W(h){let f=h.match(/^data:([^;,]+)/);if(f?.[1])return f[1];for(let[x,F]of Object.entries(G))if(h.startsWith(x))return F;return null}function X(h){let f=h.match(/^data:[^;,]+;base64,(.+)$/);if(f?.[1])return f[1];return h}function Y(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 j(h){return h.split(";")[0]?.trim()??h}class _{baseUrl;basePath;apiKey;retryCount;retryDelayMs;constructor(h){if(this.apiKey=h.apiKey,this.baseUrl=(h.baseUrl||v).replace(/\/+$/,""),this.basePath=(h.basePath??Q).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 V(()=>fetch(x,{...f,headers:{Authorization:`Bearer ${this.apiKey}`,...f.headers||{}}}),f.retries??this.retryCount,f.retryDelayMs??this.retryDelayMs)}}async function P(h){if(!h.ok){let f=await h.json().catch(()=>({}));throw new c(f.message||"Unknown error",h.status)}return await h.json()}class A{fetcher;constructor(h){this.fetcher=h}fetch(h,f){return this.fetcher.fetch(h,f)}}class U extends A{async generateSignedUrl(h,f,x){let F=new URLSearchParams;F.append("contentType",h),F.append("size",f.toString());let r=await this.fetch(`storage/signed-url?${F.toString()}`);return P(r)}getBodyInfo(h){if(typeof Blob<"u"&&h instanceof Blob)return{contentType:j(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=W(h);if(f){let F=X(h),r=Y(F);return{contentType:f,size:r.byteLength,normalizedBody:new Blob([r.buffer])}}let x=new TextEncoder().encode(h);return{contentType:"text/plain",size:x.length,normalizedBody:new Blob([x.buffer])}}throw new c("Unsupported body type",400)}async upload(h,f){let{contentType:x,size:F,normalizedBody:r}=this.getBodyInfo(h),{url:$,mediaUrl:K,mediaId:O}=await this.generateSignedUrl(x,F,f),D=await fetch($,{method:"PUT",headers:{"Content-Length":F.toString()},body:r});if(!D.ok){let q=await D.text().catch(()=>D.statusText);throw new c(`Failed to upload file to R2: ${q}`,D.status)}return{id:O,url:K}}async uploadStream(h,f,x,F){let{url:r,mediaUrl:$,mediaId:K}=await this.generateSignedUrl(f,x,F),O=await fetch(r,{method:"PUT",headers:{"Content-Length":x.toString()},body:h,duplex:"half"});if(!O.ok){let D=await O.text().catch(()=>O.statusText);throw new c(`Failed to upload stream to R2: ${D}`,O.status)}return{id:K,url:$}}async delete(h){if(!Array.isArray(h)){let F=await this.fetch(`storage/${h}`,{method:"DELETE"});await P(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 r=F*f,$=Math.min(r+f,h.length),K=h.slice(r,$),O=await this.fetch("storage/bulk",{method:"DELETE",headers:{"Content-Type":"application/json"},body:JSON.stringify({ids:K})});await P(O)}}}class u{fetcher;constructor(h){if(typeof h==="string")h={apiKey:h};this.fetcher=new _({apiKey:h.apiKey,baseUrl:h.baseUrl,basePath:h.basePath,retries:h.retries,retryDelayMs:h.retryDelayMs})}_storage;get storage(){return this._storage??=new U(this.fetcher)}}var hh=u;export{hh as default,c as NoCloudAPIError,u as NoCloud};
|