@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,7 +1,7 @@
1
1
  import EventEmitter from "events";
2
2
 
3
3
  import {
4
- DispatcherData,
4
+ QueueDataType,
5
5
  getRequestType,
6
6
  isFailedRequest,
7
7
  canRetryRequest,
@@ -9,38 +9,38 @@ import {
9
9
  DispatcherRequestType,
10
10
  DispatcherOptionsType,
11
11
  DispatcherStorageType,
12
- DispatcherDumpValueType,
12
+ DispatcherStorageValueType,
13
13
  RunningRequestValueType,
14
14
  } from "dispatcher";
15
- import { BuilderInstance } from "builder";
15
+ import { ClientInstance } from "client";
16
16
  import { getUniqueRequestId } from "utils";
17
- import { CommandResponseDetails, LoggerType } from "managers";
18
- import { CommandInstance, Command } from "command";
19
- import { getErrorMessage } from "client";
17
+ import { ResponseDetailsType, LoggerType } from "managers";
18
+ import { RequestInstance, Request } from "request";
19
+ import { getErrorMessage } from "adapter";
20
20
 
21
21
  /**
22
- * Dispatcher class was made to store controlled request Fetches, and firing them all-at-once or one-by-one in command queue.
22
+ * Dispatcher class was made to store controlled request Fetches, and firing them all-at-once or one-by-one in request queue.
23
23
  * Generally requests should be flushed at the same time, the queue provide mechanism to fire them in the order.
24
24
  */
25
25
  export class Dispatcher {
26
26
  public emitter = new EventEmitter();
27
27
  public events = getDispatcherEvents(this.emitter);
28
- public storage: DispatcherStorageType = new Map<string, DispatcherData<CommandInstance>>();
28
+ public storage: DispatcherStorageType = new Map<string, QueueDataType<RequestInstance>>();
29
29
 
30
30
  private requestCount = new Map<string, number>();
31
31
  private runningRequests = new Map<string, RunningRequestValueType[]>();
32
32
 
33
33
  private logger: LoggerType;
34
34
 
35
- constructor(public builder: BuilderInstance, public options?: DispatcherOptionsType) {
36
- this.logger = builder.loggerManager.init("Dispatcher");
35
+ constructor(public client: ClientInstance, public options?: DispatcherOptionsType) {
36
+ this.logger = client.loggerManager.init("Dispatcher");
37
37
 
38
38
  if (this.options?.storage) {
39
39
  this.storage = this.options.storage;
40
40
  }
41
41
 
42
42
  // Going back from offline should re-trigger all requests
43
- this.builder.appManager.events.onOnline(() => {
43
+ this.client.appManager.events.onOnline(() => {
44
44
  this.flush();
45
45
  });
46
46
 
@@ -104,9 +104,9 @@ export class Dispatcher {
104
104
  /**
105
105
  * Return queue state object
106
106
  */
107
- getQueue = <Command extends CommandInstance = CommandInstance>(queueKey: string) => {
108
- const initialQueueState: DispatcherData<Command> = { requests: [], stopped: false };
109
- const storedEntity = this.storage.get<Command>(queueKey);
107
+ getQueue = <Request extends RequestInstance = RequestInstance>(queueKey: string) => {
108
+ const initialQueueState: QueueDataType<Request> = { requests: [], stopped: false };
109
+ const storedEntity = this.storage.get<Request>(queueKey);
110
110
 
111
111
  return storedEntity || initialQueueState;
112
112
  };
@@ -114,9 +114,9 @@ export class Dispatcher {
114
114
  /**
115
115
  * Return request from queue state
116
116
  */
117
- getRequest = <Command extends CommandInstance = CommandInstance>(queueKey: string, requestId: string) => {
118
- const initialQueueState: DispatcherData<Command> = { requests: [], stopped: false };
119
- const storedEntity = this.storage.get<Command>(queueKey) || initialQueueState;
117
+ getRequest = <Request extends RequestInstance = RequestInstance>(queueKey: string, requestId: string) => {
118
+ const initialQueueState: QueueDataType<Request> = { requests: [], stopped: false };
119
+ const storedEntity = this.storage.get<Request>(queueKey) || initialQueueState;
120
120
 
121
121
  return storedEntity.requests.find((req) => req.requestId === requestId);
122
122
  };
@@ -134,20 +134,20 @@ export class Dispatcher {
134
134
  /**
135
135
  * Add new element to storage
136
136
  */
137
- addQueueElement = <Command extends CommandInstance = CommandInstance>(
137
+ addQueueElement = <Request extends RequestInstance = RequestInstance>(
138
138
  queueKey: string,
139
- dispatcherDump: DispatcherDumpValueType<Command>,
139
+ dispatcherDump: DispatcherStorageValueType<Request>,
140
140
  ) => {
141
- const queue = this.getQueue<Command>(queueKey);
141
+ const queue = this.getQueue<Request>(queueKey);
142
142
  queue.requests.push(dispatcherDump);
143
- this.setQueue<Command>(queueKey, queue);
143
+ this.setQueue<Request>(queueKey, queue);
144
144
  };
145
145
 
146
146
  /**
147
147
  * Set new queue storage value
148
148
  */
149
- setQueue = <Command extends CommandInstance = CommandInstance>(queueKey: string, queue: DispatcherData<Command>) => {
150
- this.storage.set<Command>(queueKey, queue);
149
+ setQueue = <Request extends RequestInstance = RequestInstance>(queueKey: string, queue: QueueDataType<Request>) => {
150
+ this.storage.set<Request>(queueKey, queue);
151
151
 
152
152
  // Emit Queue Changes
153
153
  this.options?.onUpdateStorage?.(queueKey, queue);
@@ -180,8 +180,8 @@ export class Dispatcher {
180
180
  const queueElement = queue.requests.find((request) => !request.stopped);
181
181
 
182
182
  const isStopped = queue && queue.stopped;
183
- const isOffline = !this.builder.appManager.isOnline;
184
- const isConcurrent = !queueElement?.commandDump.queued;
183
+ const isOffline = !this.client.appManager.isOnline;
184
+ const isConcurrent = !queueElement?.requestDump.queued;
185
185
  const isInactive = !runningRequests.length;
186
186
  const isEmpty = !queueElement;
187
187
 
@@ -295,9 +295,9 @@ export class Dispatcher {
295
295
  /**
296
296
  * Add request to the running requests list
297
297
  */
298
- addRunningRequest = (queueKey: string, requestId: string, command: CommandInstance) => {
298
+ addRunningRequest = (queueKey: string, requestId: string, request: RequestInstance) => {
299
299
  const runningRequests = this.getRunningRequests(queueKey);
300
- runningRequests.push({ requestId, command });
300
+ runningRequests.push({ requestId, request });
301
301
  this.runningRequests.set(queueKey, runningRequests);
302
302
  };
303
303
 
@@ -321,7 +321,7 @@ export class Dispatcher {
321
321
  */
322
322
  cancelRunningRequests = (queueKey: string) => {
323
323
  this.runningRequests.get(queueKey)?.forEach((request) => {
324
- this.builder.commandManager.abortByRequestId(request.command.abortKey, request.requestId);
324
+ this.client.requestManager.abortByRequestId(request.request.abortKey, request.requestId);
325
325
  });
326
326
  this.deleteRunningRequests(queueKey);
327
327
  };
@@ -331,7 +331,7 @@ export class Dispatcher {
331
331
  cancelRunningRequest = (queueKey: string, requestId: string) => {
332
332
  const requests = this.getRunningRequests(queueKey).filter((request) => {
333
333
  if (request.requestId === requestId) {
334
- this.builder.commandManager.abortByRequestId(request.command.abortKey, request.requestId);
334
+ this.client.requestManager.abortByRequestId(request.request.abortKey, request.requestId);
335
335
  return false;
336
336
  }
337
337
  return true;
@@ -374,15 +374,15 @@ export class Dispatcher {
374
374
  };
375
375
 
376
376
  /**
377
- * Create storage element from command
377
+ * Create storage element from request
378
378
  */
379
379
  // eslint-disable-next-line class-methods-use-this
380
- createStorageElement = <Command extends CommandInstance>(command: Command) => {
381
- const requestId = getUniqueRequestId(command.queueKey);
382
- const storageElement: DispatcherDumpValueType<Command> = {
380
+ createStorageElement = <Request extends RequestInstance>(request: Request) => {
381
+ const requestId = getUniqueRequestId(request.queueKey);
382
+ const storageElement: DispatcherStorageValueType<Request> = {
383
383
  requestId,
384
384
  timestamp: +new Date(),
385
- commandDump: command.dump(),
385
+ requestDump: request.dump(),
386
386
  retries: 0,
387
387
  stopped: false,
388
388
  };
@@ -396,21 +396,21 @@ export class Dispatcher {
396
396
  // ********************
397
397
 
398
398
  /**
399
- * Add command to the dispatcher handler
399
+ * Add request to the dispatcher handler
400
400
  */
401
- add = (command: CommandInstance) => {
402
- const { queueKey } = command;
401
+ add = (request: RequestInstance) => {
402
+ const { queueKey } = request;
403
403
 
404
404
  // Create dump of the request to allow storing it in localStorage, AsyncStorage or any other
405
405
  // This way we don't save the Class but the instruction of the request to be done
406
- const storageElement = this.createStorageElement(command);
406
+ const storageElement = this.createStorageElement(request);
407
407
  const { requestId } = storageElement;
408
408
 
409
409
  const queue = this.getQueue(queueKey);
410
410
  const [latestRequest] = queue.requests.slice(-1);
411
- const requestType = getRequestType(command, latestRequest);
411
+ const requestType = getRequestType(request, latestRequest);
412
412
 
413
- this.logger.debug("Adding request to queue", { requestType, command, requestId });
413
+ this.logger.debug("Adding request to queue", { requestType, request, requestId });
414
414
 
415
415
  switch (requestType) {
416
416
  case DispatcherRequestType.oneByOne: {
@@ -445,24 +445,24 @@ export class Dispatcher {
445
445
  delete = (queueKey: string, requestId: string, abortKey: string) => {
446
446
  this.logger.debug("Deleting request", { queueKey, requestId, abortKey });
447
447
  const queue = this.getQueue(queueKey);
448
- const request = queue.requests.find((req) => req.requestId === requestId);
448
+ const existingRequest = queue.requests.find((req) => req.requestId === requestId);
449
449
 
450
- if (!request) return;
450
+ if (!existingRequest) return;
451
451
 
452
- const command = new Command(this.builder, request.commandDump.commandOptions, request.commandDump);
452
+ const request = new Request(this.client, existingRequest.requestDump.requestOptions, existingRequest.requestDump);
453
453
  queue.requests = queue.requests.filter((req) => req.requestId !== requestId);
454
454
  this.storage.set(queueKey, queue);
455
455
 
456
456
  // Clean controllers
457
457
  if (this.hasRunningRequest(queueKey, requestId)) {
458
458
  this.deleteRunningRequest(queueKey, requestId);
459
- this.builder.commandManager.abortByRequestId(abortKey, requestId);
459
+ this.client.requestManager.abortByRequestId(abortKey, requestId);
460
460
  }
461
461
 
462
462
  // Emit Queue Changes
463
463
  this.options?.onDeleteFromStorage?.(queueKey, queue);
464
464
  this.events.setQueueChanged(queueKey, queue);
465
- this.builder.commandManager.events.emitRemove(queueKey, requestId, { requestId, command });
465
+ this.client.requestManager.events.emitRemove(queueKey, requestId, { requestId, request });
466
466
 
467
467
  if (!queue.requests.length) {
468
468
  this.events.setDrained(queueKey, queue);
@@ -475,13 +475,13 @@ export class Dispatcher {
475
475
  * Request can run for some time, once it's done, we have to check if it's successful or if it was aborted
476
476
  * It can be different once the previous call was set as cancelled and removed from queue before this request got resolved
477
477
  */
478
- performRequest = async (storageElement: DispatcherDumpValueType) => {
479
- const command = new Command(this.builder, storageElement.commandDump.commandOptions, storageElement.commandDump);
480
- const { commandDump, requestId } = storageElement;
481
- this.logger.info("Performing request", { command, requestId });
478
+ performRequest = async (storageElement: DispatcherStorageValueType) => {
479
+ const request = new Request(this.client, storageElement.requestDump.requestOptions, storageElement.requestDump);
480
+ const { requestDump, requestId } = storageElement;
481
+ this.logger.info("Performing request", { request, requestId });
482
482
 
483
- const { retry, retryTime, queueKey, cacheKey, abortKey, offline } = commandDump;
484
- const { client, commandManager, cache, appManager } = this.builder;
483
+ const { retry, retryTime, queueKey, cacheKey, abortKey, offline } = requestDump;
484
+ const { adapter, requestManager, cache, appManager } = this.client;
485
485
 
486
486
  const canRetry = canRetryRequest(storageElement.retries, retry);
487
487
  // When offline not perform any request
@@ -495,10 +495,10 @@ export class Dispatcher {
495
495
  }
496
496
 
497
497
  // Additionally keep the running request to possibly abort it later
498
- this.addRunningRequest(queueKey, requestId, command);
498
+ this.addRunningRequest(queueKey, requestId, request);
499
499
 
500
500
  // Propagate the loading to all connected hooks
501
- commandManager.events.emitLoading(queueKey, requestId, {
501
+ requestManager.events.emitLoading(queueKey, requestId, {
502
502
  queueKey,
503
503
  requestId,
504
504
  loading: true,
@@ -509,7 +509,7 @@ export class Dispatcher {
509
509
  // Trigger Request
510
510
  this.incrementQueueRequestCount(queueKey);
511
511
 
512
- const response = await client(command, requestId);
512
+ const response = await adapter(request, requestId);
513
513
 
514
514
  // Do not continue the request handling when it got stopped and request was unsuccessful
515
515
  // Or when the request was aborted/canceled
@@ -523,7 +523,7 @@ export class Dispatcher {
523
523
  // Remove running request, must be called after isCancelled
524
524
  this.deleteRunningRequest(queueKey, requestId);
525
525
 
526
- const requestDetails: CommandResponseDetails = {
526
+ const requestDetails: ResponseDetailsType = {
527
527
  isFailed,
528
528
  isCanceled,
529
529
  isOffline: isOfflineResponseStatus,
@@ -532,41 +532,41 @@ export class Dispatcher {
532
532
  };
533
533
 
534
534
  // Turn off loading
535
- commandManager.events.emitLoading(queueKey, requestId, {
535
+ requestManager.events.emitLoading(queueKey, requestId, {
536
536
  queueKey,
537
537
  requestId,
538
538
  loading: false,
539
539
  isRetry: !!storageElement.retries,
540
540
  isOffline,
541
541
  });
542
- // Global response emitter to handle command execution
543
- commandManager.events.emitResponse(cacheKey, requestId, response, requestDetails);
542
+ // Global response emitter to handle request execution
543
+ requestManager.events.emitResponse(cacheKey, requestId, response, requestDetails);
544
544
  // Cache event to emit the data inside and store it
545
- cache.set(command, response, requestDetails);
546
- this.logger.info("Request finished", { requestId, command, response, requestDetails });
545
+ cache.set(request, response, requestDetails);
546
+ this.logger.info("Request finished", { requestId, request, response, requestDetails });
547
547
 
548
548
  // On cancelled
549
549
  if (isCanceled) {
550
550
  const queue = this.getQueue(queueKey);
551
- const request = queue.requests.find((req) => req.requestId === requestId);
551
+ const existingRequest = queue.requests.find((req) => req.requestId === requestId);
552
552
 
553
553
  // do not remove cancelled request as it may be result of manual queue pause
554
554
  // if abort was done without stop action we can remove request
555
- if (!queue.stopped && !request?.stopped) {
556
- this.logger.debug("Request paused", { response, requestDetails, command });
555
+ if (!queue.stopped && !existingRequest?.stopped) {
556
+ this.logger.debug("Request paused", { response, requestDetails, request });
557
557
  return this.delete(queueKey, requestId, abortKey);
558
558
  }
559
- return this.logger.debug("Request canceled", { response, requestDetails, command });
559
+ return this.logger.debug("Request canceled", { response, requestDetails, request });
560
560
  }
561
561
  // On offline
562
562
  if (isFailed && isOfflineResponseStatus) {
563
563
  // if we don't want to keep offline request - just delete them
564
564
  if (!offline) {
565
- this.logger.warning("Removing non-offline request", { response, requestDetails, command });
565
+ this.logger.warning("Removing non-offline request", { response, requestDetails, request });
566
566
  return this.delete(queueKey, requestId, abortKey);
567
567
  }
568
568
  // do not remove request from store as we want to re-send it later
569
- return this.logger.debug("Awaiting for network restoration", { response, requestDetails, command });
569
+ return this.logger.debug("Awaiting for network restoration", { response, requestDetails, request });
570
570
  }
571
571
  // On success
572
572
  if (!isFailed) {
@@ -574,12 +574,12 @@ export class Dispatcher {
574
574
  return this.logger.debug("Successful response, removing request from queue.", {
575
575
  response,
576
576
  requestDetails,
577
- command,
577
+ request,
578
578
  });
579
579
  }
580
580
  // On retry
581
581
  if (isFailed && canRetry) {
582
- this.logger.debug("Waiting for retry", { response, requestDetails, command });
582
+ this.logger.debug("Waiting for retry", { response, requestDetails, request });
583
583
  // Perform retry once request is failed
584
584
  setTimeout(() => {
585
585
  this.logger.warning("Error response, performing retry");
@@ -1,31 +1,31 @@
1
1
  import { Dispatcher } from "dispatcher";
2
- import { CommandDump, CommandInstance } from "command";
2
+ import { RequestDump, RequestInstance } from "request";
3
3
 
4
4
  export type DispatcherOptionsType = {
5
5
  storage?: DispatcherStorageType;
6
6
  onInitialization?: (dispatcherInstance: Dispatcher) => void;
7
- onUpdateStorage?: <Command extends CommandInstance>(queueKey: string, data: DispatcherData<Command>) => void;
8
- onDeleteFromStorage?: <Command extends CommandInstance>(queueKey: string, data: DispatcherData<Command>) => void;
7
+ onUpdateStorage?: <Request extends RequestInstance>(queueKey: string, data: QueueDataType<Request>) => void;
8
+ onDeleteFromStorage?: <Request extends RequestInstance>(queueKey: string, data: QueueDataType<Request>) => void;
9
9
  onClearStorage?: (dispatcherInstance: Dispatcher) => void;
10
10
  };
11
11
 
12
12
  // Values
13
- export type DispatcherDumpValueType<Command extends CommandInstance = CommandInstance> = {
13
+ export type DispatcherStorageValueType<Request extends RequestInstance = RequestInstance> = {
14
14
  requestId: string;
15
- commandDump: CommandDump<Command>;
15
+ requestDump: RequestDump<Request>;
16
16
  retries: number;
17
17
  timestamp: number;
18
18
  stopped: boolean;
19
19
  };
20
- export type DispatcherData<Command extends CommandInstance = CommandInstance> = {
21
- requests: DispatcherDumpValueType<Command>[];
20
+ export type QueueDataType<Request extends RequestInstance = RequestInstance> = {
21
+ requests: DispatcherStorageValueType<Request>[];
22
22
  stopped: boolean;
23
23
  };
24
24
 
25
25
  // Storage
26
26
  export type DispatcherStorageType = {
27
- set: <Command extends CommandInstance = CommandInstance>(key: string, data: DispatcherData<Command>) => void;
28
- get: <Command extends CommandInstance = CommandInstance>(key: string) => DispatcherData<Command> | undefined;
27
+ set: <Request extends RequestInstance = RequestInstance>(key: string, data: QueueDataType<Request>) => void;
28
+ get: <Request extends RequestInstance = RequestInstance>(key: string) => QueueDataType<Request> | undefined;
29
29
  keys: () => string[] | IterableIterator<string>;
30
30
  delete: (key: string) => void;
31
31
  clear: () => void;
@@ -35,5 +35,5 @@ export type DispatcherStorageType = {
35
35
 
36
36
  export type RunningRequestValueType = {
37
37
  requestId: string;
38
- command: CommandInstance;
38
+ request: RequestInstance;
39
39
  };
@@ -1,6 +1,6 @@
1
- import { CommandInstance } from "command";
2
- import { ClientResponseType } from "client";
3
- import { DispatcherRequestType, DispatcherDumpValueType } from "dispatcher";
1
+ import { RequestInstance } from "request";
2
+ import { ResponseType } from "adapter";
3
+ import { DispatcherRequestType, DispatcherStorageValueType } from "dispatcher";
4
4
 
5
5
  // Events
6
6
 
@@ -30,9 +30,9 @@ export const canRetryRequest = (currentRetries: number, retry: number | undefine
30
30
  return false;
31
31
  };
32
32
 
33
- export const getRequestType = (command: CommandInstance, latestRequest: DispatcherDumpValueType | undefined) => {
34
- const { queued, cancelable, deduplicate } = command;
35
- const canDeduplicate = latestRequest ? +new Date() - latestRequest.timestamp <= command.deduplicateTime : false;
33
+ export const getRequestType = (request: RequestInstance, latestRequest: DispatcherStorageValueType | undefined) => {
34
+ const { queued, cancelable, deduplicate } = request;
35
+ const canDeduplicate = latestRequest ? +new Date() - latestRequest.timestamp <= request.deduplicateTime : false;
36
36
 
37
37
  if (queued) {
38
38
  return DispatcherRequestType.oneByOne;
@@ -46,7 +46,7 @@ export const getRequestType = (command: CommandInstance, latestRequest: Dispatch
46
46
  return DispatcherRequestType.allAtOnce;
47
47
  };
48
48
 
49
- export const isFailedRequest = (data: ClientResponseType<unknown, unknown>) => {
49
+ export const isFailedRequest = (data: ResponseType<unknown, unknown>) => {
50
50
  const [, , status] = data;
51
51
  if (!status || status >= 400) {
52
52
  return true;
@@ -1,2 +1,2 @@
1
- export * from "./fetch.effect";
2
- export * from "./fetch.effect.types";
1
+ export * from "./request.effect";
2
+ export * from "./request.effect.types";
@@ -0,0 +1,28 @@
1
+ import { ExtractErrorType, ExtractResponseType } from "types";
2
+ import { RequestEffectOptionsType } from "effect";
3
+ import { RequestInstance } from "request";
4
+ import { ResponseErrorType, ResponseType, ResponseSuccessType } from "adapter";
5
+
6
+ export class RequestEffect<T extends RequestInstance> {
7
+ constructor(public config: RequestEffectOptionsType<T>) {}
8
+
9
+ getEffectKey = () => {
10
+ return this.config.effectKey;
11
+ };
12
+
13
+ onTrigger = (request: T) => {
14
+ this.config.onTrigger?.(request);
15
+ };
16
+ onStart = (request: T) => {
17
+ this.config.onStart?.(request);
18
+ };
19
+ onSuccess = (response: ResponseSuccessType<ExtractResponseType<T>>, request: T) => {
20
+ this.config.onSuccess?.(response, request);
21
+ };
22
+ onError = (response: ResponseErrorType<ExtractErrorType<T>>, request: T) => {
23
+ this.config.onError?.(response, request);
24
+ };
25
+ onFinished = (response: ResponseType<ExtractResponseType<T>, ExtractErrorType<T>>, request: T) => {
26
+ this.config.onFinished?.(response, request);
27
+ };
28
+ }
@@ -0,0 +1,35 @@
1
+ import { RequestInstance } from "request";
2
+ import { ResponseErrorType, ResponseType, ResponseSuccessType } from "adapter";
3
+ import { RequestEffect } from "effect";
4
+ import { ExtractErrorType, ExtractResponseType } from "types";
5
+
6
+ export type RequestEffectLifecycle = "trigger" | "start" | "success" | "error" | "finished";
7
+
8
+ export type RequestEffectInstance = RequestEffect<RequestInstance>;
9
+
10
+ export type RequestEffectOptionsType<T extends RequestInstance> = {
11
+ /**
12
+ * It should match effectKey on the request for which given effect should be triggered.
13
+ */
14
+ effectKey: string;
15
+ /**
16
+ * Callback that will be executed when request gets triggered
17
+ */
18
+ onTrigger?: (request: RequestInstance) => void;
19
+ /**
20
+ * Callback that will be executed when request starts
21
+ */
22
+ onStart?: (request: RequestInstance) => void;
23
+ /**
24
+ * Callback that will be executed when response is successful
25
+ */
26
+ onSuccess?: (response: ResponseSuccessType<ExtractResponseType<T>>, request: RequestInstance) => void;
27
+ /**
28
+ * Callback that will be executed when response is failed
29
+ */
30
+ onError?: (response: ResponseErrorType<ExtractErrorType<T>>, request: RequestInstance) => void;
31
+ /**
32
+ * Callback that will be executed when response is finished
33
+ */
34
+ onFinished?: (response: ResponseType<ExtractResponseType<T>, ExtractErrorType<T>>, request: RequestInstance) => void;
35
+ };
package/src/index.ts CHANGED
@@ -1,10 +1,10 @@
1
- // rollup builder entry point
1
+ // rollup client entry point
2
2
 
3
3
  export * from "./effect";
4
- export * from "./builder";
5
- export * from "./cache";
6
4
  export * from "./client";
7
- export * from "./command";
5
+ export * from "./cache";
6
+ export * from "./adapter";
7
+ export * from "./request";
8
8
  export * from "./constants/time.constants";
9
9
  export * from "./constants/http.constants";
10
10
  export * from "./managers";
@@ -1,3 +1,3 @@
1
1
  export * from "./app";
2
- export * from "./command";
2
+ export * from "./request";
3
3
  export * from "./logger";
@@ -1,12 +1,12 @@
1
1
  import EventEmitter from "events";
2
2
 
3
- import { BuilderInstance } from "builder";
3
+ import { ClientInstance } from "client";
4
4
  import { logger, LoggerType, SeverityType, LoggerMessageType, LoggerOptionsType, LoggerFunctionType } from "managers";
5
5
 
6
6
  /**
7
- * This class is used across the Hyper Fetch library to provide unified logging system with necessary setup per each builder.
7
+ * This class is used across the Hyper Fetch library to provide unified logging system with necessary setup per each client.
8
8
  * We can set up the logging level based on available values. This manager enable to initialize the logging instance per individual module
9
- * like Builder, Command etc. Which can give you better feedback on the logging itself.
9
+ * like Client, Request etc. Which can give you better feedback on the logging itself.
10
10
  */
11
11
  export class LoggerManager {
12
12
  logger: LoggerFunctionType;
@@ -14,7 +14,7 @@ export class LoggerManager {
14
14
 
15
15
  public emitter = new EventEmitter();
16
16
 
17
- constructor(private builder: Pick<BuilderInstance, "debug">, private options?: LoggerOptionsType) {
17
+ constructor(private client: Pick<ClientInstance, "debug">, private options?: LoggerOptionsType) {
18
18
  this.logger = this.options?.logger || logger;
19
19
  this.severity = this.options?.severity || 2;
20
20
  }
@@ -32,7 +32,7 @@ export class LoggerManager {
32
32
  message,
33
33
  additionalData,
34
34
  severity: this.severity,
35
- enabled: this.builder.debug,
35
+ enabled: this.client.debug,
36
36
  });
37
37
  },
38
38
  warning: (message: LoggerMessageType, ...additionalData: LoggerMessageType[]) => {
@@ -42,7 +42,7 @@ export class LoggerManager {
42
42
  message,
43
43
  additionalData,
44
44
  severity: this.severity,
45
- enabled: this.builder.debug,
45
+ enabled: this.client.debug,
46
46
  });
47
47
  },
48
48
  info: (message: LoggerMessageType, ...additionalData: LoggerMessageType[]) => {
@@ -52,7 +52,7 @@ export class LoggerManager {
52
52
  message,
53
53
  additionalData,
54
54
  severity: this.severity,
55
- enabled: this.builder.debug,
55
+ enabled: this.client.debug,
56
56
  });
57
57
  },
58
58
  debug: (message: LoggerMessageType, ...additionalData: LoggerMessageType[]) => {
@@ -62,7 +62,7 @@ export class LoggerManager {
62
62
  message,
63
63
  additionalData,
64
64
  severity: this.severity,
65
- enabled: this.builder.debug,
65
+ enabled: this.client.debug,
66
66
  });
67
67
  },
68
68
  };
@@ -1,6 +1,6 @@
1
- import { ClientResponseType } from "client";
2
- import { CommandInstance } from "command";
3
- import { CommandResponseDetails } from "managers/command";
1
+ import { ResponseType } from "adapter";
2
+ import { RequestInstance } from "request";
3
+ import { ResponseDetailsType } from "managers/request";
4
4
 
5
5
  // Logger
6
6
  export type SeverityType = 0 | 1 | 2 | 3;
@@ -26,10 +26,10 @@ export type LoggerMessageType = string | Record<string, unknown> | Array<unknown
26
26
 
27
27
  // Events
28
28
 
29
- export type LoggerRequestEventData = { requestId: string; command: CommandInstance };
29
+ export type LoggerRequestEventData = { requestId: string; request: RequestInstance };
30
30
  export type LoggerResponseEventData = {
31
31
  requestId: string;
32
- command: CommandInstance;
33
- response: ClientResponseType<unknown, unknown>;
34
- details: CommandResponseDetails;
32
+ request: RequestInstance;
33
+ response: ResponseType<unknown, unknown>;
34
+ details: ResponseDetailsType;
35
35
  };
@@ -0,0 +1,4 @@
1
+ export * from "./request.manager";
2
+ export * from "./request.manager.utils";
3
+ export * from "./request.manager.events";
4
+ export * from "./request.manager.types";