@ripetchor/lana 1.0.0
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.d.ts +76 -0
- package/dist/index.js +1 -0
- package/package.json +31 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
//#region lib/interceptors/types.d.ts
|
|
2
|
+
interface LanaInterceptor<T> {
|
|
3
|
+
readonly id: number;
|
|
4
|
+
readonly onfulfilled?: (value: T) => T | Promise<T>;
|
|
5
|
+
readonly onrejected?: (error: unknown) => unknown;
|
|
6
|
+
}
|
|
7
|
+
//#endregion
|
|
8
|
+
//#region lib/interceptors/manager.d.ts
|
|
9
|
+
declare class LanaInterceptorManager<T> {
|
|
10
|
+
private readonly handlers;
|
|
11
|
+
private nextId;
|
|
12
|
+
use(onfulfilled?: (value: T) => T | Promise<T>, onrejected?: (error: unknown) => unknown): number;
|
|
13
|
+
eject(id: number): void;
|
|
14
|
+
clear(): void;
|
|
15
|
+
getAll(): readonly LanaInterceptor<T>[];
|
|
16
|
+
}
|
|
17
|
+
//#endregion
|
|
18
|
+
//#region lib/types.d.ts
|
|
19
|
+
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS';
|
|
20
|
+
type LanaQueryValue = string | number | boolean | null | undefined;
|
|
21
|
+
type LanaQueryParams = Readonly<Record<string, LanaQueryValue>>;
|
|
22
|
+
type LanaHeaders = Readonly<Record<string, string>>;
|
|
23
|
+
type LanaResponseType = 'json' | 'text' | 'blob' | 'arrayBuffer' | 'formData' | 'bytes';
|
|
24
|
+
interface LanaRequest<Body = unknown> extends Omit<RequestInit, 'body' | 'headers' | 'method'> {
|
|
25
|
+
readonly baseURL?: string;
|
|
26
|
+
readonly url: string;
|
|
27
|
+
readonly method?: HttpMethod;
|
|
28
|
+
readonly params?: LanaQueryParams;
|
|
29
|
+
readonly headers?: LanaHeaders;
|
|
30
|
+
readonly responseType?: LanaResponseType;
|
|
31
|
+
readonly body?: Body;
|
|
32
|
+
}
|
|
33
|
+
interface LanaClientConfig {
|
|
34
|
+
readonly baseURL?: string;
|
|
35
|
+
readonly headers?: LanaHeaders;
|
|
36
|
+
}
|
|
37
|
+
interface LanaResponse<Data> {
|
|
38
|
+
readonly data: Data;
|
|
39
|
+
readonly status: number;
|
|
40
|
+
readonly statusText: string;
|
|
41
|
+
readonly headers: globalThis.Headers;
|
|
42
|
+
}
|
|
43
|
+
//#endregion
|
|
44
|
+
//#region lib/lana.d.ts
|
|
45
|
+
declare class Lana {
|
|
46
|
+
private readonly baseURL?;
|
|
47
|
+
private readonly headers;
|
|
48
|
+
readonly interceptors: {
|
|
49
|
+
request: LanaInterceptorManager<LanaRequest<unknown>>;
|
|
50
|
+
response: LanaInterceptorManager<LanaResponse<unknown>>;
|
|
51
|
+
};
|
|
52
|
+
constructor(config?: LanaClientConfig);
|
|
53
|
+
request<Response = unknown, Body = unknown>(request: LanaRequest<Body>): Promise<LanaResponse<Response>>;
|
|
54
|
+
get<Response = unknown>(url: string, request?: Omit<LanaRequest<never>, 'url' | 'method'>): Promise<LanaResponse<Response>>;
|
|
55
|
+
post<Response = unknown, Body = unknown>(url: string, body: Body, request?: Omit<LanaRequest<Body>, 'url' | 'method' | 'body'>): Promise<LanaResponse<Response>>;
|
|
56
|
+
put<Response = unknown, Body = unknown>(url: string, body: Body, request?: Omit<LanaRequest<Body>, 'url' | 'method' | 'body'>): Promise<LanaResponse<Response>>;
|
|
57
|
+
patch<Response = unknown, Body = unknown>(url: string, body: Body, request?: Omit<LanaRequest<Body>, 'url' | 'method' | 'body'>): Promise<LanaResponse<Response>>;
|
|
58
|
+
delete<Response = unknown, Body = unknown>(url: string, body?: Body, request?: Omit<LanaRequest<never>, 'url' | 'method'>): Promise<LanaResponse<Response>>;
|
|
59
|
+
head<Response = unknown>(url: string, config?: Omit<LanaRequest<never>, 'url' | 'method'>): Promise<LanaResponse<Response>>;
|
|
60
|
+
options<Response = unknown>(url: string, config?: Omit<LanaRequest<never>, 'url' | 'method'>): Promise<LanaResponse<Response>>;
|
|
61
|
+
create(config?: LanaClientConfig): Lana;
|
|
62
|
+
private execute;
|
|
63
|
+
private buildHeaders;
|
|
64
|
+
private buildBody;
|
|
65
|
+
private buildUrl;
|
|
66
|
+
private parseResponse;
|
|
67
|
+
private parseBody;
|
|
68
|
+
private runRequestInterceptors;
|
|
69
|
+
private runResponseInterceptors;
|
|
70
|
+
private runResponseErrorInterceptors;
|
|
71
|
+
}
|
|
72
|
+
//#endregion
|
|
73
|
+
//#region lib/index.d.ts
|
|
74
|
+
declare const lana: Lana;
|
|
75
|
+
//#endregion
|
|
76
|
+
export { lana as default, lana };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var e=class extends Error{status;response;constructor(e,t,n){super(e),this.name=`LanaError`,this.status=t,this.response=n}},t=class{handlers=new Map;nextId=0;use(e,t){let n=this.nextId++;return this.handlers.set(n,{id:n,onfulfilled:e,onrejected:t}),n}eject(e){this.handlers.delete(e)}clear(){this.handlers.clear()}getAll(){return[...this.handlers.values()]}};const n={json:async e=>{let t=await e.text();return t?JSON.parse(t):null},text:e=>e.text(),blob:e=>e.blob(),bytes:e=>e.bytes(),formData:e=>e.formData(),arrayBuffer:e=>e.arrayBuffer()},r=new class r{baseURL;headers;interceptors={request:new t,response:new t};constructor(e){this.baseURL=e?.baseURL,this.headers=e?.headers??{}}request(e){return this.execute(e)}get(e,t={}){return this.execute({...t,url:e,method:`GET`})}post(e,t,n={}){return this.execute({...n,url:e,method:`POST`,body:t})}put(e,t,n={}){return this.execute({...n,url:e,method:`PUT`,body:t})}patch(e,t,n={}){return this.execute({...n,url:e,method:`PATCH`,body:t})}delete(e,t,n={}){return this.execute({...n,url:e,method:`DELETE`,body:t})}head(e,t={}){return this.execute({...t,url:e,method:`HEAD`})}options(e,t={}){return this.execute({...t,url:e,method:`OPTIONS`})}create(e){return new r(e)}async execute(t){try{let{baseURL:n,url:r,params:i,headers:a,responseType:o,body:s,method:c,...l}=await this.runRequestInterceptors(t),u=await fetch(this.buildUrl(n??this.baseURL,r,i),{...l,method:c,headers:this.buildHeaders(a),body:this.buildBody(s)}),d=await this.parseResponse(u,o);if(!u.ok)throw new e(u.statusText,u.status,d);return await this.runResponseInterceptors(d)}catch(e){throw await this.runResponseErrorInterceptors(e)}}buildHeaders(e){let t=new Headers;for(let[e,n]of Object.entries(this.headers))t.set(e,n);if(e!==void 0)for(let[n,r]of Object.entries(e))t.set(n,r);return t}buildBody(e){if(e!==void 0)return e instanceof FormData||e instanceof URLSearchParams||e instanceof Blob||e instanceof ArrayBuffer||e instanceof ReadableStream||typeof e==`string`?e:JSON.stringify(e)}buildUrl(e,t,n){let r=new URL(t,e);if(n!==void 0)for(let[e,t]of Object.entries(n))t!=null&&r.searchParams.set(e,String(t));return r.toString()}async parseResponse(e,t=`json`){return{data:await this.parseBody(e,t),status:e.status,statusText:e.statusText,headers:e.headers}}parseBody(e,t){return n[t](e)}async runRequestInterceptors(e){let t=e;for(let e of this.interceptors.request.getAll())try{e.onfulfilled!==void 0&&(t=await e.onfulfilled(t))}catch(t){throw e.onrejected!==void 0&&await e.onrejected(t),t}return t}async runResponseInterceptors(e){let t=e;for(let e of this.interceptors.response.getAll())e.onfulfilled!==void 0&&(t=await e.onfulfilled(t));return t}async runResponseErrorInterceptors(e){let t=e;for(let e of this.interceptors.response.getAll())e.onrejected!==void 0&&(t=await e.onrejected(t));return t}};export{r as default,r as lana};
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ripetchor/lana",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "ripetchor",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"main": "./dist/index.js",
|
|
11
|
+
"module": "./dist/index.js",
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": "./dist/index.js",
|
|
15
|
+
"./package.json": "./package.json"
|
|
16
|
+
},
|
|
17
|
+
"publishConfig": {
|
|
18
|
+
"access": "public"
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"dev": "tsdown --watch",
|
|
22
|
+
"build": "tsdown",
|
|
23
|
+
"preview": "vite preview",
|
|
24
|
+
"typecheck": "tsc --noEmit"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"tsdown": "^0.22.12",
|
|
28
|
+
"typescript": "~6.0.2",
|
|
29
|
+
"vite": "^8.1.1"
|
|
30
|
+
}
|
|
31
|
+
}
|