@ribbon-studios/js-utils 1.4.2 → 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 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,42 +41,53 @@ 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 = [];
45
- async function rfetch(url, options) {
50
+ let delimiter = 1;
51
+ async function rfetch(url, { params, body, ...options } = {}) {
46
52
  var _a, _b;
47
53
  const requestInit = {
48
- method: (options == null ? void 0 : options.method) ?? "GET"
54
+ method: "GET",
55
+ ...options
49
56
  };
50
57
  const internalURL = url instanceof URL ? url : new URL(url, url.startsWith("/") ? location.origin : void 0);
51
- if (options == null ? void 0 : options.params) {
52
- for (const [key, values] of Object.entries(options.params)) {
58
+ if (params) {
59
+ for (const [key, values] of Object.entries(params)) {
53
60
  if (Array.isArray(values)) {
54
- for (const value of values) {
55
- internalURL.searchParams.append(key, value.toString());
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;
56
70
  }
57
71
  } else {
58
72
  internalURL.searchParams.append(key, values.toString());
59
73
  }
60
74
  }
61
75
  }
62
- if (requestInit.method !== "GET" && (options == null ? void 0 : options.body)) {
63
- if (options.body instanceof FormData) {
64
- requestInit.body = options.body;
76
+ if (requestInit.method !== "GET" && body) {
77
+ if (body instanceof FormData) {
78
+ requestInit.body = body;
65
79
  requestInit.headers = {
66
- "Content-Type": "application/x-www-form-urlencoded",
67
- ...requestInit.headers
80
+ "Content-Type": "application/x-www-form-urlencoded"
68
81
  };
69
- } else if (typeof options.body === "string") {
70
- requestInit.body = options.body;
82
+ } else if (typeof body === "string") {
83
+ requestInit.body = body;
71
84
  requestInit.headers = {
72
- "Content-Type": "application/json",
73
- ...requestInit.headers
85
+ "Content-Type": "application/json"
74
86
  };
75
87
  } else {
76
- requestInit.body = JSON.stringify(options.body);
88
+ requestInit.body = JSON.stringify(body);
77
89
  requestInit.headers = {
78
- "Content-Type": "application/json",
79
- ...requestInit.headers
90
+ "Content-Type": "application/json"
80
91
  };
81
92
  }
82
93
  }
@@ -132,6 +143,10 @@ async function rfetch(url, options) {
132
143
  });
133
144
  }
134
145
  rfetch2.remove = remove;
146
+ async function delimiters(type) {
147
+ delimiter = type;
148
+ }
149
+ rfetch2.delimiters = delimiters;
135
150
  rfetch2.interceptors = {
136
151
  add(interceptor) {
137
152
  fetchInterceptors.push(interceptor);
@@ -146,6 +161,7 @@ async function rfetch(url, options) {
146
161
  }
147
162
  };
148
163
  })(rfetch || (rfetch = {}));
164
+ exports.DelimiterType = DelimiterType;
149
165
  exports.assert = assert;
150
166
  exports.delay = delay;
151
167
  exports.never = never;
package/dist/index.js CHANGED
@@ -39,42 +39,53 @@ 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 = [];
43
- async function rfetch(url, options) {
48
+ let delimiter = 1;
49
+ async function rfetch(url, { params, body, ...options } = {}) {
44
50
  var _a, _b;
45
51
  const requestInit = {
46
- method: (options == null ? void 0 : options.method) ?? "GET"
52
+ method: "GET",
53
+ ...options
47
54
  };
48
55
  const internalURL = url instanceof URL ? url : new URL(url, url.startsWith("/") ? location.origin : void 0);
49
- if (options == null ? void 0 : options.params) {
50
- for (const [key, values] of Object.entries(options.params)) {
56
+ if (params) {
57
+ for (const [key, values] of Object.entries(params)) {
51
58
  if (Array.isArray(values)) {
52
- for (const value of values) {
53
- internalURL.searchParams.append(key, value.toString());
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;
54
68
  }
55
69
  } else {
56
70
  internalURL.searchParams.append(key, values.toString());
57
71
  }
58
72
  }
59
73
  }
60
- if (requestInit.method !== "GET" && (options == null ? void 0 : options.body)) {
61
- if (options.body instanceof FormData) {
62
- requestInit.body = options.body;
74
+ if (requestInit.method !== "GET" && body) {
75
+ if (body instanceof FormData) {
76
+ requestInit.body = body;
63
77
  requestInit.headers = {
64
- "Content-Type": "application/x-www-form-urlencoded",
65
- ...requestInit.headers
78
+ "Content-Type": "application/x-www-form-urlencoded"
66
79
  };
67
- } else if (typeof options.body === "string") {
68
- requestInit.body = options.body;
80
+ } else if (typeof body === "string") {
81
+ requestInit.body = body;
69
82
  requestInit.headers = {
70
- "Content-Type": "application/json",
71
- ...requestInit.headers
83
+ "Content-Type": "application/json"
72
84
  };
73
85
  } else {
74
- requestInit.body = JSON.stringify(options.body);
86
+ requestInit.body = JSON.stringify(body);
75
87
  requestInit.headers = {
76
- "Content-Type": "application/json",
77
- ...requestInit.headers
88
+ "Content-Type": "application/json"
78
89
  };
79
90
  }
80
91
  }
@@ -130,6 +141,10 @@ async function rfetch(url, 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);
@@ -145,6 +160,7 @@ async function rfetch(url, options) {
145
160
  };
146
161
  })(rfetch || (rfetch = {}));
147
162
  export {
163
+ DelimiterType,
148
164
  assert,
149
165
  delay,
150
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
  *
@@ -18,7 +22,7 @@ export type RibbonFetchInterceptor = (url: URL, options: RequestInit) => Request
18
22
  * @param options The request options.
19
23
  * @returns The typed response or an error containing the `status` and the `content`
20
24
  */
21
- export declare function rfetch<T = any, O extends RibbonFetchOptions = RibbonFetchOptions>(url: string | URL, options?: O): Promise<T>;
25
+ export declare function rfetch<T = any>(url: string | URL, { params, body, ...options }?: RibbonFetchOptions): Promise<T>;
22
26
  export declare namespace rfetch {
23
27
  /**
24
28
  * Shorthand method for a GET request
@@ -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.2",
3
+ "version": "1.5.0",
4
4
  "description": "Collection of generic javascript utilities curated by the Rainbow Cafe~",
5
5
  "type": "module",
6
6
  "source": "src/*.ts",