@nocloud/sdk 0.1.1 → 0.1.11

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:P,getOwnPropertyNames:q,getOwnPropertyDescriptor:u}=Object,G=Object.prototype.hasOwnProperty;var v=new WeakMap,L=(h)=>{var F=v.get(h),c;if(F)return F;if(F=P({},"__esModule",{value:!0}),h&&typeof h==="object"||typeof h==="function")q(h).map((x)=>!G.call(F,x)&&P(F,x,{get:()=>h[x],enumerable:!(c=u(h,x))||c.enumerable}));return v.set(h,F),F};var k=(h,F)=>{for(var c in F)P(h,c,{get:F[c],enumerable:!0,configurable:!0,set:(x)=>F[c]=()=>x})};var J={};k(J,{default:()=>g,NoCloudAPIError:()=>O,NoCloud:()=>A});module.exports=L(J);var C="https://api.nonefivem.com",Q="/cloud";class O 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,c=1000){let x;for(let f=0;f<=F;f++)try{return await h()}catch(K){if(x=K,f<F)await w(c)}throw x}var E={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[c,x]of Object.entries(E))if(h.startsWith(c))return x;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),c=new Uint8Array(F.length);for(let x=0;x<F.length;x++)c[x]=F.charCodeAt(x);return c}function Z(h){return h.split(";")[0]?.trim()??h}class r{baseUrl;basePath;apiKey;retryCount;retryDelayMs;constructor(h){if(this.apiKey=h.apiKey,this.baseUrl=(h.baseUrl||C).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 c=this.buildUrl(h);return V(()=>fetch(c,{...F,headers:{Authorization:`Bearer ${this.apiKey}`,...F.headers||{}}}),F.retries??this.retryCount,F.retryDelayMs??this.retryDelayMs)}}async function U(h){if(!h.ok){let F=await h.json().catch(()=>({}));throw new O(F.message||"Unknown error",h.status)}return await h.json()}class B{fetcher;constructor(h){this.fetcher=h}fetch(h,F){return this.fetcher.fetch(h,F)}}class H extends B{async generateSignedUrl(h,F,c){let x=new URLSearchParams;x.append("contentType",h),x.append("size",F.toString());let f=await this.fetch(`storage/signed-url?${x.toString()}`);return U(f)}getBodyInfo(h){if(typeof Blob<"u"&&h instanceof Blob)return{contentType:Z(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 x=X(h),f=Y(x);return{contentType:F,size:f.byteLength,normalizedBody:new Blob([f.buffer])}}let c=new TextEncoder().encode(h);return{contentType:"text/plain",size:c.length,normalizedBody:new Blob([c.buffer])}}throw new O("Unsupported body type",400)}async upload(h,F){let{contentType:c,size:x,normalizedBody:f}=this.getBodyInfo(h),{url:K,mediaUrl:D,mediaId:_}=await this.generateSignedUrl(c,x,F),$=await fetch(K,{method:"PUT",headers:{"Content-Length":x.toString()},body:f});if(!$.ok){let j=await $.text().catch(()=>$.statusText);throw new O(`Failed to upload file to R2: ${j}`,$.status)}return{id:_,url:D}}async uploadStream(h,F,c,x){let{url:f,mediaUrl:K,mediaId:D}=await this.generateSignedUrl(F,c,x),_=await fetch(f,{method:"PUT",headers:{"Content-Length":c.toString()},body:h,duplex:"half"});if(!_.ok){let $=await _.text().catch(()=>_.statusText);throw new O(`Failed to upload stream to R2: ${$}`,_.status)}return{id:D,url:K}}async delete(h){let F=await this.fetch(`storage/${h}`,{method:"DELETE"});await U(F)}}class A{fetcher;constructor(h){if(typeof h==="string")h={apiKey:h};this.fetcher=new r({apiKey:h.apiKey,baseUrl:h.baseUrl,basePath:h.basePath,retries:h.retries,retryDelayMs:h.retryDelayMs})}_storage;get storage(){return this._storage??=new H(this.fetcher)}}var g=A;
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
  /**
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 H="https://api.nonefivem.com",v="/cloud";class O 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 C(h,F=3,c=1000){let x;for(let f=0;f<=F;f++)try{return await h()}catch(K){if(x=K,f<F)await Z(c)}throw x}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 Q(h){let F=h.match(/^data:([^;,]+)/);if(F?.[1])return F[1];for(let[c,x]of Object.entries(j))if(h.startsWith(c))return x;return null}function V(h){let F=h.match(/^data:[^;,]+;base64,(.+)$/);if(F?.[1])return F[1];return h}function W(h){let F=atob(h),c=new Uint8Array(F.length);for(let x=0;x<F.length;x++)c[x]=F.charCodeAt(x);return c}function X(h){return h.split(";")[0]?.trim()??h}class D{baseUrl;basePath;apiKey;retryCount;retryDelayMs;constructor(h){if(this.apiKey=h.apiKey,this.baseUrl=(h.baseUrl||H).replace(/\/+$/,""),this.basePath=(h.basePath??v).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 c=this.buildUrl(h);return C(()=>fetch(c,{...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 O(F.message||"Unknown error",h.status)}return await h.json()}class r{fetcher;constructor(h){this.fetcher=h}fetch(h,F){return this.fetcher.fetch(h,F)}}class U extends r{async generateSignedUrl(h,F,c){let x=new URLSearchParams;x.append("contentType",h),x.append("size",F.toString());let f=await this.fetch(`storage/signed-url?${x.toString()}`);return P(f)}getBodyInfo(h){if(typeof Blob<"u"&&h instanceof Blob)return{contentType:X(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=Q(h);if(F){let x=V(h),f=W(x);return{contentType:F,size:f.byteLength,normalizedBody:new Blob([f.buffer])}}let c=new TextEncoder().encode(h);return{contentType:"text/plain",size:c.length,normalizedBody:new Blob([c.buffer])}}throw new O("Unsupported body type",400)}async upload(h,F){let{contentType:c,size:x,normalizedBody:f}=this.getBodyInfo(h),{url:K,mediaUrl:A,mediaId:_}=await this.generateSignedUrl(c,x,F),$=await fetch(K,{method:"PUT",headers:{"Content-Length":x.toString()},body:f});if(!$.ok){let Y=await $.text().catch(()=>$.statusText);throw new O(`Failed to upload file to R2: ${Y}`,$.status)}return{id:_,url:A}}async uploadStream(h,F,c,x){let{url:f,mediaUrl:K,mediaId:A}=await this.generateSignedUrl(F,c,x),_=await fetch(f,{method:"PUT",headers:{"Content-Length":c.toString()},body:h,duplex:"half"});if(!_.ok){let $=await _.text().catch(()=>_.statusText);throw new O(`Failed to upload stream to R2: ${$}`,_.status)}return{id:A,url:K}}async delete(h){let F=await this.fetch(`storage/${h}`,{method:"DELETE"});await P(F)}}class B{fetcher;constructor(h){if(typeof h==="string")h={apiKey:h};this.fetcher=new D({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=B;export{hh as default,O as NoCloudAPIError,B 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.11",
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
  },