@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.
Files changed (109) hide show
  1. package/README.md +14 -13
  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} +12 -12
  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,32 +0,0 @@
1
- import { CommandInstance } from "command";
2
- import { ClientResponseErrorType, ClientResponseType, ClientResponseSuccessType } from "client";
3
- import { FetchEffect } from "effect";
4
- import { ExtractError } from "types";
5
- export type FetchEffectLifecycle = "trigger" | "start" | "success" | "error" | "finished";
6
- export type FetchEffectInstance = FetchEffect<CommandInstance>;
7
- export type FetchEffectConfig<T extends CommandInstance> = {
8
- /**
9
- * It should match effectKey on the command for which given effect should be triggered.
10
- */
11
- effectKey: string;
12
- /**
13
- * Callback that will be executed when request gets triggered
14
- */
15
- onTrigger?: (command: CommandInstance) => void;
16
- /**
17
- * Callback that will be executed when request starts
18
- */
19
- onStart?: (command: CommandInstance) => void;
20
- /**
21
- * Callback that will be executed when response is successful
22
- */
23
- onSuccess?: (response: ClientResponseSuccessType<ResponseType>, command: CommandInstance) => void;
24
- /**
25
- * Callback that will be executed when response is failed
26
- */
27
- onError?: (response: ClientResponseErrorType<ExtractError<T>>, command: CommandInstance) => void;
28
- /**
29
- * Callback that will be executed when response is finished
30
- */
31
- onFinished?: (response: ClientResponseType<ResponseType, ExtractError<T>>, command: CommandInstance) => void;
32
- };
@@ -1,43 +0,0 @@
1
- /// <reference types="node" />
2
- import EventEmitter from "events";
3
- /**
4
- * **Command Manager** is used to emit `command lifecycle events` like - request start, request end, upload and download progress.
5
- * It is also the place of `request aborting` system, here we store all the keys and controllers that are isolated for each builder instance.
6
- */
7
- export declare class CommandManager {
8
- emitter: EventEmitter;
9
- events: {
10
- emitLoading: (queueKey: string, requestId: string, values: import("managers").CommandLoadingEventType) => void;
11
- emitRequestStart: (queueKey: string, requestId: string, details: import("managers").CommandEventDetails<import("../..").CommandInstance>) => void;
12
- emitResponseStart: (queueKey: string, requestId: string, details: import("managers").CommandEventDetails<import("../..").CommandInstance>) => void;
13
- emitUploadProgress: (queueKey: string, requestId: string, values: import("../..").FetchProgressType, details: import("managers").CommandEventDetails<import("../..").CommandInstance>) => void;
14
- emitDownloadProgress: (queueKey: string, requestId: string, values: import("../..").FetchProgressType, details: import("managers").CommandEventDetails<import("../..").CommandInstance>) => void;
15
- emitResponse: (cacheKey: string, requestId: string, response: import("../..").ClientResponseType<unknown, unknown>, details: import("managers").CommandResponseDetails) => void;
16
- emitAbort: (abortKey: string, requestId: string, command: import("../..").CommandInstance) => void;
17
- emitRemove: <T extends import("../..").CommandInstance>(queueKey: string, requestId: string, details: import("managers").CommandEventDetails<T>) => void;
18
- onLoading: (queueKey: string, callback: (values: import("managers").CommandLoadingEventType) => void) => VoidFunction;
19
- onLoadingById: (requestId: string, callback: (values: import("managers").CommandLoadingEventType) => void) => VoidFunction;
20
- onRequestStart: <T_1 extends import("../..").CommandInstance>(queueKey: string, callback: (details: import("managers").CommandEventDetails<T_1>) => void) => VoidFunction;
21
- onRequestStartById: <T_2 extends import("../..").CommandInstance>(requestId: string, callback: (details: import("managers").CommandEventDetails<T_2>) => void) => VoidFunction;
22
- onResponseStart: <T_3 extends import("../..").CommandInstance>(queueKey: string, callback: (details: import("managers").CommandEventDetails<T_3>) => void) => VoidFunction;
23
- onResponseStartById: <T_4 extends import("../..").CommandInstance>(requestId: string, callback: (details: import("managers").CommandEventDetails<T_4>) => void) => VoidFunction;
24
- onUploadProgress: <T_5 extends import("../..").CommandInstance = import("../..").CommandInstance>(queueKey: string, callback: (values: import("../..").FetchProgressType, details: import("managers").CommandEventDetails<T_5>) => void) => VoidFunction;
25
- onUploadProgressById: <T_6 extends import("../..").CommandInstance = import("../..").CommandInstance>(requestId: string, callback: (values: import("../..").FetchProgressType, details: import("managers").CommandEventDetails<T_6>) => void) => VoidFunction;
26
- onDownloadProgress: <T_7 extends import("../..").CommandInstance = import("../..").CommandInstance>(queueKey: string, callback: (values: import("../..").FetchProgressType, details: import("managers").CommandEventDetails<T_7>) => void) => VoidFunction;
27
- onDownloadProgressById: <T_8 extends import("../..").CommandInstance = import("../..").CommandInstance>(requestId: string, callback: (values: import("../..").FetchProgressType, details: import("managers").CommandEventDetails<T_8>) => void) => VoidFunction;
28
- onResponse: <ResponseType_1, ErrorType>(cacheKey: string, callback: (response: import("../..").ClientResponseType<ResponseType_1, ErrorType>, details: import("managers").CommandResponseDetails) => void) => VoidFunction;
29
- onResponseById: <ResponseType_2, ErrorType_1>(requestId: string, callback: (response: import("../..").ClientResponseType<ResponseType_2, ErrorType_1>, details: import("managers").CommandResponseDetails) => void) => VoidFunction;
30
- onAbort: (abortKey: string, callback: (command: import("../..").CommandInstance) => void) => VoidFunction;
31
- onAbortById: (requestId: string, callback: (command: import("../..").CommandInstance) => void) => VoidFunction;
32
- onRemove: <T_9 extends import("../..").CommandInstance = import("../..").CommandInstance>(queueKey: string, callback: (details: import("managers").CommandEventDetails<T_9>) => void) => VoidFunction;
33
- onRemoveById: <T_10 extends import("../..").CommandInstance = import("../..").CommandInstance>(requestId: string, callback: (details: import("managers").CommandEventDetails<T_10>) => void) => VoidFunction;
34
- };
35
- abortControllers: Map<string, Map<string, AbortController>>;
36
- addAbortController: (abortKey: string, requestId: string) => void;
37
- getAbortController: (abortKey: string, requestId: string) => AbortController;
38
- removeAbortController: (abortKey: string, requestId: string) => void;
39
- useAbortController: (abortKey: string, requestId: string) => void;
40
- abortByKey: (abortKey: string) => void;
41
- abortByRequestId: (abortKey: string, requestId: string) => void;
42
- abortAll: () => void;
43
- }
@@ -1,36 +0,0 @@
1
- import EventEmitter from "events";
2
- import { CommandEventDetails, CommandResponseDetails, CommandLoadingEventType } from "managers";
3
- import { FetchProgressType, ClientResponseType } from "client";
4
- import { CommandInstance } from "command";
5
- export declare const getCommandManagerEvents: (emitter: EventEmitter) => {
6
- /**
7
- * Emiter
8
- */
9
- emitLoading: (queueKey: string, requestId: string, values: CommandLoadingEventType) => void;
10
- emitRequestStart: (queueKey: string, requestId: string, details: CommandEventDetails<CommandInstance>) => void;
11
- emitResponseStart: (queueKey: string, requestId: string, details: CommandEventDetails<CommandInstance>) => void;
12
- emitUploadProgress: (queueKey: string, requestId: string, values: FetchProgressType, details: CommandEventDetails<CommandInstance>) => void;
13
- emitDownloadProgress: (queueKey: string, requestId: string, values: FetchProgressType, details: CommandEventDetails<CommandInstance>) => void;
14
- emitResponse: (cacheKey: string, requestId: string, response: ClientResponseType<unknown, unknown>, details: CommandResponseDetails) => void;
15
- emitAbort: (abortKey: string, requestId: string, command: CommandInstance) => void;
16
- emitRemove: <T extends CommandInstance>(queueKey: string, requestId: string, details: CommandEventDetails<T>) => void;
17
- /**
18
- * Listeners
19
- */
20
- onLoading: (queueKey: string, callback: (values: CommandLoadingEventType) => void) => VoidFunction;
21
- onLoadingById: (requestId: string, callback: (values: CommandLoadingEventType) => void) => VoidFunction;
22
- onRequestStart: <T_1 extends CommandInstance>(queueKey: string, callback: (details: CommandEventDetails<T_1>) => void) => VoidFunction;
23
- onRequestStartById: <T_2 extends CommandInstance>(requestId: string, callback: (details: CommandEventDetails<T_2>) => void) => VoidFunction;
24
- onResponseStart: <T_3 extends CommandInstance>(queueKey: string, callback: (details: CommandEventDetails<T_3>) => void) => VoidFunction;
25
- onResponseStartById: <T_4 extends CommandInstance>(requestId: string, callback: (details: CommandEventDetails<T_4>) => void) => VoidFunction;
26
- onUploadProgress: <T_5 extends CommandInstance = CommandInstance>(queueKey: string, callback: (values: FetchProgressType, details: CommandEventDetails<T_5>) => void) => VoidFunction;
27
- onUploadProgressById: <T_6 extends CommandInstance = CommandInstance>(requestId: string, callback: (values: FetchProgressType, details: CommandEventDetails<T_6>) => void) => VoidFunction;
28
- onDownloadProgress: <T_7 extends CommandInstance = CommandInstance>(queueKey: string, callback: (values: FetchProgressType, details: CommandEventDetails<T_7>) => void) => VoidFunction;
29
- onDownloadProgressById: <T_8 extends CommandInstance = CommandInstance>(requestId: string, callback: (values: FetchProgressType, details: CommandEventDetails<T_8>) => void) => VoidFunction;
30
- onResponse: <ResponseType_1, ErrorType>(cacheKey: string, callback: (response: ClientResponseType<ResponseType_1, ErrorType>, details: CommandResponseDetails) => void) => VoidFunction;
31
- onResponseById: <ResponseType_2, ErrorType_1>(requestId: string, callback: (response: ClientResponseType<ResponseType_2, ErrorType_1>, details: CommandResponseDetails) => void) => VoidFunction;
32
- onAbort: (abortKey: string, callback: (command: CommandInstance) => void) => VoidFunction;
33
- onAbortById: (requestId: string, callback: (command: CommandInstance) => void) => VoidFunction;
34
- onRemove: <T_9 extends CommandInstance = CommandInstance>(queueKey: string, callback: (details: CommandEventDetails<T_9>) => void) => VoidFunction;
35
- onRemoveById: <T_10 extends CommandInstance = CommandInstance>(requestId: string, callback: (details: CommandEventDetails<T_10>) => void) => VoidFunction;
36
- };
@@ -1,4 +0,0 @@
1
- export * from "./command.manager";
2
- export * from "./command.manager.utils";
3
- export * from "./command.manager.events";
4
- export * from "./command.manager.types";
@@ -1,308 +0,0 @@
1
- import {
2
- ClientType,
3
- fetchClient,
4
- ClientDefaultOptionsType,
5
- ClientResponseType,
6
- ClientQueryParamsType,
7
- QueryStringifyOptions,
8
- ClientHeaderMappingCallback,
9
- ClientPayloadMappingCallback,
10
- } from "client";
11
- import {
12
- stringifyQueryParams,
13
- getClientHeaders,
14
- getClientPayload,
15
- BuilderConfig,
16
- BuilderInstance,
17
- BuilderErrorType,
18
- StringifyCallbackType,
19
- RequestInterceptorCallback,
20
- ResponseInterceptorCallback,
21
- } from "builder";
22
- import { Cache } from "cache";
23
- import { Dispatcher } from "dispatcher";
24
- import { FetchEffectInstance } from "effect";
25
- import { Command, CommandConfig, CommandInstance } from "command";
26
- import { AppManager, CommandManager, LoggerManager, SeverityType } from "managers";
27
- import { interceptRequest, interceptResponse } from "./builder.utils";
28
-
29
- /**
30
- * **Builder** is a class that allows you to configure the connection with the server and then use it to create
31
- * commands which, when called using the appropriate method, will cause the server to be queried for the endpoint and
32
- * method specified in the command.
33
- * @position 1
34
- */
35
- export class Builder<GlobalErrorType extends BuilderErrorType = Error, RequestConfigType = ClientDefaultOptionsType> {
36
- readonly url: string;
37
- public debug: boolean;
38
-
39
- // Private
40
- __onErrorCallbacks: ResponseInterceptorCallback[] = [];
41
- __onSuccessCallbacks: ResponseInterceptorCallback[] = [];
42
- __onResponseCallbacks: ResponseInterceptorCallback[] = [];
43
- __onAuthCallbacks: RequestInterceptorCallback[] = [];
44
- __onRequestCallbacks: RequestInterceptorCallback[] = [];
45
-
46
- // Managers
47
- commandManager: CommandManager = new CommandManager();
48
- appManager: AppManager;
49
- loggerManager: LoggerManager = new LoggerManager(this);
50
-
51
- // Config
52
- client: ClientType;
53
- cache: Cache;
54
- fetchDispatcher: Dispatcher;
55
- submitDispatcher: Dispatcher;
56
-
57
- // Registered requests effect
58
- effects: FetchEffectInstance[] = [];
59
-
60
- // Options
61
- requestConfig?: (command: CommandInstance) => RequestConfigType;
62
- commandConfig?: (
63
- commandOptions: CommandConfig<string, RequestConfigType>,
64
- ) => Partial<CommandConfig<string, RequestConfigType>>;
65
- queryParamsConfig?: QueryStringifyOptions;
66
-
67
- // Utils
68
-
69
- /**
70
- * Method to stringify query params from objects.
71
- */
72
- stringifyQueryParams: StringifyCallbackType = (queryParams) =>
73
- stringifyQueryParams(queryParams, this.queryParamsConfig);
74
- /**
75
- * Method to get default headers and to map them based on the data format exchange, by default it handles FormData / JSON formats.
76
- */
77
- headerMapper: ClientHeaderMappingCallback = getClientHeaders;
78
- /**
79
- * Method to get request data and transform them to the required format. It handles FormData and JSON by default.
80
- */
81
- payloadMapper: ClientPayloadMappingCallback = getClientPayload;
82
-
83
- // Logger
84
- logger = this.loggerManager.init("Builder");
85
-
86
- constructor(public options: BuilderConfig) {
87
- const { url, client, appManager, cache, fetchDispatcher, submitDispatcher } = this.options;
88
- this.url = url;
89
- this.client = client || fetchClient;
90
-
91
- // IMPORTANT: Do not change initialization order as it's crucial for dependencies injection
92
- this.appManager = appManager?.(this) || new AppManager();
93
- this.cache = cache?.(this) || new Cache(this);
94
- this.fetchDispatcher = fetchDispatcher?.(this) || new Dispatcher(this);
95
- this.submitDispatcher = submitDispatcher?.(this) || new Dispatcher(this);
96
- }
97
-
98
- /**
99
- * This method allows to configure global defaults for the command configuration like method, auth, deduplication etc.
100
- */
101
- setCommandConfig = (
102
- callback: (command: CommandInstance) => Partial<CommandConfig<string, RequestConfigType>>,
103
- ): Builder<GlobalErrorType, RequestConfigType> => {
104
- this.commandConfig = callback;
105
- return this;
106
- };
107
-
108
- /**
109
- * This method enables the logger usage and display the logs in console
110
- */
111
- setDebug = (debug: boolean): Builder<GlobalErrorType, RequestConfigType> => {
112
- this.debug = debug;
113
- return this;
114
- };
115
-
116
- /**
117
- * Set the logger severity of the messages displayed to the console
118
- */
119
- setLoggerSeverity = (severity: SeverityType): Builder<GlobalErrorType, RequestConfigType> => {
120
- this.loggerManager.setSeverity(severity);
121
- return this;
122
- };
123
-
124
- /**
125
- * Set the new logger instance to the builder
126
- */
127
- setLogger = (callback: (builder: BuilderInstance) => LoggerManager): Builder<GlobalErrorType, RequestConfigType> => {
128
- this.loggerManager = callback(this);
129
- return this;
130
- };
131
-
132
- /**
133
- * Set config for the query params stringify method, we can set here, among others, arrayFormat, skipNull, encode, skipEmptyString and more
134
- */
135
- setQueryParamsConfig = (queryParamsConfig: QueryStringifyOptions): Builder<GlobalErrorType, RequestConfigType> => {
136
- this.queryParamsConfig = queryParamsConfig;
137
- return this;
138
- };
139
-
140
- /**
141
- * Set the custom query params stringify method to the builder
142
- * @param stringifyFn Custom callback handling query params stringify
143
- */
144
- setStringifyQueryParams = (stringifyFn: StringifyCallbackType): Builder<GlobalErrorType, RequestConfigType> => {
145
- this.stringifyQueryParams = stringifyFn;
146
- return this;
147
- };
148
-
149
- /**
150
- * Set the custom header mapping function
151
- */
152
- setHeaderMapper = (headerMapper: ClientHeaderMappingCallback): Builder<GlobalErrorType, RequestConfigType> => {
153
- this.headerMapper = headerMapper;
154
- return this;
155
- };
156
-
157
- /**
158
- * Set the request payload mapping function which get triggered before request get send
159
- */
160
- setPayloadMapper = (payloadMapper: ClientPayloadMappingCallback): Builder<GlobalErrorType, RequestConfigType> => {
161
- this.payloadMapper = payloadMapper;
162
- return this;
163
- };
164
-
165
- /**
166
- * Set custom http client to handle graphql, rest, firebase or other
167
- */
168
- setClient = (callback: (builder: BuilderInstance) => ClientType): Builder<GlobalErrorType, RequestConfigType> => {
169
- this.client = callback(this);
170
- return this;
171
- };
172
-
173
- /**
174
- * Method of manipulating commands before sending the request. We can for example add custom header with token to the request which command had the auth set to true.
175
- */
176
- onAuth = (callback: RequestInterceptorCallback): Builder<GlobalErrorType, RequestConfigType> => {
177
- this.__onAuthCallbacks.push(callback);
178
- return this;
179
- };
180
-
181
- /**
182
- * Method for intercepting error responses. It can be used for example to refresh tokens.
183
- */
184
- onError = <ErrorType = null>(
185
- callback: ResponseInterceptorCallback<any, ErrorType | GlobalErrorType>,
186
- ): Builder<GlobalErrorType, RequestConfigType> => {
187
- this.__onErrorCallbacks.push(callback);
188
- return this;
189
- };
190
-
191
- /**
192
- * Method for intercepting success responses.
193
- */
194
- onSuccess = <ErrorType = null>(
195
- callback: ResponseInterceptorCallback<any, ErrorType | GlobalErrorType>,
196
- ): Builder<GlobalErrorType, RequestConfigType> => {
197
- this.__onSuccessCallbacks.push(callback);
198
- return this;
199
- };
200
-
201
- /**
202
- * Method of manipulating commands before sending the request.
203
- */
204
- onRequest = (callback: RequestInterceptorCallback): Builder<GlobalErrorType, RequestConfigType> => {
205
- this.__onRequestCallbacks.push(callback);
206
- return this;
207
- };
208
-
209
- /**
210
- * Method for intercepting any responses.
211
- */
212
- onResponse = <ErrorType = null>(
213
- callback: ResponseInterceptorCallback<any, ErrorType | GlobalErrorType>,
214
- ): Builder<GlobalErrorType, RequestConfigType> => {
215
- this.__onResponseCallbacks.push(callback);
216
- return this;
217
- };
218
-
219
- /**
220
- * Add persistent effects which trigger on the request lifecycle
221
- */
222
- addEffect = (effect: FetchEffectInstance | FetchEffectInstance[]) => {
223
- this.effects = this.effects.concat(effect);
224
-
225
- return this;
226
- };
227
-
228
- /**
229
- * Remove effects from builder
230
- */
231
- removeEffect = (effect: FetchEffectInstance | string) => {
232
- const name = typeof effect === "string" ? effect : effect.getEffectKey();
233
- this.effects = this.effects.filter((currentEffect) => currentEffect.getEffectKey() !== name);
234
-
235
- return this;
236
- };
237
-
238
- /**
239
- * Helper used by http client to apply the modifications on response error
240
- */
241
- __modifyAuth = async (command: CommandInstance) => interceptRequest(this.__onAuthCallbacks, command);
242
-
243
- /**
244
- * Private helper to run async pre-request processing
245
- */
246
- __modifyRequest = async (command: CommandInstance) => interceptRequest(this.__onRequestCallbacks, command);
247
-
248
- /**
249
- * Private helper to run async on-error response processing
250
- */
251
- __modifyErrorResponse = async (response: ClientResponseType<any, GlobalErrorType>, command: CommandInstance) =>
252
- interceptResponse<GlobalErrorType>(this.__onErrorCallbacks, response, command);
253
-
254
- /**
255
- * Private helper to run async on-success response processing
256
- */
257
- __modifySuccessResponse = async (response: ClientResponseType<any, GlobalErrorType>, command: CommandInstance) =>
258
- interceptResponse<GlobalErrorType>(this.__onSuccessCallbacks, response, command);
259
-
260
- /**
261
- * Private helper to run async response processing
262
- */
263
- __modifyResponse = async (response: ClientResponseType<any, GlobalErrorType>, command: CommandInstance) =>
264
- interceptResponse<GlobalErrorType>(this.__onResponseCallbacks, response, command);
265
-
266
- /**
267
- * Clears the builder instance and remove all listeners on it's dependencies
268
- */
269
- clear = () => {
270
- const { appManager, cache, fetchDispatcher, submitDispatcher } = this.options;
271
-
272
- this.commandManager.abortControllers.clear();
273
- this.fetchDispatcher.clear();
274
- this.submitDispatcher.clear();
275
- this.cache.clear();
276
-
277
- this.commandManager.emitter.removeAllListeners();
278
- this.fetchDispatcher.emitter.removeAllListeners();
279
- this.submitDispatcher.emitter.removeAllListeners();
280
- this.cache.emitter.removeAllListeners();
281
-
282
- this.appManager = appManager?.(this) || new AppManager();
283
- this.cache = cache?.(this) || new Cache(this);
284
- this.fetchDispatcher = fetchDispatcher?.(this) || new Dispatcher(this);
285
- this.submitDispatcher = submitDispatcher?.(this) || new Dispatcher(this);
286
- };
287
-
288
- /**
289
- * Create commands based on the builder setup
290
- */
291
- createCommand = <
292
- ResponseType,
293
- RequestDataType = undefined,
294
- LocalErrorType extends BuilderErrorType | undefined = undefined,
295
- QueryParamsType extends ClientQueryParamsType | string = ClientQueryParamsType | string,
296
- >() => {
297
- return <EndpointType extends string>(params: CommandConfig<EndpointType, RequestConfigType>) =>
298
- new Command<
299
- ResponseType,
300
- RequestDataType,
301
- QueryParamsType,
302
- GlobalErrorType,
303
- LocalErrorType,
304
- EndpointType,
305
- RequestConfigType
306
- >(this, params);
307
- };
308
- }
@@ -1,53 +0,0 @@
1
- import { Cache } from "cache";
2
- import { CommandInstance } from "command";
3
- import { Dispatcher } from "dispatcher";
4
- import { ClientResponseType, ClientType, ClientQueryParamsType } from "client";
5
- import { Builder } from "builder";
6
- import { AppManager } from "managers";
7
- import { NegativeTypes } from "types";
8
-
9
- /**
10
- * Configuration setup for the builder
11
- */
12
- export type BuilderConfig = {
13
- /**
14
- * Url to your server
15
- */
16
- url: string;
17
- /**
18
- * Custom client initialization prop
19
- */
20
- client?: ClientType;
21
- /**
22
- * Custom cache initialization prop
23
- */
24
- cache?: <B extends BuilderInstance, C extends Cache>(builder: B) => C;
25
- /**
26
- * Custom app manager initialization prop
27
- */
28
- appManager?: <B extends BuilderInstance, A extends AppManager>(builder: B) => A;
29
- /**
30
- * Custom fetch dispatcher initialization prop
31
- */
32
- fetchDispatcher?: <B extends BuilderInstance, D extends Dispatcher>(builder: B) => D;
33
- /**
34
- * Custom submit dispatcher initialization prop
35
- */
36
- submitDispatcher?: <B extends BuilderInstance, D extends Dispatcher>(builder: B) => D;
37
- };
38
-
39
- export type BuilderInstance = Builder<any, any>;
40
-
41
- export type BuilderErrorType = Record<string, any> | string;
42
-
43
- // Interceptors
44
-
45
- export type RequestInterceptorCallback = (command: CommandInstance) => Promise<CommandInstance> | CommandInstance;
46
- export type ResponseInterceptorCallback<Response = any, Error = any> = (
47
- response: ClientResponseType<Response, Error>,
48
- command: CommandInstance,
49
- ) => Promise<ClientResponseType<any, any>> | ClientResponseType<any, any>;
50
-
51
- // Stringify
52
-
53
- export type StringifyCallbackType = (queryParams: ClientQueryParamsType | string | NegativeTypes) => string;
@@ -1,4 +0,0 @@
1
- export * from "./builder";
2
- export * from "./builder.utils";
3
- export * from "./builder.types";
4
- export * from "./builder.constants";
@@ -1,9 +0,0 @@
1
- import { browserClient, ClientType } from "client";
2
- import { serverClient } from "./fetch.client.server";
3
-
4
- export const fetchClient: ClientType = async (command, requestId) => {
5
- if (command.builder.appManager.isNodeJs || !window?.XMLHttpRequest) {
6
- return serverClient(command, requestId);
7
- }
8
- return browserClient(command, requestId);
9
- };
@@ -1,83 +0,0 @@
1
- import { CommandInstance } from "command";
2
-
3
- // Client
4
-
5
- export type ClientType = (command: CommandInstance, requestId: string) => Promise<ClientResponseType<any, any>>;
6
-
7
- export type ClientDefaultOptionsType = Partial<XMLHttpRequest>;
8
-
9
- export type ClientPayloadMappingCallback = (data: unknown) => string | FormData;
10
-
11
- // Responses
12
-
13
- export type ClientResponseType<GenericDataType, GenericErrorType> = [
14
- GenericDataType | null,
15
- GenericErrorType | null,
16
- number | null,
17
- ];
18
- export type ClientResponseSuccessType<GenericDataType> = [GenericDataType, null, number | null];
19
- export type ClientResponseErrorType<GenericErrorType> = [null, GenericErrorType, number | null];
20
-
21
- // QueryParams
22
-
23
- export type ClientQueryParamValues = number | string | boolean | null | undefined;
24
- export type ClientQueryParam =
25
- | ClientQueryParamValues
26
- | Array<ClientQueryParamValues>
27
- | Record<string, ClientQueryParamValues>;
28
-
29
- export type ClientQueryParamsType = Record<string, ClientQueryParam>;
30
-
31
- // Headers
32
-
33
- export type ClientHeaderMappingCallback = <T extends CommandInstance>(command: T) => HeadersInit;
34
-
35
- export type ClientHeadersProps = {
36
- isFormData: boolean;
37
- headers: HeadersInit | undefined;
38
- };
39
-
40
- // Stringify
41
-
42
- export type QueryStringifyOptions = {
43
- /**
44
- * Strict URI encoding
45
- */
46
- strict?: boolean;
47
- /**
48
- * Encode keys and values
49
- */
50
- encode?: boolean;
51
- /**
52
- * Array encoding type
53
- */
54
- arrayFormat?: "bracket" | "index" | "comma" | "separator" | "bracket-separator" | "none";
55
- /**
56
- * Array format separator
57
- */
58
- arraySeparator?: string;
59
- /**
60
- * Skip keys with null values
61
- */
62
- skipNull?: boolean;
63
- /**
64
- * Skip keys with empty string
65
- */
66
- skipEmptyString?: boolean;
67
- };
68
-
69
- // Progress
70
-
71
- export type ProgressRequestDataType = {
72
- total?: number;
73
- loaded?: number;
74
- };
75
-
76
- export type FetchProgressType = {
77
- progress: number;
78
- timeLeft: number | null;
79
- sizeLeft: number;
80
- total: number;
81
- loaded: number;
82
- startTimestamp: number;
83
- };