@ribbon-studios/js-utils 1.5.1 → 1.6.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
@@ -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";
@@ -100,10 +107,12 @@ async function rfetch(url, { params, body, ...options } = {}) {
100
107
  if (response.ok) {
101
108
  return content;
102
109
  }
103
- return Promise.reject({
104
- status: response.status,
105
- content
106
- });
110
+ return Promise.reject(
111
+ new RibbonFetchError({
112
+ status: response.status,
113
+ content
114
+ })
115
+ );
107
116
  }
108
117
  ((rfetch2) => {
109
118
  async function get(url, options) {
@@ -158,8 +167,15 @@ async function rfetch(url, { params, body, ...options } = {}) {
158
167
  fetchInterceptors = [];
159
168
  }
160
169
  };
170
+ ((is2) => {
171
+ function error(value) {
172
+ return value instanceof RibbonFetchError;
173
+ }
174
+ is2.error = error;
175
+ })(rfetch2.is || (rfetch2.is = {}));
161
176
  })(rfetch || (rfetch = {}));
162
177
  exports.DelimiterType = DelimiterType;
178
+ exports.RibbonFetchError = RibbonFetchError;
163
179
  exports.assert = assert;
164
180
  exports.delay = delay;
165
181
  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";
@@ -98,10 +105,12 @@ async function rfetch(url, { params, body, ...options } = {}) {
98
105
  if (response.ok) {
99
106
  return content;
100
107
  }
101
- return Promise.reject({
102
- status: response.status,
103
- content
104
- });
108
+ return Promise.reject(
109
+ new RibbonFetchError({
110
+ status: response.status,
111
+ content
112
+ })
113
+ );
105
114
  }
106
115
  ((rfetch2) => {
107
116
  async function get(url, options) {
@@ -156,9 +165,16 @@ async function rfetch(url, { params, body, ...options } = {}) {
156
165
  fetchInterceptors = [];
157
166
  }
158
167
  };
168
+ ((is2) => {
169
+ function error(value) {
170
+ return value instanceof RibbonFetchError;
171
+ }
172
+ is2.error = error;
173
+ })(rfetch2.is || (rfetch2.is = {}));
159
174
  })(rfetch || (rfetch = {}));
160
175
  export {
161
176
  DelimiterType,
177
+ RibbonFetchError,
162
178
  assert,
163
179
  delay,
164
180
  never,
@@ -2,4 +2,4 @@
2
2
  * Purely for testing spinners / skeletons
3
3
  * @returns A promise that never resolves
4
4
  */
5
- export declare function never(p?: Promise<any>): Promise<void>;
5
+ export declare function never<T>(p?: Promise<T>): Promise<T>;
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.1",
3
+ "version": "1.6.0",
4
4
  "description": "Collection of generic javascript utilities curated by the Rainbow Cafe~",
5
5
  "type": "module",
6
6
  "source": "src/*.ts",