@ribbon-studios/js-utils 1.5.2 → 1.6.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
@@ -249,6 +249,24 @@ await rfetch.get('https://ribbonstudios.com', {
249
249
  });
250
250
  ```
251
251
 
252
+ ### `rfetch.is.error`
253
+
254
+ A type guard that helps determine if the error is from a `rfetch` response.
255
+
256
+ ```ts
257
+ import { rfetch } from '@ribbon-studios/js-utils';
258
+
259
+ try {
260
+ await rfetch.get('https://ribbonstudios.com');
261
+ } catch (error: any) {
262
+ if (rfetch.is.error(error) && error.status === 404) {
263
+ // Do something!
264
+ }
265
+
266
+ throw error;
267
+ }
268
+ ```
269
+
252
270
  [_**Want to Contribute?**_](/CONTRIBUTING.md)
253
271
 
254
272
  [npm-version-image]: https://img.shields.io/npm/v/@ribbon-studios/js-utils.svg
package/dist/index.cjs CHANGED
@@ -41,6 +41,13 @@ async function retry(fn, n) {
41
41
  }
42
42
  }
43
43
  }
44
+ class RibbonFetchError extends Error {
45
+ constructor({ status, content }) {
46
+ super();
47
+ this.status = status;
48
+ this.content = content;
49
+ }
50
+ }
44
51
  var DelimiterType = /* @__PURE__ */ ((DelimiterType2) => {
45
52
  DelimiterType2[DelimiterType2["COMMA"] = 0] = "COMMA";
46
53
  DelimiterType2[DelimiterType2["DUPLICATE"] = 1] = "DUPLICATE";
@@ -75,17 +82,20 @@ async function rfetch(url, { params, body, ...options } = {}) {
75
82
  if (body instanceof FormData) {
76
83
  requestInit.body = body;
77
84
  requestInit.headers = {
78
- "Content-Type": "application/x-www-form-urlencoded"
85
+ "Content-Type": "application/x-www-form-urlencoded",
86
+ ...requestInit.headers
79
87
  };
80
88
  } else if (typeof body === "string") {
81
89
  requestInit.body = body;
82
90
  requestInit.headers = {
83
- "Content-Type": "application/json"
91
+ "Content-Type": "application/json",
92
+ ...requestInit.headers
84
93
  };
85
94
  } else {
86
95
  requestInit.body = JSON.stringify(body);
87
96
  requestInit.headers = {
88
- "Content-Type": "application/json"
97
+ "Content-Type": "application/json",
98
+ ...requestInit.headers
89
99
  };
90
100
  }
91
101
  }
@@ -100,10 +110,12 @@ async function rfetch(url, { params, body, ...options } = {}) {
100
110
  if (response.ok) {
101
111
  return content;
102
112
  }
103
- return Promise.reject({
104
- status: response.status,
105
- content
106
- });
113
+ return Promise.reject(
114
+ new RibbonFetchError({
115
+ status: response.status,
116
+ content
117
+ })
118
+ );
107
119
  }
108
120
  ((rfetch2) => {
109
121
  async function get(url, options) {
@@ -158,8 +170,15 @@ async function rfetch(url, { params, body, ...options } = {}) {
158
170
  fetchInterceptors = [];
159
171
  }
160
172
  };
173
+ ((is2) => {
174
+ function error(value) {
175
+ return value instanceof RibbonFetchError;
176
+ }
177
+ is2.error = error;
178
+ })(rfetch2.is || (rfetch2.is = {}));
161
179
  })(rfetch || (rfetch = {}));
162
180
  exports.DelimiterType = DelimiterType;
181
+ exports.RibbonFetchError = RibbonFetchError;
163
182
  exports.assert = assert;
164
183
  exports.delay = delay;
165
184
  exports.never = never;
package/dist/index.js CHANGED
@@ -39,6 +39,13 @@ async function retry(fn, n) {
39
39
  }
40
40
  }
41
41
  }
42
+ class RibbonFetchError extends Error {
43
+ constructor({ status, content }) {
44
+ super();
45
+ this.status = status;
46
+ this.content = content;
47
+ }
48
+ }
42
49
  var DelimiterType = /* @__PURE__ */ ((DelimiterType2) => {
43
50
  DelimiterType2[DelimiterType2["COMMA"] = 0] = "COMMA";
44
51
  DelimiterType2[DelimiterType2["DUPLICATE"] = 1] = "DUPLICATE";
@@ -73,17 +80,20 @@ async function rfetch(url, { params, body, ...options } = {}) {
73
80
  if (body instanceof FormData) {
74
81
  requestInit.body = body;
75
82
  requestInit.headers = {
76
- "Content-Type": "application/x-www-form-urlencoded"
83
+ "Content-Type": "application/x-www-form-urlencoded",
84
+ ...requestInit.headers
77
85
  };
78
86
  } else if (typeof body === "string") {
79
87
  requestInit.body = body;
80
88
  requestInit.headers = {
81
- "Content-Type": "application/json"
89
+ "Content-Type": "application/json",
90
+ ...requestInit.headers
82
91
  };
83
92
  } else {
84
93
  requestInit.body = JSON.stringify(body);
85
94
  requestInit.headers = {
86
- "Content-Type": "application/json"
95
+ "Content-Type": "application/json",
96
+ ...requestInit.headers
87
97
  };
88
98
  }
89
99
  }
@@ -98,10 +108,12 @@ async function rfetch(url, { params, body, ...options } = {}) {
98
108
  if (response.ok) {
99
109
  return content;
100
110
  }
101
- return Promise.reject({
102
- status: response.status,
103
- content
104
- });
111
+ return Promise.reject(
112
+ new RibbonFetchError({
113
+ status: response.status,
114
+ content
115
+ })
116
+ );
105
117
  }
106
118
  ((rfetch2) => {
107
119
  async function get(url, options) {
@@ -156,9 +168,16 @@ async function rfetch(url, { params, body, ...options } = {}) {
156
168
  fetchInterceptors = [];
157
169
  }
158
170
  };
171
+ ((is2) => {
172
+ function error(value) {
173
+ return value instanceof RibbonFetchError;
174
+ }
175
+ is2.error = error;
176
+ })(rfetch2.is || (rfetch2.is = {}));
159
177
  })(rfetch || (rfetch = {}));
160
178
  export {
161
179
  DelimiterType,
180
+ RibbonFetchError,
162
181
  assert,
163
182
  delay,
164
183
  never,
package/dist/rfetch.d.ts CHANGED
@@ -6,10 +6,14 @@ export type RibbonFetchOptions = {
6
6
  } & Omit<RequestInit, 'body' | 'headers' | 'method'>;
7
7
  export type RibbonFetchBasicOptions = Omit<RibbonFetchOptions, 'method' | 'body'>;
8
8
  export type RibbonFetchBodyOptions = Omit<RibbonFetchOptions, 'method'>;
9
- export type RibbonFetchError<R> = {
9
+ export declare class RibbonFetchError<R> extends Error {
10
10
  status: number;
11
11
  content: R;
12
- };
12
+ constructor({ status, content }: {
13
+ status: number;
14
+ content: R;
15
+ });
16
+ }
13
17
  export type RibbonFetchInterceptor = (url: URL, options: RequestInit) => RequestInit | Promise<RequestInit>;
14
18
  export declare enum DelimiterType {
15
19
  COMMA = 0,
@@ -71,4 +75,7 @@ export declare namespace rfetch {
71
75
  remove(interceptor: RibbonFetchInterceptor): void;
72
76
  clear(): void;
73
77
  };
78
+ namespace is {
79
+ function error<T = any>(value: any): value is RibbonFetchError<T>;
80
+ }
74
81
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ribbon-studios/js-utils",
3
- "version": "1.5.2",
3
+ "version": "1.6.1",
4
4
  "description": "Collection of generic javascript utilities curated by the Rainbow Cafe~",
5
5
  "type": "module",
6
6
  "source": "src/*.ts",