@hyper-fetch/core 1.6.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 (118) hide show
  1. package/.eslintrc.json +7 -0
  2. package/README.md +83 -0
  3. package/dist/builder/builder.constants.d.ts +9 -0
  4. package/dist/builder/builder.d.ts +141 -0
  5. package/dist/builder/builder.types.d.ts +45 -0
  6. package/dist/builder/builder.utils.d.ts +10 -0
  7. package/dist/builder/index.d.ts +4 -0
  8. package/dist/cache/cache.d.ts +76 -0
  9. package/dist/cache/cache.events.d.ts +28 -0
  10. package/dist/cache/cache.types.d.ts +49 -0
  11. package/dist/cache/cache.utils.d.ts +6 -0
  12. package/dist/cache/index.d.ts +4 -0
  13. package/dist/client/fetch.client.bindings.d.ts +25 -0
  14. package/dist/client/fetch.client.constants.d.ts +1 -0
  15. package/dist/client/fetch.client.d.ts +2 -0
  16. package/dist/client/fetch.client.types.d.ts +57 -0
  17. package/dist/client/fetch.client.utils.d.ts +5 -0
  18. package/dist/client/index.d.ts +5 -0
  19. package/dist/command/command.d.ts +87 -0
  20. package/dist/command/command.types.d.ts +196 -0
  21. package/dist/command/command.utils.d.ts +22 -0
  22. package/dist/command/index.d.ts +3 -0
  23. package/dist/constants/http.constants.d.ts +7 -0
  24. package/dist/constants/time.constants.d.ts +11 -0
  25. package/dist/dispatcher/dispatcher.constants.d.ts +6 -0
  26. package/dist/dispatcher/dispatcher.d.ts +155 -0
  27. package/dist/dispatcher/dispatcher.events.d.ts +11 -0
  28. package/dist/dispatcher/dispatcher.types.d.ts +31 -0
  29. package/dist/dispatcher/dispatcher.utils.d.ts +10 -0
  30. package/dist/dispatcher/index.d.ts +5 -0
  31. package/dist/effect/fetch.effect.d.ts +14 -0
  32. package/dist/effect/fetch.effect.types.d.ts +32 -0
  33. package/dist/effect/index.d.ts +2 -0
  34. package/dist/index.cjs.js +2 -0
  35. package/dist/index.cjs.js.map +7 -0
  36. package/dist/index.d.ts +10 -0
  37. package/dist/index.esm.js +2 -0
  38. package/dist/index.esm.js.map +7 -0
  39. package/dist/managers/app/app.manager.constants.d.ts +9 -0
  40. package/dist/managers/app/app.manager.d.ts +35 -0
  41. package/dist/managers/app/app.manager.events.d.ts +11 -0
  42. package/dist/managers/app/app.manager.types.d.ts +7 -0
  43. package/dist/managers/app/app.manager.utils.d.ts +4 -0
  44. package/dist/managers/app/index.d.ts +5 -0
  45. package/dist/managers/command/command.manager.d.ts +43 -0
  46. package/dist/managers/command/command.manager.events.d.ts +36 -0
  47. package/dist/managers/command/command.manager.types.d.ts +23 -0
  48. package/dist/managers/command/command.manager.utils.d.ts +16 -0
  49. package/dist/managers/command/index.d.ts +4 -0
  50. package/dist/managers/index.d.ts +3 -0
  51. package/dist/managers/logger/index.d.ts +4 -0
  52. package/dist/managers/logger/logger.manager.constants.d.ts +4 -0
  53. package/dist/managers/logger/logger.manager.d.ts +19 -0
  54. package/dist/managers/logger/logger.manager.types.d.ts +30 -0
  55. package/dist/managers/logger/logger.manager.utils.d.ts +3 -0
  56. package/dist/types/fetch.types.d.ts +15 -0
  57. package/dist/types/helpers.types.d.ts +11 -0
  58. package/dist/types/http.types.d.ts +1 -0
  59. package/dist/types/index.d.ts +3 -0
  60. package/dist/utils/index.d.ts +1 -0
  61. package/dist/utils/uuid.utils.d.ts +1 -0
  62. package/package.json +60 -0
  63. package/src/builder/builder.constants.ts +9 -0
  64. package/src/builder/builder.ts +309 -0
  65. package/src/builder/builder.types.ts +57 -0
  66. package/src/builder/builder.utils.ts +178 -0
  67. package/src/builder/index.ts +4 -0
  68. package/src/cache/cache.events.ts +44 -0
  69. package/src/cache/cache.ts +221 -0
  70. package/src/cache/cache.types.ts +56 -0
  71. package/src/cache/cache.utils.ts +29 -0
  72. package/src/cache/index.ts +4 -0
  73. package/src/client/fetch.client.bindings.ts +290 -0
  74. package/src/client/fetch.client.constants.ts +3 -0
  75. package/src/client/fetch.client.ts +98 -0
  76. package/src/client/fetch.client.types.ts +83 -0
  77. package/src/client/fetch.client.utils.ts +28 -0
  78. package/src/client/index.ts +5 -0
  79. package/src/command/command.ts +539 -0
  80. package/src/command/command.types.ts +268 -0
  81. package/src/command/command.utils.ts +179 -0
  82. package/src/command/index.ts +3 -0
  83. package/src/constants/http.constants.ts +7 -0
  84. package/src/constants/time.constants.ts +11 -0
  85. package/src/dispatcher/dispatcher.constants.ts +6 -0
  86. package/src/dispatcher/dispatcher.events.ts +42 -0
  87. package/src/dispatcher/dispatcher.ts +596 -0
  88. package/src/dispatcher/dispatcher.types.ts +39 -0
  89. package/src/dispatcher/dispatcher.utils.ts +55 -0
  90. package/src/dispatcher/index.ts +5 -0
  91. package/src/effect/fetch.effect.ts +28 -0
  92. package/src/effect/fetch.effect.types.ts +35 -0
  93. package/src/effect/index.ts +2 -0
  94. package/src/index.ts +12 -0
  95. package/src/managers/app/app.manager.constants.ts +24 -0
  96. package/src/managers/app/app.manager.events.ts +34 -0
  97. package/src/managers/app/app.manager.ts +74 -0
  98. package/src/managers/app/app.manager.types.ts +8 -0
  99. package/src/managers/app/app.manager.utils.ts +34 -0
  100. package/src/managers/app/index.ts +5 -0
  101. package/src/managers/command/command.manager.events.ts +212 -0
  102. package/src/managers/command/command.manager.ts +68 -0
  103. package/src/managers/command/command.manager.types.ts +25 -0
  104. package/src/managers/command/command.manager.utils.ts +19 -0
  105. package/src/managers/command/index.ts +4 -0
  106. package/src/managers/index.ts +3 -0
  107. package/src/managers/logger/index.ts +4 -0
  108. package/src/managers/logger/logger.manager.constants.ts +24 -0
  109. package/src/managers/logger/logger.manager.ts +70 -0
  110. package/src/managers/logger/logger.manager.types.ts +35 -0
  111. package/src/managers/logger/logger.manager.utils.ts +27 -0
  112. package/src/types/fetch.types.ts +39 -0
  113. package/src/types/helpers.types.ts +15 -0
  114. package/src/types/http.types.ts +1 -0
  115. package/src/types/index.ts +3 -0
  116. package/src/utils/index.ts +1 -0
  117. package/src/utils/uuid.utils.ts +3 -0
  118. package/tsconfig.json +8 -0
