@hyper-fetch/core 4.0.1 → 5.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/README.md +0 -9
- package/dist/browser/index.cjs.js +2375 -1
- package/dist/browser/index.cjs.js.map +3 -3
- package/dist/browser/index.esm.js +2350 -1
- package/dist/browser/index.esm.js.map +3 -3
- package/dist/index.cjs.js +2534 -1
- package/dist/index.cjs.js.map +4 -4
- package/dist/index.d.ts +66 -43
- package/dist/index.esm.js +2429 -1
- package/dist/index.esm.js.map +4 -4
- package/package.json +1 -1
- package/src/adapter/adapter.browser.ts +8 -2
- package/src/adapter/adapter.constants.ts +5 -0
- package/src/adapter/adapter.server.ts +13 -27
- package/src/adapter/adapter.types.ts +21 -3
- package/src/adapter/adapter.utils.ts +0 -10
- package/src/cache/cache.events.ts +7 -7
- package/src/cache/cache.ts +51 -14
- package/src/cache/cache.types.ts +2 -0
- package/src/cache/cache.utils.ts +2 -2
- package/src/client/client.constants.ts +0 -6
- package/src/client/client.ts +95 -36
- package/src/client/client.types.ts +4 -0
- package/src/mocker/mocker.ts +22 -31
- package/src/mocker/mocker.types.ts +8 -5
package/package.json
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import {
|
|
2
|
+
xhrExtra,
|
|
3
|
+
getAdapterBindings,
|
|
4
|
+
AdapterType,
|
|
5
|
+
getResponseHeaders,
|
|
6
|
+
parseResponse,
|
|
7
|
+
parseErrorResponse,
|
|
8
|
+
} from "adapter";
|
|
3
9
|
import { defaultTimeout } from "./adapter.constants";
|
|
4
10
|
|
|
5
11
|
export const adapter: AdapterType = async (request, requestId) => {
|
|
@@ -1,13 +1,8 @@
|
|
|
1
1
|
import http, { OutgoingHttpHeaders } from "http";
|
|
2
|
-
import stream from "stream";
|
|
3
2
|
import https from "https";
|
|
4
3
|
|
|
5
|
-
import { getAdapterBindings } from "
|
|
6
|
-
import { AdapterType } from "./adapter.types";
|
|
7
|
-
import { defaultTimeout } from "./adapter.constants";
|
|
8
|
-
import { getStreamPayload, getUploadSize, parseErrorResponse, parseResponse } from "./adapter.utils";
|
|
4
|
+
import { xhrExtra, defaultTimeout, AdapterType, getAdapterBindings, parseErrorResponse, parseResponse } from "adapter";
|
|
9
5
|
import { HttpMethodsEnum } from "../constants/http.constants";
|
|
10
|
-
import { xhrExtra } from "client";
|
|
11
6
|
|
|
12
7
|
export const adapter: AdapterType = async (request, requestId) => {
|
|
13
8
|
const {
|
|
@@ -22,7 +17,6 @@ export const adapter: AdapterType = async (request, requestId) => {
|
|
|
22
17
|
onRequestEnd,
|
|
23
18
|
createAbortListener,
|
|
24
19
|
onResponseProgress,
|
|
25
|
-
onRequestProgress,
|
|
26
20
|
onResponseStart,
|
|
27
21
|
onBeforeRequest,
|
|
28
22
|
onRequestStart,
|
|
@@ -45,10 +39,9 @@ export const adapter: AdapterType = async (request, requestId) => {
|
|
|
45
39
|
let unmountListener = () => null;
|
|
46
40
|
onBeforeRequest();
|
|
47
41
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
const payloadChunks = await getStreamPayload(payload);
|
|
42
|
+
if (payload) {
|
|
43
|
+
options.headers["Content-Length"] = Buffer.byteLength(JSON.stringify(payload));
|
|
44
|
+
}
|
|
52
45
|
|
|
53
46
|
return makeRequest((resolve) => {
|
|
54
47
|
const httpRequest = httpClient.request(options, (response) => {
|
|
@@ -59,8 +52,13 @@ export const adapter: AdapterType = async (request, requestId) => {
|
|
|
59
52
|
const totalDownloadBytes = Number(response.headers["content-length"]);
|
|
60
53
|
let downloadedBytes = 0;
|
|
61
54
|
|
|
55
|
+
onRequestStart();
|
|
56
|
+
|
|
62
57
|
response.on("data", (chunk) => {
|
|
63
|
-
if (!chunks)
|
|
58
|
+
if (!chunks) {
|
|
59
|
+
onRequestEnd();
|
|
60
|
+
onResponseStart();
|
|
61
|
+
}
|
|
64
62
|
downloadedBytes += chunk.length;
|
|
65
63
|
chunks += chunk;
|
|
66
64
|
onResponseProgress({ total: totalDownloadBytes, loaded: downloadedBytes });
|
|
@@ -86,21 +84,9 @@ export const adapter: AdapterType = async (request, requestId) => {
|
|
|
86
84
|
|
|
87
85
|
httpRequest.on("timeout", () => onTimeoutError(0, xhrExtra, resolve));
|
|
88
86
|
httpRequest.on("error", (error) => onError(error, 0, xhrExtra, resolve));
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
const readableStream = stream.Readable.from(payloadChunks, { objectMode: false })
|
|
92
|
-
.on("data", (chunk) => {
|
|
93
|
-
if (!uploadedBytes) onRequestStart();
|
|
94
|
-
uploadedBytes += chunk.length;
|
|
95
|
-
onRequestProgress({ total: totalUploadBytes, loaded: uploadedBytes });
|
|
96
|
-
})
|
|
97
|
-
.on("end", () => {
|
|
98
|
-
onRequestEnd();
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
readableStream.pipe(httpRequest);
|
|
102
|
-
} else {
|
|
103
|
-
httpRequest.end();
|
|
87
|
+
if (payload) {
|
|
88
|
+
httpRequest.write(payload);
|
|
104
89
|
}
|
|
90
|
+
httpRequest.end();
|
|
105
91
|
});
|
|
106
92
|
};
|
|
@@ -5,7 +5,7 @@ import { HttpMethodsType, HttpStatusType } from "../types";
|
|
|
5
5
|
* Base Adapter
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
export type AdapterInstance = AdapterType<any, any, any, any, any>;
|
|
8
|
+
export type AdapterInstance = AdapterType<any, any, any, any, any, any>;
|
|
9
9
|
|
|
10
10
|
export type AdapterType<
|
|
11
11
|
AdapterOptions = AdapterOptionsType,
|
|
@@ -13,6 +13,7 @@ export type AdapterType<
|
|
|
13
13
|
StatusType = HttpStatusType,
|
|
14
14
|
Extra extends Record<string, any> = AdapterExtraType,
|
|
15
15
|
QueryParams = QueryParamsType | string,
|
|
16
|
+
EndpointType = string,
|
|
16
17
|
> = (
|
|
17
18
|
request: RequestInstance,
|
|
18
19
|
requestId: string,
|
|
@@ -23,6 +24,7 @@ export type AdapterType<
|
|
|
23
24
|
status?: StatusType;
|
|
24
25
|
extra?: Extra;
|
|
25
26
|
queryParams?: QueryParams;
|
|
27
|
+
endpointType?: EndpointType;
|
|
26
28
|
},
|
|
27
29
|
// [any any any] as a way to avoid circular reference that destroyed request type.
|
|
28
30
|
) => Promise<ResponseReturnType<any, any, any>>;
|
|
@@ -36,6 +38,7 @@ export type ExtractAdapterMethodType<T> = T extends AdapterType<any, infer M, an
|
|
|
36
38
|
export type ExtractAdapterStatusType<T> = T extends AdapterType<any, any, infer S, any, any> ? S : never;
|
|
37
39
|
export type ExtractAdapterExtraType<T> = T extends AdapterType<any, any, any, infer A, any> ? A : never;
|
|
38
40
|
export type ExtractAdapterQueryParamsType<T> = T extends AdapterType<any, any, any, any, infer Q> ? Q : never;
|
|
41
|
+
export type ExtractAdapterEndpointType<T> = T extends AdapterType<any, any, any, any, any, infer E> ? E : never;
|
|
39
42
|
// Special type only for selecting appropriate AdapterType union version (check FirebaseAdapterType).
|
|
40
43
|
export type ExtractUnionAdapter<
|
|
41
44
|
Adapter extends AdapterInstance,
|
|
@@ -45,14 +48,29 @@ export type ExtractUnionAdapter<
|
|
|
45
48
|
status?: any;
|
|
46
49
|
extra?: any;
|
|
47
50
|
queryParams?: any;
|
|
51
|
+
endpointType?: any;
|
|
48
52
|
},
|
|
49
53
|
> = Extract<
|
|
50
54
|
Adapter,
|
|
51
|
-
AdapterType<
|
|
55
|
+
AdapterType<
|
|
56
|
+
Values["options"],
|
|
57
|
+
Values["method"],
|
|
58
|
+
Values["status"],
|
|
59
|
+
Values["extra"],
|
|
60
|
+
Values["queryParams"],
|
|
61
|
+
Values["endpointType"]
|
|
62
|
+
>
|
|
52
63
|
> extends AdapterInstance
|
|
53
64
|
? Extract<
|
|
54
65
|
Adapter,
|
|
55
|
-
AdapterType<
|
|
66
|
+
AdapterType<
|
|
67
|
+
Values["options"],
|
|
68
|
+
Values["method"],
|
|
69
|
+
Values["status"],
|
|
70
|
+
Values["extra"],
|
|
71
|
+
Values["queryParams"],
|
|
72
|
+
Values["endpointType"]
|
|
73
|
+
>
|
|
56
74
|
>
|
|
57
75
|
: never;
|
|
58
76
|
|
|
@@ -40,13 +40,3 @@ export const parseResponse = (response: string | unknown) => {
|
|
|
40
40
|
export const parseErrorResponse = <T extends RequestInstance>(response: unknown): ExtractErrorType<T> => {
|
|
41
41
|
return response ? parseResponse(response) : getErrorMessage();
|
|
42
42
|
};
|
|
43
|
-
|
|
44
|
-
// Request
|
|
45
|
-
|
|
46
|
-
export const getUploadSize = (payload: string) => {
|
|
47
|
-
return payload.length;
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
export const getStreamPayload = (payload: string | null) => {
|
|
51
|
-
return payload;
|
|
52
|
-
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import EventEmitter from "events";
|
|
2
2
|
|
|
3
|
-
import { CacheValueType,
|
|
3
|
+
import { CacheValueType, getInvalidateEventKey, getCacheKey } from "cache";
|
|
4
4
|
import { AdapterInstance } from "adapter";
|
|
5
5
|
|
|
6
6
|
export const getCacheEvents = (emitter: EventEmitter) => ({
|
|
@@ -16,10 +16,10 @@ export const getCacheEvents = (emitter: EventEmitter) => ({
|
|
|
16
16
|
emitter.emit(getCacheKey(cacheKey), data);
|
|
17
17
|
},
|
|
18
18
|
/**
|
|
19
|
-
*
|
|
19
|
+
* Invalidate cache values event
|
|
20
20
|
*/
|
|
21
|
-
|
|
22
|
-
emitter.emit(
|
|
21
|
+
emitInvalidation: (cacheKey: string): void => {
|
|
22
|
+
emitter.emit(getInvalidateEventKey(cacheKey));
|
|
23
23
|
},
|
|
24
24
|
/** StatusType
|
|
25
25
|
* Cache data listener
|
|
@@ -40,8 +40,8 @@ export const getCacheEvents = (emitter: EventEmitter) => ({
|
|
|
40
40
|
* @param callback
|
|
41
41
|
* @returns
|
|
42
42
|
*/
|
|
43
|
-
|
|
44
|
-
emitter.on(
|
|
45
|
-
return () => emitter.removeListener(
|
|
43
|
+
onInvalidate: (cacheKey: string, callback: () => void): VoidFunction => {
|
|
44
|
+
emitter.on(getInvalidateEventKey(cacheKey), callback);
|
|
45
|
+
return () => emitter.removeListener(getInvalidateEventKey(cacheKey), callback);
|
|
46
46
|
},
|
|
47
47
|
});
|
package/src/cache/cache.ts
CHANGED
|
@@ -10,8 +10,10 @@ import {
|
|
|
10
10
|
getCacheData,
|
|
11
11
|
getCacheEvents,
|
|
12
12
|
CacheValueType,
|
|
13
|
+
CacheMethodType,
|
|
13
14
|
} from "cache";
|
|
14
15
|
import { RequestJSON, RequestInstance } from "request";
|
|
16
|
+
import { ExtractAdapterType, ExtractErrorType, ExtractResponseType } from "types";
|
|
15
17
|
|
|
16
18
|
/**
|
|
17
19
|
* Cache class handles the data exchange with the dispatchers.
|
|
@@ -55,25 +57,31 @@ export class Cache<C extends ClientInstance> {
|
|
|
55
57
|
* Set the cache data to the storage
|
|
56
58
|
* @param request
|
|
57
59
|
* @param response
|
|
58
|
-
* @param details
|
|
59
60
|
* @returns
|
|
60
61
|
*/
|
|
61
|
-
set = <
|
|
62
|
-
request: RequestInstance | RequestJSON<
|
|
63
|
-
response:
|
|
64
|
-
|
|
62
|
+
set = <Request extends RequestInstance>(
|
|
63
|
+
request: RequestInstance | RequestJSON<any>,
|
|
64
|
+
response: CacheMethodType<
|
|
65
|
+
ResponseReturnType<ExtractResponseType<Request>, ExtractErrorType<Request>, ExtractAdapterType<Request>> &
|
|
66
|
+
ResponseDetailsType
|
|
67
|
+
>,
|
|
65
68
|
): void => {
|
|
66
69
|
this.logger.debug("Processing cache response", { request, response });
|
|
67
70
|
const { cacheKey, cache, cacheTime, garbageCollection } = request;
|
|
68
|
-
const cachedData = this.storage.get<
|
|
71
|
+
const cachedData = this.storage.get<
|
|
72
|
+
ExtractResponseType<Request>,
|
|
73
|
+
ExtractErrorType<Request>,
|
|
74
|
+
ExtractAdapterType<Request>
|
|
75
|
+
>(cacheKey);
|
|
69
76
|
|
|
70
77
|
// Once refresh error occurs we don't want to override already valid data in our cache with the thrown error
|
|
71
78
|
// We need to check it against cache and return last valid data we have
|
|
72
|
-
const
|
|
79
|
+
const processedResponse = typeof response === "function" ? response(cachedData) : response;
|
|
80
|
+
const data = getCacheData(cachedData, processedResponse);
|
|
73
81
|
|
|
74
82
|
const newCacheData: CacheValueType = { ...data, cacheTime, clearKey: this.clearKey, garbageCollection };
|
|
75
83
|
|
|
76
|
-
this.events.emitCacheData<
|
|
84
|
+
this.events.emitCacheData<ExtractResponseType<Request>, ExtractErrorType<Request>, ExtractAdapterType<Request>>(
|
|
77
85
|
cacheKey,
|
|
78
86
|
newCacheData,
|
|
79
87
|
);
|
|
@@ -85,7 +93,7 @@ export class Cache<C extends ClientInstance> {
|
|
|
85
93
|
}
|
|
86
94
|
|
|
87
95
|
// Only success data is valid for the cache store
|
|
88
|
-
if (
|
|
96
|
+
if (processedResponse.success) {
|
|
89
97
|
this.logger.debug("Saving response to cache storage", { request, data });
|
|
90
98
|
this.storage.set<Response, Error, ExtractAdapterTypeFromClient<typeof this.client>>(cacheKey, newCacheData);
|
|
91
99
|
this.lazyStorage?.set<Response, Error, ExtractAdapterTypeFromClient<typeof this.client>>(cacheKey, newCacheData);
|
|
@@ -94,6 +102,35 @@ export class Cache<C extends ClientInstance> {
|
|
|
94
102
|
}
|
|
95
103
|
};
|
|
96
104
|
|
|
105
|
+
/**
|
|
106
|
+
* Update the cache data with partial response data
|
|
107
|
+
* @param request
|
|
108
|
+
* @param partialResponse
|
|
109
|
+
* @returns
|
|
110
|
+
*/
|
|
111
|
+
update = <Request extends RequestInstance>(
|
|
112
|
+
request: RequestInstance | RequestJSON<RequestInstance>,
|
|
113
|
+
partialResponse: CacheMethodType<
|
|
114
|
+
Partial<
|
|
115
|
+
ResponseReturnType<ExtractResponseType<Request>, ExtractErrorType<Request>, ExtractAdapterType<Request>> &
|
|
116
|
+
ResponseDetailsType
|
|
117
|
+
>
|
|
118
|
+
>,
|
|
119
|
+
): void => {
|
|
120
|
+
this.logger.debug("Processing cache update", { request, partialResponse });
|
|
121
|
+
const { cacheKey } = request;
|
|
122
|
+
const cachedData = this.storage.get<
|
|
123
|
+
ExtractResponseType<Request>,
|
|
124
|
+
ExtractErrorType<Request>,
|
|
125
|
+
ExtractAdapterType<Request>
|
|
126
|
+
>(cacheKey);
|
|
127
|
+
|
|
128
|
+
const processedResponse = typeof partialResponse === "function" ? partialResponse(cachedData) : partialResponse;
|
|
129
|
+
if (cachedData) {
|
|
130
|
+
this.set(request, { ...cachedData, ...processedResponse });
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
|
|
97
134
|
/**
|
|
98
135
|
* Get particular record from storage by cacheKey. It will trigger lazyStorage to emit lazy load event for reading it's data.
|
|
99
136
|
* @param cacheKey
|
|
@@ -118,7 +155,7 @@ export class Cache<C extends ClientInstance> {
|
|
|
118
155
|
};
|
|
119
156
|
|
|
120
157
|
/**
|
|
121
|
-
* Delete record from storages and trigger
|
|
158
|
+
* Delete record from storages and trigger invalidation event
|
|
122
159
|
* @param cacheKey
|
|
123
160
|
*/
|
|
124
161
|
delete = (cacheKey: string): void => {
|
|
@@ -129,21 +166,21 @@ export class Cache<C extends ClientInstance> {
|
|
|
129
166
|
};
|
|
130
167
|
|
|
131
168
|
/**
|
|
132
|
-
*
|
|
169
|
+
* Invalidate cache by cacheKey or partial matching with RegExp
|
|
133
170
|
* @param cacheKey
|
|
134
171
|
*/
|
|
135
|
-
|
|
172
|
+
invalidate = async (cacheKey: string | RegExp) => {
|
|
136
173
|
this.logger.debug("Revalidating cache element", { cacheKey });
|
|
137
174
|
const keys = await this.getLazyKeys();
|
|
138
175
|
|
|
139
176
|
if (typeof cacheKey === "string") {
|
|
140
|
-
this.events.
|
|
177
|
+
this.events.emitInvalidation(cacheKey);
|
|
141
178
|
this.delete(cacheKey);
|
|
142
179
|
} else {
|
|
143
180
|
// eslint-disable-next-line no-restricted-syntax
|
|
144
181
|
for (const entityKey of keys) {
|
|
145
182
|
if (cacheKey.test(entityKey)) {
|
|
146
|
-
this.events.
|
|
183
|
+
this.events.emitInvalidation(entityKey);
|
|
147
184
|
this.delete(entityKey);
|
|
148
185
|
}
|
|
149
186
|
}
|
package/src/cache/cache.types.ts
CHANGED
package/src/cache/cache.utils.ts
CHANGED
|
@@ -14,8 +14,8 @@ export const getCacheData = <T extends RequestInstance>(
|
|
|
14
14
|
return { ...response, data: computedData };
|
|
15
15
|
};
|
|
16
16
|
|
|
17
|
-
export const
|
|
18
|
-
return `${key}
|
|
17
|
+
export const getInvalidateEventKey = (key: string): string => {
|
|
18
|
+
return `${key}_invalidate`;
|
|
19
19
|
};
|
|
20
20
|
|
|
21
21
|
export const getCacheKey = (key: string): string => {
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { AdapterExtraType } from "adapter";
|
|
2
|
-
|
|
3
1
|
export const stringifyDefaultOptions = {
|
|
4
2
|
strict: true,
|
|
5
3
|
encode: true,
|
|
@@ -9,7 +7,3 @@ export const stringifyDefaultOptions = {
|
|
|
9
7
|
skipNull: true,
|
|
10
8
|
skipEmptyString: true,
|
|
11
9
|
} as const;
|
|
12
|
-
|
|
13
|
-
export const xhrExtra: AdapterExtraType = {
|
|
14
|
-
headers: {},
|
|
15
|
-
};
|
package/src/client/client.ts
CHANGED
|
@@ -11,12 +11,14 @@ import {
|
|
|
11
11
|
HeaderMappingType,
|
|
12
12
|
QueryStringifyOptionsType,
|
|
13
13
|
ResponseReturnType,
|
|
14
|
+
xhrExtra,
|
|
15
|
+
ExtractAdapterEndpointType,
|
|
14
16
|
} from "adapter";
|
|
15
17
|
import {
|
|
16
|
-
xhrExtra,
|
|
17
18
|
ClientErrorType,
|
|
18
19
|
ClientInstance,
|
|
19
20
|
ClientOptionsType,
|
|
21
|
+
DefaultEndpointMapper,
|
|
20
22
|
getAdapterHeaders,
|
|
21
23
|
getAdapterPayload,
|
|
22
24
|
RequestInterceptorType,
|
|
@@ -31,13 +33,18 @@ import { getRequestKey, getSimpleKey, Request, RequestInstance, RequestOptionsTy
|
|
|
31
33
|
import { AppManager, LoggerManager, RequestManager, SeverityType } from "managers";
|
|
32
34
|
import { interceptRequest, interceptResponse } from "./client.utils";
|
|
33
35
|
import { HttpMethodsEnum } from "../constants/http.constants";
|
|
36
|
+
import { ExtractAdapterType, NegativeTypes } from "types";
|
|
34
37
|
|
|
35
38
|
/**
|
|
36
39
|
* **Client** is a class that allows you to configure the connection with the server and then use it to create
|
|
37
40
|
* requests which, when called using the appropriate method, will cause the server to be queried for the endpoint and
|
|
38
41
|
* method specified in the request.
|
|
39
42
|
*/
|
|
40
|
-
export class Client<
|
|
43
|
+
export class Client<
|
|
44
|
+
GlobalErrorType extends ClientErrorType = Error,
|
|
45
|
+
Adapter extends AdapterInstance = AdapterType,
|
|
46
|
+
EndpointMapper extends DefaultEndpointMapper = DefaultEndpointMapper,
|
|
47
|
+
> {
|
|
41
48
|
readonly url: string;
|
|
42
49
|
public debug: boolean;
|
|
43
50
|
|
|
@@ -91,11 +98,16 @@ export class Client<GlobalErrorType extends ClientErrorType = Error, Adapter ext
|
|
|
91
98
|
* Method to get request data and transform them to the required format. It handles FormData and JSON by default.
|
|
92
99
|
*/
|
|
93
100
|
payloadMapper: AdapterPayloadMappingType = getAdapterPayload;
|
|
101
|
+
/**
|
|
102
|
+
* Method to get request data and transform them to the required format. It handles FormData and JSON by default.
|
|
103
|
+
*/
|
|
104
|
+
// eslint-disable-next-line class-methods-use-this
|
|
105
|
+
endpointMapper: EndpointMapper = ((endpoint) => endpoint) as any;
|
|
94
106
|
|
|
95
107
|
// Logger
|
|
96
108
|
logger = this.loggerManager.init("Client");
|
|
97
109
|
|
|
98
|
-
constructor(public options: ClientOptionsType<Client<GlobalErrorType, Adapter>>) {
|
|
110
|
+
constructor(public options: ClientOptionsType<Client<GlobalErrorType, Adapter, EndpointMapper>>) {
|
|
99
111
|
const { url, adapter, appManager, cache, fetchDispatcher, submitDispatcher } = this.options;
|
|
100
112
|
this.url = url;
|
|
101
113
|
this.adapter = (adapter || defaultAdapter) as Adapter;
|
|
@@ -114,14 +126,14 @@ export class Client<GlobalErrorType extends ClientErrorType = Error, Adapter ext
|
|
|
114
126
|
callback: (
|
|
115
127
|
request: RequestInstance,
|
|
116
128
|
) => Partial<RequestOptionsType<string, ExtractAdapterOptionsType<Adapter>, ExtractAdapterMethodType<Adapter>>>,
|
|
117
|
-
): Client<GlobalErrorType, Adapter> => {
|
|
129
|
+
): Client<GlobalErrorType, Adapter, EndpointMapper> => {
|
|
118
130
|
this.requestDefaultOptions = callback;
|
|
119
131
|
return this;
|
|
120
132
|
};
|
|
121
133
|
|
|
122
134
|
setAdapterDefaultOptions = (
|
|
123
135
|
callback: (request: RequestInstance) => ExtractAdapterOptionsType<Adapter>,
|
|
124
|
-
): Client<GlobalErrorType, Adapter> => {
|
|
136
|
+
): Client<GlobalErrorType, Adapter, EndpointMapper> => {
|
|
125
137
|
this.adapterDefaultOptions = callback;
|
|
126
138
|
return this;
|
|
127
139
|
};
|
|
@@ -129,7 +141,7 @@ export class Client<GlobalErrorType extends ClientErrorType = Error, Adapter ext
|
|
|
129
141
|
/**
|
|
130
142
|
* This method enables the logger usage and display the logs in console
|
|
131
143
|
*/
|
|
132
|
-
setDebug = (debug: boolean): Client<GlobalErrorType, Adapter> => {
|
|
144
|
+
setDebug = (debug: boolean): Client<GlobalErrorType, Adapter, EndpointMapper> => {
|
|
133
145
|
this.debug = debug;
|
|
134
146
|
return this;
|
|
135
147
|
};
|
|
@@ -137,7 +149,7 @@ export class Client<GlobalErrorType extends ClientErrorType = Error, Adapter ext
|
|
|
137
149
|
/**
|
|
138
150
|
* Set the logger severity of the messages displayed to the console
|
|
139
151
|
*/
|
|
140
|
-
setLoggerSeverity = (severity: SeverityType): Client<GlobalErrorType, Adapter> => {
|
|
152
|
+
setLoggerSeverity = (severity: SeverityType): Client<GlobalErrorType, Adapter, EndpointMapper> => {
|
|
141
153
|
this.loggerManager.setSeverity(severity);
|
|
142
154
|
return this;
|
|
143
155
|
};
|
|
@@ -145,7 +157,9 @@ export class Client<GlobalErrorType extends ClientErrorType = Error, Adapter ext
|
|
|
145
157
|
/**
|
|
146
158
|
* Set the new logger instance to the Client
|
|
147
159
|
*/
|
|
148
|
-
setLogger = (
|
|
160
|
+
setLogger = (
|
|
161
|
+
callback: (Client: ClientInstance) => LoggerManager,
|
|
162
|
+
): Client<GlobalErrorType, Adapter, EndpointMapper> => {
|
|
149
163
|
this.loggerManager = callback(this);
|
|
150
164
|
return this;
|
|
151
165
|
};
|
|
@@ -153,7 +167,9 @@ export class Client<GlobalErrorType extends ClientErrorType = Error, Adapter ext
|
|
|
153
167
|
/**
|
|
154
168
|
* Set config for the query params stringify method, we can set here, among others, arrayFormat, skipNull, encode, skipEmptyString and more
|
|
155
169
|
*/
|
|
156
|
-
setQueryParamsConfig = (
|
|
170
|
+
setQueryParamsConfig = (
|
|
171
|
+
queryParamsConfig: QueryStringifyOptionsType,
|
|
172
|
+
): Client<GlobalErrorType, Adapter, EndpointMapper> => {
|
|
157
173
|
this.queryParamsConfig = queryParamsConfig;
|
|
158
174
|
return this;
|
|
159
175
|
};
|
|
@@ -162,7 +178,7 @@ export class Client<GlobalErrorType extends ClientErrorType = Error, Adapter ext
|
|
|
162
178
|
* Set the custom query params stringify method to the Client
|
|
163
179
|
* @param stringifyFn Custom callback handling query params stringify
|
|
164
180
|
*/
|
|
165
|
-
setStringifyQueryParams = (stringifyFn: StringifyCallbackType): Client<GlobalErrorType, Adapter> => {
|
|
181
|
+
setStringifyQueryParams = (stringifyFn: StringifyCallbackType): Client<GlobalErrorType, Adapter, EndpointMapper> => {
|
|
166
182
|
this.stringifyQueryParams = stringifyFn;
|
|
167
183
|
return this;
|
|
168
184
|
};
|
|
@@ -170,7 +186,7 @@ export class Client<GlobalErrorType extends ClientErrorType = Error, Adapter ext
|
|
|
170
186
|
/**
|
|
171
187
|
* Set the custom header mapping function
|
|
172
188
|
*/
|
|
173
|
-
setHeaderMapper = (headerMapper: HeaderMappingType): Client<GlobalErrorType, Adapter> => {
|
|
189
|
+
setHeaderMapper = (headerMapper: HeaderMappingType): Client<GlobalErrorType, Adapter, EndpointMapper> => {
|
|
174
190
|
this.headerMapper = headerMapper;
|
|
175
191
|
return this;
|
|
176
192
|
};
|
|
@@ -178,19 +194,39 @@ export class Client<GlobalErrorType extends ClientErrorType = Error, Adapter ext
|
|
|
178
194
|
/**
|
|
179
195
|
* Set the request payload mapping function which get triggered before request get send
|
|
180
196
|
*/
|
|
181
|
-
setPayloadMapper = (payloadMapper: AdapterPayloadMappingType): Client<GlobalErrorType, Adapter> => {
|
|
197
|
+
setPayloadMapper = (payloadMapper: AdapterPayloadMappingType): Client<GlobalErrorType, Adapter, EndpointMapper> => {
|
|
182
198
|
this.payloadMapper = payloadMapper;
|
|
183
199
|
return this;
|
|
184
200
|
};
|
|
185
201
|
|
|
202
|
+
/**
|
|
203
|
+
* Set the request payload mapping function which get triggered before request get send
|
|
204
|
+
*/
|
|
205
|
+
setEndpointMapper = <NewEndpointMapper extends DefaultEndpointMapper>(endpointMapper: NewEndpointMapper) => {
|
|
206
|
+
this.endpointMapper = endpointMapper as any;
|
|
207
|
+
return this as unknown as Client<GlobalErrorType, Adapter, NewEndpointMapper>;
|
|
208
|
+
};
|
|
209
|
+
|
|
186
210
|
/**
|
|
187
211
|
* Set custom http adapter to handle graphql, rest, firebase or others
|
|
188
212
|
*/
|
|
189
|
-
setAdapter = <NewAdapter extends AdapterInstance>(
|
|
190
|
-
callback: (
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
213
|
+
setAdapter = <NewAdapter extends AdapterInstance, Returns extends AdapterInstance | ClientInstance>(
|
|
214
|
+
callback: (
|
|
215
|
+
client: this,
|
|
216
|
+
) => Returns extends AdapterInstance ? NewAdapter : Client<GlobalErrorType, NewAdapter, EndpointMapper>,
|
|
217
|
+
): Client<
|
|
218
|
+
GlobalErrorType,
|
|
219
|
+
Returns extends AdapterInstance ? NewAdapter : ExtractAdapterType<NewAdapter>,
|
|
220
|
+
EndpointMapper
|
|
221
|
+
> => {
|
|
222
|
+
const value = callback(this) as unknown as Adapter;
|
|
223
|
+
|
|
224
|
+
if (value instanceof Client) {
|
|
225
|
+
return value as any;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
this.adapter = value;
|
|
229
|
+
return this as any;
|
|
194
230
|
};
|
|
195
231
|
|
|
196
232
|
/**
|
|
@@ -212,7 +248,7 @@ export class Client<GlobalErrorType extends ClientErrorType = Error, Adapter ext
|
|
|
212
248
|
/**
|
|
213
249
|
* Method of manipulating requests before sending the request. We can for example add custom header with token to the request which request had the auth set to true.
|
|
214
250
|
*/
|
|
215
|
-
onAuth = (callback: RequestInterceptorType): Client<GlobalErrorType, Adapter> => {
|
|
251
|
+
onAuth = (callback: RequestInterceptorType): Client<GlobalErrorType, Adapter, EndpointMapper> => {
|
|
216
252
|
this.__onAuthCallbacks.push(callback);
|
|
217
253
|
return this;
|
|
218
254
|
};
|
|
@@ -222,7 +258,7 @@ export class Client<GlobalErrorType extends ClientErrorType = Error, Adapter ext
|
|
|
222
258
|
*/
|
|
223
259
|
onError = <ErrorType = null>(
|
|
224
260
|
callback: ResponseInterceptorType<any, ErrorType | GlobalErrorType, Adapter>,
|
|
225
|
-
): Client<GlobalErrorType, Adapter> => {
|
|
261
|
+
): Client<GlobalErrorType, Adapter, EndpointMapper> => {
|
|
226
262
|
this.__onErrorCallbacks.push(callback);
|
|
227
263
|
return this;
|
|
228
264
|
};
|
|
@@ -232,7 +268,7 @@ export class Client<GlobalErrorType extends ClientErrorType = Error, Adapter ext
|
|
|
232
268
|
*/
|
|
233
269
|
onSuccess = <ErrorType = null>(
|
|
234
270
|
callback: ResponseInterceptorType<any, ErrorType | GlobalErrorType, Adapter>,
|
|
235
|
-
): Client<GlobalErrorType, Adapter> => {
|
|
271
|
+
): Client<GlobalErrorType, Adapter, EndpointMapper> => {
|
|
236
272
|
this.__onSuccessCallbacks.push(callback);
|
|
237
273
|
return this;
|
|
238
274
|
};
|
|
@@ -240,7 +276,7 @@ export class Client<GlobalErrorType extends ClientErrorType = Error, Adapter ext
|
|
|
240
276
|
/**
|
|
241
277
|
* Method of manipulating requests before sending the request.
|
|
242
278
|
*/
|
|
243
|
-
onRequest = (callback: RequestInterceptorType): Client<GlobalErrorType, Adapter> => {
|
|
279
|
+
onRequest = (callback: RequestInterceptorType): Client<GlobalErrorType, Adapter, EndpointMapper> => {
|
|
244
280
|
this.__onRequestCallbacks.push(callback);
|
|
245
281
|
return this;
|
|
246
282
|
};
|
|
@@ -250,7 +286,7 @@ export class Client<GlobalErrorType extends ClientErrorType = Error, Adapter ext
|
|
|
250
286
|
*/
|
|
251
287
|
onResponse = <ErrorType = null>(
|
|
252
288
|
callback: ResponseInterceptorType<any, ErrorType | GlobalErrorType, Adapter>,
|
|
253
|
-
): Client<GlobalErrorType, Adapter> => {
|
|
289
|
+
): Client<GlobalErrorType, Adapter, EndpointMapper> => {
|
|
254
290
|
this.__onResponseCallbacks.push(callback);
|
|
255
291
|
return this;
|
|
256
292
|
};
|
|
@@ -351,31 +387,54 @@ export class Client<GlobalErrorType extends ClientErrorType = Error, Adapter ext
|
|
|
351
387
|
createRequest = <
|
|
352
388
|
Response,
|
|
353
389
|
Payload = undefined,
|
|
354
|
-
LocalError
|
|
390
|
+
LocalError = undefined,
|
|
355
391
|
QueryParams = ExtractAdapterQueryParamsType<Adapter>,
|
|
356
392
|
>() => {
|
|
357
393
|
return <
|
|
358
|
-
EndpointType extends
|
|
394
|
+
EndpointType extends ExtractAdapterEndpointType<Adapter>,
|
|
359
395
|
AdapterOptions extends ExtractAdapterOptionsType<Adapter>,
|
|
360
396
|
MethodType extends ExtractAdapterMethodType<Adapter>,
|
|
361
397
|
>(
|
|
362
398
|
params: RequestOptionsType<EndpointType, AdapterOptions, MethodType>,
|
|
363
|
-
) =>
|
|
364
|
-
|
|
399
|
+
) => {
|
|
400
|
+
const endpoint = this.endpointMapper(params.endpoint);
|
|
401
|
+
|
|
402
|
+
type ExtractedAdapterType = ExtractUnionAdapter<
|
|
403
|
+
Adapter,
|
|
404
|
+
{
|
|
405
|
+
method: MethodType;
|
|
406
|
+
options: AdapterOptions;
|
|
407
|
+
queryParams: QueryParams;
|
|
408
|
+
}
|
|
409
|
+
> extends NegativeTypes
|
|
410
|
+
? Adapter
|
|
411
|
+
: ExtractUnionAdapter<
|
|
412
|
+
Adapter,
|
|
413
|
+
{
|
|
414
|
+
method: MethodType;
|
|
415
|
+
options: AdapterOptions;
|
|
416
|
+
queryParams: QueryParams;
|
|
417
|
+
}
|
|
418
|
+
>;
|
|
419
|
+
|
|
420
|
+
const mappedParams: RequestOptionsType<
|
|
421
|
+
EndpointType extends string ? EndpointType : typeof endpoint,
|
|
422
|
+
AdapterOptions,
|
|
423
|
+
MethodType
|
|
424
|
+
> = {
|
|
425
|
+
...params,
|
|
426
|
+
endpoint: endpoint as EndpointType extends string ? EndpointType : typeof endpoint,
|
|
427
|
+
};
|
|
428
|
+
|
|
429
|
+
return new Request<
|
|
365
430
|
Response,
|
|
366
431
|
Payload,
|
|
367
432
|
QueryParams,
|
|
368
433
|
GlobalErrorType,
|
|
369
434
|
LocalError,
|
|
370
|
-
EndpointType,
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
method: MethodType;
|
|
375
|
-
options: AdapterOptions;
|
|
376
|
-
queryParams: QueryParams;
|
|
377
|
-
}
|
|
378
|
-
>
|
|
379
|
-
>(this as any, params);
|
|
435
|
+
EndpointType extends string ? EndpointType : typeof endpoint,
|
|
436
|
+
ExtractedAdapterType
|
|
437
|
+
>(this as any, mappedParams);
|
|
438
|
+
};
|
|
380
439
|
};
|
|
381
440
|
}
|