@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.
- package/README.md +1 -1
- package/dist/adapter/adapter.bindings.d.ts +25 -0
- package/dist/adapter/adapter.browser.d.ts +2 -0
- package/dist/{client/fetch.client.constants.d.ts → adapter/adapter.constants.d.ts} +0 -0
- package/dist/adapter/adapter.d.ts +2 -0
- package/dist/adapter/adapter.server.d.ts +2 -0
- package/dist/adapter/adapter.types.d.ts +57 -0
- package/dist/{client/fetch.client.utils.d.ts → adapter/adapter.utils.d.ts} +3 -3
- package/dist/adapter/index.d.ts +7 -0
- package/dist/cache/cache.d.ts +9 -9
- package/dist/cache/cache.types.d.ts +4 -4
- package/dist/cache/cache.utils.d.ts +3 -3
- package/dist/{builder/builder.constants.d.ts → client/client.constants.d.ts} +0 -0
- package/dist/client/client.d.ts +142 -0
- package/dist/client/client.types.d.ts +41 -0
- package/dist/client/client.utils.d.ts +10 -0
- package/dist/client/index.d.ts +4 -6
- package/dist/dispatcher/dispatcher.d.ts +23 -23
- package/dist/dispatcher/dispatcher.events.d.ts +8 -8
- package/dist/dispatcher/dispatcher.types.d.ts +10 -10
- package/dist/dispatcher/dispatcher.utils.d.ts +5 -5
- package/dist/effect/index.d.ts +2 -2
- package/dist/effect/request.effect.d.ts +14 -0
- package/dist/effect/request.effect.types.d.ts +32 -0
- package/dist/index.cjs.js +1 -1
- package/dist/index.cjs.js.map +4 -4
- package/dist/index.d.ts +3 -3
- package/dist/index.esm.js +1 -1
- package/dist/index.esm.js.map +4 -4
- package/dist/managers/index.d.ts +1 -1
- package/dist/managers/logger/logger.manager.d.ts +5 -5
- package/dist/managers/logger/logger.manager.types.d.ts +7 -7
- package/dist/managers/request/index.d.ts +4 -0
- package/dist/managers/request/request.manager.d.ts +43 -0
- package/dist/managers/request/request.manager.events.d.ts +36 -0
- package/dist/managers/{command/command.manager.types.d.ts → request/request.manager.types.d.ts} +6 -6
- package/dist/managers/{command/command.manager.utils.d.ts → request/request.manager.utils.d.ts} +0 -0
- package/dist/request/index.d.ts +3 -0
- package/dist/request/request.d.ts +98 -0
- package/dist/request/request.types.d.ts +202 -0
- package/dist/request/request.utils.d.ts +22 -0
- package/dist/types/fetch.types.d.ts +16 -16
- package/package.json +1 -1
- package/src/{client/fetch.client.bindings.ts → adapter/adapter.bindings.ts} +58 -58
- package/src/{client/fetch.client.browser.ts → adapter/adapter.browser.ts} +6 -6
- package/src/{client/fetch.client.constants.ts → adapter/adapter.constants.ts} +0 -0
- package/src/{client/fetch.client.server.ts → adapter/adapter.server.ts} +12 -12
- package/src/adapter/adapter.ts +8 -0
- package/src/adapter/adapter.types.ts +79 -0
- package/src/{client/fetch.client.utils.ts → adapter/adapter.utils.ts} +3 -3
- package/src/adapter/index.ts +7 -0
- package/src/cache/cache.ts +19 -19
- package/src/cache/cache.types.ts +4 -4
- package/src/cache/cache.utils.ts +6 -6
- package/src/{builder/builder.constants.ts → client/client.constants.ts} +0 -0
- package/src/client/client.ts +310 -0
- package/src/client/client.types.ts +52 -0
- package/src/{builder/builder.utils.ts → client/client.utils.ts} +34 -31
- package/src/client/index.ts +4 -6
- package/src/dispatcher/dispatcher.events.ts +11 -11
- package/src/dispatcher/dispatcher.ts +68 -68
- package/src/dispatcher/dispatcher.types.ts +10 -10
- package/src/dispatcher/dispatcher.utils.ts +7 -7
- package/src/effect/index.ts +2 -2
- package/src/effect/request.effect.ts +28 -0
- package/src/effect/request.effect.types.ts +35 -0
- package/src/index.ts +4 -4
- package/src/managers/index.ts +1 -1
- package/src/managers/logger/logger.manager.ts +8 -8
- package/src/managers/logger/logger.manager.types.ts +7 -7
- package/src/managers/request/index.ts +4 -0
- package/src/managers/{command/command.manager.events.ts → request/request.manager.events.ts} +47 -51
- package/src/managers/{command/command.manager.ts → request/request.manager.ts} +5 -5
- package/src/managers/request/request.manager.types.ts +25 -0
- package/src/managers/{command/command.manager.utils.ts → request/request.manager.utils.ts} +0 -0
- package/src/request/index.ts +3 -0
- package/src/{command/command.ts → request/request.ts} +188 -194
- package/src/request/request.types.ts +282 -0
- package/src/{command/command.utils.ts → request/request.utils.ts} +56 -53
- package/src/types/fetch.types.ts +18 -18
- package/dist/builder/builder.d.ts +0 -141
- package/dist/builder/builder.types.d.ts +0 -41
- package/dist/builder/builder.utils.d.ts +0 -10
- package/dist/builder/index.d.ts +0 -4
- package/dist/client/fetch.client.bindings.d.ts +0 -25
- package/dist/client/fetch.client.browser.d.ts +0 -2
- package/dist/client/fetch.client.d.ts +0 -2
- package/dist/client/fetch.client.server.d.ts +0 -2
- package/dist/client/fetch.client.types.d.ts +0 -57
- package/dist/command/command.d.ts +0 -98
- package/dist/command/command.types.d.ts +0 -202
- package/dist/command/command.utils.d.ts +0 -22
- package/dist/command/index.d.ts +0 -3
- package/dist/effect/fetch.effect.d.ts +0 -14
- package/dist/effect/fetch.effect.types.d.ts +0 -32
- package/dist/managers/command/command.manager.d.ts +0 -43
- package/dist/managers/command/command.manager.events.d.ts +0 -36
- package/dist/managers/command/index.d.ts +0 -4
- package/src/builder/builder.ts +0 -308
- package/src/builder/builder.types.ts +0 -53
- package/src/builder/index.ts +0 -4
- package/src/client/fetch.client.ts +0 -9
- package/src/client/fetch.client.types.ts +0 -83
- package/src/command/command.types.ts +0 -273
- package/src/command/index.ts +0 -3
- package/src/effect/fetch.effect.ts +0 -28
- package/src/effect/fetch.effect.types.ts +0 -35
- package/src/managers/command/command.manager.types.ts +0 -25
- package/src/managers/command/index.ts +0 -4
package/README.md
CHANGED
|
@@ -59,7 +59,7 @@ particular `caching`, `queuing`, `persistence`, `offline first support`, `reques
|
|
|
59
59
|
|
|
60
60
|
🔋 **Offline First** - [Read more](https://hyperfetch.bettertyped.com/docs/guides/Advanced/Offline)
|
|
61
61
|
|
|
62
|
-
📡 **Built-in
|
|
62
|
+
📡 **Built-in adapter** - [Read more](https://hyperfetch.bettertyped.com/docs/documentation/Core/Adapter)
|
|
63
63
|
|
|
64
64
|
🧪 **Easy to test** - [Read more](https://hyperfetch.bettertyped.com/docs/documentation/Getting%20Started/Testing)
|
|
65
65
|
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { ResponseSuccessType, ResponseErrorType, ProgressDataType } from "adapter";
|
|
2
|
+
import { RequestInstance } from "request";
|
|
3
|
+
import { ExtractResponseType, ExtractErrorType } from "types";
|
|
4
|
+
export declare const getAdapterBindings: <ConfigType = any>(cmd: RequestInstance, 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 RequestInstance>(callback: () => void, resolve: (value: ResponseErrorType<ExtractErrorType<T>>) => void) => () => void;
|
|
13
|
+
onBeforeRequest: () => void;
|
|
14
|
+
onRequestStart: (progress?: ProgressDataType) => number;
|
|
15
|
+
onRequestProgress: (progress: ProgressDataType) => number;
|
|
16
|
+
onRequestEnd: () => number;
|
|
17
|
+
onResponseStart: (progress?: ProgressDataType) => number;
|
|
18
|
+
onResponseProgress: (progress: ProgressDataType) => number;
|
|
19
|
+
onResponseEnd: () => number;
|
|
20
|
+
onSuccess: <T_1 extends RequestInstance>(responseData: unknown, status: number, resolve: (value: ResponseErrorType<ExtractErrorType<T_1>>) => void) => Promise<ResponseSuccessType<ExtractResponseType<T_1>>>;
|
|
21
|
+
onAbortError: <T_2 extends RequestInstance>(resolve: (value: ResponseErrorType<ExtractErrorType<T_2>>) => void) => Promise<ResponseErrorType<ExtractErrorType<T_2>>>;
|
|
22
|
+
onTimeoutError: <T_3 extends RequestInstance>(resolve: (value: ResponseErrorType<ExtractErrorType<T_3>>) => void) => Promise<ResponseErrorType<ExtractErrorType<T_3>>>;
|
|
23
|
+
onUnexpectedError: <T_4 extends RequestInstance>(resolve: (value: ResponseErrorType<ExtractErrorType<T_4>>) => void) => Promise<ResponseErrorType<ExtractErrorType<T_4>>>;
|
|
24
|
+
onError: <T_5 extends RequestInstance>(error: Error | ExtractErrorType<T_5>, status: number, resolve: (value: ResponseErrorType<ExtractErrorType<T_5>>) => void) => Promise<ResponseErrorType<ExtractErrorType<T_5>>>;
|
|
25
|
+
}>;
|
|
File without changes
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { RequestInstance } from "request";
|
|
2
|
+
export type AdapterType = (request: RequestInstance, requestId: string) => Promise<ResponseType<any, any>>;
|
|
3
|
+
export type AdapterOptionsType = Partial<XMLHttpRequest>;
|
|
4
|
+
export type AdapterPayloadMappingType = (data: unknown) => string | FormData;
|
|
5
|
+
export type ResponseType<GenericDataType, GenericErrorType> = [
|
|
6
|
+
GenericDataType | null,
|
|
7
|
+
GenericErrorType | null,
|
|
8
|
+
number | null
|
|
9
|
+
];
|
|
10
|
+
export type ResponseSuccessType<GenericDataType> = [GenericDataType, null, number | null];
|
|
11
|
+
export type ResponseErrorType<GenericErrorType> = [null, GenericErrorType, number | null];
|
|
12
|
+
export type QueryParamValuesType = number | string | boolean | null | undefined;
|
|
13
|
+
export type QueryParamType = QueryParamValuesType | Array<QueryParamValuesType> | Record<string, QueryParamValuesType>;
|
|
14
|
+
export type QueryParamsType = Record<string, QueryParamType>;
|
|
15
|
+
export type HeaderMappingType = <T extends RequestInstance>(request: T) => HeadersInit;
|
|
16
|
+
export type AdapterHeadersProps = {
|
|
17
|
+
isFormData: boolean;
|
|
18
|
+
headers: HeadersInit | undefined;
|
|
19
|
+
};
|
|
20
|
+
export type QueryStringifyOptionsType = {
|
|
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 ProgressDataType = {
|
|
47
|
+
total?: number;
|
|
48
|
+
loaded?: number;
|
|
49
|
+
};
|
|
50
|
+
export type ProgressType = {
|
|
51
|
+
progress: number;
|
|
52
|
+
timeLeft: number | null;
|
|
53
|
+
sizeLeft: number;
|
|
54
|
+
total: number;
|
|
55
|
+
loaded: number;
|
|
56
|
+
startTimestamp: number;
|
|
57
|
+
};
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { RequestInstance } from "request";
|
|
2
|
+
import { ExtractErrorType } from "types";
|
|
3
3
|
export declare const getErrorMessage: (errorCase?: "timeout" | "abort" | "deleted") => Error;
|
|
4
4
|
export declare const parseResponse: (response: string | unknown) => any;
|
|
5
|
-
export declare const parseErrorResponse: <T extends
|
|
5
|
+
export declare const parseErrorResponse: <T extends RequestInstance>(response: unknown) => ExtractErrorType<T>;
|
|
6
6
|
export declare const getUploadSize: (payload: FormData | string) => number;
|
|
7
7
|
export declare const fileToBuffer: (file: File | Blob) => Promise<Uint8Array>;
|
|
8
8
|
export declare const getStreamPayload: (payload: FormData | string | null) => string | Promise<(string | Uint8Array)[]>;
|
package/dist/cache/cache.d.ts
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
/// <reference types="node" />
|
|
3
3
|
import EventEmitter from "events";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
4
|
+
import { ResponseType } from "adapter";
|
|
5
|
+
import { ClientInstance } from "client";
|
|
6
|
+
import { ResponseDetailsType } from "managers";
|
|
7
7
|
import { CacheOptionsType, CacheAsyncStorageType, CacheStorageType, getCacheEvents, CacheValueType } from "cache";
|
|
8
|
-
import {
|
|
8
|
+
import { RequestDump, RequestInstance } from "request";
|
|
9
9
|
/**
|
|
10
10
|
* Cache class handles the data exchange with the dispatchers.
|
|
11
11
|
*
|
|
12
12
|
* @note
|
|
13
|
-
* Keys used to save the values are created dynamically on the
|
|
13
|
+
* Keys used to save the values are created dynamically on the Request class
|
|
14
14
|
*
|
|
15
15
|
*/
|
|
16
16
|
export declare class Cache {
|
|
17
|
-
|
|
17
|
+
client: ClientInstance;
|
|
18
18
|
options?: CacheOptionsType;
|
|
19
19
|
emitter: EventEmitter;
|
|
20
20
|
events: ReturnType<typeof getCacheEvents>;
|
|
@@ -23,15 +23,15 @@ export declare class Cache {
|
|
|
23
23
|
clearKey: string;
|
|
24
24
|
garbageCollectors: Map<string, NodeJS.Timeout>;
|
|
25
25
|
private logger;
|
|
26
|
-
constructor(
|
|
26
|
+
constructor(client: ClientInstance, options?: CacheOptionsType);
|
|
27
27
|
/**
|
|
28
28
|
* Set the cache data to the storage
|
|
29
|
-
* @param
|
|
29
|
+
* @param request
|
|
30
30
|
* @param response
|
|
31
31
|
* @param details
|
|
32
32
|
* @returns
|
|
33
33
|
*/
|
|
34
|
-
set: <Response_1, Error_1>(
|
|
34
|
+
set: <Response_1, Error_1>(request: RequestInstance | RequestDump<RequestInstance>, response: ResponseType<Response_1, Error_1>, details: ResponseDetailsType) => void;
|
|
35
35
|
/**
|
|
36
36
|
* Get particular record from storage by cacheKey. It will trigger lazyStorage to emit lazy load event for reading it's data.
|
|
37
37
|
* @param cacheKey
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Cache } from "cache";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { ResponseType } from "adapter";
|
|
3
|
+
import { ResponseDetailsType } from "managers";
|
|
4
4
|
export type CacheOptionsType = {
|
|
5
5
|
/**
|
|
6
6
|
* Assign your custom sync storage
|
|
@@ -28,8 +28,8 @@ export type CacheOptionsType = {
|
|
|
28
28
|
onDelete?: (key: string) => void;
|
|
29
29
|
};
|
|
30
30
|
export type CacheValueType<Response = any, Error = any> = {
|
|
31
|
-
data:
|
|
32
|
-
details:
|
|
31
|
+
data: ResponseType<Response, Error>;
|
|
32
|
+
details: ResponseDetailsType;
|
|
33
33
|
cacheTime: number;
|
|
34
34
|
clearKey: string;
|
|
35
35
|
garbageCollection: number;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
export declare const getCacheData: <T extends
|
|
1
|
+
import { RequestInstance } from "request";
|
|
2
|
+
import { ExtractAdapterReturnType } from "types";
|
|
3
|
+
export declare const getCacheData: <T extends RequestInstance>(previousResponse: ExtractAdapterReturnType<T>, response: ExtractAdapterReturnType<T>) => ExtractAdapterReturnType<T>;
|
|
4
4
|
export declare const getRevalidateEventKey: (key: string) => string;
|
|
5
5
|
export declare const getCacheKey: (key: string) => string;
|
|
6
6
|
export declare const getCacheIdKey: (key: string) => string;
|
|
File without changes
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { AdapterType, AdapterOptionsType, ResponseType, QueryParamsType, QueryStringifyOptionsType, HeaderMappingType, AdapterPayloadMappingType } from "adapter";
|
|
2
|
+
import { ClientOptionsType, ClientInstance, ClientErrorType, StringifyCallbackType, RequestInterceptorType, ResponseInterceptorType } from "client";
|
|
3
|
+
import { Cache } from "cache";
|
|
4
|
+
import { Dispatcher } from "dispatcher";
|
|
5
|
+
import { RequestEffectInstance } from "effect";
|
|
6
|
+
import { Request, RequestOptionsType, RequestInstance } from "request";
|
|
7
|
+
import { AppManager, RequestManager, LoggerManager, SeverityType } from "managers";
|
|
8
|
+
/**
|
|
9
|
+
* **Client** is a class that allows you to configure the connection with the server and then use it to create
|
|
10
|
+
* requests which, when called using the appropriate method, will cause the server to be queried for the endpoint and
|
|
11
|
+
* method specified in the request.
|
|
12
|
+
* @position 1
|
|
13
|
+
*/
|
|
14
|
+
export declare class Client<GlobalErrorType extends ClientErrorType = Error, AdapterOptions = AdapterOptionsType> {
|
|
15
|
+
options: ClientOptionsType;
|
|
16
|
+
readonly url: string;
|
|
17
|
+
debug: boolean;
|
|
18
|
+
__onErrorCallbacks: ResponseInterceptorType[];
|
|
19
|
+
__onSuccessCallbacks: ResponseInterceptorType[];
|
|
20
|
+
__onResponseCallbacks: ResponseInterceptorType[];
|
|
21
|
+
__onAuthCallbacks: RequestInterceptorType[];
|
|
22
|
+
__onRequestCallbacks: RequestInterceptorType[];
|
|
23
|
+
requestManager: RequestManager;
|
|
24
|
+
appManager: AppManager;
|
|
25
|
+
loggerManager: LoggerManager;
|
|
26
|
+
adapter: AdapterType;
|
|
27
|
+
cache: Cache;
|
|
28
|
+
fetchDispatcher: Dispatcher;
|
|
29
|
+
submitDispatcher: Dispatcher;
|
|
30
|
+
effects: RequestEffectInstance[];
|
|
31
|
+
queryParamsConfig?: QueryStringifyOptionsType;
|
|
32
|
+
adapterDefaultOptions?: (request: RequestInstance) => AdapterOptions;
|
|
33
|
+
requestDefaultOptions?: (options: RequestOptionsType<string, AdapterOptions>) => Partial<RequestOptionsType<string, AdapterOptions>>;
|
|
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: HeaderMappingType;
|
|
42
|
+
/**
|
|
43
|
+
* Method to get request data and transform them to the required format. It handles FormData and JSON by default.
|
|
44
|
+
*/
|
|
45
|
+
payloadMapper: AdapterPayloadMappingType;
|
|
46
|
+
logger: import("managers").LoggerType;
|
|
47
|
+
constructor(options: ClientOptionsType);
|
|
48
|
+
/**
|
|
49
|
+
* This method allows to configure global defaults for the request configuration like method, auth, deduplication etc.
|
|
50
|
+
*/
|
|
51
|
+
setRequestDefaultOptions: (callback: (request: RequestInstance) => Partial<RequestOptionsType<string, AdapterOptions>>) => Client<GlobalErrorType, AdapterOptions>;
|
|
52
|
+
setAdapterDefaultOptions: (callback: (request: RequestInstance) => AdapterOptions) => Client<GlobalErrorType, AdapterOptions>;
|
|
53
|
+
/**
|
|
54
|
+
* This method enables the logger usage and display the logs in console
|
|
55
|
+
*/
|
|
56
|
+
setDebug: (debug: boolean) => Client<GlobalErrorType, AdapterOptions>;
|
|
57
|
+
/**
|
|
58
|
+
* Set the logger severity of the messages displayed to the console
|
|
59
|
+
*/
|
|
60
|
+
setLoggerSeverity: (severity: SeverityType) => Client<GlobalErrorType, AdapterOptions>;
|
|
61
|
+
/**
|
|
62
|
+
* Set the new logger instance to the Client
|
|
63
|
+
*/
|
|
64
|
+
setLogger: (callback: (Client: ClientInstance) => LoggerManager) => Client<GlobalErrorType, AdapterOptions>;
|
|
65
|
+
/**
|
|
66
|
+
* Set config for the query params stringify method, we can set here, among others, arrayFormat, skipNull, encode, skipEmptyString and more
|
|
67
|
+
*/
|
|
68
|
+
setQueryParamsConfig: (queryParamsConfig: QueryStringifyOptionsType) => Client<GlobalErrorType, AdapterOptions>;
|
|
69
|
+
/**
|
|
70
|
+
* Set the custom query params stringify method to the Client
|
|
71
|
+
* @param stringifyFn Custom callback handling query params stringify
|
|
72
|
+
*/
|
|
73
|
+
setStringifyQueryParams: (stringifyFn: StringifyCallbackType) => Client<GlobalErrorType, AdapterOptions>;
|
|
74
|
+
/**
|
|
75
|
+
* Set the custom header mapping function
|
|
76
|
+
*/
|
|
77
|
+
setHeaderMapper: (headerMapper: HeaderMappingType) => Client<GlobalErrorType, AdapterOptions>;
|
|
78
|
+
/**
|
|
79
|
+
* Set the request payload mapping function which get triggered before request get send
|
|
80
|
+
*/
|
|
81
|
+
setPayloadMapper: (payloadMapper: AdapterPayloadMappingType) => Client<GlobalErrorType, AdapterOptions>;
|
|
82
|
+
/**
|
|
83
|
+
* Set custom http adapter to handle graphql, rest, firebase or other
|
|
84
|
+
*/
|
|
85
|
+
setAdapter: (callback: (Client: ClientInstance) => AdapterType) => Client<GlobalErrorType, AdapterOptions>;
|
|
86
|
+
/**
|
|
87
|
+
* Method of manipulating requests before sending the request. We can for example add custom header with token to the request which request had the auth set to true.
|
|
88
|
+
*/
|
|
89
|
+
onAuth: (callback: RequestInterceptorType) => Client<GlobalErrorType, AdapterOptions>;
|
|
90
|
+
/**
|
|
91
|
+
* Method for intercepting error responses. It can be used for example to refresh tokens.
|
|
92
|
+
*/
|
|
93
|
+
onError: <ErrorType = null>(callback: ResponseInterceptorType<any, GlobalErrorType | ErrorType>) => Client<GlobalErrorType, AdapterOptions>;
|
|
94
|
+
/**
|
|
95
|
+
* Method for intercepting success responses.
|
|
96
|
+
*/
|
|
97
|
+
onSuccess: <ErrorType = null>(callback: ResponseInterceptorType<any, GlobalErrorType | ErrorType>) => Client<GlobalErrorType, AdapterOptions>;
|
|
98
|
+
/**
|
|
99
|
+
* Method of manipulating requests before sending the request.
|
|
100
|
+
*/
|
|
101
|
+
onRequest: (callback: RequestInterceptorType) => Client<GlobalErrorType, AdapterOptions>;
|
|
102
|
+
/**
|
|
103
|
+
* Method for intercepting any responses.
|
|
104
|
+
*/
|
|
105
|
+
onResponse: <ErrorType = null>(callback: ResponseInterceptorType<any, GlobalErrorType | ErrorType>) => Client<GlobalErrorType, AdapterOptions>;
|
|
106
|
+
/**
|
|
107
|
+
* Add persistent effects which trigger on the request lifecycle
|
|
108
|
+
*/
|
|
109
|
+
addEffect: (effect: RequestEffectInstance | RequestEffectInstance[]) => this;
|
|
110
|
+
/**
|
|
111
|
+
* Remove effects from Client
|
|
112
|
+
*/
|
|
113
|
+
removeEffect: (effect: RequestEffectInstance | string) => this;
|
|
114
|
+
/**
|
|
115
|
+
* Helper used by http adapter to apply the modifications on response error
|
|
116
|
+
*/
|
|
117
|
+
__modifyAuth: (request: RequestInstance) => Promise<RequestInstance>;
|
|
118
|
+
/**
|
|
119
|
+
* Private helper to run async pre-request processing
|
|
120
|
+
*/
|
|
121
|
+
__modifyRequest: (request: RequestInstance) => Promise<RequestInstance>;
|
|
122
|
+
/**
|
|
123
|
+
* Private helper to run async on-error response processing
|
|
124
|
+
*/
|
|
125
|
+
__modifyErrorResponse: (response: ResponseType<any, GlobalErrorType>, request: RequestInstance) => Promise<ResponseType<any, GlobalErrorType>>;
|
|
126
|
+
/**
|
|
127
|
+
* Private helper to run async on-success response processing
|
|
128
|
+
*/
|
|
129
|
+
__modifySuccessResponse: (response: ResponseType<any, GlobalErrorType>, request: RequestInstance) => Promise<ResponseType<any, GlobalErrorType>>;
|
|
130
|
+
/**
|
|
131
|
+
* Private helper to run async response processing
|
|
132
|
+
*/
|
|
133
|
+
__modifyResponse: (response: ResponseType<any, GlobalErrorType>, request: RequestInstance) => Promise<ResponseType<any, GlobalErrorType>>;
|
|
134
|
+
/**
|
|
135
|
+
* Clears the Client instance and remove all listeners on it's dependencies
|
|
136
|
+
*/
|
|
137
|
+
clear: () => void;
|
|
138
|
+
/**
|
|
139
|
+
* Create requests based on the Client setup
|
|
140
|
+
*/
|
|
141
|
+
createRequest: <Response_1, Payload = undefined, LocalError extends ClientErrorType = undefined, QueryParams extends string | QueryParamsType = string | QueryParamsType>() => <EndpointType extends string>(params: RequestOptionsType<EndpointType, AdapterOptions>) => Request<Response_1, Payload, QueryParams, GlobalErrorType, LocalError, EndpointType, AdapterOptions, false, false, false, undefined>;
|
|
142
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Cache } from "cache";
|
|
2
|
+
import { RequestInstance } from "request";
|
|
3
|
+
import { Dispatcher } from "dispatcher";
|
|
4
|
+
import { ResponseType, AdapterType, QueryParamsType } from "adapter";
|
|
5
|
+
import { Client } from "client";
|
|
6
|
+
import { AppManager } from "managers";
|
|
7
|
+
import { NegativeTypes } from "types";
|
|
8
|
+
export type ClientErrorType = Record<string, any> | string;
|
|
9
|
+
export type ClientInstance = Client<any, any>;
|
|
10
|
+
/**
|
|
11
|
+
* Configuration setup for the client
|
|
12
|
+
*/
|
|
13
|
+
export type ClientOptionsType = {
|
|
14
|
+
/**
|
|
15
|
+
* Url to your server
|
|
16
|
+
*/
|
|
17
|
+
url: string;
|
|
18
|
+
/**
|
|
19
|
+
* Custom adapter initialization prop
|
|
20
|
+
*/
|
|
21
|
+
adapter?: AdapterType;
|
|
22
|
+
/**
|
|
23
|
+
* Custom cache initialization prop
|
|
24
|
+
*/
|
|
25
|
+
cache?: <B extends ClientInstance, C extends Cache>(client: B) => C;
|
|
26
|
+
/**
|
|
27
|
+
* Custom app manager initialization prop
|
|
28
|
+
*/
|
|
29
|
+
appManager?: <B extends ClientInstance, A extends AppManager>(client: B) => A;
|
|
30
|
+
/**
|
|
31
|
+
* Custom fetch dispatcher initialization prop
|
|
32
|
+
*/
|
|
33
|
+
fetchDispatcher?: <B extends ClientInstance, D extends Dispatcher>(client: B) => D;
|
|
34
|
+
/**
|
|
35
|
+
* Custom submit dispatcher initialization prop
|
|
36
|
+
*/
|
|
37
|
+
submitDispatcher?: <B extends ClientInstance, D extends Dispatcher>(client: B) => D;
|
|
38
|
+
};
|
|
39
|
+
export type RequestInterceptorType = (request: RequestInstance) => Promise<RequestInstance> | RequestInstance;
|
|
40
|
+
export type ResponseInterceptorType<Response = any, Error = any> = (response: ResponseType<Response, Error>, request: RequestInstance) => Promise<ResponseType<any, any>> | ResponseType<any, any>;
|
|
41
|
+
export type StringifyCallbackType = (queryParams: QueryParamsType | string | NegativeTypes) => string;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { QueryParamsType, ResponseType, QueryStringifyOptionsType } from "adapter";
|
|
2
|
+
import { RequestInstance } from "request";
|
|
3
|
+
import { NegativeTypes } from "types";
|
|
4
|
+
import { RequestInterceptorType, ResponseInterceptorType } from "./client.types";
|
|
5
|
+
export declare const stringifyValue: (response: string | unknown) => string;
|
|
6
|
+
export declare const interceptRequest: (interceptors: RequestInterceptorType[], request: RequestInstance) => Promise<RequestInstance>;
|
|
7
|
+
export declare const interceptResponse: <GlobalErrorType>(interceptors: ResponseInterceptorType[], response: ResponseType<any, GlobalErrorType>, request: RequestInstance) => Promise<ResponseType<any, GlobalErrorType>>;
|
|
8
|
+
export declare const getAdapterHeaders: (request: RequestInstance) => HeadersInit;
|
|
9
|
+
export declare const getAdapterPayload: (data: unknown) => string | FormData;
|
|
10
|
+
export declare const stringifyQueryParams: (queryParams: QueryParamsType | string | NegativeTypes, options?: QueryStringifyOptionsType) => string;
|
package/dist/client/index.d.ts
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
export * from "./
|
|
2
|
-
export * from "./
|
|
3
|
-
export * from "./
|
|
4
|
-
export * from "./
|
|
5
|
-
export * from "./fetch.client.bindings";
|
|
6
|
-
export * from "./fetch.client.constants";
|
|
1
|
+
export * from "./client";
|
|
2
|
+
export * from "./client.utils";
|
|
3
|
+
export * from "./client.types";
|
|
4
|
+
export * from "./client.constants";
|
|
@@ -1,29 +1,29 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import EventEmitter from "events";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
3
|
+
import { QueueDataType, DispatcherOptionsType, DispatcherStorageType, DispatcherStorageValueType, RunningRequestValueType } from "dispatcher";
|
|
4
|
+
import { ClientInstance } from "client";
|
|
5
|
+
import { RequestInstance } from "request";
|
|
6
6
|
/**
|
|
7
|
-
* Dispatcher class was made to store controlled request Fetches, and firing them all-at-once or one-by-one in
|
|
7
|
+
* Dispatcher class was made to store controlled request Fetches, and firing them all-at-once or one-by-one in request queue.
|
|
8
8
|
* Generally requests should be flushed at the same time, the queue provide mechanism to fire them in the order.
|
|
9
9
|
*/
|
|
10
10
|
export declare class Dispatcher {
|
|
11
|
-
|
|
11
|
+
client: ClientInstance;
|
|
12
12
|
options?: DispatcherOptionsType;
|
|
13
13
|
emitter: EventEmitter;
|
|
14
14
|
events: {
|
|
15
|
-
setDrained: <
|
|
16
|
-
setQueueStatus: <
|
|
17
|
-
setQueueChanged: <
|
|
18
|
-
onDrained: <
|
|
19
|
-
onQueueStatus: <
|
|
20
|
-
onQueueChange: <
|
|
15
|
+
setDrained: <Request_1 extends RequestInstance>(queueKey: string, values: QueueDataType<Request_1>) => void;
|
|
16
|
+
setQueueStatus: <Request_2 extends RequestInstance>(queueKey: string, values: QueueDataType<Request_2>) => void;
|
|
17
|
+
setQueueChanged: <Request_3 extends RequestInstance>(queueKey: string, values: QueueDataType<Request_3>) => void;
|
|
18
|
+
onDrained: <Request_4 extends RequestInstance>(queueKey: string, callback: (values: QueueDataType<Request_4>) => void) => VoidFunction;
|
|
19
|
+
onQueueStatus: <Request_5 extends RequestInstance>(queueKey: string, callback: (values: QueueDataType<Request_5>) => void) => VoidFunction;
|
|
20
|
+
onQueueChange: <Request_6 extends RequestInstance>(queueKey: string, callback: (values: QueueDataType<Request_6>) => void) => VoidFunction;
|
|
21
21
|
};
|
|
22
22
|
storage: DispatcherStorageType;
|
|
23
23
|
private requestCount;
|
|
24
24
|
private runningRequests;
|
|
25
25
|
private logger;
|
|
26
|
-
constructor(
|
|
26
|
+
constructor(client: ClientInstance, options?: DispatcherOptionsType);
|
|
27
27
|
/**
|
|
28
28
|
* Start request handling by queueKey
|
|
29
29
|
*/
|
|
@@ -43,11 +43,11 @@ export declare class Dispatcher {
|
|
|
43
43
|
/**
|
|
44
44
|
* Return queue state object
|
|
45
45
|
*/
|
|
46
|
-
getQueue: <
|
|
46
|
+
getQueue: <Request_1 extends RequestInstance = RequestInstance>(queueKey: string) => QueueDataType<Request_1>;
|
|
47
47
|
/**
|
|
48
48
|
* Return request from queue state
|
|
49
49
|
*/
|
|
50
|
-
getRequest: <
|
|
50
|
+
getRequest: <Request_1 extends RequestInstance = RequestInstance>(queueKey: string, requestId: string) => DispatcherStorageValueType<Request_1>;
|
|
51
51
|
/**
|
|
52
52
|
* Get value of the active queue status based on the stopped status
|
|
53
53
|
*/
|
|
@@ -55,11 +55,11 @@ export declare class Dispatcher {
|
|
|
55
55
|
/**
|
|
56
56
|
* Add new element to storage
|
|
57
57
|
*/
|
|
58
|
-
addQueueElement: <
|
|
58
|
+
addQueueElement: <Request_1 extends RequestInstance = RequestInstance>(queueKey: string, dispatcherDump: DispatcherStorageValueType<Request_1>) => void;
|
|
59
59
|
/**
|
|
60
60
|
* Set new queue storage value
|
|
61
61
|
*/
|
|
62
|
-
setQueue: <
|
|
62
|
+
setQueue: <Request_1 extends RequestInstance = RequestInstance>(queueKey: string, queue: QueueDataType<Request_1>) => QueueDataType<Request_1>;
|
|
63
63
|
/**
|
|
64
64
|
* Clear requests from queue cache
|
|
65
65
|
*/
|
|
@@ -102,7 +102,7 @@ export declare class Dispatcher {
|
|
|
102
102
|
/**
|
|
103
103
|
* Add request to the running requests list
|
|
104
104
|
*/
|
|
105
|
-
addRunningRequest: (queueKey: string, requestId: string,
|
|
105
|
+
addRunningRequest: (queueKey: string, requestId: string, request: RequestInstance) => void;
|
|
106
106
|
/**
|
|
107
107
|
* Get the value based on the currently running requests
|
|
108
108
|
*/
|
|
@@ -136,20 +136,20 @@ export declare class Dispatcher {
|
|
|
136
136
|
*/
|
|
137
137
|
incrementQueueRequestCount: (queueKey: string) => void;
|
|
138
138
|
/**
|
|
139
|
-
* Create storage element from
|
|
139
|
+
* Create storage element from request
|
|
140
140
|
*/
|
|
141
|
-
createStorageElement: <
|
|
141
|
+
createStorageElement: <Request_1 extends RequestInstance>(request: Request_1) => DispatcherStorageValueType<Request_1>;
|
|
142
142
|
/**
|
|
143
|
-
* Add
|
|
143
|
+
* Add request to the dispatcher handler
|
|
144
144
|
*/
|
|
145
|
-
add: (
|
|
145
|
+
add: (request: RequestInstance) => string;
|
|
146
146
|
/**
|
|
147
147
|
* Delete from the storage and cancel request
|
|
148
148
|
*/
|
|
149
|
-
delete: (queueKey: string, requestId: string, abortKey: string) =>
|
|
149
|
+
delete: (queueKey: string, requestId: string, abortKey: string) => QueueDataType<RequestInstance>;
|
|
150
150
|
/**
|
|
151
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
152
|
* It can be different once the previous call was set as cancelled and removed from queue before this request got resolved
|
|
153
153
|
*/
|
|
154
|
-
performRequest: (storageElement:
|
|
154
|
+
performRequest: (storageElement: DispatcherStorageValueType) => Promise<void | QueueDataType<RequestInstance>>;
|
|
155
155
|
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import EventEmitter from "events";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { QueueDataType } from "dispatcher";
|
|
3
|
+
import { RequestInstance } from "request";
|
|
4
4
|
export declare const getDispatcherEvents: (emitter: EventEmitter) => {
|
|
5
|
-
setDrained: <
|
|
6
|
-
setQueueStatus: <
|
|
7
|
-
setQueueChanged: <
|
|
8
|
-
onDrained: <
|
|
9
|
-
onQueueStatus: <
|
|
10
|
-
onQueueChange: <
|
|
5
|
+
setDrained: <Request_1 extends RequestInstance>(queueKey: string, values: QueueDataType<Request_1>) => void;
|
|
6
|
+
setQueueStatus: <Request_2 extends RequestInstance>(queueKey: string, values: QueueDataType<Request_2>) => void;
|
|
7
|
+
setQueueChanged: <Request_3 extends RequestInstance>(queueKey: string, values: QueueDataType<Request_3>) => void;
|
|
8
|
+
onDrained: <Request_4 extends RequestInstance>(queueKey: string, callback: (values: QueueDataType<Request_4>) => void) => VoidFunction;
|
|
9
|
+
onQueueStatus: <Request_5 extends RequestInstance>(queueKey: string, callback: (values: QueueDataType<Request_5>) => void) => VoidFunction;
|
|
10
|
+
onQueueChange: <Request_6 extends RequestInstance>(queueKey: string, callback: (values: QueueDataType<Request_6>) => void) => VoidFunction;
|
|
11
11
|
};
|
|
@@ -1,31 +1,31 @@
|
|
|
1
1
|
import { Dispatcher } from "dispatcher";
|
|
2
|
-
import {
|
|
2
|
+
import { RequestDump, RequestInstance } from "request";
|
|
3
3
|
export type DispatcherOptionsType = {
|
|
4
4
|
storage?: DispatcherStorageType;
|
|
5
5
|
onInitialization?: (dispatcherInstance: Dispatcher) => void;
|
|
6
|
-
onUpdateStorage?: <
|
|
7
|
-
onDeleteFromStorage?: <
|
|
6
|
+
onUpdateStorage?: <Request extends RequestInstance>(queueKey: string, data: QueueDataType<Request>) => void;
|
|
7
|
+
onDeleteFromStorage?: <Request extends RequestInstance>(queueKey: string, data: QueueDataType<Request>) => void;
|
|
8
8
|
onClearStorage?: (dispatcherInstance: Dispatcher) => void;
|
|
9
9
|
};
|
|
10
|
-
export type
|
|
10
|
+
export type DispatcherStorageValueType<Request extends RequestInstance = RequestInstance> = {
|
|
11
11
|
requestId: string;
|
|
12
|
-
|
|
12
|
+
requestDump: RequestDump<Request>;
|
|
13
13
|
retries: number;
|
|
14
14
|
timestamp: number;
|
|
15
15
|
stopped: boolean;
|
|
16
16
|
};
|
|
17
|
-
export type
|
|
18
|
-
requests:
|
|
17
|
+
export type QueueDataType<Request extends RequestInstance = RequestInstance> = {
|
|
18
|
+
requests: DispatcherStorageValueType<Request>[];
|
|
19
19
|
stopped: boolean;
|
|
20
20
|
};
|
|
21
21
|
export type DispatcherStorageType = {
|
|
22
|
-
set: <
|
|
23
|
-
get: <
|
|
22
|
+
set: <Request extends RequestInstance = RequestInstance>(key: string, data: QueueDataType<Request>) => void;
|
|
23
|
+
get: <Request extends RequestInstance = RequestInstance>(key: string) => QueueDataType<Request> | undefined;
|
|
24
24
|
keys: () => string[] | IterableIterator<string>;
|
|
25
25
|
delete: (key: string) => void;
|
|
26
26
|
clear: () => void;
|
|
27
27
|
};
|
|
28
28
|
export type RunningRequestValueType = {
|
|
29
29
|
requestId: string;
|
|
30
|
-
|
|
30
|
+
request: RequestInstance;
|
|
31
31
|
};
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { DispatcherRequestType,
|
|
1
|
+
import { RequestInstance } from "request";
|
|
2
|
+
import { ResponseType } from "adapter";
|
|
3
|
+
import { DispatcherRequestType, DispatcherStorageValueType } from "dispatcher";
|
|
4
4
|
export declare const getDispatcherDrainedEventKey: (key: string) => string;
|
|
5
5
|
export declare const getDispatcherStatusEventKey: (key: string) => string;
|
|
6
6
|
export declare const getDispatcherChangeEventKey: (key: string) => string;
|
|
7
7
|
export declare const getIsEqualTimestamp: (currentTimestamp: number, threshold: number, queueTimestamp?: number) => boolean;
|
|
8
8
|
export declare const canRetryRequest: (currentRetries: number, retry: number | undefined) => boolean;
|
|
9
|
-
export declare const getRequestType: (
|
|
10
|
-
export declare const isFailedRequest: (data:
|
|
9
|
+
export declare const getRequestType: (request: RequestInstance, latestRequest: DispatcherStorageValueType | undefined) => DispatcherRequestType;
|
|
10
|
+
export declare const isFailedRequest: (data: ResponseType<unknown, unknown>) => boolean;
|
package/dist/effect/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from "./
|
|
2
|
-
export * from "./
|
|
1
|
+
export * from "./request.effect";
|
|
2
|
+
export * from "./request.effect.types";
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { ExtractErrorType, ExtractResponseType } from "types";
|
|
2
|
+
import { RequestEffectOptionsType } from "effect";
|
|
3
|
+
import { RequestInstance } from "request";
|
|
4
|
+
import { ResponseErrorType, ResponseType, ResponseSuccessType } from "adapter";
|
|
5
|
+
export declare class RequestEffect<T extends RequestInstance> {
|
|
6
|
+
config: RequestEffectOptionsType<T>;
|
|
7
|
+
constructor(config: RequestEffectOptionsType<T>);
|
|
8
|
+
getEffectKey: () => string;
|
|
9
|
+
onTrigger: (request: T) => void;
|
|
10
|
+
onStart: (request: T) => void;
|
|
11
|
+
onSuccess: (response: ResponseSuccessType<ExtractResponseType<T>>, request: T) => void;
|
|
12
|
+
onError: (response: ResponseErrorType<ExtractErrorType<T>>, request: T) => void;
|
|
13
|
+
onFinished: (response: ResponseType<ExtractResponseType<T>, ExtractErrorType<T>>, request: T) => void;
|
|
14
|
+
}
|