@ribbon-studios/js-utils 1.3.0 → 1.4.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 +17 -0
- package/dist/index.cjs +45 -17
- package/dist/index.js +45 -17
- package/dist/rfetch.d.ts +7 -1
- package/package.json +2 -2
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,16 +41,15 @@ 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
|
-
const
|
|
47
|
-
method: "GET"
|
|
48
|
-
headers: {},
|
|
49
|
-
...options
|
|
47
|
+
const requestInit = {
|
|
48
|
+
method: (options == null ? void 0 : options.method) ?? "GET"
|
|
50
49
|
};
|
|
51
50
|
const internalURL = url instanceof URL ? url : new URL(url, url.startsWith("/") ? location.origin : void 0);
|
|
52
|
-
if (params) {
|
|
53
|
-
for (const [key, values] of Object.entries(params)) {
|
|
51
|
+
if (options == null ? void 0 : options.params) {
|
|
52
|
+
for (const [key, values] of Object.entries(options.params)) {
|
|
54
53
|
if (Array.isArray(values)) {
|
|
55
54
|
for (const value of values) {
|
|
56
55
|
internalURL.searchParams.append(key, value.toString());
|
|
@@ -60,18 +59,34 @@ async function rfetch(url, options) {
|
|
|
60
59
|
}
|
|
61
60
|
}
|
|
62
61
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
62
|
+
if (requestInit.method !== "GET" && (options == null ? void 0 : options.body)) {
|
|
63
|
+
if (options.body instanceof FormData) {
|
|
64
|
+
requestInit.body = options.body;
|
|
65
|
+
requestInit.headers = {
|
|
66
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
67
|
+
...requestInit.headers
|
|
68
|
+
};
|
|
69
|
+
} else if (typeof options.body === "string") {
|
|
70
|
+
requestInit.body = options.body;
|
|
71
|
+
requestInit.headers = {
|
|
72
|
+
"Content-Type": "application/json",
|
|
73
|
+
...requestInit.headers
|
|
74
|
+
};
|
|
75
|
+
} else {
|
|
76
|
+
requestInit.body = JSON.stringify(options.body);
|
|
77
|
+
requestInit.headers = {
|
|
78
|
+
"Content-Type": "application/json",
|
|
79
|
+
...requestInit.headers
|
|
80
|
+
};
|
|
81
|
+
}
|
|
66
82
|
}
|
|
67
|
-
const response = await fetch(
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
});
|
|
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
|
+
);
|
|
75
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();
|
|
76
91
|
if (response.ok) {
|
|
77
92
|
return content;
|
|
@@ -117,6 +132,19 @@ async function rfetch(url, options) {
|
|
|
117
132
|
});
|
|
118
133
|
}
|
|
119
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
|
+
};
|
|
120
148
|
})(rfetch || (rfetch = {}));
|
|
121
149
|
exports.assert = assert;
|
|
122
150
|
exports.delay = delay;
|
package/dist/index.js
CHANGED
|
@@ -39,16 +39,15 @@ 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
|
-
const
|
|
45
|
-
method: "GET"
|
|
46
|
-
headers: {},
|
|
47
|
-
...options
|
|
45
|
+
const requestInit = {
|
|
46
|
+
method: (options == null ? void 0 : options.method) ?? "GET"
|
|
48
47
|
};
|
|
49
48
|
const internalURL = url instanceof URL ? url : new URL(url, url.startsWith("/") ? location.origin : void 0);
|
|
50
|
-
if (params) {
|
|
51
|
-
for (const [key, values] of Object.entries(params)) {
|
|
49
|
+
if (options == null ? void 0 : options.params) {
|
|
50
|
+
for (const [key, values] of Object.entries(options.params)) {
|
|
52
51
|
if (Array.isArray(values)) {
|
|
53
52
|
for (const value of values) {
|
|
54
53
|
internalURL.searchParams.append(key, value.toString());
|
|
@@ -58,18 +57,34 @@ async function rfetch(url, options) {
|
|
|
58
57
|
}
|
|
59
58
|
}
|
|
60
59
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
60
|
+
if (requestInit.method !== "GET" && (options == null ? void 0 : options.body)) {
|
|
61
|
+
if (options.body instanceof FormData) {
|
|
62
|
+
requestInit.body = options.body;
|
|
63
|
+
requestInit.headers = {
|
|
64
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
65
|
+
...requestInit.headers
|
|
66
|
+
};
|
|
67
|
+
} else if (typeof options.body === "string") {
|
|
68
|
+
requestInit.body = options.body;
|
|
69
|
+
requestInit.headers = {
|
|
70
|
+
"Content-Type": "application/json",
|
|
71
|
+
...requestInit.headers
|
|
72
|
+
};
|
|
73
|
+
} else {
|
|
74
|
+
requestInit.body = JSON.stringify(options.body);
|
|
75
|
+
requestInit.headers = {
|
|
76
|
+
"Content-Type": "application/json",
|
|
77
|
+
...requestInit.headers
|
|
78
|
+
};
|
|
79
|
+
}
|
|
64
80
|
}
|
|
65
|
-
const response = await fetch(
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
});
|
|
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
|
+
);
|
|
73
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();
|
|
74
89
|
if (response.ok) {
|
|
75
90
|
return content;
|
|
@@ -115,6 +130,19 @@ async function rfetch(url, options) {
|
|
|
115
130
|
});
|
|
116
131
|
}
|
|
117
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
|
+
};
|
|
118
146
|
})(rfetch || (rfetch = {}));
|
|
119
147
|
export {
|
|
120
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) => RibbonFetchOptions | Promise<RibbonFetchOptions>;
|
|
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?:
|
|
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,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ribbon-studios/js-utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"description": "Collection of generic javascript utilities curated by the Rainbow Cafe~",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"source": "src/*.ts",
|
|
7
7
|
"main": "./dist/index.cjs",
|
|
8
8
|
"module": "./dist/index.module.js",
|
|
9
9
|
"unpkg": "./dist/index.umd.js",
|
|
10
|
-
"types": "dist/index.d.ts",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
11
|
"exports": {
|
|
12
12
|
".": {
|
|
13
13
|
"import": {
|