@torthu/jacketui-bring 0.2.4 → 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.
Files changed (73) hide show
  1. package/README.md +0 -2
  2. package/dist/bring.js +5 -4
  3. package/dist/errors/AbortError.js +1 -1
  4. package/dist/errors/ClientError.js +1 -1
  5. package/dist/errors/NetworkError.js +1 -1
  6. package/dist/errors/ServerError.js +1 -1
  7. package/dist/errors/TimeoutError.js +1 -1
  8. package/dist/errors/index.js +6 -6
  9. package/dist/fetch.js +1 -1
  10. package/dist/index.d.ts +2 -0
  11. package/dist/index.js +6 -5
  12. package/dist/request-builder-pipe/headers.js +11 -2
  13. package/dist/request-builder-pipe/index.d.ts +1 -0
  14. package/dist/request-builder-pipe/index.js +20 -19
  15. package/dist/request-builder-pipe/jitter.js +1 -1
  16. package/dist/response-pipe/index.js +6 -6
  17. package/dist/tryFetch.js +17 -1
  18. package/dist/types/BringInit.d.ts +1 -3
  19. package/package.json +27 -9
  20. package/src/bring.ts +0 -207
  21. package/src/errors/AbortError.ts +0 -18
  22. package/src/errors/BringError.ts +0 -24
  23. package/src/errors/ClientError.ts +0 -24
  24. package/src/errors/NetworkError.ts +0 -23
  25. package/src/errors/ServerError.ts +0 -24
  26. package/src/errors/TimeoutError.ts +0 -18
  27. package/src/errors/index.ts +0 -6
  28. package/src/fetch.ts +0 -34
  29. package/src/helpers/exponentialBackoff.ts +0 -28
  30. package/src/helpers/extractCallbacks.ts +0 -23
  31. package/src/helpers/extractRequestInit.ts +0 -30
  32. package/src/helpers/randomJitter.ts +0 -22
  33. package/src/helpers/shouldRetry.ts +0 -40
  34. package/src/index.ts +0 -7
  35. package/src/request-builder-pipe/backoff.ts +0 -22
  36. package/src/request-builder-pipe/body.ts +0 -17
  37. package/src/request-builder-pipe/cache.ts +0 -23
  38. package/src/request-builder-pipe/cors.ts +0 -39
  39. package/src/request-builder-pipe/credentials.ts +0 -33
  40. package/src/request-builder-pipe/header.ts +0 -28
  41. package/src/request-builder-pipe/headers.ts +0 -34
  42. package/src/request-builder-pipe/index.ts +0 -20
  43. package/src/request-builder-pipe/integrity.ts +0 -23
  44. package/src/request-builder-pipe/jitter.ts +0 -17
  45. package/src/request-builder-pipe/jsonBody.ts +0 -12
  46. package/src/request-builder-pipe/keepalive.ts +0 -21
  47. package/src/request-builder-pipe/method.ts +0 -18
  48. package/src/request-builder-pipe/priority.ts +0 -20
  49. package/src/request-builder-pipe/redirect.ts +0 -23
  50. package/src/request-builder-pipe/referrer.ts +0 -21
  51. package/src/request-builder-pipe/referrerPolicy.ts +0 -33
  52. package/src/request-builder-pipe/retry.ts +0 -13
  53. package/src/request-builder-pipe/shouldRetry.ts +0 -11
  54. package/src/request-builder-pipe/timeout.ts +0 -19
  55. package/src/request-builder-pipe/url.ts +0 -18
  56. package/src/response-pipe/arrayBuffer.ts +0 -18
  57. package/src/response-pipe/blob.ts +0 -18
  58. package/src/response-pipe/bytes.ts +0 -18
  59. package/src/response-pipe/formData.ts +0 -18
  60. package/src/response-pipe/index.ts +0 -6
  61. package/src/response-pipe/json.ts +0 -20
  62. package/src/response-pipe/text.ts +0 -18
  63. package/src/tryFetch.ts +0 -32
  64. package/src/types/BringDecorator.ts +0 -5
  65. package/src/types/BringInit.ts +0 -86
  66. package/src/types/ClientErrorResponse.ts +0 -72
  67. package/src/types/InformationalResponse.ts +0 -20
  68. package/src/types/RedirectResponse.ts +0 -20
  69. package/src/types/ServerErrorResponse.ts +0 -56
  70. package/src/types/SuccessResponse.ts +0 -57
  71. package/src/types/statusCodes.ts +0 -69
  72. package/test/bring.spec.ts +0 -149
  73. package/tsconfig.json +0 -21
