@labeg/tfetch 0.4.0 → 0.6.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/contructors/TsFetch.js +33 -5
- package/dist/index.d.ts +1 -1
- package/dist/index.js +2 -2
- package/package.json +4 -3
|
@@ -10,7 +10,6 @@ export class TsFetch {
|
|
|
10
10
|
// eslint-disable-next-line max-lines-per-function, max-statements, complexity
|
|
11
11
|
async send(options) {
|
|
12
12
|
const { url, body, returnType, ...otherInits } = options;
|
|
13
|
-
const headers = options.headers ?? this.setHeaders();
|
|
14
13
|
const input = url;
|
|
15
14
|
/**
|
|
16
15
|
* Setup cache
|
|
@@ -28,6 +27,32 @@ export class TsFetch {
|
|
|
28
27
|
}
|
|
29
28
|
this.requestCache.set(cacheKey, []);
|
|
30
29
|
}
|
|
30
|
+
/**
|
|
31
|
+
* Prepare headers
|
|
32
|
+
*/
|
|
33
|
+
let sendBody = void 0;
|
|
34
|
+
const headers = this.setHeaders();
|
|
35
|
+
if (body instanceof ArrayBuffer ||
|
|
36
|
+
body instanceof Uint8Array ||
|
|
37
|
+
body instanceof Blob ||
|
|
38
|
+
body instanceof FormData ||
|
|
39
|
+
// NodeJS.ArrayBufferView
|
|
40
|
+
body instanceof URLSearchParams ||
|
|
41
|
+
body === null ||
|
|
42
|
+
typeof body === "string") {
|
|
43
|
+
// Fetch add needed headers self
|
|
44
|
+
sendBody = body;
|
|
45
|
+
}
|
|
46
|
+
else if (typeof body === "object") {
|
|
47
|
+
headers.set("content-type", "application/json");
|
|
48
|
+
sendBody = JSON.stringify(body);
|
|
49
|
+
}
|
|
50
|
+
if (options.headers) {
|
|
51
|
+
const optHeaders = new Headers(options.headers);
|
|
52
|
+
for (const entry of optHeaders.entries()) {
|
|
53
|
+
headers.set(entry[0], entry[1]);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
31
56
|
/**
|
|
32
57
|
* Process request
|
|
33
58
|
*/
|
|
@@ -36,7 +61,7 @@ export class TsFetch {
|
|
|
36
61
|
try {
|
|
37
62
|
let response = await fetch(input, {
|
|
38
63
|
method: options.method,
|
|
39
|
-
body:
|
|
64
|
+
body: sendBody,
|
|
40
65
|
headers,
|
|
41
66
|
...otherInits
|
|
42
67
|
});
|
|
@@ -68,7 +93,12 @@ export class TsFetch {
|
|
|
68
93
|
else if (Array.isArray(returnType) &&
|
|
69
94
|
responseText.startsWith("[")) {
|
|
70
95
|
data = JSON.parse(responseText);
|
|
71
|
-
|
|
96
|
+
if ( // If its Serializable class
|
|
97
|
+
typeof returnType[0] === "function" &&
|
|
98
|
+
returnType[0].prototype instanceof Serializable) {
|
|
99
|
+
const constructor = returnType[0];
|
|
100
|
+
data = data.map((model) => new constructor().fromJSON(model));
|
|
101
|
+
}
|
|
72
102
|
}
|
|
73
103
|
else if (typeof returnType === "function" &&
|
|
74
104
|
returnType.prototype instanceof Serializable &&
|
|
@@ -152,8 +182,6 @@ export class TsFetch {
|
|
|
152
182
|
}
|
|
153
183
|
setHeaders() {
|
|
154
184
|
const headers = new Headers();
|
|
155
|
-
headers.set("content-type", "application/json");
|
|
156
|
-
headers.set("Pragma", "no-cache");
|
|
157
185
|
return headers;
|
|
158
186
|
}
|
|
159
187
|
parseBackendError(response, body) {
|
package/dist/index.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ export * from "./contructors/TsFetch.js";
|
|
|
3
3
|
export * from "./contructors/CrudHttpRepository.js";
|
|
4
4
|
import { TsFetch } from "./contructors/TsFetch.js";
|
|
5
5
|
declare const instance: TsFetch;
|
|
6
|
-
export declare const
|
|
6
|
+
export declare const tfetch: typeof instance.send;
|
|
7
7
|
export declare const tf: {
|
|
8
8
|
(options: import("./contructors/TsFetch.js").TsRequestInit<void>): Promise<void>;
|
|
9
9
|
<T extends boolean>(options: import("./contructors/TsFetch.js").TsRequestInit<T>): Promise<T>;
|
package/dist/index.js
CHANGED
|
@@ -4,5 +4,5 @@ export * from "./contructors/CrudHttpRepository.js";
|
|
|
4
4
|
import { TsFetch } from "./contructors/TsFetch.js";
|
|
5
5
|
const instance = new TsFetch();
|
|
6
6
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-return
|
|
7
|
-
export const
|
|
8
|
-
export const tf =
|
|
7
|
+
export const tfetch = (options) => instance.send(options);
|
|
8
|
+
export const tf = tfetch; // Alias
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@labeg/tfetch",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"author": "Eugene Labutin",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://github.com/LabEG/ts-fetch#readme",
|
|
@@ -42,11 +42,12 @@
|
|
|
42
42
|
"@swc-node/register": "^1.10.9",
|
|
43
43
|
"@types/chai": "^5.0.1",
|
|
44
44
|
"chai": "^5.1.2",
|
|
45
|
+
"fastify": "^5.2.2",
|
|
45
46
|
"husky": "^9.1.7",
|
|
46
47
|
"lint-staged": "^15.3.0",
|
|
47
|
-
"ts-serializable": "^3.7.3",
|
|
48
|
-
"rimraf": "^6.0.1",
|
|
49
48
|
"npm-check-updates": "^17.1.13",
|
|
49
|
+
"rimraf": "^6.0.1",
|
|
50
|
+
"ts-serializable": "^3.7.3",
|
|
50
51
|
"typescript": "^5.7.2"
|
|
51
52
|
},
|
|
52
53
|
"keywords": [
|