@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.
Files changed (132) hide show
  1. package/README.md +11 -0
  2. package/dist/bring.d.ts +38 -0
  3. package/dist/bring.js +142 -0
  4. package/dist/errors/AbortError.d.ts +11 -0
  5. package/dist/errors/AbortError.js +14 -0
  6. package/dist/errors/BringError.d.ts +15 -0
  7. package/dist/errors/BringError.js +14 -0
  8. package/dist/errors/ClientError.d.ts +11 -0
  9. package/dist/errors/ClientError.js +19 -0
  10. package/dist/errors/NetworkError.d.ts +11 -0
  11. package/dist/errors/NetworkError.js +18 -0
  12. package/dist/errors/ServerError.d.ts +11 -0
  13. package/dist/errors/ServerError.js +19 -0
  14. package/dist/errors/TimeoutError.d.ts +11 -0
  15. package/dist/errors/TimeoutError.js +14 -0
  16. package/dist/errors/index.d.ts +6 -0
  17. package/dist/errors/index.js +6 -0
  18. package/dist/fetch.d.ts +30 -0
  19. package/dist/fetch.js +32 -0
  20. package/dist/helpers/exponentialBackoff.d.ts +21 -0
  21. package/dist/helpers/exponentialBackoff.js +18 -0
  22. package/dist/helpers/extractCallbacks.d.ts +2 -0
  23. package/dist/helpers/extractCallbacks.js +12 -0
  24. package/dist/helpers/extractRequestInit.d.ts +2 -0
  25. package/dist/helpers/extractRequestInit.js +16 -0
  26. package/dist/helpers/randomJitter.d.ts +15 -0
  27. package/dist/helpers/randomJitter.js +14 -0
  28. package/dist/helpers/shouldRetry.d.ts +22 -0
  29. package/dist/helpers/shouldRetry.js +28 -0
  30. package/dist/index.d.ts +4 -0
  31. package/dist/index.js +4 -0
  32. package/dist/pipe-helpers/backoff.d.ts +14 -0
  33. package/dist/pipe-helpers/backoff.js +16 -0
  34. package/dist/pipe-helpers/body.d.ts +14 -0
  35. package/dist/pipe-helpers/body.js +12 -0
  36. package/dist/pipe-helpers/cache.d.ts +17 -0
  37. package/dist/pipe-helpers/cache.js +18 -0
  38. package/dist/pipe-helpers/cors.d.ts +33 -0
  39. package/dist/pipe-helpers/cors.js +34 -0
  40. package/dist/pipe-helpers/credentials.d.ts +25 -0
  41. package/dist/pipe-helpers/credentials.js +26 -0
  42. package/dist/pipe-helpers/header.d.ts +13 -0
  43. package/dist/pipe-helpers/header.js +25 -0
  44. package/dist/pipe-helpers/index.d.ts +17 -0
  45. package/dist/pipe-helpers/index.js +17 -0
  46. package/dist/pipe-helpers/integrity.d.ts +17 -0
  47. package/dist/pipe-helpers/integrity.js +18 -0
  48. package/dist/pipe-helpers/jitter.d.ts +13 -0
  49. package/dist/pipe-helpers/jitter.js +15 -0
  50. package/dist/pipe-helpers/jsonBody.d.ts +10 -0
  51. package/dist/pipe-helpers/jsonBody.js +8 -0
  52. package/dist/pipe-helpers/keepalive.d.ts +15 -0
  53. package/dist/pipe-helpers/keepalive.js +16 -0
  54. package/dist/pipe-helpers/method.d.ts +12 -0
  55. package/dist/pipe-helpers/method.js +13 -0
  56. package/dist/pipe-helpers/priority.d.ts +15 -0
  57. package/dist/pipe-helpers/priority.js +15 -0
  58. package/dist/pipe-helpers/redirect.d.ts +17 -0
  59. package/dist/pipe-helpers/redirect.js +18 -0
  60. package/dist/pipe-helpers/referrer.d.ts +15 -0
  61. package/dist/pipe-helpers/referrer.js +16 -0
  62. package/dist/pipe-helpers/referrerPolicy.d.ts +25 -0
  63. package/dist/pipe-helpers/referrerPolicy.js +26 -0
  64. package/dist/pipe-helpers/retry.d.ts +10 -0
  65. package/dist/pipe-helpers/retry.js +8 -0
  66. package/dist/pipe-helpers/shouldRetry.d.ts +8 -0
  67. package/dist/pipe-helpers/shouldRetry.js +6 -0
  68. package/dist/pipe-helpers/timeout.d.ts +16 -0
  69. package/dist/pipe-helpers/timeout.js +14 -0
  70. package/dist/tryFetch.d.ts +3 -0
  71. package/dist/tryFetch.js +19 -0
  72. package/dist/types/BringDecorator.d.ts +4 -0
  73. package/dist/types/BringDecorator.js +1 -0
  74. package/dist/types/BringInit.d.ts +37 -0
  75. package/dist/types/BringInit.js +1 -0
  76. package/dist/types/ClientErrorResponse.d.ts +6 -0
  77. package/dist/types/ClientErrorResponse.js +32 -0
  78. package/dist/types/InformationalResponse.d.ts +6 -0
  79. package/dist/types/InformationalResponse.js +8 -0
  80. package/dist/types/RedirectResponse.d.ts +6 -0
  81. package/dist/types/RedirectResponse.js +9 -0
  82. package/dist/types/ServerErrorResponse.d.ts +6 -0
  83. package/dist/types/ServerErrorResponse.js +24 -0
  84. package/dist/types/SuccessResponse.d.ts +13 -0
  85. package/dist/types/SuccessResponse.js +20 -0
  86. package/dist/types/statusCodes.d.ts +69 -0
  87. package/dist/types/statusCodes.js +69 -0
  88. package/package.json +23 -0
  89. package/src/bring.ts +198 -0
  90. package/src/errors/AbortError.ts +18 -0
  91. package/src/errors/BringError.ts +24 -0
  92. package/src/errors/ClientError.ts +24 -0
  93. package/src/errors/NetworkError.ts +23 -0
  94. package/src/errors/ServerError.ts +24 -0
  95. package/src/errors/TimeoutError.ts +18 -0
  96. package/src/errors/index.ts +6 -0
  97. package/src/fetch.ts +34 -0
  98. package/src/helpers/exponentialBackoff.ts +28 -0
  99. package/src/helpers/extractCallbacks.ts +23 -0
  100. package/src/helpers/extractRequestInit.ts +30 -0
  101. package/src/helpers/randomJitter.ts +22 -0
  102. package/src/helpers/shouldRetry.ts +40 -0
  103. package/src/index.ts +4 -0
  104. package/src/pipe-helpers/backoff.ts +22 -0
  105. package/src/pipe-helpers/body.ts +17 -0
  106. package/src/pipe-helpers/cache.ts +23 -0
  107. package/src/pipe-helpers/cors.ts +39 -0
  108. package/src/pipe-helpers/credentials.ts +33 -0
  109. package/src/pipe-helpers/header.ts +28 -0
  110. package/src/pipe-helpers/index.ts +18 -0
  111. package/src/pipe-helpers/integrity.ts +23 -0
  112. package/src/pipe-helpers/jitter.ts +17 -0
  113. package/src/pipe-helpers/jsonBody.ts +13 -0
  114. package/src/pipe-helpers/keepalive.ts +21 -0
  115. package/src/pipe-helpers/method.ts +18 -0
  116. package/src/pipe-helpers/priority.ts +20 -0
  117. package/src/pipe-helpers/redirect.ts +23 -0
  118. package/src/pipe-helpers/referrer.ts +21 -0
  119. package/src/pipe-helpers/referrerPolicy.ts +33 -0
  120. package/src/pipe-helpers/retry.ts +13 -0
  121. package/src/pipe-helpers/shouldRetry.ts +11 -0
  122. package/src/pipe-helpers/timeout.ts +19 -0
  123. package/src/tryFetch.ts +32 -0
  124. package/src/types/BringDecorator.ts +5 -0
  125. package/src/types/BringInit.ts +78 -0
  126. package/src/types/ClientErrorResponse.ts +72 -0
  127. package/src/types/InformationalResponse.ts +20 -0
  128. package/src/types/RedirectResponse.ts +20 -0
  129. package/src/types/ServerErrorResponse.ts +56 -0
  130. package/src/types/SuccessResponse.ts +57 -0
  131. package/src/types/statusCodes.ts +69 -0
  132. package/tsconfig.json +11 -0
