@hyper-fetch/core 3.3.3 → 4.0.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/dist/browser/index.cjs.js +1 -1
- package/dist/browser/index.cjs.js.map +4 -4
- package/dist/browser/index.esm.js +1 -1
- package/dist/browser/index.esm.js.map +4 -4
- package/dist/index.cjs.js +1 -1
- package/dist/index.cjs.js.map +4 -4
- package/dist/index.d.ts +330 -180
- package/dist/index.esm.js +1 -1
- package/dist/index.esm.js.map +4 -4
- package/package.json +4 -2
- package/src/adapter/adapter.bindings.ts +124 -35
- package/src/adapter/adapter.browser.ts +29 -25
- package/src/adapter/adapter.server.ts +81 -81
- package/src/adapter/adapter.ts +1 -6
- package/src/adapter/adapter.types.ts +100 -12
- package/src/adapter/adapter.utils.ts +16 -23
- package/src/cache/cache.events.ts +9 -6
- package/src/cache/cache.ts +37 -30
- package/src/cache/cache.types.ts +32 -15
- package/src/cache/cache.utils.ts +7 -9
- package/src/client/client.constants.ts +6 -0
- package/src/client/client.ts +130 -59
- package/src/client/client.types.ts +11 -10
- package/src/client/client.utils.ts +20 -6
- package/src/dispatcher/dispatcher.ts +20 -26
- package/src/dispatcher/dispatcher.types.ts +4 -4
- package/src/dispatcher/dispatcher.utils.ts +2 -11
- package/src/effect/request.effect.ts +8 -5
- package/src/effect/request.effect.types.ts +14 -5
- package/src/index.ts +1 -0
- package/src/managers/logger/logger.manager.ts +8 -8
- package/src/managers/logger/logger.manager.types.ts +5 -8
- package/src/managers/logger/logger.manager.utils.ts +2 -2
- package/src/managers/request/request.manager.events.ts +7 -7
- package/src/managers/request/request.manager.types.ts +0 -1
- package/src/mocker/index.ts +2 -0
- package/src/mocker/mocker.ts +96 -0
- package/src/mocker/mocker.types.ts +26 -0
- package/src/request/request.ts +227 -224
- package/src/request/request.types.ts +75 -35
- package/src/request/request.utils.ts +61 -28
- package/src/types/client.types.ts +4 -0
- package/src/types/fetch.types.ts +16 -25
- package/src/types/http.types.ts +1 -0
- package/src/types/index.ts +1 -0
- package/{tsconfig.node.json → tsconfig.base.json} +3 -3
- package/tsconfig.json +3 -4
- package/dist/server/index.cjs.js +0 -2
- package/dist/server/index.cjs.js.map +0 -7
- package/dist/server/index.esm.js +0 -2
- package/dist/server/index.esm.js.map +0 -7
|
@@ -1,26 +1,100 @@
|
|
|
1
1
|
import { RequestInstance } from "request";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
import { HttpMethodsType, HttpStatusType } from "../types";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Base Adapter
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export type AdapterInstance = AdapterType<any, any, any, any, any>;
|
|
9
|
+
|
|
10
|
+
export type AdapterType<
|
|
11
|
+
AdapterOptions = AdapterOptionsType,
|
|
12
|
+
MethodType = HttpMethodsType,
|
|
13
|
+
StatusType = HttpStatusType,
|
|
14
|
+
Extra extends Record<string, any> = AdapterExtraType,
|
|
15
|
+
QueryParams = QueryParamsType | string,
|
|
16
|
+
> = (
|
|
17
|
+
request: RequestInstance,
|
|
18
|
+
requestId: string,
|
|
19
|
+
// This is never used in the application, we pass this type to have unions extracting possibilities
|
|
20
|
+
DO_NOT_USE?: {
|
|
21
|
+
method?: MethodType;
|
|
22
|
+
options?: AdapterOptions;
|
|
23
|
+
status?: StatusType;
|
|
24
|
+
extra?: Extra;
|
|
25
|
+
queryParams?: QueryParams;
|
|
26
|
+
},
|
|
27
|
+
// [any any any] as a way to avoid circular reference that destroyed request type.
|
|
28
|
+
) => Promise<ResponseReturnType<any, any, any>>;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Extractors
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
export type ExtractAdapterOptionsType<T> = T extends AdapterType<infer O, any, any, any, any> ? O : never;
|
|
35
|
+
export type ExtractAdapterMethodType<T> = T extends AdapterType<any, infer M, any, any, any> ? M : never;
|
|
36
|
+
export type ExtractAdapterStatusType<T> = T extends AdapterType<any, any, infer S, any, any> ? S : never;
|
|
37
|
+
export type ExtractAdapterExtraType<T> = T extends AdapterType<any, any, any, infer A, any> ? A : never;
|
|
38
|
+
export type ExtractAdapterQueryParamsType<T> = T extends AdapterType<any, any, any, any, infer Q> ? Q : never;
|
|
39
|
+
// Special type only for selecting appropriate AdapterType union version (check FirebaseAdapterType).
|
|
40
|
+
export type ExtractUnionAdapter<
|
|
41
|
+
Adapter extends AdapterInstance,
|
|
42
|
+
Values extends {
|
|
43
|
+
method?: any;
|
|
44
|
+
options?: any;
|
|
45
|
+
status?: any;
|
|
46
|
+
extra?: any;
|
|
47
|
+
queryParams?: any;
|
|
48
|
+
},
|
|
49
|
+
> = Extract<
|
|
50
|
+
Adapter,
|
|
51
|
+
AdapterType<Values["options"], Values["method"], Values["status"], Values["extra"], Values["queryParams"]>
|
|
52
|
+
> extends AdapterInstance
|
|
53
|
+
? Extract<
|
|
54
|
+
Adapter,
|
|
55
|
+
AdapterType<Values["options"], Values["method"], Values["status"], Values["extra"], Values["queryParams"]>
|
|
56
|
+
>
|
|
57
|
+
: never;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Options
|
|
61
|
+
*/
|
|
6
62
|
|
|
7
63
|
export type AdapterOptionsType = Partial<XMLHttpRequest>;
|
|
8
64
|
|
|
65
|
+
export type AdapterExtraType = {
|
|
66
|
+
headers: Record<string, string>;
|
|
67
|
+
};
|
|
68
|
+
|
|
9
69
|
export type AdapterPayloadMappingType = (data: unknown) => string | FormData;
|
|
10
70
|
|
|
11
71
|
// Responses
|
|
12
72
|
|
|
13
|
-
export type
|
|
14
|
-
GenericDataType | null
|
|
15
|
-
GenericErrorType | null
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
73
|
+
export type ResponseReturnType<GenericDataType, GenericErrorType, Adapter extends AdapterInstance> = {
|
|
74
|
+
data: GenericDataType | null;
|
|
75
|
+
error: GenericErrorType | null;
|
|
76
|
+
status: ExtractAdapterStatusType<Adapter> | null;
|
|
77
|
+
success: boolean;
|
|
78
|
+
extra: ExtractAdapterExtraType<Adapter> | null;
|
|
79
|
+
};
|
|
80
|
+
export type ResponseReturnSuccessType<GenericDataType, Adapter extends AdapterInstance> = {
|
|
81
|
+
data: GenericDataType;
|
|
82
|
+
error: null;
|
|
83
|
+
status: ExtractAdapterStatusType<Adapter> | null;
|
|
84
|
+
success: boolean;
|
|
85
|
+
extra: ExtractAdapterExtraType<Adapter> | null;
|
|
86
|
+
};
|
|
87
|
+
export type ResponseReturnErrorType<GenericErrorType, Adapter extends AdapterInstance> = {
|
|
88
|
+
data: null;
|
|
89
|
+
error: GenericErrorType;
|
|
90
|
+
status: ExtractAdapterStatusType<Adapter> | null;
|
|
91
|
+
success: boolean;
|
|
92
|
+
extra: ExtractAdapterExtraType<Adapter> | null;
|
|
93
|
+
};
|
|
20
94
|
|
|
21
95
|
// QueryParams
|
|
22
96
|
|
|
23
|
-
export type QueryParamValuesType = number | string | boolean | null | undefined
|
|
97
|
+
export type QueryParamValuesType = number | string | boolean | null | undefined | Record<any, any>;
|
|
24
98
|
export type QueryParamType = QueryParamValuesType | Array<QueryParamValuesType> | Record<string, QueryParamValuesType>;
|
|
25
99
|
export type QueryParamsType = Record<string, QueryParamType>;
|
|
26
100
|
|
|
@@ -60,6 +134,15 @@ export type QueryStringifyOptionsType = {
|
|
|
60
134
|
* Skip keys with empty string
|
|
61
135
|
*/
|
|
62
136
|
skipEmptyString?: boolean;
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Parsing function for date type query param
|
|
140
|
+
*/
|
|
141
|
+
dateParser?: (value: QueryParamType) => string;
|
|
142
|
+
/**
|
|
143
|
+
* Parsing function for object type query param
|
|
144
|
+
*/
|
|
145
|
+
objectParser?: (value: QueryParamType) => string;
|
|
63
146
|
};
|
|
64
147
|
|
|
65
148
|
// Progress
|
|
@@ -77,3 +160,8 @@ export type ProgressType = {
|
|
|
77
160
|
loaded: number;
|
|
78
161
|
startTimestamp: number;
|
|
79
162
|
};
|
|
163
|
+
|
|
164
|
+
export type AdapterBindingsReturnType = {
|
|
165
|
+
fullUrl: string;
|
|
166
|
+
config;
|
|
167
|
+
};
|
|
@@ -13,6 +13,20 @@ export const getErrorMessage = (errorCase?: "timeout" | "abort" | "deleted") =>
|
|
|
13
13
|
return new Error("Unexpected error");
|
|
14
14
|
};
|
|
15
15
|
|
|
16
|
+
export const getResponseHeaders = (headersString: string): Record<string, string> => {
|
|
17
|
+
const arr = headersString.trim().split(/[\r\n]+/);
|
|
18
|
+
|
|
19
|
+
const headers = {};
|
|
20
|
+
arr.forEach((line) => {
|
|
21
|
+
const parts = line.split(": ");
|
|
22
|
+
const header = parts.shift();
|
|
23
|
+
const value = parts.join(": ");
|
|
24
|
+
headers[header] = value;
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
return headers;
|
|
28
|
+
};
|
|
29
|
+
|
|
16
30
|
// Responses
|
|
17
31
|
|
|
18
32
|
export const parseResponse = (response: string | unknown) => {
|
|
@@ -29,31 +43,10 @@ export const parseErrorResponse = <T extends RequestInstance>(response: unknown)
|
|
|
29
43
|
|
|
30
44
|
// Request
|
|
31
45
|
|
|
32
|
-
export const getUploadSize = (payload:
|
|
33
|
-
if (payload instanceof FormData) {
|
|
34
|
-
return Array.from(payload.values())
|
|
35
|
-
.map((value) => (typeof value === "string" ? value.length : value.size))
|
|
36
|
-
.reduce((a, b) => a + b, 0);
|
|
37
|
-
}
|
|
46
|
+
export const getUploadSize = (payload: string) => {
|
|
38
47
|
return payload.length;
|
|
39
48
|
};
|
|
40
49
|
|
|
41
|
-
export const
|
|
42
|
-
return new Promise<Uint8Array>((resolve) => {
|
|
43
|
-
const reader = new FileReader();
|
|
44
|
-
reader.onload = () => {
|
|
45
|
-
resolve(new Uint8Array(reader.result as ArrayBufferLike));
|
|
46
|
-
};
|
|
47
|
-
reader.readAsArrayBuffer(file);
|
|
48
|
-
});
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
export const getStreamPayload = (payload: FormData | string | null) => {
|
|
52
|
-
if (payload instanceof FormData) {
|
|
53
|
-
const parts = Array.from(payload.values()).map((value) =>
|
|
54
|
-
typeof value === "string" ? value : fileToBuffer(value),
|
|
55
|
-
);
|
|
56
|
-
return Promise.all(parts);
|
|
57
|
-
}
|
|
50
|
+
export const getStreamPayload = (payload: string | null) => {
|
|
58
51
|
return payload;
|
|
59
52
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import EventEmitter from "events";
|
|
2
2
|
|
|
3
|
-
import { CacheValueType, getRevalidateEventKey } from "cache";
|
|
4
|
-
import {
|
|
3
|
+
import { CacheValueType, getRevalidateEventKey, getCacheKey } from "cache";
|
|
4
|
+
import { AdapterInstance } from "adapter";
|
|
5
5
|
|
|
6
6
|
export const getCacheEvents = (emitter: EventEmitter) => ({
|
|
7
7
|
/**
|
|
@@ -9,7 +9,10 @@ export const getCacheEvents = (emitter: EventEmitter) => ({
|
|
|
9
9
|
* @param cacheKey
|
|
10
10
|
* @param data
|
|
11
11
|
*/
|
|
12
|
-
emitCacheData: <Response, Error
|
|
12
|
+
emitCacheData: <Response, Error, Adapter extends AdapterInstance>(
|
|
13
|
+
cacheKey: string,
|
|
14
|
+
data: CacheValueType<Response, Error, Adapter>,
|
|
15
|
+
): void => {
|
|
13
16
|
emitter.emit(getCacheKey(cacheKey), data);
|
|
14
17
|
},
|
|
15
18
|
/**
|
|
@@ -18,15 +21,15 @@ export const getCacheEvents = (emitter: EventEmitter) => ({
|
|
|
18
21
|
emitRevalidation: (cacheKey: string): void => {
|
|
19
22
|
emitter.emit(getRevalidateEventKey(cacheKey));
|
|
20
23
|
},
|
|
21
|
-
/**
|
|
24
|
+
/** StatusType
|
|
22
25
|
* Cache data listener
|
|
23
26
|
* @param cacheKey
|
|
24
27
|
* @param callback
|
|
25
28
|
* @returns
|
|
26
29
|
*/
|
|
27
|
-
onData: <Response, Error>(
|
|
30
|
+
onData: <Response, Error, Adapter extends AdapterInstance>(
|
|
28
31
|
cacheKey: string,
|
|
29
|
-
callback: (data: CacheValueType<Response, Error>) => void,
|
|
32
|
+
callback: (data: CacheValueType<Response, Error, Adapter>) => void,
|
|
30
33
|
): VoidFunction => {
|
|
31
34
|
emitter.on(getCacheKey(cacheKey), callback);
|
|
32
35
|
return () => emitter.removeListener(getCacheKey(cacheKey), callback);
|
package/src/cache/cache.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import EventEmitter from "events";
|
|
2
2
|
|
|
3
|
-
import {
|
|
4
|
-
import { ClientInstance } from "client";
|
|
3
|
+
import { AdapterInstance, ResponseReturnType } from "adapter";
|
|
4
|
+
import { ClientInstance, ExtractAdapterTypeFromClient } from "client";
|
|
5
5
|
import { ResponseDetailsType, LoggerType } from "managers";
|
|
6
6
|
import {
|
|
7
7
|
CacheOptionsType,
|
|
@@ -11,7 +11,7 @@ import {
|
|
|
11
11
|
getCacheEvents,
|
|
12
12
|
CacheValueType,
|
|
13
13
|
} from "cache";
|
|
14
|
-
import {
|
|
14
|
+
import { RequestJSON, RequestInstance } from "request";
|
|
15
15
|
|
|
16
16
|
/**
|
|
17
17
|
* Cache class handles the data exchange with the dispatchers.
|
|
@@ -20,7 +20,7 @@ import { RequestDump, RequestInstance } from "request";
|
|
|
20
20
|
* Keys used to save the values are created dynamically on the Request class
|
|
21
21
|
*
|
|
22
22
|
*/
|
|
23
|
-
export class Cache {
|
|
23
|
+
export class Cache<C extends ClientInstance> {
|
|
24
24
|
public emitter = new EventEmitter();
|
|
25
25
|
public events: ReturnType<typeof getCacheEvents>;
|
|
26
26
|
|
|
@@ -30,7 +30,7 @@ export class Cache {
|
|
|
30
30
|
public garbageCollectors = new Map<string, ReturnType<typeof setTimeout>>();
|
|
31
31
|
private logger: LoggerType;
|
|
32
32
|
|
|
33
|
-
constructor(public client:
|
|
33
|
+
constructor(public client: C, public options?: CacheOptionsType) {
|
|
34
34
|
this.storage = this.options?.storage || new Map<string, CacheValueType>();
|
|
35
35
|
this.events = getCacheEvents(this.emitter);
|
|
36
36
|
this.options?.onInitialization?.(this);
|
|
@@ -59,33 +59,36 @@ export class Cache {
|
|
|
59
59
|
* @returns
|
|
60
60
|
*/
|
|
61
61
|
set = <Response, Error>(
|
|
62
|
-
request: RequestInstance |
|
|
63
|
-
response:
|
|
64
|
-
|
|
62
|
+
request: RequestInstance | RequestJSON<RequestInstance>,
|
|
63
|
+
response: ResponseReturnType<Response, Error, ExtractAdapterTypeFromClient<typeof this.client>> &
|
|
64
|
+
ResponseDetailsType,
|
|
65
65
|
): void => {
|
|
66
|
-
this.logger.debug("Processing cache response", { request, response
|
|
66
|
+
this.logger.debug("Processing cache response", { request, response });
|
|
67
67
|
const { cacheKey, cache, cacheTime, garbageCollection } = request;
|
|
68
|
-
const cachedData = this.storage.get<Response, Error
|
|
68
|
+
const cachedData = this.storage.get<Response, Error, ExtractAdapterTypeFromClient<typeof this.client>>(cacheKey);
|
|
69
69
|
|
|
70
70
|
// Once refresh error occurs we don't want to override already valid data in our cache with the thrown error
|
|
71
71
|
// We need to check it against cache and return last valid data we have
|
|
72
|
-
const data = getCacheData(cachedData
|
|
72
|
+
const data = getCacheData(cachedData, response);
|
|
73
73
|
|
|
74
|
-
const newCacheData: CacheValueType = { data,
|
|
74
|
+
const newCacheData: CacheValueType = { ...data, cacheTime, clearKey: this.clearKey, garbageCollection };
|
|
75
75
|
|
|
76
|
-
this.events.emitCacheData<Response, Error
|
|
77
|
-
|
|
76
|
+
this.events.emitCacheData<Response, Error, ExtractAdapterTypeFromClient<typeof this.client>>(
|
|
77
|
+
cacheKey,
|
|
78
|
+
newCacheData,
|
|
79
|
+
);
|
|
80
|
+
this.logger.debug("Emitting cache response", { request, data });
|
|
78
81
|
|
|
79
82
|
// If request should not use cache - just emit response data
|
|
80
83
|
if (!cache) {
|
|
81
|
-
return this.logger.debug("Prevented saving response to cache", { request,
|
|
84
|
+
return this.logger.debug("Prevented saving response to cache", { request, data });
|
|
82
85
|
}
|
|
83
86
|
|
|
84
87
|
// Only success data is valid for the cache store
|
|
85
|
-
if (
|
|
86
|
-
this.logger.debug("Saving response to cache storage", { request,
|
|
87
|
-
this.storage.set<Response, Error
|
|
88
|
-
this.lazyStorage?.set<Response, Error
|
|
88
|
+
if (response.success) {
|
|
89
|
+
this.logger.debug("Saving response to cache storage", { request, data });
|
|
90
|
+
this.storage.set<Response, Error, ExtractAdapterTypeFromClient<typeof this.client>>(cacheKey, newCacheData);
|
|
91
|
+
this.lazyStorage?.set<Response, Error, ExtractAdapterTypeFromClient<typeof this.client>>(cacheKey, newCacheData);
|
|
89
92
|
this.options?.onChange?.(cacheKey, newCacheData);
|
|
90
93
|
this.scheduleGarbageCollector(cacheKey);
|
|
91
94
|
}
|
|
@@ -96,9 +99,11 @@ export class Cache {
|
|
|
96
99
|
* @param cacheKey
|
|
97
100
|
* @returns
|
|
98
101
|
*/
|
|
99
|
-
get = <Response, Error
|
|
100
|
-
|
|
101
|
-
|
|
102
|
+
get = <Response, Error, Adapter extends AdapterInstance>(
|
|
103
|
+
cacheKey: string,
|
|
104
|
+
): CacheValueType<Response, Error, Adapter> | undefined => {
|
|
105
|
+
this.getLazyResource<Response, Error, Adapter>(cacheKey);
|
|
106
|
+
const cachedData = this.storage.get<Response, Error, Adapter>(cacheKey);
|
|
102
107
|
return cachedData;
|
|
103
108
|
};
|
|
104
109
|
|
|
@@ -149,24 +154,26 @@ export class Cache {
|
|
|
149
154
|
* Used to receive data from lazy storage
|
|
150
155
|
* @param cacheKey
|
|
151
156
|
*/
|
|
152
|
-
getLazyResource = async <Response, Error
|
|
153
|
-
|
|
154
|
-
|
|
157
|
+
getLazyResource = async <Response, Error, Adapter extends AdapterInstance>(
|
|
158
|
+
cacheKey: string,
|
|
159
|
+
): Promise<CacheValueType<Response, Error, Adapter> | undefined> => {
|
|
160
|
+
const data = await this.lazyStorage?.get<Response, Error, Adapter>(cacheKey);
|
|
161
|
+
const syncData = this.storage.get<Response, Error, Adapter>(cacheKey);
|
|
155
162
|
|
|
156
163
|
// No data in lazy storage
|
|
157
164
|
const hasLazyData = this.lazyStorage && data;
|
|
158
165
|
if (hasLazyData) {
|
|
159
166
|
const now = +new Date();
|
|
160
|
-
const isNewestData = syncData ? syncData.
|
|
161
|
-
const isStaleData = data.cacheTime <= now - data.
|
|
167
|
+
const isNewestData = syncData ? syncData.timestamp < data.timestamp : true;
|
|
168
|
+
const isStaleData = data.cacheTime <= now - data.timestamp;
|
|
162
169
|
const isValidLazyData = data.clearKey === this.clearKey;
|
|
163
170
|
|
|
164
171
|
if (!isValidLazyData) {
|
|
165
172
|
this.lazyStorage.delete(cacheKey);
|
|
166
173
|
}
|
|
167
174
|
if (isNewestData && !isStaleData && isValidLazyData) {
|
|
168
|
-
this.storage.set<Response, Error>(cacheKey, data);
|
|
169
|
-
this.events.emitCacheData<Response, Error>(cacheKey, data);
|
|
175
|
+
this.storage.set<Response, Error, Adapter>(cacheKey, data);
|
|
176
|
+
this.events.emitCacheData<Response, Error, Adapter>(cacheKey, data);
|
|
170
177
|
return data;
|
|
171
178
|
}
|
|
172
179
|
}
|
|
@@ -204,7 +211,7 @@ export class Cache {
|
|
|
204
211
|
|
|
205
212
|
// Garbage collect
|
|
206
213
|
if (cacheData) {
|
|
207
|
-
const timeLeft = cacheData.garbageCollection + cacheData.
|
|
214
|
+
const timeLeft = cacheData.garbageCollection + cacheData.timestamp - +new Date();
|
|
208
215
|
if (cacheData.garbageCollection !== null && JSON.stringify(cacheData.garbageCollection) === "null") {
|
|
209
216
|
this.logger.info("Cache value is Infinite", { cacheKey });
|
|
210
217
|
} else if (timeLeft >= 0) {
|
package/src/cache/cache.types.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { Cache } from "cache";
|
|
2
|
-
import {
|
|
2
|
+
import { AdapterInstance, ResponseReturnType } from "adapter";
|
|
3
3
|
import { ResponseDetailsType } from "managers";
|
|
4
|
+
import { ClientInstance } from "../client";
|
|
4
5
|
|
|
5
|
-
export type CacheOptionsType = {
|
|
6
|
+
export type CacheOptionsType<C extends ClientInstance = ClientInstance> = {
|
|
6
7
|
/**
|
|
7
8
|
* Assign your custom sync storage
|
|
8
9
|
*/
|
|
@@ -18,11 +19,14 @@ export type CacheOptionsType = {
|
|
|
18
19
|
/**
|
|
19
20
|
* Initialization callback
|
|
20
21
|
*/
|
|
21
|
-
onInitialization?: (cache: Cache) => void;
|
|
22
|
+
onInitialization?: (cache: Cache<C>) => void;
|
|
22
23
|
/**
|
|
23
24
|
* Callback for every change in the storage
|
|
24
25
|
*/
|
|
25
|
-
onChange?: <Response = any, Error = any
|
|
26
|
+
onChange?: <Response = any, Error = any, Adapter extends AdapterInstance = AdapterInstance>(
|
|
27
|
+
key: string,
|
|
28
|
+
data: CacheValueType<Response, Error, Adapter>,
|
|
29
|
+
) => void;
|
|
26
30
|
/**
|
|
27
31
|
* Callback for every delete in the storage
|
|
28
32
|
*/
|
|
@@ -30,25 +34,38 @@ export type CacheOptionsType = {
|
|
|
30
34
|
};
|
|
31
35
|
|
|
32
36
|
// Values
|
|
33
|
-
export type CacheValueType<
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
37
|
+
export type CacheValueType<
|
|
38
|
+
Response = any,
|
|
39
|
+
Error = any,
|
|
40
|
+
Adapter extends AdapterInstance = AdapterInstance,
|
|
41
|
+
> = ResponseReturnType<Response, Error, Adapter> &
|
|
42
|
+
ResponseDetailsType & {
|
|
43
|
+
cacheTime: number;
|
|
44
|
+
clearKey: string;
|
|
45
|
+
garbageCollection: number;
|
|
46
|
+
};
|
|
40
47
|
|
|
41
48
|
// Storage
|
|
42
49
|
export type CacheAsyncStorageType = {
|
|
43
|
-
set: <Response, Error
|
|
44
|
-
|
|
50
|
+
set: <Response, Error, Adapter extends AdapterInstance>(
|
|
51
|
+
key: string,
|
|
52
|
+
data: CacheValueType<Response, Error, Adapter>,
|
|
53
|
+
) => Promise<void>;
|
|
54
|
+
get: <Response, Error, Adapter extends AdapterInstance>(
|
|
55
|
+
key: string,
|
|
56
|
+
) => Promise<CacheValueType<Response, Error, Adapter> | undefined>;
|
|
45
57
|
keys: () => Promise<string[] | IterableIterator<string> | string[]>;
|
|
46
58
|
delete: (key: string) => Promise<void>;
|
|
47
59
|
};
|
|
48
60
|
|
|
49
61
|
export type CacheStorageType = {
|
|
50
|
-
set: <Response, Error
|
|
51
|
-
|
|
62
|
+
set: <Response, Error, Adapter extends AdapterInstance>(
|
|
63
|
+
key: string,
|
|
64
|
+
data: CacheValueType<Response, Error, Adapter>,
|
|
65
|
+
) => void;
|
|
66
|
+
get: <Response, Error, Adapter extends AdapterInstance>(
|
|
67
|
+
key: string,
|
|
68
|
+
) => CacheValueType<Response, Error, Adapter> | undefined;
|
|
52
69
|
keys: () => string[] | IterableIterator<string> | string[];
|
|
53
70
|
delete: (key: string) => void;
|
|
54
71
|
clear: () => void;
|
package/src/cache/cache.utils.ts
CHANGED
|
@@ -1,19 +1,17 @@
|
|
|
1
|
+
import { ResponseDetailsType } from "managers";
|
|
1
2
|
import { RequestInstance } from "request";
|
|
2
|
-
import { isFailedRequest } from "dispatcher";
|
|
3
3
|
import { ExtractAdapterReturnType } from "types";
|
|
4
4
|
|
|
5
5
|
export const getCacheData = <T extends RequestInstance>(
|
|
6
6
|
previousResponse: ExtractAdapterReturnType<T> | undefined,
|
|
7
|
-
response: ExtractAdapterReturnType<T
|
|
8
|
-
): ExtractAdapterReturnType<T> => {
|
|
9
|
-
const
|
|
7
|
+
response: ExtractAdapterReturnType<T> & ResponseDetailsType,
|
|
8
|
+
): ExtractAdapterReturnType<T> & ResponseDetailsType => {
|
|
9
|
+
const { data, success } = response;
|
|
10
10
|
|
|
11
|
-
const previousData =
|
|
12
|
-
const
|
|
13
|
-
const error = response[1];
|
|
14
|
-
const status = response[2];
|
|
11
|
+
const previousData = !success && previousResponse ? previousResponse.data : null;
|
|
12
|
+
const computedData = data || previousData;
|
|
15
13
|
|
|
16
|
-
return
|
|
14
|
+
return { ...response, data: computedData };
|
|
17
15
|
};
|
|
18
16
|
|
|
19
17
|
export const getRevalidateEventKey = (key: string): string => {
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { AdapterExtraType } from "adapter";
|
|
2
|
+
|
|
1
3
|
export const stringifyDefaultOptions = {
|
|
2
4
|
strict: true,
|
|
3
5
|
encode: true,
|
|
@@ -7,3 +9,7 @@ export const stringifyDefaultOptions = {
|
|
|
7
9
|
skipNull: true,
|
|
8
10
|
skipEmptyString: true,
|
|
9
11
|
} as const;
|
|
12
|
+
|
|
13
|
+
export const xhrExtra: AdapterExtraType = {
|
|
14
|
+
headers: {},
|
|
15
|
+
};
|