@hyper-fetch/core 1.7.1 → 2.1.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 +15 -13
- package/dist/builder/builder.d.ts +17 -18
- package/dist/builder/builder.types.d.ts +14 -18
- package/dist/builder/builder.utils.d.ts +3 -3
- package/dist/cache/cache.d.ts +5 -5
- package/dist/cache/cache.events.d.ts +1 -1
- package/dist/cache/cache.types.d.ts +9 -9
- package/dist/cache/cache.utils.d.ts +2 -2
- package/dist/client/fetch.client.bindings.d.ts +5 -5
- package/dist/client/fetch.client.browser.d.ts +2 -0
- package/dist/client/fetch.client.d.ts +1 -1
- package/dist/client/fetch.client.server.d.ts +2 -0
- package/dist/client/fetch.client.types.d.ts +15 -15
- package/dist/client/fetch.client.utils.d.ts +5 -2
- package/dist/client/index.d.ts +1 -0
- package/dist/command/command.d.ts +5 -5
- package/dist/command/command.types.d.ts +25 -25
- package/dist/command/command.utils.d.ts +4 -4
- package/dist/dispatcher/dispatcher.d.ts +3 -3
- package/dist/dispatcher/dispatcher.events.d.ts +2 -2
- package/dist/dispatcher/dispatcher.types.d.ts +7 -7
- package/dist/dispatcher/dispatcher.utils.d.ts +3 -3
- package/dist/effect/fetch.effect.d.ts +4 -4
- package/dist/effect/fetch.effect.types.d.ts +7 -7
- package/dist/index.cjs.js +1 -1
- package/dist/index.cjs.js.map +4 -4
- package/dist/index.d.ts +1 -0
- package/dist/index.esm.js +1 -1
- package/dist/index.esm.js.map +4 -4
- package/dist/managers/app/app.manager.constants.d.ts +2 -2
- package/dist/managers/app/app.manager.d.ts +3 -4
- package/dist/managers/app/app.manager.types.d.ts +3 -4
- package/dist/managers/command/command.manager.d.ts +21 -21
- package/dist/managers/command/command.manager.events.d.ts +3 -3
- package/dist/managers/command/command.manager.types.d.ts +5 -5
- package/dist/managers/logger/logger.manager.d.ts +3 -3
- package/dist/managers/logger/logger.manager.types.d.ts +12 -12
- package/dist/managers/logger/logger.manager.utils.d.ts +1 -1
- package/dist/types/fetch.types.d.ts +16 -16
- package/dist/types/helpers.types.d.ts +5 -5
- package/dist/types/http.types.d.ts +1 -1
- package/package.json +5 -5
- package/src/builder/builder.ts +48 -50
- package/src/builder/builder.types.ts +1 -5
- package/src/cache/cache.ts +24 -17
- package/src/cache/cache.types.ts +1 -1
- package/src/client/fetch.client.bindings.ts +5 -5
- package/src/client/fetch.client.browser.ts +89 -0
- package/src/client/fetch.client.server.ts +105 -0
- package/src/client/fetch.client.ts +5 -94
- package/src/client/fetch.client.utils.ts +31 -0
- package/src/client/index.ts +1 -0
- package/src/command/command.ts +6 -4
- package/src/command/command.types.ts +2 -2
- package/src/index.ts +1 -0
- package/src/managers/app/app.manager.ts +7 -5
- package/src/managers/app/app.manager.types.ts +2 -4
- package/src/managers/logger/logger.manager.ts +1 -1
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import EventEmitter from "events";
|
|
3
|
-
import {
|
|
4
|
-
import { AppManagerOptionsType } from "./..";
|
|
3
|
+
import { AppManagerOptionsType } from "managers";
|
|
5
4
|
/**
|
|
6
5
|
* App manager handles main application states - focus and online. Those two values can answer questions:
|
|
7
6
|
* - Is the tab or current view instance focused and visible for user?
|
|
@@ -12,7 +11,6 @@ import { AppManagerOptionsType } from "./..";
|
|
|
12
11
|
* Make sure to apply valid focus/online handlers for different environments like for example for native mobile applications.
|
|
13
12
|
*/
|
|
14
13
|
export declare class AppManager {
|
|
15
|
-
builder: BuilderInstance;
|
|
16
14
|
options?: AppManagerOptionsType;
|
|
17
15
|
emitter: EventEmitter;
|
|
18
16
|
events: {
|
|
@@ -25,9 +23,10 @@ export declare class AppManager {
|
|
|
25
23
|
onOnline: (callback: () => void) => VoidFunction;
|
|
26
24
|
onOffline: (callback: () => void) => VoidFunction;
|
|
27
25
|
};
|
|
26
|
+
isNodeJs: boolean;
|
|
28
27
|
isOnline: boolean;
|
|
29
28
|
isFocused: boolean;
|
|
30
|
-
constructor(
|
|
29
|
+
constructor(options?: AppManagerOptionsType);
|
|
31
30
|
private setInitialFocus;
|
|
32
31
|
private setInitialOnline;
|
|
33
32
|
setFocused: (isFocused: boolean) => void;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
export declare type AppManagerOptionsType = {
|
|
1
|
+
export type AppManagerOptionsType = {
|
|
3
2
|
initiallyFocused?: boolean | (() => boolean | Promise<boolean>);
|
|
4
3
|
initiallyOnline?: boolean | (() => boolean | Promise<boolean>);
|
|
5
|
-
focusEvent?: (setFocused: (isFocused: boolean) => void
|
|
6
|
-
onlineEvent?: (setOnline: (isOnline: boolean) => void
|
|
4
|
+
focusEvent?: (setFocused: (isFocused: boolean) => void) => void;
|
|
5
|
+
onlineEvent?: (setOnline: (isOnline: boolean) => void) => void;
|
|
7
6
|
};
|
|
@@ -7,30 +7,30 @@ import EventEmitter from "events";
|
|
|
7
7
|
export declare class CommandManager {
|
|
8
8
|
emitter: EventEmitter;
|
|
9
9
|
events: {
|
|
10
|
-
emitLoading: (queueKey: string, requestId: string, values: import("
|
|
11
|
-
emitRequestStart: (queueKey: string, requestId: string, details: import("
|
|
12
|
-
emitResponseStart: (queueKey: string, requestId: string, details: import("
|
|
13
|
-
emitUploadProgress: (queueKey: string, requestId: string, values: import("../..").FetchProgressType, details: import("
|
|
14
|
-
emitDownloadProgress: (queueKey: string, requestId: string, values: import("../..").FetchProgressType, details: import("
|
|
15
|
-
emitResponse: (cacheKey: string, requestId: string, response: import("../..").ClientResponseType<unknown, unknown>, details: import("
|
|
10
|
+
emitLoading: (queueKey: string, requestId: string, values: import("managers").CommandLoadingEventType) => void;
|
|
11
|
+
emitRequestStart: (queueKey: string, requestId: string, details: import("managers").CommandEventDetails<import("../..").CommandInstance>) => void;
|
|
12
|
+
emitResponseStart: (queueKey: string, requestId: string, details: import("managers").CommandEventDetails<import("../..").CommandInstance>) => void;
|
|
13
|
+
emitUploadProgress: (queueKey: string, requestId: string, values: import("../..").FetchProgressType, details: import("managers").CommandEventDetails<import("../..").CommandInstance>) => void;
|
|
14
|
+
emitDownloadProgress: (queueKey: string, requestId: string, values: import("../..").FetchProgressType, details: import("managers").CommandEventDetails<import("../..").CommandInstance>) => void;
|
|
15
|
+
emitResponse: (cacheKey: string, requestId: string, response: import("../..").ClientResponseType<unknown, unknown>, details: import("managers").CommandResponseDetails) => void;
|
|
16
16
|
emitAbort: (abortKey: string, requestId: string, command: import("../..").CommandInstance) => void;
|
|
17
|
-
emitRemove: <T extends import("../..").CommandInstance>(queueKey: string, requestId: string, details: import("
|
|
18
|
-
onLoading: (queueKey: string, callback: (values: import("
|
|
19
|
-
onLoadingById: (requestId: string, callback: (values: import("
|
|
20
|
-
onRequestStart: <T_1 extends import("../..").CommandInstance>(queueKey: string, callback: (details: import("
|
|
21
|
-
onRequestStartById: <T_2 extends import("../..").CommandInstance>(requestId: string, callback: (details: import("
|
|
22
|
-
onResponseStart: <T_3 extends import("../..").CommandInstance>(queueKey: string, callback: (details: import("
|
|
23
|
-
onResponseStartById: <T_4 extends import("../..").CommandInstance>(requestId: string, callback: (details: import("
|
|
24
|
-
onUploadProgress: <T_5 extends import("../..").CommandInstance = import("../..").CommandInstance>(queueKey: string, callback: (values: import("../..").FetchProgressType, details: import("
|
|
25
|
-
onUploadProgressById: <T_6 extends import("../..").CommandInstance = import("../..").CommandInstance>(requestId: string, callback: (values: import("../..").FetchProgressType, details: import("
|
|
26
|
-
onDownloadProgress: <T_7 extends import("../..").CommandInstance = import("../..").CommandInstance>(queueKey: string, callback: (values: import("../..").FetchProgressType, details: import("
|
|
27
|
-
onDownloadProgressById: <T_8 extends import("../..").CommandInstance = import("../..").CommandInstance>(requestId: string, callback: (values: import("../..").FetchProgressType, details: import("
|
|
28
|
-
onResponse: <ResponseType_1, ErrorType>(cacheKey: string, callback: (response: import("../..").ClientResponseType<ResponseType_1, ErrorType>, details: import("
|
|
29
|
-
onResponseById: <ResponseType_2, ErrorType_1>(requestId: string, callback: (response: import("../..").ClientResponseType<ResponseType_2, ErrorType_1>, details: import("
|
|
17
|
+
emitRemove: <T extends import("../..").CommandInstance>(queueKey: string, requestId: string, details: import("managers").CommandEventDetails<T>) => void;
|
|
18
|
+
onLoading: (queueKey: string, callback: (values: import("managers").CommandLoadingEventType) => void) => VoidFunction;
|
|
19
|
+
onLoadingById: (requestId: string, callback: (values: import("managers").CommandLoadingEventType) => void) => VoidFunction;
|
|
20
|
+
onRequestStart: <T_1 extends import("../..").CommandInstance>(queueKey: string, callback: (details: import("managers").CommandEventDetails<T_1>) => void) => VoidFunction;
|
|
21
|
+
onRequestStartById: <T_2 extends import("../..").CommandInstance>(requestId: string, callback: (details: import("managers").CommandEventDetails<T_2>) => void) => VoidFunction;
|
|
22
|
+
onResponseStart: <T_3 extends import("../..").CommandInstance>(queueKey: string, callback: (details: import("managers").CommandEventDetails<T_3>) => void) => VoidFunction;
|
|
23
|
+
onResponseStartById: <T_4 extends import("../..").CommandInstance>(requestId: string, callback: (details: import("managers").CommandEventDetails<T_4>) => void) => VoidFunction;
|
|
24
|
+
onUploadProgress: <T_5 extends import("../..").CommandInstance = import("../..").CommandInstance>(queueKey: string, callback: (values: import("../..").FetchProgressType, details: import("managers").CommandEventDetails<T_5>) => void) => VoidFunction;
|
|
25
|
+
onUploadProgressById: <T_6 extends import("../..").CommandInstance = import("../..").CommandInstance>(requestId: string, callback: (values: import("../..").FetchProgressType, details: import("managers").CommandEventDetails<T_6>) => void) => VoidFunction;
|
|
26
|
+
onDownloadProgress: <T_7 extends import("../..").CommandInstance = import("../..").CommandInstance>(queueKey: string, callback: (values: import("../..").FetchProgressType, details: import("managers").CommandEventDetails<T_7>) => void) => VoidFunction;
|
|
27
|
+
onDownloadProgressById: <T_8 extends import("../..").CommandInstance = import("../..").CommandInstance>(requestId: string, callback: (values: import("../..").FetchProgressType, details: import("managers").CommandEventDetails<T_8>) => void) => VoidFunction;
|
|
28
|
+
onResponse: <ResponseType_1, ErrorType>(cacheKey: string, callback: (response: import("../..").ClientResponseType<ResponseType_1, ErrorType>, details: import("managers").CommandResponseDetails) => void) => VoidFunction;
|
|
29
|
+
onResponseById: <ResponseType_2, ErrorType_1>(requestId: string, callback: (response: import("../..").ClientResponseType<ResponseType_2, ErrorType_1>, details: import("managers").CommandResponseDetails) => void) => VoidFunction;
|
|
30
30
|
onAbort: (abortKey: string, callback: (command: import("../..").CommandInstance) => void) => VoidFunction;
|
|
31
31
|
onAbortById: (requestId: string, callback: (command: import("../..").CommandInstance) => void) => VoidFunction;
|
|
32
|
-
onRemove: <T_9 extends import("../..").CommandInstance = import("../..").CommandInstance>(queueKey: string, callback: (details: import("
|
|
33
|
-
onRemoveById: <T_10 extends import("../..").CommandInstance = import("../..").CommandInstance>(requestId: string, callback: (details: import("
|
|
32
|
+
onRemove: <T_9 extends import("../..").CommandInstance = import("../..").CommandInstance>(queueKey: string, callback: (details: import("managers").CommandEventDetails<T_9>) => void) => VoidFunction;
|
|
33
|
+
onRemoveById: <T_10 extends import("../..").CommandInstance = import("../..").CommandInstance>(requestId: string, callback: (details: import("managers").CommandEventDetails<T_10>) => void) => VoidFunction;
|
|
34
34
|
};
|
|
35
35
|
abortControllers: Map<string, Map<string, AbortController>>;
|
|
36
36
|
addAbortController: (abortKey: string, requestId: string) => void;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import EventEmitter from "events";
|
|
2
|
-
import { CommandEventDetails, CommandResponseDetails, CommandLoadingEventType } from "
|
|
3
|
-
import { FetchProgressType, ClientResponseType } from "
|
|
4
|
-
import { CommandInstance } from "
|
|
2
|
+
import { CommandEventDetails, CommandResponseDetails, CommandLoadingEventType } from "managers";
|
|
3
|
+
import { FetchProgressType, ClientResponseType } from "client";
|
|
4
|
+
import { CommandInstance } from "command";
|
|
5
5
|
export declare const getCommandManagerEvents: (emitter: EventEmitter) => {
|
|
6
6
|
/**
|
|
7
7
|
* Emiter
|
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
import { CommandInstance } from "
|
|
2
|
-
export
|
|
1
|
+
import { CommandInstance } from "command";
|
|
2
|
+
export type CommandLoadingEventType = {
|
|
3
3
|
queueKey: string;
|
|
4
4
|
requestId: string;
|
|
5
5
|
loading: boolean;
|
|
6
6
|
isRetry: boolean;
|
|
7
7
|
isOffline: boolean;
|
|
8
8
|
};
|
|
9
|
-
export
|
|
9
|
+
export type CommandEventDetails<T extends CommandInstance> = {
|
|
10
10
|
requestId: string;
|
|
11
11
|
command: T;
|
|
12
12
|
};
|
|
13
|
-
export
|
|
13
|
+
export type CommandResponseDetails = {
|
|
14
14
|
retries: number;
|
|
15
15
|
timestamp: number;
|
|
16
16
|
isFailed: boolean;
|
|
17
17
|
isCanceled: boolean;
|
|
18
18
|
isOffline: boolean;
|
|
19
19
|
};
|
|
20
|
-
export
|
|
20
|
+
export type CommandRemoveDetails = {
|
|
21
21
|
queueKey: string;
|
|
22
22
|
requestId: string;
|
|
23
23
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import EventEmitter from "events";
|
|
3
|
-
import { BuilderInstance } from "
|
|
4
|
-
import { LoggerType, SeverityType, LoggerOptionsType, LoggerFunctionType } from "
|
|
3
|
+
import { BuilderInstance } from "builder";
|
|
4
|
+
import { LoggerType, SeverityType, LoggerOptionsType, LoggerFunctionType } from "managers";
|
|
5
5
|
/**
|
|
6
6
|
* This class is used across the Hyper Fetch library to provide unified logging system with necessary setup per each builder.
|
|
7
7
|
* We can set up the logging level based on available values. This manager enable to initialize the logging instance per individual module
|
|
@@ -13,7 +13,7 @@ export declare class LoggerManager {
|
|
|
13
13
|
logger: LoggerFunctionType;
|
|
14
14
|
severity: SeverityType;
|
|
15
15
|
emitter: EventEmitter;
|
|
16
|
-
constructor(builder: BuilderInstance, options?: LoggerOptionsType);
|
|
16
|
+
constructor(builder: Pick<BuilderInstance, "debug">, options?: LoggerOptionsType);
|
|
17
17
|
setSeverity: (severity: SeverityType) => void;
|
|
18
18
|
init: (module: string) => LoggerType;
|
|
19
19
|
}
|
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import { ClientResponseType } from "
|
|
2
|
-
import { CommandInstance } from "
|
|
3
|
-
import { CommandResponseDetails } from "
|
|
4
|
-
export
|
|
5
|
-
export
|
|
6
|
-
export
|
|
7
|
-
export
|
|
1
|
+
import { ClientResponseType } from "client";
|
|
2
|
+
import { CommandInstance } from "command";
|
|
3
|
+
import { CommandResponseDetails } from "managers/command";
|
|
4
|
+
export type SeverityType = 0 | 1 | 2 | 3;
|
|
5
|
+
export type LoggerType = Record<LoggerLevelType, (message: LoggerMessageType, ...additionalData: LoggerMessageType[]) => void>;
|
|
6
|
+
export type LoggerFunctionType = (log: LogType) => void;
|
|
7
|
+
export type LoggerOptionsType = {
|
|
8
8
|
logger?: LoggerFunctionType;
|
|
9
9
|
severity?: SeverityType;
|
|
10
10
|
};
|
|
11
|
-
export
|
|
11
|
+
export type LogType = {
|
|
12
12
|
module: string;
|
|
13
13
|
level: LoggerLevelType;
|
|
14
14
|
message: LoggerMessageType;
|
|
@@ -16,13 +16,13 @@ export declare type LogType = {
|
|
|
16
16
|
enabled: boolean;
|
|
17
17
|
severity: SeverityType;
|
|
18
18
|
};
|
|
19
|
-
export
|
|
20
|
-
export
|
|
21
|
-
export
|
|
19
|
+
export type LoggerLevelType = "error" | "warning" | "info" | "debug";
|
|
20
|
+
export type LoggerMessageType = string | Record<string, unknown> | Array<unknown>;
|
|
21
|
+
export type LoggerRequestEventData = {
|
|
22
22
|
requestId: string;
|
|
23
23
|
command: CommandInstance;
|
|
24
24
|
};
|
|
25
|
-
export
|
|
25
|
+
export type LoggerResponseEventData = {
|
|
26
26
|
requestId: string;
|
|
27
27
|
command: CommandInstance;
|
|
28
28
|
response: ClientResponseType<unknown, unknown>;
|
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import { ClientResponseType } from "
|
|
2
|
-
import { ExtractRouteParams, Command, CommandInstance } from "
|
|
3
|
-
export
|
|
4
|
-
export
|
|
5
|
-
export
|
|
6
|
-
export
|
|
7
|
-
export
|
|
8
|
-
export
|
|
9
|
-
export
|
|
10
|
-
export
|
|
11
|
-
export
|
|
12
|
-
export
|
|
13
|
-
export
|
|
14
|
-
export
|
|
15
|
-
export
|
|
16
|
-
export
|
|
1
|
+
import { ClientResponseType } from "client";
|
|
2
|
+
import { ExtractRouteParams, Command, CommandInstance } from "command";
|
|
3
|
+
export type ExtractClientReturnType<T extends CommandInstance> = ClientResponseType<ExtractResponse<T>, ExtractError<T>>;
|
|
4
|
+
export type ExtractResponse<T> = T extends Command<infer D, any, any, any, any, any, any, any, any, any, any> ? D : never;
|
|
5
|
+
export type ExtractRequestData<T> = T extends Command<any, infer D, any, any, any, any, any, any, any, any, any> ? D : never;
|
|
6
|
+
export type ExtractQueryParams<T> = T extends Command<any, any, infer Q, any, any, any, any, any, any, any, any> ? Q : never;
|
|
7
|
+
export type ExtractError<T> = T extends Command<any, any, any, infer G, infer L, any, any, any, any, any, any> ? G | L : never;
|
|
8
|
+
export type ExtractGlobalError<T> = T extends Command<any, any, any, infer E, any, any, any, any, any, any, any> ? E : never;
|
|
9
|
+
export type ExtractLocalError<T> = T extends Command<any, any, any, any, infer E, any, any, any, any, any, any> ? E : never;
|
|
10
|
+
export type ExtractParams<T> = T extends Command<any, any, any, any, any, infer P, any, any, any, any, any> ? ExtractRouteParams<P> : never;
|
|
11
|
+
export type ExtractEndpoint<T> = T extends Command<any, any, any, any, any, infer E, any, any, any, any, any> ? E : never;
|
|
12
|
+
export type ExtractClientOptions<T> = T extends Command<any, any, any, any, any, any, infer O, any, any, any, any> ? O : never;
|
|
13
|
+
export type ExtractHasData<T> = T extends Command<any, any, any, any, any, any, any, infer D, any, any, any> ? D : never;
|
|
14
|
+
export type ExtractHasParams<T> = T extends Command<any, any, any, any, any, any, any, any, infer P, any, any> ? P : never;
|
|
15
|
+
export type ExtractHasQueryParams<T> = T extends Command<any, any, any, any, any, any, any, any, any, infer Q, any> ? Q : never;
|
|
16
|
+
export type ExtractHasMappedData<T> = T extends Command<any, any, any, any, any, any, any, any, any, any, infer M> ? M : never;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
3
|
-
export
|
|
1
|
+
export type NegativeTypes = null | undefined;
|
|
2
|
+
export type NullableType<T> = T | NegativeTypes;
|
|
3
|
+
export type NullableKeys<T> = {
|
|
4
4
|
[P in keyof T]-?: NullableType<T[P]>;
|
|
5
5
|
};
|
|
6
|
-
export
|
|
6
|
+
export type NonNullableKeys<T> = {
|
|
7
7
|
[P in keyof T]-?: NonNullable<T[P]>;
|
|
8
8
|
};
|
|
9
|
-
export
|
|
9
|
+
export type RequiredKeys<T> = {
|
|
10
10
|
[P in keyof T]-?: Exclude<T[P], NegativeTypes>;
|
|
11
11
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export
|
|
1
|
+
export type HttpMethodsType = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hyper-fetch/core",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Cache, Queue and Persist your requests no matter if you are online or offline!",
|
|
6
6
|
"author": "Maciej Pyrc <m.pyrc@interia.pl>",
|
|
@@ -20,6 +20,7 @@
|
|
|
20
20
|
"postbuild": "ttsc --project tsconfig.json --outDir dist",
|
|
21
21
|
"lint": "eslint . --ext .js,.jsx,.tsx,.ts --fix",
|
|
22
22
|
"format": "prettier --write .",
|
|
23
|
+
"typecheck": "tsc --noEmit --emitDeclarationOnly false",
|
|
23
24
|
"precommit": "yarn lint-staged",
|
|
24
25
|
"release": "yarn semantic-release --extends ./release.config.js -t 'hyper-fetch-v${version}'"
|
|
25
26
|
},
|
|
@@ -35,13 +36,12 @@
|
|
|
35
36
|
"bugs": {
|
|
36
37
|
"url": "https://github.com/BetterTyped/hyper-fetch/issues"
|
|
37
38
|
},
|
|
38
|
-
"
|
|
39
|
-
|
|
40
|
-
},
|
|
39
|
+
"peerDependencies": {},
|
|
40
|
+
"dependencies": {},
|
|
41
41
|
"devDependencies": {},
|
|
42
42
|
"lint-staged": {
|
|
43
43
|
"*.{js,jsx,ts,tsx}": [
|
|
44
|
-
"yarn jest --
|
|
44
|
+
"yarn jest --passWithNoTests"
|
|
45
45
|
]
|
|
46
46
|
},
|
|
47
47
|
"keywords": [
|
package/src/builder/builder.ts
CHANGED
|
@@ -33,9 +33,8 @@ import { interceptRequest, interceptResponse } from "./builder.utils";
|
|
|
33
33
|
* @position 1
|
|
34
34
|
*/
|
|
35
35
|
export class Builder<GlobalErrorType extends BuilderErrorType = Error, RequestConfigType = ClientDefaultOptionsType> {
|
|
36
|
-
readonly
|
|
37
|
-
|
|
38
|
-
debug: boolean;
|
|
36
|
+
readonly url: string;
|
|
37
|
+
public debug: boolean;
|
|
39
38
|
|
|
40
39
|
// Private
|
|
41
40
|
__onErrorCallbacks: ResponseInterceptorCallback[] = [];
|
|
@@ -85,13 +84,12 @@ export class Builder<GlobalErrorType extends BuilderErrorType = Error, RequestCo
|
|
|
85
84
|
logger = this.loggerManager.init("Builder");
|
|
86
85
|
|
|
87
86
|
constructor(public options: BuilderConfig) {
|
|
88
|
-
const {
|
|
89
|
-
this.
|
|
90
|
-
this.isNodeJS = isNodeJS;
|
|
87
|
+
const { url, client, appManager, cache, fetchDispatcher, submitDispatcher } = this.options;
|
|
88
|
+
this.url = url;
|
|
91
89
|
this.client = client || fetchClient;
|
|
92
90
|
|
|
93
91
|
// IMPORTANT: Do not change initialization order as it's crucial for dependencies injection
|
|
94
|
-
this.appManager = appManager?.(this) || new AppManager(
|
|
92
|
+
this.appManager = appManager?.(this) || new AppManager();
|
|
95
93
|
this.cache = cache?.(this) || new Cache(this);
|
|
96
94
|
this.fetchDispatcher = fetchDispatcher?.(this) || new Dispatcher(this);
|
|
97
95
|
this.submitDispatcher = submitDispatcher?.(this) || new Dispatcher(this);
|
|
@@ -237,49 +235,6 @@ export class Builder<GlobalErrorType extends BuilderErrorType = Error, RequestCo
|
|
|
237
235
|
return this;
|
|
238
236
|
};
|
|
239
237
|
|
|
240
|
-
/**
|
|
241
|
-
* Create commands based on the builder setup
|
|
242
|
-
*/
|
|
243
|
-
createCommand = <
|
|
244
|
-
ResponseType,
|
|
245
|
-
RequestDataType = undefined,
|
|
246
|
-
LocalErrorType extends BuilderErrorType | undefined = undefined,
|
|
247
|
-
QueryParamsType extends ClientQueryParamsType | string = ClientQueryParamsType | string,
|
|
248
|
-
>() => {
|
|
249
|
-
return <EndpointType extends string>(params: CommandConfig<EndpointType, RequestConfigType>) =>
|
|
250
|
-
new Command<
|
|
251
|
-
ResponseType,
|
|
252
|
-
RequestDataType,
|
|
253
|
-
QueryParamsType,
|
|
254
|
-
GlobalErrorType,
|
|
255
|
-
LocalErrorType,
|
|
256
|
-
EndpointType,
|
|
257
|
-
RequestConfigType
|
|
258
|
-
>(this, params);
|
|
259
|
-
};
|
|
260
|
-
|
|
261
|
-
/**
|
|
262
|
-
* Clears the builder instance and remove all listeners on it's dependencies
|
|
263
|
-
*/
|
|
264
|
-
clear = () => {
|
|
265
|
-
const { appManager, cache, fetchDispatcher, submitDispatcher } = this.options;
|
|
266
|
-
|
|
267
|
-
this.commandManager.abortControllers.clear();
|
|
268
|
-
this.fetchDispatcher.clear();
|
|
269
|
-
this.submitDispatcher.clear();
|
|
270
|
-
this.cache.clear();
|
|
271
|
-
|
|
272
|
-
this.commandManager.emitter.removeAllListeners();
|
|
273
|
-
this.fetchDispatcher.emitter.removeAllListeners();
|
|
274
|
-
this.submitDispatcher.emitter.removeAllListeners();
|
|
275
|
-
this.cache.emitter.removeAllListeners();
|
|
276
|
-
|
|
277
|
-
this.appManager = appManager?.(this) || new AppManager(this);
|
|
278
|
-
this.cache = cache?.(this) || new Cache(this);
|
|
279
|
-
this.fetchDispatcher = fetchDispatcher?.(this) || new Dispatcher(this);
|
|
280
|
-
this.submitDispatcher = submitDispatcher?.(this) || new Dispatcher(this);
|
|
281
|
-
};
|
|
282
|
-
|
|
283
238
|
/**
|
|
284
239
|
* Helper used by http client to apply the modifications on response error
|
|
285
240
|
*/
|
|
@@ -307,4 +262,47 @@ export class Builder<GlobalErrorType extends BuilderErrorType = Error, RequestCo
|
|
|
307
262
|
*/
|
|
308
263
|
__modifyResponse = async (response: ClientResponseType<any, GlobalErrorType>, command: CommandInstance) =>
|
|
309
264
|
interceptResponse<GlobalErrorType>(this.__onResponseCallbacks, response, command);
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Clears the builder instance and remove all listeners on it's dependencies
|
|
268
|
+
*/
|
|
269
|
+
clear = () => {
|
|
270
|
+
const { appManager, cache, fetchDispatcher, submitDispatcher } = this.options;
|
|
271
|
+
|
|
272
|
+
this.commandManager.abortControllers.clear();
|
|
273
|
+
this.fetchDispatcher.clear();
|
|
274
|
+
this.submitDispatcher.clear();
|
|
275
|
+
this.cache.clear();
|
|
276
|
+
|
|
277
|
+
this.commandManager.emitter.removeAllListeners();
|
|
278
|
+
this.fetchDispatcher.emitter.removeAllListeners();
|
|
279
|
+
this.submitDispatcher.emitter.removeAllListeners();
|
|
280
|
+
this.cache.emitter.removeAllListeners();
|
|
281
|
+
|
|
282
|
+
this.appManager = appManager?.(this) || new AppManager();
|
|
283
|
+
this.cache = cache?.(this) || new Cache(this);
|
|
284
|
+
this.fetchDispatcher = fetchDispatcher?.(this) || new Dispatcher(this);
|
|
285
|
+
this.submitDispatcher = submitDispatcher?.(this) || new Dispatcher(this);
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Create commands based on the builder setup
|
|
290
|
+
*/
|
|
291
|
+
createCommand = <
|
|
292
|
+
ResponseType,
|
|
293
|
+
RequestDataType = undefined,
|
|
294
|
+
LocalErrorType extends BuilderErrorType | undefined = undefined,
|
|
295
|
+
QueryParamsType extends ClientQueryParamsType | string = ClientQueryParamsType | string,
|
|
296
|
+
>() => {
|
|
297
|
+
return <EndpointType extends string>(params: CommandConfig<EndpointType, RequestConfigType>) =>
|
|
298
|
+
new Command<
|
|
299
|
+
ResponseType,
|
|
300
|
+
RequestDataType,
|
|
301
|
+
QueryParamsType,
|
|
302
|
+
GlobalErrorType,
|
|
303
|
+
LocalErrorType,
|
|
304
|
+
EndpointType,
|
|
305
|
+
RequestConfigType
|
|
306
|
+
>(this, params);
|
|
307
|
+
};
|
|
310
308
|
}
|
|
@@ -13,11 +13,7 @@ export type BuilderConfig = {
|
|
|
13
13
|
/**
|
|
14
14
|
* Url to your server
|
|
15
15
|
*/
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Disable the web event listeners and actions on window object
|
|
19
|
-
*/
|
|
20
|
-
isNodeJS?: boolean;
|
|
16
|
+
url: string;
|
|
21
17
|
/**
|
|
22
18
|
* Custom client initialization prop
|
|
23
19
|
*/
|
package/src/cache/cache.ts
CHANGED
|
@@ -154,20 +154,26 @@ export class Cache {
|
|
|
154
154
|
const syncData = this.storage.get<Response, Error>(cacheKey);
|
|
155
155
|
|
|
156
156
|
// No data in lazy storage
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
157
|
+
const hasLazyData = this.lazyStorage && data;
|
|
158
|
+
if (hasLazyData) {
|
|
159
|
+
const now = +new Date();
|
|
160
|
+
const isNewestData = syncData ? syncData.details.timestamp < data.details.timestamp : true;
|
|
161
|
+
const isStaleData = data.cacheTime <= now - data.details.timestamp;
|
|
162
|
+
const isValidLazyData = data.clearKey === this.clearKey;
|
|
163
|
+
|
|
164
|
+
if (!isValidLazyData) {
|
|
165
|
+
this.lazyStorage.delete(cacheKey);
|
|
166
|
+
}
|
|
167
|
+
if (isNewestData && !isStaleData && isValidLazyData) {
|
|
168
|
+
this.storage.set<Response, Error>(cacheKey, data);
|
|
169
|
+
this.events.emitCacheData<Response, Error>(cacheKey, data);
|
|
170
|
+
return data;
|
|
171
|
+
}
|
|
166
172
|
}
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
173
|
+
|
|
174
|
+
const isValidData = syncData?.clearKey === this.clearKey;
|
|
175
|
+
if (syncData && !isValidData) {
|
|
176
|
+
this.delete(cacheKey);
|
|
171
177
|
}
|
|
172
178
|
return syncData;
|
|
173
179
|
};
|
|
@@ -196,19 +202,20 @@ export class Cache {
|
|
|
196
202
|
// Clear running garbage collectors for given key
|
|
197
203
|
clearTimeout(this.garbageCollectors.get(cacheKey));
|
|
198
204
|
|
|
199
|
-
|
|
200
|
-
|
|
205
|
+
// Garbage collect
|
|
206
|
+
if (cacheData) {
|
|
207
|
+
const timeLeft = cacheData.garbageCollection + cacheData.details.timestamp - +new Date();
|
|
201
208
|
if (timeLeft >= 0) {
|
|
202
209
|
this.garbageCollectors.set(
|
|
203
210
|
cacheKey,
|
|
204
211
|
setTimeout(() => {
|
|
205
|
-
if (this.builder.appManager.isOnline
|
|
212
|
+
if (this.builder.appManager.isOnline) {
|
|
206
213
|
this.logger.info("Garbage collecting cache element", { cacheKey });
|
|
207
214
|
this.delete(cacheKey);
|
|
208
215
|
}
|
|
209
216
|
}, timeLeft),
|
|
210
217
|
);
|
|
211
|
-
} else if (this.builder.appManager.isOnline
|
|
218
|
+
} else if (this.builder.appManager.isOnline) {
|
|
212
219
|
this.logger.info("Garbage collecting cache element", { cacheKey });
|
|
213
220
|
this.delete(cacheKey);
|
|
214
221
|
}
|
package/src/cache/cache.types.ts
CHANGED
|
@@ -2,8 +2,8 @@ import { getErrorMessage, ClientResponseSuccessType, ClientResponseErrorType, Pr
|
|
|
2
2
|
import { CommandInstance, getProgressData, ClientProgressEvent } from "command";
|
|
3
3
|
import { ExtractResponse, ExtractError } from "types";
|
|
4
4
|
|
|
5
|
-
export const getClientBindings = async (cmd: CommandInstance, requestId: string) => {
|
|
6
|
-
const {
|
|
5
|
+
export const getClientBindings = async <ConfigType = any>(cmd: CommandInstance, requestId: string) => {
|
|
6
|
+
const { url, commandManager, loggerManager, headerMapper, payloadMapper } = cmd.builder;
|
|
7
7
|
|
|
8
8
|
const logger = loggerManager.init("Client");
|
|
9
9
|
|
|
@@ -31,11 +31,11 @@ export const getClientBindings = async (cmd: CommandInstance, requestId: string)
|
|
|
31
31
|
|
|
32
32
|
commandManager.addAbortController(abortKey, requestId);
|
|
33
33
|
|
|
34
|
-
const fullUrl =
|
|
34
|
+
const fullUrl = url + endpoint;
|
|
35
35
|
const effects = builder.effects.filter((effect) => command.effectKey === effect.getEffectKey());
|
|
36
36
|
const headers = headerMapper(command);
|
|
37
37
|
const payload = payloadMapper(data);
|
|
38
|
-
const config = { ...command.commandOptions.options };
|
|
38
|
+
const config: ConfigType = { ...command.commandOptions.options };
|
|
39
39
|
|
|
40
40
|
const getRequestStartTimestamp = () => {
|
|
41
41
|
return requestStartTimestamp;
|
|
@@ -255,8 +255,8 @@ export const getClientBindings = async (cmd: CommandInstance, requestId: string)
|
|
|
255
255
|
}
|
|
256
256
|
|
|
257
257
|
const fn = () => {
|
|
258
|
-
callback();
|
|
259
258
|
onAbortError(resolve);
|
|
259
|
+
callback();
|
|
260
260
|
commandManager.events.emitAbort(abortKey, requestId, command);
|
|
261
261
|
};
|
|
262
262
|
|