@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 +1 -1
- package/dist/index.d.ts +5 -4
- package/dist/index.js +1 -1
- package/package.json +4 -2
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var{defineProperty:A,getOwnPropertyNames:
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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.
|
|
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
|
},
|