@ribbon-studios/js-utils 2.1.0 → 3.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.
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
@@ -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,20 +100,25 @@ 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
  )
104
107
  );
105
108
  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();
106
109
  if (response.ok) {
110
+ if (response.status === 204) return void 0;
107
111
  return content;
108
112
  }
113
+ const error = new RibbonFetchError({
114
+ status: response.status,
115
+ content
116
+ });
109
117
  return Promise.reject(
110
- new RibbonFetchError({
111
- status: response.status,
112
- content
113
- })
118
+ await fetchInterceptors.reject.reduce(
119
+ async (output, interceptor) => await interceptor(internalURL, await output),
120
+ Promise.resolve(error)
121
+ )
114
122
  );
115
123
  }
116
124
  rfetch.delete = (url, options) => {
@@ -157,16 +165,35 @@ rfetch.delete = (url, options) => {
157
165
  }
158
166
  rfetch2.delimiters = delimiters;
159
167
  rfetch2.interceptors = {
160
- add(interceptor) {
161
- 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
+ }
162
180
  },
163
- remove(interceptor) {
164
- const index = fetchInterceptors.indexOf(interceptor);
165
- if (index === -1) return;
166
- 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
+ }
167
193
  },
168
194
  clear() {
169
- fetchInterceptors = [];
195
+ fetchInterceptors.reject = [];
196
+ fetchInterceptors.request = [];
170
197
  }
171
198
  };
172
199
  ((is2) => {
package/dist/index.js CHANGED
@@ -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,20 +98,25 @@ 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
  )
102
105
  );
103
106
  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();
104
107
  if (response.ok) {
108
+ if (response.status === 204) return void 0;
105
109
  return content;
106
110
  }
111
+ const error = new RibbonFetchError({
112
+ status: response.status,
113
+ content
114
+ });
107
115
  return Promise.reject(
108
- new RibbonFetchError({
109
- status: response.status,
110
- content
111
- })
116
+ await fetchInterceptors.reject.reduce(
117
+ async (output, interceptor) => await interceptor(internalURL, await output),
118
+ Promise.resolve(error)
119
+ )
112
120
  );
113
121
  }
114
122
  rfetch.delete = (url, options) => {
@@ -155,16 +163,35 @@ rfetch.delete = (url, options) => {
155
163
  }
156
164
  rfetch2.delimiters = delimiters;
157
165
  rfetch2.interceptors = {
158
- add(interceptor) {
159
- 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
+ }
160
178
  },
161
- remove(interceptor) {
162
- const index = fetchInterceptors.indexOf(interceptor);
163
- if (index === -1) return;
164
- 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
+ }
165
191
  },
166
192
  clear() {
167
- fetchInterceptors = [];
193
+ fetchInterceptors.reject = [];
194
+ fetchInterceptors.request = [];
168
195
  }
169
196
  };
170
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.0",
3
+ "version": "3.0.0",
4
4
  "description": "Collection of generic javascript utilities curated by the Rainbow Cafe~",
5
5
  "type": "module",
6
6
  "source": "src/*.ts",