@hyper-fetch/core 2.1.0 → 3.0.1

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.
Files changed (109) hide show
  1. package/README.md +1 -1
  2. package/dist/adapter/adapter.bindings.d.ts +25 -0
  3. package/dist/adapter/adapter.browser.d.ts +2 -0
  4. package/dist/{client/fetch.client.constants.d.ts → adapter/adapter.constants.d.ts} +0 -0
  5. package/dist/adapter/adapter.d.ts +2 -0
  6. package/dist/adapter/adapter.server.d.ts +2 -0
  7. package/dist/adapter/adapter.types.d.ts +57 -0
  8. package/dist/{client/fetch.client.utils.d.ts → adapter/adapter.utils.d.ts} +3 -3
  9. package/dist/adapter/index.d.ts +7 -0
  10. package/dist/cache/cache.d.ts +9 -9
  11. package/dist/cache/cache.types.d.ts +4 -4
  12. package/dist/cache/cache.utils.d.ts +3 -3
  13. package/dist/{builder/builder.constants.d.ts → client/client.constants.d.ts} +0 -0
  14. package/dist/client/client.d.ts +142 -0
  15. package/dist/client/client.types.d.ts +41 -0
  16. package/dist/client/client.utils.d.ts +10 -0
  17. package/dist/client/index.d.ts +4 -6
  18. package/dist/dispatcher/dispatcher.d.ts +23 -23
  19. package/dist/dispatcher/dispatcher.events.d.ts +8 -8
  20. package/dist/dispatcher/dispatcher.types.d.ts +10 -10
  21. package/dist/dispatcher/dispatcher.utils.d.ts +5 -5
  22. package/dist/effect/index.d.ts +2 -2
  23. package/dist/effect/request.effect.d.ts +14 -0
  24. package/dist/effect/request.effect.types.d.ts +32 -0
  25. package/dist/index.cjs.js +1 -1
  26. package/dist/index.cjs.js.map +4 -4
  27. package/dist/index.d.ts +3 -3
  28. package/dist/index.esm.js +1 -1
  29. package/dist/index.esm.js.map +4 -4
  30. package/dist/managers/index.d.ts +1 -1
  31. package/dist/managers/logger/logger.manager.d.ts +5 -5
  32. package/dist/managers/logger/logger.manager.types.d.ts +7 -7
  33. package/dist/managers/request/index.d.ts +4 -0
  34. package/dist/managers/request/request.manager.d.ts +43 -0
  35. package/dist/managers/request/request.manager.events.d.ts +36 -0
  36. package/dist/managers/{command/command.manager.types.d.ts → request/request.manager.types.d.ts} +6 -6
  37. package/dist/managers/{command/command.manager.utils.d.ts → request/request.manager.utils.d.ts} +0 -0
  38. package/dist/request/index.d.ts +3 -0
  39. package/dist/request/request.d.ts +98 -0
  40. package/dist/request/request.types.d.ts +202 -0
  41. package/dist/request/request.utils.d.ts +22 -0
  42. package/dist/types/fetch.types.d.ts +16 -16
  43. package/package.json +1 -1
  44. package/src/{client/fetch.client.bindings.ts → adapter/adapter.bindings.ts} +58 -58
  45. package/src/{client/fetch.client.browser.ts → adapter/adapter.browser.ts} +6 -6
  46. package/src/{client/fetch.client.constants.ts → adapter/adapter.constants.ts} +0 -0
  47. package/src/{client/fetch.client.server.ts → adapter/adapter.server.ts} +22 -18
  48. package/src/adapter/adapter.ts +8 -0
  49. package/src/adapter/adapter.types.ts +79 -0
  50. package/src/{client/fetch.client.utils.ts → adapter/adapter.utils.ts} +3 -3
  51. package/src/adapter/index.ts +7 -0
  52. package/src/cache/cache.ts +19 -19
  53. package/src/cache/cache.types.ts +4 -4
  54. package/src/cache/cache.utils.ts +6 -6
  55. package/src/{builder/builder.constants.ts → client/client.constants.ts} +0 -0
  56. package/src/client/client.ts +310 -0
  57. package/src/client/client.types.ts +52 -0
  58. package/src/{builder/builder.utils.ts → client/client.utils.ts} +34 -31
  59. package/src/client/index.ts +4 -6
  60. package/src/dispatcher/dispatcher.events.ts +11 -11
  61. package/src/dispatcher/dispatcher.ts +68 -68
  62. package/src/dispatcher/dispatcher.types.ts +10 -10
  63. package/src/dispatcher/dispatcher.utils.ts +7 -7
  64. package/src/effect/index.ts +2 -2
  65. package/src/effect/request.effect.ts +28 -0
  66. package/src/effect/request.effect.types.ts +35 -0
  67. package/src/index.ts +4 -4
  68. package/src/managers/index.ts +1 -1
  69. package/src/managers/logger/logger.manager.ts +8 -8
  70. package/src/managers/logger/logger.manager.types.ts +7 -7
  71. package/src/managers/request/index.ts +4 -0
  72. package/src/managers/{command/command.manager.events.ts → request/request.manager.events.ts} +47 -51
  73. package/src/managers/{command/command.manager.ts → request/request.manager.ts} +5 -5
  74. package/src/managers/request/request.manager.types.ts +25 -0
  75. package/src/managers/{command/command.manager.utils.ts → request/request.manager.utils.ts} +0 -0
  76. package/src/request/index.ts +3 -0
  77. package/src/{command/command.ts → request/request.ts} +188 -194
  78. package/src/request/request.types.ts +282 -0
  79. package/src/{command/command.utils.ts → request/request.utils.ts} +56 -53
  80. package/src/types/fetch.types.ts +18 -18
  81. package/dist/builder/builder.d.ts +0 -141
  82. package/dist/builder/builder.types.d.ts +0 -41
  83. package/dist/builder/builder.utils.d.ts +0 -10
  84. package/dist/builder/index.d.ts +0 -4
  85. package/dist/client/fetch.client.bindings.d.ts +0 -25
  86. package/dist/client/fetch.client.browser.d.ts +0 -2
  87. package/dist/client/fetch.client.d.ts +0 -2
  88. package/dist/client/fetch.client.server.d.ts +0 -2
  89. package/dist/client/fetch.client.types.d.ts +0 -57
  90. package/dist/command/command.d.ts +0 -98
  91. package/dist/command/command.types.d.ts +0 -202
  92. package/dist/command/command.utils.d.ts +0 -22
  93. package/dist/command/index.d.ts +0 -3
  94. package/dist/effect/fetch.effect.d.ts +0 -14
  95. package/dist/effect/fetch.effect.types.d.ts +0 -32
  96. package/dist/managers/command/command.manager.d.ts +0 -43
  97. package/dist/managers/command/command.manager.events.d.ts +0 -36
  98. package/dist/managers/command/index.d.ts +0 -4
  99. package/src/builder/builder.ts +0 -308
  100. package/src/builder/builder.types.ts +0 -53
  101. package/src/builder/index.ts +0 -4
  102. package/src/client/fetch.client.ts +0 -9
  103. package/src/client/fetch.client.types.ts +0 -83
  104. package/src/command/command.types.ts +0 -273
  105. package/src/command/index.ts +0 -3
  106. package/src/effect/fetch.effect.ts +0 -28
  107. package/src/effect/fetch.effect.types.ts +0 -35
  108. package/src/managers/command/command.manager.types.ts +0 -25
  109. package/src/managers/command/index.ts +0 -4
