@ribbon-studios/js-utils 1.3.1 → 1.4.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 CHANGED
@@ -27,6 +27,7 @@ Collection of generic javascript utilities curated by the Rainbow Cafe~
27
27
  - [`rfetch.post`](#rfetchpost)
28
28
  - [`rfetch.patch`](#rfetchpatch)
29
29
  - [`rfetch.remove`](#rfetchremove)
30
+ - [`rfetch.interceptors`](#rfetchinterceptors)
30
31
 
31
32
  ## Promises
32
33
 
@@ -204,6 +205,22 @@ import { rfetch, type RibbonFetchError } from '@ribbon-studios/js-utils';
204
205
  await rfetch.remove<MyExpectedResponse>('https://ribbonstudios.com');
205
206
  ```
206
207
 
208
+ ### `rfetch.interceptors`
209
+
210
+ Useful for enhancing requests with additional information
211
+
212
+ ```tsx
213
+ import { rfetch, type RibbonFetchError } from '@ribbon-studios/js-utils';
214
+
215
+ const interceptor = (url: URL, options: RequestInit): RequestInit | Promise<RequestInit> => {
216
+ return options; // Return the modified options!
217
+ };
218
+
219
+ rfetch.interceptors.add(interceptor); // Add the interceptor
220
+ rfetch.interceptors.remove(interceptor); // Remove the interceptor
221
+ rfetch.interceptors.clear(); // Clear all interceptors
222
+ ```
223
+
207
224
  [_**Want to Contribute?**_](/CONTRIBUTING.md)
208
225
 
209
226
  [npm-version-image]: https://img.shields.io/npm/v/@ribbon-studios/js-utils.svg
package/dist/index.cjs CHANGED
@@ -41,6 +41,7 @@ async function retry(fn, n) {
41
41
  }
42
42
  }
43
43
  }
44
+ let fetchInterceptors = [];
44
45
  async function rfetch(url, options) {
45
46
  var _a, _b;
46
47
  const requestInit = {
@@ -79,7 +80,13 @@ async function rfetch(url, options) {
79
80
  };
80
81
  }
81
82
  }
82
- const response = await fetch(internalURL, requestInit);
83
+ const response = await fetch(
84
+ internalURL,
85
+ await fetchInterceptors.reduce(
86
+ async (output, interceptor) => await interceptor(internalURL, await output),
87
+ Promise.resolve(requestInit)
88
+ )
89
+ );
83
90
  const content = ((_b = (_a = response.headers.get("Content-Type")) == null ? void 0 : _a.toLowerCase()) == null ? void 0 : _b.includes("json")) ? await response.json() : await response.text();
84
91
  if (response.ok) {
85
92
  return content;
@@ -125,6 +132,19 @@ async function rfetch(url, options) {
125
132
  });
126
133
  }
127
134
  rfetch2.remove = remove;
135
+ rfetch2.interceptors = {
136
+ add(interceptor) {
137
+ fetchInterceptors.push(interceptor);
138
+ },
139
+ remove(interceptor) {
140
+ const index = fetchInterceptors.indexOf(interceptor);
141
+ if (index === -1) return;
142
+ fetchInterceptors.splice(index, 1);
143
+ },
144
+ clear() {
145
+ fetchInterceptors = [];
146
+ }
147
+ };
128
148
  })(rfetch || (rfetch = {}));
129
149
  exports.assert = assert;
130
150
  exports.delay = delay;
package/dist/index.js CHANGED
@@ -39,6 +39,7 @@ async function retry(fn, n) {
39
39
  }
40
40
  }
41
41
  }
42
+ let fetchInterceptors = [];
42
43
  async function rfetch(url, options) {
43
44
  var _a, _b;
44
45
  const requestInit = {
@@ -77,7 +78,13 @@ async function rfetch(url, options) {
77
78
  };
78
79
  }
79
80
  }
80
- const response = await fetch(internalURL, requestInit);
81
+ const response = await fetch(
82
+ internalURL,
83
+ await fetchInterceptors.reduce(
84
+ async (output, interceptor) => await interceptor(internalURL, await output),
85
+ Promise.resolve(requestInit)
86
+ )
87
+ );
81
88
  const content = ((_b = (_a = response.headers.get("Content-Type")) == null ? void 0 : _a.toLowerCase()) == null ? void 0 : _b.includes("json")) ? await response.json() : await response.text();
82
89
  if (response.ok) {
83
90
  return content;
@@ -123,6 +130,19 @@ async function rfetch(url, options) {
123
130
  });
124
131
  }
125
132
  rfetch2.remove = remove;
133
+ rfetch2.interceptors = {
134
+ add(interceptor) {
135
+ fetchInterceptors.push(interceptor);
136
+ },
137
+ remove(interceptor) {
138
+ const index = fetchInterceptors.indexOf(interceptor);
139
+ if (index === -1) return;
140
+ fetchInterceptors.splice(index, 1);
141
+ },
142
+ clear() {
143
+ fetchInterceptors = [];
144
+ }
145
+ };
126
146
  })(rfetch || (rfetch = {}));
127
147
  export {
128
148
  assert,
package/dist/rfetch.d.ts CHANGED
@@ -11,6 +11,7 @@ export type RibbonFetchError<R> = {
11
11
  status: number;
12
12
  content: R;
13
13
  };
14
+ export type RibbonFetchInterceptor = (url: URL, options: RequestInit) => RequestInit | Promise<RequestInit>;
14
15
  /**
15
16
  * A lightweight wrapper around fetch to simplify its usage.
16
17
  *
@@ -18,7 +19,7 @@ export type RibbonFetchError<R> = {
18
19
  * @param options The request options.
19
20
  * @returns The typed response or an error containing the `status` and the `content`
20
21
  */
21
- export declare function rfetch<T = any>(url: string | URL, options?: RibbonFetchOptions): Promise<T>;
22
+ export declare function rfetch<T = any, O extends RibbonFetchOptions = RibbonFetchOptions>(url: string | URL, options?: O): Promise<T>;
22
23
  export declare namespace rfetch {
23
24
  /**
24
25
  * Shorthand method for a GET request
@@ -61,5 +62,10 @@ export declare namespace rfetch {
61
62
  * @note This is named `remove` purely because `delete` is a reserved key
62
63
  */
63
64
  function remove<T>(url: string | URL, options?: RibbonFetchBodyOptions): Promise<T>;
65
+ const interceptors: {
66
+ add(interceptor: RibbonFetchInterceptor): void;
67
+ remove(interceptor: RibbonFetchInterceptor): void;
68
+ clear(): void;
69
+ };
64
70
  }
65
71
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ribbon-studios/js-utils",
3
- "version": "1.3.1",
3
+ "version": "1.4.1",
4
4
  "description": "Collection of generic javascript utilities curated by the Rainbow Cafe~",
5
5
  "type": "module",
6
6
  "source": "src/*.ts",