@@ -0,0 +1,87 @@
1
+ import { FetchMethodType, CommandData, CommandDump, CommandConfig, ExtractRouteParams, CommandCurrentType } from "./";
2
+ import { Builder } from "../builder";
3
+ import { ClientQueryParamsType } from "../client";
4
+ import { HttpMethodsType, NegativeTypes } from "../types";
5
+ /**
6
+ * Fetch command it is designed to prepare the necessary setup to execute the request to the server.
7
+ * We can setup basic options for example endpoint, method, headers and advanced settings like cache, invalidation patterns, concurrency, retries and much, much more.
8
+ * :::info Usage
9
+ * We should not use this class directly in the standard development flow. We can initialize it using the `createCommand` method on the **Builder** class.
10
+ * :::
11
+ *
12
+ * @attention
13
+ * The most important thing about the command is that it keeps data in the format that can be dumped. This is necessary for the persistance and different dispatcher storage types.
14
+ * This class doesn't have any callback methods by design and communicate with dispatcher and cache by events.
15
+ */
16
+ export declare class Command<ResponseType, RequestDataType, QueryParamsType extends ClientQueryParamsType | string, GlobalErrorType, // Global Error Type
17
+ LocalErrorType, // Additional Error for specific endpoint
18
+ EndpointType extends string, ClientOptions, HasData extends true | false = false, HasParams extends true | false = false, HasQuery extends true | false = false, MappedData = undefined> {
19
+ readonly builder: Builder<GlobalErrorType, ClientOptions>;
20
+ readonly commandOptions: CommandConfig<EndpointType, ClientOptions>;
21
+ readonly commandDump?: CommandCurrentType<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType | LocalErrorType, EndpointType, ClientOptions, MappedData> | undefined;
22
+ readonly dataMapper?: (data: RequestDataType) => MappedData;
23
+ endpoint: EndpointType;
24
+ headers?: HeadersInit;
25
+ auth: boolean;
26
+ method: HttpMethodsType;
27
+ params: ExtractRouteParams<EndpointType> | NegativeTypes;
28
+ data: CommandData<RequestDataType, MappedData>;
29
+ queryParams: QueryParamsType | NegativeTypes;
30
+ options?: ClientOptions | undefined;
31
+ cancelable: boolean;
32
+ retry: number;
33
+ retryTime: number;
34
+ cache: boolean;
35
+ cacheTime: number;
36
+ queued: boolean;
37
+ offline: boolean;
38
+ abortKey: string;
39
+ cacheKey: string;
40
+ queueKey: string;
41
+ effectKey: string;
42
+ used: boolean;
43
+ deduplicate: boolean;
44
+ deduplicateTime: number;
45
+ private updatedAbortKey;
46
+ private updatedCacheKey;
47
+ private updatedQueueKey;
48
+ private updatedEffectKey;
49
+ constructor(builder: Builder<GlobalErrorType, ClientOptions>, commandOptions: CommandConfig<EndpointType, ClientOptions>, commandDump?: CommandCurrentType<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType | LocalErrorType, EndpointType, ClientOptions, MappedData> | undefined, dataMapper?: (data: RequestDataType) => MappedData);
50
+ setHeaders: (headers: HeadersInit) => Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, HasData, HasParams, HasQuery, MappedData>;
51
+ setAuth: (auth: boolean) => Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, HasData, HasParams, HasQuery, MappedData>;
52
+ setParams: (params: ExtractRouteParams<EndpointType>) => Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, HasData, true, HasQuery, MappedData>;
53
+ setData: (data: RequestDataType) => Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, true, HasParams, HasQuery, MappedData>;
54
+ setQueryParams: (queryParams: QueryParamsType) => Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, HasData, HasParams, true, MappedData>;
55
+ setOptions: (options: ClientOptions) => Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, HasData, HasParams, true, MappedData>;
56
+ setCancelable: (cancelable: boolean) => Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, HasData, HasParams, HasQuery, MappedData>;
57
+ setRetry: (retry: CommandConfig<EndpointType, ClientOptions>["retry"]) => Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, HasData, HasParams, HasQuery, MappedData>;
58
+ setRetryTime: (retryTime: CommandConfig<EndpointType, ClientOptions>["retryTime"]) => Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, HasData, HasParams, HasQuery, MappedData>;
59
+ setCache: (cache: CommandConfig<EndpointType, ClientOptions>["cache"]) => Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, HasData, HasParams, HasQuery, MappedData>;
60
+ setCacheTime: (cacheTime: CommandConfig<EndpointType, ClientOptions>["cacheTime"]) => Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, HasData, HasParams, HasQuery, MappedData>;
61
+ setQueued: (queued: boolean) => Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, HasData, HasParams, HasQuery, MappedData>;
62
+ setAbortKey: (abortKey: string) => Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, HasData, HasParams, HasQuery, MappedData>;
63
+ setCacheKey: (cacheKey: string) => Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, HasData, HasParams, HasQuery, MappedData>;
64
+ setQueueKey: (queueKey: string) => Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, HasData, HasParams, HasQuery, MappedData>;
65
+ setEffectKey: (effectKey: string) => Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, HasData, HasParams, HasQuery, MappedData>;
66
+ setDeduplicate: (deduplicate: boolean) => Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, HasData, HasParams, HasQuery, MappedData>;
67
+ setDeduplicateTime: (deduplicateTime: number) => Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, HasData, HasParams, HasQuery, MappedData>;
68
+ setUsed: (used: boolean) => Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, HasData, HasParams, HasQuery, MappedData>;
69
+ setOffline: (offline: boolean) => Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, HasData, HasParams, HasQuery, MappedData>;
70
+ setDataMapper: <DataMapper>(mapper: (data: RequestDataType) => DataMapper) => Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, HasData, HasParams, HasQuery, DataMapper>;
71
+ private paramsMapper;
72
+ dump(): CommandDump<Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, HasData, HasParams, HasQuery, MappedData>, ClientOptions, QueryParamsType>;
73
+ clone<D extends true | false = HasData, P extends true | false = HasParams, Q extends true | false = HasQuery, MapperData = MappedData>(options?: CommandCurrentType<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType | LocalErrorType, EndpointType, ClientOptions, MapperData>, mapper?: (data: RequestDataType) => MapperData): Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, D, P, Q, MapperData>;
74
+ abort: () => Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, HasData, HasParams, HasQuery, MappedData>;
75
+ /**
76
+ * Method to use the command WITHOUT adding it to cache and queues. This mean it will make simple request without queue side effects.
77
+ * @param options
78
+ * @returns
79
+ */
80
+ exec: FetchMethodType<Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, HasData, HasParams, HasQuery, MappedData>>;
81
+ /**
82
+ * Method used to perform requests with usage of cache and queues
83
+ * @param options
84
+ * @param requestCallback
85
+ */
86
+ send: FetchMethodType<Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, HasData, HasParams, HasQuery, MappedData>>;
87
+ }
@@ -0,0 +1,196 @@
1
+ import { NullableKeys, NegativeTypes, ExtractParams, ExtractRequestData, HttpMethodsType, ExtractQueryParams, ExtractClientOptions, ExtractEndpoint, ExtractHasData, ExtractHasParams, ExtractHasQueryParams, ExtractError, ExtractResponse } from "../types";
2
+ import { Command } from "./";
3
+ import { ClientResponseType, ClientQueryParamsType, FetchProgressType } from "../client";
4
+ import { CommandEventDetails, CommandResponseDetails } from "../managers";
5
+ export declare type ClientProgressEvent = {
6
+ total: number;
7
+ loaded: number;
8
+ };
9
+ export declare type ClientProgressResponse = {
10
+ progress: number;
11
+ timeLeft: number;
12
+ sizeLeft: number;
13
+ };
14
+ /**
15
+ * Dump of the command used to later recreate it
16
+ */
17
+ export declare type CommandDump<Command extends CommandInstance, ClientOptions = unknown, QueryParamsType = ClientQueryParamsType, Params = ExtractParams<Command>> = {
18
+ commandOptions: CommandConfig<string, ClientOptions | ExtractClientOptions<Command>>;
19
+ endpoint: string;
20
+ method: HttpMethodsType;
21
+ headers?: HeadersInit;
22
+ auth: boolean;
23
+ cancelable: boolean;
24
+ retry: number;
25
+ retryTime: number;
26
+ cache: boolean;
27
+ cacheTime: number;
28
+ queued: boolean;
29
+ offline: boolean;
30
+ disableResponseInterceptors: boolean | undefined;
31
+ disableRequestInterceptors: boolean | undefined;
32
+ options?: ClientOptions | ExtractClientOptions<Command>;
33
+ data: CommandData<ExtractRequestData<Command>, unknown>;
34
+ params: Params | NegativeTypes;
35
+ queryParams: QueryParamsType | NegativeTypes;
36
+ abortKey: string;
37
+ cacheKey: string;
38
+ queueKey: string;
39
+ effectKey: string;
40
+ used: boolean;
41
+ updatedAbortKey: boolean;
42
+ updatedCacheKey: boolean;
43
+ updatedQueueKey: boolean;
44
+ updatedEffectKey: boolean;
45
+ deduplicate: boolean;
46
+ deduplicateTime: number;
47
+ };
48
+ /**
49
+ * Configuration options for command creation
50
+ */
51
+ export declare type CommandConfig<GenericEndpoint extends string, ClientOptions> = {
52
+ /**
53
+ * Determine the endpoint for command request
54
+ */
55
+ endpoint: GenericEndpoint;
56
+ /**
57
+ * Custom headers for command
58
+ */
59
+ headers?: HeadersInit;
60
+ /**
61
+ * Should the onAuth method get called on this command
62
+ */
63
+ auth?: boolean;
64
+ /**
65
+ * Request method GET | POST | PATCH | PUT | DELETE
66
+ */
67
+ method?: HttpMethodsType;
68
+ /**
69
+ * Should enable cancelable mode in the Dispatcher
70
+ */
71
+ cancelable?: boolean;
72
+ /**
73
+ * Retry count when request is failed
74
+ */
75
+ retry?: number;
76
+ /**
77
+ * Retry time delay between retries
78
+ */
79
+ retryTime?: number;
80
+ /**
81
+ * Should we save the response to cache
82
+ */
83
+ cache?: boolean;
84
+ /**
85
+ * Time for which the cache is considered up-to-date
86
+ */
87
+ cacheTime?: number;
88
+ /**
89
+ * Should the requests from this command be send one-by-one
90
+ */
91
+ queued?: boolean;
92
+ /**
93
+ * Do we want to store request made in offline mode for latter use when we go back online
94
+ */
95
+ offline?: boolean;
96
+ /**
97
+ * Disable post-request interceptors
98
+ */
99
+ disableResponseInterceptors?: boolean;
100
+ /**
101
+ * Disable pre-request interceptors
102
+ */
103
+ disableRequestInterceptors?: boolean;
104
+ /**
105
+ * Additional options for your client, by default XHR options
106
+ */
107
+ options?: ClientOptions;
108
+ /**
109
+ * Key which will be used to cancel requests. Autogenerated by default.
110
+ */
111
+ abortKey?: string;
112
+ /**
113
+ * Key which will be used to cache requests. Autogenerated by default.
114
+ */
115
+ cacheKey?: string;
116
+ /**
117
+ * Key which will be used to queue requests. Autogenerated by default.
118
+ */
119
+ queueKey?: string;
120
+ /**
121
+ * Key which will be used to use effects. Autogenerated by default.
122
+ */
123
+ effectKey?: string;
124
+ /**
125
+ * Should we deduplicate two requests made at the same time into one
126
+ */
127
+ deduplicate?: boolean;
128
+ /**
129
+ * Time of pooling for the deduplication to be active (default 10ms)
130
+ */
131
+ deduplicateTime?: number;
132
+ };
133
+ export declare type CommandData<RequestDataType, MappedData> = (MappedData extends undefined ? RequestDataType : MappedData) | NegativeTypes;
134
+ export declare type CommandCurrentType<ResponseType, RequestDataType, QueryParamsType, ErrorType, GenericEndpoint extends string, ClientOptions, MappedData> = {
135
+ used?: boolean;
136
+ params?: ExtractRouteParams<GenericEndpoint> | NegativeTypes;
137
+ queryParams?: QueryParamsType | NegativeTypes;
138
+ data?: CommandData<RequestDataType, MappedData>;
139
+ mockCallback?: ((data: RequestDataType) => ClientResponseType<ResponseType, ErrorType>) | undefined;
140
+ headers?: HeadersInit;
141
+ updatedAbortKey?: boolean;
142
+ updatedCacheKey?: boolean;
143
+ updatedQueueKey?: boolean;
144
+ updatedEffectKey?: boolean;
145
+ } & Partial<NullableKeys<CommandConfig<GenericEndpoint, ClientOptions>>>;
146
+ export declare type ParamType = string | number;
147
+ export declare type ParamsType = Record<string, ParamType>;
148
+ export declare type ExtractRouteParams<T extends string> = string extends T ? NegativeTypes : T extends `${string}:${infer Param}/${infer Rest}` ? {
149
+ [k in Param | keyof ExtractRouteParams<Rest>]: ParamType;
150
+ } : T extends `${string}:${infer Param}` ? {
151
+ [k in Param]: ParamType;
152
+ } : NegativeTypes;
153
+ export declare type FetchOptionsType<ClientOptions> = Omit<Partial<CommandConfig<string, ClientOptions>>, "endpoint" | "method">;
154
+ /**
155
+ * It will check if the query params are already set
156
+ */
157
+ export declare type FetchQueryParamsType<QueryParamsType, HasQuery extends true | false = false> = HasQuery extends true ? {
158
+ queryParams?: NegativeTypes;
159
+ } : {
160
+ queryParams?: QueryParamsType | string;
161
+ };
162
+ /**
163
+ * If the command endpoint parameters are not filled it will throw an error
164
+ */
165
+ export declare type FetchParamsType<EndpointType extends string, HasParams extends true | false = false> = ExtractRouteParams<EndpointType> extends NegativeTypes ? {
166
+ params?: NegativeTypes;
167
+ } : true extends HasParams ? {
168
+ params?: NegativeTypes;
169
+ } : {
170
+ params: ExtractRouteParams<EndpointType>;
171
+ };
172
+ /**
173
+ * If the command data is not filled it will throw an error
174
+ */
175
+ export declare type FetchRequestDataType<RequestDataType, HasData extends true | false = false> = RequestDataType extends NegativeTypes ? {
176
+ data?: NegativeTypes;
177
+ } : HasData extends true ? {
178
+ data?: NegativeTypes;
179
+ } : {
180
+ data: RequestDataType;
181
+ };
182
+ export declare type CommandQueueOptions = {
183
+ dispatcherType?: "auto" | "fetch" | "submit";
184
+ };
185
+ export declare type FetchType<Command extends CommandInstance> = FetchQueryParamsType<ExtractQueryParams<Command>, ExtractHasQueryParams<Command>> & FetchParamsType<ExtractEndpoint<Command>, ExtractHasParams<Command>> & FetchRequestDataType<ExtractRequestData<Command>, ExtractHasData<Command>> & Omit<FetchOptionsType<ExtractClientOptions<Command>>, "params" | "data"> & FetchSendActionsType<Command> & CommandQueueOptions;
186
+ export declare type FetchSendActionsType<Command extends CommandInstance> = {
187
+ onSettle?: (requestId: string, command: Command) => void;
188
+ onRequestStart?: (details: CommandEventDetails<Command>) => void;
189
+ onResponseStart?: (details: CommandEventDetails<Command>) => void;
190
+ onUploadProgress?: (values: FetchProgressType, details: CommandEventDetails<Command>) => void;
191
+ onDownloadProgress?: (values: FetchProgressType, details: CommandEventDetails<Command>) => void;
192
+ onResponse?: (response: ClientResponseType<ExtractResponse<Command>, ExtractError<Command>>, details: CommandResponseDetails) => void;
193
+ onRemove?: (details: CommandEventDetails<Command>) => void;
194
+ };
195
+ export declare type FetchMethodType<Command extends CommandInstance> = FetchType<Command>["data"] extends any ? (options?: FetchType<Command>) => Promise<ClientResponseType<ExtractResponse<Command>, ExtractError<Command>>> : FetchType<Command>["data"] extends NegativeTypes ? FetchType<Command>["params"] extends NegativeTypes ? (options?: FetchType<Command>) => Promise<ClientResponseType<ExtractResponse<Command>, ExtractError<Command>>> : (options: FetchType<Command>) => Promise<ClientResponseType<ExtractResponse<Command>, ExtractError<Command>>> : (options: FetchType<Command>) => Promise<ClientResponseType<ExtractResponse<Command>, ExtractError<Command>>>;
196
+ export declare type CommandInstance = Command<any, any, any, any, any, any, any, any, any, any, any>;
@@ -0,0 +1,22 @@
1
+ import { FetchProgressType, ClientResponseType } from "../client";
2
+ import { ClientProgressEvent, CommandInstance, CommandDump, FetchType } from "./";
3
+ import { Dispatcher } from "../dispatcher";
4
+ import { ExtractError, ExtractResponse } from "../types";
5
+ export declare const stringifyKey: (value: unknown) => string;
6
+ export declare const getProgressValue: ({ loaded, total }: ClientProgressEvent) => number;
7
+ export declare const getRequestEta: (startDate: Date, progressDate: Date, { total, loaded }: ClientProgressEvent) => {
8
+ sizeLeft: number;
9
+ timeLeft: number | null;
10
+ };
11
+ export declare const getProgressData: (requestStartTime: Date, progressDate: Date, progressEvent: ClientProgressEvent) => FetchProgressType;
12
+ export declare const getSimpleKey: (command: CommandInstance | CommandDump<CommandInstance>) => string;
13
+ /**
14
+ * Cache instance for individual command that collects individual requests responses from
15
+ * the same endpoint (they may differ base on the custom key, endpoint params etc)
16
+ * @param command
17
+ * @param useInitialValues
18
+ * @returns
19
+ */
20
+ export declare const getCommandKey: (command: CommandInstance | CommandDump<CommandInstance>, useInitialValues?: boolean) => string;
21
+ export declare const getCommandDispatcher: <Command extends CommandInstance>(command: Command, dispatcherType?: "auto" | "fetch" | "submit") => [Dispatcher, boolean];
22
+ export declare const commandSendRequest: <Command extends CommandInstance>(command: Command, options?: FetchType<Command>) => Promise<ClientResponseType<ExtractResponse<Command>, ExtractError<Command>>>;
@@ -0,0 +1,3 @@
1
+ export * from "./command";
2
+ export * from "./command.types";
3
+ export * from "./command.utils";
@@ -0,0 +1,7 @@
1
+ export declare enum HttpMethodsEnum {
2
+ get = "GET",
3
+ post = "POST",
4
+ put = "PUT",
5
+ patch = "PATCH",
6
+ delete = "DELETE"
7
+ }
@@ -0,0 +1,11 @@
1
+ export declare enum DateInterval {
2
+ second = 1000,
3
+ minute = 60000,
4
+ hour = 3600000,
5
+ day = 86400000,
6
+ week = 604800000,
7
+ month30 = 2592000000,
8
+ month31 = 2678400000,
9
+ year = 31536000000,
10
+ yearLeap = 31622400000
11
+ }
@@ -0,0 +1,6 @@
1
+ export declare enum DispatcherRequestType {
2
+ oneByOne = "one-by-one",
3
+ allAtOnce = "all-at-once",
4
+ previousCanceled = "previous-canceled",
5
+ deduplicated = "deduplicated"
6
+ }
@@ -0,0 +1,155 @@
1
+ /// <reference types="node" />
2
+ import EventEmitter from "events";
3
+ import { DispatcherData, DispatcherOptionsType, DispatcherStorageType, DispatcherDumpValueType, RunningRequestValueType } from "./";
4
+ import { BuilderInstance } from "../builder";
5
+ import { CommandInstance } from "../command";
6
+ /**
7
+ * Dispatcher class was made to store controlled request Fetches, and firing them all-at-once or one-by-one in command queue.
8
+ * Generally requests should be flushed at the same time, the queue provide mechanism to fire them in the order.
9
+ */
10
+ export declare class Dispatcher {
11
+ builder: BuilderInstance;
12
+ options?: DispatcherOptionsType;
13
+ emitter: EventEmitter;
14
+ events: {
15
+ setDrained: <Command_1 extends CommandInstance>(queueKey: string, values: DispatcherData<Command_1>) => void;
16
+ setQueueStatus: <Command_2 extends CommandInstance>(queueKey: string, values: DispatcherData<Command_2>) => void;
17
+ setQueueChanged: <Command_3 extends CommandInstance>(queueKey: string, values: DispatcherData<Command_3>) => void;
18
+ onDrained: <Command_4 extends CommandInstance>(queueKey: string, callback: (values: DispatcherData<Command_4>) => void) => VoidFunction;
19
+ onQueueStatus: <Command_5 extends CommandInstance>(queueKey: string, callback: (values: DispatcherData<Command_5>) => void) => VoidFunction;
20
+ onQueueChange: <Command_6 extends CommandInstance>(queueKey: string, callback: (values: DispatcherData<Command_6>) => void) => VoidFunction;
21
+ };
22
+ storage: DispatcherStorageType;
23
+ private requestCount;
24
+ private runningRequests;
25
+ private logger;
26
+ constructor(builder: BuilderInstance, options?: DispatcherOptionsType);
27
+ /**
28
+ * Start request handling by queueKey
29
+ */
30
+ start: (queueKey: string) => void;
31
+ /**
32
+ * Pause request queue, but not cancel already started requests
33
+ */
34
+ pause: (queueKey: string) => void;
35
+ /**
36
+ * Stop request queue and cancel all started requests - those will be treated like not started
37
+ */
38
+ stop: (queueKey: string) => void;
39
+ /**
40
+ * Return all
41
+ */
42
+ getQueuesKeys: () => string[];
43
+ /**
44
+ * Return queue state object
45
+ */
46
+ getQueue: <Command_1 extends CommandInstance = CommandInstance>(queueKey: string) => DispatcherData<Command_1>;
47
+ /**
48
+ * Return request from queue state
49
+ */
50
+ getRequest: <Command_1 extends CommandInstance = CommandInstance>(queueKey: string, requestId: string) => DispatcherDumpValueType<Command_1>;
51
+ /**
52
+ * Get value of the active queue status based on the stopped status
53
+ */
54
+ getIsActiveQueue: (queueKey: string) => boolean;
55
+ /**
56
+ * Add new element to storage
57
+ */
58
+ addQueueElement: <Command_1 extends CommandInstance = CommandInstance>(queueKey: string, dispatcherDump: DispatcherDumpValueType<Command_1>) => void;
59
+ /**
60
+ * Set new queue storage value
61
+ */
62
+ setQueue: <Command_1 extends CommandInstance = CommandInstance>(queueKey: string, queue: DispatcherData<Command_1>) => DispatcherData<Command_1>;
63
+ /**
64
+ * Clear requests from queue cache
65
+ */
66
+ clearQueue: (queueKey: string) => {
67
+ requests: any[];
68
+ stopped: boolean;
69
+ };
70
+ /**
71
+ * Method used to flush the queue requests
72
+ */
73
+ flushQueue: (queueKey: string) => Promise<void>;
74
+ /**
75
+ * Flush all available requests from all queues
76
+ */
77
+ flush: () => Promise<void>;
78
+ /**
79
+ * Clear all running requests and storage
80
+ */
81
+ clear: () => void;
82
+ /**
83
+ * Start particular request
84
+ */
85
+ startRequest: (queueKey: string, requestId: string) => void;
86
+ /**
87
+ * Stop particular request
88
+ */
89
+ stopRequest: (queueKey: string, requestId: string) => void;
90
+ /**
91
+ * Get currently running requests from all queueKeys
92
+ */
93
+ getAllRunningRequest: () => RunningRequestValueType[];
94
+ /**
95
+ * Get currently running requests
96
+ */
97
+ getRunningRequests: (queueKey: string) => RunningRequestValueType[];
98
+ /**
99
+ * Get running request by id
100
+ */
101
+ getRunningRequest: (queueKey: string, requestId: string) => RunningRequestValueType;
102
+ /**
103
+ * Add request to the running requests list
104
+ */
105
+ addRunningRequest: (queueKey: string, requestId: string, command: CommandInstance) => void;
106
+ /**
107
+ * Get the value based on the currently running requests
108
+ */
109
+ hasRunningRequests: (queueKey: string) => boolean;
110
+ /**
111
+ * Check if request is currently processing
112
+ */
113
+ hasRunningRequest: (queueKey: string, requestId: string) => boolean;
114
+ /**
115
+ * Cancel all started requests, but do NOT remove it from main storage
116
+ */
117
+ cancelRunningRequests: (queueKey: string) => void;
118
+ /**
119
+ * Cancel started request, but do NOT remove it from main storage
120
+ */
121
+ cancelRunningRequest: (queueKey: string, requestId: string) => void;
122
+ /**
123
+ * Delete all started requests, but do NOT clear it from queue and do NOT cancel them
124
+ */
125
+ deleteRunningRequests: (queueKey: string) => void;
126
+ /**
127
+ * Delete request by id, but do NOT clear it from queue and do NOT cancel them
128
+ */
129
+ deleteRunningRequest: (queueKey: string, requestId: string) => void;
130
+ /**
131
+ * Get count of requests from the same queueKey
132
+ */
133
+ getQueueRequestCount: (queueKey: string) => number;
134
+ /**
135
+ * Add request count to the queueKey
136
+ */
137
+ incrementQueueRequestCount: (queueKey: string) => void;
138
+ /**
139
+ * Create storage element from command
140
+ */
141
+ createStorageElement: <Command_1 extends CommandInstance>(command: Command_1) => DispatcherDumpValueType<Command_1>;
142
+ /**
143
+ * Add command to the dispatcher handler
144
+ */
145
+ add: (command: CommandInstance) => string;
146
+ /**
147
+ * Delete from the storage and cancel request
148
+ */
149
+ delete: (queueKey: string, requestId: string, abortKey: string) => DispatcherData<CommandInstance>;
150
+ /**
151
+ * Request can run for some time, once it's done, we have to check if it's successful or if it was aborted
152
+ * It can be different once the previous call was set as cancelled and removed from queue before this request got resolved
153
+ */
154
+ performRequest: (storageElement: DispatcherDumpValueType) => Promise<void | DispatcherData<CommandInstance>>;
155
+ }
@@ -0,0 +1,11 @@
1
+ import EventEmitter from "events";
2
+ import { DispatcherData } from "./";
3
+ import { CommandInstance } from "../command";
4
+ export declare const getDispatcherEvents: (emitter: EventEmitter) => {
5
+ setDrained: <Command extends CommandInstance>(queueKey: string, values: DispatcherData<Command>) => void;
6
+ setQueueStatus: <Command_1 extends CommandInstance>(queueKey: string, values: DispatcherData<Command_1>) => void;
7
+ setQueueChanged: <Command_2 extends CommandInstance>(queueKey: string, values: DispatcherData<Command_2>) => void;
8
+ onDrained: <Command_3 extends CommandInstance>(queueKey: string, callback: (values: DispatcherData<Command_3>) => void) => VoidFunction;
9
+ onQueueStatus: <Command_4 extends CommandInstance>(queueKey: string, callback: (values: DispatcherData<Command_4>) => void) => VoidFunction;
10
+ onQueueChange: <Command_5 extends CommandInstance>(queueKey: string, callback: (values: DispatcherData<Command_5>) => void) => VoidFunction;
11
+ };
@@ -0,0 +1,31 @@
1
+ import { Dispatcher } from "./";
2
+ import { CommandDump, CommandInstance } from "../command";
3
+ export declare type DispatcherOptionsType = {
4
+ storage?: DispatcherStorageType;
5
+ onInitialization?: (dispatcherInstance: Dispatcher) => void;
6
+ onUpdateStorage?: <Command extends CommandInstance>(queueKey: string, data: DispatcherData<Command>) => void;
7
+ onDeleteFromStorage?: <Command extends CommandInstance>(queueKey: string, data: DispatcherData<Command>) => void;
8
+ onClearStorage?: (dispatcherInstance: Dispatcher) => void;
9
+ };
10
+ export declare type DispatcherDumpValueType<Command extends CommandInstance = CommandInstance> = {
11
+ requestId: string;
12
+ commandDump: CommandDump<Command>;
13
+ retries: number;
14
+ timestamp: number;
15
+ stopped: boolean;
16
+ };
17
+ export declare type DispatcherData<Command extends CommandInstance = CommandInstance> = {
18
+ requests: DispatcherDumpValueType<Command>[];
19
+ stopped: boolean;
20
+ };
21
+ export declare type DispatcherStorageType = {
22
+ set: <Command extends CommandInstance = CommandInstance>(key: string, data: DispatcherData<Command>) => void;
23
+ get: <Command extends CommandInstance = CommandInstance>(key: string) => DispatcherData<Command> | undefined;
24
+ keys: () => string[] | IterableIterator<string>;
25
+ delete: (key: string) => void;
26
+ clear: () => void;
27
+ };
28
+ export declare type RunningRequestValueType = {
29
+ requestId: string;
30
+ command: CommandInstance;
31
+ };
@@ -0,0 +1,10 @@
1
+ import { CommandInstance } from "../command";
2
+ import { ClientResponseType } from "../client";
3
+ import { DispatcherRequestType, DispatcherDumpValueType } from "./";
4
+ export declare const getDispatcherDrainedEventKey: (key: string) => string;
5
+ export declare const getDispatcherStatusEventKey: (key: string) => string;
6
+ export declare const getDispatcherChangeEventKey: (key: string) => string;
7
+ export declare const getIsEqualTimestamp: (currentTimestamp: number, threshold: number, queueTimestamp?: number) => boolean;
8
+ export declare const canRetryRequest: (currentRetries: number, retry: number | undefined) => boolean;
9
+ export declare const getRequestType: (command: CommandInstance, latestRequest: DispatcherDumpValueType | undefined) => DispatcherRequestType;
10
+ export declare const isFailedRequest: (data: ClientResponseType<unknown, unknown>) => boolean;
@@ -0,0 +1,5 @@
1
+ export * from "./dispatcher.constants";
2
+ export * from "./dispatcher.events";
3
+ export * from "./dispatcher";
4
+ export * from "./dispatcher.types";
5
+ export * from "./dispatcher.utils";
@@ -0,0 +1,14 @@
1
+ import { ExtractError } from "../types";
2
+ import { FetchEffectConfig } from "./";
3
+ import { CommandInstance } from "../command";
4
+ import { ClientResponseErrorType, ClientResponseType, ClientResponseSuccessType } from "../client";
5
+ export declare class FetchEffect<T extends CommandInstance> {
6
+ config: FetchEffectConfig<T>;
7
+ constructor(config: FetchEffectConfig<T>);
8
+ getEffectKey: () => string;
9
+ onTrigger: (command: T) => void;
10
+ onStart: (command: T) => void;
11
+ onSuccess: (response: ClientResponseSuccessType<ResponseType>, command: T) => void;
12
+ onError: (response: ClientResponseErrorType<ExtractError<T>>, command: T) => void;
13
+ onFinished: (response: ClientResponseType<ResponseType, ExtractError<T>>, command: T) => void;
14
+ }
@@ -0,0 +1,32 @@
1
+ import { CommandInstance } from "../command";
2
+ import { ClientResponseErrorType, ClientResponseType, ClientResponseSuccessType } from "../client";
3
+ import { FetchEffect } from "./";
4
+ import { ExtractError } from "../types";
5
+ export declare type FetchEffectLifecycle = "trigger" | "start" | "success" | "error" | "finished";
6
+ export declare type FetchEffectInstance = FetchEffect<CommandInstance>;
7
+ export declare 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
+ };
@@ -0,0 +1,2 @@
1
+ export * from "./fetch.effect";
2
+ export * from "./fetch.effect.types";