@ribbon-studios/js-utils 1.4.1 → 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 +14 -16
- package/dist/index.js +14 -16
- package/dist/rfetch.d.ts +2 -4
- package/package.json +1 -1
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:
|
|
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 (
|
|
52
|
-
for (const [key, values] of Object.entries(
|
|
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" &&
|
|
63
|
-
if (
|
|
64
|
-
requestInit.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
|
|
70
|
-
requestInit.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(
|
|
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:
|
|
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 (
|
|
50
|
-
for (const [key, values] of Object.entries(
|
|
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" &&
|
|
61
|
-
if (
|
|
62
|
-
requestInit.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
|
|
68
|
-
requestInit.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(
|
|
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
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
type RibbonFetchParamType = string | number | boolean;
|
|
2
1
|
export type RibbonFetchOptions = {
|
|
3
2
|
method?: 'GET' | 'PUT' | 'POST' | 'PATCH' | 'DELETE';
|
|
4
|
-
params?: Record<string,
|
|
3
|
+
params?: Record<string, any>;
|
|
5
4
|
body?: any;
|
|
6
5
|
headers?: HeadersInit;
|
|
7
6
|
} & Omit<RequestInit, 'body' | 'headers' | 'method'>;
|
|
@@ -19,7 +18,7 @@ export type RibbonFetchInterceptor = (url: URL, options: RequestInit) => Request
|
|
|
19
18
|
* @param options The request options.
|
|
20
19
|
* @returns The typed response or an error containing the `status` and the `content`
|
|
21
20
|
*/
|
|
22
|
-
export declare function rfetch<T = any
|
|
21
|
+
export declare function rfetch<T = any>(url: string | URL, { params, body, ...options }?: RibbonFetchOptions): Promise<T>;
|
|
23
22
|
export declare namespace rfetch {
|
|
24
23
|
/**
|
|
25
24
|
* Shorthand method for a GET request
|
|
@@ -68,4 +67,3 @@ export declare namespace rfetch {
|
|
|
68
67
|
clear(): void;
|
|
69
68
|
};
|
|
70
69
|
}
|
|
71
|
-
export {};
|