package/README.md ADDED
@@ -0,0 +1,11 @@
1
+ # JacketUI Bring
2
+
3
+ Bring is a fetch wrapper that provides a simple API for making HTTP requests. It supports retrying, timeouts and callbacks. And will return a Result ({ok: true, value} | {ok: false, error}) instead of throwing errors.
4
+
5
+ ## Philosophy
6
+
7
+ We want flexibility in our components and escape hatches in order to solve edge cases and make components truly expandable and reusable.
8
+
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
@@ -0,0 +1,38 @@
1
+ import { Result, AbortablePromise } from "@torthu/jacketui-core";
2
+ import { BringInit } from "./types/BringInit";
3
+ import { BringError } from "./errors";
4
+ /** bring({url: string | URL})
5
+ *
6
+ * A wrapper around the fetch API that adds support for timeouts, retries, and aborts.
7
+ * It also adds support for custom error handling and success handling.
8
+ * It returns a promise that resolves to a Result object.
9
+ * The returned promise is extended with an abort method that can be used to abort the fetch request.
10
+ *
11
+ * The Result object contains either a Response object or a BringError.
12
+ *
13
+ * @param init {BringInit | BringError} RequestInit + retries, timeout, onSuccess, onError, onAbort, onRetry, onTimeout, onClientError, onServerError
14
+ * @param init.url The URL to fetch.
15
+ * @param init.abortController (optional) An AbortController object to abort the fetch request. (default: new AbortController())
16
+ * @param init.timeout (optional) The timeout in milliseconds. (default: 0)
17
+ * @param init.retry (optional) The number of times to retry the fetch request. (default: 0)
18
+ * @param init.onSuccess (optional) A callback function that is called when the fetch request is successful.
19
+ * @param init.onError (optional) A callback function that is called when the fetch request fails.
20
+ * @param init.onAbort (optional) A callback function that is called when the fetch request is aborted.
21
+ * @param init.onRetry (optional) A callback function that is called when the fetch request is retried.
22
+ * @param init.onTimeout (optional) A callback function that is called when the fetch request times out.
23
+ * @param init.onClientError (optional) A callback function that is called when the fetch request fails with a client error.
24
+ * @param init.onServerError (optional) A callback function that is called when the fetch request fails with a server error.
25
+ * @param init.body (optional) The body of the fetch request.
26
+ * @param init.headers (optional) The headers of the fetch request.
27
+ * @param init.method (optional) The method of the fetch request.
28
+ * @param init.mode (optional) The mode of the fetch request.
29
+ * @param init.credentials (optional) The credentials of the fetch request.
30
+ * @param init.cache (optional) The cache of the fetch request.
31
+ * @param init.redirect (optional) The redirect of the fetch request.
32
+ * @param init.referrer (optional) The referrer of the fetch request.
33
+ * @param init.referrerPolicy (optional) The referrerPolicy of the fetch request.
34
+ * @param init.integrity (optional) The integrity of the fetch request.
35
+ * @param init.keepalive (optional) The keepalive of the fetch request.
36
+ * @returns AbortablePromise<Result<Response, BringError>>
37
+ */
38
+ export declare const bring: (init: BringInit | BringError) => AbortablePromise<Result<Response, BringError>>;
package/dist/bring.js ADDED
@@ -0,0 +1,142 @@
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, ServerError, TimeoutError, } from "./errors";
5
+ import { tryFetch } from "./tryFetch";
6
+ /** bring({url: string | URL})
7
+ *
8
+ * A wrapper around the fetch API that adds support for timeouts, retries, and aborts.
9
+ * It also adds support for custom error handling and success handling.
10
+ * It returns a promise that resolves to a Result object.
11
+ * The returned promise is extended with an abort method that can be used to abort the fetch request.
12
+ *
13
+ * The Result object contains either a Response object or a BringError.
14
+ *
15
+ * @param init {BringInit | BringError} RequestInit + retries, timeout, onSuccess, onError, onAbort, onRetry, onTimeout, onClientError, onServerError
16
+ * @param init.url The URL to fetch.
17
+ * @param init.abortController (optional) An AbortController object to abort the fetch request. (default: new AbortController())
18
+ * @param init.timeout (optional) The timeout in milliseconds. (default: 0)
19
+ * @param init.retry (optional) The number of times to retry the fetch request. (default: 0)
20
+ * @param init.onSuccess (optional) A callback function that is called when the fetch request is successful.
21
+ * @param init.onError (optional) A callback function that is called when the fetch request fails.
22
+ * @param init.onAbort (optional) A callback function that is called when the fetch request is aborted.
23
+ * @param init.onRetry (optional) A callback function that is called when the fetch request is retried.
24
+ * @param init.onTimeout (optional) A callback function that is called when the fetch request times out.
25
+ * @param init.onClientError (optional) A callback function that is called when the fetch request fails with a client error.
26
+ * @param init.onServerError (optional) A callback function that is called when the fetch request fails with a server error.
27
+ * @param init.body (optional) The body of the fetch request.
28
+ * @param init.headers (optional) The headers of the fetch request.
29
+ * @param init.method (optional) The method of the fetch request.
30
+ * @param init.mode (optional) The mode of the fetch request.
31
+ * @param init.credentials (optional) The credentials of the fetch request.
32
+ * @param init.cache (optional) The cache of the fetch request.
33
+ * @param init.redirect (optional) The redirect of the fetch request.
34
+ * @param init.referrer (optional) The referrer of the fetch request.
35
+ * @param init.referrerPolicy (optional) The referrerPolicy of the fetch request.
36
+ * @param init.integrity (optional) The integrity of the fetch request.
37
+ * @param init.keepalive (optional) The keepalive of the fetch request.
38
+ * @returns AbortablePromise<Result<Response, BringError>>
39
+ */
40
+ export const bring = (init) => {
41
+ if (init instanceof BringError)
42
+ init = init.requestInit;
43
+ const { url, abortController = new AbortController(), timeout = 0, retry = 0, onSuccess, onError, onAbort, onRetry, onTimeout, onClientError, onServerError, ...requestInit } = init;
44
+ // Configure promise
45
+ const { promise, resolve } = withResolvers();
46
+ const abortablePromise = promise;
47
+ abortablePromise.abort = abortController.abort;
48
+ // Configure timeout signal
49
+ const timeoutSignal = timeout > 0 ? AbortSignal.timeout(timeout) : undefined;
50
+ const signal = AbortSignal.any([abortController.signal, timeoutSignal].filter((s) => !!s));
51
+ tryFetch(url, {
52
+ ...requestInit,
53
+ signal,
54
+ }, init, 0).then(({ error, value }) => {
55
+ if (error) {
56
+ if (abortController.signal.aborted) {
57
+ const abortError = new AbortError(abortController.signal.reason, {
58
+ context: {
59
+ url: url,
60
+ method: requestInit.method ?? "GET",
61
+ reason: abortController.signal.reason,
62
+ },
63
+ url,
64
+ requestInit: init,
65
+ });
66
+ resolve(failure(abortError));
67
+ onAbort?.(abortError);
68
+ onError?.(abortError);
69
+ return;
70
+ }
71
+ if (timeoutSignal?.aborted) {
72
+ const error = new TimeoutError("Timeout", {
73
+ context: { reason: timeoutSignal.reason, timeout },
74
+ url,
75
+ requestInit: init,
76
+ });
77
+ resolve(failure(error));
78
+ onTimeout?.(error);
79
+ onError?.(error);
80
+ return;
81
+ }
82
+ if (error instanceof Error) {
83
+ resolve(failure(new BringError(error.message, {
84
+ cause: error,
85
+ context: { url, method: requestInit.method ?? "GET" },
86
+ url,
87
+ requestInit: init,
88
+ })));
89
+ return;
90
+ }
91
+ if (typeof error === "string") {
92
+ resolve(failure(new BringError(error, { url, requestInit: init })));
93
+ return;
94
+ }
95
+ resolve(failure(new BringError("Unknown error", { url, requestInit: init })));
96
+ }
97
+ else {
98
+ const response = value;
99
+ if (response.ok) {
100
+ resolve(success(response));
101
+ onSuccess?.(response);
102
+ }
103
+ else {
104
+ if (isClientErrorResponse(response)) {
105
+ return failure(new ClientError(response.statusText, {
106
+ context: {
107
+ status: response.status,
108
+ statusText: response.statusText,
109
+ },
110
+ response,
111
+ requestInit: init,
112
+ url,
113
+ }));
114
+ }
115
+ if (isServerErrorResponse(response)) {
116
+ return failure(new ServerError(response.statusText, {
117
+ response,
118
+ requestInit: init,
119
+ url,
120
+ context: {
121
+ status: response.status,
122
+ statusText: response.statusText,
123
+ },
124
+ }));
125
+ }
126
+ const error = new BringError("Fetch failed", {
127
+ context: {
128
+ url,
129
+ status: response.status,
130
+ statusText: response.statusText,
131
+ },
132
+ url: url,
133
+ requestInit: init,
134
+ response,
135
+ });
136
+ resolve(failure(error));
137
+ onError?.(error);
138
+ }
139
+ }
140
+ });
141
+ return abortablePromise;
142
+ };
@@ -0,0 +1,11 @@
1
+ import { BringError } from "./BringError";
2
+ export declare class AbortError extends BringError {
3
+ tag: string;
4
+ }
5
+ /** isAbortError
6
+ *
7
+ * Checks if error is AbortError.
8
+ * An AbortError is either an instance of AbortError or a BringError with tag "AbortError".
9
+ *
10
+ */
11
+ export declare const isAbortError: (error: unknown) => error is AbortError;
@@ -0,0 +1,14 @@
1
+ import { BringError } from "./BringError";
2
+ export class AbortError extends BringError {
3
+ tag = "AbortError";
4
+ }
5
+ /** isAbortError
6
+ *
7
+ * Checks if error is AbortError.
8
+ * An AbortError is either an instance of AbortError or a BringError with tag "AbortError".
9
+ *
10
+ */
11
+ export const isAbortError = (error) => {
12
+ return (error instanceof AbortError ||
13
+ (error instanceof BringError && error.tag === "AbortError"));
14
+ };
@@ -0,0 +1,15 @@
1
+ import { BringInit } from "../types/BringInit";
2
+ import { JuiError, JuiErrorOptions } from "@torthu/jacketui-core";
3
+ export interface BringErrorOptions extends JuiErrorOptions {
4
+ response?: Response;
5
+ request?: Request;
6
+ requestInit: BringInit;
7
+ url: string | URL;
8
+ }
9
+ export declare class BringError extends JuiError {
10
+ readonly response?: BringErrorOptions["response"];
11
+ readonly request?: BringErrorOptions["request"];
12
+ readonly requestInit: BringErrorOptions["requestInit"];
13
+ readonly url: BringErrorOptions["url"];
14
+ constructor(message: string, options: BringErrorOptions);
15
+ }
@@ -0,0 +1,14 @@
1
+ import { JuiError } from "@torthu/jacketui-core";
2
+ export class BringError extends JuiError {
3
+ response;
4
+ request;
5
+ requestInit;
6
+ url;
7
+ constructor(message, options) {
8
+ super(message, options);
9
+ this.response = options.response;
10
+ this.request = options.request;
11
+ this.requestInit = options.requestInit;
12
+ this.url = options.url;
13
+ }
14
+ }
@@ -0,0 +1,11 @@
1
+ import { BringError } from "./BringError";
2
+ export declare class ClientError extends BringError {
3
+ tag: string;
4
+ }
5
+ /** isClientError
6
+ *
7
+ * Checks if error is ClientError.
8
+ * A ClientError is either an instance of ClientError or a BringError with a status code between 400 and 499.
9
+ *
10
+ */
11
+ export declare const isClientError: (error: unknown) => error is ClientError;
@@ -0,0 +1,19 @@
1
+ import { BringError } from "./BringError";
2
+ export class ClientError extends BringError {
3
+ tag = "ClientError";
4
+ }
5
+ /** isClientError
6
+ *
7
+ * Checks if error is ClientError.
8
+ * A ClientError is either an instance of ClientError or a BringError with a status code between 400 and 499.
9
+ *
10
+ */
11
+ export const isClientError = (error) => {
12
+ if (error instanceof ClientError) {
13
+ return true;
14
+ }
15
+ return (error instanceof BringError &&
16
+ !!error.response &&
17
+ error.response.status >= 400 &&
18
+ error.response.status < 500);
19
+ };
@@ -0,0 +1,11 @@
1
+ import { BringError } from "./BringError";
2
+ export declare class NetworkError extends BringError {
3
+ tag: string;
4
+ }
5
+ /** isNetworkError
6
+ *
7
+ * Checks if error is NetworkError.
8
+ * A NetworkError is either an instance of NetworkError or a BringError with status code 0.
9
+ *
10
+ */
11
+ export declare const isNetworkError: (error: unknown) => error is NetworkError;
@@ -0,0 +1,18 @@
1
+ import { BringError } from "./BringError";
2
+ export class NetworkError extends BringError {
3
+ tag = "NetworkError";
4
+ }
5
+ /** isNetworkError
6
+ *
7
+ * Checks if error is NetworkError.
8
+ * A NetworkError is either an instance of NetworkError or a BringError with status code 0.
9
+ *
10
+ */
11
+ export const isNetworkError = (error) => {
12
+ if (error instanceof NetworkError) {
13
+ return true;
14
+ }
15
+ return (error instanceof BringError &&
16
+ !!error.response &&
17
+ error.response.status === 0);
18
+ };
@@ -0,0 +1,11 @@
1
+ import { BringError } from "./BringError";
2
+ export declare class ServerError extends BringError {
3
+ tag: string;
4
+ }
5
+ /** isServerError
6
+ *
7
+ * Checks if error is ServerError.
8
+ * A ServerError is either an instance of ServerError or a BringError with status code >= 500 and < 600.
9
+ *
10
+ */
11
+ export declare const isServerError: (error: unknown) => error is ServerError;
@@ -0,0 +1,19 @@
1
+ import { BringError } from "./BringError";
2
+ export class ServerError extends BringError {
3
+ tag = "ServerError";
4
+ }
5
+ /** isServerError
6
+ *
7
+ * Checks if error is ServerError.
8
+ * A ServerError is either an instance of ServerError or a BringError with status code >= 500 and < 600.
9
+ *
10
+ */
11
+ export const isServerError = (error) => {
12
+ if (error instanceof ServerError) {
13
+ return true;
14
+ }
15
+ return (error instanceof BringError &&
16
+ !!error.response &&
17
+ error.response.status >= 500 &&
18
+ error.response.status < 600);
19
+ };
@@ -0,0 +1,11 @@
1
+ import { BringError } from "./BringError";
2
+ export declare class TimeoutError extends BringError {
3
+ tag: string;
4
+ }
5
+ /** isTimeoutError
6
+ *
7
+ * Checks if error is TimeoutError.
8
+ * A TimeoutError is either an instance of TimeoutError or a BringError with tag "TimeoutError".
9
+ *
10
+ */
11
+ export declare const isTimeoutError: (error: unknown) => error is TimeoutError;
@@ -0,0 +1,14 @@
1
+ import { BringError } from "./BringError";
2
+ export class TimeoutError extends BringError {
3
+ tag = "TimeoutError";
4
+ }
5
+ /** isTimeoutError
6
+ *
7
+ * Checks if error is TimeoutError.
8
+ * A TimeoutError is either an instance of TimeoutError or a BringError with tag "TimeoutError".
9
+ *
10
+ */
11
+ export const isTimeoutError = (error) => {
12
+ return (error instanceof TimeoutError ||
13
+ (error instanceof BringError && error.tag === "TimeoutError"));
14
+ };
@@ -0,0 +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";
@@ -0,0 +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";
@@ -0,0 +1,30 @@
1
+ import { BringInit } from "./types/BringInit";
2
+ /** fetch(url: string, init?: Omit<BringInit, "url">): AbortablePromise<Result<Response, BringError>>
3
+ *
4
+ * Wraps bring in a fetch-like API.
5
+ * This is a convenience function for bring.
6
+ *
7
+ * Some caveats:
8
+ * - BringInit expects and AbortController instead of a signal.
9
+ * - The first argument cannot be a Request object.
10
+ *
11
+ * @note This is not a drop-in replacement for the Fetch API.
12
+ *
13
+ * @note bring does not throw errors, but will return a Result.
14
+ *
15
+ * @example
16
+ * const result = await fetch("https://example.com", { method: "GET" });
17
+ * if (result.ok) {
18
+ * const data = await result.value.json();
19
+ * console.log(data);
20
+ * } else {
21
+ * console.error(result.error);
22
+ * }
23
+ *
24
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
25
+ *
26
+ * @param url string | URL - The URL to fetch.
27
+ * @param init BringInit - extends RequestInit from the Fetch API.
28
+ * @returns AbortablePromise<Result<Response, BringError>> - A promise that resolves to a Result object containing either a Response or a BringError.
29
+ */
30
+ export declare const fetch: (url: string | URL, init?: Omit<BringInit, "url">) => import("@torthu/jacketui-core").AbortablePromise<import("@torthu/jacketui-core").Result<Response, import("./errors").BringError>>;
package/dist/fetch.js ADDED
@@ -0,0 +1,32 @@
1
+ import { bring } from "./bring";
2
+ /** fetch(url: string, init?: Omit<BringInit, "url">): AbortablePromise<Result<Response, BringError>>
3
+ *
4
+ * Wraps bring in a fetch-like API.
5
+ * This is a convenience function for bring.
6
+ *
7
+ * Some caveats:
8
+ * - BringInit expects and AbortController instead of a signal.
9
+ * - The first argument cannot be a Request object.
10
+ *
11
+ * @note This is not a drop-in replacement for the Fetch API.
12
+ *
13
+ * @note bring does not throw errors, but will return a Result.
14
+ *
15
+ * @example
16
+ * const result = await fetch("https://example.com", { method: "GET" });
17
+ * if (result.ok) {
18
+ * const data = await result.value.json();
19
+ * console.log(data);
20
+ * } else {
21
+ * console.error(result.error);
22
+ * }
23
+ *
24
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
25
+ *
26
+ * @param url string | URL - The URL to fetch.
27
+ * @param init BringInit - extends RequestInit from the Fetch API.
28
+ * @returns AbortablePromise<Result<Response, BringError>> - A promise that resolves to a Result object containing either a Response or a BringError.
29
+ */
30
+ export const fetch = (url, init) => {
31
+ return bring({ url, ...init });
32
+ };
@@ -0,0 +1,21 @@
1
+ export interface BackoffOptions {
2
+ base?: number;
3
+ factor?: number;
4
+ max?: number;
5
+ min?: number;
6
+ }
7
+ /** backoff(retryNum: number): number
8
+ *
9
+ * Backoff is the time to wait before retrying a request. It is calculated using an exponential backoff algorithm.
10
+ *
11
+ * The formula is: base * factor^(retryNum - 1)
12
+ *
13
+ * @param retryNum The number of times the request has been retried
14
+ * @param options
15
+ * @param options.base Base backoff time in milliseconds
16
+ * @param options.factor Multiplier for the backoff time
17
+ * @param options.max Maximum backoff time in milliseconds
18
+ * @param options.min Minimum backoff time in milliseconds
19
+ * @returns number
20
+ */
21
+ export declare const exponentialBackoff: (retryNum: number, { base, factor, max, min }?: BackoffOptions) => number;
@@ -0,0 +1,18 @@
1
+ /** backoff(retryNum: number): number
2
+ *
3
+ * Backoff is the time to wait before retrying a request. It is calculated using an exponential backoff algorithm.
4
+ *
5
+ * The formula is: base * factor^(retryNum - 1)
6
+ *
7
+ * @param retryNum The number of times the request has been retried
8
+ * @param options
9
+ * @param options.base Base backoff time in milliseconds
10
+ * @param options.factor Multiplier for the backoff time
11
+ * @param options.max Maximum backoff time in milliseconds
12
+ * @param options.min Minimum backoff time in milliseconds
13
+ * @returns number
14
+ */
15
+ export const exponentialBackoff = (retryNum, { base = 500, factor = 1.5, max = 10000, min = 500 } = {}) => {
16
+ const calculatedBackoff = base * Math.pow(factor, retryNum - 1);
17
+ return Math.min(Math.max(calculatedBackoff, min), max);
18
+ };
@@ -0,0 +1,2 @@
1
+ import { BringCallbacks, BringInit } from "../types/BringInit";
2
+ export declare const extractCallbacks: (init: BringInit) => BringCallbacks;
@@ -0,0 +1,12 @@
1
+ export const extractCallbacks = (init) => {
2
+ const { onSuccess, onError, onAbort, onRetry, onTimeout, onClientError, onServerError, } = init;
3
+ return {
4
+ onSuccess,
5
+ onError,
6
+ onAbort,
7
+ onRetry,
8
+ onTimeout,
9
+ onClientError,
10
+ onServerError,
11
+ };
12
+ };
@@ -0,0 +1,2 @@
1
+ import { BringInit, RequestInit } from "../types/BringInit";
2
+ export declare const extractRequestInit: (init: BringInit) => RequestInit;
@@ -0,0 +1,16 @@
1
+ export const extractRequestInit = (init) => {
2
+ const { method, headers, body, mode, credentials, cache, redirect, referrer, referrerPolicy, integrity, keepalive, } = init;
3
+ return {
4
+ method,
5
+ headers,
6
+ body,
7
+ mode,
8
+ credentials,
9
+ cache,
10
+ redirect,
11
+ referrer,
12
+ referrerPolicy,
13
+ integrity,
14
+ keepalive,
15
+ };
16
+ };
@@ -0,0 +1,15 @@
1
+ export interface JitterOptions {
2
+ min?: number;
3
+ max?: number;
4
+ }
5
+ /** jitter(backoff: number): number
6
+ *
7
+ * Jitter is a random value added to the backoff time to prevent a thundering herd problem (when multiple clients retry at the same time).
8
+ *
9
+ * @param backoff The backoff time in milliseconds
10
+ * @param options
11
+ * @param options.min (default 0) Minimum jitter time in milliseconds
12
+ * @param options.max (default 100) Maximum jitter time in milliseconds
13
+ * @returns number
14
+ */
15
+ export declare const randomJitter: ({ min, max, }?: JitterOptions) => number;
@@ -0,0 +1,14 @@
1
+ /** jitter(backoff: number): number
2
+ *
3
+ * Jitter is a random value added to the backoff time to prevent a thundering herd problem (when multiple clients retry at the same time).
4
+ *
5
+ * @param backoff The backoff time in milliseconds
6
+ * @param options
7
+ * @param options.min (default 0) Minimum jitter time in milliseconds
8
+ * @param options.max (default 100) Maximum jitter time in milliseconds
9
+ * @returns number
10
+ */
11
+ export const randomJitter = ({ min = 0, max = 100, } = {}) => {
12
+ const jitter = Math.random() * max;
13
+ return jitter < min ? min : jitter;
14
+ };
@@ -0,0 +1,22 @@
1
+ import { Failure } from "@torthu/jacketui-core";
2
+ import { BringError } from "../errors";
3
+ interface RetryOptions {
4
+ retryStatuses?: number[];
5
+ }
6
+ /** shouldRetry(failure: Failure<BringError>, options?: RetryOptions): boolean
7
+ *
8
+ * Determines if a request should be retried based on the failure and options.
9
+ * By default, it retries on 0, 408, 425, 429, 500, 502, 503, and 504 status codes.
10
+ *
11
+ * Additionally if there is no response (i.e the request failed before a response was received),
12
+ * it will retry.
13
+ *
14
+ * You can override this behavior by passing a custom `retryStatuses` array.
15
+ *
16
+ * @param failure Failure<BringError>
17
+ * @param options RetryOptions
18
+ * @param options.retryStatuses Array of status codes to retry on
19
+ * @returns boolean
20
+ */
21
+ export declare const shouldRetry: (failure: Failure<BringError>, { retryStatuses }?: RetryOptions) => boolean;
22
+ export {};
@@ -0,0 +1,28 @@
1
+ const defaultRetryStatuses = [0, 408, 425, 429, 500, 502, 503, 504];
2
+ /** shouldRetry(failure: Failure<BringError>, options?: RetryOptions): boolean
3
+ *
4
+ * Determines if a request should be retried based on the failure and options.
5
+ * By default, it retries on 0, 408, 425, 429, 500, 502, 503, and 504 status codes.
6
+ *
7
+ * Additionally if there is no response (i.e the request failed before a response was received),
8
+ * it will retry.
9
+ *
10
+ * You can override this behavior by passing a custom `retryStatuses` array.
11
+ *
12
+ * @param failure Failure<BringError>
13
+ * @param options RetryOptions
14
+ * @param options.retryStatuses Array of status codes to retry on
15
+ * @returns boolean
16
+ */
17
+ export const shouldRetry = (failure, { retryStatuses = defaultRetryStatuses } = {}) => {
18
+ const error = failure.error;
19
+ if (error.response) {
20
+ if (retryStatuses.includes(error.response.status)) {
21
+ return true;
22
+ }
23
+ else {
24
+ return false;
25
+ }
26
+ }
27
+ return true;
28
+ };
@@ -0,0 +1,4 @@
1
+ export * from "./bring";
2
+ export * from "./tryFetch";
3
+ export * from "./errors";
4
+ export * from "./pipe-helpers";
package/dist/index.js ADDED
@@ -0,0 +1,4 @@
1
+ export * from "./bring";
2
+ export * from "./tryFetch";
3
+ export * from "./errors";
4
+ export * from "./pipe-helpers";
@@ -0,0 +1,14 @@
1
+ import { BackoffOptions } from "../helpers/exponentialBackoff";
2
+ import { BringInitDecorator } from "../types/BringDecorator";
3
+ /** backoff(options?: BackoffOptions): BringInitDecorator
4
+ *
5
+ * Sets a exponential backoff function for retrying requests.
6
+ *
7
+ * @param options (optional) Backoff options
8
+ * @param options.base Base backoff time in milliseconds
9
+ * @param options.factor Multiplier for the backoff time
10
+ * @param options.max Maximum backoff time in milliseconds
11
+ * @param options.min Minimum backoff time in milliseconds
12
+ * @returns BringInitDecorator
13
+ */
14
+ export declare const backoff: (options?: BackoffOptions) => BringInitDecorator;