@ooneex/fetcher 0.13.0 → 0.14.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.js +2 -128
- package/dist/index.js.map +2 -2
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,129 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
import { Header } from "@ooneex/http-header";
|
|
1
|
+
import{Header as c}from"@ooneex/http-header";class t{baseURL;abortController;header=new c;constructor(e){this.baseURL=e;this.abortController=new AbortController}setBearerToken(e){return this.header.setBearerToken(e),this}setBasicToken(e){return this.header.setBasicAuth(e),this}clearBearerToken(){return this.header.remove("Authorization"),this}clearBasicToken(){return this.header.remove("Authorization"),this}setContentType(e){return this.header.contentType(e),this}setLang(e){return this.header.setLang(e),this}abort(){return this.abortController.abort(),this.abortController=new AbortController,this}clone(){return new t(this.baseURL)}async get(e){return this.request("GET",e)}async post(e,n){return this.request("POST",e,n)}async put(e,n){return this.request("PUT",e,n)}async patch(e,n){return this.request("PATCH",e,n)}async delete(e){return this.request("DELETE",e)}async head(e){return this.request("HEAD",e)}async options(e){return this.request("OPTIONS",e)}async request(e,n,r){let s=this.buildURL(n),o=this.buildRequestOptions(e,r),i=await fetch(s,o);try{return await i.json()}catch(T){throw Error(T instanceof Error?T.message:"Failed to parse JSON response")}}async upload(e,n,r="file"){let s=new FormData;s.append(r,n);let o=this.header.get("Content-Type");this.header.remove("Content-Type");let i=await this.request("POST",e,s);if(o)this.header.set("Content-Type",o);return i}buildURL(e){if(e.startsWith("http://")||e.startsWith("https://"))return e;if(!this.baseURL)return e;let n=e.startsWith("/")?e:`/${e}`;return`${this.baseURL.endsWith("/")?this.baseURL.slice(0,-1):this.baseURL}${n}`}buildRequestOptions(e,n){let r=this.header.native,s;if(n!==void 0&&e!=="GET"&&e!=="HEAD"&&e!=="OPTIONS"){if(n instanceof FormData)s=n,this.header.clearContentType();else if(typeof n==="string")s=n;else if(n instanceof Blob||n instanceof ArrayBuffer)s=n;else if(s=JSON.stringify(n),!this.header.has("Content-Type"))this.header.setJson()}return{method:e,headers:r,...s!==void 0&&{body:s},signal:this.abortController.signal}}}export{t as Fetcher};
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
baseURL;
|
|
6
|
-
abortController;
|
|
7
|
-
header = new Header;
|
|
8
|
-
constructor(baseURL) {
|
|
9
|
-
this.baseURL = baseURL;
|
|
10
|
-
this.abortController = new AbortController;
|
|
11
|
-
}
|
|
12
|
-
setBearerToken(token) {
|
|
13
|
-
this.header.setBearerToken(token);
|
|
14
|
-
return this;
|
|
15
|
-
}
|
|
16
|
-
setBasicToken(token) {
|
|
17
|
-
this.header.setBasicAuth(token);
|
|
18
|
-
return this;
|
|
19
|
-
}
|
|
20
|
-
clearBearerToken() {
|
|
21
|
-
this.header.remove("Authorization");
|
|
22
|
-
return this;
|
|
23
|
-
}
|
|
24
|
-
clearBasicToken() {
|
|
25
|
-
this.header.remove("Authorization");
|
|
26
|
-
return this;
|
|
27
|
-
}
|
|
28
|
-
setContentType(contentType) {
|
|
29
|
-
this.header.contentType(contentType);
|
|
30
|
-
return this;
|
|
31
|
-
}
|
|
32
|
-
setLang(lang) {
|
|
33
|
-
this.header.setLang(lang);
|
|
34
|
-
return this;
|
|
35
|
-
}
|
|
36
|
-
abort() {
|
|
37
|
-
this.abortController.abort();
|
|
38
|
-
this.abortController = new AbortController;
|
|
39
|
-
return this;
|
|
40
|
-
}
|
|
41
|
-
clone() {
|
|
42
|
-
const cloned = new Fetcher(this.baseURL);
|
|
43
|
-
return cloned;
|
|
44
|
-
}
|
|
45
|
-
async get(path) {
|
|
46
|
-
return this.request("GET", path);
|
|
47
|
-
}
|
|
48
|
-
async post(path, data) {
|
|
49
|
-
return this.request("POST", path, data);
|
|
50
|
-
}
|
|
51
|
-
async put(path, data) {
|
|
52
|
-
return this.request("PUT", path, data);
|
|
53
|
-
}
|
|
54
|
-
async patch(path, data) {
|
|
55
|
-
return this.request("PATCH", path, data);
|
|
56
|
-
}
|
|
57
|
-
async delete(path) {
|
|
58
|
-
return this.request("DELETE", path);
|
|
59
|
-
}
|
|
60
|
-
async head(path) {
|
|
61
|
-
return this.request("HEAD", path);
|
|
62
|
-
}
|
|
63
|
-
async options(path) {
|
|
64
|
-
return this.request("OPTIONS", path);
|
|
65
|
-
}
|
|
66
|
-
async request(method, path, data) {
|
|
67
|
-
const fullURL = this.buildURL(path);
|
|
68
|
-
const requestOptions = this.buildRequestOptions(method, data);
|
|
69
|
-
const response = await fetch(fullURL, requestOptions);
|
|
70
|
-
try {
|
|
71
|
-
return await response.json();
|
|
72
|
-
} catch (error) {
|
|
73
|
-
throw new Error(error instanceof Error ? error.message : "Failed to parse JSON response");
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
async upload(path, file, name = "file") {
|
|
77
|
-
const formData = new FormData;
|
|
78
|
-
formData.append(name, file);
|
|
79
|
-
const originalContentType = this.header.get("Content-Type");
|
|
80
|
-
this.header.remove("Content-Type");
|
|
81
|
-
const result = await this.request("POST", path, formData);
|
|
82
|
-
if (originalContentType) {
|
|
83
|
-
this.header.set("Content-Type", originalContentType);
|
|
84
|
-
}
|
|
85
|
-
return result;
|
|
86
|
-
}
|
|
87
|
-
buildURL(url) {
|
|
88
|
-
if (url.startsWith("http://") || url.startsWith("https://")) {
|
|
89
|
-
return url;
|
|
90
|
-
}
|
|
91
|
-
if (!this.baseURL) {
|
|
92
|
-
return url;
|
|
93
|
-
}
|
|
94
|
-
const path = url.startsWith("/") ? url : `/${url}`;
|
|
95
|
-
const baseURL = this.baseURL.endsWith("/") ? this.baseURL.slice(0, -1) : this.baseURL;
|
|
96
|
-
return `${baseURL}${path}`;
|
|
97
|
-
}
|
|
98
|
-
buildRequestOptions(method, data) {
|
|
99
|
-
const headers = this.header.native;
|
|
100
|
-
let body;
|
|
101
|
-
if (data !== undefined && method !== "GET" && method !== "HEAD" && method !== "OPTIONS") {
|
|
102
|
-
if (data instanceof FormData) {
|
|
103
|
-
body = data;
|
|
104
|
-
this.header.clearContentType();
|
|
105
|
-
} else if (typeof data === "string") {
|
|
106
|
-
body = data;
|
|
107
|
-
} else if (data instanceof Blob || data instanceof ArrayBuffer) {
|
|
108
|
-
body = data;
|
|
109
|
-
} else {
|
|
110
|
-
body = JSON.stringify(data);
|
|
111
|
-
if (!this.header.has("Content-Type")) {
|
|
112
|
-
this.header.setJson();
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
const requestOptions = {
|
|
117
|
-
method,
|
|
118
|
-
headers,
|
|
119
|
-
...body !== undefined && { body },
|
|
120
|
-
signal: this.abortController.signal
|
|
121
|
-
};
|
|
122
|
-
return requestOptions;
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
export {
|
|
126
|
-
Fetcher
|
|
127
|
-
};
|
|
128
|
-
|
|
129
|
-
//# debugId=4387EBAC0815D5D364756E2164756E21
|
|
3
|
+
//# debugId=FC0FC58E0FCC4CA964756E2164756E21
|
package/dist/index.js.map
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"sourcesContent": [
|
|
5
5
|
"import { Header } from \"@ooneex/http-header\";\nimport type { MimeType } from \"@ooneex/http-mimes\";\nimport type { ResponseDataType } from \"@ooneex/http-response\";\nimport type { HttpMethodType } from \"@ooneex/types\";\nimport type { IFetcher } from \"./types\";\n\nexport class Fetcher implements IFetcher {\n private abortController: AbortController;\n public readonly header: Header = new Header();\n\n constructor(private baseURL?: string) {\n this.abortController = new AbortController();\n }\n\n public setBearerToken(token: string): this {\n this.header.setBearerToken(token);\n\n return this;\n }\n\n public setBasicToken(token: string): this {\n this.header.setBasicAuth(token);\n\n return this;\n }\n\n public clearBearerToken(): this {\n this.header.remove(\"Authorization\");\n\n return this;\n }\n\n public clearBasicToken(): this {\n this.header.remove(\"Authorization\");\n\n return this;\n }\n\n public setContentType(contentType: MimeType): this {\n this.header.contentType(contentType);\n\n return this;\n }\n\n public setLang(lang: string): this {\n this.header.setLang(lang);\n\n return this;\n }\n\n public abort(): this {\n this.abortController.abort();\n this.abortController = new AbortController();\n\n return this;\n }\n\n public clone(): Fetcher {\n const cloned = new Fetcher(this.baseURL);\n return cloned;\n }\n\n public async get<T extends Record<string, unknown> = Record<string, unknown>>(\n path: string,\n ): Promise<ResponseDataType<T>> {\n return this.request<T>(\"GET\", path);\n }\n\n public async post<T extends Record<string, unknown> = Record<string, unknown>>(\n path: string,\n data?: unknown,\n ): Promise<ResponseDataType<T>> {\n return this.request<T>(\"POST\", path, data);\n }\n\n public async put<T extends Record<string, unknown> = Record<string, unknown>>(\n path: string,\n data?: unknown,\n ): Promise<ResponseDataType<T>> {\n return this.request<T>(\"PUT\", path, data);\n }\n\n public async patch<T extends Record<string, unknown> = Record<string, unknown>>(\n path: string,\n data?: unknown,\n ): Promise<ResponseDataType<T>> {\n return this.request<T>(\"PATCH\", path, data);\n }\n\n public async delete<T extends Record<string, unknown> = Record<string, unknown>>(\n path: string,\n ): Promise<ResponseDataType<T>> {\n return this.request<T>(\"DELETE\", path);\n }\n\n public async head<T extends Record<string, unknown> = Record<string, unknown>>(\n path: string,\n ): Promise<ResponseDataType<T>> {\n return this.request<T>(\"HEAD\", path);\n }\n\n public async options<T extends Record<string, unknown> = Record<string, unknown>>(\n path: string,\n ): Promise<ResponseDataType<T>> {\n return this.request<T>(\"OPTIONS\", path);\n }\n\n public async request<T extends Record<string, unknown> = Record<string, unknown>>(\n method: HttpMethodType,\n path: string,\n data?: unknown,\n ): Promise<ResponseDataType<T>> {\n const fullURL = this.buildURL(path);\n const requestOptions = this.buildRequestOptions(method, data);\n const response = await fetch(fullURL, requestOptions);\n\n try {\n return await response.json();\n } catch (error) {\n throw new Error(error instanceof Error ? error.message : \"Failed to parse JSON response\");\n }\n }\n\n public async upload<T extends Record<string, unknown> = Record<string, unknown>>(\n path: string,\n file: File | Blob,\n name = \"file\",\n ): Promise<ResponseDataType<T>> {\n const formData = new FormData();\n formData.append(name, file);\n\n // Clear any existing Content-Type to let browser set multipart/form-data boundary\n const originalContentType = this.header.get(\"Content-Type\");\n this.header.remove(\"Content-Type\");\n\n const result = await this.request<T>(\"POST\", path, formData);\n\n // Restore original Content-Type if it existed\n if (originalContentType) {\n this.header.set(\"Content-Type\", originalContentType);\n }\n\n return result;\n }\n\n private buildURL(url: string): string {\n if (url.startsWith(\"http://\") || url.startsWith(\"https://\")) {\n return url;\n }\n\n if (!this.baseURL) {\n return url;\n }\n\n const path = url.startsWith(\"/\") ? url : `/${url}`;\n const baseURL = this.baseURL.endsWith(\"/\") ? this.baseURL.slice(0, -1) : this.baseURL;\n\n return `${baseURL}${path}`;\n }\n\n private buildRequestOptions(method: string, data?: unknown): RequestInit {\n const headers = this.header.native;\n\n let body: BodyInit | undefined;\n\n if (data !== undefined && method !== \"GET\" && method !== \"HEAD\" && method !== \"OPTIONS\") {\n if (data instanceof FormData) {\n body = data;\n this.header.clearContentType();\n } else if (typeof data === \"string\") {\n body = data;\n } else if (data instanceof Blob || data instanceof ArrayBuffer) {\n body = data;\n } else {\n body = JSON.stringify(data);\n // Set Content-Type to application/json if not already set\n if (!this.header.has(\"Content-Type\")) {\n this.header.setJson();\n }\n }\n }\n\n const requestOptions: RequestInit = {\n method,\n headers,\n ...(body !== undefined && { body }),\n signal: this.abortController.signal,\n };\n\n return requestOptions;\n }\n}\n"
|
|
6
6
|
],
|
|
7
|
-
"mappings": "
|
|
8
|
-
"debugId": "
|
|
7
|
+
"mappings": "AAAA,iBAAS,4BAMF,MAAM,CAA4B,CAInB,QAHZ,gBACQ,OAAiB,IAAI,EAErC,WAAW,CAAS,EAAkB,CAAlB,eAClB,KAAK,gBAAkB,IAAI,gBAGtB,cAAc,CAAC,EAAqB,CAGzC,OAFA,KAAK,OAAO,eAAe,CAAK,EAEzB,KAGF,aAAa,CAAC,EAAqB,CAGxC,OAFA,KAAK,OAAO,aAAa,CAAK,EAEvB,KAGF,gBAAgB,EAAS,CAG9B,OAFA,KAAK,OAAO,OA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