@hyper-fetch/core 9.4.1 → 9.4.2
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/__tests__/features/adapter/adapter.browser.spec.ts +33 -0
- package/__tests__/features/client/client.utils.spec.ts +55 -0
- package/dist/client/client.constants.d.ts +1 -1
- package/dist/http-adapter/http-adapter.fetch.d.ts.map +1 -1
- package/dist/http-adapter/http-adapter.utils.d.ts.map +1 -1
- package/dist/index.mjs +31 -32
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/client/client.constants.ts +1 -1
- package/src/http-adapter/http-adapter.fetch.ts +5 -3
- package/src/http-adapter/http-adapter.utils.ts +32 -28
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hyper-fetch/core",
|
|
3
|
-
"version": "9.4.
|
|
3
|
+
"version": "9.4.2",
|
|
4
4
|
"description": "Cache, Queue and Persist your requests no matter if you are online or offline!",
|
|
5
5
|
"author": "Maciej Pyrc <maciekpyrc@gmail.com>, Kacper Skawina <kacper.skawina@gmail.com>",
|
|
6
6
|
"homepage": "https://hyperfetch.bettertyped.com/",
|
|
@@ -38,6 +38,9 @@ export const getAdapter = () =>
|
|
|
38
38
|
.setFetcher(
|
|
39
39
|
async ({
|
|
40
40
|
request,
|
|
41
|
+
url,
|
|
42
|
+
endpoint,
|
|
43
|
+
queryParams,
|
|
41
44
|
adapterOptions,
|
|
42
45
|
headers,
|
|
43
46
|
payload,
|
|
@@ -54,9 +57,8 @@ export const getAdapter = () =>
|
|
|
54
57
|
onRequestProgress,
|
|
55
58
|
onSuccess,
|
|
56
59
|
}) => {
|
|
57
|
-
const { method
|
|
58
|
-
const
|
|
59
|
-
const fullUrl = `${client.url}${endpoint}${queryString}`;
|
|
60
|
+
const { method } = request;
|
|
61
|
+
const fullUrl = `${url}${endpoint}${queryParams}`;
|
|
60
62
|
|
|
61
63
|
const controller = getAbortController();
|
|
62
64
|
const { timeout: timeoutMs = defaultTimeout, streaming = false, ...restOptions } = adapterOptions || {};
|
|
@@ -85,6 +85,18 @@ const encodeValue = (
|
|
|
85
85
|
return value;
|
|
86
86
|
};
|
|
87
87
|
|
|
88
|
+
const parseValue = (value: QueryParamValuesType, options: QueryStringifyOptionsType): string => {
|
|
89
|
+
if (value instanceof Date) {
|
|
90
|
+
return options.dateParser?.(value) || value.toISOString();
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (value !== null && typeof value === "object" && !Array.isArray(value)) {
|
|
94
|
+
return options.objectParser?.(value) || JSON.stringify(value);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return String(value);
|
|
98
|
+
};
|
|
99
|
+
|
|
88
100
|
const encodeParams = (key: string, value: QueryParamType, options: QueryStringifyOptionsType) => {
|
|
89
101
|
const shouldSkip = !isValidValue(options)(value);
|
|
90
102
|
|
|
@@ -92,53 +104,42 @@ const encodeParams = (key: string, value: QueryParamType, options: QueryStringif
|
|
|
92
104
|
return "";
|
|
93
105
|
}
|
|
94
106
|
|
|
95
|
-
|
|
96
|
-
if (value instanceof Date) {
|
|
97
|
-
return options.dateParser?.(value) || value.toISOString();
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
if (typeof value === "object" && !Array.isArray(value)) {
|
|
101
|
-
return options.objectParser?.(value) || JSON.stringify(value);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
return String(value);
|
|
105
|
-
};
|
|
106
|
-
|
|
107
|
-
return `${encodeValue(key, options)}=${encodeValue(parsedValue(), options)}`;
|
|
107
|
+
return `${encodeValue(key, options)}=${encodeValue(parseValue(value as QueryParamValuesType, options), options)}`;
|
|
108
108
|
};
|
|
109
109
|
|
|
110
110
|
const encodeArray = (key: string, array: QueryParamValuesType[], options: QueryStringifyOptionsType): string => {
|
|
111
111
|
const { arrayFormat, arraySeparator } = options;
|
|
112
|
+
const separator = arraySeparator || "|";
|
|
113
|
+
const encodedKey = encodeValue(key, options);
|
|
112
114
|
|
|
113
115
|
return array
|
|
114
116
|
.filter(isValidValue(options))
|
|
115
117
|
.reduce<string[]>((acc, value, index) => {
|
|
118
|
+
const encodedValue = encodeValue(parseValue(value, options), options);
|
|
119
|
+
|
|
116
120
|
switch (arrayFormat) {
|
|
117
121
|
case "index": {
|
|
118
|
-
|
|
119
|
-
acc.push(`${keyValue}${encodeValue(String(value), options)}`);
|
|
122
|
+
acc.push(`${encodedKey}[${encodeValue(String(index), options)}]=${encodedValue}`);
|
|
120
123
|
break;
|
|
121
124
|
}
|
|
122
125
|
case "bracket": {
|
|
123
|
-
|
|
124
|
-
acc.push(`${keyValue}${encodeValue(String(value), options)}`);
|
|
126
|
+
acc.push(`${encodedKey}[]=${encodedValue}`);
|
|
125
127
|
break;
|
|
126
128
|
}
|
|
127
129
|
case "comma": {
|
|
128
|
-
const keyValue = (acc.length === 0 && `${
|
|
129
|
-
return [[...acc, `${keyValue}${
|
|
130
|
+
const keyValue = (acc.length === 0 && `${encodedKey}=`) || "";
|
|
131
|
+
return [[...acc, `${keyValue}${encodedValue}`].join(",")];
|
|
130
132
|
}
|
|
131
133
|
case "separator": {
|
|
132
|
-
const keyValue = (acc.length === 0 && `${
|
|
133
|
-
return [[...acc, `${keyValue}${
|
|
134
|
+
const keyValue = (acc.length === 0 && `${encodedKey}=`) || "";
|
|
135
|
+
return [[...acc, `${keyValue}${encodedValue}`].join(separator)];
|
|
134
136
|
}
|
|
135
137
|
case "bracket-separator": {
|
|
136
|
-
const keyValue = (acc.length === 0 && `${
|
|
137
|
-
return [[...acc, `${keyValue}${
|
|
138
|
+
const keyValue = (acc.length === 0 && `${encodedKey}[]=`) || "";
|
|
139
|
+
return [[...acc, `${keyValue}${encodedValue}`].join(separator)];
|
|
138
140
|
}
|
|
139
141
|
default: {
|
|
140
|
-
|
|
141
|
-
acc.push(`${keyValue}${encodeValue(String(value), options)}`);
|
|
142
|
+
acc.push(`${encodedKey}=${encodedValue}`);
|
|
142
143
|
}
|
|
143
144
|
}
|
|
144
145
|
|
|
@@ -149,8 +150,11 @@ const encodeArray = (key: string, array: QueryParamValuesType[], options: QueryS
|
|
|
149
150
|
|
|
150
151
|
export const stringifyQueryParams = (
|
|
151
152
|
queryParams: QueryParamsType | string | EmptyTypes,
|
|
152
|
-
options
|
|
153
|
+
options?: QueryStringifyOptionsType,
|
|
153
154
|
): string => {
|
|
155
|
+
// Merge with defaults so a partial config (e.g. `{ arrayFormat: "comma" }`) keeps encoding and skipping enabled
|
|
156
|
+
const config: QueryStringifyOptionsType = { ...stringifyDefaultOptions, ...options };
|
|
157
|
+
|
|
154
158
|
if (!queryParams || Object.keys(queryParams).length === 0) {
|
|
155
159
|
return "";
|
|
156
160
|
}
|
|
@@ -163,10 +167,10 @@ export const stringifyQueryParams = (
|
|
|
163
167
|
const stringified = Object.entries(queryParams)
|
|
164
168
|
.map(([key, value]): string => {
|
|
165
169
|
if (Array.isArray(value)) {
|
|
166
|
-
return encodeArray(key, value,
|
|
170
|
+
return encodeArray(key, value, config);
|
|
167
171
|
}
|
|
168
172
|
|
|
169
|
-
return encodeParams(key, value,
|
|
173
|
+
return encodeParams(key, value, config);
|
|
170
174
|
})
|
|
171
175
|
.filter(Boolean)
|
|
172
176
|
.join("&");
|