@nocloud/sdk 0.1.1 → 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 CHANGED
@@ -1 +1 @@
1
- var{defineProperty:A,getOwnPropertyNames:Y,getOwnPropertyDescriptor:Z}=Object,j=Object.prototype.hasOwnProperty;var H=new WeakMap,E=(r)=>{var h=H.get(r),x;if(h)return h;if(h=A({},"__esModule",{value:!0}),r&&typeof r==="object"||typeof r==="function")Y(r).map((F)=>!j.call(h,F)&&A(h,F,{get:()=>r[F],enumerable:!(x=Z(r,F))||x.enumerable}));return H.set(r,h),h};var G=(r,h)=>{for(var x in h)A(r,x,{get:h[x],enumerable:!0,configurable:!0,set:(F)=>h[x]=()=>F})};var C={};G(C,{default:()=>L,NoCloudAPIError:()=>f,NoCloud:()=>_});module.exports=E(C);var U="https://api.nonefivem.com",v="/cloud";class f extends Error{status;constructor(r,h){super(r);this.status=h;this.name="NoCloudAPIError"}}function M(r){return new Promise((h)=>setTimeout(h,r))}async function Q(r,h=3,x=1000){let F;for(let O=0;O<=h;O++)try{return await r()}catch(P){if(F=P,O<h)await M(x)}throw F}var k={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 V(r){let h=r.match(/^data:([^;,]+)/);if(h?.[1])return h[1];for(let[x,F]of Object.entries(k))if(r.startsWith(x))return F;return null}function W(r){let h=r.match(/^data:[^;,]+;base64,(.+)$/);if(h?.[1])return h[1];return r}function X(r){let h=(r.match(/=+$/)||[""])[0].length;return Math.floor(r.length*3/4)-h}class D{baseUrl;basePath;apiKey;retryCount;retryDelayMs;constructor(r){if(this.apiKey=r.apiKey,this.baseUrl=(r.baseUrl||U).replace(/\/+$/,""),this.basePath=(r.basePath??v).replace(/^\/+|\/+$/g,""),this.retryCount=r.retries??3,this.retryDelayMs=r.retryDelayMs??1000,!this.apiKey)throw Error("API key is required")}buildUrl(r){let h=this.basePath?`${this.basePath}/${r}`:r;return`${this.baseUrl}/${h}`}fetch(r,h={}){let x=this.buildUrl(r);return Q(()=>fetch(x,{...h,headers:{Authorization:`Bearer ${this.apiKey}`,...h.headers||{}}}),h.retries??this.retryCount,h.retryDelayMs??this.retryDelayMs)}}async function B(r){if(!r.ok)try{let h=await r.json();throw new f(h.message||"API Error",r.status)}catch(h){throw new f("Unknown error",r.status)}return await r.json()}class q{fetcher;constructor(r){this.fetcher=r}fetch(r,h){return this.fetcher.fetch(r,h)}}class u extends q{async generateSignedUrl(r,h,x){let F=await this.fetch("storage/signed-url",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({contentType:r,size:h,metadata:x})});return B(F)}getBodyInfo(r){if(typeof Blob<"u"&&r instanceof Blob)return{contentType:r.type||"application/octet-stream",size:r.size};if(typeof ArrayBuffer<"u"&&r instanceof ArrayBuffer)return{contentType:"application/octet-stream",size:r.byteLength};if(typeof r==="string"){let h=V(r);if(h){let x=W(r),F=X(x);return{contentType:h,size:F}}return{contentType:"text/plain",size:new TextEncoder().encode(r).length}}throw new f("Unsupported body type",400)}async upload(r,h){let{contentType:x,size:F}=this.getBodyInfo(r),{url:O,mediaUrl:P,mediaId:$}=await this.generateSignedUrl(x,F,h),c=await fetch(O,{method:"PUT",headers:{"Content-Type":x,"Content-Length":F.toString()},body:r});if(!c.ok){let K=await c.text().catch(()=>c.statusText);throw new f(`Failed to upload file to R2: ${K}`,c.status)}return{id:$,url:P}}async uploadStream(r,h,x,F){let{url:O,mediaUrl:P,mediaId:$}=await this.generateSignedUrl(h,x,F),c=await fetch(O,{method:"PUT",headers:{"Content-Type":h,"Content-Length":x.toString()},body:r,duplex:"half"});if(!c.ok){let K=await c.text().catch(()=>c.statusText);throw new f(`Failed to upload stream to R2: ${K}`,c.status)}return{id:$,url:P}}async delete(r){let h=await this.fetch(`storage/${r}`,{method:"DELETE"});return B(h)}}class _{fetcher;constructor(r){if(typeof r==="string")r={apiKey:r};this.fetcher=new D({apiKey:r.apiKey,baseUrl:r.baseUrl,basePath:r.basePath,retries:r.retries,retryDelayMs:r.retryDelayMs})}_storage;get storage(){return this._storage??=new u(this.fetcher)}}var L=_;
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
@@ -23,7 +23,7 @@ declare class Fetcher {
23
23
  declare class SDKModule {
24
24
  private readonly fetcher;
25
25
  constructor(fetcher: Fetcher);
26
- protected fetch(endpoint: string, options: FetchOptions): Promise<Response>;
26
+ protected fetch(endpoint: string, options?: FetchOptions): Promise<Response>;
27
27
  }
28
28
  /**
29
29
  * Upload body types.
@@ -83,7 +83,8 @@ declare class Storage$1 extends SDKModule {
83
83
  */
84
84
  generateSignedUrl(contentType: string, size: number, metadata?: FileMetadata): Promise<SignedUrlResponse>;
85
85
  /**
86
- * Extracts content type and size from the body.
86
+ * Extracts content type, size, and normalized body from the input.
87
+ * For base64 strings, this decodes them to binary.
87
88
  */
88
89
  private getBodyInfo;
89
90
  /**
@@ -106,11 +107,11 @@ declare class Storage$1 extends SDKModule {
106
107
  uploadStream(stream: ReadableStream, contentType: string, contentLength: number, metadata?: FileMetadata): Promise<UploadResponse>;
107
108
  /**
108
109
  * Deletes a media file from the storage.
109
- * @param mediaId The ID of the media file to be deleted.
110
+ * @param mediaId - The ID of the media to delete, or an array of IDs for batch deletion (max 100 per batch).
110
111
  * @returns {Promise<void>} A promise that resolves when the deletion is complete.
111
112
  * @throws {NoCloudAPIError} If the deletion fails.
112
113
  */
113
- delete(mediaId: string): Promise<void>;
114
+ delete(mediaId: string | string[]): Promise<void>;
114
115
  }
