@scirexs/fetchy 0.2.0 → 0.2.1
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/README.md +14 -13
- package/esm/main.js +25 -6
- package/package.json +1 -1
- package/types/main.d.ts +10 -4
- package/types/main.d.ts.map +1 -1
package/README.md
CHANGED
|
@@ -274,29 +274,30 @@ const response = await fetchy("https://api.example.com/data", {
|
|
|
274
274
|
```ts
|
|
275
275
|
import { fetchy, HTTPStatusError, RedirectError } from "@scirexs/fetchy";
|
|
276
276
|
|
|
277
|
-
//
|
|
278
|
-
const data = await fetchyb("https://api.example.com/data", "json");
|
|
279
|
-
if (data === null) {
|
|
280
|
-
console.log("Request failed");
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
// Throw on error
|
|
277
|
+
// Throw on error (default is same with `fetch`)
|
|
284
278
|
try {
|
|
285
|
-
const
|
|
286
|
-
throwError: true
|
|
287
|
-
});
|
|
279
|
+
const response = await fetchy("https://api.example.com/data");
|
|
288
280
|
} catch (error) {
|
|
289
281
|
console.error("Request failed:", error);
|
|
290
282
|
}
|
|
291
283
|
|
|
284
|
+
// Return null on error
|
|
285
|
+
const response = await fetchy("https://api.example.com/data", {
|
|
286
|
+
throwError: false,
|
|
287
|
+
});
|
|
288
|
+
if (response === null) {
|
|
289
|
+
console.log("Request failed");
|
|
290
|
+
}
|
|
291
|
+
|
|
292
292
|
// Throw only on HTTP errors
|
|
293
293
|
try {
|
|
294
|
-
const
|
|
295
|
-
throwError: { onErrorStatus: true }
|
|
294
|
+
const response = await fetchy("https://api.example.com/data", {
|
|
295
|
+
throwError: { onError: false, onErrorStatus: true }
|
|
296
296
|
});
|
|
297
297
|
} catch (error) {
|
|
298
298
|
if (error instanceof HTTPStatusError) {
|
|
299
|
-
|
|
299
|
+
// You can also use error.status and error.body.
|
|
300
|
+
console.error("HTTP error:", error.message); // e.g., "404 Not Found: (no response body)"
|
|
300
301
|
}
|
|
301
302
|
}
|
|
302
303
|
|
package/esm/main.js
CHANGED
|
@@ -17,7 +17,7 @@ const DEFAULT = {
|
|
|
17
17
|
};
|
|
18
18
|
/*=============== Main Code =====================*/
|
|
19
19
|
/**
|
|
20
|
-
* Error thrown when HTTP response has a non-OK status code (4xx, 5xx).
|
|
20
|
+
* Error thrown when HTTP response has a non-OK status code (4xx, 5xx, ...).
|
|
21
21
|
* Only thrown when throwError.onErrorStatus is set to true.
|
|
22
22
|
*
|
|
23
23
|
* @example
|
|
@@ -28,15 +28,28 @@ const DEFAULT = {
|
|
|
28
28
|
* });
|
|
29
29
|
* } catch (error) {
|
|
30
30
|
* if (error instanceof HTTPStatusError) {
|
|
31
|
-
* console.error("HTTP error:", error.message); // e.g., "
|
|
31
|
+
* console.error("HTTP error:", error.message); // e.g., "403 Forbidden: {success:false}"
|
|
32
32
|
* }
|
|
33
33
|
* }
|
|
34
34
|
* ```
|
|
35
35
|
*/
|
|
36
36
|
class HTTPStatusError extends Error {
|
|
37
|
-
|
|
37
|
+
static #MAX_BODY_LEN = 80;
|
|
38
|
+
status;
|
|
39
|
+
body;
|
|
40
|
+
constructor(msg, status, body) {
|
|
38
41
|
super(msg);
|
|
39
42
|
this.name = "HTTPStatusError";
|
|
43
|
+
this.status = status;
|
|
44
|
+
this.body = body;
|
|
45
|
+
}
|
|
46
|
+
static async fromResponse(resp) {
|
|
47
|
+
const body = await resp.text();
|
|
48
|
+
const bodyMsg = body.length > this.#MAX_BODY_LEN
|
|
49
|
+
? `${body.slice(0, this.#MAX_BODY_LEN)}... (more ${body.length - this.#MAX_BODY_LEN} chars)`
|
|
50
|
+
: body || "(no response body)";
|
|
51
|
+
const msg = `${resp.status} ${resp.statusText}: ${bodyMsg}`;
|
|
52
|
+
return new this(msg, resp.status, body);
|
|
40
53
|
}
|
|
41
54
|
}
|
|
42
55
|
/**
|
|
@@ -56,9 +69,15 @@ class HTTPStatusError extends Error {
|
|
|
56
69
|
* ```
|
|
57
70
|
*/
|
|
58
71
|
class RedirectError extends Error {
|
|
59
|
-
|
|
72
|
+
status;
|
|
73
|
+
constructor(msg, status) {
|
|
60
74
|
super(msg);
|
|
61
75
|
this.name = "RedirectError";
|
|
76
|
+
this.status = status;
|
|
77
|
+
}
|
|
78
|
+
static fromResponse(resp) {
|
|
79
|
+
const msg = `${resp.status} ${resp.statusText}`.trim();
|
|
80
|
+
return new this(msg, resp.status);
|
|
62
81
|
}
|
|
63
82
|
}
|
|
64
83
|
async function fetchyb(url, type = "auto", options) {
|
|
@@ -117,7 +136,7 @@ async function fetchy(url, options) {
|
|
|
117
136
|
const init = _getRequestInit(options, opts.abort);
|
|
118
137
|
const resp = await _fetchWithRetry(url, init, opts);
|
|
119
138
|
if (!resp.ok && opts.onErrorStatus)
|
|
120
|
-
throw
|
|
139
|
+
throw await HTTPStatusError.fromResponse(resp);
|
|
121
140
|
return resp;
|
|
122
141
|
}
|
|
123
142
|
catch (e) {
|
|
@@ -323,7 +342,7 @@ async function _shouldNotRetry(count, opts, resp) {
|
|
|
323
342
|
return true;
|
|
324
343
|
if (opts.userRedirect === "error") {
|
|
325
344
|
opts.max = 0;
|
|
326
|
-
throw
|
|
345
|
+
throw RedirectError.fromResponse(resp);
|
|
327
346
|
}
|
|
328
347
|
}
|
|
329
348
|
const interval = _getNextInterval(count, opts, resp);
|
package/package.json
CHANGED
package/types/main.d.ts
CHANGED
|
@@ -27,7 +27,7 @@ interface Options {
|
|
|
27
27
|
userRedirect: "follow" | "error" | "manual";
|
|
28
28
|
}
|
|
29
29
|
/**
|
|
30
|
-
* Error thrown when HTTP response has a non-OK status code (4xx, 5xx).
|
|
30
|
+
* Error thrown when HTTP response has a non-OK status code (4xx, 5xx, ...).
|
|
31
31
|
* Only thrown when throwError.onErrorStatus is set to true.
|
|
32
32
|
*
|
|
33
33
|
* @example
|
|
@@ -38,13 +38,17 @@ interface Options {
|
|
|
38
38
|
* });
|
|
39
39
|
* } catch (error) {
|
|
40
40
|
* if (error instanceof HTTPStatusError) {
|
|
41
|
-
* console.error("HTTP error:", error.message); // e.g., "
|
|
41
|
+
* console.error("HTTP error:", error.message); // e.g., "403 Forbidden: {success:false}"
|
|
42
42
|
* }
|
|
43
43
|
* }
|
|
44
44
|
* ```
|
|
45
45
|
*/
|
|
46
46
|
declare class HTTPStatusError extends Error {
|
|
47
|
-
|
|
47
|
+
#private;
|
|
48
|
+
status: number;
|
|
49
|
+
body: string;
|
|
50
|
+
constructor(msg: string, status: number, body: string);
|
|
51
|
+
static fromResponse(resp: Response): Promise<InstanceType<typeof this>>;
|
|
48
52
|
}
|
|
49
53
|
/**
|
|
50
54
|
* Error thrown when a redirect response is received and redirect option is set to "error".
|
|
@@ -63,7 +67,9 @@ declare class HTTPStatusError extends Error {
|
|
|
63
67
|
* ```
|
|
64
68
|
*/
|
|
65
69
|
declare class RedirectError extends Error {
|
|
66
|
-
|
|
70
|
+
status: number;
|
|
71
|
+
constructor(msg: string, status: number);
|
|
72
|
+
static fromResponse(resp: Response): InstanceType<typeof this>;
|
|
67
73
|
}
|
|
68
74
|
/**
|
|
69
75
|
* Performs an HTTP request and automatically parses the response body based on Content-Type or specified type.
|
package/types/main.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,gBAAgB,EAChB,eAAe,EACf,iBAAiB,EACjB,QAAQ,EACR,eAAe,EACf,WAAW,EACX,gBAAgB,EAChB,WAAW,EACX,eAAe,EACf,eAAe,EACf,uBAAuB,EACvB,OAAO,EACP,aAAa,EACb,SAAS,EACT,cAAc,EACd,SAAS,EACT,gBAAgB,EAChB,eAAe,EACf,eAAe,EACf,WAAW,EACX,KAAK,EACL,OAAO,EACP,MAAM,EACN,OAAO,EACP,eAAe,EACf,aAAa,GACd,CAAC;AAEF,OAAO,KAAK,EAAE,YAAY,EAAE,UAAU,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,YAAY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