@torthu/jacketui-bring 0.0.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 +11 -0
- package/dist/bring.d.ts +38 -0
- package/dist/bring.js +142 -0
- package/dist/errors/AbortError.d.ts +11 -0
- package/dist/errors/AbortError.js +14 -0
- package/dist/errors/BringError.d.ts +15 -0
- package/dist/errors/BringError.js +14 -0
- package/dist/errors/ClientError.d.ts +11 -0
- package/dist/errors/ClientError.js +19 -0
- package/dist/errors/NetworkError.d.ts +11 -0
- package/dist/errors/NetworkError.js +18 -0
- package/dist/errors/ServerError.d.ts +11 -0
- package/dist/errors/ServerError.js +19 -0
- package/dist/errors/TimeoutError.d.ts +11 -0
- package/dist/errors/TimeoutError.js +14 -0
- package/dist/errors/index.d.ts +6 -0
- package/dist/errors/index.js +6 -0
- package/dist/fetch.d.ts +30 -0
- package/dist/fetch.js +32 -0
- package/dist/helpers/exponentialBackoff.d.ts +21 -0
- package/dist/helpers/exponentialBackoff.js +18 -0
- package/dist/helpers/extractCallbacks.d.ts +2 -0
- package/dist/helpers/extractCallbacks.js +12 -0
- package/dist/helpers/extractRequestInit.d.ts +2 -0
- package/dist/helpers/extractRequestInit.js +16 -0
- package/dist/helpers/randomJitter.d.ts +15 -0
- package/dist/helpers/randomJitter.js +14 -0
- package/dist/helpers/shouldRetry.d.ts +22 -0
- package/dist/helpers/shouldRetry.js +28 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/pipe-helpers/backoff.d.ts +14 -0
- package/dist/pipe-helpers/backoff.js +16 -0
- package/dist/pipe-helpers/body.d.ts +14 -0
- package/dist/pipe-helpers/body.js +12 -0
- package/dist/pipe-helpers/cache.d.ts +17 -0
- package/dist/pipe-helpers/cache.js +18 -0
- package/dist/pipe-helpers/cors.d.ts +33 -0
- package/dist/pipe-helpers/cors.js +34 -0
- package/dist/pipe-helpers/credentials.d.ts +25 -0
- package/dist/pipe-helpers/credentials.js +26 -0
- package/dist/pipe-helpers/header.d.ts +13 -0
- package/dist/pipe-helpers/header.js +25 -0
- package/dist/pipe-helpers/index.d.ts +17 -0
- package/dist/pipe-helpers/index.js +17 -0
- package/dist/pipe-helpers/integrity.d.ts +17 -0
- package/dist/pipe-helpers/integrity.js +18 -0
- package/dist/pipe-helpers/jitter.d.ts +13 -0
- package/dist/pipe-helpers/jitter.js +15 -0
- package/dist/pipe-helpers/jsonBody.d.ts +10 -0
- package/dist/pipe-helpers/jsonBody.js +8 -0
- package/dist/pipe-helpers/keepalive.d.ts +15 -0
- package/dist/pipe-helpers/keepalive.js +16 -0
- package/dist/pipe-helpers/method.d.ts +12 -0
- package/dist/pipe-helpers/method.js +13 -0
- package/dist/pipe-helpers/priority.d.ts +15 -0
- package/dist/pipe-helpers/priority.js +15 -0
- package/dist/pipe-helpers/redirect.d.ts +17 -0
- package/dist/pipe-helpers/redirect.js +18 -0
- package/dist/pipe-helpers/referrer.d.ts +15 -0
- package/dist/pipe-helpers/referrer.js +16 -0
- package/dist/pipe-helpers/referrerPolicy.d.ts +25 -0
- package/dist/pipe-helpers/referrerPolicy.js +26 -0
- package/dist/pipe-helpers/retry.d.ts +10 -0
- package/dist/pipe-helpers/retry.js +8 -0
- package/dist/pipe-helpers/shouldRetry.d.ts +8 -0
- package/dist/pipe-helpers/shouldRetry.js +6 -0
- package/dist/pipe-helpers/timeout.d.ts +16 -0
- package/dist/pipe-helpers/timeout.js +14 -0
- package/dist/tryFetch.d.ts +3 -0
- package/dist/tryFetch.js +19 -0
- package/dist/types/BringDecorator.d.ts +4 -0
- package/dist/types/BringDecorator.js +1 -0
- package/dist/types/BringInit.d.ts +37 -0
- package/dist/types/BringInit.js +1 -0
- package/dist/types/ClientErrorResponse.d.ts +6 -0
- package/dist/types/ClientErrorResponse.js +32 -0
- package/dist/types/InformationalResponse.d.ts +6 -0
- package/dist/types/InformationalResponse.js +8 -0
- package/dist/types/RedirectResponse.d.ts +6 -0
- package/dist/types/RedirectResponse.js +9 -0
- package/dist/types/ServerErrorResponse.d.ts +6 -0
- package/dist/types/ServerErrorResponse.js +24 -0
- package/dist/types/SuccessResponse.d.ts +13 -0
- package/dist/types/SuccessResponse.js +20 -0
- package/dist/types/statusCodes.d.ts +69 -0
- package/dist/types/statusCodes.js +69 -0
- package/package.json +23 -0
- package/src/bring.ts +198 -0
- package/src/errors/AbortError.ts +18 -0
- package/src/errors/BringError.ts +24 -0
- package/src/errors/ClientError.ts +24 -0
- package/src/errors/NetworkError.ts +23 -0
- package/src/errors/ServerError.ts +24 -0
- package/src/errors/TimeoutError.ts +18 -0
- package/src/errors/index.ts +6 -0
- package/src/fetch.ts +34 -0
- package/src/helpers/exponentialBackoff.ts +28 -0
- package/src/helpers/extractCallbacks.ts +23 -0
- package/src/helpers/extractRequestInit.ts +30 -0
- package/src/helpers/randomJitter.ts +22 -0
- package/src/helpers/shouldRetry.ts +40 -0
- package/src/index.ts +4 -0
- package/src/pipe-helpers/backoff.ts +22 -0
- package/src/pipe-helpers/body.ts +17 -0
- package/src/pipe-helpers/cache.ts +23 -0
- package/src/pipe-helpers/cors.ts +39 -0
- package/src/pipe-helpers/credentials.ts +33 -0
- package/src/pipe-helpers/header.ts +28 -0
- package/src/pipe-helpers/index.ts +18 -0
- package/src/pipe-helpers/integrity.ts +23 -0
- package/src/pipe-helpers/jitter.ts +17 -0
- package/src/pipe-helpers/jsonBody.ts +13 -0
- package/src/pipe-helpers/keepalive.ts +21 -0
- package/src/pipe-helpers/method.ts +18 -0
- package/src/pipe-helpers/priority.ts +20 -0
- package/src/pipe-helpers/redirect.ts +23 -0
- package/src/pipe-helpers/referrer.ts +21 -0
- package/src/pipe-helpers/referrerPolicy.ts +33 -0
- package/src/pipe-helpers/retry.ts +13 -0
- package/src/pipe-helpers/shouldRetry.ts +11 -0
- package/src/pipe-helpers/timeout.ts +19 -0
- package/src/tryFetch.ts +32 -0
- package/src/types/BringDecorator.ts +5 -0
- package/src/types/BringInit.ts +78 -0
- package/src/types/ClientErrorResponse.ts +72 -0
- package/src/types/InformationalResponse.ts +20 -0
- package/src/types/RedirectResponse.ts +20 -0
- package/src/types/ServerErrorResponse.ts +56 -0
- package/src/types/SuccessResponse.ts +57 -0
- package/src/types/statusCodes.ts +69 -0
- package/tsconfig.json +11 -0
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { BringInitDecorator } from "../types/BringDecorator";
|
|
2
|
+
import { BringInit } from "../types/BringInit";
|
|
3
|
+
|
|
4
|
+
/** referrerPolicy(string): BringInitDecorator
|
|
5
|
+
*
|
|
6
|
+
* A string specifying the value of the referrer-policy header to use for the request.
|
|
7
|
+
*
|
|
8
|
+
* Possible values are:
|
|
9
|
+
* - "no-referrer" The Referer header will not be sent.
|
|
10
|
+
* - "no-referrer-when-downgrade" Send the origin, path, and query string in Referer when the protocol security level stays the same or improves
|
|
11
|
+
* - "origin" Send only the origin in the Referer header. For example, a document at https://example.com/page.html will send the referrer https://example.com/.
|
|
12
|
+
* - "origin-when-cross-origin" When performing a same-origin request to the same protocol level (HTTP→HTTP, HTTPS→HTTPS), send the origin, path, and query string. Send only the origin for cross origin requests and requests to less secure destinations (HTTPS→HTTP).
|
|
13
|
+
* - "same-origin" Send the origin, path, and query string for same-origin requests. Don't send the Referer header for cross-origin requests.
|
|
14
|
+
* - "strict-origin" Send only the origin when the protocol security level stays the same (HTTPS→HTTPS). Don't send the Referer header to less secure destinations (HTTPS→HTTP).
|
|
15
|
+
* - "strict-origin-when-cross-origin" (default) Send the origin, path, and query string when performing a same-origin request. For cross-origin requests send the origin (only) when the protocol security level stays same (HTTPS→HTTPS). Don't send the Referer header to less secure destinations (HTTPS→HTTP).
|
|
16
|
+
* - "unsafe-url" Send the origin, path, and query string when performing any request, regardless of security.
|
|
17
|
+
*
|
|
18
|
+
* Default is "strict-origin-when-cross-origin"
|
|
19
|
+
*
|
|
20
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/RequestInit#referrerpolicy
|
|
21
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Referrer-Policy
|
|
22
|
+
*
|
|
23
|
+
* @param referrerPolicyMode
|
|
24
|
+
* @returns BringInitDecorator
|
|
25
|
+
*/
|
|
26
|
+
export const referrerPolicy =
|
|
27
|
+
(
|
|
28
|
+
referrerPolicyMode: BringInit["referrerPolicy"] = "strict-origin-when-cross-origin"
|
|
29
|
+
): BringInitDecorator =>
|
|
30
|
+
(init) => {
|
|
31
|
+
init.referrerPolicy = referrerPolicyMode;
|
|
32
|
+
return init;
|
|
33
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { BringInitDecorator } from "../types/BringDecorator";
|
|
2
|
+
import { BringInit } from "../types/BringInit";
|
|
3
|
+
|
|
4
|
+
/** retry(number): BringInitDecorator
|
|
5
|
+
*
|
|
6
|
+
* Sets the number of times to retry the request.
|
|
7
|
+
*
|
|
8
|
+
* @param retry The number of times to retry the request.
|
|
9
|
+
* @returns BringInitDecorator
|
|
10
|
+
*/
|
|
11
|
+
export const retry =
|
|
12
|
+
(retry: BringInit["retry"]): BringInitDecorator =>
|
|
13
|
+
(init) => ({ ...init, retry });
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { BringInitDecorator } from "../types/BringDecorator";
|
|
2
|
+
import { BringInit } from "../types/BringInit";
|
|
3
|
+
|
|
4
|
+
/** shouldRetry((response: Response, retryNum: number) => boolean)): BringInitDecorator
|
|
5
|
+
*
|
|
6
|
+
* @param shouldRetry ((response: Response, retryNum: number) => boolean)
|
|
7
|
+
* @returns BringInitDecorator
|
|
8
|
+
*/
|
|
9
|
+
export const shouldRetry =
|
|
10
|
+
(shouldRetry: BringInit["shouldRetry"]): BringInitDecorator =>
|
|
11
|
+
(init) => ({ ...init, shouldRetry });
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { BringInitDecorator } from "../types/BringDecorator";
|
|
2
|
+
import { BringInit } from "../types/BringInit";
|
|
3
|
+
|
|
4
|
+
/** timeout(number): BringInitDecorator
|
|
5
|
+
*
|
|
6
|
+
* Sets in-flight timeout for request.
|
|
7
|
+
* Used to configure AbortSignal.timeout().
|
|
8
|
+
*
|
|
9
|
+
* If retry is set, timeout will be applied to each retry.
|
|
10
|
+
* Total time for request will be (timeout + jitter + backoff) * retry.
|
|
11
|
+
*
|
|
12
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal/timeout_static
|
|
13
|
+
*
|
|
14
|
+
* @param timeout The time to wait before aborting the request.
|
|
15
|
+
* @returns BringInitDecorator
|
|
16
|
+
*/
|
|
17
|
+
export const timeout =
|
|
18
|
+
(timeout: BringInit["timeout"]): BringInitDecorator =>
|
|
19
|
+
(init) => ({ ...init, timeout });
|
package/src/tryFetch.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import {
|
|
2
|
+
failure,
|
|
3
|
+
JuiError,
|
|
4
|
+
Result,
|
|
5
|
+
sleep,
|
|
6
|
+
success,
|
|
7
|
+
tryAwait,
|
|
8
|
+
} from "@torthu/jacketui-core";
|
|
9
|
+
import { BringInit } from "./types/BringInit";
|
|
10
|
+
|
|
11
|
+
export const tryFetch = async (
|
|
12
|
+
url: string | URL,
|
|
13
|
+
requestInit: RequestInit,
|
|
14
|
+
init: BringInit,
|
|
15
|
+
retryCount = 0
|
|
16
|
+
): Promise<Result<Response, JuiError>> => {
|
|
17
|
+
const { error, value } = await tryAwait(fetch(url, requestInit));
|
|
18
|
+
if (error && retryCount < (init.retry ?? 0)) {
|
|
19
|
+
init.onRetry?.(retryCount);
|
|
20
|
+
const backoff = init.backoff?.(retryCount) ?? 0;
|
|
21
|
+
const jitter = init.jitter?.() ?? 0;
|
|
22
|
+
|
|
23
|
+
if (backoff + jitter > 0) {
|
|
24
|
+
await sleep(backoff + jitter);
|
|
25
|
+
}
|
|
26
|
+
return await tryFetch(url, requestInit, init, retryCount + 1);
|
|
27
|
+
} else if (error) {
|
|
28
|
+
return failure(error);
|
|
29
|
+
} else {
|
|
30
|
+
return success(value);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { AbortError, BringError, TimeoutError } from "../errors";
|
|
2
|
+
|
|
3
|
+
type TypedArray =
|
|
4
|
+
| Int8Array
|
|
5
|
+
| Uint8Array
|
|
6
|
+
| Uint8ClampedArray
|
|
7
|
+
| Int16Array
|
|
8
|
+
| Uint16Array
|
|
9
|
+
| Int32Array
|
|
10
|
+
| Uint32Array
|
|
11
|
+
| Float32Array
|
|
12
|
+
| Float64Array
|
|
13
|
+
| BigInt64Array
|
|
14
|
+
| BigUint64Array;
|
|
15
|
+
|
|
16
|
+
export interface BringCallbacks {
|
|
17
|
+
onAbort?: (error: AbortError) => void;
|
|
18
|
+
onError?: (error: BringError) => void;
|
|
19
|
+
onRetry?: (retryNum: number) => void;
|
|
20
|
+
onSuccess?: (response: Response) => void;
|
|
21
|
+
onTimeout?: (error: TimeoutError) => void;
|
|
22
|
+
onClientError?: (response: Response) => void;
|
|
23
|
+
onServerError?: (response: Response) => void;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface RequestInit {
|
|
27
|
+
method?: "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "HEAD" | "OPTIONS";
|
|
28
|
+
body?:
|
|
29
|
+
| string
|
|
30
|
+
| ArrayBuffer
|
|
31
|
+
| Blob
|
|
32
|
+
| DataView
|
|
33
|
+
| File
|
|
34
|
+
| FormData
|
|
35
|
+
| TypedArray
|
|
36
|
+
| URLSearchParams
|
|
37
|
+
| ReadableStream;
|
|
38
|
+
cache?:
|
|
39
|
+
| "default"
|
|
40
|
+
| "no-store"
|
|
41
|
+
| "reload"
|
|
42
|
+
| "no-cache"
|
|
43
|
+
| "force-cache"
|
|
44
|
+
| "only-if-cached";
|
|
45
|
+
credentials?: "omit" | "same-origin" | "include";
|
|
46
|
+
headers?: Record<string, string> | Headers;
|
|
47
|
+
integrity?: string;
|
|
48
|
+
keepalive?: boolean;
|
|
49
|
+
mode?: "cors" | "no-cors" | "same-origin";
|
|
50
|
+
priority?: "low" | "high" | "auto";
|
|
51
|
+
redirect?: "follow" | "error" | "manual";
|
|
52
|
+
referrer?: string | "" | "about:client";
|
|
53
|
+
referrerPolicy?:
|
|
54
|
+
| "no-referrer"
|
|
55
|
+
| "no-referrer-when-downgrade"
|
|
56
|
+
| "origin"
|
|
57
|
+
| "origin-when-cross-origin"
|
|
58
|
+
| "same-origin"
|
|
59
|
+
| "strict-origin"
|
|
60
|
+
| "strict-origin-when-cross-origin"
|
|
61
|
+
| "unsafe-url";
|
|
62
|
+
// signal?: AbortSignal; // Handled by bring() or the AbortController passed in
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface RetryBackoff {
|
|
66
|
+
retry?: number;
|
|
67
|
+
timeout?: number;
|
|
68
|
+
shouldRetry?: (response: Response, retryNum: number) => boolean;
|
|
69
|
+
backoff?: (retryNum: number) => number;
|
|
70
|
+
jitter?: () => number;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export type BringInit = RequestInit &
|
|
74
|
+
RetryBackoff &
|
|
75
|
+
BringCallbacks & {
|
|
76
|
+
url: string | URL;
|
|
77
|
+
abortController?: AbortController;
|
|
78
|
+
};
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
type ClientErrorStatus =
|
|
2
|
+
| 400
|
|
3
|
+
| 401
|
|
4
|
+
| 403
|
|
5
|
+
| 404
|
|
6
|
+
| 405
|
|
7
|
+
| 406
|
|
8
|
+
| 407
|
|
9
|
+
| 408
|
|
10
|
+
| 409
|
|
11
|
+
| 410
|
|
12
|
+
| 411
|
|
13
|
+
| 412
|
|
14
|
+
| 413
|
|
15
|
+
| 414
|
|
16
|
+
| 415
|
|
17
|
+
| 416
|
|
18
|
+
| 417
|
|
19
|
+
| 418
|
|
20
|
+
| 421
|
|
21
|
+
| 422
|
|
22
|
+
| 423
|
|
23
|
+
| 424
|
|
24
|
+
| 425
|
|
25
|
+
| 426
|
|
26
|
+
| 428
|
|
27
|
+
| 429
|
|
28
|
+
| 431
|
|
29
|
+
| 451;
|
|
30
|
+
|
|
31
|
+
const clientStatusErrorMap: Record<ClientErrorStatus, string> = {
|
|
32
|
+
400: "Bad Request",
|
|
33
|
+
401: "Unauthorized",
|
|
34
|
+
403: "Forbidden",
|
|
35
|
+
404: "Not Found",
|
|
36
|
+
405: "Method Not Allowed",
|
|
37
|
+
406: "Not Acceptable",
|
|
38
|
+
407: "Proxy Authentication Required",
|
|
39
|
+
408: "Request Timeout",
|
|
40
|
+
409: "Conflict",
|
|
41
|
+
410: "Gone",
|
|
42
|
+
411: "Length Required",
|
|
43
|
+
412: "Precondition Failed",
|
|
44
|
+
413: "Payload Too Large",
|
|
45
|
+
414: "URI Too Long",
|
|
46
|
+
415: "Unsupported Media Type",
|
|
47
|
+
416: "Range Not Satisfiable",
|
|
48
|
+
417: "Expectation Failed",
|
|
49
|
+
418: "I'm a teapot",
|
|
50
|
+
421: "Misdirected Request",
|
|
51
|
+
422: "Unprocessable Entity",
|
|
52
|
+
423: "Locked",
|
|
53
|
+
424: "Failed Dependency",
|
|
54
|
+
425: "Too Early",
|
|
55
|
+
426: "Upgrade Required",
|
|
56
|
+
428: "Precondition Required",
|
|
57
|
+
429: "Too Many Requests",
|
|
58
|
+
431: "Request Header Fields Too Large",
|
|
59
|
+
451: "Unavailable For Legal Reasons",
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const clientStatusErrorSet = new Set(
|
|
63
|
+
Object.keys(clientStatusErrorMap).map((s) => parseInt(s, 10))
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
export interface ClientErrorResponse extends Response {
|
|
67
|
+
status: ClientErrorStatus;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export const isClientErrorResponse = (
|
|
71
|
+
res: Response
|
|
72
|
+
): res is ClientErrorResponse => clientStatusErrorSet.has(res.status);
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
type InformationalResponseStatus = 100 | 101 | 102 | 103;
|
|
2
|
+
|
|
3
|
+
const informationalResponseMap: Record<InformationalResponseStatus, string> = {
|
|
4
|
+
100: "Continue",
|
|
5
|
+
101: "Switching Protocols",
|
|
6
|
+
102: "Processing",
|
|
7
|
+
103: "Early Hints",
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
const informationalResponseSet = new Set(
|
|
11
|
+
Object.keys(informationalResponseMap).map((s) => parseInt(s, 10))
|
|
12
|
+
);
|
|
13
|
+
|
|
14
|
+
export interface InformationalResponse extends Response {
|
|
15
|
+
status: InformationalResponseStatus;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const isInformationalResponse = (
|
|
19
|
+
res: Response
|
|
20
|
+
): res is InformationalResponse => informationalResponseSet.has(res.status);
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
type RedirectStatus = 301 | 302 | 303 | 307 | 308;
|
|
2
|
+
|
|
3
|
+
const redirectMap: Record<RedirectStatus, string> = {
|
|
4
|
+
301: "Moved Permanently",
|
|
5
|
+
302: "Found",
|
|
6
|
+
303: "See Other",
|
|
7
|
+
307: "Temporary Redirect",
|
|
8
|
+
308: "Permanent Redirect",
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const redirectSet = new Set(
|
|
12
|
+
Object.keys(redirectMap).map((s) => parseInt(s, 10))
|
|
13
|
+
);
|
|
14
|
+
|
|
15
|
+
export interface RedirectResponse extends Response {
|
|
16
|
+
status: RedirectStatus;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const isRedirectResponse = (res: Response): res is RedirectResponse =>
|
|
20
|
+
redirectSet.has(res.status);
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
type ServerErrorStatus =
|
|
2
|
+
| 500
|
|
3
|
+
| 501
|
|
4
|
+
| 502
|
|
5
|
+
| 503
|
|
6
|
+
| 504
|
|
7
|
+
| 505
|
|
8
|
+
| 506
|
|
9
|
+
| 507
|
|
10
|
+
| 508
|
|
11
|
+
| 510
|
|
12
|
+
| 511
|
|
13
|
+
| 520
|
|
14
|
+
| 521
|
|
15
|
+
| 522
|
|
16
|
+
| 523
|
|
17
|
+
| 524
|
|
18
|
+
| 525
|
|
19
|
+
| 526
|
|
20
|
+
| 527
|
|
21
|
+
| 530;
|
|
22
|
+
|
|
23
|
+
const serverStatusErrorMap: Record<ServerErrorStatus, string> = {
|
|
24
|
+
500: "Internal Server Error",
|
|
25
|
+
501: "Not Implemented",
|
|
26
|
+
502: "Bad Gateway",
|
|
27
|
+
503: "Service Unavailable",
|
|
28
|
+
504: "Gateway Timeout",
|
|
29
|
+
505: "HTTP Version Not Supported",
|
|
30
|
+
506: "Variant Also Negotiates",
|
|
31
|
+
507: "Insufficient Storage",
|
|
32
|
+
508: "Loop Detected",
|
|
33
|
+
510: "Not Extended",
|
|
34
|
+
511: "Network Authentication Required",
|
|
35
|
+
520: "Unknown Error",
|
|
36
|
+
521: "Web Server Is Down",
|
|
37
|
+
522: "Connection Timed Out",
|
|
38
|
+
523: "Origin Is Unreachable",
|
|
39
|
+
524: "A Timeout Occurred",
|
|
40
|
+
525: "SSL Handshake Failed",
|
|
41
|
+
526: "Invalid SSL Certificate",
|
|
42
|
+
527: "Railgun Error",
|
|
43
|
+
530: "Site Is Frozen",
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const serverStatusErrorSet = new Set(
|
|
47
|
+
Object.keys(serverStatusErrorMap).map((s) => parseInt(s, 10))
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
export interface ServerErrorResponse extends Response {
|
|
51
|
+
status: ServerErrorStatus;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export const isServerErrorResponse = (
|
|
55
|
+
res: Response
|
|
56
|
+
): res is ServerErrorResponse => serverStatusErrorSet.has(res.status);
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
type KnownSuccessStatus =
|
|
2
|
+
| 200
|
|
3
|
+
| 201
|
|
4
|
+
| 202
|
|
5
|
+
| 203
|
|
6
|
+
| 204
|
|
7
|
+
| 205
|
|
8
|
+
| 206
|
|
9
|
+
| 207
|
|
10
|
+
| 208
|
|
11
|
+
| 226;
|
|
12
|
+
|
|
13
|
+
const knownSuccessStatusMap = {
|
|
14
|
+
200: "OK",
|
|
15
|
+
201: "Created",
|
|
16
|
+
202: "Accepted",
|
|
17
|
+
203: "Non-Authoritative Information",
|
|
18
|
+
204: "No Content",
|
|
19
|
+
205: "Reset Content",
|
|
20
|
+
206: "Partial Content",
|
|
21
|
+
207: "Multi-Status",
|
|
22
|
+
208: "Already Reported",
|
|
23
|
+
226: "IM Used",
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const unknownSuccessStatusText = "Unknown Success";
|
|
27
|
+
|
|
28
|
+
const knownSuccessStatusSet = new Set(
|
|
29
|
+
Object.keys(knownSuccessStatusMap).map((s) => parseInt(s, 10))
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
export interface KnownSuccessResponse extends Response {
|
|
33
|
+
status: KnownSuccessStatus;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export const isKnownSuccessResponse = (
|
|
37
|
+
res: Response
|
|
38
|
+
): res is KnownSuccessResponse => knownSuccessStatusSet.has(res.status);
|
|
39
|
+
|
|
40
|
+
export interface UnknownSuccessResponse extends Response {
|
|
41
|
+
status: number;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export const isUnknownSuccessResponse = (
|
|
45
|
+
res: Response
|
|
46
|
+
): res is UnknownSuccessResponse =>
|
|
47
|
+
res.status >= 200 && res.status < 300 && !isKnownSuccessResponse(res);
|
|
48
|
+
|
|
49
|
+
export type SuccessResponse = KnownSuccessResponse | UnknownSuccessResponse;
|
|
50
|
+
|
|
51
|
+
export const isSuccessResponse = (res: Response): res is SuccessResponse =>
|
|
52
|
+
res.status >= 200 && res.status < 300;
|
|
53
|
+
|
|
54
|
+
export const getSuccessStatusText = (res: SuccessResponse): string =>
|
|
55
|
+
isKnownSuccessResponse(res)
|
|
56
|
+
? knownSuccessStatusMap[res.status]
|
|
57
|
+
: unknownSuccessStatusText;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
export const statusCodes = {
|
|
2
|
+
100: "Continue",
|
|
3
|
+
101: "Switching Protocols",
|
|
4
|
+
102: "Processing",
|
|
5
|
+
103: "Early Hints",
|
|
6
|
+
200: "OK",
|
|
7
|
+
201: "Created",
|
|
8
|
+
202: "Accepted",
|
|
9
|
+
203: "Non-Authoritative Information",
|
|
10
|
+
204: "No Content",
|
|
11
|
+
205: "Reset Content",
|
|
12
|
+
206: "Partial Content",
|
|
13
|
+
207: "Multi-Status",
|
|
14
|
+
208: "Already Reported",
|
|
15
|
+
226: "IM Used",
|
|
16
|
+
301: "Moved Permanently",
|
|
17
|
+
302: "Found",
|
|
18
|
+
303: "See Other",
|
|
19
|
+
307: "Temporary Redirect",
|
|
20
|
+
308: "Permanent Redirect",
|
|
21
|
+
400: "Bad Request",
|
|
22
|
+
401: "Unauthorized",
|
|
23
|
+
403: "Forbidden",
|
|
24
|
+
404: "Not Found",
|
|
25
|
+
405: "Method Not Allowed",
|
|
26
|
+
406: "Not Acceptable",
|
|
27
|
+
407: "Proxy Authentication Required",
|
|
28
|
+
408: "Request Timeout",
|
|
29
|
+
409: "Conflict",
|
|
30
|
+
410: "Gone",
|
|
31
|
+
411: "Length Required",
|
|
32
|
+
412: "Precondition Failed",
|
|
33
|
+
413: "Payload Too Large",
|
|
34
|
+
414: "URI Too Long",
|
|
35
|
+
415: "Unsupported Media Type",
|
|
36
|
+
416: "Range Not Satisfiable",
|
|
37
|
+
417: "Expectation Failed",
|
|
38
|
+
418: "I'm a teapot",
|
|
39
|
+
421: "Misdirected Request",
|
|
40
|
+
422: "Unprocessable Entity",
|
|
41
|
+
423: "Locked",
|
|
42
|
+
424: "Failed Dependency",
|
|
43
|
+
425: "Too Early",
|
|
44
|
+
426: "Upgrade Required",
|
|
45
|
+
428: "Precondition Required",
|
|
46
|
+
429: "Too Many Requests",
|
|
47
|
+
431: "Request Header Fields Too Large",
|
|
48
|
+
451: "Unavailable For Legal Reasons",
|
|
49
|
+
500: "Internal Server Error",
|
|
50
|
+
501: "Not Implemented",
|
|
51
|
+
502: "Bad Gateway",
|
|
52
|
+
503: "Service Unavailable",
|
|
53
|
+
504: "Gateway Timeout",
|
|
54
|
+
505: "HTTP Version Not Supported",
|
|
55
|
+
506: "Variant Also Negotiates",
|
|
56
|
+
507: "Insufficient Storage",
|
|
57
|
+
508: "Loop Detected",
|
|
58
|
+
510: "Not Extended",
|
|
59
|
+
511: "Network Authentication Required",
|
|
60
|
+
520: "Unknown Error",
|
|
61
|
+
521: "Web Server Is Down",
|
|
62
|
+
522: "Connection Timed Out",
|
|
63
|
+
523: "Origin Is Unreachable",
|
|
64
|
+
524: "A Timeout Occurred",
|
|
65
|
+
525: "SSL Handshake Failed",
|
|
66
|
+
526: "Invalid SSL Certificate",
|
|
67
|
+
527: "Railgun Error",
|
|
68
|
+
530: "Site Is Frozen",
|
|
69
|
+
};
|
package/tsconfig.json
ADDED