@@ -1,18 +0,0 @@
1
- import { BringInitDecorator } from "../types/BringDecorator";
2
- import { BringInit } from "../types/BringInit";
3
-
4
- /** url(string | URL): BringInitDecorator
5
- *
6
- * Sets the URL of the request
7
- *
8
- * @see https://developer.mozilla.org/en-US/docs/Web/API/Request/url
9
- *
10
- * @param url - URL to request.
11
- * @returns BringDecorator
12
- */
13
- export const url =
14
- (url: BringInit["url"]): ((init?: BringInit) => BringInit) =>
15
- (init = { url }) => ({
16
- ...init,
17
- url,
18
- });
@@ -1,18 +0,0 @@
1
- import { Result, success } from "@torthu/jacketui-core";
2
- import { BringError } from "../errors";
3
-
4
- /** arrayBuffer(result: Result<Response, BringError>)
5
- *
6
- * Utility function to await Response.arrayBuffer().
7
- *
8
- * @param result Result<Response, BringError>
9
- * @returns Promise<Result<ArrayBuffer, BringError>>
10
- */
11
- export const arrayBuffer = async (result: Result<Response, BringError>) => {
12
- if (result.ok) {
13
- const res = await result.value.arrayBuffer();
14
- return success(res);
15
- } else {
16
- return result;
17
- }
18
- };
@@ -1,18 +0,0 @@
1
- import { Result, success } from "@torthu/jacketui-core";
2
- import { BringError } from "../errors";
3
-
4
- /** blob(result: Result<Response, BringError>)
5
- *
6
- * Utility function to await Response.blob().
7
- *
8
- * @param result Result<Response, BringError>
9
- * @returns Promise<Result<Blob, BringError>>
10
- */
11
- export const blob = async (result: Result<Response, BringError>) => {
12
- if (result.ok) {
13
- const res = await result.value.blob();
14
- return success(res);
15
- } else {
16
- return result;
17
- }
18
- };
@@ -1,18 +0,0 @@
1
- import { Result, success } from "@torthu/jacketui-core";
2
- import { BringError } from "../errors";
3
-
4
- /** bytes(result: Result<Response, BringError>)
5
- *
6
- * Utility function to await Response.bytes().
7
- *
8
- * @param result Result<Response, BringError>
9
- * @returns Promise<Result<Uint8Array, BringError>>
10
- */
11
- export const bytes = async (result: Result<Response, BringError>) => {
12
- if (result.ok) {
13
- const res = await result.value.bytes();
14
- return success(res);
15
- } else {
16
- return result;
17
- }
18
- };
@@ -1,18 +0,0 @@
1
- import { Result, success } from "@torthu/jacketui-core";
2
- import { BringError } from "../errors";
3
-
4
- /** formData(result: Result<Response, BringError>)
5
- *
6
- * Utility function to await Response.formData().
7
- *
8
- * @param result Result<Response, BringError>
9
- * @returns Promise<Result<FormData, BringError>>
10
- */
11
- export const formData = async (result: Result<Response, BringError>) => {
12
- if (result.ok) {
13
- const res = await result.value.formData();
14
- return success(res);
15
- } else {
16
- return result;
17
- }
18
- };
@@ -1,6 +0,0 @@
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,20 +0,0 @@
1
- import { Result, success } from "@torthu/jacketui-core";
2
- import { BringError } from "../errors";
3
-
4
- /** json(result: Result<Response, BringError>)
5
- *
6
- * Utility function to await Response.json().
7
- *
8
- * @param result Result<Response, BringError>
9
- * @returns Promise<Result<T, BringError>>
10
- */
11
- export const json = async <T = unknown>(
12
- result: Result<Response, BringError>
13
- ) => {
14
- if (result.ok) {
15
- const json = await result.value.json();
16
- return success(json as T);
17
- } else {
18
- return result;
19
- }
20
- };
@@ -1,18 +0,0 @@
1
- import { Result, success } from "@torthu/jacketui-core";
2
- import { BringError } from "../errors";
3
-
4
- /** text(result: Result<Response, BringError>)
5
- *
6
- * Utility function to await Response.text().
7
- *
8
- * @param result Result<Response, BringError>
9
- * @returns Promise<Result<string, BringError>>
10
- */
11
- export const text = async (result: Result<Response, BringError>) => {
12
- if (result.ok) {
13
- const res = await result.value.text();
14
- return success(res);
15
- } else {
16
- return result;
17
- }
18
- };
package/src/tryFetch.ts DELETED
@@ -1,32 +0,0 @@
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
- };
@@ -1,5 +0,0 @@
1
- import { BringInit } from "./BringInit";
2
-
3
- export interface BringInitDecorator {
4
- (init: BringInit): BringInit;
5
- }
@@ -1,86 +0,0 @@
1
- import {
2
- AbortError,
3
- BringError,
4
- ClientError,
5
- NetworkError,
6
- ServerError,
7
- TimeoutError,
8
- } from "../errors";
9
-
10
- type TypedArray =
11
- | Int8Array
12
- | Uint8Array
13
- | Uint8ClampedArray
14
- | Int16Array
15
- | Uint16Array
16
- | Int32Array
17
- | Uint32Array
18
- | Float32Array
19
- | Float64Array
20
- | BigInt64Array
21
- | BigUint64Array;
22
-
23
- export interface BringCallbacks {
24
- onAbort?: (error: AbortError) => void;
25
- onNetworkError?: (error: NetworkError) => void;
26
- onError?: (error: BringError) => void;
27
- onRetry?: (retryNum: number) => void;
28
- onSuccess?: (response: Response) => void;
29
- onTimeout?: (error: TimeoutError) => void;
30
- onClientError?: (error: ClientError) => void;
31
- onServerError?: (error: ServerError) => void;
32
- }
33
-
34
- export interface RequestInit {
35
- method?: "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "HEAD" | "OPTIONS";
36
- body?:
37
- | string
38
- | ArrayBuffer
39
- | Blob
40
- | DataView
41
- | File
42
- | FormData
43
- | TypedArray
44
- | URLSearchParams
45
- | ReadableStream;
46
- cache?:
47
- | "default"
48
- | "no-store"
49
- | "reload"
50
- | "no-cache"
51
- | "force-cache"
52
- | "only-if-cached";
53
- credentials?: "omit" | "same-origin" | "include";
54
- headers?: Record<string, string> | Headers;
55
- integrity?: string;
56
- keepalive?: boolean;
57
- mode?: "cors" | "no-cors" | "same-origin";
58
- priority?: "low" | "high" | "auto";
59
- redirect?: "follow" | "error" | "manual";
60
- referrer?: string | "" | "about:client";
61
- referrerPolicy?:
62
- | "no-referrer"
63
- | "no-referrer-when-downgrade"
64
- | "origin"
65
- | "origin-when-cross-origin"
66
- | "same-origin"
67
- | "strict-origin"
68
- | "strict-origin-when-cross-origin"
69
- | "unsafe-url";
70
- // signal?: AbortSignal; // Handled by bring() or the AbortController passed in
71
- }
72
-
73
- export interface RetryBackoff {
74
- retry?: number;
75
- timeout?: number;
76
- shouldRetry?: (response: Response, retryNum: number) => boolean;
77
- backoff?: (retryNum: number) => number;
78
- jitter?: () => number;
79
- }
80
-
81
- export type BringInit = RequestInit &
82
- RetryBackoff &
83
- BringCallbacks & {
84
- url: string | URL;
85
- abortController?: AbortController;
86
- };
@@ -1,72 +0,0 @@
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);
@@ -1,20 +0,0 @@
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);
@@ -1,20 +0,0 @@
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);
@@ -1,56 +0,0 @@
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);
@@ -1,57 +0,0 @@
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;
@@ -1,69 +0,0 @@
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
- };