@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.
- package/.eslintrc.json +7 -0
- package/README.md +83 -0
- package/dist/builder/builder.constants.d.ts +9 -0
- package/dist/builder/builder.d.ts +141 -0
- package/dist/builder/builder.types.d.ts +45 -0
- package/dist/builder/builder.utils.d.ts +10 -0
- package/dist/builder/index.d.ts +4 -0
- package/dist/cache/cache.d.ts +76 -0
- package/dist/cache/cache.events.d.ts +28 -0
- package/dist/cache/cache.types.d.ts +49 -0
- package/dist/cache/cache.utils.d.ts +6 -0
- package/dist/cache/index.d.ts +4 -0
- package/dist/client/fetch.client.bindings.d.ts +25 -0
- package/dist/client/fetch.client.constants.d.ts +1 -0
- package/dist/client/fetch.client.d.ts +2 -0
- package/dist/client/fetch.client.types.d.ts +57 -0
- package/dist/client/fetch.client.utils.d.ts +5 -0
- package/dist/client/index.d.ts +5 -0
- package/dist/command/command.d.ts +87 -0
- package/dist/command/command.types.d.ts +196 -0
- package/dist/command/command.utils.d.ts +22 -0
- package/dist/command/index.d.ts +3 -0
- package/dist/constants/http.constants.d.ts +7 -0
- package/dist/constants/time.constants.d.ts +11 -0
- package/dist/dispatcher/dispatcher.constants.d.ts +6 -0
- package/dist/dispatcher/dispatcher.d.ts +155 -0
- package/dist/dispatcher/dispatcher.events.d.ts +11 -0
- package/dist/dispatcher/dispatcher.types.d.ts +31 -0
- package/dist/dispatcher/dispatcher.utils.d.ts +10 -0
- package/dist/dispatcher/index.d.ts +5 -0
- package/dist/effect/fetch.effect.d.ts +14 -0
- package/dist/effect/fetch.effect.types.d.ts +32 -0
- package/dist/effect/index.d.ts +2 -0
- package/dist/index.cjs.js +2 -0
- package/dist/index.cjs.js.map +7 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.esm.js +2 -0
- package/dist/index.esm.js.map +7 -0
- package/dist/managers/app/app.manager.constants.d.ts +9 -0
- package/dist/managers/app/app.manager.d.ts +35 -0
- package/dist/managers/app/app.manager.events.d.ts +11 -0
- package/dist/managers/app/app.manager.types.d.ts +7 -0
- package/dist/managers/app/app.manager.utils.d.ts +4 -0
- package/dist/managers/app/index.d.ts +5 -0
- package/dist/managers/command/command.manager.d.ts +43 -0
- package/dist/managers/command/command.manager.events.d.ts +36 -0
- package/dist/managers/command/command.manager.types.d.ts +23 -0
- package/dist/managers/command/command.manager.utils.d.ts +16 -0
- package/dist/managers/command/index.d.ts +4 -0
- package/dist/managers/index.d.ts +3 -0
- package/dist/managers/logger/index.d.ts +4 -0
- package/dist/managers/logger/logger.manager.constants.d.ts +4 -0
- package/dist/managers/logger/logger.manager.d.ts +19 -0
- package/dist/managers/logger/logger.manager.types.d.ts +30 -0
- package/dist/managers/logger/logger.manager.utils.d.ts +3 -0
- package/dist/types/fetch.types.d.ts +15 -0
- package/dist/types/helpers.types.d.ts +11 -0
- package/dist/types/http.types.d.ts +1 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/uuid.utils.d.ts +1 -0
- package/package.json +60 -0
- package/src/builder/builder.constants.ts +9 -0
- package/src/builder/builder.ts +309 -0
- package/src/builder/builder.types.ts +57 -0
- package/src/builder/builder.utils.ts +178 -0
- package/src/builder/index.ts +4 -0
- package/src/cache/cache.events.ts +44 -0
- package/src/cache/cache.ts +221 -0
- package/src/cache/cache.types.ts +56 -0
- package/src/cache/cache.utils.ts +29 -0
- package/src/cache/index.ts +4 -0
- package/src/client/fetch.client.bindings.ts +290 -0
- package/src/client/fetch.client.constants.ts +3 -0
- package/src/client/fetch.client.ts +98 -0
- package/src/client/fetch.client.types.ts +83 -0
- package/src/client/fetch.client.utils.ts +28 -0
- package/src/client/index.ts +5 -0
- package/src/command/command.ts +539 -0
- package/src/command/command.types.ts +268 -0
- package/src/command/command.utils.ts +179 -0
- package/src/command/index.ts +3 -0
- package/src/constants/http.constants.ts +7 -0
- package/src/constants/time.constants.ts +11 -0
- package/src/dispatcher/dispatcher.constants.ts +6 -0
- package/src/dispatcher/dispatcher.events.ts +42 -0
- package/src/dispatcher/dispatcher.ts +596 -0
- package/src/dispatcher/dispatcher.types.ts +39 -0
- package/src/dispatcher/dispatcher.utils.ts +55 -0
- package/src/dispatcher/index.ts +5 -0
- package/src/effect/fetch.effect.ts +28 -0
- package/src/effect/fetch.effect.types.ts +35 -0
- package/src/effect/index.ts +2 -0
- package/src/index.ts +12 -0
- package/src/managers/app/app.manager.constants.ts +24 -0
- package/src/managers/app/app.manager.events.ts +34 -0
- package/src/managers/app/app.manager.ts +74 -0
- package/src/managers/app/app.manager.types.ts +8 -0
- package/src/managers/app/app.manager.utils.ts +34 -0
- package/src/managers/app/index.ts +5 -0
- package/src/managers/command/command.manager.events.ts +212 -0
- package/src/managers/command/command.manager.ts +68 -0
- package/src/managers/command/command.manager.types.ts +25 -0
- package/src/managers/command/command.manager.utils.ts +19 -0
- package/src/managers/command/index.ts +4 -0
- package/src/managers/index.ts +3 -0
- package/src/managers/logger/index.ts +4 -0
- package/src/managers/logger/logger.manager.constants.ts +24 -0
- package/src/managers/logger/logger.manager.ts +70 -0
- package/src/managers/logger/logger.manager.types.ts +35 -0
- package/src/managers/logger/logger.manager.utils.ts +27 -0
- package/src/types/fetch.types.ts +39 -0
- package/src/types/helpers.types.ts +15 -0
- package/src/types/http.types.ts +1 -0
- package/src/types/index.ts +3 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/uuid.utils.ts +3 -0
- package/tsconfig.json +8 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import EventEmitter from "events";
|
|
3
|
+
import { BuilderInstance } from "../../builder";
|
|
4
|
+
import { LoggerType, SeverityType, LoggerOptionsType, LoggerFunctionType } from "./..";
|
|
5
|
+
/**
|
|
6
|
+
* This class is used across the Hyper Fetch library to provide unified logging system with necessary setup per each builder.
|
|
7
|
+
* We can set up the logging level based on available values. This manager enable to initialize the logging instance per individual module
|
|
8
|
+
* like Builder, Command etc. Which can give you better feedback on the logging itself.
|
|
9
|
+
*/
|
|
10
|
+
export declare class LoggerManager {
|
|
11
|
+
private builder;
|
|
12
|
+
private options?;
|
|
13
|
+
logger: LoggerFunctionType;
|
|
14
|
+
severity: SeverityType;
|
|
15
|
+
emitter: EventEmitter;
|
|
16
|
+
constructor(builder: BuilderInstance, options?: LoggerOptionsType);
|
|
17
|
+
setSeverity: (severity: SeverityType) => void;
|
|
18
|
+
init: (module: string) => LoggerType;
|
|
19
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { ClientResponseType } from "../../client";
|
|
2
|
+
import { CommandInstance } from "../../command";
|
|
3
|
+
import { CommandResponseDetails } from "../command";
|
|
4
|
+
export declare type SeverityType = 0 | 1 | 2 | 3;
|
|
5
|
+
export declare type LoggerType = Record<LoggerLevelType, (message: LoggerMessageType, ...additionalData: LoggerMessageType[]) => void>;
|
|
6
|
+
export declare type LoggerFunctionType = (log: LogType) => void;
|
|
7
|
+
export declare type LoggerOptionsType = {
|
|
8
|
+
logger?: LoggerFunctionType;
|
|
9
|
+
severity?: SeverityType;
|
|
10
|
+
};
|
|
11
|
+
export declare type LogType = {
|
|
12
|
+
module: string;
|
|
13
|
+
level: LoggerLevelType;
|
|
14
|
+
message: LoggerMessageType;
|
|
15
|
+
additionalData?: LoggerMessageType[];
|
|
16
|
+
enabled: boolean;
|
|
17
|
+
severity: SeverityType;
|
|
18
|
+
};
|
|
19
|
+
export declare type LoggerLevelType = "error" | "warning" | "info" | "debug";
|
|
20
|
+
export declare type LoggerMessageType = string | Record<string, unknown> | Array<unknown>;
|
|
21
|
+
export declare type LoggerRequestEventData = {
|
|
22
|
+
requestId: string;
|
|
23
|
+
command: CommandInstance;
|
|
24
|
+
};
|
|
25
|
+
export declare type LoggerResponseEventData = {
|
|
26
|
+
requestId: string;
|
|
27
|
+
command: CommandInstance;
|
|
28
|
+
response: ClientResponseType<unknown, unknown>;
|
|
29
|
+
details: CommandResponseDetails;
|
|
30
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { ClientResponseType } from "../client";
|
|
2
|
+
import { ExtractRouteParams, Command, CommandInstance } from "../command";
|
|
3
|
+
export declare type ExtractClientReturnType<T extends CommandInstance> = ClientResponseType<ExtractResponse<T>, ExtractError<T>>;
|
|
4
|
+
export declare type ExtractResponse<T> = T extends Command<infer D, any, any, any, any, any, any, any, any, any> ? D : never;
|
|
5
|
+
export declare type ExtractRequestData<T> = T extends Command<any, infer D, any, any, any, any, any, any, any, any> ? D : never;
|
|
6
|
+
export declare type ExtractQueryParams<T> = T extends Command<any, any, infer Q, any, any, any, any, any, any, any> ? Q : never;
|
|
7
|
+
export declare type ExtractError<T> = T extends Command<any, any, any, infer G, infer L, any, any, any, any, any> ? G | L : never;
|
|
8
|
+
export declare type ExtractGlobalError<T> = T extends Command<any, any, any, infer E, any, any, any, any, any, any> ? E : never;
|
|
9
|
+
export declare type ExtractLocalError<T> = T extends Command<any, any, any, any, infer E, any, any, any, any, any> ? E : never;
|
|
10
|
+
export declare type ExtractParams<T> = T extends Command<any, any, any, any, any, infer P, any, any, any, any> ? ExtractRouteParams<P> : never;
|
|
11
|
+
export declare type ExtractEndpoint<T> = T extends Command<any, any, any, any, any, infer E, any, any, any, any> ? E : never;
|
|
12
|
+
export declare type ExtractClientOptions<T> = T extends Command<any, any, any, any, any, any, infer O, any, any, any> ? O : never;
|
|
13
|
+
export declare type ExtractHasData<T> = T extends Command<any, any, any, any, any, any, any, infer D, any, any> ? D : never;
|
|
14
|
+
export declare type ExtractHasParams<T> = T extends Command<any, any, any, any, any, any, any, any, infer P, any> ? P : never;
|
|
15
|
+
export declare type ExtractHasQueryParams<T> = T extends Command<any, any, any, any, any, any, any, any, any, infer Q> ? Q : never;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare type NegativeTypes = null | undefined;
|
|
2
|
+
export declare type NullableType<T> = T | NegativeTypes;
|
|
3
|
+
export declare type NullableKeys<T> = {
|
|
4
|
+
[P in keyof T]-?: NullableType<T[P]>;
|
|
5
|
+
};
|
|
6
|
+
export declare type NonNullableKeys<T> = {
|
|
7
|
+
[P in keyof T]-?: NonNullable<T[P]>;
|
|
8
|
+
};
|
|
9
|
+
export declare type RequiredKeys<T> = {
|
|
10
|
+
[P in keyof T]-?: Exclude<T[P], NegativeTypes>;
|
|
11
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare type HttpMethodsType = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./uuid.utils";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const getUniqueRequestId: (key: string) => string;
|
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hyper-fetch/core",
|
|
3
|
+
"version": "1.6.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "Cache, Queue and Persist your requests no matter if you are online or offline!",
|
|
6
|
+
"author": "Maciej Pyrc <m.pyrc@interia.pl>",
|
|
7
|
+
"homepage": "https://hyperfetch.bettertyped.com/",
|
|
8
|
+
"license": "Apache-2.0",
|
|
9
|
+
"main": "dist/index.cjs.js",
|
|
10
|
+
"module": "dist/index.esm.js",
|
|
11
|
+
"types": "dist/index.d.ts",
|
|
12
|
+
"source": "src/index.ts",
|
|
13
|
+
"scripts": {
|
|
14
|
+
"prepublishOnly": "yarn build",
|
|
15
|
+
"clean": "npx rimraf dist",
|
|
16
|
+
"test": "jest --watchAll --maxWorkers=3",
|
|
17
|
+
"test-pipeline": "jest --watchAll=false --maxWorkers=1 --no-cache",
|
|
18
|
+
"start": "yarn build && npx run-when-changed --watch 'src/**/*.ts' --exec 'yarn build'",
|
|
19
|
+
"build": "yarn clean && node ../../scripts/build.js",
|
|
20
|
+
"postbuild": "ttsc --project tsconfig.json --outDir dist",
|
|
21
|
+
"lint": "eslint . --ext .js,.jsx,.tsx,.ts --fix",
|
|
22
|
+
"format": "prettier --write .",
|
|
23
|
+
"precommit": "yarn lint-staged",
|
|
24
|
+
"release": "yarn semantic-release --extends ../../.releaserc.json -t 'hyper-fetch-v${version}'"
|
|
25
|
+
},
|
|
26
|
+
"publishConfig": {
|
|
27
|
+
"access": "public",
|
|
28
|
+
"registry": "https://registry.npmjs.com/"
|
|
29
|
+
},
|
|
30
|
+
"repository": {
|
|
31
|
+
"type": "git",
|
|
32
|
+
"url": "git+https://github.com/BetterTyped/hyper-fetch.git",
|
|
33
|
+
"directory": "packages/core"
|
|
34
|
+
},
|
|
35
|
+
"bugs": {
|
|
36
|
+
"url": "https://github.com/BetterTyped/hyper-fetch/issues"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"events": "^3.3.0"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {},
|
|
42
|
+
"lint-staged": {
|
|
43
|
+
"*.{js,jsx,ts,tsx}": [
|
|
44
|
+
"yarn jest --bail --findRelatedTests --passWithNoTests"
|
|
45
|
+
]
|
|
46
|
+
},
|
|
47
|
+
"keywords": [
|
|
48
|
+
"fetch",
|
|
49
|
+
"xhr",
|
|
50
|
+
"requests",
|
|
51
|
+
"js",
|
|
52
|
+
"fetcher",
|
|
53
|
+
"queue",
|
|
54
|
+
"offline",
|
|
55
|
+
"persist",
|
|
56
|
+
"cache",
|
|
57
|
+
"builder",
|
|
58
|
+
"command"
|
|
59
|
+
]
|
|
60
|
+
}
|
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ClientType,
|
|
3
|
+
fetchClient,
|
|
4
|
+
ClientDefaultOptionsType,
|
|
5
|
+
ClientResponseType,
|
|
6
|
+
ClientQueryParamsType,
|
|
7
|
+
QueryStringifyOptions,
|
|
8
|
+
ClientHeaderMappingCallback,
|
|
9
|
+
ClientPayloadMappingCallback,
|
|
10
|
+
} from "client";
|
|
11
|
+
import {
|
|
12
|
+
stringifyQueryParams,
|
|
13
|
+
getClientHeaders,
|
|
14
|
+
getClientPayload,
|
|
15
|
+
BuilderConfig,
|
|
16
|
+
BuilderInstance,
|
|
17
|
+
BuilderErrorType,
|
|
18
|
+
StringifyCallbackType,
|
|
19
|
+
RequestInterceptorCallback,
|
|
20
|
+
ResponseInterceptorCallback,
|
|
21
|
+
} from "builder";
|
|
22
|
+
import { Cache } from "cache";
|
|
23
|
+
import { Dispatcher } from "dispatcher";
|
|
24
|
+
import { FetchEffectInstance } from "effect";
|
|
25
|
+
import { Command, CommandConfig, CommandInstance } from "command";
|
|
26
|
+
import { AppManager, CommandManager, LoggerManager, SeverityType } from "managers";
|
|
27
|
+
import { interceptRequest, interceptResponse } from "./builder.utils";
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* **Builder** is a class that allows you to configure the connection with the server and then use it to create
|
|
31
|
+
* commands which, when called using the appropriate method, will cause the server to be queried for the endpoint and
|
|
32
|
+
* method specified in the command.
|
|
33
|
+
*/
|
|
34
|
+
export class Builder<GlobalErrorType extends BuilderErrorType = Error, RequestConfigType = ClientDefaultOptionsType> {
|
|
35
|
+
readonly baseUrl: string;
|
|
36
|
+
readonly isNodeJS: boolean;
|
|
37
|
+
debug: boolean;
|
|
38
|
+
|
|
39
|
+
// Private
|
|
40
|
+
__onErrorCallbacks: ResponseInterceptorCallback[] = [];
|
|
41
|
+
__onSuccessCallbacks: ResponseInterceptorCallback[] = [];
|
|
42
|
+
__onResponseCallbacks: ResponseInterceptorCallback[] = [];
|
|
43
|
+
__onAuthCallbacks: RequestInterceptorCallback[] = [];
|
|
44
|
+
__onRequestCallbacks: RequestInterceptorCallback[] = [];
|
|
45
|
+
|
|
46
|
+
// Managers
|
|
47
|
+
commandManager: CommandManager = new CommandManager();
|
|
48
|
+
appManager: AppManager;
|
|
49
|
+
loggerManager: LoggerManager = new LoggerManager(this);
|
|
50
|
+
|
|
51
|
+
// Config
|
|
52
|
+
client: ClientType;
|
|
53
|
+
cache: Cache;
|
|
54
|
+
fetchDispatcher: Dispatcher;
|
|
55
|
+
submitDispatcher: Dispatcher;
|
|
56
|
+
|
|
57
|
+
// Registered requests effect
|
|
58
|
+
effects: FetchEffectInstance[] = [];
|
|
59
|
+
|
|
60
|
+
// Options
|
|
61
|
+
requestConfig?: (command: CommandInstance) => RequestConfigType;
|
|
62
|
+
commandConfig?: (
|
|
63
|
+
commandOptions: CommandConfig<string, RequestConfigType>,
|
|
64
|
+
) => Partial<CommandConfig<string, RequestConfigType>>;
|
|
65
|
+
queryParamsConfig?: QueryStringifyOptions;
|
|
66
|
+
|
|
67
|
+
// Utils
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Method to stringify query params from objects.
|
|
71
|
+
*/
|
|
72
|
+
stringifyQueryParams: StringifyCallbackType = (queryParams) =>
|
|
73
|
+
stringifyQueryParams(queryParams, this.queryParamsConfig);
|
|
74
|
+
/**
|
|
75
|
+
* Method to get default headers and to map them based on the data format exchange, by default it handles FormData / JSON formats.
|
|
76
|
+
*/
|
|
77
|
+
headerMapper: ClientHeaderMappingCallback = getClientHeaders;
|
|
78
|
+
/**
|
|
79
|
+
* Method to get request data and transform them to the required format. It handles FormData and JSON by default.
|
|
80
|
+
*/
|
|
81
|
+
payloadMapper: ClientPayloadMappingCallback = getClientPayload;
|
|
82
|
+
|
|
83
|
+
// Logger
|
|
84
|
+
logger = this.loggerManager.init("Builder");
|
|
85
|
+
|
|
86
|
+
constructor(public options: BuilderConfig) {
|
|
87
|
+
const { baseUrl, isNodeJS, client, appManager, cache, fetchDispatcher, submitDispatcher } = this.options;
|
|
88
|
+
this.baseUrl = baseUrl;
|
|
89
|
+
this.isNodeJS = isNodeJS;
|
|
90
|
+
this.client = client || fetchClient;
|
|
91
|
+
|
|
92
|
+
// IMPORTANT: Do not change initialization order as it's crucial for dependencies injection
|
|
93
|
+
this.appManager = appManager?.(this) || new AppManager(this);
|
|
94
|
+
this.cache = cache?.(this) || new Cache(this);
|
|
95
|
+
this.fetchDispatcher = fetchDispatcher?.(this) || new Dispatcher(this);
|
|
96
|
+
this.submitDispatcher = submitDispatcher?.(this) || new Dispatcher(this);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* This method allows to configure global defaults for the command configuration like method, auth, deduplication etc.
|
|
101
|
+
*/
|
|
102
|
+
setCommandConfig = (
|
|
103
|
+
callback: (command: CommandInstance) => Partial<CommandConfig<string, RequestConfigType>>,
|
|
104
|
+
): Builder<GlobalErrorType, RequestConfigType> => {
|
|
105
|
+
this.commandConfig = callback;
|
|
106
|
+
return this;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* This method enables the logger usage and display the logs in console
|
|
111
|
+
*/
|
|
112
|
+
setDebug = (debug: boolean): Builder<GlobalErrorType, RequestConfigType> => {
|
|
113
|
+
this.debug = debug;
|
|
114
|
+
return this;
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Set the logger severity of the messages displayed to the console
|
|
119
|
+
*/
|
|
120
|
+
setLoggerSeverity = (severity: SeverityType): Builder<GlobalErrorType, RequestConfigType> => {
|
|
121
|
+
this.loggerManager.setSeverity(severity);
|
|
122
|
+
return this;
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Set the new logger instance to the builder
|
|
127
|
+
*/
|
|
128
|
+
setLogger = (callback: (builder: BuilderInstance) => LoggerManager): Builder<GlobalErrorType, RequestConfigType> => {
|
|
129
|
+
this.loggerManager = callback(this);
|
|
130
|
+
return this;
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Set config for the query params stringify method, we can set here, among others, arrayFormat, skipNull, encode, skipEmptyString and more
|
|
135
|
+
*/
|
|
136
|
+
setQueryParamsConfig = (queryParamsConfig: QueryStringifyOptions): Builder<GlobalErrorType, RequestConfigType> => {
|
|
137
|
+
this.queryParamsConfig = queryParamsConfig;
|
|
138
|
+
return this;
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Set the custom query params stringify method to the builder
|
|
143
|
+
* @param stringifyFn Custom callback handling query params stringify
|
|
144
|
+
*/
|
|
145
|
+
setStringifyQueryParams = (stringifyFn: StringifyCallbackType): Builder<GlobalErrorType, RequestConfigType> => {
|
|
146
|
+
this.stringifyQueryParams = stringifyFn;
|
|
147
|
+
return this;
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Set the custom header mapping function
|
|
152
|
+
*/
|
|
153
|
+
setHeaderMapper = (headerMapper: ClientHeaderMappingCallback): Builder<GlobalErrorType, RequestConfigType> => {
|
|
154
|
+
this.headerMapper = headerMapper;
|
|
155
|
+
return this;
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Set the request payload mapping function which get triggered before request get send
|
|
160
|
+
*/
|
|
161
|
+
setPayloadMapper = (payloadMapper: ClientPayloadMappingCallback): Builder<GlobalErrorType, RequestConfigType> => {
|
|
162
|
+
this.payloadMapper = payloadMapper;
|
|
163
|
+
return this;
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Set custom http client to handle graphql, rest, firebase or other
|
|
168
|
+
*/
|
|
169
|
+
setClient = (callback: (builder: BuilderInstance) => ClientType): Builder<GlobalErrorType, RequestConfigType> => {
|
|
170
|
+
this.client = callback(this);
|
|
171
|
+
return this;
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* 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.
|
|
176
|
+
*/
|
|
177
|
+
onAuth = (callback: RequestInterceptorCallback): Builder<GlobalErrorType, RequestConfigType> => {
|
|
178
|
+
this.__onAuthCallbacks.push(callback);
|
|
179
|
+
return this;
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Method for intercepting error responses. It can be used for example to refresh tokens.
|
|
184
|
+
*/
|
|
185
|
+
onError = <ErrorType = null>(
|
|
186
|
+
callback: ResponseInterceptorCallback<any, ErrorType | GlobalErrorType>,
|
|
187
|
+
): Builder<GlobalErrorType, RequestConfigType> => {
|
|
188
|
+
this.__onErrorCallbacks.push(callback);
|
|
189
|
+
return this;
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Method for intercepting success responses.
|
|
194
|
+
*/
|
|
195
|
+
onSuccess = <ErrorType = null>(
|
|
196
|
+
callback: ResponseInterceptorCallback<any, ErrorType | GlobalErrorType>,
|
|
197
|
+
): Builder<GlobalErrorType, RequestConfigType> => {
|
|
198
|
+
this.__onSuccessCallbacks.push(callback);
|
|
199
|
+
return this;
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Method of manipulating commands before sending the request.
|
|
204
|
+
*/
|
|
205
|
+
onRequest = (callback: RequestInterceptorCallback): Builder<GlobalErrorType, RequestConfigType> => {
|
|
206
|
+
this.__onRequestCallbacks.push(callback);
|
|
207
|
+
return this;
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Method for intercepting any responses.
|
|
212
|
+
*/
|
|
213
|
+
onResponse = <ErrorType = null>(
|
|
214
|
+
callback: ResponseInterceptorCallback<any, ErrorType | GlobalErrorType>,
|
|
215
|
+
): Builder<GlobalErrorType, RequestConfigType> => {
|
|
216
|
+
this.__onResponseCallbacks.push(callback);
|
|
217
|
+
return this;
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Add persistent effects which trigger on the request lifecycle
|
|
222
|
+
*/
|
|
223
|
+
addEffect = (effect: FetchEffectInstance | FetchEffectInstance[]) => {
|
|
224
|
+
this.effects = this.effects.concat(effect);
|
|
225
|
+
|
|
226
|
+
return this;
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Remove effects from builder
|
|
231
|
+
*/
|
|
232
|
+
removeEffect = (effect: FetchEffectInstance | string) => {
|
|
233
|
+
const name = typeof effect === "string" ? effect : effect.getEffectKey();
|
|
234
|
+
this.effects = this.effects.filter((currentEffect) => currentEffect.getEffectKey() !== name);
|
|
235
|
+
|
|
236
|
+
return this;
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Create commands based on the builder setup
|
|
241
|
+
*/
|
|
242
|
+
createCommand = <
|
|
243
|
+
ResponseType,
|
|
244
|
+
RequestDataType = undefined,
|
|
245
|
+
LocalErrorType extends BuilderErrorType | undefined = undefined,
|
|
246
|
+
QueryParamsType extends ClientQueryParamsType | string = ClientQueryParamsType | string,
|
|
247
|
+
>() => {
|
|
248
|
+
return <EndpointType extends string>(params: CommandConfig<EndpointType, RequestConfigType>) =>
|
|
249
|
+
new Command<
|
|
250
|
+
ResponseType,
|
|
251
|
+
RequestDataType,
|
|
252
|
+
QueryParamsType,
|
|
253
|
+
GlobalErrorType,
|
|
254
|
+
LocalErrorType,
|
|
255
|
+
EndpointType,
|
|
256
|
+
RequestConfigType
|
|
257
|
+
>(this, params);
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Clears the builder instance and remove all listeners on it's dependencies
|
|
262
|
+
*/
|
|
263
|
+
clear = () => {
|
|
264
|
+
const { appManager, cache, fetchDispatcher, submitDispatcher } = this.options;
|
|
265
|
+
|
|
266
|
+
this.commandManager.abortControllers.clear();
|
|
267
|
+
this.fetchDispatcher.clear();
|
|
268
|
+
this.submitDispatcher.clear();
|
|
269
|
+
this.cache.clear();
|
|
270
|
+
|
|
271
|
+
this.commandManager.emitter.removeAllListeners();
|
|
272
|
+
this.fetchDispatcher.emitter.removeAllListeners();
|
|
273
|
+
this.submitDispatcher.emitter.removeAllListeners();
|
|
274
|
+
this.cache.emitter.removeAllListeners();
|
|
275
|
+
|
|
276
|
+
this.appManager = appManager?.(this) || new AppManager(this);
|
|
277
|
+
this.cache = cache?.(this) || new Cache(this);
|
|
278
|
+
this.fetchDispatcher = fetchDispatcher?.(this) || new Dispatcher(this);
|
|
279
|
+
this.submitDispatcher = submitDispatcher?.(this) || new Dispatcher(this);
|
|
280
|
+
};
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Helper used by http client to apply the modifications on response error
|
|
284
|
+
*/
|
|
285
|
+
__modifyAuth = async (command: CommandInstance) => interceptRequest(this.__onAuthCallbacks, command);
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Private helper to run async pre-request processing
|
|
289
|
+
*/
|
|
290
|
+
__modifyRequest = async (command: CommandInstance) => interceptRequest(this.__onRequestCallbacks, command);
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* Private helper to run async on-error response processing
|
|
294
|
+
*/
|
|
295
|
+
__modifyErrorResponse = async (response: ClientResponseType<any, GlobalErrorType>, command: CommandInstance) =>
|
|
296
|
+
interceptResponse<GlobalErrorType>(this.__onErrorCallbacks, response, command);
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Private helper to run async on-success response processing
|
|
300
|
+
*/
|
|
301
|
+
__modifySuccessResponse = async (response: ClientResponseType<any, GlobalErrorType>, command: CommandInstance) =>
|
|
302
|
+
interceptResponse<GlobalErrorType>(this.__onSuccessCallbacks, response, command);
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Private helper to run async response processing
|
|
306
|
+
*/
|
|
307
|
+
__modifyResponse = async (response: ClientResponseType<any, GlobalErrorType>, command: CommandInstance) =>
|
|
308
|
+
interceptResponse<GlobalErrorType>(this.__onResponseCallbacks, response, command);
|
|
309
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { Cache } from "cache";
|
|
2
|
+
import { CommandInstance } from "command";
|
|
3
|
+
import { Dispatcher } from "dispatcher";
|
|
4
|
+
import { ClientResponseType, ClientType, ClientQueryParamsType } from "client";
|
|
5
|
+
import { Builder } from "builder";
|
|
6
|
+
import { AppManager } from "managers";
|
|
7
|
+
import { NegativeTypes } from "types";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Configuration setup for the builder
|
|
11
|
+
*/
|
|
12
|
+
export type BuilderConfig = {
|
|
13
|
+
/**
|
|
14
|
+
* Url to your server
|
|
15
|
+
*/
|
|
16
|
+
baseUrl: string;
|
|
17
|
+
/**
|
|
18
|
+
* Disable the web event listeners and actions on window object
|
|
19
|
+
*/
|
|
20
|
+
isNodeJS?: boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Custom client initialization prop
|
|
23
|
+
*/
|
|
24
|
+
client?: ClientType;
|
|
25
|
+
/**
|
|
26
|
+
* Custom cache initialization prop
|
|
27
|
+
*/
|
|
28
|
+
cache?: <B extends BuilderInstance, C extends Cache>(builder: B) => C;
|
|
29
|
+
/**
|
|
30
|
+
* Custom app manager initialization prop
|
|
31
|
+
*/
|
|
32
|
+
appManager?: <B extends BuilderInstance, A extends AppManager>(builder: B) => A;
|
|
33
|
+
/**
|
|
34
|
+
* Custom fetch dispatcher initialization prop
|
|
35
|
+
*/
|
|
36
|
+
fetchDispatcher?: <B extends BuilderInstance, D extends Dispatcher>(builder: B) => D;
|
|
37
|
+
/**
|
|
38
|
+
* Custom submit dispatcher initialization prop
|
|
39
|
+
*/
|
|
40
|
+
submitDispatcher?: <B extends BuilderInstance, D extends Dispatcher>(builder: B) => D;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export type BuilderInstance = Builder<any, any>;
|
|
44
|
+
|
|
45
|
+
export type BuilderErrorType = Record<string, any> | string;
|
|
46
|
+
|
|
47
|
+
// Interceptors
|
|
48
|
+
|
|
49
|
+
export type RequestInterceptorCallback = (command: CommandInstance) => Promise<CommandInstance> | CommandInstance;
|
|
50
|
+
export type ResponseInterceptorCallback<Response = any, Error = any> = (
|
|
51
|
+
response: ClientResponseType<Response, Error>,
|
|
52
|
+
command: CommandInstance,
|
|
53
|
+
) => Promise<ClientResponseType<any, any>> | ClientResponseType<any, any>;
|
|
54
|
+
|
|
55
|
+
// Stringify
|
|
56
|
+
|
|
57
|
+
export type StringifyCallbackType = (queryParams: ClientQueryParamsType | string | NegativeTypes) => string;
|