@torthu/jacketui-bring 0.2.5 → 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/README.md +0 -2
- package/dist/bring.js +5 -4
- package/dist/errors/AbortError.js +1 -1
- package/dist/errors/ClientError.js +1 -1
- package/dist/errors/NetworkError.js +1 -1
- package/dist/errors/ServerError.js +1 -1
- package/dist/errors/TimeoutError.js +1 -1
- package/dist/errors/index.js +6 -6
- package/dist/fetch.js +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +6 -5
- package/dist/request-builder-pipe/headers.js +11 -2
- package/dist/request-builder-pipe/index.d.ts +1 -0
- package/dist/request-builder-pipe/index.js +20 -19
- package/dist/request-builder-pipe/jitter.js +1 -1
- package/dist/response-pipe/index.js +6 -6
- package/dist/tryFetch.js +17 -1
- package/dist/types/BringInit.d.ts +1 -3
- package/package.json +27 -9
- package/src/bring.ts +0 -207
- package/src/errors/AbortError.ts +0 -18
- package/src/errors/BringError.ts +0 -24
- package/src/errors/ClientError.ts +0 -24
- package/src/errors/NetworkError.ts +0 -23
- package/src/errors/ServerError.ts +0 -24
- package/src/errors/TimeoutError.ts +0 -18
- package/src/errors/index.ts +0 -6
- package/src/fetch.ts +0 -34
- package/src/helpers/exponentialBackoff.ts +0 -28
- package/src/helpers/extractCallbacks.ts +0 -23
- package/src/helpers/extractRequestInit.ts +0 -30
- package/src/helpers/randomJitter.ts +0 -22
- package/src/helpers/shouldRetry.ts +0 -40
- package/src/index.ts +0 -8
- package/src/request-builder-pipe/backoff.ts +0 -22
- package/src/request-builder-pipe/body.ts +0 -17
- package/src/request-builder-pipe/cache.ts +0 -23
- package/src/request-builder-pipe/cors.ts +0 -39
- package/src/request-builder-pipe/credentials.ts +0 -33
- package/src/request-builder-pipe/header.ts +0 -28
- package/src/request-builder-pipe/headers.ts +0 -34
- package/src/request-builder-pipe/index.ts +0 -20
- package/src/request-builder-pipe/integrity.ts +0 -23
- package/src/request-builder-pipe/jitter.ts +0 -17
- package/src/request-builder-pipe/jsonBody.ts +0 -12
- package/src/request-builder-pipe/keepalive.ts +0 -21
- package/src/request-builder-pipe/method.ts +0 -18
- package/src/request-builder-pipe/priority.ts +0 -20
- package/src/request-builder-pipe/redirect.ts +0 -23
- package/src/request-builder-pipe/referrer.ts +0 -21
- package/src/request-builder-pipe/referrerPolicy.ts +0 -33
- package/src/request-builder-pipe/retry.ts +0 -13
- package/src/request-builder-pipe/shouldRetry.ts +0 -11
- package/src/request-builder-pipe/timeout.ts +0 -19
- package/src/request-builder-pipe/url.ts +0 -18
- package/src/response-pipe/arrayBuffer.ts +0 -18
- package/src/response-pipe/blob.ts +0 -18
- package/src/response-pipe/bytes.ts +0 -18
- package/src/response-pipe/formData.ts +0 -18
- package/src/response-pipe/index.ts +0 -6
- package/src/response-pipe/json.ts +0 -20
- package/src/response-pipe/text.ts +0 -18
- package/src/tryFetch.ts +0 -32
- package/src/types/BringDecorator.ts +0 -5
- package/src/types/BringInit.ts +0 -86
- package/src/types/ClientErrorResponse.ts +0 -72
- package/src/types/InformationalResponse.ts +0 -20
- package/src/types/RedirectResponse.ts +0 -20
- package/src/types/ServerErrorResponse.ts +0 -56
- package/src/types/SuccessResponse.ts +0 -57
- package/src/types/statusCodes.ts +0 -69
- package/test/bring.spec.ts +0 -149
- package/tsconfig.json +0 -21
package/README.md
CHANGED
|
@@ -7,5 +7,3 @@ Bring is a fetch wrapper that provides a simple API for making HTTP requests. It
|
|
|
7
7
|
We want flexibility in our components and escape hatches in order to solve edge cases and make components truly expandable and reusable.
|
|
8
8
|
|
|
9
9
|
In order to achieve this we need to create flexible components, meaning expandable and full of escape hatches in order to cater for edge cases.
|
|
10
|
-
|
|
11
|
-
## In practice
|
package/dist/bring.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { success, failure, withResolvers, } from "@torthu/jacketui-core";
|
|
2
|
-
import { isClientErrorResponse } from "./types/ClientErrorResponse";
|
|
3
|
-
import { isServerErrorResponse } from "./types/ServerErrorResponse";
|
|
4
|
-
import { AbortError, ClientError, BringError, NetworkError, ServerError, TimeoutError, } from "./errors";
|
|
5
|
-
import { tryFetch } from "./tryFetch";
|
|
2
|
+
import { isClientErrorResponse } from "./types/ClientErrorResponse.js";
|
|
3
|
+
import { isServerErrorResponse } from "./types/ServerErrorResponse.js";
|
|
4
|
+
import { AbortError, ClientError, BringError, NetworkError, ServerError, TimeoutError, } from "./errors/index.js";
|
|
5
|
+
import { tryFetch } from "./tryFetch.js";
|
|
6
6
|
/** bring({url: string | URL})
|
|
7
7
|
*
|
|
8
8
|
* A wrapper around the fetch API that adds support for timeouts, retries, and aborts.
|
|
@@ -89,6 +89,7 @@ export const bring = (init) => {
|
|
|
89
89
|
url,
|
|
90
90
|
requestInit: init,
|
|
91
91
|
});
|
|
92
|
+
onNetworkError?.(networkError);
|
|
92
93
|
onError?.(networkError);
|
|
93
94
|
return resolve(failure(networkError));
|
|
94
95
|
}
|
package/dist/errors/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export * from "./AbortError";
|
|
2
|
-
export * from "./ClientError";
|
|
3
|
-
export * from "./BringError";
|
|
4
|
-
export * from "./NetworkError";
|
|
5
|
-
export * from "./ServerError";
|
|
6
|
-
export * from "./TimeoutError";
|
|
1
|
+
export * from "./AbortError.js";
|
|
2
|
+
export * from "./ClientError.js";
|
|
3
|
+
export * from "./BringError.js";
|
|
4
|
+
export * from "./NetworkError.js";
|
|
5
|
+
export * from "./ServerError.js";
|
|
6
|
+
export * from "./TimeoutError.js";
|
package/dist/fetch.js
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
export * from "./bring";
|
|
2
|
-
export * from "./
|
|
3
|
-
export * from "./
|
|
4
|
-
export * from "./
|
|
5
|
-
export * from "./
|
|
1
|
+
export * from "./bring.js";
|
|
2
|
+
export * from "./fetch.js";
|
|
3
|
+
export * from "./tryFetch.js";
|
|
4
|
+
export * from "./errors/index.js";
|
|
5
|
+
export * from "./request-builder-pipe/index.js";
|
|
6
|
+
export * from "./response-pipe/index.js";
|
|
@@ -12,15 +12,24 @@
|
|
|
12
12
|
export const headers = (headers) => {
|
|
13
13
|
return (init) => {
|
|
14
14
|
init.headers ??= new Headers();
|
|
15
|
+
const entries = [];
|
|
16
|
+
if (headers instanceof Headers) {
|
|
17
|
+
headers.forEach((value, key) => entries.push([key, value]));
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
21
|
+
entries.push([key, String(value)]);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
15
24
|
if (init.headers instanceof Headers) {
|
|
16
|
-
for (const [key, val] of
|
|
25
|
+
for (const [key, val] of entries) {
|
|
17
26
|
!init.headers.has(key)
|
|
18
27
|
? init.headers.append(key, val)
|
|
19
28
|
: init.headers.set(key, val);
|
|
20
29
|
}
|
|
21
30
|
}
|
|
22
31
|
else {
|
|
23
|
-
for (const [key, val] of
|
|
32
|
+
for (const [key, val] of entries) {
|
|
24
33
|
init.headers[key] = val;
|
|
25
34
|
}
|
|
26
35
|
}
|
|
@@ -1,19 +1,20 @@
|
|
|
1
|
-
export * from "./cache";
|
|
2
|
-
export * from "./cors";
|
|
3
|
-
export * from "./credentials";
|
|
4
|
-
export * from "./header";
|
|
5
|
-
export * from "./headers";
|
|
6
|
-
export * from "./integrity";
|
|
7
|
-
export * from "./keepalive";
|
|
8
|
-
export * from "./method";
|
|
9
|
-
export * from "./redirect";
|
|
10
|
-
export * from "./referrer";
|
|
11
|
-
export * from "./referrerPolicy";
|
|
12
|
-
export * from "./retry";
|
|
13
|
-
export * from "./body";
|
|
14
|
-
export * from "./jsonBody";
|
|
15
|
-
export * from "./priority";
|
|
16
|
-
export * from "./url";
|
|
17
|
-
export * from "./backoff";
|
|
18
|
-
export * from "./jitter";
|
|
19
|
-
export * from "./timeout";
|
|
1
|
+
export * from "./cache.js";
|
|
2
|
+
export * from "./cors.js";
|
|
3
|
+
export * from "./credentials.js";
|
|
4
|
+
export * from "./header.js";
|
|
5
|
+
export * from "./headers.js";
|
|
6
|
+
export * from "./integrity.js";
|
|
7
|
+
export * from "./keepalive.js";
|
|
8
|
+
export * from "./method.js";
|
|
9
|
+
export * from "./redirect.js";
|
|
10
|
+
export * from "./referrer.js";
|
|
11
|
+
export * from "./referrerPolicy.js";
|
|
12
|
+
export * from "./retry.js";
|
|
13
|
+
export * from "./body.js";
|
|
14
|
+
export * from "./jsonBody.js";
|
|
15
|
+
export * from "./priority.js";
|
|
16
|
+
export * from "./url.js";
|
|
17
|
+
export * from "./backoff.js";
|
|
18
|
+
export * from "./jitter.js";
|
|
19
|
+
export * from "./timeout.js";
|
|
20
|
+
export * from "./shouldRetry.js";
|
|
@@ -11,5 +11,5 @@ import { randomJitter } from "../helpers/randomJitter";
|
|
|
11
11
|
*/
|
|
12
12
|
export const jitter = (options) => {
|
|
13
13
|
const jitterFun = () => randomJitter(options);
|
|
14
|
-
return (init) => ({ ...init,
|
|
14
|
+
return (init) => ({ ...init, jitter: jitterFun });
|
|
15
15
|
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export * from "./arrayBuffer";
|
|
2
|
-
export * from "./blob";
|
|
3
|
-
export * from "./bytes";
|
|
4
|
-
export * from "./formData";
|
|
5
|
-
export * from "./json";
|
|
6
|
-
export * from "./text";
|
|
1
|
+
export * from "./arrayBuffer.js";
|
|
2
|
+
export * from "./blob.js";
|
|
3
|
+
export * from "./bytes.js";
|
|
4
|
+
export * from "./formData.js";
|
|
5
|
+
export * from "./json.js";
|
|
6
|
+
export * from "./text.js";
|
package/dist/tryFetch.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import { failure, sleep, success, tryAwait, } from "@torthu/jacketui-core";
|
|
2
|
+
const shouldRetryFailure = (response, init, retryNum) => response ? init.shouldRetry?.(response, retryNum) ?? false : true;
|
|
2
3
|
export const tryFetch = async (url, requestInit, init, retryCount = 0) => {
|
|
3
4
|
const { error, value } = await tryAwait(fetch(url, requestInit));
|
|
4
|
-
|
|
5
|
+
const maxRetries = init.retry ?? 0;
|
|
6
|
+
if (error &&
|
|
7
|
+
retryCount < maxRetries &&
|
|
8
|
+
shouldRetryFailure(undefined, init, retryCount)) {
|
|
5
9
|
init.onRetry?.(retryCount);
|
|
6
10
|
const backoff = init.backoff?.(retryCount) ?? 0;
|
|
7
11
|
const jitter = init.jitter?.() ?? 0;
|
|
@@ -14,6 +18,18 @@ export const tryFetch = async (url, requestInit, init, retryCount = 0) => {
|
|
|
14
18
|
return failure(error);
|
|
15
19
|
}
|
|
16
20
|
else {
|
|
21
|
+
if (!value.ok && retryCount < maxRetries) {
|
|
22
|
+
const shouldRetry = shouldRetryFailure(value, init, retryCount);
|
|
23
|
+
if (shouldRetry) {
|
|
24
|
+
init.onRetry?.(retryCount);
|
|
25
|
+
const backoff = init.backoff?.(retryCount) ?? 0;
|
|
26
|
+
const jitter = init.jitter?.() ?? 0;
|
|
27
|
+
if (backoff + jitter > 0) {
|
|
28
|
+
await sleep(backoff + jitter);
|
|
29
|
+
}
|
|
30
|
+
return await tryFetch(url, requestInit, init, retryCount + 1);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
17
33
|
return success(value);
|
|
18
34
|
}
|
|
19
35
|
};
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { AbortError, BringError, ClientError, NetworkError, ServerError, TimeoutError } from "../errors";
|
|
2
|
-
type TypedArray = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array;
|
|
3
2
|
export interface BringCallbacks {
|
|
4
3
|
onAbort?: (error: AbortError) => void;
|
|
5
4
|
onNetworkError?: (error: NetworkError) => void;
|
|
@@ -12,7 +11,7 @@ export interface BringCallbacks {
|
|
|
12
11
|
}
|
|
13
12
|
export interface RequestInit {
|
|
14
13
|
method?: "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "HEAD" | "OPTIONS";
|
|
15
|
-
body?:
|
|
14
|
+
body?: BodyInit | null;
|
|
16
15
|
cache?: "default" | "no-store" | "reload" | "no-cache" | "force-cache" | "only-if-cached";
|
|
17
16
|
credentials?: "omit" | "same-origin" | "include";
|
|
18
17
|
headers?: Record<string, string> | Headers;
|
|
@@ -35,4 +34,3 @@ export type BringInit = RequestInit & RetryBackoff & BringCallbacks & {
|
|
|
35
34
|
url: string | URL;
|
|
36
35
|
abortController?: AbortController;
|
|
37
36
|
};
|
|
38
|
-
export {};
|
package/package.json
CHANGED
|
@@ -1,24 +1,42 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@torthu/jacketui-bring",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "Improved fetch API with retry, timeout, and more.",
|
|
5
|
-
"main": "dist/index.js",
|
|
6
|
-
"types": "dist/index.d.ts",
|
|
7
5
|
"author": {
|
|
8
6
|
"name": "Torstein Thune",
|
|
9
7
|
"email": "torstein@thune.io",
|
|
10
8
|
"url": "https://thune.io"
|
|
11
9
|
},
|
|
12
|
-
"
|
|
13
|
-
|
|
10
|
+
"homepage": "https://code.thune.io/torstein/jacketui",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://code.thune.io/torstein/jacketui.git",
|
|
14
|
+
"directory": "packages/bring"
|
|
14
15
|
},
|
|
15
|
-
"
|
|
16
|
-
|
|
16
|
+
"type": "module",
|
|
17
|
+
"files": [
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
20
|
+
"main": "dist/index.js",
|
|
21
|
+
"types": "dist/index.d.ts",
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"import": "./dist/index.js",
|
|
25
|
+
"types": "./dist/index.d.ts"
|
|
26
|
+
}
|
|
17
27
|
},
|
|
18
28
|
"scripts": {
|
|
19
29
|
"prepublish": "npm run build",
|
|
20
|
-
"
|
|
21
|
-
"
|
|
30
|
+
"typecheck": "tsc --noEmit",
|
|
31
|
+
"build": "rimraf ./dist && tsc --project tsconfig.json && fix-imports --tsconfig tsconfig.json",
|
|
32
|
+
"test": "vitest run",
|
|
33
|
+
"coverage": "vitest run --coverage"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"fetch-mock": "12.6.0"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@torthu/jacketui-core": "latest"
|
|
22
40
|
},
|
|
23
41
|
"license": "LGPL-3.0-only"
|
|
24
42
|
}
|
package/src/bring.ts
DELETED
|
@@ -1,207 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Result,
|
|
3
|
-
success,
|
|
4
|
-
failure,
|
|
5
|
-
AbortablePromise,
|
|
6
|
-
withResolvers,
|
|
7
|
-
} from "@torthu/jacketui-core";
|
|
8
|
-
import { isClientErrorResponse } from "./types/ClientErrorResponse";
|
|
9
|
-
import { BringInit } from "./types/BringInit";
|
|
10
|
-
import { isInformationalResponse } from "./types/InformationalResponse";
|
|
11
|
-
import { isRedirectResponse } from "./types/RedirectResponse";
|
|
12
|
-
import { isServerErrorResponse } from "./types/ServerErrorResponse";
|
|
13
|
-
import {
|
|
14
|
-
AbortError,
|
|
15
|
-
ClientError,
|
|
16
|
-
BringError,
|
|
17
|
-
NetworkError,
|
|
18
|
-
ServerError,
|
|
19
|
-
TimeoutError,
|
|
20
|
-
} from "./errors";
|
|
21
|
-
import { tryFetch } from "./tryFetch";
|
|
22
|
-
|
|
23
|
-
/** bring({url: string | URL})
|
|
24
|
-
*
|
|
25
|
-
* A wrapper around the fetch API that adds support for timeouts, retries, and aborts.
|
|
26
|
-
* It also adds support for custom error handling and success handling.
|
|
27
|
-
* It returns a promise that resolves to a Result object.
|
|
28
|
-
* The returned promise is extended with an abort method that can be used to abort the fetch request.
|
|
29
|
-
*
|
|
30
|
-
* The Result object contains either a Response object or a BringError.
|
|
31
|
-
*
|
|
32
|
-
* @param init {BringInit | BringError} RequestInit + retries, timeout, onSuccess, onError, onAbort, onRetry, onTimeout, onClientError, onServerError
|
|
33
|
-
* @param init.url The URL to fetch.
|
|
34
|
-
* @param init.abortController (optional) An AbortController object to abort the fetch request. (default: new AbortController())
|
|
35
|
-
* @param init.timeout (optional) The timeout in milliseconds. (default: 0)
|
|
36
|
-
* @param init.retry (optional) The number of times to retry the fetch request. (default: 0)
|
|
37
|
-
* @param init.onSuccess (optional) A callback function that is called when the fetch request is successful.
|
|
38
|
-
* @param init.onError (optional) A callback function that is called when the fetch request fails.
|
|
39
|
-
* @param init.onAbort (optional) A callback function that is called when the fetch request is aborted.
|
|
40
|
-
* @param init.onRetry (optional) A callback function that is called when the fetch request is retried.
|
|
41
|
-
* @param init.onTimeout (optional) A callback function that is called when the fetch request times out.
|
|
42
|
-
* @param init.onClientError (optional) A callback function that is called when the fetch request fails with a client error.
|
|
43
|
-
* @param init.onServerError (optional) A callback function that is called when the fetch request fails with a server error.
|
|
44
|
-
* @param init.body (optional) The body of the fetch request.
|
|
45
|
-
* @param init.headers (optional) The headers of the fetch request.
|
|
46
|
-
* @param init.method (optional) The method of the fetch request.
|
|
47
|
-
* @param init.mode (optional) The mode of the fetch request.
|
|
48
|
-
* @param init.credentials (optional) The credentials of the fetch request.
|
|
49
|
-
* @param init.cache (optional) The cache of the fetch request.
|
|
50
|
-
* @param init.redirect (optional) The redirect of the fetch request.
|
|
51
|
-
* @param init.referrer (optional) The referrer of the fetch request.
|
|
52
|
-
* @param init.referrerPolicy (optional) The referrerPolicy of the fetch request.
|
|
53
|
-
* @param init.integrity (optional) The integrity of the fetch request.
|
|
54
|
-
* @param init.keepalive (optional) The keepalive of the fetch request.
|
|
55
|
-
* @returns AbortablePromise<Result<Response, BringError>>
|
|
56
|
-
*/
|
|
57
|
-
export const bring = (init: BringInit | BringError) => {
|
|
58
|
-
if (init instanceof BringError) init = init.requestInit;
|
|
59
|
-
|
|
60
|
-
const {
|
|
61
|
-
url,
|
|
62
|
-
abortController = new AbortController(),
|
|
63
|
-
timeout = 0,
|
|
64
|
-
retry = 0,
|
|
65
|
-
onSuccess,
|
|
66
|
-
onError,
|
|
67
|
-
onAbort,
|
|
68
|
-
onRetry,
|
|
69
|
-
onTimeout,
|
|
70
|
-
onClientError,
|
|
71
|
-
onServerError,
|
|
72
|
-
onNetworkError,
|
|
73
|
-
...requestInit
|
|
74
|
-
} = init;
|
|
75
|
-
|
|
76
|
-
// Configure promise
|
|
77
|
-
const { promise, resolve } = withResolvers();
|
|
78
|
-
const abortablePromise = promise as AbortablePromise<
|
|
79
|
-
Result<Response, BringError>
|
|
80
|
-
>;
|
|
81
|
-
abortablePromise.abort = abortController.abort;
|
|
82
|
-
|
|
83
|
-
// Configure timeout signal
|
|
84
|
-
const timeoutSignal = timeout > 0 ? AbortSignal.timeout(timeout) : undefined;
|
|
85
|
-
const signal = AbortSignal.any(
|
|
86
|
-
[abortController.signal, timeoutSignal].filter((s) => !!s)
|
|
87
|
-
);
|
|
88
|
-
|
|
89
|
-
tryFetch(
|
|
90
|
-
url,
|
|
91
|
-
{
|
|
92
|
-
...requestInit,
|
|
93
|
-
signal,
|
|
94
|
-
},
|
|
95
|
-
init,
|
|
96
|
-
0
|
|
97
|
-
).then(({ error, value }) => {
|
|
98
|
-
if (error) {
|
|
99
|
-
if (abortController.signal.aborted) {
|
|
100
|
-
const abortError = new AbortError(abortController.signal.reason, {
|
|
101
|
-
context: {
|
|
102
|
-
url: url,
|
|
103
|
-
method: requestInit.method ?? "GET",
|
|
104
|
-
reason: abortController.signal.reason,
|
|
105
|
-
},
|
|
106
|
-
cause: error,
|
|
107
|
-
url,
|
|
108
|
-
requestInit: init,
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
onAbort?.(abortError);
|
|
112
|
-
onError?.(abortError);
|
|
113
|
-
return resolve(failure(abortError));
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
if (timeoutSignal?.aborted) {
|
|
117
|
-
const timeoutError = new TimeoutError("Timeout", {
|
|
118
|
-
context: {
|
|
119
|
-
reason: timeoutSignal.reason,
|
|
120
|
-
timeout,
|
|
121
|
-
},
|
|
122
|
-
cause: error,
|
|
123
|
-
url,
|
|
124
|
-
requestInit: init,
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
onTimeout?.(timeoutError);
|
|
128
|
-
onError?.(timeoutError);
|
|
129
|
-
return resolve(failure(timeoutError));
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
if (error instanceof Error) {
|
|
133
|
-
const networkError = new NetworkError(error.message, {
|
|
134
|
-
context: { url, method: requestInit.method ?? "GET" },
|
|
135
|
-
cause: error,
|
|
136
|
-
url,
|
|
137
|
-
requestInit: init,
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
onError?.(networkError);
|
|
141
|
-
return resolve(failure(networkError));
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
if (typeof error === "string") {
|
|
145
|
-
return resolve(
|
|
146
|
-
failure(new BringError(error, { url, requestInit: init }))
|
|
147
|
-
);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
return resolve(
|
|
151
|
-
failure(new BringError("Unknown error", { url, requestInit: init }))
|
|
152
|
-
);
|
|
153
|
-
} else {
|
|
154
|
-
const response = value;
|
|
155
|
-
|
|
156
|
-
if (response.ok) {
|
|
157
|
-
onSuccess?.(response);
|
|
158
|
-
return resolve(success(response));
|
|
159
|
-
} else {
|
|
160
|
-
if (isClientErrorResponse(response)) {
|
|
161
|
-
const clientError = new ClientError(response.statusText, {
|
|
162
|
-
context: {
|
|
163
|
-
status: response.status,
|
|
164
|
-
statusText: response.statusText,
|
|
165
|
-
},
|
|
166
|
-
response,
|
|
167
|
-
requestInit: init,
|
|
168
|
-
url,
|
|
169
|
-
});
|
|
170
|
-
onClientError?.(clientError);
|
|
171
|
-
onError?.(clientError);
|
|
172
|
-
return resolve(failure(clientError));
|
|
173
|
-
}
|
|
174
|
-
if (isServerErrorResponse(response)) {
|
|
175
|
-
const serverError = new ServerError(response.statusText, {
|
|
176
|
-
response,
|
|
177
|
-
requestInit: init,
|
|
178
|
-
url,
|
|
179
|
-
context: {
|
|
180
|
-
status: response.status,
|
|
181
|
-
statusText: response.statusText,
|
|
182
|
-
},
|
|
183
|
-
});
|
|
184
|
-
onServerError?.(serverError);
|
|
185
|
-
onError?.(serverError);
|
|
186
|
-
return resolve(failure(serverError));
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
const unknownError = new BringError("Fetch failed", {
|
|
190
|
-
context: {
|
|
191
|
-
url,
|
|
192
|
-
status: response.status,
|
|
193
|
-
statusText: response.statusText,
|
|
194
|
-
},
|
|
195
|
-
url: url,
|
|
196
|
-
requestInit: init,
|
|
197
|
-
response,
|
|
198
|
-
});
|
|
199
|
-
|
|
200
|
-
onError?.(unknownError);
|
|
201
|
-
return resolve(failure(unknownError));
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
});
|
|
205
|
-
|
|
206
|
-
return abortablePromise;
|
|
207
|
-
};
|
package/src/errors/AbortError.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { BringError } from "./BringError";
|
|
2
|
-
|
|
3
|
-
export class AbortError extends BringError {
|
|
4
|
-
tag = "AbortError";
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
/** isAbortError
|
|
8
|
-
*
|
|
9
|
-
* Checks if error is AbortError.
|
|
10
|
-
* An AbortError is either an instance of AbortError or a BringError with tag "AbortError".
|
|
11
|
-
*
|
|
12
|
-
*/
|
|
13
|
-
export const isAbortError = (error: unknown): error is AbortError => {
|
|
14
|
-
return (
|
|
15
|
-
error instanceof AbortError ||
|
|
16
|
-
(error instanceof BringError && error.tag === "AbortError")
|
|
17
|
-
);
|
|
18
|
-
};
|
package/src/errors/BringError.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { BringInit } from "../types/BringInit";
|
|
2
|
-
import { JuiError, JuiErrorOptions } from "@torthu/jacketui-core";
|
|
3
|
-
|
|
4
|
-
export interface BringErrorOptions extends JuiErrorOptions {
|
|
5
|
-
response?: Response;
|
|
6
|
-
request?: Request;
|
|
7
|
-
requestInit: BringInit;
|
|
8
|
-
url: string | URL;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export class BringError extends JuiError {
|
|
12
|
-
public readonly response?: BringErrorOptions["response"];
|
|
13
|
-
public readonly request?: BringErrorOptions["request"];
|
|
14
|
-
public readonly requestInit: BringErrorOptions["requestInit"];
|
|
15
|
-
public readonly url: BringErrorOptions["url"];
|
|
16
|
-
|
|
17
|
-
constructor(message: string, options: BringErrorOptions) {
|
|
18
|
-
super(message, options);
|
|
19
|
-
this.response = options.response;
|
|
20
|
-
this.request = options.request;
|
|
21
|
-
this.requestInit = options.requestInit;
|
|
22
|
-
this.url = options.url;
|
|
23
|
-
}
|
|
24
|
-
}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { BringError } from "./BringError";
|
|
2
|
-
|
|
3
|
-
export class ClientError extends BringError {
|
|
4
|
-
tag = "ClientError";
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
/** isClientError
|
|
8
|
-
*
|
|
9
|
-
* Checks if error is ClientError.
|
|
10
|
-
* A ClientError is either an instance of ClientError or a BringError with a status code between 400 and 499.
|
|
11
|
-
*
|
|
12
|
-
*/
|
|
13
|
-
export const isClientError = (error: unknown): error is ClientError => {
|
|
14
|
-
if (error instanceof ClientError) {
|
|
15
|
-
return true;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
return (
|
|
19
|
-
error instanceof BringError &&
|
|
20
|
-
!!error.response &&
|
|
21
|
-
error.response.status >= 400 &&
|
|
22
|
-
error.response.status < 500
|
|
23
|
-
);
|
|
24
|
-
};
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { BringError } from "./BringError";
|
|
2
|
-
|
|
3
|
-
export class NetworkError extends BringError {
|
|
4
|
-
tag = "NetworkError";
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
/** isNetworkError
|
|
8
|
-
*
|
|
9
|
-
* Checks if error is NetworkError.
|
|
10
|
-
* A NetworkError is either an instance of NetworkError or a BringError with status code 0.
|
|
11
|
-
*
|
|
12
|
-
*/
|
|
13
|
-
export const isNetworkError = (error: unknown): error is NetworkError => {
|
|
14
|
-
if (error instanceof NetworkError) {
|
|
15
|
-
return true;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
return (
|
|
19
|
-
error instanceof BringError &&
|
|
20
|
-
!!error.response &&
|
|
21
|
-
error.response.status === 0
|
|
22
|
-
);
|
|
23
|
-
};
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { BringError } from "./BringError";
|
|
2
|
-
|
|
3
|
-
export class ServerError extends BringError {
|
|
4
|
-
tag = "ServerError";
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
/** isServerError
|
|
8
|
-
*
|
|
9
|
-
* Checks if error is ServerError.
|
|
10
|
-
* A ServerError is either an instance of ServerError or a BringError with status code >= 500 and < 600.
|
|
11
|
-
*
|
|
12
|
-
*/
|
|
13
|
-
export const isServerError = (error: unknown): error is ServerError => {
|
|
14
|
-
if (error instanceof ServerError) {
|
|
15
|
-
return true;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
return (
|
|
19
|
-
error instanceof BringError &&
|
|
20
|
-
!!error.response &&
|
|
21
|
-
error.response.status >= 500 &&
|
|
22
|
-
error.response.status < 600
|
|
23
|
-
);
|
|
24
|
-
};
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { BringError } from "./BringError";
|
|
2
|
-
|
|
3
|
-
export class TimeoutError extends BringError {
|
|
4
|
-
tag = "TimeoutError";
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
/** isTimeoutError
|
|
8
|
-
*
|
|
9
|
-
* Checks if error is TimeoutError.
|
|
10
|
-
* A TimeoutError is either an instance of TimeoutError or a BringError with tag "TimeoutError".
|
|
11
|
-
*
|
|
12
|
-
*/
|
|
13
|
-
export const isTimeoutError = (error: unknown): error is TimeoutError => {
|
|
14
|
-
return (
|
|
15
|
-
error instanceof TimeoutError ||
|
|
16
|
-
(error instanceof BringError && error.tag === "TimeoutError")
|
|
17
|
-
);
|
|
18
|
-
};
|