@hyper-fetch/core 10.0.0 → 10.1.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/README.md +126 -86
- package/__tests__/features/adapter/adapter.base.spec.ts +32 -0
- package/__tests__/features/adapter/adapter.bindings.progress.spec.ts +14 -0
- package/__tests__/features/adapter/adapter.bindings.spec.ts +17 -0
- package/__tests__/features/adapter/adapter.query-params-resolver.spec.ts +93 -0
- package/__tests__/features/adapter/adapter.server.spec.ts +46 -0
- package/__tests__/features/cache/cache.timing.spec.ts +34 -12
- package/__tests__/features/dispatcher/dispatcher.cancel.spec.ts +72 -0
- package/__tests__/features/dispatcher/dispatcher.lazy-storage.spec.ts +237 -0
- package/__tests__/features/dispatcher/dispatcher.persistence.spec.ts +187 -0
- package/__tests__/features/dispatcher/dispatcher.retry-time.spec.ts +142 -0
- package/__tests__/features/http-adapter/http-adapter.e2e.spec.ts +146 -0
- package/__tests__/features/managers/app/app.manager.events.spec.ts +46 -1
- package/__tests__/features/request/request.inference.spec.ts +78 -0
- package/__tests__/features/sdk/sdk.base.spec.ts +39 -0
- package/__tests__/features/sdk/sdk.request-fields.spec.ts +23 -0
- package/__tests__/features/sdk/sdk.types.spec.ts +28 -1
- package/dist/adapter/adapter.bindings.d.ts.map +1 -1
- package/dist/adapter/adapter.d.ts +18 -0
- package/dist/adapter/adapter.d.ts.map +1 -1
- package/dist/cache/cache.d.ts +2 -1
- package/dist/cache/cache.d.ts.map +1 -1
- package/dist/client/client.d.ts +2 -2
- package/dist/client/client.d.ts.map +1 -1
- package/dist/dispatcher/dispatcher.d.ts +28 -1
- package/dist/dispatcher/dispatcher.d.ts.map +1 -1
- package/dist/dispatcher/dispatcher.types.d.ts +17 -0
- package/dist/dispatcher/dispatcher.types.d.ts.map +1 -1
- package/dist/http-adapter/http-adapter.fetch.d.ts.map +1 -1
- package/dist/http-adapter/http-adapter.types.d.ts +12 -0
- package/dist/http-adapter/http-adapter.types.d.ts.map +1 -1
- package/dist/index.mjs +280 -42
- package/dist/index.mjs.map +1 -1
- package/dist/managers/app/app.manager.d.ts +5 -0
- package/dist/managers/app/app.manager.d.ts.map +1 -1
- package/dist/managers/request/request.manager.d.ts +7 -0
- package/dist/managers/request/request.manager.d.ts.map +1 -1
- package/dist/request/request.d.ts +3 -3
- package/dist/request/request.d.ts.map +1 -1
- package/dist/request/request.types.d.ts +45 -7
- package/dist/request/request.types.d.ts.map +1 -1
- package/dist/request/request.utils.d.ts +26 -1
- package/dist/request/request.utils.d.ts.map +1 -1
- package/dist/sdk/sdk.d.ts +22 -3
- package/dist/sdk/sdk.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/adapter/adapter.bindings.ts +5 -3
- package/src/adapter/adapter.ts +24 -1
- package/src/cache/cache.ts +5 -2
- package/src/client/client.ts +10 -3
- package/src/dispatcher/dispatcher.ts +161 -6
- package/src/dispatcher/dispatcher.types.ts +18 -0
- package/src/http-adapter/http-adapter.fetch.ts +76 -19
- package/src/http-adapter/http-adapter.types.ts +13 -0
- package/src/managers/app/app.manager.ts +17 -2
- package/src/managers/request/request.manager.ts +15 -1
- package/src/request/request.ts +5 -4
- package/src/request/request.types.ts +71 -11
- package/src/request/request.utils.ts +55 -0
- package/src/sdk/sdk.ts +33 -3
|
@@ -178,7 +178,7 @@ export type RequestJSON<Request extends RequestInstance> = {
|
|
|
178
178
|
auth: boolean;
|
|
179
179
|
cancelable: boolean;
|
|
180
180
|
retry: number;
|
|
181
|
-
retryTime:
|
|
181
|
+
retryTime: RetryTimeType;
|
|
182
182
|
cacheTime: number;
|
|
183
183
|
cache: boolean;
|
|
184
184
|
staleTime: number;
|
|
@@ -207,6 +207,19 @@ export type RequestJSON<Request extends RequestInstance> = {
|
|
|
207
207
|
|
|
208
208
|
// Request
|
|
209
209
|
|
|
210
|
+
/**
|
|
211
|
+
* Delay in milliseconds before a retry. The function form gets the `attempt` (0 for the first retry) and the failed
|
|
212
|
+
* `response`. A delay that is not a positive finite number retries right away. When the function throws, the request
|
|
213
|
+
* is not retried.
|
|
214
|
+
*/
|
|
215
|
+
export type RetryTimeType = number | RetryTimeFunctionType;
|
|
216
|
+
|
|
217
|
+
/** Function form of the retry time - returns the delay in milliseconds before the retry `attempt` (from 0) */
|
|
218
|
+
export type RetryTimeFunctionType = (options: {
|
|
219
|
+
attempt: number;
|
|
220
|
+
response: RequestResponseType<RequestInstance>;
|
|
221
|
+
}) => number;
|
|
222
|
+
|
|
210
223
|
/**
|
|
211
224
|
* Configuration options for request creation
|
|
212
225
|
*/
|
|
@@ -237,9 +250,11 @@ export type RequestOptionsType<GenericEndpoint, AdapterOptions, RequestMethods =
|
|
|
237
250
|
*/
|
|
238
251
|
retry?: number;
|
|
239
252
|
/**
|
|
240
|
-
*
|
|
253
|
+
* Delay in milliseconds before a retry, or a function that returns it for each attempt - for example
|
|
254
|
+
* `exponentialBackoff()`. A function is not kept when the request is serialized for a persistent queue.
|
|
255
|
+
* @default 500
|
|
241
256
|
*/
|
|
242
|
-
retryTime?:
|
|
257
|
+
retryTime?: RetryTimeType;
|
|
243
258
|
/**
|
|
244
259
|
* Should we trigger garbage collection or leave data in memory
|
|
245
260
|
*/
|
|
@@ -324,18 +339,61 @@ export type RequestConfigurationType<
|
|
|
324
339
|
scope?: string | null;
|
|
325
340
|
} & Partial<NullableKeys<RequestOptionsType<GenericEndpoint, AdapterOptions, MethodsType>>>;
|
|
326
341
|
|
|
342
|
+
/**
|
|
343
|
+
* The function signature of the `__apiType` decoration an endpoint carries - the one graphql-codegen puts on a
|
|
344
|
+
* `TypedDocumentNode` and gql.tada on its documents. `undefined` for endpoints without it.
|
|
345
|
+
*/
|
|
346
|
+
type ExtractEndpointApiType<Endpoint> = "__apiType" extends keyof Endpoint
|
|
347
|
+
? Endpoint extends { __apiType?: infer ApiType }
|
|
348
|
+
? NonNullable<ApiType>
|
|
349
|
+
: undefined
|
|
350
|
+
: undefined;
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* Response type an endpoint is decorated with through `__apiType`, like a `TypedDocumentNode`.
|
|
354
|
+
* `createRequest` uses it when its generics do not declare the `response`. `undefined` without the decoration.
|
|
355
|
+
*
|
|
356
|
+
* The decoration is meant for document endpoints. On a string endpoint it hides the url params from the types.
|
|
357
|
+
*/
|
|
358
|
+
export type ExtractEndpointResponseType<Endpoint> =
|
|
359
|
+
ExtractEndpointApiType<Endpoint> extends (payload: any) => infer Response ? Response : undefined;
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* Payload type an endpoint is decorated with through `__apiType`, like the variables of a `TypedDocumentNode`.
|
|
363
|
+
* `createRequest` uses it when its generics do not declare the `payload`. Without the decoration, or when the payload
|
|
364
|
+
* can hold no keys (`{ [key: string]: never }`), it is `undefined` - there is nothing to send.
|
|
365
|
+
*/
|
|
366
|
+
export type ExtractEndpointPayloadType<Endpoint> =
|
|
367
|
+
ExtractEndpointApiType<Endpoint> extends (payload: infer Payload) => any
|
|
368
|
+
? Payload extends Record<string, never>
|
|
369
|
+
? undefined
|
|
370
|
+
: Payload
|
|
371
|
+
: undefined;
|
|
372
|
+
|
|
327
373
|
export type ParamType = string | number;
|
|
328
374
|
export type ParamsType = Record<string, ParamType>;
|
|
329
375
|
|
|
376
|
+
/**
|
|
377
|
+
* A param name starts with a letter or `_`. Other colons - a protocol, a port, or the `$id: ID!` of a GraphQL
|
|
378
|
+
* document - are not params.
|
|
379
|
+
*/
|
|
380
|
+
type IsParamNameStart<Char extends string> = Char extends "_"
|
|
381
|
+
? true
|
|
382
|
+
: Lowercase<Char> extends Uppercase<Char>
|
|
383
|
+
? false
|
|
384
|
+
: true;
|
|
385
|
+
|
|
330
386
|
export type ExtractUrlParams<T extends string> = string extends T
|
|
331
387
|
? EmptyTypes
|
|
332
|
-
:
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
388
|
+
: T extends `${string}:${infer AfterColon}`
|
|
389
|
+
? AfterColon extends `${infer First}${string}`
|
|
390
|
+
? IsParamNameStart<First> extends true
|
|
391
|
+
? AfterColon extends `${infer Param}/${infer Rest}`
|
|
392
|
+
? { [k in Param | keyof ExtractUrlParams<Rest>]: ParamType }
|
|
393
|
+
: { [k in AfterColon]: ParamType }
|
|
394
|
+
: ExtractUrlParams<AfterColon>
|
|
395
|
+
: EmptyTypes
|
|
396
|
+
: EmptyTypes;
|
|
339
397
|
|
|
340
398
|
/**
|
|
341
399
|
* If the request endpoint parameters are not filled it will throw an error
|
|
@@ -492,7 +550,9 @@ export type OptimisticCallback<Req extends RequestInstance, Ctx = undefined> = (
|
|
|
492
550
|
/**
|
|
493
551
|
* Per-call optimistic update - operations or a callback like the one of `setOptimistic`. Replaces the request's own.
|
|
494
552
|
*/
|
|
495
|
-
export type OptimisticSendOptionType<Req extends RequestInstance> =
|
|
553
|
+
export type OptimisticSendOptionType<Req extends RequestInstance> =
|
|
554
|
+
| OptimisticUpdatesType
|
|
555
|
+
| OptimisticCallback<Req, any>;
|
|
496
556
|
|
|
497
557
|
// Mappers
|
|
498
558
|
|
|
@@ -15,6 +15,7 @@ import type {
|
|
|
15
15
|
RequestInstance,
|
|
16
16
|
RequestJSON,
|
|
17
17
|
RequestSendOptionsType,
|
|
18
|
+
RetryTimeFunctionType,
|
|
18
19
|
} from "./request.types";
|
|
19
20
|
|
|
20
21
|
type MappedResponse =
|
|
@@ -22,6 +23,21 @@ type MappedResponse =
|
|
|
22
23
|
| Promise<ResponseType<any, any, AdapterInstance>>
|
|
23
24
|
| undefined;
|
|
24
25
|
|
|
26
|
+
/**
|
|
27
|
+
* Turns query params given as a callback into data with the adapter's resolver, so a request never stores a function.
|
|
28
|
+
* Without a resolver the value is kept as given.
|
|
29
|
+
*/
|
|
30
|
+
export const resolveQueryParams = <T>(
|
|
31
|
+
client: { adapter: Pick<AdapterInstance, "unstable_queryParamsResolver"> },
|
|
32
|
+
queryParams: T,
|
|
33
|
+
): T => {
|
|
34
|
+
const resolver = client.adapter.unstable_queryParamsResolver;
|
|
35
|
+
if (typeof queryParams === "function" && resolver) {
|
|
36
|
+
return resolver(queryParams as (...args: any[]) => unknown) as T;
|
|
37
|
+
}
|
|
38
|
+
return queryParams;
|
|
39
|
+
};
|
|
40
|
+
|
|
25
41
|
export const scopeKey = (key: string, scope: string | null): string => {
|
|
26
42
|
return scope ? `${scope}__${key}` : key;
|
|
27
43
|
};
|
|
@@ -402,3 +418,42 @@ export const sendRequest = async <Request extends RequestInstance>(
|
|
|
402
418
|
}
|
|
403
419
|
});
|
|
404
420
|
};
|
|
421
|
+
|
|
422
|
+
/** Above this exponent `min * 2 ** attempt` is past any sane maximum, and a larger one only risks Infinity */
|
|
423
|
+
const MAX_BACKOFF_EXPONENT = 30;
|
|
424
|
+
|
|
425
|
+
/**
|
|
426
|
+
* Retry time that grows exponentially with every attempt: `min * 2 ** attempt`, never above `max`. With `jitter` (the
|
|
427
|
+
* default) the delay is a random value from `min` up to that window, so the clients that failed together do not all
|
|
428
|
+
* retry at the same moment.
|
|
429
|
+
*
|
|
430
|
+
* @example
|
|
431
|
+
* client.createRequest()({ endpoint: "/users", retry: 5, retryTime: exponentialBackoff({ min: 500, max: 10_000 }) });
|
|
432
|
+
*/
|
|
433
|
+
export const exponentialBackoff = ({
|
|
434
|
+
min = 500,
|
|
435
|
+
max = 30_000,
|
|
436
|
+
jitter = true,
|
|
437
|
+
random = Math.random,
|
|
438
|
+
}: {
|
|
439
|
+
/** Delay of the first retry in milliseconds. @default 500 */
|
|
440
|
+
min?: number;
|
|
441
|
+
/** Longest delay in milliseconds. @default 30000 */
|
|
442
|
+
max?: number;
|
|
443
|
+
/** Spread the delays randomly between `min` and the exponential window. @default true */
|
|
444
|
+
jitter?: boolean;
|
|
445
|
+
/** Source of the randomness, for deterministic tests. @default Math.random */
|
|
446
|
+
random?: () => number;
|
|
447
|
+
} = {}): RetryTimeFunctionType => {
|
|
448
|
+
return ({ attempt }) => {
|
|
449
|
+
const floor = Math.max(0, min);
|
|
450
|
+
const exponent = Math.min(Math.max(0, attempt), MAX_BACKOFF_EXPONENT);
|
|
451
|
+
const exponential = floor * 2 ** exponent;
|
|
452
|
+
|
|
453
|
+
if (!jitter) {
|
|
454
|
+
return Math.min(max, exponential);
|
|
455
|
+
}
|
|
456
|
+
const window = Math.max(0, Math.min(max - floor, exponential));
|
|
457
|
+
return Math.round(floor + random() * window);
|
|
458
|
+
};
|
|
459
|
+
};
|
package/src/sdk/sdk.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ClientInstance } from "client";
|
|
2
|
-
import type { RequestInstance } from "request";
|
|
2
|
+
import type { RequestInstance, RetryTimeType } from "request";
|
|
3
3
|
import { Request } from "request";
|
|
4
4
|
import type {
|
|
5
5
|
ExtractEndpointType,
|
|
@@ -28,6 +28,21 @@ export type RecursiveSchemaType = Record<
|
|
|
28
28
|
* schema objects are recursed into. Depth is guarded at 10 levels to keep the type checker
|
|
29
29
|
* within reasonable bounds.
|
|
30
30
|
*/
|
|
31
|
+
/**
|
|
32
|
+
* Keys every request has at runtime - its public properties and the private ones. Below the top level, the SDK reads
|
|
33
|
+
* such a key from the request instead of treating it as a path segment or procedure name.
|
|
34
|
+
*/
|
|
35
|
+
type SdkReservedKey = keyof RequestInstance | "updatedAbortKey" | "updatedCacheKey" | "updatedQueryKey" | "paramsMapper";
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Type of a schema key the SDK can't reach, because every request has a property with the same name - for example a
|
|
39
|
+
* procedure called `send`. Using it fails to compile instead of crashing at runtime. Rename the key, or set the path
|
|
40
|
+
* with a `$`-prefixed key.
|
|
41
|
+
*/
|
|
42
|
+
export type SdkReservedKeyError<Key extends PropertyKey> = {
|
|
43
|
+
__sdkError: `"${Key & string}" is a property of every request, so the SDK can't reach a path or procedure with this name. Rename it in the schema.`;
|
|
44
|
+
};
|
|
45
|
+
|
|
31
46
|
export type InjectClient<T, TClient extends ClientInstance, Depth extends unknown[] = []> = Depth["length"] extends 10
|
|
32
47
|
? T
|
|
33
48
|
: T extends RequestInstance
|
|
@@ -44,7 +59,14 @@ export type InjectClient<T, TClient extends ClientInstance, Depth extends unknow
|
|
|
44
59
|
ExtractMutationContextType<T>
|
|
45
60
|
>
|
|
46
61
|
: T extends Record<string, any>
|
|
47
|
-
? {
|
|
62
|
+
? {
|
|
63
|
+
// The top level is not a request, so only the nested keys can clash with the request properties
|
|
64
|
+
[K in keyof T]: Depth["length"] extends 0
|
|
65
|
+
? InjectClient<T[K], TClient, [...Depth, unknown]>
|
|
66
|
+
: K extends SdkReservedKey
|
|
67
|
+
? SdkReservedKeyError<K>
|
|
68
|
+
: InjectClient<T[K], TClient, [...Depth, unknown]>;
|
|
69
|
+
}
|
|
48
70
|
: T;
|
|
49
71
|
|
|
50
72
|
/**
|
|
@@ -58,7 +80,7 @@ export type SdkRequestDefaults = {
|
|
|
58
80
|
cacheTime?: number;
|
|
59
81
|
staleTime?: number;
|
|
60
82
|
retry?: number;
|
|
61
|
-
retryTime?:
|
|
83
|
+
retryTime?: RetryTimeType;
|
|
62
84
|
cancelable?: boolean;
|
|
63
85
|
queued?: boolean;
|
|
64
86
|
offline?: boolean;
|
|
@@ -132,6 +154,12 @@ export type SdkConfigurationMap<Schema extends RecursiveSchemaType = RecursiveSc
|
|
|
132
154
|
export type CreateSdkOptions<Schema extends RecursiveSchemaType = RecursiveSchemaType> = {
|
|
133
155
|
/** @default true */
|
|
134
156
|
camelCaseToKebabCase?: boolean;
|
|
157
|
+
/**
|
|
158
|
+
* URL segment of a schema key, when it differs from the key - for example `{ findByStatus: "findByStatus" }` keeps
|
|
159
|
+
* the camelCase segment that `camelCaseToKebabCase` would turn into `find-by-status`. Mapped keys are not converted.
|
|
160
|
+
* SDKs generated by the CLI fill it from the paths of the API schema.
|
|
161
|
+
*/
|
|
162
|
+
pathSegments?: Record<string, string>;
|
|
135
163
|
/** @default (method) => method.toUpperCase() */
|
|
136
164
|
methodTransform?: (method: string) => string;
|
|
137
165
|
/** Per-endpoint request defaults */
|
|
@@ -255,6 +283,8 @@ const createRecursiveProxy = (
|
|
|
255
283
|
isMethod = true;
|
|
256
284
|
methodName = key.slice(1);
|
|
257
285
|
pathSegment = `:${key.slice(1)}`;
|
|
286
|
+
} else if (options?.pathSegments && Object.prototype.hasOwnProperty.call(options.pathSegments, key)) {
|
|
287
|
+
pathSegment = options.pathSegments[key];
|
|
258
288
|
} else if (options?.camelCaseToKebabCase) {
|
|
259
289
|
pathSegment = key.replaceAll(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`);
|
|
260
290
|
}
|