@nocloud/sdk 0.1.0 → 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 +1 -1
- package/dist/index.d.ts +10 -2
- package/dist/index.js +1 -1
- package/package.json +4 -2
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var{defineProperty:
|
|
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
|
@@ -7,10 +7,12 @@ export interface FetchOptionsBase {
|
|
|
7
7
|
export interface FetcherOptions extends FetchOptionsBase {
|
|
8
8
|
apiKey: string;
|
|
9
9
|
baseUrl?: string;
|
|
10
|
+
basePath?: string;
|
|
10
11
|
}
|
|
11
12
|
export type FetchOptions = RequestInit & FetchOptionsBase;
|
|
12
13
|
declare class Fetcher {
|
|
13
14
|
private readonly baseUrl;
|
|
15
|
+
private readonly basePath;
|
|
14
16
|
private readonly apiKey;
|
|
15
17
|
private readonly retryCount;
|
|
16
18
|
private readonly retryDelayMs;
|
|
@@ -21,7 +23,7 @@ declare class Fetcher {
|
|
|
21
23
|
declare class SDKModule {
|
|
22
24
|
private readonly fetcher;
|
|
23
25
|
constructor(fetcher: Fetcher);
|
|
24
|
-
protected fetch(endpoint: string, options
|
|
26
|
+
protected fetch(endpoint: string, options?: FetchOptions): Promise<Response>;
|
|
25
27
|
}
|
|
26
28
|
/**
|
|
27
29
|
* Upload body types.
|
|
@@ -81,7 +83,8 @@ declare class Storage$1 extends SDKModule {
|
|
|
81
83
|
*/
|
|
82
84
|
generateSignedUrl(contentType: string, size: number, metadata?: FileMetadata): Promise<SignedUrlResponse>;
|
|
83
85
|
/**
|
|
84
|
-
* Extracts content type and
|
|
86
|
+
* Extracts content type, size, and normalized body from the input.
|
|
87
|
+
* For base64 strings, this decodes them to binary.
|
|
85
88
|
*/
|
|
86
89
|
private getBodyInfo;
|
|
87
90
|
/**
|
|
@@ -120,6 +123,11 @@ export interface NoCloudOptions {
|
|
|
120
123
|
* @default "https://api.nonefivem.com"
|
|
121
124
|
*/
|
|
122
125
|
baseUrl?: string;
|
|
126
|
+
/**
|
|
127
|
+
* Optional base path for API endpoints.
|
|
128
|
+
* @default "/cloud"
|
|
129
|
+
*/
|
|
130
|
+
basePath?: string;
|
|
123
131
|
/**
|
|
124
132
|
* Number of retry attempts for failed requests.
|
|
125
133
|
* @default 3
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
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.
|
|
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
|
},
|