@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
@@ -0,0 +1,24 @@
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
+ };
@@ -0,0 +1,23 @@
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
+ };
@@ -0,0 +1,24 @@
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
+ };
@@ -0,0 +1,18 @@
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
+ };
@@ -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";
package/src/fetch.ts ADDED
@@ -0,0 +1,34 @@
1
+ import { bring } from "./bring";
2
+ import { BringInit } from "./types/BringInit";
3
+
4
+ /** fetch(url: string, init?: Omit<BringInit, "url">): AbortablePromise<Result<Response, BringError>>
5
+ *
6
+ * Wraps bring in a fetch-like API.
7
+ * This is a convenience function for bring.
8
+ *
9
+ * Some caveats:
10
+ * - BringInit expects and AbortController instead of a signal.
11
+ * - The first argument cannot be a Request object.
12
+ *
13
+ * @note This is not a drop-in replacement for the Fetch API.
14
+ *
15
+ * @note bring does not throw errors, but will return a Result.
16
+ *
17
+ * @example
18
+ * const result = await fetch("https://example.com", { method: "GET" });
19
+ * if (result.ok) {
20
+ * const data = await result.value.json();
21
+ * console.log(data);
22
+ * } else {
23
+ * console.error(result.error);
24
+ * }
25
+ *
26
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
27
+ *
28
+ * @param url string | URL - The URL to fetch.
29
+ * @param init BringInit - extends RequestInit from the Fetch API.
30
+ * @returns AbortablePromise<Result<Response, BringError>> - A promise that resolves to a Result object containing either a Response or a BringError.
31
+ */
32
+ export const fetch = (url: string | URL, init?: Omit<BringInit, "url">) => {
33
+ return bring({ url, ...init });
34
+ };
@@ -0,0 +1,28 @@
1
+ export interface BackoffOptions {
2
+ base?: number;
3
+ factor?: number;
4
+ max?: number;
5
+ min?: number;
6
+ }
7
+
8
+ /** backoff(retryNum: number): number
9
+ *
10
+ * Backoff is the time to wait before retrying a request. It is calculated using an exponential backoff algorithm.
11
+ *
12
+ * The formula is: base * factor^(retryNum - 1)
13
+ *
14
+ * @param retryNum The number of times the request has been retried
15
+ * @param options
16
+ * @param options.base Base backoff time in milliseconds
17
+ * @param options.factor Multiplier for the backoff time
18
+ * @param options.max Maximum backoff time in milliseconds
19
+ * @param options.min Minimum backoff time in milliseconds
20
+ * @returns number
21
+ */
22
+ export const exponentialBackoff = (
23
+ retryNum: number,
24
+ { base = 500, factor = 1.5, max = 10000, min = 500 }: BackoffOptions = {}
25
+ ): number => {
26
+ const calculatedBackoff = base * Math.pow(factor, retryNum - 1);
27
+ return Math.min(Math.max(calculatedBackoff, min), max);
28
+ };
@@ -0,0 +1,23 @@
1
+ import { BringCallbacks, BringInit } from "../types/BringInit";
2
+
3
+ export const extractCallbacks = (init: BringInit): BringCallbacks => {
4
+ const {
5
+ onSuccess,
6
+ onError,
7
+ onAbort,
8
+ onRetry,
9
+ onTimeout,
10
+ onClientError,
11
+ onServerError,
12
+ } = init;
13
+
14
+ return {
15
+ onSuccess,
16
+ onError,
17
+ onAbort,
18
+ onRetry,
19
+ onTimeout,
20
+ onClientError,
21
+ onServerError,
22
+ };
23
+ };
@@ -0,0 +1,30 @@
1
+ import { BringInit, RequestInit } from "../types/BringInit";
2
+
3
+ export const extractRequestInit = (init: BringInit): RequestInit => {
4
+ const {
5
+ method,
6
+ headers,
7
+ body,
8
+ mode,
9
+ credentials,
10
+ cache,
11
+ redirect,
12
+ referrer,
13
+ referrerPolicy,
14
+ integrity,
15
+ keepalive,
16
+ } = init;
17
+ return {
18
+ method,
19
+ headers,
20
+ body,
21
+ mode,
22
+ credentials,
23
+ cache,
24
+ redirect,
25
+ referrer,
26
+ referrerPolicy,
27
+ integrity,
28
+ keepalive,
29
+ };
30
+ };
@@ -0,0 +1,22 @@
1
+ export interface JitterOptions {
2
+ min?: number;
3
+ max?: number;
4
+ }
5
+
6
+ /** jitter(backoff: number): number
7
+ *
8
+ * Jitter is a random value added to the backoff time to prevent a thundering herd problem (when multiple clients retry at the same time).
9
+ *
10
+ * @param backoff The backoff time in milliseconds
11
+ * @param options
12
+ * @param options.min (default 0) Minimum jitter time in milliseconds
13
+ * @param options.max (default 100) Maximum jitter time in milliseconds
14
+ * @returns number
15
+ */
16
+ export const randomJitter = ({
17
+ min = 0,
18
+ max = 100,
19
+ }: JitterOptions = {}): number => {
20
+ const jitter = Math.random() * max;
21
+ return jitter < min ? min : jitter;
22
+ };
@@ -0,0 +1,40 @@
1
+ import { Failure } from "@torthu/jacketui-core";
2
+ import { BringError } from "../errors";
3
+
4
+ const defaultRetryStatuses = [0, 408, 425, 429, 500, 502, 503, 504];
5
+
6
+ interface RetryOptions {
7
+ retryStatuses?: number[];
8
+ }
9
+
10
+ /** shouldRetry(failure: Failure<BringError>, options?: RetryOptions): boolean
11
+ *
12
+ * Determines if a request should be retried based on the failure and options.
13
+ * By default, it retries on 0, 408, 425, 429, 500, 502, 503, and 504 status codes.
14
+ *
15
+ * Additionally if there is no response (i.e the request failed before a response was received),
16
+ * it will retry.
17
+ *
18
+ * You can override this behavior by passing a custom `retryStatuses` array.
19
+ *
20
+ * @param failure Failure<BringError>
21
+ * @param options RetryOptions
22
+ * @param options.retryStatuses Array of status codes to retry on
23
+ * @returns boolean
24
+ */
25
+ export const shouldRetry = (
26
+ failure: Failure<BringError>,
27
+ { retryStatuses = defaultRetryStatuses }: RetryOptions = {}
28
+ ): boolean => {
29
+ const error = failure.error;
30
+
31
+ if (error.response) {
32
+ if (retryStatuses.includes(error.response.status)) {
33
+ return true;
34
+ } else {
35
+ return false;
36
+ }
37
+ }
38
+
39
+ return true;
40
+ };
package/src/index.ts 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,22 @@
1
+ import {
2
+ BackoffOptions,
3
+ exponentialBackoff,
4
+ } from "../helpers/exponentialBackoff";
5
+ import { BringInitDecorator } from "../types/BringDecorator";
6
+
7
+ /** backoff(options?: BackoffOptions): BringInitDecorator
8
+ *
9
+ * Sets a exponential backoff function for retrying requests.
10
+ *
11
+ * @param options (optional) Backoff options
12
+ * @param options.base Base backoff time in milliseconds
13
+ * @param options.factor Multiplier for the backoff time
14
+ * @param options.max Maximum backoff time in milliseconds
15
+ * @param options.min Minimum backoff time in milliseconds
16
+ * @returns BringInitDecorator
17
+ */
18
+ export const backoff = (options?: BackoffOptions): BringInitDecorator => {
19
+ const backoffFun = (retryNum: number) =>
20
+ exponentialBackoff(retryNum, options);
21
+ return (init) => ({ ...init, backoff: backoffFun });
22
+ };
@@ -0,0 +1,17 @@
1
+ import { BringInitDecorator } from "../types/BringDecorator";
2
+ import { BringInit } from "../types/BringInit";
3
+
4
+ /** body(body): BringInitDecorator
5
+ *
6
+ * Sets the body of the request.
7
+ *
8
+ * @note This is a low-level helper. For JSON bodies, use `jsonBody` instead or manually JSON.stringify first.
9
+ *
10
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/RequestInit#body
11
+ *
12
+ * @param body
13
+ * @returns BringInitDecorator
14
+ */
15
+ export const body =
16
+ (body: BringInit["body"]): BringInitDecorator =>
17
+ (init) => ({ ...init, body });
@@ -0,0 +1,23 @@
1
+ import { BringInitDecorator } from "../types/BringDecorator";
2
+ import { BringInit } from "../types/BringInit";
3
+
4
+ /** cache(cacheMode): BringInitDecorator
5
+ *
6
+ * Sets the cache mode for the request.
7
+ * Possible values are "default", "no-store", "reload", "no-cache", "force-cache", and "only-if-cached".
8
+ * The default value is "default".
9
+ *
10
+ * @example
11
+ * const customGet = (url: string) => pipe(cache("no-cache"), method("GET"), bring)({ url })
12
+ *
13
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/Request/cache
14
+ *
15
+ * @param cacheMode - The cache mode to use for the request. "default", "no-store", "reload", "no-cache", "force-cache", and "only-if-cached"
16
+ * @returns BringInitDecorator
17
+ */
18
+ export const cache =
19
+ (cacheMode: BringInit["cache"] = "default"): BringInitDecorator =>
20
+ (init) => {
21
+ init.cache = cacheMode;
22
+ return init;
23
+ };
@@ -0,0 +1,39 @@
1
+ import { BringInitDecorator } from "../types/BringDecorator";
2
+ import { BringInit } from "../types/BringInit";
3
+
4
+ /** cors(corsMode): BringInitDecorator
5
+ *
6
+ * Sets the CORS mode for the request.
7
+ *
8
+ * The default value is "cors".
9
+ *
10
+ * Possible values are: "cors", "no-cors", "same-origin".
11
+ * - "cors": CORS mode is enabled. This is the default value.
12
+ * - "no-cors": CORS mode is disabled.
13
+ * - "same-origin": CORS mode is enabled only for same-origin requests.
14
+ *
15
+ * An origin is the combination of protocol, host, and port.
16
+ * The path is not included in the origin.
17
+ *
18
+ * For example,
19
+ * - https://example.com is an origin.
20
+ * - https://example.com/path is the same origin.
21
+ * - https://example.com:8080 is a different origin.
22
+ * - https://sub.example.com/path is a different origin.
23
+ * - http://example.com is a different origin.
24
+ *
25
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/Request/mode
26
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
27
+ *
28
+ * @example
29
+ * const customGet = (url: string) => pipe(cors("no-cors"), method("GET"), bring)({ url }
30
+ *
31
+ * @param corsMode - The mode to use for the request. "cors", "no-cors", "same-origin" (default: "cors").
32
+ * @returns BringInitDecorator
33
+ */
34
+ export const cors =
35
+ (corsMode: BringInit["mode"] = "cors"): BringInitDecorator =>
36
+ (init) => {
37
+ init.mode = corsMode;
38
+ return init;
39
+ };
@@ -0,0 +1,33 @@
1
+ import { BringInitDecorator } from "../types/BringDecorator";
2
+ import { BringInit } from "../types/BringInit";
3
+
4
+ /** credentials(credentialsMode): BringInitDecorator
5
+ *
6
+ * Sets the credentials mode for the request.
7
+ *
8
+ * Credentials are cookies, TLS client certificates, or authentication headers containing a username and password.
9
+ *
10
+ * Possible values are:
11
+ * - "omit": Never send cookies.
12
+ * - "same-origin": Send cookies only if the URL is on the same origin as the calling script. This is the default value.
13
+ * - "include": Always send cookies, even for cross-origin calls.
14
+ *
15
+ * The default value is "same-origin".
16
+ *
17
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/Request/credentials
18
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#including_credentials
19
+ *
20
+ * @example
21
+ * const customGet = (url: string) => pipe(credentials("include"), method("GET"), bring)({ url })
22
+ *
23
+ * @param credentialsMode
24
+ * @returns
25
+ */
26
+ export const credentials =
27
+ (
28
+ credentialsMode: BringInit["credentials"] = "same-origin"
29
+ ): BringInitDecorator =>
30
+ (init) => {
31
+ init.credentials = credentialsMode;
32
+ return init;
33
+ };
@@ -0,0 +1,28 @@
1
+ import { BringInitDecorator } from "../types/BringDecorator";
2
+
3
+ /** header(key: string, val: string): BringInitDecorator
4
+ *
5
+ * Sets a header on the request.
6
+ * If the header already exists, it will be overwritten.
7
+ *
8
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/Headers
9
+ *
10
+ * @param key The name of the header to set.
11
+ * @param val The value of the header to set.
12
+ * @returns BringDecorator
13
+ */
14
+ export const header = (key: string, val: string): BringInitDecorator => {
15
+ return (init) => {
16
+ init.headers ??= new Headers();
17
+
18
+ if (init.headers instanceof Headers) {
19
+ !init.headers.has(key)
20
+ ? init.headers.append(key, val)
21
+ : init.headers.set(key, val);
22
+ } else {
23
+ init.headers[key] = val;
24
+ }
25
+
26
+ return init;
27
+ };
28
+ };
@@ -0,0 +1,18 @@
1
+ export * from "./cache";
2
+ export * from "./cors";
3
+ export * from "./credentials";
4
+ export * from "./header";
5
+ export * from "./integrity";
6
+ export * from "./keepalive";
7
+ export * from "./method";
8
+ export * from "./redirect";
9
+ export * from "./referrer";
10
+ export * from "./referrerPolicy";
11
+ export * from "./retry";
12
+ export * from "./body";
13
+ export * from "./jsonBody";
14
+ export * from "./priority";
15
+
16
+ export * from "./backoff";
17
+ export * from "./jitter";
18
+ export * from "./timeout";
@@ -0,0 +1,23 @@
1
+ import { BringInitDecorator } from "../types/BringDecorator";
2
+ import { BringInit } from "../types/BringInit";
3
+
4
+ /** integrity(integrityMode): BringInitDecorator
5
+ *
6
+ * Sets the integrity mode of the request.
7
+ *
8
+ * The format of this option is <hash-algo>-<hash-source> where:
9
+ * - <hash-algo> is one of the following values: sha256, sha384, or sha512
10
+ * - <hash-source> is the Base64-encoding of the result of hashing the resource with the specified hash algorithm.
11
+ *
12
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/RequestInit#integrity
13
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/Request/integrity
14
+ *
15
+ * @param integrityMode
16
+ * @returns BringDecorator
17
+ */
18
+ export const integrity =
19
+ (integrityMode: BringInit["integrity"] = ""): BringInitDecorator =>
20
+ (init) => {
21
+ init.integrity = integrityMode;
22
+ return init;
23
+ };
@@ -0,0 +1,17 @@
1
+ import { JitterOptions, randomJitter } from "../helpers/randomJitter";
2
+ import { BringInitDecorator } from "../types/BringDecorator";
3
+
4
+ /** jitter(options?: JitterOptions): BringInitDecorator
5
+ *
6
+ * Sets a jitter function for retrying requests.
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 options (optional) Jitter options
10
+ * @param options.max (default 100) Maximum jitter time in milliseconds
11
+ * @param options.min (default 0) Minimum jitter time in milliseconds
12
+ * @returns BringInitDecorator
13
+ */
14
+ export const jitter = (options?: JitterOptions): BringInitDecorator => {
15
+ const jitterFun = () => randomJitter(options);
16
+ return (init) => ({ ...init, backoff: jitterFun });
17
+ };
@@ -0,0 +1,13 @@
1
+ import { BringInitDecorator } from "../types/BringDecorator";
2
+ import { BringInit } from "../types/BringInit";
3
+
4
+ /** jsonBody(body: BringInit["body"]): BringInitDecorator
5
+ *
6
+ * Sets the body of the request to a JSON string.
7
+ *
8
+ * @param body Any JSON serializable value
9
+ * @returns BringInitDecorator
10
+ */
11
+ export const jsonBody =
12
+ (body: BringInit["body"]): BringInitDecorator =>
13
+ (init) => ({ ...init, body: JSON.stringify(body) });
@@ -0,0 +1,21 @@
1
+ import { BringInitDecorator } from "../types/BringDecorator";
2
+ import { BringInit } from "../types/BringInit";
3
+
4
+ /** keepalive(boolean): BringInitDecorator
5
+ *
6
+ * Sets the keepalive mode of the request.
7
+ *
8
+ * If set to true, the request will be kept alive until the browser is closed.
9
+ *
10
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/Request/keepalive
11
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#keepalive
12
+ *
13
+ * @param keepaliveMode
14
+ * @returns
15
+ */
16
+ export const keepalive =
17
+ (keepaliveMode: BringInit["keepalive"] = false): BringInitDecorator =>
18
+ (init) => {
19
+ init.keepalive = keepaliveMode;
20
+ return init;
21
+ };
@@ -0,0 +1,18 @@
1
+ import { BringInitDecorator } from "../types/BringDecorator";
2
+ import { BringInit } from "../types/BringInit";
3
+
4
+ /** method(string): BringInitDecorator
5
+ *
6
+ * Sets the HTTP method of the request.
7
+ *
8
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/Request/method
9
+ *
10
+ * @param method - The HTTP method to use for the request.
11
+ * @returns BringDecorator
12
+ */
13
+ export const method =
14
+ (method: BringInit["method"] = "GET"): BringInitDecorator =>
15
+ (init) => ({
16
+ ...init,
17
+ method,
18
+ });
@@ -0,0 +1,20 @@
1
+ import { BringInitDecorator } from "../types/BringDecorator";
2
+ import { BringInit } from "../types/BringInit";
3
+
4
+ /** priority("auto" | "high" | "low"): BringInitDecorator
5
+ *
6
+ * The priority of the request relative to other requests of the same type.
7
+ *
8
+ * Possible values are:
9
+ * - "auto" (default) No user preference for the fetch priority
10
+ * - "high" A high priority fetch request relative to other requests of the same type.
11
+ * - "low" A low priority fetch request relative to other requests of the same type.
12
+ *
13
+ * @param priorityMode "auto" | "high" | "low
14
+ * @returns BringInitDecorator
15
+ */
16
+ export const priority = (
17
+ priorityMode: BringInit["priority"] = "auto"
18
+ ): BringInitDecorator => {
19
+ return (init) => ({ ...init, priority: priorityMode });
20
+ };
@@ -0,0 +1,23 @@
1
+ import { BringInitDecorator } from "../types/BringDecorator";
2
+ import { BringInit } from "../types/BringInit";
3
+
4
+ /** redirect(string): BringInitDecorator
5
+ *
6
+ * Sets the redirect mode of the request.
7
+ *
8
+ * Possible values are:
9
+ * - "follow": Automatically follow redirects. This is the default value.
10
+ * - "error": Abort with an error if a redirect occurs.
11
+ * - "manual": Handle redirects manually.
12
+ *
13
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/Request/redirect
14
+ *
15
+ * @param redirectMode - The redirect mode to use. "follow" | "error" | "manual". Defaults to "follow".
16
+ * @returns BringIntDecorator
17
+ */
18
+ export const redirect =
19
+ (redirectMode: BringInit["redirect"] = "follow"): BringInitDecorator =>
20
+ (init) => {
21
+ init.redirect = redirectMode;
22
+ return init;
23
+ };
@@ -0,0 +1,21 @@
1
+ import { BringInitDecorator } from "../types/BringDecorator";
2
+ import { BringInit } from "../types/BringInit";
3
+
4
+ /** referrer(string): BringInitDecorator
5
+ *
6
+ * A string specifying the value to use for the request's Referer header. One of the following:
7
+ * - A same-origin relative or absolute URL
8
+ * - An empty string, which indicates that no Referer header should be sent
9
+ * - "about:client" (default) set the Referer header to the default value for the context of the request (for example, the URL of the page that made the request).
10
+ *
11
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/RequestInit#referrer
12
+ *
13
+ * @param referrerMode
14
+ * @returns
15
+ */
16
+ export const referrer =
17
+ (referrerMode: BringInit["referrer"] = "about:client"): BringInitDecorator =>
18
+ (init) => {
19
+ init.referrer = referrerMode;
20
+ return init;
21
+ };