115
116
  export interface NoCloudOptions {
116
117
  /**
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- var u="https://api.nonefivem.com",H="/cloud";class f extends Error{status;constructor(r,h){super(r);this.status=h;this.name="NoCloudAPIError"}}function W(r){return new Promise((h)=>setTimeout(h,r))}async function U(r,h=3,x=1000){let F;for(let O=0;O<=h;O++)try{return await r()}catch(P){if(F=P,O<h)await W(x)}throw F}var X={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 v(r){let h=r.match(/^data:([^;,]+)/);if(h?.[1])return h[1];for(let[x,F]of Object.entries(X))if(r.startsWith(x))return F;return null}function Q(r){let h=r.match(/^data:[^;,]+;base64,(.+)$/);if(h?.[1])return h[1];return r}function V(r){let h=(r.match(/=+$/)||[""])[0].length;return Math.floor(r.length*3/4)-h}class K{baseUrl;basePath;apiKey;retryCount;retryDelayMs;constructor(r){if(this.apiKey=r.apiKey,this.baseUrl=(r.baseUrl||u).replace(/\/+$/,""),this.basePath=(r.basePath??H).replace(/^\/+|\/+$/g,""),this.retryCount=r.retries??3,this.retryDelayMs=r.retryDelayMs??1000,!this.apiKey)throw Error("API key is required")}buildUrl(r){let h=this.basePath?`${this.basePath}/${r}`:r;return`${this.baseUrl}/${h}`}fetch(r,h={}){let x=this.buildUrl(r);return U(()=>fetch(x,{...h,headers:{Authorization:`Bearer ${this.apiKey}`,...h.headers||{}}}),h.retries??this.retryCount,h.retryDelayMs??this.retryDelayMs)}}async function A(r){if(!r.ok)try{let h=await r.json();throw new f(h.message||"API Error",r.status)}catch(h){throw new f("Unknown error",r.status)}return await r.json()}class D{fetcher;constructor(r){this.fetcher=r}fetch(r,h){return this.fetcher.fetch(r,h)}}class B extends D{async generateSignedUrl(r,h,x){let F=await this.fetch("storage/signed-url",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({contentType:r,size:h,metadata:x})});return A(F)}getBodyInfo(r){if(typeof Blob<"u"&&r instanceof Blob)return{contentType:r.type||"application/octet-stream",size:r.size};if(typeof ArrayBuffer<"u"&&r instanceof ArrayBuffer)return{contentType:"application/octet-stream",size:r.byteLength};if(typeof r==="string"){let h=v(r);if(h){let x=Q(r),F=V(x);return{contentType:h,size:F}}return{contentType:"text/plain",size:new TextEncoder().encode(r).length}}throw new f("Unsupported body type",400)}async upload(r,h){let{contentType:x,size:F}=this.getBodyInfo(r),{url:O,mediaUrl:P,mediaId:_}=await this.generateSignedUrl(x,F,h),c=await fetch(O,{method:"PUT",headers:{"Content-Type":x,"Content-Length":F.toString()},body:r});if(!c.ok){let $=await c.text().catch(()=>c.statusText);throw new f(`Failed to upload file to R2: ${$}`,c.status)}return{id:_,url:P}}async uploadStream(r,h,x,F){let{url:O,mediaUrl:P,mediaId:_}=await this.generateSignedUrl(h,x,F),c=await fetch(O,{method:"PUT",headers:{"Content-Type":h,"Content-Length":x.toString()},body:r,duplex:"half"});if(!c.ok){let $=await c.text().catch(()=>c.statusText);throw new f(`Failed to upload stream to R2: ${$}`,c.status)}return{id:_,url:P}}async delete(r){let h=await this.fetch(`storage/${r}`,{method:"DELETE"});return A(h)}}class q{fetcher;constructor(r){if(typeof r==="string")r={apiKey:r};this.fetcher=new K({apiKey:r.apiKey,baseUrl:r.baseUrl,basePath:r.basePath,retries:r.retries,retryDelayMs:r.retryDelayMs})}_storage;get storage(){return this._storage??=new B(this.fetcher)}}var e=q;export{e as default,f as NoCloudAPIError,q as NoCloud};
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};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nocloud/sdk",
3
- "version": "0.1.1",
3
+ "version": "0.1.12",
4
4
  "description": "Official SDK for NoCloud services - file storage and more",
5
5
  "author": {
6
6
  "name": "sinanovicanes",
@@ -55,10 +55,12 @@
55
55
  "build:types": "bun x dts-bundle-generator -o ./dist/index.d.ts ./src/index.ts --project ./tsconfig.build.json --no-check",
56
56
  "clean": "rm -rf dist",
57
57
  "prepublishOnly": "bun run build",
58
- "typecheck": "tsc --noEmit"
58
+ "typecheck": "tsc --noEmit",
59
+ "test": "bun test"
59
60
  },
60
61
  "private": false,
61
62
  "devDependencies": {
63
+ "@types/bun": "^1.3.6",
62
64
  "@types/node": "^22.0.0",
63
65
  "typescript": "^5.7.0"
64
66
  },