@@ -1,15 +1,15 @@
1
- import { getErrorMessage, ClientResponseSuccessType, ClientResponseErrorType, ProgressRequestDataType } from "client";
2
- import { CommandInstance, getProgressData, ClientProgressEvent } from "command";
3
- import { ExtractResponse, ExtractError } from "types";
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 getClientBindings = async <ConfigType = any>(cmd: CommandInstance, requestId: string) => {
6
- const { url, commandManager, loggerManager, headerMapper, payloadMapper } = cmd.builder;
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("Client");
8
+ const logger = loggerManager.init("Adapter");
9
9
 
10
10
  let requestStartTimestamp: null | number = null;
11
11
  let responseStartTimestamp: null | number = null;
12
- let command = cmd;
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 command middleware callbacks`);
21
+ logger.debug(`Starting request middleware callbacks`);
22
22
 
23
- command = await command.builder.__modifyRequest(cmd);
23
+ request = await request.client.__modifyRequest(cmd);
24
24
 
25
- if (command.auth) {
26
- command = await command.builder.__modifyAuth(cmd);
25
+ if (request.auth) {
26
+ request = await request.client.__modifyAuth(cmd);
27
27
  }
28
28
 
29
- // Command Setup
30
- const { builder, abortKey, queueKey, endpoint, data } = command;
29
+ // Request Setup
30
+ const { client, abortKey, queueKey, endpoint, data } = request;
31
31
 
32
- commandManager.addAbortController(abortKey, requestId);
32
+ requestManager.addAbortController(abortKey, requestId);
33
33
 
34
34
  const fullUrl = url + endpoint;
35
- const effects = builder.effects.filter((effect) => command.effectKey === effect.getEffectKey());
36
- const headers = headerMapper(command);
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 = { ...command.commandOptions.options };
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?: ProgressRequestDataType) => {
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: ClientProgressEvent,
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
- commandManager.events.emitUploadProgress(queueKey, requestId, progress, { requestId, command });
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: ClientProgressEvent,
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
- commandManager.events.emitDownloadProgress(queueKey, requestId, progress, { requestId, command });
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(command));
86
+ effects.forEach((effect) => effect.onTrigger(request));
87
87
  };
88
88
 
89
89
  // Request
90
90
 
91
- const onRequestStart = (progress?: ProgressRequestDataType) => {
92
- effects.forEach((action) => action.onStart(command));
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
- commandManager.events.emitRequestStart(queueKey, requestId, { requestId, command });
104
+ requestManager.events.emitRequestStart(queueKey, requestId, { requestId, request });
105
105
  return requestStartTimestamp;
106
106
  };
107
107
 
108
- const onRequestProgress = (progress: ProgressRequestDataType) => {
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?: ProgressRequestDataType) => {
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
- commandManager.events.emitResponseStart(queueKey, requestId, { requestId, command });
149
+ requestManager.events.emitResponseStart(queueKey, requestId, { requestId, request });
150
150
  return responseStartTimestamp;
151
151
  };
152
152
 
153
- const onResponseProgress = (progress: ProgressRequestDataType) => {
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
- commandManager.removeAbortController(abortKey, requestId);
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 CommandInstance>(
184
+ const onSuccess = async <T extends RequestInstance>(
185
185
  responseData: unknown,
186
186
  status: number,
187
- resolve: (value: ClientResponseErrorType<ExtractError<T>>) => void,
188
- ): Promise<ClientResponseSuccessType<ExtractResponse<T>>> => {
189
- let response = [responseData, null, status] as ClientResponseSuccessType<ExtractResponse<T>>;
190
- response = await command.builder.__modifyResponse(response, command);
191
- response = await command.builder.__modifySuccessResponse(response, command);
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, command));
194
- effects.forEach((effect) => effect.onFinished(response, command));
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 CommandInstance>(
204
- error: Error | ExtractError<T>,
203
+ const onError = async <T extends RequestInstance>(
204
+ error: Error | ExtractErrorType<T>,
205
205
  status: number,
206
- resolve: (value: ClientResponseErrorType<ExtractError<T>>) => void,
207
- ): Promise<ClientResponseErrorType<ExtractError<T>>> => {
208
- let responseData = [null, error, status] as ClientResponseErrorType<ExtractError<T>>;
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 command.builder.__modifyResponse(responseData, command);
211
- responseData = await command.builder.__modifyErrorResponse(responseData, command);
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, command));
214
- effects.forEach((effect) => effect.onFinished(responseData, command));
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 CommandInstance>(
222
- resolve: (value: ClientResponseErrorType<ExtractError<T>>) => void,
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 CommandInstance>(
229
- resolve: (value: ClientResponseErrorType<ExtractError<T>>) => void,
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 CommandInstance>(
236
- resolve: (value: ClientResponseErrorType<ExtractError<T>>) => void,
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 commandManager.getAbortController(abortKey, requestId);
245
+ return requestManager.getAbortController(abortKey, requestId);
246
246
  };
247
247
 
248
- const createAbortListener = <T extends CommandInstance>(
248
+ const createAbortListener = <T extends RequestInstance>(
249
249
  callback: () => void,
250
- resolve: (value: ClientResponseErrorType<ExtractError<T>>) => void,
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
- commandManager.events.emitAbort(abortKey, requestId, command);
260
+ requestManager.events.emitAbort(abortKey, requestId, request);
261
261
  };
262
262
 
263
263
  controller.signal.addEventListener("abort", fn);
@@ -1,7 +1,7 @@
1
- import { getClientBindings, defaultTimeout, ClientResponseType, ClientType } from "client";
2
- import { parseErrorResponse, parseResponse } from "./fetch.client.utils";
1
+ import { getAdapterBindings, defaultTimeout, ResponseType, AdapterType } from "adapter";
2
+ import { parseErrorResponse, parseResponse } from "./adapter.utils";
3
3
 
4
- export const browserClient: ClientType = async (command, requestId) => {
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 getClientBindings(command, requestId);
21
+ } = await getAdapterBindings(request, requestId);
22
22
 
23
- const { method } = command;
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<ClientResponseType<unknown, unknown>>((resolve) => {
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;
@@ -1,10 +1,15 @@
1
- import http from "http";
2
- import stream from "stream";
1
+ import { getAdapterBindings, defaultTimeout, ResponseType, AdapterType, AdapterOptionsType } from "adapter";
2
+ import { parseErrorResponse, parseResponse, getUploadSize, getStreamPayload } from "./adapter.utils";
3
+
4
+ export const serverAdapter: AdapterType = async (request, requestId) => {
5
+ /**
6
+ * Prevent issues related to the missing Node.js polyfills
7
+ */
8
+ // eslint-disable-next-line global-require, @typescript-eslint/no-var-requires
9
+ const http = require("http");
10
+ // eslint-disable-next-line global-require, @typescript-eslint/no-var-requires
11
+ const stream = require("stream");
3
12
 
4
- import { getClientBindings, defaultTimeout, ClientResponseType, ClientType, ClientDefaultOptionsType } from "client";
5
- import { parseErrorResponse, parseResponse, getUploadSize, getStreamPayload } from "./fetch.client.utils";
6
-
7
- export const serverClient: ClientType = async (command, requestId) => {
8
13
  const {
9
14
  fullUrl,
10
15
  headers,
@@ -21,16 +26,15 @@ export const serverClient: ClientType = async (command, requestId) => {
21
26
  onTimeoutError,
22
27
  onError,
23
28
  onResponseEnd,
24
- } = await getClientBindings<ClientDefaultOptionsType>(command, requestId);
25
-
26
- const { method } = command;
29
+ } = await getAdapterBindings<AdapterOptionsType>(request, requestId);
30
+ const { method } = request;
27
31
 
28
- return new Promise<ClientResponseType<unknown, unknown>>((resolve) => {
32
+ return new Promise<ResponseType<unknown, unknown>>((resolve) => {
29
33
  const execute = async () => {
30
- const options: http.RequestOptions = {
34
+ const options = {
31
35
  path: fullUrl,
32
36
  method,
33
- headers: headers as http.RequestOptions["headers"],
37
+ headers,
34
38
  timeout: defaultTimeout,
35
39
  };
36
40
 
@@ -47,7 +51,7 @@ export const serverClient: ClientType = async (command, requestId) => {
47
51
 
48
52
  const payloadChunks = await getStreamPayload(payload);
49
53
 
50
- const request = http.request(options, (response) => {
54
+ const httpRequest = http.request(options, (response) => {
51
55
  response.setEncoding("utf8");
52
56
 
53
57
  let chunks = "";
@@ -79,10 +83,10 @@ export const serverClient: ClientType = async (command, requestId) => {
79
83
  });
80
84
  });
81
85
 
82
- unmountListener = createAbortListener(request.destroy, resolve);
86
+ unmountListener = createAbortListener(httpRequest.destroy, resolve);
83
87
 
84
- request.on("timeout", () => onTimeoutError(resolve));
85
- request.on("error", (error) => onError(error, 0, resolve));
88
+ httpRequest.on("timeout", () => onTimeoutError(resolve));
89
+ httpRequest.on("error", (error) => onError(error, 0, resolve));
86
90
 
87
91
  if (payloadChunks) {
88
92
  const readableStream = stream.Readable.from(payloadChunks, { objectMode: false })
@@ -95,9 +99,9 @@ export const serverClient: ClientType = async (command, requestId) => {
95
99
  onRequestEnd();
96
100
  });
97
101
 
98
- readableStream.pipe(request);
102
+ readableStream.pipe(httpRequest);
99
103
  } else {
100
- request.end();
104
+ httpRequest.end();
101
105
  }
102
106
  };
103
107
  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 { CommandInstance } from "command";
2
- import { ExtractError } from "types";
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 CommandInstance>(response: unknown): ExtractError<T> => {
26
+ export const parseErrorResponse = <T extends RequestInstance>(response: unknown): ExtractErrorType<T> => {
27
27
  return response ? parseResponse(response) : getErrorMessage();
28
28
  };
29
29
 
@@ -0,0 +1,7 @@
1
+ export * from "./adapter";
2
+ export * from "./adapter.types";
3
+ export * from "./adapter.utils";
4
+ export * from "./adapter.server";
5
+ export * from "./adapter.browser";
6
+ export * from "./adapter.bindings";
7
+ export * from "./adapter.constants";
@@ -1,8 +1,8 @@
1
1
  import EventEmitter from "events";
2
2
 
3
- import { ClientResponseType } from "client";
4
- import { BuilderInstance } from "builder";
5
- import { CommandResponseDetails, LoggerType } from "managers";
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 { CommandDump, CommandInstance } from "command";
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 Command class
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 builder: BuilderInstance, public options?: CacheOptionsType) {
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.builder.loggerManager.init("Cache");
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.builder.appManager.events.onOnline(() => {
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 command
56
+ * @param request
57
57
  * @param response
58
58
  * @param details
59
59
  * @returns
60
60
  */
61
61
  set = <Response, Error>(
62
- command: CommandInstance | CommandDump<CommandInstance>,
63
- response: ClientResponseType<Response, Error>,
64
- details: CommandResponseDetails,
62
+ request: RequestInstance | RequestDump<RequestInstance>,
63
+ response: ResponseType<Response, Error>,
64
+ details: ResponseDetailsType,
65
65
  ): void => {
66
- this.logger.debug("Processing cache response", { command, response, details });
67
- const { cacheKey, cache, cacheTime, garbageCollection } = command;
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", { command, response, details });
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", { command, response, details });
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", { command, response, details });
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.builder.appManager.isOnline) {
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.builder.appManager.isOnline) {
218
+ } else if (this.client.appManager.isOnline) {
219
219
  this.logger.info("Garbage collecting cache element", { cacheKey });
220
220
  this.delete(cacheKey);
221
221
  }
@@ -1,6 +1,6 @@
1
1
  import { Cache } from "cache";
2
- import { ClientResponseType } from "client";
3
- import { CommandResponseDetails } from "managers";
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: ClientResponseType<Response, Error>;
35
- details: CommandResponseDetails;
34
+ data: ResponseType<Response, Error>;
35
+ details: ResponseDetailsType;
36
36
  cacheTime: number;
37
37
  clearKey: string;
38
38
  garbageCollection: number;
@@ -1,11 +1,11 @@
1
- import { CommandInstance } from "command";
1
+ import { RequestInstance } from "request";
2
2
  import { isFailedRequest } from "dispatcher";
3
- import { ExtractClientReturnType } from "types";
3
+ import { ExtractAdapterReturnType } from "types";
4
4
 
5
- export const getCacheData = <T extends CommandInstance>(
6
- previousResponse: ExtractClientReturnType<T> | undefined,
7
- response: ExtractClientReturnType<T>,
8
- ): ExtractClientReturnType<T> => {
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;