@hyper-fetch/core 9.5.1 → 9.6.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/dispatcher/dispatcher.retry.spec.ts +220 -0
- package/__tests__/features/infinite/infinite.fetch.spec.ts +213 -0
- package/__tests__/features/infinite/infinite.utils.spec.ts +388 -0
- package/__tests__/features/request/request.response-mapper-failure.spec.ts +362 -0
- package/dist/dispatcher/dispatcher.d.ts +10 -0
- package/dist/dispatcher/dispatcher.d.ts.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.mjs +354 -6
- package/dist/index.mjs.map +1 -1
- package/dist/infinite/index.d.ts +4 -0
- package/dist/infinite/index.d.ts.map +1 -0
- package/dist/infinite/infinite.d.ts +16 -0
- package/dist/infinite/infinite.d.ts.map +1 -0
- package/dist/infinite/infinite.types.d.ts +151 -0
- package/dist/infinite/infinite.types.d.ts.map +1 -0
- package/dist/infinite/infinite.utils.d.ts +53 -0
- package/dist/infinite/infinite.utils.d.ts.map +1 -0
- package/dist/request/request.utils.d.ts +12 -0
- package/dist/request/request.utils.d.ts.map +1 -1
- package/package.json +2 -3
- package/src/dispatcher/dispatcher.ts +49 -1
- package/src/index.ts +1 -0
- package/src/infinite/index.ts +3 -0
- package/src/infinite/infinite.ts +75 -0
- package/src/infinite/infinite.types.ts +180 -0
- package/src/infinite/infinite.utils.ts +306 -0
- package/src/request/request.utils.ts +34 -5
- package/release.config.js +0 -6
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/infinite/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,kBAAkB,CAAC;AACjC,cAAc,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { RequestInstance } from '../request';
|
|
2
|
+
import { FetchInfiniteOptions, FetchInfiniteResult, Pagination } from './infinite.types';
|
|
3
|
+
/**
|
|
4
|
+
* Fetches the first pages of a list one after another, without touching the cache or the queues.
|
|
5
|
+
* Made for server rendering: pass `dehydrated` to the `Hydrate` component to render them on the client.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* const { dehydrated } = await fetchInfinite(getFeed, {
|
|
10
|
+
* pagination: { cursor: "cursor", next: "nextCursor", items: "data" },
|
|
11
|
+
* pages: 2,
|
|
12
|
+
* });
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export declare const fetchInfinite: <R extends RequestInstance, const P extends Pagination<R>>(request: R, options: FetchInfiniteOptions<P>) => Promise<FetchInfiniteResult<R, P>>;
|
|
16
|
+
//# sourceMappingURL=infinite.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"infinite.d.ts","sourceRoot":"","sources":["../../src/infinite/infinite.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAG/C,OAAO,KAAK,EAAE,oBAAoB,EAAE,mBAAmB,EAAE,UAAU,EAAoB,MAAM,kBAAkB,CAAC;AAUhH;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,aAAa,GAAU,CAAC,SAAS,eAAe,EAAE,KAAK,CAAC,CAAC,SAAS,UAAU,CAAC,CAAC,CAAC,EAC1F,SAAS,CAAC,EACV,SAAS,oBAAoB,CAAC,CAAC,CAAC,KAC/B,OAAO,CAAC,mBAAmB,CAAC,CAAC,EAAE,CAAC,CAAC,CA8CnC,CAAC"}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { ResponseType } from '../adapter';
|
|
2
|
+
import { RequestInstance } from '../request';
|
|
3
|
+
import { ExtractAdapterType, ExtractErrorType, ExtractParamsType, ExtractPayloadType, ExtractQueryParamsType, ExtractResponseType, HydrateDataType } from '../types';
|
|
4
|
+
/** Part of the request that holds the paging field */
|
|
5
|
+
export type PaginationSource = "queryParams" | "params" | "payload";
|
|
6
|
+
export type PaginationMode = "page" | "offset" | "cursor";
|
|
7
|
+
type PaginationFields<R, In extends PaginationSource> = NonNullable<In extends "params" ? ExtractParamsType<R> : In extends "payload" ? ExtractPayloadType<R> : ExtractQueryParamsType<R>>;
|
|
8
|
+
type KeyOf<T> = Extract<keyof T, string>;
|
|
9
|
+
type NumericFieldKey<T> = {
|
|
10
|
+
[K in KeyOf<T>]-?: number extends NonNullable<T[K]> ? K : never;
|
|
11
|
+
}[KeyOf<T>];
|
|
12
|
+
type ArrayFieldKey<T> = {
|
|
13
|
+
[K in KeyOf<T>]-?: NonNullable<T[K]> extends readonly unknown[] ? K : never;
|
|
14
|
+
}[KeyOf<T>];
|
|
15
|
+
type NumberFieldKey<T> = {
|
|
16
|
+
[K in KeyOf<T>]-?: NonNullable<T[K]> extends number ? K : never;
|
|
17
|
+
}[KeyOf<T>];
|
|
18
|
+
type CursorFieldKey<T> = {
|
|
19
|
+
[K in KeyOf<T>]-?: NonNullable<T[K]> extends string | number ? K : never;
|
|
20
|
+
}[KeyOf<T>];
|
|
21
|
+
/**
|
|
22
|
+
* Passed to every pagination callback
|
|
23
|
+
*/
|
|
24
|
+
export type PaginationContext<R> = {
|
|
25
|
+
/** Response data of the page */
|
|
26
|
+
data: ExtractResponseType<R>;
|
|
27
|
+
/** Value of the paging field used to fetch this page */
|
|
28
|
+
param: unknown;
|
|
29
|
+
/** Response data of the pages loaded so far */
|
|
30
|
+
pages: ExtractResponseType<R>[];
|
|
31
|
+
};
|
|
32
|
+
type PaginationItems<R> = (context: PaginationContext<R>) => readonly unknown[];
|
|
33
|
+
type PaginationCursor<R> = (context: PaginationContext<R>) => string | number | null | undefined;
|
|
34
|
+
type PaginationItemsOption<R> = ExtractResponseType<R> extends readonly unknown[] ? {
|
|
35
|
+
/** Where the items are. Not needed when the response is an array. */
|
|
36
|
+
items?: PaginationItems<R>;
|
|
37
|
+
} : {
|
|
38
|
+
/** Array field of the response holding the items, or a function returning them */
|
|
39
|
+
items: ArrayFieldKey<NonNullable<ExtractResponseType<R>>> | PaginationItems<R>;
|
|
40
|
+
};
|
|
41
|
+
type PaginationShared<R> = PaginationItemsOption<R> & {
|
|
42
|
+
/** Number field of the response with the size of the whole list, or a function returning it */
|
|
43
|
+
total?: NumberFieldKey<NonNullable<ExtractResponseType<R>>> | ((context: PaginationContext<R>) => number | null | undefined);
|
|
44
|
+
/** Overrides detecting the end of the list */
|
|
45
|
+
hasNext?: (context: PaginationContext<R>) => boolean;
|
|
46
|
+
/** Overrides detecting the start of the list */
|
|
47
|
+
hasPrevious?: (context: PaginationContext<R>) => boolean;
|
|
48
|
+
};
|
|
49
|
+
type PaginationModes<R, In extends PaginationSource> = {
|
|
50
|
+
/** Numeric field holding the page number */
|
|
51
|
+
page: NumericFieldKey<PaginationFields<R, In>>;
|
|
52
|
+
/** Numeric field holding the page size */
|
|
53
|
+
limit?: NumericFieldKey<PaginationFields<R, In>>;
|
|
54
|
+
offset?: never;
|
|
55
|
+
cursor?: never;
|
|
56
|
+
next?: never;
|
|
57
|
+
previous?: never;
|
|
58
|
+
} | {
|
|
59
|
+
/** Numeric field holding the number of skipped items */
|
|
60
|
+
offset: NumericFieldKey<PaginationFields<R, In>>;
|
|
61
|
+
/** Numeric field holding the page size */
|
|
62
|
+
limit: NumericFieldKey<PaginationFields<R, In>>;
|
|
63
|
+
page?: never;
|
|
64
|
+
cursor?: never;
|
|
65
|
+
next?: never;
|
|
66
|
+
previous?: never;
|
|
67
|
+
} | {
|
|
68
|
+
/** Field holding the cursor */
|
|
69
|
+
cursor: KeyOf<PaginationFields<R, In>>;
|
|
70
|
+
/** Response field with the cursor of the next page, or a function returning it */
|
|
71
|
+
next: CursorFieldKey<NonNullable<ExtractResponseType<R>>> | PaginationCursor<R>;
|
|
72
|
+
/** Response field with the cursor of the previous page, or a function returning it */
|
|
73
|
+
previous?: CursorFieldKey<NonNullable<ExtractResponseType<R>>> | PaginationCursor<R>;
|
|
74
|
+
page?: never;
|
|
75
|
+
offset?: never;
|
|
76
|
+
limit?: never;
|
|
77
|
+
};
|
|
78
|
+
/**
|
|
79
|
+
* Describes which fields of a request page through a list. Every key is checked against the request types.
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```ts
|
|
83
|
+
* const pagination = { cursor: "cursor", next: "nextCursor", items: "data" } satisfies Pagination<typeof getFeed>;
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
export type Pagination<R> = PaginationShared<R> & ((PaginationModes<R, "queryParams"> & {
|
|
87
|
+
in?: "queryParams";
|
|
88
|
+
}) | (PaginationModes<R, "params"> & {
|
|
89
|
+
in: "params";
|
|
90
|
+
}) | (PaginationModes<R, "payload"> & {
|
|
91
|
+
in: "payload";
|
|
92
|
+
}));
|
|
93
|
+
type ArrayItem<T> = NonNullable<T> extends readonly (infer Item)[] ? Item : never;
|
|
94
|
+
/**
|
|
95
|
+
* Type of a single list item for the given request and pagination
|
|
96
|
+
*/
|
|
97
|
+
export type PaginationItem<R, P> = P extends {
|
|
98
|
+
items: infer Items;
|
|
99
|
+
} ? Items extends (...args: any[]) => infer Output ? ArrayItem<Output> : Items extends keyof NonNullable<ExtractResponseType<R>> ? ArrayItem<NonNullable<ExtractResponseType<R>>[Items]> : never : ArrayItem<ExtractResponseType<R>>;
|
|
100
|
+
/**
|
|
101
|
+
* Untyped form of the pagination, used where the request type is not known
|
|
102
|
+
*/
|
|
103
|
+
export type PaginationConfig = {
|
|
104
|
+
page?: string;
|
|
105
|
+
offset?: string;
|
|
106
|
+
limit?: string;
|
|
107
|
+
cursor?: string;
|
|
108
|
+
next?: string | ((context: any) => unknown);
|
|
109
|
+
previous?: string | ((context: any) => unknown);
|
|
110
|
+
items?: string | ((context: any) => readonly unknown[]);
|
|
111
|
+
total?: string | ((context: any) => number | null | undefined);
|
|
112
|
+
hasNext?: (context: any) => boolean;
|
|
113
|
+
hasPrevious?: (context: any) => boolean;
|
|
114
|
+
in?: PaginationSource;
|
|
115
|
+
};
|
|
116
|
+
export type PaginationPageContext = {
|
|
117
|
+
request: RequestInstance;
|
|
118
|
+
pagination: PaginationConfig;
|
|
119
|
+
data: any;
|
|
120
|
+
param: unknown;
|
|
121
|
+
pages: any[];
|
|
122
|
+
};
|
|
123
|
+
export type CachedInfinitePage<T = any> = {
|
|
124
|
+
param: unknown;
|
|
125
|
+
request: RequestInstance;
|
|
126
|
+
value: T;
|
|
127
|
+
};
|
|
128
|
+
export type FetchInfiniteOptions<P> = {
|
|
129
|
+
pagination: P;
|
|
130
|
+
/** How many pages to fetch, one after another. Defaults to 1. */
|
|
131
|
+
pages?: number;
|
|
132
|
+
};
|
|
133
|
+
export type FetchInfiniteResult<R extends RequestInstance, P> = {
|
|
134
|
+
/** Items of all the fetched pages */
|
|
135
|
+
items: PaginationItem<R, P>[];
|
|
136
|
+
/** Response data of the fetched pages */
|
|
137
|
+
pages: ExtractResponseType<R>[];
|
|
138
|
+
/** Paging values of the fetched pages */
|
|
139
|
+
pageParams: unknown[];
|
|
140
|
+
/** Size of the whole list, when the pagination declares `total` */
|
|
141
|
+
total: number | null;
|
|
142
|
+
hasNextPage: boolean;
|
|
143
|
+
/** Error of the page that stopped fetching */
|
|
144
|
+
error: ExtractErrorType<R> | null;
|
|
145
|
+
/** Full responses of the fetched pages */
|
|
146
|
+
responses: ResponseType<ExtractResponseType<R>, ExtractErrorType<R>, ExtractAdapterType<R>>[];
|
|
147
|
+
/** Pass it to `client.hydrate()` or the `Hydrate` component */
|
|
148
|
+
dehydrated: HydrateDataType[];
|
|
149
|
+
};
|
|
150
|
+
export {};
|
|
151
|
+
//# sourceMappingURL=infinite.types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"infinite.types.d.ts","sourceRoot":"","sources":["../../src/infinite/infinite.types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAC5C,OAAO,KAAK,EAAE,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OAAO,EAAE,eAAe,CAAC;IACzB,KAAK,EAAE,CAAC,CAAC;CACV,CAAC;AAEF,MAAM,MAAM,oBAAoB,CAAC,CAAC,IAAI;IACpC,UAAU,EAAE,CAAC,CAAC;IACd,iEAAiE;IACjE,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF,MAAM,MAAM,mBAAmB,CAAC,CAAC,SAAS,eAAe,EAAE,CAAC,IAAI;IAC9D,qCAAqC;IACrC,KAAK,EAAE,cAAc,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC;IAC9B,yCAAyC;IACzC,KAAK,EAAE,mBAAmB,CAAC,CAAC,CAAC,EAAE,CAAC;IAChC,yCAAyC;IACzC,UAAU,EAAE,OAAO,EAAE,CAAC;IACtB,mEAAmE;IACnE,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,WAAW,EAAE,OAAO,CAAC;IACrB,8CAA8C;IAC9C,KAAK,EAAE,gBAAgB,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC;IAClC,0CAA0C;IAC1C,SAAS,EAAE,YAAY,CAAC,mBAAmB,CAAC,CAAC,CAAC,EAAE,gBAAgB,CAAC,CAAC,CAAC,EAAE,kBAAkB,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;IAC9F,+DAA+D;IAC/D,UAAU,EAAE,eAAe,EAAE,CAAC;CAC/B,CAAC"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { RequestInstance } from '../request';
|
|
2
|
+
import { CachedInfinitePage, PaginationConfig, PaginationMode, PaginationPageContext } from './infinite.types';
|
|
3
|
+
/**
|
|
4
|
+
* Tells how the list is paged. Throws for a missing or incomplete pagination, because a list without it
|
|
5
|
+
* would silently fetch the same page forever.
|
|
6
|
+
*/
|
|
7
|
+
export declare const getPaginationMode: (pagination: PaginationConfig) => PaginationMode;
|
|
8
|
+
/**
|
|
9
|
+
* The value the list starts from: what the request already holds, otherwise the first page.
|
|
10
|
+
*/
|
|
11
|
+
export declare const getInitialPageParam: (request: RequestInstance, pagination: PaginationConfig) => unknown;
|
|
12
|
+
/**
|
|
13
|
+
* Creates the request of a single page. Generated keys are left untouched, so a page shares its cache
|
|
14
|
+
* with any other request for the same data.
|
|
15
|
+
*/
|
|
16
|
+
export declare const getPageRequest: <R extends RequestInstance>(request: R, pagination: PaginationConfig, param: unknown) => R;
|
|
17
|
+
/**
|
|
18
|
+
* Items of a single page
|
|
19
|
+
*/
|
|
20
|
+
export declare const getPageItems: (context: PaginationPageContext) => unknown[];
|
|
21
|
+
/**
|
|
22
|
+
* Size of the whole list, when the pagination declares it
|
|
23
|
+
*/
|
|
24
|
+
export declare const getPageTotal: (context: PaginationPageContext) => number | null;
|
|
25
|
+
/**
|
|
26
|
+
* Paging value of the page after the given one
|
|
27
|
+
*/
|
|
28
|
+
export declare const getNextPageParam: (context: PaginationPageContext) => unknown;
|
|
29
|
+
/**
|
|
30
|
+
* Paging value of the page before the given one
|
|
31
|
+
*/
|
|
32
|
+
export declare const getPreviousPageParam: (context: PaginationPageContext) => unknown;
|
|
33
|
+
/**
|
|
34
|
+
* Tells whether the list continues after the given page
|
|
35
|
+
*/
|
|
36
|
+
export declare const getHasNextPage: (context: PaginationPageContext) => boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Tells whether the list continues before the given page
|
|
39
|
+
*/
|
|
40
|
+
export declare const getHasPreviousPage: (context: PaginationPageContext) => boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Rebuilds a list from already stored pages: from the starting page forwards, then backwards, until a page
|
|
43
|
+
* is missing. Nothing has to remember which pages were loaded.
|
|
44
|
+
*/
|
|
45
|
+
export declare const getCachedPages: <T extends {
|
|
46
|
+
data: unknown;
|
|
47
|
+
}>({ request, pagination, read, maxPages, }: {
|
|
48
|
+
request: RequestInstance;
|
|
49
|
+
pagination: PaginationConfig;
|
|
50
|
+
read: (pageRequest: RequestInstance) => T | null | undefined;
|
|
51
|
+
maxPages?: number;
|
|
52
|
+
}) => CachedInfinitePage<T>[];
|
|
53
|
+
//# sourceMappingURL=infinite.utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"infinite.utils.d.ts","sourceRoot":"","sources":["../../src/infinite/infinite.utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAG/C,OAAO,KAAK,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,cAAc,EAAE,qBAAqB,EAAE,MAAM,kBAAkB,CAAC;AAYpH;;;GAGG;AACH,eAAO,MAAM,iBAAiB,GAAI,YAAY,gBAAgB,KAAG,cAchE,CAAC;AAmBF;;GAEG;AACH,eAAO,MAAM,mBAAmB,GAAI,SAAS,eAAe,EAAE,YAAY,gBAAgB,KAAG,OAQ5F,CAAC;AA2CF;;;GAGG;AACH,eAAO,MAAM,cAAc,GAAI,CAAC,SAAS,eAAe,EACtD,SAAS,CAAC,EACV,YAAY,gBAAgB,EAC5B,OAAO,OAAO,KACb,CAeF,CAAC;AAWF;;GAEG;AACH,eAAO,MAAM,YAAY,GAAI,SAAS,qBAAqB,KAAG,OAAO,EAIpE,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,YAAY,GAAI,SAAS,qBAAqB,KAAG,MAAM,GAAG,IAEtE,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,gBAAgB,GAAI,SAAS,qBAAqB,KAAG,OAYjE,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,oBAAoB,GAAI,SAAS,qBAAqB,KAAG,OAiBrE,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,cAAc,GAAI,SAAS,qBAAqB,KAAG,OA8B/D,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,kBAAkB,GAAI,SAAS,qBAAqB,KAAG,OAKnE,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,cAAc,GAAI,CAAC,SAAS;IAAE,IAAI,EAAE,OAAO,CAAA;CAAE,EAAE,0CAKzD;IACD,OAAO,EAAE,eAAe,CAAC;IACzB,UAAU,EAAE,gBAAgB,CAAC;IAC7B,IAAI,EAAE,CAAC,WAAW,EAAE,eAAe,KAAK,CAAC,GAAG,IAAI,GAAG,SAAS,CAAC;IAC7D,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB,KAAG,kBAAkB,CAAC,CAAC,CAAC,EAmDxB,CAAC"}
|
|
@@ -2,6 +2,7 @@ import { AdapterInstance, ProgressType, RequestResponseType, ResponseType } from
|
|
|
2
2
|
import { Dispatcher } from '../dispatcher';
|
|
3
3
|
import { ExtractAdapterType } from '../types';
|
|
4
4
|
import { ProgressEventType, RequestInstance, RequestJSON, RequestSendOptionsType } from './request.types';
|
|
5
|
+
type MappedResponse = ResponseType<any, any, AdapterInstance> | Promise<ResponseType<any, any, AdapterInstance>> | undefined;
|
|
5
6
|
export declare const scopeKey: (key: string, scope: string | null) => string;
|
|
6
7
|
export declare const stringifyKey: (value: unknown) => string;
|
|
7
8
|
export declare const getProgressValue: ({ loaded, total }: ProgressEventType) => number;
|
|
@@ -20,6 +21,17 @@ export declare const getSimpleKey: (request: RequestInstance | RequestJSON<Reque
|
|
|
20
21
|
*/
|
|
21
22
|
export declare const getRequestKey: (request: RequestInstance | RequestJSON<RequestInstance>, useInitialValues?: boolean) => string;
|
|
22
23
|
export declare const getRequestDispatcher: <Request extends RequestInstance>(request: Request, dispatcherType?: "auto" | "fetch" | "submit") => [Dispatcher<ExtractAdapterType<Request>>, isFetchDispatcher: boolean];
|
|
24
|
+
/**
|
|
25
|
+
* Runs the response mapper of the request. Mapper is user code running in the middle of the response handling,
|
|
26
|
+
* so its failure is logged and ends up as a failed response carrying the thrown value as the `error`.
|
|
27
|
+
* Failures are always delivered through a promise, so sync and async mappers fail the same way.
|
|
28
|
+
* @internal
|
|
29
|
+
* @param request request with the optional response mapper
|
|
30
|
+
* @param response response to be mapped
|
|
31
|
+
* @returns mapped response, promise of it for async and failed mappers, `undefined` when there is no mapper
|
|
32
|
+
*/
|
|
33
|
+
export declare const runResponseMapper: <Request extends RequestInstance>(request: Request, response: ResponseType<any, any, AdapterInstance>) => MappedResponse;
|
|
23
34
|
export declare const mapResponseForSend: <Request extends RequestInstance>(request: Request, response: ResponseType<any, any, AdapterInstance>) => Promise<RequestResponseType<Request>>;
|
|
24
35
|
export declare const sendRequest: <Request extends RequestInstance>(request: Request, options?: RequestSendOptionsType<Request>) => Promise<RequestResponseType<Request>>;
|
|
36
|
+
export {};
|
|
25
37
|
//# sourceMappingURL=request.utils.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request.utils.d.ts","sourceRoot":"","sources":["../../src/request/request.utils.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,YAAY,EAAE,mBAAmB,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAEhG,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAE7C,OAAO,KAAK,EAAE,kBAAkB,EAAoB,MAAM,OAAO,CAAC;AAElE,OAAO,KAAK,EAGV,iBAAiB,EACjB,eAAe,EACf,WAAW,EACX,sBAAsB,EACvB,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"request.utils.d.ts","sourceRoot":"","sources":["../../src/request/request.utils.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,YAAY,EAAE,mBAAmB,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAEhG,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAE7C,OAAO,KAAK,EAAE,kBAAkB,EAAoB,MAAM,OAAO,CAAC;AAElE,OAAO,KAAK,EAGV,iBAAiB,EACjB,eAAe,EACf,WAAW,EACX,sBAAsB,EACvB,MAAM,iBAAiB,CAAC;AAEzB,KAAK,cAAc,GACf,YAAY,CAAC,GAAG,EAAE,GAAG,EAAE,eAAe,CAAC,GACvC,OAAO,CAAC,YAAY,CAAC,GAAG,EAAE,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YAAY,CAAC,GAAG,EAAE,GAAG,EAAE,eAAe,CAAC,KAChD,OAAO,CAAC,mBAAmB,CAAC,OAAO,CAAC,CAQtC,CAAC;AAuCF,eAAO,MAAM,WAAW,GAAU,OAAO,SAAS,eAAe,EAC/D,SAAS,OAAO,EAChB,UAAU,sBAAsB,CAAC,OAAO,CAAC,KACxC,OAAO,CAAC,mBAAmB,CAAC,OAAO,CAAC,CAkMtC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hyper-fetch/core",
|
|
3
|
-
"version": "9.
|
|
3
|
+
"version": "9.6.0",
|
|
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/",
|
|
@@ -16,8 +16,7 @@
|
|
|
16
16
|
"lint": "oxlint .",
|
|
17
17
|
"format": "oxfmt --write .",
|
|
18
18
|
"typecheck": "tsgo --noEmit --emitDeclarationOnly false && tsgo --noEmit -p __tests__/tsconfig.json",
|
|
19
|
-
"tests": "pnpm lint-staged"
|
|
20
|
-
"release": "pnpm semantic-release --extends ./release.config.js -t 'hyper-fetch-v${version}'"
|
|
19
|
+
"tests": "pnpm lint-staged"
|
|
21
20
|
},
|
|
22
21
|
"publishConfig": {
|
|
23
22
|
"access": "public",
|
|
@@ -27,6 +27,7 @@ export class Dispatcher<Adapter extends AdapterInstance> {
|
|
|
27
27
|
|
|
28
28
|
private requestCount = new Map<string, number>();
|
|
29
29
|
private runningRequests = new Map<string, RunningRequestValueType[]>();
|
|
30
|
+
private retryTimers = new Map<string, ReturnType<typeof setTimeout>>();
|
|
30
31
|
|
|
31
32
|
private logger: LoggerMethods;
|
|
32
33
|
private client: ClientInstance<{ adapter: Adapter }>;
|
|
@@ -203,6 +204,8 @@ export class Dispatcher<Adapter extends AdapterInstance> {
|
|
|
203
204
|
this.events.emitQueueChanged(newQueue);
|
|
204
205
|
this.events.emitDrained(newQueue);
|
|
205
206
|
|
|
207
|
+
this.removeWaitingRetries(queue);
|
|
208
|
+
|
|
206
209
|
return newQueue;
|
|
207
210
|
};
|
|
208
211
|
|
|
@@ -257,14 +260,41 @@ export class Dispatcher<Adapter extends AdapterInstance> {
|
|
|
257
260
|
*/
|
|
258
261
|
clear = () => {
|
|
259
262
|
const keys = this.getQueuesKeys();
|
|
263
|
+
const queues = keys.map((queryKey) => this.getQueue(queryKey));
|
|
260
264
|
keys.forEach((queryKey) => this.cancelRunningRequests(queryKey));
|
|
261
265
|
|
|
262
266
|
[...this.runningRequests.keys()].forEach((queryKey) => this.setRunningRequests(queryKey, []));
|
|
263
267
|
this.runningRequests.clear();
|
|
264
268
|
this.storage.clear();
|
|
269
|
+
queues.forEach((queue) => this.removeWaitingRetries(queue));
|
|
265
270
|
this.client.triggerPlugins("onDispatcherCleared", { dispatcher: this });
|
|
266
271
|
};
|
|
267
272
|
|
|
273
|
+
/**
|
|
274
|
+
* Drop the scheduled retry of the request
|
|
275
|
+
*/
|
|
276
|
+
private cancelRetry = (requestId: string) => {
|
|
277
|
+
const timer = this.retryTimers.get(requestId);
|
|
278
|
+
if (timer === undefined) {
|
|
279
|
+
return false;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
clearTimeout(timer);
|
|
283
|
+
return this.retryTimers.delete(requestId);
|
|
284
|
+
};
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Clearing does not announce removed items. Requests waiting for a retry have no attempt left to end with,
|
|
288
|
+
* so without this event their listeners would never be notified.
|
|
289
|
+
*/
|
|
290
|
+
private removeWaitingRetries = (queue: ResolvedQueueDataType) => {
|
|
291
|
+
queue.requests.forEach(({ requestId, request, resolved }) => {
|
|
292
|
+
if (this.cancelRetry(requestId)) {
|
|
293
|
+
this.client.requestManager.events.emitRemove({ requestId, request, resolved });
|
|
294
|
+
}
|
|
295
|
+
});
|
|
296
|
+
};
|
|
297
|
+
|
|
268
298
|
// *********************************************************************
|
|
269
299
|
// *********************************************************************
|
|
270
300
|
// Requests
|
|
@@ -506,6 +536,8 @@ export class Dispatcher<Adapter extends AdapterInstance> {
|
|
|
506
536
|
|
|
507
537
|
if (!queueItem) {return;}
|
|
508
538
|
|
|
539
|
+
this.cancelRetry(requestId);
|
|
540
|
+
|
|
509
541
|
queue.requests = queue.requests.filter((req) => req.requestId !== requestId);
|
|
510
542
|
this.storage.set(queryKey, queue);
|
|
511
543
|
|
|
@@ -680,8 +712,23 @@ export class Dispatcher<Adapter extends AdapterInstance> {
|
|
|
680
712
|
// On retry
|
|
681
713
|
if (willRetry) {
|
|
682
714
|
this.logger.debug({ title: "Waiting for retry", type: "system", extra: { response, requestDetails, request } });
|
|
715
|
+
// Flushing the queue during the wait sends the request earlier - only the latest failure schedules the retry
|
|
716
|
+
this.cancelRetry(requestId);
|
|
683
717
|
// Perform retry once request is failed
|
|
684
|
-
setTimeout(() => {
|
|
718
|
+
const retryTimer = setTimeout(() => {
|
|
719
|
+
this.retryTimers.delete(requestId);
|
|
720
|
+
|
|
721
|
+
// Dispatcher methods cancel this timer, so the item was taken directly from the storage and nobody announced it
|
|
722
|
+
if (!this.getRequest(queryKey, requestId)) {
|
|
723
|
+
this.logger.debug({
|
|
724
|
+
title: "Request no longer in queue, skipping retry",
|
|
725
|
+
type: "request",
|
|
726
|
+
extra: { response, requestDetails, request, requestId },
|
|
727
|
+
});
|
|
728
|
+
requestManager.events.emitRemove({ requestId, request, resolved: storageItem.resolved });
|
|
729
|
+
return;
|
|
730
|
+
}
|
|
731
|
+
|
|
685
732
|
this.logger.warning({
|
|
686
733
|
title: "Error response, performing retry",
|
|
687
734
|
type: "request",
|
|
@@ -692,6 +739,7 @@ export class Dispatcher<Adapter extends AdapterInstance> {
|
|
|
692
739
|
retries: storageItem.retries + 1,
|
|
693
740
|
});
|
|
694
741
|
}, retryTime || 0);
|
|
742
|
+
this.retryTimers.set(requestId, retryTimer);
|
|
695
743
|
} else {
|
|
696
744
|
if (request.retry) {
|
|
697
745
|
this.logger.error({
|
package/src/index.ts
CHANGED
|
@@ -7,6 +7,7 @@ export * from "./adapter";
|
|
|
7
7
|
export * from "./sdk";
|
|
8
8
|
export * from "./http-adapter";
|
|
9
9
|
export * from "./request";
|
|
10
|
+
export * from "./infinite";
|
|
10
11
|
export * from "./constants/time.constants";
|
|
11
12
|
export * from "./constants/http.constants";
|
|
12
13
|
export * from "./managers";
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import type { RequestInstance } from "request";
|
|
2
|
+
import type { HydrateDataType } from "types";
|
|
3
|
+
|
|
4
|
+
import type { FetchInfiniteOptions, FetchInfiniteResult, Pagination, PaginationConfig } from "./infinite.types";
|
|
5
|
+
import {
|
|
6
|
+
getHasNextPage,
|
|
7
|
+
getInitialPageParam,
|
|
8
|
+
getNextPageParam,
|
|
9
|
+
getPageItems,
|
|
10
|
+
getPageRequest,
|
|
11
|
+
getPageTotal,
|
|
12
|
+
} from "./infinite.utils";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Fetches the first pages of a list one after another, without touching the cache or the queues.
|
|
16
|
+
* Made for server rendering: pass `dehydrated` to the `Hydrate` component to render them on the client.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* const { dehydrated } = await fetchInfinite(getFeed, {
|
|
21
|
+
* pagination: { cursor: "cursor", next: "nextCursor", items: "data" },
|
|
22
|
+
* pages: 2,
|
|
23
|
+
* });
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export const fetchInfinite = async <R extends RequestInstance, const P extends Pagination<R>>(
|
|
27
|
+
request: R,
|
|
28
|
+
options: FetchInfiniteOptions<P>,
|
|
29
|
+
): Promise<FetchInfiniteResult<R, P>> => {
|
|
30
|
+
const pagination = options.pagination as PaginationConfig;
|
|
31
|
+
const pageCount = Math.max(1, options.pages ?? 1);
|
|
32
|
+
|
|
33
|
+
const result: FetchInfiniteResult<any, any> = {
|
|
34
|
+
items: [],
|
|
35
|
+
pages: [],
|
|
36
|
+
pageParams: [],
|
|
37
|
+
total: null,
|
|
38
|
+
hasNextPage: false,
|
|
39
|
+
error: null,
|
|
40
|
+
responses: [],
|
|
41
|
+
dehydrated: [],
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
let param = getInitialPageParam(request, pagination);
|
|
45
|
+
|
|
46
|
+
for (let index = 0; index < pageCount; index += 1) {
|
|
47
|
+
const pageRequest = getPageRequest(request, pagination, param);
|
|
48
|
+
// Pages depend on each other, so they cannot be requested at the same time
|
|
49
|
+
// eslint-disable-next-line no-await-in-loop
|
|
50
|
+
const response = await (pageRequest as RequestInstance).exec({});
|
|
51
|
+
|
|
52
|
+
result.responses.push(response);
|
|
53
|
+
|
|
54
|
+
if (!response.success) {
|
|
55
|
+
result.error = response.error;
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const context = { request, pagination, data: response.data, param, pages: result.pages };
|
|
60
|
+
|
|
61
|
+
result.pages.push(response.data);
|
|
62
|
+
result.pageParams.push(param);
|
|
63
|
+
(result.items as unknown[]).push(...getPageItems(context));
|
|
64
|
+
result.total = getPageTotal(context) ?? result.total;
|
|
65
|
+
result.hasNextPage = getHasNextPage(context);
|
|
66
|
+
result.dehydrated.push(pageRequest.dehydrate({ response }) as HydrateDataType);
|
|
67
|
+
|
|
68
|
+
if (!result.hasNextPage) {
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
param = getNextPageParam(context);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return result as FetchInfiniteResult<R, P>;
|
|
75
|
+
};
|