@hyper-fetch/core 2.1.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 +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} +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,141 +0,0 @@
1
- import { ClientType, ClientDefaultOptionsType, ClientResponseType, ClientQueryParamsType, QueryStringifyOptions, ClientHeaderMappingCallback, ClientPayloadMappingCallback } from "client";
2
- import { BuilderConfig, BuilderInstance, BuilderErrorType, StringifyCallbackType, RequestInterceptorCallback, ResponseInterceptorCallback } from "builder";
3
- import { Cache } from "cache";
4
- import { Dispatcher } from "dispatcher";
5
- import { FetchEffectInstance } from "effect";
6
- import { Command, CommandConfig, CommandInstance } from "command";
7
- import { AppManager, CommandManager, LoggerManager, SeverityType } from "managers";
8
- /**
9
- * **Builder** is a class that allows you to configure the connection with the server and then use it to create
10
- * commands which, when called using the appropriate method, will cause the server to be queried for the endpoint and
11
- * method specified in the command.
12
- * @position 1
13
- */
14
- export declare class Builder<GlobalErrorType extends BuilderErrorType = Error, RequestConfigType = ClientDefaultOptionsType> {
15
- options: BuilderConfig;
16
- readonly url: string;
17
- debug: boolean;
18
- __onErrorCallbacks: ResponseInterceptorCallback[];
19
- __onSuccessCallbacks: ResponseInterceptorCallback[];
20
- __onResponseCallbacks: ResponseInterceptorCallback[];
21
- __onAuthCallbacks: RequestInterceptorCallback[];
22
- __onRequestCallbacks: RequestInterceptorCallback[];
23
- commandManager: CommandManager;
24
- appManager: AppManager;
25
- loggerManager: LoggerManager;
26
- client: ClientType;
27
- cache: Cache;
28
- fetchDispatcher: Dispatcher;
29
- submitDispatcher: Dispatcher;
30
- effects: FetchEffectInstance[];
31
- requestConfig?: (command: CommandInstance) => RequestConfigType;
32
- commandConfig?: (commandOptions: CommandConfig<string, RequestConfigType>) => Partial<CommandConfig<string, RequestConfigType>>;
33
- queryParamsConfig?: QueryStringifyOptions;
34
- /**
35
- * Method to stringify query params from objects.
36
- */
37
- stringifyQueryParams: StringifyCallbackType;
38
- /**
39
- * Method to get default headers and to map them based on the data format exchange, by default it handles FormData / JSON formats.
40
- */
41
- headerMapper: ClientHeaderMappingCallback;
42
- /**
43
- * Method to get request data and transform them to the required format. It handles FormData and JSON by default.
44
- */
45
- payloadMapper: ClientPayloadMappingCallback;
46
- logger: import("managers").LoggerType;
47
- constructor(options: BuilderConfig);
48
- /**
49
- * This method allows to configure global defaults for the command configuration like method, auth, deduplication etc.
50
- */
51
- setCommandConfig: (callback: (command: CommandInstance) => Partial<CommandConfig<string, RequestConfigType>>) => Builder<GlobalErrorType, RequestConfigType>;
52
- /**
53
- * This method enables the logger usage and display the logs in console
54
- */
55
- setDebug: (debug: boolean) => Builder<GlobalErrorType, RequestConfigType>;
56
- /**
57
- * Set the logger severity of the messages displayed to the console
58
- */
59
- setLoggerSeverity: (severity: SeverityType) => Builder<GlobalErrorType, RequestConfigType>;
60
- /**
61
- * Set the new logger instance to the builder
62
- */
63
- setLogger: (callback: (builder: BuilderInstance) => LoggerManager) => Builder<GlobalErrorType, RequestConfigType>;
64
- /**
65
- * Set config for the query params stringify method, we can set here, among others, arrayFormat, skipNull, encode, skipEmptyString and more
66
- */
67
- setQueryParamsConfig: (queryParamsConfig: QueryStringifyOptions) => Builder<GlobalErrorType, RequestConfigType>;
68
- /**
69
- * Set the custom query params stringify method to the builder
70
- * @param stringifyFn Custom callback handling query params stringify
71
- */
72
- setStringifyQueryParams: (stringifyFn: StringifyCallbackType) => Builder<GlobalErrorType, RequestConfigType>;
73
- /**
74
- * Set the custom header mapping function
75
- */
76
- setHeaderMapper: (headerMapper: ClientHeaderMappingCallback) => Builder<GlobalErrorType, RequestConfigType>;
77
- /**
78
- * Set the request payload mapping function which get triggered before request get send
79
- */
80
- setPayloadMapper: (payloadMapper: ClientPayloadMappingCallback) => Builder<GlobalErrorType, RequestConfigType>;
81
- /**
82
- * Set custom http client to handle graphql, rest, firebase or other
83
- */
84
- setClient: (callback: (builder: BuilderInstance) => ClientType) => Builder<GlobalErrorType, RequestConfigType>;
85
- /**
86
- * 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.
87
- */
88
- onAuth: (callback: RequestInterceptorCallback) => Builder<GlobalErrorType, RequestConfigType>;
89
- /**
90
- * Method for intercepting error responses. It can be used for example to refresh tokens.
91
- */
92
- onError: <ErrorType = null>(callback: ResponseInterceptorCallback<any, GlobalErrorType | ErrorType>) => Builder<GlobalErrorType, RequestConfigType>;
93
- /**
94
- * Method for intercepting success responses.
95
- */
96
- onSuccess: <ErrorType = null>(callback: ResponseInterceptorCallback<any, GlobalErrorType | ErrorType>) => Builder<GlobalErrorType, RequestConfigType>;
97
- /**
98
- * Method of manipulating commands before sending the request.
99
- */
100
- onRequest: (callback: RequestInterceptorCallback) => Builder<GlobalErrorType, RequestConfigType>;
101
- /**
102
- * Method for intercepting any responses.
103
- */
104
- onResponse: <ErrorType = null>(callback: ResponseInterceptorCallback<any, GlobalErrorType | ErrorType>) => Builder<GlobalErrorType, RequestConfigType>;
105
- /**
106
- * Add persistent effects which trigger on the request lifecycle
107
- */
108
- addEffect: (effect: FetchEffectInstance | FetchEffectInstance[]) => this;
109
- /**
110
- * Remove effects from builder
111
- */
112
- removeEffect: (effect: FetchEffectInstance | string) => this;
113
- /**
114
- * Helper used by http client to apply the modifications on response error
115
- */
116
- __modifyAuth: (command: CommandInstance) => Promise<CommandInstance>;
117
- /**
118
- * Private helper to run async pre-request processing
119
- */
120
- __modifyRequest: (command: CommandInstance) => Promise<CommandInstance>;
121
- /**
122
- * Private helper to run async on-error response processing
123
- */
124
- __modifyErrorResponse: (response: ClientResponseType<any, GlobalErrorType>, command: CommandInstance) => Promise<ClientResponseType<any, GlobalErrorType>>;
125
- /**
126
- * Private helper to run async on-success response processing
127
- */
128
- __modifySuccessResponse: (response: ClientResponseType<any, GlobalErrorType>, command: CommandInstance) => Promise<ClientResponseType<any, GlobalErrorType>>;
129
- /**
130
- * Private helper to run async response processing
131
- */
132
- __modifyResponse: (response: ClientResponseType<any, GlobalErrorType>, command: CommandInstance) => Promise<ClientResponseType<any, GlobalErrorType>>;
133
- /**
134
- * Clears the builder instance and remove all listeners on it's dependencies
135
- */
136
- clear: () => void;
137
- /**
138
- * Create commands based on the builder setup
139
- */
140
- createCommand: <ResponseType_1, RequestDataType = undefined, LocalErrorType extends BuilderErrorType = undefined, QueryParamsType extends string | ClientQueryParamsType = string | ClientQueryParamsType>() => <EndpointType extends string>(params: CommandConfig<EndpointType, RequestConfigType>) => Command<ResponseType_1, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, RequestConfigType, false, false, false, undefined>;
141
- }
@@ -1,41 +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
- * Configuration setup for the builder
10
- */
11
- export type BuilderConfig = {
12
- /**
13
- * Url to your server
14
- */
15
- url: string;
16
- /**
17
- * Custom client initialization prop
18
- */
19
- client?: ClientType;
20
- /**
21
- * Custom cache initialization prop
22
- */
23
- cache?: <B extends BuilderInstance, C extends Cache>(builder: B) => C;
24
- /**
25
- * Custom app manager initialization prop
26
- */
27
- appManager?: <B extends BuilderInstance, A extends AppManager>(builder: B) => A;
28
- /**
29
- * Custom fetch dispatcher initialization prop
30
- */
31
- fetchDispatcher?: <B extends BuilderInstance, D extends Dispatcher>(builder: B) => D;
32
- /**
33
- * Custom submit dispatcher initialization prop
34
- */
35
- submitDispatcher?: <B extends BuilderInstance, D extends Dispatcher>(builder: B) => D;
36
- };
37
- export type BuilderInstance = Builder<any, any>;
38
- export type BuilderErrorType = Record<string, any> | string;
39
- export type RequestInterceptorCallback = (command: CommandInstance) => Promise<CommandInstance> | CommandInstance;
40
- export type ResponseInterceptorCallback<Response = any, Error = any> = (response: ClientResponseType<Response, Error>, command: CommandInstance) => Promise<ClientResponseType<any, any>> | ClientResponseType<any, any>;
41
- export type StringifyCallbackType = (queryParams: ClientQueryParamsType | string | NegativeTypes) => string;
@@ -1,10 +0,0 @@
1
- import { ClientQueryParamsType, ClientResponseType, QueryStringifyOptions } from "client";
2
- import { CommandInstance } from "command";
3
- import { NegativeTypes } from "types";
4
- import { RequestInterceptorCallback, ResponseInterceptorCallback } from "./builder.types";
5
- export declare const stringifyValue: (response: string | unknown) => string;
6
- export declare const interceptRequest: (interceptors: RequestInterceptorCallback[], command: CommandInstance) => Promise<CommandInstance>;
7
- export declare const interceptResponse: <GlobalErrorType>(interceptors: ResponseInterceptorCallback[], response: ClientResponseType<any, GlobalErrorType>, command: CommandInstance) => Promise<ClientResponseType<any, GlobalErrorType>>;
8
- export declare const getClientHeaders: (command: CommandInstance) => HeadersInit;
9
- export declare const getClientPayload: (data: unknown) => string | FormData;
10
- export declare const stringifyQueryParams: (queryParams: ClientQueryParamsType | string | NegativeTypes, options?: QueryStringifyOptions) => 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,25 +0,0 @@
1
- import { ClientResponseSuccessType, ClientResponseErrorType, ProgressRequestDataType } from "client";
2
- import { CommandInstance } from "command";
3
- import { ExtractResponse, ExtractError } from "types";
4
- export declare const getClientBindings: <ConfigType = any>(cmd: CommandInstance, requestId: string) => Promise<{
5
- fullUrl: string;
6
- headers: HeadersInit;
7
- payload: string | FormData;
8
- config: ConfigType;
9
- getAbortController: () => AbortController;
10
- getRequestStartTimestamp: () => number;
11
- getResponseStartTimestamp: () => number;
12
- createAbortListener: <T extends CommandInstance>(callback: () => void, resolve: (value: ClientResponseErrorType<ExtractError<T>>) => void) => () => void;
13
- onBeforeRequest: () => void;
14
- onRequestStart: (progress?: ProgressRequestDataType) => number;
15
- onRequestProgress: (progress: ProgressRequestDataType) => number;
16
- onRequestEnd: () => number;
17
- onResponseStart: (progress?: ProgressRequestDataType) => number;
18
- onResponseProgress: (progress: ProgressRequestDataType) => number;
19
- onResponseEnd: () => number;
20
- onSuccess: <T_1 extends CommandInstance>(responseData: unknown, status: number, resolve: (value: ClientResponseErrorType<ExtractError<T_1>>) => void) => Promise<ClientResponseSuccessType<ExtractResponse<T_1>>>;
21
- onAbortError: <T_2 extends CommandInstance>(resolve: (value: ClientResponseErrorType<ExtractError<T_2>>) => void) => Promise<ClientResponseErrorType<ExtractError<T_2>>>;
22
- onTimeoutError: <T_3 extends CommandInstance>(resolve: (value: ClientResponseErrorType<ExtractError<T_3>>) => void) => Promise<ClientResponseErrorType<ExtractError<T_3>>>;
23
- onUnexpectedError: <T_4 extends CommandInstance>(resolve: (value: ClientResponseErrorType<ExtractError<T_4>>) => void) => Promise<ClientResponseErrorType<ExtractError<T_4>>>;
24
- onError: <T_5 extends CommandInstance>(error: Error | ExtractError<T_5>, status: number, resolve: (value: ClientResponseErrorType<ExtractError<T_5>>) => void) => Promise<ClientResponseErrorType<ExtractError<T_5>>>;
25
- }>;
@@ -1,2 +0,0 @@
1
- import { ClientType } from "client";
2
- export declare const browserClient: ClientType;
@@ -1,2 +0,0 @@
1
- import { ClientType } from "client";
2
- export declare const fetchClient: ClientType;
@@ -1,2 +0,0 @@
1
- import { ClientType } from "client";
2
- export declare const serverClient: ClientType;
@@ -1,57 +0,0 @@
1
- import { CommandInstance } from "command";
2
- export type ClientType = (command: CommandInstance, requestId: string) => Promise<ClientResponseType<any, any>>;
3
- export type ClientDefaultOptionsType = Partial<XMLHttpRequest>;
4
- export type ClientPayloadMappingCallback = (data: unknown) => string | FormData;
5
- export type ClientResponseType<GenericDataType, GenericErrorType> = [
6
- GenericDataType | null,
7
- GenericErrorType | null,
8
- number | null
9
- ];
10
- export type ClientResponseSuccessType<GenericDataType> = [GenericDataType, null, number | null];
11
- export type ClientResponseErrorType<GenericErrorType> = [null, GenericErrorType, number | null];
12
- export type ClientQueryParamValues = number | string | boolean | null | undefined;
13
- export type ClientQueryParam = ClientQueryParamValues | Array<ClientQueryParamValues> | Record<string, ClientQueryParamValues>;
14
- export type ClientQueryParamsType = Record<string, ClientQueryParam>;
15
- export type ClientHeaderMappingCallback = <T extends CommandInstance>(command: T) => HeadersInit;
16
- export type ClientHeadersProps = {
17
- isFormData: boolean;
18
- headers: HeadersInit | undefined;
19
- };
20
- export type QueryStringifyOptions = {
21
- /**
22
- * Strict URI encoding
23
- */
24
- strict?: boolean;
25
- /**
26
- * Encode keys and values
27
- */
28
- encode?: boolean;
29
- /**
30
- * Array encoding type
31
- */
32
- arrayFormat?: "bracket" | "index" | "comma" | "separator" | "bracket-separator" | "none";
33
- /**
34
- * Array format separator
35
- */
36
- arraySeparator?: string;
37
- /**
38
- * Skip keys with null values
39
- */
40
- skipNull?: boolean;
41
- /**
42
- * Skip keys with empty string
43
- */
44
- skipEmptyString?: boolean;
45
- };
46
- export type ProgressRequestDataType = {
47
- total?: number;
48
- loaded?: number;
49
- };
50
- export type FetchProgressType = {
51
- progress: number;
52
- timeLeft: number | null;
53
- sizeLeft: number;
54
- total: number;
55
- loaded: number;
56
- startTimestamp: number;
57
- };
@@ -1,98 +0,0 @@
1
- import { FetchMethodType, CommandData, CommandDump, CommandConfig, ExtractRouteParams, CommandCurrentType, CommandMapperType } from "command";
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?: CommandMapperType<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
- garbageCollection: number;
35
- cache: boolean;
36
- cacheTime: number;
37
- queued: boolean;
38
- offline: boolean;
39
- abortKey: string;
40
- cacheKey: string;
41
- queueKey: string;
42
- effectKey: string;
43
- used: boolean;
44
- deduplicate: boolean;
45
- deduplicateTime: number;
46
- private updatedAbortKey;
47
- private updatedCacheKey;
48
- private updatedQueueKey;
49
- private updatedEffectKey;
50
- constructor(builder: Builder<GlobalErrorType, ClientOptions>, commandOptions: CommandConfig<EndpointType, ClientOptions>, commandDump?: CommandCurrentType<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType | LocalErrorType, EndpointType, ClientOptions, MappedData> | undefined, dataMapper?: CommandMapperType<RequestDataType, MappedData>);
51
- setHeaders: (headers: HeadersInit) => Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, HasData, HasParams, HasQuery, MappedData>;
52
- setAuth: (auth: boolean) => Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, HasData, HasParams, HasQuery, MappedData>;
53
- setParams: <P extends ExtractRouteParams<EndpointType>>(params: P) => Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, HasData, P extends null ? false : true, HasQuery, MappedData>;
54
- setData: <D extends RequestDataType>(data: D) => Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, D extends null ? false : true, HasParams, HasQuery, MappedData>;
55
- setQueryParams: (queryParams: QueryParamsType) => Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, HasData, HasParams, true, MappedData>;
56
- setOptions: (options: ClientOptions) => Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, HasData, HasParams, true, MappedData>;
57
- setCancelable: (cancelable: boolean) => Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, HasData, HasParams, HasQuery, MappedData>;
58
- setRetry: (retry: CommandConfig<EndpointType, ClientOptions>["retry"]) => Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, HasData, HasParams, HasQuery, MappedData>;
59
- setRetryTime: (retryTime: CommandConfig<EndpointType, ClientOptions>["retryTime"]) => Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, HasData, HasParams, HasQuery, MappedData>;
60
- setGarbageCollection: (garbageCollection: CommandConfig<EndpointType, ClientOptions>["garbageCollection"]) => Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, HasData, HasParams, HasQuery, MappedData>;
61
- setCache: (cache: CommandConfig<EndpointType, ClientOptions>["cache"]) => Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, HasData, HasParams, HasQuery, MappedData>;
62
- setCacheTime: (cacheTime: CommandConfig<EndpointType, ClientOptions>["cacheTime"]) => Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, HasData, HasParams, HasQuery, MappedData>;
63
- setQueued: (queued: boolean) => Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, HasData, HasParams, HasQuery, MappedData>;
64
- setAbortKey: (abortKey: string) => Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, HasData, HasParams, HasQuery, MappedData>;
65
- setCacheKey: (cacheKey: string) => Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, HasData, HasParams, HasQuery, MappedData>;
66
- setQueueKey: (queueKey: string) => Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, HasData, HasParams, HasQuery, MappedData>;
67
- setEffectKey: (effectKey: string) => Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, HasData, HasParams, HasQuery, MappedData>;
68
- setDeduplicate: (deduplicate: boolean) => Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, HasData, HasParams, HasQuery, MappedData>;
69
- setDeduplicateTime: (deduplicateTime: number) => Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, HasData, HasParams, HasQuery, MappedData>;
70
- setUsed: (used: boolean) => Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, HasData, HasParams, HasQuery, MappedData>;
71
- setOffline: (offline: boolean) => Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, HasData, HasParams, HasQuery, MappedData>;
72
- setDataMapper: <DataMapper extends (data: RequestDataType) => any>(mapper: DataMapper) => Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, HasData, HasParams, HasQuery, MappedData>;
73
- private paramsMapper;
74
- dump(): CommandDump<Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, HasData, HasParams, HasQuery, MappedData>, ClientOptions, QueryParamsType>;
75
- clone<D extends true | false = HasData, P extends true | false = HasParams, Q extends true | false = HasQuery, NewMappedData = MappedData>(options?: CommandCurrentType<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType | LocalErrorType, EndpointType, ClientOptions, NewMappedData>, mapper?: CommandMapperType<RequestDataType, NewMappedData>): Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, D, P, Q, NewMappedData>;
76
- abort: () => Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, HasData, HasParams, HasQuery, MappedData>;
77
- /**
78
- * Method to use the command WITHOUT adding it to cache and queues. This mean it will make simple request without queue side effects.
79
- * @param options
80
- * @disableReturns
81
- * @returns
82
- * ```tsx
83
- * Promise<[Data | null, Error | null, HttpStatus]>
84
- * ```
85
- */
86
- exec: FetchMethodType<Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, HasData, HasParams, HasQuery, MappedData>>;
87
- /**
88
- * Method used to perform requests with usage of cache and queues
89
- * @param options
90
- * @param requestCallback
91
- * @disableReturns
92
- * @returns
93
- * ```tsx
94
- * Promise<[Data | null, Error | null, HttpStatus]>
95
- * ```
96
- */
97
- send: FetchMethodType<Command<ResponseType, RequestDataType, QueryParamsType, GlobalErrorType, LocalErrorType, EndpointType, ClientOptions, HasData, HasParams, HasQuery, MappedData>>;
98
- }
@@ -1,202 +0,0 @@
1
- import { NullableKeys, NegativeTypes, ExtractParams, ExtractRequestData, HttpMethodsType, ExtractQueryParams, ExtractClientOptions, ExtractEndpoint, ExtractHasData, ExtractHasParams, ExtractHasQueryParams, ExtractError, ExtractResponse } from "types";
2
- import { Command } from "command";
3
- import { ClientResponseType, ClientQueryParamsType, FetchProgressType } from "client";
4
- import { CommandEventDetails, CommandResponseDetails } from "managers";
5
- export type ClientProgressEvent = {
6
- total: number;
7
- loaded: number;
8
- };
9
- export 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 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
- garbageCollection: number;
27
- cache: boolean;
28
- cacheTime: number;
29
- queued: boolean;
30
- offline: boolean;
31
- disableResponseInterceptors: boolean | undefined;
32
- disableRequestInterceptors: boolean | undefined;
33
- options?: ClientOptions | ExtractClientOptions<Command>;
34
- data: CommandData<ExtractRequestData<Command>, unknown>;
35
- params: Params | NegativeTypes;
36
- queryParams: QueryParamsType | NegativeTypes;
37
- abortKey: string;
38
- cacheKey: string;
39
- queueKey: string;
40
- effectKey: string;
41
- used: boolean;
42
- updatedAbortKey: boolean;
43
- updatedCacheKey: boolean;
44
- updatedQueueKey: boolean;
45
- updatedEffectKey: boolean;
46
- deduplicate: boolean;
47
- deduplicateTime: number;
48
- };
49
- /**
50
- * Configuration options for command creation
51
- */
52
- export type CommandConfig<GenericEndpoint extends string, ClientOptions> = {
53
- /**
54
- * Determine the endpoint for command request
55
- */
56
- endpoint: GenericEndpoint;
57
- /**
58
- * Custom headers for command
59
- */
60
- headers?: HeadersInit;
61
- /**
62
- * Should the onAuth method get called on this command
63
- */
64
- auth?: boolean;
65
- /**
66
- * Request method GET | POST | PATCH | PUT | DELETE
67
- */
68
- method?: HttpMethodsType;
69
- /**
70
- * Should enable cancelable mode in the Dispatcher
71
- */
72
- cancelable?: boolean;
73
- /**
74
- * Retry count when request is failed
75
- */
76
- retry?: number;
77
- /**
78
- * Retry time delay between retries
79
- */
80
- retryTime?: number;
81
- /**
82
- * Should we trigger garbage collection or leave data in memory
83
- */
84
- garbageCollection?: number;
85
- /**
86
- * Should we save the response to cache
87
- */
88
- cache?: boolean;
89
- /**
90
- * Time for which the cache is considered up-to-date
91
- */
92
- cacheTime?: number;
93
- /**
94
- * Should the requests from this command be send one-by-one
95
- */
96
- queued?: boolean;
97
- /**
98
- * Do we want to store request made in offline mode for latter use when we go back online
99
- */
100
- offline?: boolean;
101
- /**
102
- * Disable post-request interceptors
103
- */
104
- disableResponseInterceptors?: boolean;
105
- /**
106
- * Disable pre-request interceptors
107
- */
108
- disableRequestInterceptors?: boolean;
109
- /**
110
- * Additional options for your client, by default XHR options
111
- */
112
- options?: ClientOptions;
113
- /**
114
- * Key which will be used to cancel requests. Autogenerated by default.
115
- */
116
- abortKey?: string;
117
- /**
118
- * Key which will be used to cache requests. Autogenerated by default.
119
- */
120
- cacheKey?: string;
121
- /**
122
- * Key which will be used to queue requests. Autogenerated by default.
123
- */
124
- queueKey?: string;
125
- /**
126
- * Key which will be used to use effects. Autogenerated by default.
127
- */
128
- effectKey?: string;
129
- /**
130
- * Should we deduplicate two requests made at the same time into one
131
- */
132
- deduplicate?: boolean;
133
- /**
134
- * Time of pooling for the deduplication to be active (default 10ms)
135
- */
136
- deduplicateTime?: number;
137
- };
138
- export type CommandMapperType<RequestDataType, MappedData> = (data: RequestDataType) => MappedData;
139
- export type CommandData<RequestDataType, MappedData> = (MappedData extends never ? RequestDataType : MappedData) | NegativeTypes;
140
- export type CommandCurrentType<ResponseType, RequestDataType, QueryParamsType, ErrorType, GenericEndpoint extends string, ClientOptions, MappedData> = {
141
- used?: boolean;
142
- params?: ExtractRouteParams<GenericEndpoint> | NegativeTypes;
143
- queryParams?: QueryParamsType | NegativeTypes;
144
- data?: CommandData<RequestDataType, MappedData>;
145
- mockCallback?: ((data: RequestDataType) => ClientResponseType<ResponseType, ErrorType>) | undefined;
146
- headers?: HeadersInit;
147
- updatedAbortKey?: boolean;
148
- updatedCacheKey?: boolean;
149
- updatedQueueKey?: boolean;
150
- updatedEffectKey?: boolean;
151
- } & Partial<NullableKeys<CommandConfig<GenericEndpoint, ClientOptions>>>;
152
- export type ParamType = string | number;
153
- export type ParamsType = Record<string, ParamType>;
154
- export type ExtractRouteParams<T extends string> = string extends T ? NegativeTypes : T extends `${string}:${infer Param}/${infer Rest}` ? {
155
- [k in Param | keyof ExtractRouteParams<Rest>]: ParamType;
156
- } : T extends `${string}:${infer Param}` ? {
157
- [k in Param]: ParamType;
158
- } : NegativeTypes;
159
- export type FetchOptionsType<ClientOptions> = Omit<Partial<CommandConfig<string, ClientOptions>>, "endpoint" | "method">;
160
- /**
161
- * It will check if the query params are already set
162
- */
163
- export type FetchQueryParamsType<QueryParamsType extends ClientQueryParamsType | string, HasQuery extends true | false = false> = HasQuery extends true ? {
164
- queryParams?: NegativeTypes;
165
- } : {
166
- queryParams?: QueryParamsType | string;
167
- };
168
- /**
169
- * If the command endpoint parameters are not filled it will throw an error
170
- */
171
- export type FetchParamsType<EndpointType extends string, HasParams extends true | false> = ExtractRouteParams<EndpointType> extends NegativeTypes ? {
172
- params?: NegativeTypes;
173
- } : true extends HasParams ? {
174
- params?: NegativeTypes;
175
- } : {
176
- params: NonNullable<ExtractRouteParams<EndpointType>>;
177
- };
178
- /**
179
- * If the command data is not filled it will throw an error
180
- */
181
- export type FetchRequestDataType<RequestDataType, HasData extends true | false> = RequestDataType extends NegativeTypes ? {
182
- data?: NegativeTypes;
183
- } : HasData extends true ? {
184
- data?: NegativeTypes;
185
- } : {
186
- data: NonNullable<RequestDataType>;
187
- };
188
- export type CommandQueueOptions = {
189
- dispatcherType?: "auto" | "fetch" | "submit";
190
- };
191
- export 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;
192
- export type FetchSendActionsType<Command extends CommandInstance> = {
193
- onSettle?: (requestId: string, command: Command) => void;
194
- onRequestStart?: (details: CommandEventDetails<Command>) => void;
195
- onResponseStart?: (details: CommandEventDetails<Command>) => void;
196
- onUploadProgress?: (values: FetchProgressType, details: CommandEventDetails<Command>) => void;
197
- onDownloadProgress?: (values: FetchProgressType, details: CommandEventDetails<Command>) => void;
198
- onResponse?: (response: ClientResponseType<ExtractResponse<Command>, ExtractError<Command>>, details: CommandResponseDetails) => void;
199
- onRemove?: (details: CommandEventDetails<Command>) => void;
200
- };
201
- export type FetchMethodType<Command extends CommandInstance> = 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>>>;
202
- export type CommandInstance = Command<any, any, any, any, any, any, any, any, any, any, any>;
@@ -1,22 +0,0 @@
1
- import { FetchProgressType, ClientResponseType } from "client";
2
- import { ClientProgressEvent, CommandInstance, CommandDump, FetchType } from "command";
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>>>;
@@ -1,3 +0,0 @@
1
- export * from "./command";
2
- export * from "./command.types";
3
- export * from "./command.utils";
@@ -1,14 +0,0 @@
1
- import { ExtractError } from "types";
2
- import { FetchEffectConfig } from "effect";
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
- }