@ribbon-studios/js-utils 1.3.0 → 1.3.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/dist/index.cjs CHANGED
@@ -43,14 +43,12 @@ async function retry(fn, n) {
43
43
  }
44
44
  async function rfetch(url, options) {
45
45
  var _a, _b;
46
- const { params, headers, body, ...internalOptions } = {
47
- method: "GET",
48
- headers: {},
49
- ...options
46
+ const requestInit = {
47
+ method: (options == null ? void 0 : options.method) ?? "GET"
50
48
  };
51
49
  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)) {
50
+ if (options == null ? void 0 : options.params) {
51
+ for (const [key, values] of Object.entries(options.params)) {
54
52
  if (Array.isArray(values)) {
55
53
  for (const value of values) {
56
54
  internalURL.searchParams.append(key, value.toString());
@@ -60,18 +58,28 @@ async function rfetch(url, options) {
60
58
  }
61
59
  }
62
60
  }
63
- let internalBody = void 0;
64
- if (body) {
65
- internalBody = body instanceof FormData ? body : JSON.stringify(body);
61
+ if (requestInit.method !== "GET" && (options == null ? void 0 : options.body)) {
62
+ if (options.body instanceof FormData) {
63
+ requestInit.body = options.body;
64
+ requestInit.headers = {
65
+ "Content-Type": "application/x-www-form-urlencoded",
66
+ ...requestInit.headers
67
+ };
68
+ } else if (typeof options.body === "string") {
69
+ requestInit.body = options.body;
70
+ requestInit.headers = {
71
+ "Content-Type": "application/json",
72
+ ...requestInit.headers
73
+ };
74
+ } else {
75
+ requestInit.body = JSON.stringify(options.body);
76
+ requestInit.headers = {
77
+ "Content-Type": "application/json",
78
+ ...requestInit.headers
79
+ };
80
+ }
66
81
  }
67
- const response = await fetch(internalURL, {
68
- ...internalOptions,
69
- headers: {
70
- "Content-Type": internalBody instanceof FormData ? "application/x-www-form-urlencoded" : "application/json",
71
- ...headers
72
- },
73
- body: internalBody
74
- });
82
+ const response = await fetch(internalURL, requestInit);
75
83
  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
84
  if (response.ok) {
77
85
  return content;
package/dist/index.js CHANGED
@@ -41,14 +41,12 @@ async function retry(fn, n) {
41
41
  }
42
42
  async function rfetch(url, options) {
43
43
  var _a, _b;
44
- const { params, headers, body, ...internalOptions } = {
45
- method: "GET",
46
- headers: {},
47
- ...options
44
+ const requestInit = {
45
+ method: (options == null ? void 0 : options.method) ?? "GET"
48
46
  };
49
47
  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)) {
48
+ if (options == null ? void 0 : options.params) {
49
+ for (const [key, values] of Object.entries(options.params)) {
52
50
  if (Array.isArray(values)) {
53
51
  for (const value of values) {
54
52
  internalURL.searchParams.append(key, value.toString());
@@ -58,18 +56,28 @@ async function rfetch(url, options) {
58
56
  }
59
57
  }
60
58
  }
61
- let internalBody = void 0;
62
- if (body) {
63
- internalBody = body instanceof FormData ? body : JSON.stringify(body);
59
+ if (requestInit.method !== "GET" && (options == null ? void 0 : options.body)) {
60
+ if (options.body instanceof FormData) {
61
+ requestInit.body = options.body;
62
+ requestInit.headers = {
63
+ "Content-Type": "application/x-www-form-urlencoded",
64
+ ...requestInit.headers
65
+ };
66
+ } else if (typeof options.body === "string") {
67
+ requestInit.body = options.body;
68
+ requestInit.headers = {
69
+ "Content-Type": "application/json",
70
+ ...requestInit.headers
71
+ };
72
+ } else {
73
+ requestInit.body = JSON.stringify(options.body);
74
+ requestInit.headers = {
75
+ "Content-Type": "application/json",
76
+ ...requestInit.headers
77
+ };
78
+ }
64
79
  }
65
- const response = await fetch(internalURL, {
66
- ...internalOptions,
67
- headers: {
68
- "Content-Type": internalBody instanceof FormData ? "application/x-www-form-urlencoded" : "application/json",
69
- ...headers
70
- },
71
- body: internalBody
72
- });
80
+ const response = await fetch(internalURL, requestInit);
73
81
  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
82
  if (response.ok) {
75
83
  return content;
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@ribbon-studios/js-utils",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
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": {