@ribbon-studios/js-utils 1.4.3 → 1.5.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 +28 -0
- package/dist/index.cjs +20 -2
- package/dist/index.js +20 -2
- package/dist/rfetch.d.ts +5 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -28,6 +28,7 @@ Collection of generic javascript utilities curated by the Rainbow Cafe~
|
|
|
28
28
|
- [`rfetch.patch`](#rfetchpatch)
|
|
29
29
|
- [`rfetch.remove`](#rfetchremove)
|
|
30
30
|
- [`rfetch.interceptors`](#rfetchinterceptors)
|
|
31
|
+
- [`rfetch.delimiters`](#rfetchdelimiters)
|
|
31
32
|
|
|
32
33
|
## Promises
|
|
33
34
|
|
|
@@ -221,6 +222,33 @@ rfetch.interceptors.remove(interceptor); // Remove the interceptor
|
|
|
221
222
|
rfetch.interceptors.clear(); // Clear all interceptors
|
|
222
223
|
```
|
|
223
224
|
|
|
225
|
+
### `rfetch.delimiters`
|
|
226
|
+
|
|
227
|
+
Specifies which delimiters should be used.
|
|
228
|
+
|
|
229
|
+
```tsx
|
|
230
|
+
import { rfetch, DelimiterType } from '@ribbon-studios/js-utils';
|
|
231
|
+
|
|
232
|
+
rfetch.delimiters(DelimiterType.DUPLICATE); // Use duplicate query params
|
|
233
|
+
|
|
234
|
+
// This is the default functionality
|
|
235
|
+
// https://ribbonstudios.com?hello=world&hello=welt
|
|
236
|
+
await rfetch.get('https://ribbonstudios.com', {
|
|
237
|
+
params: {
|
|
238
|
+
hello: ['world', 'welt'],
|
|
239
|
+
},
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
rfetch.delimiters(DelimiterType.COMMA); // Use comma separators
|
|
243
|
+
|
|
244
|
+
// https://ribbonstudios.com?hello=world,welt
|
|
245
|
+
await rfetch.get('https://ribbonstudios.com', {
|
|
246
|
+
params: {
|
|
247
|
+
hello: ['world', 'welt'],
|
|
248
|
+
},
|
|
249
|
+
});
|
|
250
|
+
```
|
|
251
|
+
|
|
224
252
|
[_**Want to Contribute?**_](/CONTRIBUTING.md)
|
|
225
253
|
|
|
226
254
|
[npm-version-image]: https://img.shields.io/npm/v/@ribbon-studios/js-utils.svg
|
package/dist/index.cjs
CHANGED
|
@@ -41,7 +41,13 @@ async function retry(fn, n) {
|
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
43
|
}
|
|
44
|
+
var DelimiterType = /* @__PURE__ */ ((DelimiterType2) => {
|
|
45
|
+
DelimiterType2[DelimiterType2["COMMA"] = 0] = "COMMA";
|
|
46
|
+
DelimiterType2[DelimiterType2["DUPLICATE"] = 1] = "DUPLICATE";
|
|
47
|
+
return DelimiterType2;
|
|
48
|
+
})(DelimiterType || {});
|
|
44
49
|
let fetchInterceptors = [];
|
|
50
|
+
let delimiter = 1;
|
|
45
51
|
async function rfetch(url, { params, body, ...options } = {}) {
|
|
46
52
|
var _a, _b;
|
|
47
53
|
const requestInit = {
|
|
@@ -52,8 +58,15 @@ async function rfetch(url, { params, body, ...options } = {}) {
|
|
|
52
58
|
if (params) {
|
|
53
59
|
for (const [key, values] of Object.entries(params)) {
|
|
54
60
|
if (Array.isArray(values)) {
|
|
55
|
-
|
|
56
|
-
|
|
61
|
+
switch (delimiter) {
|
|
62
|
+
case 0:
|
|
63
|
+
internalURL.searchParams.set(key, values.map((value) => value.toString()).join(","));
|
|
64
|
+
break;
|
|
65
|
+
case 1:
|
|
66
|
+
values.forEach((value) => {
|
|
67
|
+
internalURL.searchParams.append(key, value.toString());
|
|
68
|
+
});
|
|
69
|
+
break;
|
|
57
70
|
}
|
|
58
71
|
} else {
|
|
59
72
|
internalURL.searchParams.append(key, values.toString());
|
|
@@ -130,6 +143,10 @@ async function rfetch(url, { params, body, ...options } = {}) {
|
|
|
130
143
|
});
|
|
131
144
|
}
|
|
132
145
|
rfetch2.remove = remove;
|
|
146
|
+
async function delimiters(type) {
|
|
147
|
+
delimiter = type;
|
|
148
|
+
}
|
|
149
|
+
rfetch2.delimiters = delimiters;
|
|
133
150
|
rfetch2.interceptors = {
|
|
134
151
|
add(interceptor) {
|
|
135
152
|
fetchInterceptors.push(interceptor);
|
|
@@ -144,6 +161,7 @@ async function rfetch(url, { params, body, ...options } = {}) {
|
|
|
144
161
|
}
|
|
145
162
|
};
|
|
146
163
|
})(rfetch || (rfetch = {}));
|
|
164
|
+
exports.DelimiterType = DelimiterType;
|
|
147
165
|
exports.assert = assert;
|
|
148
166
|
exports.delay = delay;
|
|
149
167
|
exports.never = never;
|
package/dist/index.js
CHANGED
|
@@ -39,7 +39,13 @@ async function retry(fn, n) {
|
|
|
39
39
|
}
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
|
+
var DelimiterType = /* @__PURE__ */ ((DelimiterType2) => {
|
|
43
|
+
DelimiterType2[DelimiterType2["COMMA"] = 0] = "COMMA";
|
|
44
|
+
DelimiterType2[DelimiterType2["DUPLICATE"] = 1] = "DUPLICATE";
|
|
45
|
+
return DelimiterType2;
|
|
46
|
+
})(DelimiterType || {});
|
|
42
47
|
let fetchInterceptors = [];
|
|
48
|
+
let delimiter = 1;
|
|
43
49
|
async function rfetch(url, { params, body, ...options } = {}) {
|
|
44
50
|
var _a, _b;
|
|
45
51
|
const requestInit = {
|
|
@@ -50,8 +56,15 @@ async function rfetch(url, { params, body, ...options } = {}) {
|
|
|
50
56
|
if (params) {
|
|
51
57
|
for (const [key, values] of Object.entries(params)) {
|
|
52
58
|
if (Array.isArray(values)) {
|
|
53
|
-
|
|
54
|
-
|
|
59
|
+
switch (delimiter) {
|
|
60
|
+
case 0:
|
|
61
|
+
internalURL.searchParams.set(key, values.map((value) => value.toString()).join(","));
|
|
62
|
+
break;
|
|
63
|
+
case 1:
|
|
64
|
+
values.forEach((value) => {
|
|
65
|
+
internalURL.searchParams.append(key, value.toString());
|
|
66
|
+
});
|
|
67
|
+
break;
|
|
55
68
|
}
|
|
56
69
|
} else {
|
|
57
70
|
internalURL.searchParams.append(key, values.toString());
|
|
@@ -128,6 +141,10 @@ async function rfetch(url, { params, body, ...options } = {}) {
|
|
|
128
141
|
});
|
|
129
142
|
}
|
|
130
143
|
rfetch2.remove = remove;
|
|
144
|
+
async function delimiters(type) {
|
|
145
|
+
delimiter = type;
|
|
146
|
+
}
|
|
147
|
+
rfetch2.delimiters = delimiters;
|
|
131
148
|
rfetch2.interceptors = {
|
|
132
149
|
add(interceptor) {
|
|
133
150
|
fetchInterceptors.push(interceptor);
|
|
@@ -143,6 +160,7 @@ async function rfetch(url, { params, body, ...options } = {}) {
|
|
|
143
160
|
};
|
|
144
161
|
})(rfetch || (rfetch = {}));
|
|
145
162
|
export {
|
|
163
|
+
DelimiterType,
|
|
146
164
|
assert,
|
|
147
165
|
delay,
|
|
148
166
|
never,
|
package/dist/rfetch.d.ts
CHANGED
|
@@ -11,6 +11,10 @@ export type RibbonFetchError<R> = {
|
|
|
11
11
|
content: R;
|
|
12
12
|
};
|
|
13
13
|
export type RibbonFetchInterceptor = (url: URL, options: RequestInit) => RequestInit | Promise<RequestInit>;
|
|
14
|
+
export declare enum DelimiterType {
|
|
15
|
+
COMMA = 0,
|
|
16
|
+
DUPLICATE = 1
|
|
17
|
+
}
|
|
14
18
|
/**
|
|
15
19
|
* A lightweight wrapper around fetch to simplify its usage.
|
|
16
20
|
*
|
|
@@ -61,6 +65,7 @@ export declare namespace rfetch {
|
|
|
61
65
|
* @note This is named `remove` purely because `delete` is a reserved key
|
|
62
66
|
*/
|
|
63
67
|
function remove<T>(url: string | URL, options?: RibbonFetchBodyOptions): Promise<T>;
|
|
68
|
+
function delimiters(type: DelimiterType): Promise<void>;
|
|
64
69
|
const interceptors: {
|
|
65
70
|
add(interceptor: RibbonFetchInterceptor): void;
|
|
66
71
|
remove(interceptor: RibbonFetchInterceptor): void;
|