@hyper-fetch/core 9.4.1 → 9.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/__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
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
* @vitest-environment jsdom
|
|
3
3
|
*/
|
|
4
4
|
import { createHttpMockingServer } from "@hyper-fetch/testing";
|
|
5
|
+
import type { QueryParamsType } from "adapter";
|
|
5
6
|
import { getErrorMessage } from "adapter";
|
|
6
7
|
import { Client } from "client";
|
|
7
8
|
import { HttpAdapter } from "http-adapter";
|
|
@@ -42,6 +43,38 @@ describe("Http Adapter [ Browser ]", () => {
|
|
|
42
43
|
expect(extra).toStrictEqual({ headers: { "content-type": "application/json", "content-length": "15" } });
|
|
43
44
|
});
|
|
44
45
|
|
|
46
|
+
it("should build the request url from the adapter-mapped query params", async () => {
|
|
47
|
+
const fetchSpy = vi.spyOn(globalThis, "fetch");
|
|
48
|
+
client.adapter.setQueryParamsMapperConfig({ arrayFormat: "comma" });
|
|
49
|
+
const queryRequest = client
|
|
50
|
+
.createRequest<{ response: any; queryParams: QueryParamsType }>()({ endpoint: "/shared-endpoint" })
|
|
51
|
+
.setQueryParams({ tags: ["a b", "c&d"], q: "x&y=1", nil: null });
|
|
52
|
+
mockRequest(queryRequest, { data: { response: [] } });
|
|
53
|
+
|
|
54
|
+
const { status } = await client.adapter.fetch(queryRequest, requestId);
|
|
55
|
+
|
|
56
|
+
expect(status).toBe(200);
|
|
57
|
+
expect(fetchSpy).toHaveBeenCalledTimes(1);
|
|
58
|
+
expect(String(fetchSpy.mock.calls[0][0])).toBe(
|
|
59
|
+
"https://shared-base-url/shared-endpoint?tags=a%20b,c%26d&q=x%26y%3D1",
|
|
60
|
+
);
|
|
61
|
+
fetchSpy.mockRestore();
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("should build the request url with a custom query params mapper", async () => {
|
|
65
|
+
const fetchSpy = vi.spyOn(globalThis, "fetch");
|
|
66
|
+
client.adapter.setQueryParamsMapper(() => "?custom=1");
|
|
67
|
+
const queryRequest = client
|
|
68
|
+
.createRequest<{ response: any; queryParams: QueryParamsType }>()({ endpoint: "/shared-endpoint" })
|
|
69
|
+
.setQueryParams({ ignored: true });
|
|
70
|
+
mockRequest(queryRequest, { data: { response: [] } });
|
|
71
|
+
|
|
72
|
+
await client.adapter.fetch(queryRequest, requestId);
|
|
73
|
+
|
|
74
|
+
expect(String(fetchSpy.mock.calls[0][0])).toBe("https://shared-base-url/shared-endpoint?custom=1");
|
|
75
|
+
fetchSpy.mockRestore();
|
|
76
|
+
});
|
|
77
|
+
|
|
45
78
|
it("should make a request and return error data with status", async () => {
|
|
46
79
|
const data = mockRequest(request, { status: 400 });
|
|
47
80
|
|
|
@@ -146,6 +146,61 @@ describe("Client [ Utils ]", () => {
|
|
|
146
146
|
expect(stringifyQueryParams(null, options)).toBe("");
|
|
147
147
|
expect(stringifyQueryParams(undefined, options)).toBe("");
|
|
148
148
|
});
|
|
149
|
+
it("should use default options when none are passed", async () => {
|
|
150
|
+
expect(stringifyQueryParams({ tags: ["a b", "c&d"], q: "x&y=1", nil: null, empty: "" })).toBe(
|
|
151
|
+
"?tags[]=a%20b&tags[]=c%26d&q=x%26y%3D1",
|
|
152
|
+
);
|
|
153
|
+
});
|
|
154
|
+
it("should merge partial options with the defaults", async () => {
|
|
155
|
+
expect(
|
|
156
|
+
stringifyQueryParams({ tags: ["a b", "c&d"], q: "x&y=1", nil: null, empty: "" }, { arrayFormat: "comma" }),
|
|
157
|
+
).toBe("?tags=a%20b,c%26d&q=x%26y%3D1");
|
|
158
|
+
expect(stringifyQueryParams({ value: "(a)" }, { arrayFormat: "comma" })).toBe("?value=%28a%29");
|
|
159
|
+
expect(stringifyQueryParams({ tags: [null, "", "a"] }, { arrayFormat: "index" })).toBe("?tags[0]=a");
|
|
160
|
+
});
|
|
161
|
+
it("should allow partial options to override the defaults", async () => {
|
|
162
|
+
expect(stringifyQueryParams({ q: "a b", nil: null }, { encode: false, skipNull: false })).toBe("?q=a b&nil=null");
|
|
163
|
+
});
|
|
164
|
+
it("should merge partial adapter mapper config with the defaults", async () => {
|
|
165
|
+
const newClient = new Client({ url: "shared-base-url" });
|
|
166
|
+
newClient.adapter.setQueryParamsMapperConfig({ arrayFormat: "comma" });
|
|
167
|
+
expect(newClient.adapter.unstable_queryParamsMapper({ tags: ["a b", "c&d"], q: "x&y=1", nil: null })).toBe(
|
|
168
|
+
"?tags=a%20b,c%26d&q=x%26y%3D1",
|
|
169
|
+
);
|
|
170
|
+
});
|
|
171
|
+
it("should serialize dates and objects inside arrays like scalar values", async () => {
|
|
172
|
+
const date = new Date(Date.UTC(2024, 0, 2));
|
|
173
|
+
expect(stringifyQueryParams({ from: date, f: { id: 1 } })).toBe(
|
|
174
|
+
"?from=2024-01-02T00%3A00%3A00.000Z&f=%7B%22id%22%3A1%7D",
|
|
175
|
+
);
|
|
176
|
+
expect(stringifyQueryParams({ from: [date], f: [{ id: 1 }] })).toBe(
|
|
177
|
+
"?from[]=2024-01-02T00%3A00%3A00.000Z&f[]=%7B%22id%22%3A1%7D",
|
|
178
|
+
);
|
|
179
|
+
expect(stringifyQueryParams({ from: [date, date] }, { arrayFormat: "comma" })).toBe(
|
|
180
|
+
"?from=2024-01-02T00%3A00%3A00.000Z,2024-01-02T00%3A00%3A00.000Z",
|
|
181
|
+
);
|
|
182
|
+
});
|
|
183
|
+
it("should apply custom date and object parsers to array items", async () => {
|
|
184
|
+
const date = new Date(Date.UTC(2024, 0, 2));
|
|
185
|
+
const parsers = {
|
|
186
|
+
dateParser: (value: unknown) => (value as Date).toISOString().slice(0, 10),
|
|
187
|
+
objectParser: (value: unknown) => `id-${(value as { id: number }).id}`,
|
|
188
|
+
};
|
|
189
|
+
expect(stringifyQueryParams({ from: [date], f: [{ id: 1 }, { id: 2 }] }, parsers)).toBe(
|
|
190
|
+
"?from[]=2024-01-02&f[]=id-1&f[]=id-2",
|
|
191
|
+
);
|
|
192
|
+
expect(stringifyQueryParams({ from: date, f: { id: 1 } }, parsers)).toBe("?from=2024-01-02&f=id-1");
|
|
193
|
+
});
|
|
194
|
+
it("should use the pipe separator by default for separator array formats", async () => {
|
|
195
|
+
expect(stringifyDefaultOptions.arraySeparator).toBe("|");
|
|
196
|
+
expect(stringifyQueryParams({ a: [1, 2] }, { ...stringifyDefaultOptions, arrayFormat: "separator" })).toBe(
|
|
197
|
+
"?a=1|2",
|
|
198
|
+
);
|
|
199
|
+
expect(
|
|
200
|
+
stringifyQueryParams({ a: [1, 2] }, { ...stringifyDefaultOptions, arrayFormat: "bracket-separator" }),
|
|
201
|
+
).toBe("?a[]=1|2");
|
|
202
|
+
expect(stringifyQueryParams({ a: [1, 2] }, { arrayFormat: "separator" })).toBe("?a=1|2");
|
|
203
|
+
});
|
|
149
204
|
});
|
|
150
205
|
|
|
151
206
|
describe("When using headerMapper", () => {
|
|
@@ -2,7 +2,7 @@ export declare const stringifyDefaultOptions: {
|
|
|
2
2
|
readonly strict: true;
|
|
3
3
|
readonly encode: true;
|
|
4
4
|
readonly arrayFormat: "bracket";
|
|
5
|
-
readonly arraySeparator: "
|
|
5
|
+
readonly arraySeparator: "|";
|
|
6
6
|
readonly sort: false;
|
|
7
7
|
readonly skipNull: true;
|
|
8
8
|
readonly skipEmptyString: true;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"http-adapter.fetch.d.ts","sourceRoot":"","sources":["../../src/http-adapter/http-adapter.fetch.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAE/C,OAAO,KAAK,EAAE,eAAe,EAAkB,MAAM,OAAO,CAAC;AAE7D,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAE7C,OAAO,KAAK,EAAE,oBAAoB,EAAE,uBAAuB,EAAE,MAAM,sBAAsB,CAAC;AAK1F,eAAO,MAAM,UAAU,
|
|
1
|
+
{"version":3,"file":"http-adapter.fetch.d.ts","sourceRoot":"","sources":["../../src/http-adapter/http-adapter.fetch.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAE/C,OAAO,KAAK,EAAE,eAAe,EAAkB,MAAM,OAAO,CAAC;AAE7D,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAE7C,OAAO,KAAK,EAAE,oBAAoB,EAAE,uBAAuB,EAAE,MAAM,sBAAsB,CAAC;AAK1F,eAAO,MAAM,UAAU,kZA8KlB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"http-adapter.utils.d.ts","sourceRoot":"","sources":["../../src/http-adapter/http-adapter.utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAwC,MAAM,SAAS,CAAC;AAGrF,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAC/C,OAAO,KAAK,EAAE,gBAAgB,EAAE,UAAU,EAAE,MAAM,OAAO,CAAC;AAE1D,OAAO,KAAK,EAAE,cAAc,EAAE,yBAAyB,EAAE,MAAM,sBAAsB,CAAC;AAItF,eAAO,MAAM,kBAAkB,GAAI,eAAe,MAAM,KAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAc/E,CAAC;AAIF,eAAO,MAAM,aAAa,GAAI,UAAU,MAAM,GAAG,OAAO,QAMvD,CAAC;AAEF,eAAO,MAAM,cAAc,GAAI,gBAAgB,GAAG,EAAE,EAAE,cAAc,MAAM,EAAE,kBAAkB,cAAc,QAa3G,CAAC;AAEF,eAAO,MAAM,kBAAkB,GAAI,CAAC,SAAS,eAAe,EAAE,UAAU,OAAO,KAAG,gBAAgB,CAAC,CAAC,CAEnG,CAAC;
|
|
1
|
+
{"version":3,"file":"http-adapter.utils.d.ts","sourceRoot":"","sources":["../../src/http-adapter/http-adapter.utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAwC,MAAM,SAAS,CAAC;AAGrF,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAC/C,OAAO,KAAK,EAAE,gBAAgB,EAAE,UAAU,EAAE,MAAM,OAAO,CAAC;AAE1D,OAAO,KAAK,EAAE,cAAc,EAAE,yBAAyB,EAAE,MAAM,sBAAsB,CAAC;AAItF,eAAO,MAAM,kBAAkB,GAAI,eAAe,MAAM,KAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAc/E,CAAC;AAIF,eAAO,MAAM,aAAa,GAAI,UAAU,MAAM,GAAG,OAAO,QAMvD,CAAC;AAEF,eAAO,MAAM,cAAc,GAAI,gBAAgB,GAAG,EAAE,EAAE,cAAc,MAAM,EAAE,kBAAkB,cAAc,QAa3G,CAAC;AAEF,eAAO,MAAM,kBAAkB,GAAI,CAAC,SAAS,eAAe,EAAE,UAAU,OAAO,KAAG,gBAAgB,CAAC,CAAC,CAEnG,CAAC;AAiGF,eAAO,MAAM,oBAAoB,GAC/B,aAAa,eAAe,GAAG,MAAM,GAAG,UAAU,EAClD,UAAU,yBAAyB,KAClC,MA4BF,CAAC"}
|
package/dist/index.mjs
CHANGED
|
@@ -3654,56 +3654,56 @@ var encodeValue = (value, { encode, strict }) => {
|
|
|
3654
3654
|
if (encode) return encodeURIComponent(value);
|
|
3655
3655
|
return value;
|
|
3656
3656
|
};
|
|
3657
|
+
var parseValue = (value, options) => {
|
|
3658
|
+
if (value instanceof Date) return options.dateParser?.(value) || value.toISOString();
|
|
3659
|
+
if (value !== null && typeof value === "object" && !Array.isArray(value)) return options.objectParser?.(value) || JSON.stringify(value);
|
|
3660
|
+
return String(value);
|
|
3661
|
+
};
|
|
3657
3662
|
var encodeParams = (key, value, options) => {
|
|
3658
3663
|
const shouldSkip = !isValidValue(options)(value);
|
|
3659
3664
|
if (!key || shouldSkip) return "";
|
|
3660
|
-
|
|
3661
|
-
if (value instanceof Date) return options.dateParser?.(value) || value.toISOString();
|
|
3662
|
-
if (typeof value === "object" && !Array.isArray(value)) return options.objectParser?.(value) || JSON.stringify(value);
|
|
3663
|
-
return String(value);
|
|
3664
|
-
};
|
|
3665
|
-
return `${encodeValue(key, options)}=${encodeValue(parsedValue(), options)}`;
|
|
3665
|
+
return `${encodeValue(key, options)}=${encodeValue(parseValue(value, options), options)}`;
|
|
3666
3666
|
};
|
|
3667
3667
|
var encodeArray = (key, array, options) => {
|
|
3668
3668
|
const { arrayFormat, arraySeparator } = options;
|
|
3669
|
+
const separator = arraySeparator || "|";
|
|
3670
|
+
const encodedKey = encodeValue(key, options);
|
|
3669
3671
|
return array.filter(isValidValue(options)).reduce((acc, value, index) => {
|
|
3672
|
+
const encodedValue = encodeValue(parseValue(value, options), options);
|
|
3670
3673
|
switch (arrayFormat) {
|
|
3671
|
-
case "index":
|
|
3672
|
-
|
|
3673
|
-
acc.push(`${keyValue}${encodeValue(String(value), options)}`);
|
|
3674
|
+
case "index":
|
|
3675
|
+
acc.push(`${encodedKey}[${encodeValue(String(index), options)}]=${encodedValue}`);
|
|
3674
3676
|
break;
|
|
3675
|
-
|
|
3676
|
-
|
|
3677
|
-
const keyValue = `${encodeValue(key, options)}[]=`;
|
|
3678
|
-
acc.push(`${keyValue}${encodeValue(String(value), options)}`);
|
|
3677
|
+
case "bracket":
|
|
3678
|
+
acc.push(`${encodedKey}[]=${encodedValue}`);
|
|
3679
3679
|
break;
|
|
3680
|
-
}
|
|
3681
3680
|
case "comma": {
|
|
3682
|
-
const keyValue = acc.length === 0 && `${
|
|
3683
|
-
return [[...acc, `${keyValue}${
|
|
3681
|
+
const keyValue = acc.length === 0 && `${encodedKey}=` || "";
|
|
3682
|
+
return [[...acc, `${keyValue}${encodedValue}`].join(",")];
|
|
3684
3683
|
}
|
|
3685
3684
|
case "separator": {
|
|
3686
|
-
const keyValue = acc.length === 0 && `${
|
|
3687
|
-
return [[...acc, `${keyValue}${
|
|
3685
|
+
const keyValue = acc.length === 0 && `${encodedKey}=` || "";
|
|
3686
|
+
return [[...acc, `${keyValue}${encodedValue}`].join(separator)];
|
|
3688
3687
|
}
|
|
3689
3688
|
case "bracket-separator": {
|
|
3690
|
-
const keyValue = acc.length === 0 && `${
|
|
3691
|
-
return [[...acc, `${keyValue}${
|
|
3692
|
-
}
|
|
3693
|
-
default: {
|
|
3694
|
-
const keyValue = `${encodeValue(key, options)}=`;
|
|
3695
|
-
acc.push(`${keyValue}${encodeValue(String(value), options)}`);
|
|
3689
|
+
const keyValue = acc.length === 0 && `${encodedKey}[]=` || "";
|
|
3690
|
+
return [[...acc, `${keyValue}${encodedValue}`].join(separator)];
|
|
3696
3691
|
}
|
|
3692
|
+
default: acc.push(`${encodedKey}=${encodedValue}`);
|
|
3697
3693
|
}
|
|
3698
3694
|
return acc;
|
|
3699
3695
|
}, []).join("&");
|
|
3700
3696
|
};
|
|
3701
|
-
var stringifyQueryParams = (queryParams, options
|
|
3697
|
+
var stringifyQueryParams = (queryParams, options) => {
|
|
3698
|
+
const config = {
|
|
3699
|
+
...stringifyDefaultOptions,
|
|
3700
|
+
...options
|
|
3701
|
+
};
|
|
3702
3702
|
if (!queryParams || Object.keys(queryParams).length === 0) return "";
|
|
3703
3703
|
if (typeof queryParams === "string") return queryParams[0] === "?" ? queryParams : `?${queryParams}`;
|
|
3704
3704
|
const stringified = Object.entries(queryParams).map(([key, value]) => {
|
|
3705
|
-
if (Array.isArray(value)) return encodeArray(key, value,
|
|
3706
|
-
return encodeParams(key, value,
|
|
3705
|
+
if (Array.isArray(value)) return encodeArray(key, value, config);
|
|
3706
|
+
return encodeParams(key, value, config);
|
|
3707
3707
|
}).filter(Boolean).join("&");
|
|
3708
3708
|
if (stringified) return `?${stringified}`;
|
|
3709
3709
|
return "";
|
|
@@ -3721,10 +3721,9 @@ var getAdapter = () => new Adapter({
|
|
|
3721
3721
|
const method = options.method ?? HttpMethods.GET;
|
|
3722
3722
|
if (SAFE_HTTP_METHODS.includes(method)) return { deduplicate: true };
|
|
3723
3723
|
return {};
|
|
3724
|
-
}).setFetcher(async ({ request, adapterOptions, headers, payload, onError, onResponseEnd, onTimeoutError, onRequestEnd, createAbortListener, getAbortController, onResponseProgress, onResponseStart, onBeforeRequest, onRequestStart, onRequestProgress, onSuccess }) => {
|
|
3725
|
-
const { method
|
|
3726
|
-
const
|
|
3727
|
-
const fullUrl = `${client.url}${endpoint}${queryString}`;
|
|
3724
|
+
}).setFetcher(async ({ request, url, endpoint, queryParams, adapterOptions, headers, payload, onError, onResponseEnd, onTimeoutError, onRequestEnd, createAbortListener, getAbortController, onResponseProgress, onResponseStart, onBeforeRequest, onRequestStart, onRequestProgress, onSuccess }) => {
|
|
3725
|
+
const { method } = request;
|
|
3726
|
+
const fullUrl = `${url}${endpoint}${queryParams}`;
|
|
3728
3727
|
const controller = getAbortController();
|
|
3729
3728
|
const { timeout: timeoutMs = defaultTimeout, streaming = false, ...restOptions } = adapterOptions || {};
|
|
3730
3729
|
let timedOut = false;
|
|
@@ -4204,7 +4203,7 @@ var stringifyDefaultOptions = {
|
|
|
4204
4203
|
strict: true,
|
|
4205
4204
|
encode: true,
|
|
4206
4205
|
arrayFormat: "bracket",
|
|
4207
|
-
arraySeparator: "
|
|
4206
|
+
arraySeparator: "|",
|
|
4208
4207
|
sort: false,
|
|
4209
4208
|
skipNull: true,
|
|
4210
4209
|
skipEmptyString: true
|