@hyper-fetch/core 10.4.0 → 10.4.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/__tests__/features/adapter/adapter.mapping.spec.ts +29 -1
- package/__tests__/features/cache/cache.modes.spec.ts +33 -0
- package/__tests__/features/client/client.configuration.spec.ts +24 -0
- package/dist/adapter/adapter.d.ts +8 -1
- package/dist/adapter/adapter.d.ts.map +1 -1
- package/dist/adapter/adapter.types.d.ts +7 -8
- package/dist/adapter/adapter.types.d.ts.map +1 -1
- package/dist/client/client.types.d.ts +2 -1
- package/dist/client/client.types.d.ts.map +1 -1
- package/dist/client/client.utils.d.ts +6 -1
- package/dist/client/client.utils.d.ts.map +1 -1
- package/dist/index.mjs +19 -4
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/adapter/adapter.ts +10 -3
- package/src/adapter/adapter.types.ts +7 -5
- package/src/client/client.types.ts +2 -1
- package/src/client/client.utils.ts +12 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hyper-fetch/core",
|
|
3
|
-
"version": "10.4.
|
|
3
|
+
"version": "10.4.1",
|
|
4
4
|
"description": "Type-safe data fetching for TypeScript: typed requests, caching, retries, deduplication, offline queues and adapters",
|
|
5
5
|
"author": "Maciej Pyrc <maciekpyrc@gmail.com>, Kacper Skawina <kacper.skawina@gmail.com>",
|
|
6
6
|
"homepage": "https://hyperfetch.bettertyped.com/docs/core/overview/",
|
package/src/adapter/adapter.ts
CHANGED
|
@@ -274,12 +274,19 @@ export class Adapter<
|
|
|
274
274
|
};
|
|
275
275
|
|
|
276
276
|
/**
|
|
277
|
-
* Set the
|
|
277
|
+
* Set the payload mapping function which runs before every request is sent.
|
|
278
|
+
* It is called with the raw payload and the value set with `setPayloadMapperConfig`,
|
|
279
|
+
* and its return value is the body passed to the adapter.
|
|
280
|
+
* Requests with their own `request.setPayloadMapper` skip it.
|
|
281
|
+
* @example
|
|
282
|
+
* ```ts
|
|
283
|
+
* client.adapter.setPayloadMapper((payload, config) => encrypt(JSON.stringify(payload), config));
|
|
284
|
+
* ```
|
|
278
285
|
*/
|
|
279
286
|
setPayloadMapper = <NewMapper extends AdapterPayloadMappingType>(payloadMapper: NewMapper) => {
|
|
280
|
-
this.unstable_payloadMapper = ((
|
|
287
|
+
this.unstable_payloadMapper = ((payload: Parameters<NewMapper>[0]) =>
|
|
281
288
|
payloadMapper(
|
|
282
|
-
|
|
289
|
+
payload,
|
|
283
290
|
this.unstable_payloadMapperConfig as Parameters<NewMapper>[1],
|
|
284
291
|
)) as unknown as PayloadMapperType;
|
|
285
292
|
return this as unknown as Adapter<
|
|
@@ -89,17 +89,19 @@ export type QueryParamsMapper<QueryParams, Config = never> = (
|
|
|
89
89
|
queryParams: QueryParams | EmptyTypes,
|
|
90
90
|
config?: Config,
|
|
91
91
|
) => any;
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
92
|
+
/**
|
|
93
|
+
* Adapter-level payload mapper. It receives the raw payload of the request (after the request mapper ran) and the
|
|
94
|
+
* value set with `setPayloadMapperConfig`, and returns the body handed to the adapter. It is skipped for requests with
|
|
95
|
+
* their own `request.setPayloadMapper`.
|
|
96
|
+
*/
|
|
97
|
+
export type AdapterPayloadMappingType<Config = never> = (payload: unknown, config?: Config) => any;
|
|
96
98
|
|
|
97
99
|
export type DefaultEndpointMapper = (endpoint: string) => string;
|
|
98
100
|
export type DefaultQueryParamsMapper = (
|
|
99
101
|
queryParams: QueryParamsType | string | EmptyTypes,
|
|
100
102
|
) => QueryParamsType | string | EmptyTypes;
|
|
101
103
|
export type DefaultHeaderMapper = (request: RequestInstance) => HeadersInit;
|
|
102
|
-
export type DefaultPayloadMapper = (
|
|
104
|
+
export type DefaultPayloadMapper = (payload: unknown) => string | FormData;
|
|
103
105
|
|
|
104
106
|
// QueryParams
|
|
105
107
|
|
|
@@ -54,7 +54,8 @@ export type ClientOptionsType<C extends ClientInstance> = {
|
|
|
54
54
|
/**
|
|
55
55
|
* How the effective {@link ClientMode} is chosen for cache behavior.
|
|
56
56
|
*
|
|
57
|
-
* - `"auto"` (default when omitted):
|
|
57
|
+
* - `"auto"` (default when omitted): `"client"` in a browser, React Native and Electron (main process included),
|
|
58
|
+
* because they serve one user. `"server"` elsewhere, like Node.js, Deno, Bun or edge runtimes.
|
|
58
59
|
* - `"client"` / `"server"`: force that mode regardless of environment (tests, SSR overrides, etc.).
|
|
59
60
|
*/
|
|
60
61
|
mode?: ClientModeOption;
|
|
@@ -10,14 +10,24 @@ import type {
|
|
|
10
10
|
ResponseInterceptorType,
|
|
11
11
|
} from "./client.types";
|
|
12
12
|
|
|
13
|
-
/**
|
|
13
|
+
/**
|
|
14
|
+
* Picks effective {@link ClientMode}: explicit override, else environment detection.
|
|
15
|
+
*
|
|
16
|
+
* Auto mode picks `"client"` in runtimes that serve one user: a browser, React Native and every Electron process.
|
|
17
|
+
* Anything else, like Node.js, Deno, Bun or an edge runtime, may serve many users and gets `"server"`.
|
|
18
|
+
*/
|
|
14
19
|
export function resolveClientMode(modeOption: ClientModeOption | undefined): ClientMode {
|
|
15
20
|
if (modeOption === "client" || modeOption === "server") {
|
|
16
21
|
return modeOption;
|
|
17
22
|
}
|
|
18
23
|
|
|
19
24
|
const inBrowser = typeof window !== "undefined" && typeof document !== "undefined";
|
|
20
|
-
|
|
25
|
+
// React Native has a window but no document, and marks itself on navigator.product
|
|
26
|
+
const inReactNative = typeof navigator !== "undefined" && navigator.product === "ReactNative";
|
|
27
|
+
// The Electron main process has neither window nor document, but it is one desktop user's app
|
|
28
|
+
const inElectron = typeof process !== "undefined" && !!process.versions?.electron;
|
|
29
|
+
|
|
30
|
+
if (inBrowser || inReactNative || inElectron) {
|
|
21
31
|
return "client";
|
|
22
32
|
}
|
|
23
33
|
|