@ribbon-studios/js-utils 1.4.2 → 1.4.3

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/dist/index.cjs CHANGED
@@ -42,14 +42,15 @@ async function retry(fn, n) {
42
42
  }
43
43
  }
44
44
  let fetchInterceptors = [];
45
- async function rfetch(url, options) {
45
+ async function rfetch(url, { params, body, ...options } = {}) {
46
46
  var _a, _b;
47
47
  const requestInit = {
48
- method: (options == null ? void 0 : options.method) ?? "GET"
48
+ method: "GET",
49
+ ...options
49
50
  };
50
51
  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)) {
52
+ if (params) {
53
+ for (const [key, values] of Object.entries(params)) {
53
54
  if (Array.isArray(values)) {
54
55
  for (const value of values) {
55
56
  internalURL.searchParams.append(key, value.toString());
@@ -59,24 +60,21 @@ async function rfetch(url, options) {
59
60
  }
60
61
  }
61
62
  }
62
- if (requestInit.method !== "GET" && (options == null ? void 0 : options.body)) {
63
- if (options.body instanceof FormData) {
64
- requestInit.body = options.body;
63
+ if (requestInit.method !== "GET" && body) {
64
+ if (body instanceof FormData) {
65
+ requestInit.body = body;
65
66
  requestInit.headers = {
66
- "Content-Type": "application/x-www-form-urlencoded",
67
- ...requestInit.headers
67
+ "Content-Type": "application/x-www-form-urlencoded"
68
68
  };
69
- } else if (typeof options.body === "string") {
70
- requestInit.body = options.body;
69
+ } else if (typeof body === "string") {
70
+ requestInit.body = body;
71
71
  requestInit.headers = {
72
- "Content-Type": "application/json",
73
- ...requestInit.headers
72
+ "Content-Type": "application/json"
74
73
  };
75
74
  } else {
76
- requestInit.body = JSON.stringify(options.body);
75
+ requestInit.body = JSON.stringify(body);
77
76
  requestInit.headers = {
78
- "Content-Type": "application/json",
79
- ...requestInit.headers
77
+ "Content-Type": "application/json"
80
78
  };
81
79
  }
82
80
  }
package/dist/index.js CHANGED
@@ -40,14 +40,15 @@ async function retry(fn, n) {
40
40
  }
41
41
  }
42
42
  let fetchInterceptors = [];
43
- async function rfetch(url, options) {
43
+ async function rfetch(url, { params, body, ...options } = {}) {
44
44
  var _a, _b;
45
45
  const requestInit = {
46
- method: (options == null ? void 0 : options.method) ?? "GET"
46
+ method: "GET",
47
+ ...options
47
48
  };
48
49
  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)) {
50
+ if (params) {
51
+ for (const [key, values] of Object.entries(params)) {
51
52
  if (Array.isArray(values)) {
52
53
  for (const value of values) {
53
54
  internalURL.searchParams.append(key, value.toString());
@@ -57,24 +58,21 @@ async function rfetch(url, options) {
57
58
  }
58
59
  }
59
60
  }
60
- if (requestInit.method !== "GET" && (options == null ? void 0 : options.body)) {
61
- if (options.body instanceof FormData) {
62
- requestInit.body = options.body;
61
+ if (requestInit.method !== "GET" && body) {
62
+ if (body instanceof FormData) {
63
+ requestInit.body = body;
63
64
  requestInit.headers = {
64
- "Content-Type": "application/x-www-form-urlencoded",
65
- ...requestInit.headers
65
+ "Content-Type": "application/x-www-form-urlencoded"
66
66
  };
67
- } else if (typeof options.body === "string") {
68
- requestInit.body = options.body;
67
+ } else if (typeof body === "string") {
68
+ requestInit.body = body;
69
69
  requestInit.headers = {
70
- "Content-Type": "application/json",
71
- ...requestInit.headers
70
+ "Content-Type": "application/json"
72
71
  };
73
72
  } else {
74
- requestInit.body = JSON.stringify(options.body);
73
+ requestInit.body = JSON.stringify(body);
75
74
  requestInit.headers = {
76
- "Content-Type": "application/json",
77
- ...requestInit.headers
75
+ "Content-Type": "application/json"
78
76
  };
79
77
  }
80
78
  }
package/dist/rfetch.d.ts CHANGED
@@ -18,7 +18,7 @@ export type RibbonFetchInterceptor = (url: URL, options: RequestInit) => Request
18
18
  * @param options The request options.
19
19
  * @returns The typed response or an error containing the `status` and the `content`
20
20
  */
21
- export declare function rfetch<T = any, O extends RibbonFetchOptions = RibbonFetchOptions>(url: string | URL, options?: O): Promise<T>;
21
+ export declare function rfetch<T = any>(url: string | URL, { params, body, ...options }?: RibbonFetchOptions): Promise<T>;
22
22
  export declare namespace rfetch {
23
23
  /**
24
24
  * Shorthand method for a GET request
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.4.3",
4
4
  "description": "Collection of generic javascript utilities curated by the Rainbow Cafe~",
5
5
  "type": "module",
6
6
  "source": "src/*.ts",