@ribbon-studios/js-utils 2.1.1 → 3.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 CHANGED
@@ -27,7 +27,8 @@ Collection of generic javascript utilities curated by the Ribbon Studios Team~
27
27
  - [`rfetch.post`](#rfetchpost)
28
28
  - [`rfetch.patch`](#rfetchpatch)
29
29
  - [`rfetch.delete`](#rfetchdelete)
30
- - [`rfetch.interceptors`](#rfetchinterceptors)
30
+ - [`rfetch.interceptors.request`](#rfetchinterceptorsrequest)
31
+ - [`rfetch.interceptors.reject`](#rfetchinterceptorsreject)
31
32
  - [`rfetch.delimiters`](#rfetchdelimiters)
32
33
 
33
34
  ## Promises
@@ -206,7 +207,7 @@ import { rfetch, type RibbonFetchError } from '@ribbon-studios/js-utils';
206
207
  await rfetch.delete<MyExpectedResponse>('https://ribbonstudios.com');
207
208
  ```
208
209
 
209
- ### `rfetch.interceptors`
210
+ ### `rfetch.interceptors.request`
210
211
 
211
212
  Useful for enhancing requests with additional information
212
213
 
@@ -217,9 +218,25 @@ const interceptor = (url: URL, options: RequestInit): RequestInit | Promise<Requ
217
218
  return options; // Return the modified options!
218
219
  };
219
220
 
220
- rfetch.interceptors.add(interceptor); // Add the interceptor
221
- rfetch.interceptors.remove(interceptor); // Remove the interceptor
222
- rfetch.interceptors.clear(); // Clear all interceptors
221
+ rfetch.interceptors.request.add(interceptor); // Add the interceptor
222
+ rfetch.interceptors.request.remove(interceptor); // Remove the interceptor
223
+ rfetch.interceptors.request.clear(); // Clear all interceptors
224
+ ```
225
+
226
+ ### `rfetch.interceptors.reject`
227
+
228
+ Useful for handling special cases when a failed request occurs
229
+
230
+ ```tsx
231
+ import { rfetch, type RibbonFetchError } from '@ribbon-studios/js-utils';
232
+
233
+ const interceptor = (url: URL, error: RibbonFetchError): RibbonFetchError | Promise<RibbonFetchError> => {
234
+ return error; // Return the modified error!
235
+ };
236
+
237
+ rfetch.interceptors.reject.add(interceptor); // Add the interceptor
238
+ rfetch.interceptors.reject.remove(interceptor); // Remove the interceptor
239
+ rfetch.interceptors.reject.clear(); // Clear all interceptors
223
240
  ```
224
241
 
225
242
  ### `rfetch.delimiters`
package/dist/index.cjs CHANGED
@@ -43,7 +43,7 @@ async function retry(fn, n) {
43
43
  }
44
44
  class RibbonFetchError extends Error {
45
45
  constructor({ status, content }) {
46
- super();
46
+ super(content);
47
47
  this.status = status;
48
48
  this.content = content;
49
49
  }
@@ -53,7 +53,10 @@ var DelimiterType = /* @__PURE__ */ ((DelimiterType2) => {
53
53
  DelimiterType2[DelimiterType2["DUPLICATE"] = 1] = "DUPLICATE";
54
54
  return DelimiterType2;
55
55
  })(DelimiterType || {});
56
- let fetchInterceptors = [];
56
+ const fetchInterceptors = {
57
+ request: [],
58
+ reject: []
59
+ };
57
60
  let delimiter = 1;
58
61
  async function rfetch(url, { params, body, ...options } = {}) {
59
62
  var _a, _b;
@@ -97,7 +100,7 @@ async function rfetch(url, { params, body, ...options } = {}) {
97
100
  }
98
101
  const response = await fetch(
99
102
  internalURL,
100
- await fetchInterceptors.reduce(
103
+ await fetchInterceptors.request.reduce(
101
104
  async (output, interceptor) => await interceptor(internalURL, await output),
102
105
  Promise.resolve(requestInit)
103
106
  )
@@ -107,11 +110,15 @@ async function rfetch(url, { params, body, ...options } = {}) {
107
110
  if (response.status === 204) return void 0;
108
111
  return content;
109
112
  }
113
+ const error = new RibbonFetchError({
114
+ status: response.status,
115
+ content
116
+ });
110
117
  return Promise.reject(
111
- new RibbonFetchError({
112
- status: response.status,
113
- content
114
- })
118
+ await fetchInterceptors.reject.reduce(
119
+ async (output, interceptor) => await interceptor(internalURL, await output),
120
+ Promise.resolve(error)
121
+ )
115
122
  );
116
123
  }
117
124
  rfetch.delete = (url, options) => {
@@ -158,16 +165,35 @@ rfetch.delete = (url, options) => {
158
165
  }
159
166
  rfetch2.delimiters = delimiters;
160
167
  rfetch2.interceptors = {
161
- add(interceptor) {
162
- fetchInterceptors.push(interceptor);
168
+ request: {
169
+ add(interceptor) {
170
+ fetchInterceptors.request.push(interceptor);
171
+ },
172
+ remove(interceptor) {
173
+ const index = fetchInterceptors.request.indexOf(interceptor);
174
+ if (index === -1) return;
175
+ fetchInterceptors.request.splice(index, 1);
176
+ },
177
+ clear() {
178
+ fetchInterceptors.request = [];
179
+ }
163
180
  },
164
- remove(interceptor) {
165
- const index = fetchInterceptors.indexOf(interceptor);
166
- if (index === -1) return;
167
- fetchInterceptors.splice(index, 1);
181
+ reject: {
182
+ add(interceptor) {
183
+ fetchInterceptors.reject.push(interceptor);
184
+ },
185
+ remove(interceptor) {
186
+ const index = fetchInterceptors.reject.indexOf(interceptor);
187
+ if (index === -1) return;
188
+ fetchInterceptors.reject.splice(index, 1);
189
+ },
190
+ clear() {
191
+ fetchInterceptors.reject = [];
192
+ }
168
193
  },
169
194
  clear() {
170
- fetchInterceptors = [];
195
+ fetchInterceptors.reject = [];
196
+ fetchInterceptors.request = [];
171
197
  }
172
198
  };
173
199
  ((is2) => {
package/dist/index.js CHANGED
@@ -41,7 +41,7 @@ async function retry(fn, n) {
41
41
  }
42
42
  class RibbonFetchError extends Error {
43
43
  constructor({ status, content }) {
44
- super();
44
+ super(content);
45
45
  this.status = status;
46
46
  this.content = content;
47
47
  }
@@ -51,7 +51,10 @@ var DelimiterType = /* @__PURE__ */ ((DelimiterType2) => {
51
51
  DelimiterType2[DelimiterType2["DUPLICATE"] = 1] = "DUPLICATE";
52
52
  return DelimiterType2;
53
53
  })(DelimiterType || {});
54
- let fetchInterceptors = [];
54
+ const fetchInterceptors = {
55
+ request: [],
56
+ reject: []
57
+ };
55
58
  let delimiter = 1;
56
59
  async function rfetch(url, { params, body, ...options } = {}) {
57
60
  var _a, _b;
@@ -95,7 +98,7 @@ async function rfetch(url, { params, body, ...options } = {}) {
95
98
  }
96
99
  const response = await fetch(
97
100
  internalURL,
98
- await fetchInterceptors.reduce(
101
+ await fetchInterceptors.request.reduce(
99
102
  async (output, interceptor) => await interceptor(internalURL, await output),
100
103
  Promise.resolve(requestInit)
101
104
  )
@@ -105,11 +108,15 @@ async function rfetch(url, { params, body, ...options } = {}) {
105
108
  if (response.status === 204) return void 0;
106
109
  return content;
107
110
  }
111
+ const error = new RibbonFetchError({
112
+ status: response.status,
113
+ content
114
+ });
108
115
  return Promise.reject(
109
- new RibbonFetchError({
110
- status: response.status,
111
- content
112
- })
116
+ await fetchInterceptors.reject.reduce(
117
+ async (output, interceptor) => await interceptor(internalURL, await output),
118
+ Promise.resolve(error)
119
+ )
113
120
  );
114
121
  }
115
122
  rfetch.delete = (url, options) => {
@@ -156,16 +163,35 @@ rfetch.delete = (url, options) => {
156
163
  }
157
164
  rfetch2.delimiters = delimiters;
158
165
  rfetch2.interceptors = {
159
- add(interceptor) {
160
- fetchInterceptors.push(interceptor);
166
+ request: {
167
+ add(interceptor) {
168
+ fetchInterceptors.request.push(interceptor);
169
+ },
170
+ remove(interceptor) {
171
+ const index = fetchInterceptors.request.indexOf(interceptor);
172
+ if (index === -1) return;
173
+ fetchInterceptors.request.splice(index, 1);
174
+ },
175
+ clear() {
176
+ fetchInterceptors.request = [];
177
+ }
161
178
  },
162
- remove(interceptor) {
163
- const index = fetchInterceptors.indexOf(interceptor);
164
- if (index === -1) return;
165
- fetchInterceptors.splice(index, 1);
179
+ reject: {
180
+ add(interceptor) {
181
+ fetchInterceptors.reject.push(interceptor);
182
+ },
183
+ remove(interceptor) {
184
+ const index = fetchInterceptors.reject.indexOf(interceptor);
185
+ if (index === -1) return;
186
+ fetchInterceptors.reject.splice(index, 1);
187
+ },
188
+ clear() {
189
+ fetchInterceptors.reject = [];
190
+ }
166
191
  },
167
192
  clear() {
168
- fetchInterceptors = [];
193
+ fetchInterceptors.reject = [];
194
+ fetchInterceptors.request = [];
169
195
  }
170
196
  };
171
197
  ((is2) => {
package/dist/rfetch.d.ts CHANGED
@@ -14,7 +14,9 @@ export declare class RibbonFetchError<R> extends Error {
14
14
  content: R;
15
15
  });
16
16
  }
17
- export type RibbonFetchInterceptor = (url: URL, options: RequestInit) => RequestInit | Promise<RequestInit>;
17
+ export type RibbonRequestInterceptor = (url: URL, options: RequestInit) => RequestInit | Promise<RequestInit>;
18
+ export type RibbonResolveInterceptor = (url: URL, options: RequestInit) => RequestInit | Promise<RequestInit>;
19
+ export type RibbonRejectInterceptor = (url: URL, error: RibbonFetchError<any>) => RibbonFetchError<any> | Promise<RibbonFetchError<any>>;
18
20
  export declare enum DelimiterType {
19
21
  COMMA = 0,
20
22
  DUPLICATE = 1
@@ -75,8 +77,16 @@ export declare namespace rfetch {
75
77
  function remove<T>(url: string | URL, options?: RibbonFetchBodyOptions): Promise<T>;
76
78
  function delimiters(type: DelimiterType): Promise<void>;
77
79
  const interceptors: {
78
- add(interceptor: RibbonFetchInterceptor): void;
79
- remove(interceptor: RibbonFetchInterceptor): void;
80
+ request: {
81
+ add(interceptor: RibbonRequestInterceptor): void;
82
+ remove(interceptor: RibbonRequestInterceptor): void;
83
+ clear(): void;
84
+ };
85
+ reject: {
86
+ add(interceptor: RibbonRejectInterceptor): void;
87
+ remove(interceptor: RibbonRejectInterceptor): void;
88
+ clear(): void;
89
+ };
80
90
  clear(): void;
81
91
  };
82
92
  namespace is {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ribbon-studios/js-utils",
3
- "version": "2.1.1",
3
+ "version": "3.0.1",
4
4
  "description": "Collection of generic javascript utilities curated by the Rainbow Cafe~",
5
5
  "type": "module",
6
6
  "source": "src/*.ts",