@hyper-fetch/core 2.0.0 → 3.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 +14 -13
- package/dist/adapter/adapter.bindings.d.ts +25 -0
- package/dist/adapter/adapter.browser.d.ts +2 -0
- package/dist/{client/fetch.client.constants.d.ts → adapter/adapter.constants.d.ts} +0 -0
- package/dist/adapter/adapter.d.ts +2 -0
- package/dist/adapter/adapter.server.d.ts +2 -0
- package/dist/adapter/adapter.types.d.ts +57 -0
- package/dist/{client/fetch.client.utils.d.ts → adapter/adapter.utils.d.ts} +3 -3
- package/dist/adapter/index.d.ts +7 -0
- package/dist/cache/cache.d.ts +9 -9
- package/dist/cache/cache.types.d.ts +4 -4
- package/dist/cache/cache.utils.d.ts +3 -3
- package/dist/{builder/builder.constants.d.ts → client/client.constants.d.ts} +0 -0
- package/dist/client/client.d.ts +142 -0
- package/dist/client/client.types.d.ts +41 -0
- package/dist/client/client.utils.d.ts +10 -0
- package/dist/client/index.d.ts +4 -6
- package/dist/dispatcher/dispatcher.d.ts +23 -23
- package/dist/dispatcher/dispatcher.events.d.ts +8 -8
- package/dist/dispatcher/dispatcher.types.d.ts +10 -10
- package/dist/dispatcher/dispatcher.utils.d.ts +5 -5
- package/dist/effect/index.d.ts +2 -2
- package/dist/effect/request.effect.d.ts +14 -0
- package/dist/effect/request.effect.types.d.ts +32 -0
- package/dist/index.cjs.js +1 -1
- package/dist/index.cjs.js.map +4 -4
- package/dist/index.d.ts +3 -3
- package/dist/index.esm.js +1 -1
- package/dist/index.esm.js.map +4 -4
- package/dist/managers/index.d.ts +1 -1
- package/dist/managers/logger/logger.manager.d.ts +5 -5
- package/dist/managers/logger/logger.manager.types.d.ts +7 -7
- package/dist/managers/request/index.d.ts +4 -0
- package/dist/managers/request/request.manager.d.ts +43 -0
- package/dist/managers/request/request.manager.events.d.ts +36 -0
- package/dist/managers/{command/command.manager.types.d.ts → request/request.manager.types.d.ts} +6 -6
- package/dist/managers/{command/command.manager.utils.d.ts → request/request.manager.utils.d.ts} +0 -0
- package/dist/request/index.d.ts +3 -0
- package/dist/request/request.d.ts +98 -0
- package/dist/request/request.types.d.ts +202 -0
- package/dist/request/request.utils.d.ts +22 -0
- package/dist/types/fetch.types.d.ts +16 -16
- package/package.json +1 -1
- package/src/{client/fetch.client.bindings.ts → adapter/adapter.bindings.ts} +58 -58
- package/src/{client/fetch.client.browser.ts → adapter/adapter.browser.ts} +6 -6
- package/src/{client/fetch.client.constants.ts → adapter/adapter.constants.ts} +0 -0
- package/src/{client/fetch.client.server.ts → adapter/adapter.server.ts} +12 -12
- package/src/adapter/adapter.ts +8 -0
- package/src/adapter/adapter.types.ts +79 -0
- package/src/{client/fetch.client.utils.ts → adapter/adapter.utils.ts} +3 -3
- package/src/adapter/index.ts +7 -0
- package/src/cache/cache.ts +19 -19
- package/src/cache/cache.types.ts +4 -4
- package/src/cache/cache.utils.ts +6 -6
- package/src/{builder/builder.constants.ts → client/client.constants.ts} +0 -0
- package/src/client/client.ts +310 -0
- package/src/client/client.types.ts +52 -0
- package/src/{builder/builder.utils.ts → client/client.utils.ts} +34 -31
- package/src/client/index.ts +4 -6
- package/src/dispatcher/dispatcher.events.ts +11 -11
- package/src/dispatcher/dispatcher.ts +68 -68
- package/src/dispatcher/dispatcher.types.ts +10 -10
- package/src/dispatcher/dispatcher.utils.ts +7 -7
- package/src/effect/index.ts +2 -2
- package/src/effect/request.effect.ts +28 -0
- package/src/effect/request.effect.types.ts +35 -0
- package/src/index.ts +4 -4
- package/src/managers/index.ts +1 -1
- package/src/managers/logger/logger.manager.ts +8 -8
- package/src/managers/logger/logger.manager.types.ts +7 -7
- package/src/managers/request/index.ts +4 -0
- package/src/managers/{command/command.manager.events.ts → request/request.manager.events.ts} +47 -51
- package/src/managers/{command/command.manager.ts → request/request.manager.ts} +5 -5
- package/src/managers/request/request.manager.types.ts +25 -0
- package/src/managers/{command/command.manager.utils.ts → request/request.manager.utils.ts} +0 -0
- package/src/request/index.ts +3 -0
- package/src/{command/command.ts → request/request.ts} +188 -194
- package/src/request/request.types.ts +282 -0
- package/src/{command/command.utils.ts → request/request.utils.ts} +56 -53
- package/src/types/fetch.types.ts +18 -18
- package/dist/builder/builder.d.ts +0 -141
- package/dist/builder/builder.types.d.ts +0 -41
- package/dist/builder/builder.utils.d.ts +0 -10
- package/dist/builder/index.d.ts +0 -4
- package/dist/client/fetch.client.bindings.d.ts +0 -25
- package/dist/client/fetch.client.browser.d.ts +0 -2
- package/dist/client/fetch.client.d.ts +0 -2
- package/dist/client/fetch.client.server.d.ts +0 -2
- package/dist/client/fetch.client.types.d.ts +0 -57
- package/dist/command/command.d.ts +0 -98
- package/dist/command/command.types.d.ts +0 -202
- package/dist/command/command.utils.d.ts +0 -22
- package/dist/command/index.d.ts +0 -3
- package/dist/effect/fetch.effect.d.ts +0 -14
- package/dist/effect/fetch.effect.types.d.ts +0 -32
- package/dist/managers/command/command.manager.d.ts +0 -43
- package/dist/managers/command/command.manager.events.d.ts +0 -36
- package/dist/managers/command/index.d.ts +0 -4
- package/src/builder/builder.ts +0 -308
- package/src/builder/builder.types.ts +0 -53
- package/src/builder/index.ts +0 -4
- package/src/client/fetch.client.ts +0 -9
- package/src/client/fetch.client.types.ts +0 -83
- package/src/command/command.types.ts +0 -273
- package/src/command/index.ts +0 -3
- package/src/effect/fetch.effect.ts +0 -28
- package/src/effect/fetch.effect.types.ts +0 -35
- package/src/managers/command/command.manager.types.ts +0 -25
- package/src/managers/command/index.ts +0 -4
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import { getErrorMessage,
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { getErrorMessage, ResponseSuccessType, ResponseErrorType, ProgressDataType } from "adapter";
|
|
2
|
+
import { RequestInstance, getProgressData, AdapterProgressEventType } from "request";
|
|
3
|
+
import { ExtractResponseType, ExtractErrorType } from "types";
|
|
4
4
|
|
|
5
|
-
export const
|
|
6
|
-
const { url,
|
|
5
|
+
export const getAdapterBindings = async <ConfigType = any>(cmd: RequestInstance, requestId: string) => {
|
|
6
|
+
const { url, requestManager, loggerManager, headerMapper, payloadMapper } = cmd.client;
|
|
7
7
|
|
|
8
|
-
const logger = loggerManager.init("
|
|
8
|
+
const logger = loggerManager.init("Adapter");
|
|
9
9
|
|
|
10
10
|
let requestStartTimestamp: null | number = null;
|
|
11
11
|
let responseStartTimestamp: null | number = null;
|
|
12
|
-
let
|
|
12
|
+
let request = cmd;
|
|
13
13
|
|
|
14
14
|
// Progress
|
|
15
15
|
let requestTotal = 1;
|
|
@@ -18,24 +18,24 @@ export const getClientBindings = async <ConfigType = any>(cmd: CommandInstance,
|
|
|
18
18
|
let previousResponseTotal = 0;
|
|
19
19
|
|
|
20
20
|
// Pre request modifications
|
|
21
|
-
logger.debug(`Starting
|
|
21
|
+
logger.debug(`Starting request middleware callbacks`);
|
|
22
22
|
|
|
23
|
-
|
|
23
|
+
request = await request.client.__modifyRequest(cmd);
|
|
24
24
|
|
|
25
|
-
if (
|
|
26
|
-
|
|
25
|
+
if (request.auth) {
|
|
26
|
+
request = await request.client.__modifyAuth(cmd);
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
//
|
|
30
|
-
const {
|
|
29
|
+
// Request Setup
|
|
30
|
+
const { client, abortKey, queueKey, endpoint, data } = request;
|
|
31
31
|
|
|
32
|
-
|
|
32
|
+
requestManager.addAbortController(abortKey, requestId);
|
|
33
33
|
|
|
34
34
|
const fullUrl = url + endpoint;
|
|
35
|
-
const effects =
|
|
36
|
-
const headers = headerMapper(
|
|
35
|
+
const effects = client.effects.filter((effect) => request.effectKey === effect.getEffectKey());
|
|
36
|
+
const headers = headerMapper(request);
|
|
37
37
|
const payload = payloadMapper(data);
|
|
38
|
-
const config: ConfigType = { ...
|
|
38
|
+
const config: ConfigType = { ...request.requestOptions.options };
|
|
39
39
|
|
|
40
40
|
const getRequestStartTimestamp = () => {
|
|
41
41
|
return requestStartTimestamp;
|
|
@@ -47,7 +47,7 @@ export const getClientBindings = async <ConfigType = any>(cmd: CommandInstance,
|
|
|
47
47
|
|
|
48
48
|
// Progress
|
|
49
49
|
|
|
50
|
-
const getTotal = (previousTotal: number, progress?:
|
|
50
|
+
const getTotal = (previousTotal: number, progress?: ProgressDataType) => {
|
|
51
51
|
if (!progress) return previousTotal;
|
|
52
52
|
const total = Number(progress.total || 0);
|
|
53
53
|
const loaded = Number(progress.loaded || 0);
|
|
@@ -57,39 +57,39 @@ export const getClientBindings = async <ConfigType = any>(cmd: CommandInstance,
|
|
|
57
57
|
const handleRequestProgress = (
|
|
58
58
|
startTimestamp: number,
|
|
59
59
|
progressTimestamp: number,
|
|
60
|
-
progressEvent:
|
|
60
|
+
progressEvent: AdapterProgressEventType,
|
|
61
61
|
) => {
|
|
62
62
|
const progress = getProgressData(new Date(startTimestamp), new Date(progressTimestamp), progressEvent);
|
|
63
63
|
|
|
64
64
|
if (previousRequestTotal !== 100) {
|
|
65
65
|
previousRequestTotal = progress.total;
|
|
66
|
-
|
|
66
|
+
requestManager.events.emitUploadProgress(queueKey, requestId, progress, { requestId, request });
|
|
67
67
|
}
|
|
68
68
|
};
|
|
69
69
|
|
|
70
70
|
const handleResponseProgress = (
|
|
71
71
|
startTimestamp: number,
|
|
72
72
|
progressTimestamp: number,
|
|
73
|
-
progressEvent:
|
|
73
|
+
progressEvent: AdapterProgressEventType,
|
|
74
74
|
) => {
|
|
75
75
|
const progress = getProgressData(new Date(startTimestamp), new Date(progressTimestamp), progressEvent);
|
|
76
76
|
|
|
77
77
|
if (previousResponseTotal !== 100) {
|
|
78
78
|
previousResponseTotal = progress.total;
|
|
79
|
-
|
|
79
|
+
requestManager.events.emitDownloadProgress(queueKey, requestId, progress, { requestId, request });
|
|
80
80
|
}
|
|
81
81
|
};
|
|
82
82
|
|
|
83
83
|
// Pre-request
|
|
84
84
|
|
|
85
85
|
const onBeforeRequest = () => {
|
|
86
|
-
effects.forEach((effect) => effect.onTrigger(
|
|
86
|
+
effects.forEach((effect) => effect.onTrigger(request));
|
|
87
87
|
};
|
|
88
88
|
|
|
89
89
|
// Request
|
|
90
90
|
|
|
91
|
-
const onRequestStart = (progress?:
|
|
92
|
-
effects.forEach((action) => action.onStart(
|
|
91
|
+
const onRequestStart = (progress?: ProgressDataType) => {
|
|
92
|
+
effects.forEach((action) => action.onStart(request));
|
|
93
93
|
|
|
94
94
|
if (progress?.total) {
|
|
95
95
|
requestTotal = getTotal(requestTotal, progress);
|
|
@@ -101,11 +101,11 @@ export const getClientBindings = async <ConfigType = any>(cmd: CommandInstance,
|
|
|
101
101
|
};
|
|
102
102
|
requestStartTimestamp = +new Date();
|
|
103
103
|
handleRequestProgress(requestStartTimestamp, requestStartTimestamp, initialPayload);
|
|
104
|
-
|
|
104
|
+
requestManager.events.emitRequestStart(queueKey, requestId, { requestId, request });
|
|
105
105
|
return requestStartTimestamp;
|
|
106
106
|
};
|
|
107
107
|
|
|
108
|
-
const onRequestProgress = (progress:
|
|
108
|
+
const onRequestProgress = (progress: ProgressDataType) => {
|
|
109
109
|
if (!requestStartTimestamp) {
|
|
110
110
|
requestStartTimestamp = +new Date();
|
|
111
111
|
}
|
|
@@ -135,7 +135,7 @@ export const getClientBindings = async <ConfigType = any>(cmd: CommandInstance,
|
|
|
135
135
|
|
|
136
136
|
// Response
|
|
137
137
|
|
|
138
|
-
const onResponseStart = (progress?:
|
|
138
|
+
const onResponseStart = (progress?: ProgressDataType) => {
|
|
139
139
|
responseStartTimestamp = +new Date();
|
|
140
140
|
|
|
141
141
|
responseTotal = getTotal(responseTotal, progress);
|
|
@@ -146,11 +146,11 @@ export const getClientBindings = async <ConfigType = any>(cmd: CommandInstance,
|
|
|
146
146
|
};
|
|
147
147
|
|
|
148
148
|
handleResponseProgress(responseStartTimestamp, responseStartTimestamp, initialPayload);
|
|
149
|
-
|
|
149
|
+
requestManager.events.emitResponseStart(queueKey, requestId, { requestId, request });
|
|
150
150
|
return responseStartTimestamp;
|
|
151
151
|
};
|
|
152
152
|
|
|
153
|
-
const onResponseProgress = (progress:
|
|
153
|
+
const onResponseProgress = (progress: ProgressDataType) => {
|
|
154
154
|
if (!responseStartTimestamp) {
|
|
155
155
|
responseStartTimestamp = +new Date();
|
|
156
156
|
}
|
|
@@ -171,7 +171,7 @@ export const getClientBindings = async <ConfigType = any>(cmd: CommandInstance,
|
|
|
171
171
|
}
|
|
172
172
|
|
|
173
173
|
const progressTimestamp = +new Date();
|
|
174
|
-
|
|
174
|
+
requestManager.removeAbortController(abortKey, requestId);
|
|
175
175
|
handleResponseProgress(responseStartTimestamp, progressTimestamp, {
|
|
176
176
|
total: responseTotal,
|
|
177
177
|
loaded: responseTotal,
|
|
@@ -181,17 +181,17 @@ export const getClientBindings = async <ConfigType = any>(cmd: CommandInstance,
|
|
|
181
181
|
|
|
182
182
|
// Success
|
|
183
183
|
|
|
184
|
-
const onSuccess = async <T extends
|
|
184
|
+
const onSuccess = async <T extends RequestInstance>(
|
|
185
185
|
responseData: unknown,
|
|
186
186
|
status: number,
|
|
187
|
-
resolve: (value:
|
|
188
|
-
): Promise<
|
|
189
|
-
let response = [responseData, null, status] as
|
|
190
|
-
response = await
|
|
191
|
-
response = await
|
|
187
|
+
resolve: (value: ResponseErrorType<ExtractErrorType<T>>) => void,
|
|
188
|
+
): Promise<ResponseSuccessType<ExtractResponseType<T>>> => {
|
|
189
|
+
let response = [responseData, null, status] as ResponseSuccessType<ExtractResponseType<T>>;
|
|
190
|
+
response = await request.client.__modifyResponse(response, request);
|
|
191
|
+
response = await request.client.__modifySuccessResponse(response, request);
|
|
192
192
|
|
|
193
|
-
effects.forEach((effect) => effect.onSuccess(response,
|
|
194
|
-
effects.forEach((effect) => effect.onFinished(response,
|
|
193
|
+
effects.forEach((effect) => effect.onSuccess(response, request));
|
|
194
|
+
effects.forEach((effect) => effect.onFinished(response, request));
|
|
195
195
|
|
|
196
196
|
resolve(response);
|
|
197
197
|
|
|
@@ -200,40 +200,40 @@ export const getClientBindings = async <ConfigType = any>(cmd: CommandInstance,
|
|
|
200
200
|
|
|
201
201
|
// Errors
|
|
202
202
|
|
|
203
|
-
const onError = async <T extends
|
|
204
|
-
error: Error |
|
|
203
|
+
const onError = async <T extends RequestInstance>(
|
|
204
|
+
error: Error | ExtractErrorType<T>,
|
|
205
205
|
status: number,
|
|
206
|
-
resolve: (value:
|
|
207
|
-
): Promise<
|
|
208
|
-
let responseData = [null, error, status] as
|
|
206
|
+
resolve: (value: ResponseErrorType<ExtractErrorType<T>>) => void,
|
|
207
|
+
): Promise<ResponseErrorType<ExtractErrorType<T>>> => {
|
|
208
|
+
let responseData = [null, error, status] as ResponseErrorType<ExtractErrorType<T>>;
|
|
209
209
|
|
|
210
|
-
responseData = await
|
|
211
|
-
responseData = await
|
|
210
|
+
responseData = await request.client.__modifyResponse(responseData, request);
|
|
211
|
+
responseData = await request.client.__modifyErrorResponse(responseData, request);
|
|
212
212
|
|
|
213
|
-
effects.forEach((effect) => effect.onError(responseData,
|
|
214
|
-
effects.forEach((effect) => effect.onFinished(responseData,
|
|
213
|
+
effects.forEach((effect) => effect.onError(responseData, request));
|
|
214
|
+
effects.forEach((effect) => effect.onFinished(responseData, request));
|
|
215
215
|
|
|
216
216
|
resolve(responseData);
|
|
217
217
|
|
|
218
218
|
return responseData;
|
|
219
219
|
};
|
|
220
220
|
|
|
221
|
-
const onAbortError = <T extends
|
|
222
|
-
resolve: (value:
|
|
221
|
+
const onAbortError = <T extends RequestInstance>(
|
|
222
|
+
resolve: (value: ResponseErrorType<ExtractErrorType<T>>) => void,
|
|
223
223
|
) => {
|
|
224
224
|
const error = getErrorMessage("abort");
|
|
225
225
|
return onError(error, 0, resolve);
|
|
226
226
|
};
|
|
227
227
|
|
|
228
|
-
const onTimeoutError = <T extends
|
|
229
|
-
resolve: (value:
|
|
228
|
+
const onTimeoutError = <T extends RequestInstance>(
|
|
229
|
+
resolve: (value: ResponseErrorType<ExtractErrorType<T>>) => void,
|
|
230
230
|
) => {
|
|
231
231
|
const error = getErrorMessage("timeout");
|
|
232
232
|
return onError(error, 0, resolve);
|
|
233
233
|
};
|
|
234
234
|
|
|
235
|
-
const onUnexpectedError = <T extends
|
|
236
|
-
resolve: (value:
|
|
235
|
+
const onUnexpectedError = <T extends RequestInstance>(
|
|
236
|
+
resolve: (value: ResponseErrorType<ExtractErrorType<T>>) => void,
|
|
237
237
|
) => {
|
|
238
238
|
const error = getErrorMessage();
|
|
239
239
|
return onError(error, 0, resolve);
|
|
@@ -242,12 +242,12 @@ export const getClientBindings = async <ConfigType = any>(cmd: CommandInstance,
|
|
|
242
242
|
// Abort
|
|
243
243
|
|
|
244
244
|
const getAbortController = () => {
|
|
245
|
-
return
|
|
245
|
+
return requestManager.getAbortController(abortKey, requestId);
|
|
246
246
|
};
|
|
247
247
|
|
|
248
|
-
const createAbortListener = <T extends
|
|
248
|
+
const createAbortListener = <T extends RequestInstance>(
|
|
249
249
|
callback: () => void,
|
|
250
|
-
resolve: (value:
|
|
250
|
+
resolve: (value: ResponseErrorType<ExtractErrorType<T>>) => void,
|
|
251
251
|
) => {
|
|
252
252
|
const controller = getAbortController();
|
|
253
253
|
if (!controller) {
|
|
@@ -257,7 +257,7 @@ export const getClientBindings = async <ConfigType = any>(cmd: CommandInstance,
|
|
|
257
257
|
const fn = () => {
|
|
258
258
|
onAbortError(resolve);
|
|
259
259
|
callback();
|
|
260
|
-
|
|
260
|
+
requestManager.events.emitAbort(abortKey, requestId, request);
|
|
261
261
|
};
|
|
262
262
|
|
|
263
263
|
controller.signal.addEventListener("abort", fn);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { parseErrorResponse, parseResponse } from "./
|
|
1
|
+
import { getAdapterBindings, defaultTimeout, ResponseType, AdapterType } from "adapter";
|
|
2
|
+
import { parseErrorResponse, parseResponse } from "./adapter.utils";
|
|
3
3
|
|
|
4
|
-
export const
|
|
4
|
+
export const browserAdapter: AdapterType = async (request, requestId) => {
|
|
5
5
|
const {
|
|
6
6
|
fullUrl,
|
|
7
7
|
headers,
|
|
@@ -18,16 +18,16 @@ export const browserClient: ClientType = async (command, requestId) => {
|
|
|
18
18
|
onError,
|
|
19
19
|
onResponseEnd,
|
|
20
20
|
onTimeoutError,
|
|
21
|
-
} = await
|
|
21
|
+
} = await getAdapterBindings(request, requestId);
|
|
22
22
|
|
|
23
|
-
const { method } =
|
|
23
|
+
const { method } = request;
|
|
24
24
|
|
|
25
25
|
const xhr = new XMLHttpRequest();
|
|
26
26
|
xhr.timeout = defaultTimeout;
|
|
27
27
|
|
|
28
28
|
const abort = () => xhr.abort();
|
|
29
29
|
|
|
30
|
-
return new Promise<
|
|
30
|
+
return new Promise<ResponseType<unknown, unknown>>((resolve) => {
|
|
31
31
|
// Inject xhr options
|
|
32
32
|
Object.entries(config).forEach(([name, value]) => {
|
|
33
33
|
xhr[name] = value;
|
|
File without changes
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import http from "http";
|
|
2
2
|
import stream from "stream";
|
|
3
3
|
|
|
4
|
-
import {
|
|
5
|
-
import { parseErrorResponse, parseResponse, getUploadSize, getStreamPayload } from "./
|
|
4
|
+
import { getAdapterBindings, defaultTimeout, ResponseType, AdapterType, AdapterOptionsType } from "adapter";
|
|
5
|
+
import { parseErrorResponse, parseResponse, getUploadSize, getStreamPayload } from "./adapter.utils";
|
|
6
6
|
|
|
7
|
-
export const
|
|
7
|
+
export const serverAdapter: AdapterType = async (request, requestId) => {
|
|
8
8
|
const {
|
|
9
9
|
fullUrl,
|
|
10
10
|
headers,
|
|
@@ -21,11 +21,11 @@ export const serverClient: ClientType = async (command, requestId) => {
|
|
|
21
21
|
onTimeoutError,
|
|
22
22
|
onError,
|
|
23
23
|
onResponseEnd,
|
|
24
|
-
} = await
|
|
24
|
+
} = await getAdapterBindings<AdapterOptionsType>(request, requestId);
|
|
25
25
|
|
|
26
|
-
const { method } =
|
|
26
|
+
const { method } = request;
|
|
27
27
|
|
|
28
|
-
return new Promise<
|
|
28
|
+
return new Promise<ResponseType<unknown, unknown>>((resolve) => {
|
|
29
29
|
const execute = async () => {
|
|
30
30
|
const options: http.RequestOptions = {
|
|
31
31
|
path: fullUrl,
|
|
@@ -47,7 +47,7 @@ export const serverClient: ClientType = async (command, requestId) => {
|
|
|
47
47
|
|
|
48
48
|
const payloadChunks = await getStreamPayload(payload);
|
|
49
49
|
|
|
50
|
-
const
|
|
50
|
+
const httpRequest = http.request(options, (response) => {
|
|
51
51
|
response.setEncoding("utf8");
|
|
52
52
|
|
|
53
53
|
let chunks = "";
|
|
@@ -79,10 +79,10 @@ export const serverClient: ClientType = async (command, requestId) => {
|
|
|
79
79
|
});
|
|
80
80
|
});
|
|
81
81
|
|
|
82
|
-
unmountListener = createAbortListener(
|
|
82
|
+
unmountListener = createAbortListener(httpRequest.destroy, resolve);
|
|
83
83
|
|
|
84
|
-
|
|
85
|
-
|
|
84
|
+
httpRequest.on("timeout", () => onTimeoutError(resolve));
|
|
85
|
+
httpRequest.on("error", (error) => onError(error, 0, resolve));
|
|
86
86
|
|
|
87
87
|
if (payloadChunks) {
|
|
88
88
|
const readableStream = stream.Readable.from(payloadChunks, { objectMode: false })
|
|
@@ -95,9 +95,9 @@ export const serverClient: ClientType = async (command, requestId) => {
|
|
|
95
95
|
onRequestEnd();
|
|
96
96
|
});
|
|
97
97
|
|
|
98
|
-
readableStream.pipe(
|
|
98
|
+
readableStream.pipe(httpRequest);
|
|
99
99
|
} else {
|
|
100
|
-
|
|
100
|
+
httpRequest.end();
|
|
101
101
|
}
|
|
102
102
|
};
|
|
103
103
|
execute();
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { browserAdapter, serverAdapter, AdapterType } from "adapter";
|
|
2
|
+
|
|
3
|
+
export const adapter: AdapterType = async (request, requestId) => {
|
|
4
|
+
if (request.client.appManager.isNodeJs || !window?.XMLHttpRequest) {
|
|
5
|
+
return serverAdapter(request, requestId);
|
|
6
|
+
}
|
|
7
|
+
return browserAdapter(request, requestId);
|
|
8
|
+
};
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { RequestInstance } from "request";
|
|
2
|
+
|
|
3
|
+
// Adapter
|
|
4
|
+
|
|
5
|
+
export type AdapterType = (request: RequestInstance, requestId: string) => Promise<ResponseType<any, any>>;
|
|
6
|
+
|
|
7
|
+
export type AdapterOptionsType = Partial<XMLHttpRequest>;
|
|
8
|
+
|
|
9
|
+
export type AdapterPayloadMappingType = (data: unknown) => string | FormData;
|
|
10
|
+
|
|
11
|
+
// Responses
|
|
12
|
+
|
|
13
|
+
export type ResponseType<GenericDataType, GenericErrorType> = [
|
|
14
|
+
GenericDataType | null,
|
|
15
|
+
GenericErrorType | null,
|
|
16
|
+
number | null,
|
|
17
|
+
];
|
|
18
|
+
export type ResponseSuccessType<GenericDataType> = [GenericDataType, null, number | null];
|
|
19
|
+
export type ResponseErrorType<GenericErrorType> = [null, GenericErrorType, number | null];
|
|
20
|
+
|
|
21
|
+
// QueryParams
|
|
22
|
+
|
|
23
|
+
export type QueryParamValuesType = number | string | boolean | null | undefined;
|
|
24
|
+
export type QueryParamType = QueryParamValuesType | Array<QueryParamValuesType> | Record<string, QueryParamValuesType>;
|
|
25
|
+
export type QueryParamsType = Record<string, QueryParamType>;
|
|
26
|
+
|
|
27
|
+
// Headers
|
|
28
|
+
|
|
29
|
+
export type HeaderMappingType = <T extends RequestInstance>(request: T) => HeadersInit;
|
|
30
|
+
|
|
31
|
+
export type AdapterHeadersProps = {
|
|
32
|
+
isFormData: boolean;
|
|
33
|
+
headers: HeadersInit | undefined;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
// Stringify
|
|
37
|
+
|
|
38
|
+
export type QueryStringifyOptionsType = {
|
|
39
|
+
/**
|
|
40
|
+
* Strict URI encoding
|
|
41
|
+
*/
|
|
42
|
+
strict?: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Encode keys and values
|
|
45
|
+
*/
|
|
46
|
+
encode?: boolean;
|
|
47
|
+
/**
|
|
48
|
+
* Array encoding type
|
|
49
|
+
*/
|
|
50
|
+
arrayFormat?: "bracket" | "index" | "comma" | "separator" | "bracket-separator" | "none";
|
|
51
|
+
/**
|
|
52
|
+
* Array format separator
|
|
53
|
+
*/
|
|
54
|
+
arraySeparator?: string;
|
|
55
|
+
/**
|
|
56
|
+
* Skip keys with null values
|
|
57
|
+
*/
|
|
58
|
+
skipNull?: boolean;
|
|
59
|
+
/**
|
|
60
|
+
* Skip keys with empty string
|
|
61
|
+
*/
|
|
62
|
+
skipEmptyString?: boolean;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
// Progress
|
|
66
|
+
|
|
67
|
+
export type ProgressDataType = {
|
|
68
|
+
total?: number;
|
|
69
|
+
loaded?: number;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
export type ProgressType = {
|
|
73
|
+
progress: number;
|
|
74
|
+
timeLeft: number | null;
|
|
75
|
+
sizeLeft: number;
|
|
76
|
+
total: number;
|
|
77
|
+
loaded: number;
|
|
78
|
+
startTimestamp: number;
|
|
79
|
+
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { RequestInstance } from "request";
|
|
2
|
+
import { ExtractErrorType } from "types";
|
|
3
3
|
|
|
4
4
|
// Utils
|
|
5
5
|
|
|
@@ -23,7 +23,7 @@ export const parseResponse = (response: string | unknown) => {
|
|
|
23
23
|
}
|
|
24
24
|
};
|
|
25
25
|
|
|
26
|
-
export const parseErrorResponse = <T extends
|
|
26
|
+
export const parseErrorResponse = <T extends RequestInstance>(response: unknown): ExtractErrorType<T> => {
|
|
27
27
|
return response ? parseResponse(response) : getErrorMessage();
|
|
28
28
|
};
|
|
29
29
|
|
package/src/cache/cache.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import EventEmitter from "events";
|
|
2
2
|
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
3
|
+
import { ResponseType } from "adapter";
|
|
4
|
+
import { ClientInstance } from "client";
|
|
5
|
+
import { ResponseDetailsType, LoggerType } from "managers";
|
|
6
6
|
import {
|
|
7
7
|
CacheOptionsType,
|
|
8
8
|
CacheAsyncStorageType,
|
|
@@ -11,13 +11,13 @@ import {
|
|
|
11
11
|
getCacheEvents,
|
|
12
12
|
CacheValueType,
|
|
13
13
|
} from "cache";
|
|
14
|
-
import {
|
|
14
|
+
import { RequestDump, RequestInstance } from "request";
|
|
15
15
|
|
|
16
16
|
/**
|
|
17
17
|
* Cache class handles the data exchange with the dispatchers.
|
|
18
18
|
*
|
|
19
19
|
* @note
|
|
20
|
-
* Keys used to save the values are created dynamically on the
|
|
20
|
+
* Keys used to save the values are created dynamically on the Request class
|
|
21
21
|
*
|
|
22
22
|
*/
|
|
23
23
|
export class Cache {
|
|
@@ -30,21 +30,21 @@ export class Cache {
|
|
|
30
30
|
public garbageCollectors = new Map<string, ReturnType<typeof setTimeout>>();
|
|
31
31
|
private logger: LoggerType;
|
|
32
32
|
|
|
33
|
-
constructor(public
|
|
33
|
+
constructor(public client: ClientInstance, 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);
|
|
37
37
|
|
|
38
38
|
this.clearKey = this.options?.clearKey || "";
|
|
39
39
|
this.lazyStorage = this.options?.lazyStorage;
|
|
40
|
-
this.logger = this.
|
|
40
|
+
this.logger = this.client.loggerManager.init("Cache");
|
|
41
41
|
|
|
42
42
|
this.getLazyKeys().then((keys) => {
|
|
43
43
|
keys.forEach(this.scheduleGarbageCollector);
|
|
44
44
|
});
|
|
45
45
|
|
|
46
46
|
// Going back from offline should re-trigger garbage collection
|
|
47
|
-
this.
|
|
47
|
+
this.client.appManager.events.onOnline(() => {
|
|
48
48
|
this.getLazyKeys().then((keys) => {
|
|
49
49
|
keys.forEach(this.scheduleGarbageCollector);
|
|
50
50
|
});
|
|
@@ -53,18 +53,18 @@ export class Cache {
|
|
|
53
53
|
|
|
54
54
|
/**
|
|
55
55
|
* Set the cache data to the storage
|
|
56
|
-
* @param
|
|
56
|
+
* @param request
|
|
57
57
|
* @param response
|
|
58
58
|
* @param details
|
|
59
59
|
* @returns
|
|
60
60
|
*/
|
|
61
61
|
set = <Response, Error>(
|
|
62
|
-
|
|
63
|
-
response:
|
|
64
|
-
details:
|
|
62
|
+
request: RequestInstance | RequestDump<RequestInstance>,
|
|
63
|
+
response: ResponseType<Response, Error>,
|
|
64
|
+
details: ResponseDetailsType,
|
|
65
65
|
): void => {
|
|
66
|
-
this.logger.debug("Processing cache response", {
|
|
67
|
-
const { cacheKey, cache, cacheTime, garbageCollection } =
|
|
66
|
+
this.logger.debug("Processing cache response", { request, response, details });
|
|
67
|
+
const { cacheKey, cache, cacheTime, garbageCollection } = request;
|
|
68
68
|
const cachedData = this.storage.get<Response, Error>(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
|
|
@@ -74,16 +74,16 @@ export class Cache {
|
|
|
74
74
|
const newCacheData: CacheValueType = { data, details, cacheTime, clearKey: this.clearKey, garbageCollection };
|
|
75
75
|
|
|
76
76
|
this.events.emitCacheData<Response, Error>(cacheKey, newCacheData);
|
|
77
|
-
this.logger.debug("Emitting cache response", {
|
|
77
|
+
this.logger.debug("Emitting cache response", { request, response, details });
|
|
78
78
|
|
|
79
79
|
// If request should not use cache - just emit response data
|
|
80
80
|
if (!cache) {
|
|
81
|
-
return this.logger.debug("Prevented saving response to cache", {
|
|
81
|
+
return this.logger.debug("Prevented saving response to cache", { request, response, details });
|
|
82
82
|
}
|
|
83
83
|
|
|
84
84
|
// Only success data is valid for the cache store
|
|
85
85
|
if (!details.isFailed) {
|
|
86
|
-
this.logger.debug("Saving response to cache storage", {
|
|
86
|
+
this.logger.debug("Saving response to cache storage", { request, response, details });
|
|
87
87
|
this.storage.set<Response, Error>(cacheKey, newCacheData);
|
|
88
88
|
this.lazyStorage?.set<Response, Error>(cacheKey, newCacheData);
|
|
89
89
|
this.options?.onChange?.(cacheKey, newCacheData);
|
|
@@ -209,13 +209,13 @@ export class Cache {
|
|
|
209
209
|
this.garbageCollectors.set(
|
|
210
210
|
cacheKey,
|
|
211
211
|
setTimeout(() => {
|
|
212
|
-
if (this.
|
|
212
|
+
if (this.client.appManager.isOnline) {
|
|
213
213
|
this.logger.info("Garbage collecting cache element", { cacheKey });
|
|
214
214
|
this.delete(cacheKey);
|
|
215
215
|
}
|
|
216
216
|
}, timeLeft),
|
|
217
217
|
);
|
|
218
|
-
} else if (this.
|
|
218
|
+
} else if (this.client.appManager.isOnline) {
|
|
219
219
|
this.logger.info("Garbage collecting cache element", { cacheKey });
|
|
220
220
|
this.delete(cacheKey);
|
|
221
221
|
}
|
package/src/cache/cache.types.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Cache } from "cache";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { ResponseType } from "adapter";
|
|
3
|
+
import { ResponseDetailsType } from "managers";
|
|
4
4
|
|
|
5
5
|
export type CacheOptionsType = {
|
|
6
6
|
/**
|
|
@@ -31,8 +31,8 @@ export type CacheOptionsType = {
|
|
|
31
31
|
|
|
32
32
|
// Values
|
|
33
33
|
export type CacheValueType<Response = any, Error = any> = {
|
|
34
|
-
data:
|
|
35
|
-
details:
|
|
34
|
+
data: ResponseType<Response, Error>;
|
|
35
|
+
details: ResponseDetailsType;
|
|
36
36
|
cacheTime: number;
|
|
37
37
|
clearKey: string;
|
|
38
38
|
garbageCollection: number;
|
package/src/cache/cache.utils.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { RequestInstance } from "request";
|
|
2
2
|
import { isFailedRequest } from "dispatcher";
|
|
3
|
-
import {
|
|
3
|
+
import { ExtractAdapterReturnType } from "types";
|
|
4
4
|
|
|
5
|
-
export const getCacheData = <T extends
|
|
6
|
-
previousResponse:
|
|
7
|
-
response:
|
|
8
|
-
):
|
|
5
|
+
export const getCacheData = <T extends RequestInstance>(
|
|
6
|
+
previousResponse: ExtractAdapterReturnType<T> | undefined,
|
|
7
|
+
response: ExtractAdapterReturnType<T>,
|
|
8
|
+
): ExtractAdapterReturnType<T> => {
|
|
9
9
|
const isFailed = isFailedRequest(response);
|
|
10
10
|
|
|
11
11
|
const previousData = isFailed && previousResponse ? previousResponse[0] : null;
|
|
File without changes
|