@ribbon-studios/js-utils 1.4.3 → 1.5.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
@@ -11,7 +11,7 @@
11
11
 
12
12
  # JS Utils 🔧
13
13
 
14
- Collection of generic javascript utilities curated by the Rainbow Cafe~
14
+ Collection of generic javascript utilities curated by the Ribbon Studios Team~
15
15
 
16
16
  - [Promises](#promises)
17
17
  - [`delay`](#delay)
@@ -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 = {
@@ -50,13 +56,18 @@ async function rfetch(url, { params, body, ...options } = {}) {
50
56
  };
51
57
  const internalURL = url instanceof URL ? url : new URL(url, url.startsWith("/") ? location.origin : void 0);
52
58
  if (params) {
53
- for (const [key, values] of Object.entries(params)) {
54
- if (Array.isArray(values)) {
55
- for (const value of values) {
56
- internalURL.searchParams.append(key, value.toString());
57
- }
58
- } else {
59
- internalURL.searchParams.append(key, values.toString());
59
+ for (const [key, value] of Object.entries(params)) {
60
+ let values = Array.isArray(value) ? value : [value];
61
+ values = values.filter((value2) => ![void 0, null].includes(value2));
62
+ switch (delimiter) {
63
+ case 0:
64
+ internalURL.searchParams.set(key, values.map((value2) => value2.toString()).join(","));
65
+ break;
66
+ case 1:
67
+ values.forEach((value2) => {
68
+ internalURL.searchParams.append(key, value2.toString());
69
+ });
70
+ break;
60
71
  }
61
72
  }
62
73
  }
@@ -130,6 +141,10 @@ async function rfetch(url, { params, body, ...options } = {}) {
130
141
  });
131
142
  }
132
143
  rfetch2.remove = remove;
144
+ async function delimiters(type) {
145
+ delimiter = type;
146
+ }
147
+ rfetch2.delimiters = delimiters;
133
148
  rfetch2.interceptors = {
134
149
  add(interceptor) {
135
150
  fetchInterceptors.push(interceptor);
@@ -144,6 +159,7 @@ async function rfetch(url, { params, body, ...options } = {}) {
144
159
  }
145
160
  };
146
161
  })(rfetch || (rfetch = {}));
162
+ exports.DelimiterType = DelimiterType;
147
163
  exports.assert = assert;
148
164
  exports.delay = delay;
149
165
  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 = {
@@ -48,13 +54,18 @@ async function rfetch(url, { params, body, ...options } = {}) {
48
54
  };
49
55
  const internalURL = url instanceof URL ? url : new URL(url, url.startsWith("/") ? location.origin : void 0);
50
56
  if (params) {
51
- for (const [key, values] of Object.entries(params)) {
52
- if (Array.isArray(values)) {
53
- for (const value of values) {
54
- internalURL.searchParams.append(key, value.toString());
55
- }
56
- } else {
57
- internalURL.searchParams.append(key, values.toString());
57
+ for (const [key, value] of Object.entries(params)) {
58
+ let values = Array.isArray(value) ? value : [value];
59
+ values = values.filter((value2) => ![void 0, null].includes(value2));
60
+ switch (delimiter) {
61
+ case 0:
62
+ internalURL.searchParams.set(key, values.map((value2) => value2.toString()).join(","));
63
+ break;
64
+ case 1:
65
+ values.forEach((value2) => {
66
+ internalURL.searchParams.append(key, value2.toString());
67
+ });
68
+ break;
58
69
  }
59
70
  }
60
71
  }
@@ -128,6 +139,10 @@ async function rfetch(url, { params, body, ...options } = {}) {
128
139
  });
129
140
  }
130
141
  rfetch2.remove = remove;
142
+ async function delimiters(type) {
143
+ delimiter = type;
144
+ }
145
+ rfetch2.delimiters = delimiters;
131
146
  rfetch2.interceptors = {
132
147
  add(interceptor) {
133
148
  fetchInterceptors.push(interceptor);
@@ -143,6 +158,7 @@ async function rfetch(url, { params, body, ...options } = {}) {
143
158
  };
144
159
  })(rfetch || (rfetch = {}));
145
160
  export {
161
+ DelimiterType,
146
162
  assert,
147
163
  delay,
148
164
  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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ribbon-studios/js-utils",
3
- "version": "1.4.3",
3
+ "version": "1.5.1",
4
4
  "description": "Collection of generic javascript utilities curated by the Rainbow Cafe~",
5
5
  "type": "module",
6
6
  "source": "src/*.ts",