@hyper-fetch/core 1.7.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/README.md +3 -2
  2. package/dist/builder/builder.d.ts +17 -18
  3. package/dist/builder/builder.types.d.ts +14 -18
  4. package/dist/builder/builder.utils.d.ts +3 -3
  5. package/dist/cache/cache.d.ts +5 -5
  6. package/dist/cache/cache.events.d.ts +1 -1
  7. package/dist/cache/cache.types.d.ts +9 -9
  8. package/dist/cache/cache.utils.d.ts +2 -2
  9. package/dist/client/fetch.client.bindings.d.ts +5 -5
  10. package/dist/client/fetch.client.browser.d.ts +2 -0
  11. package/dist/client/fetch.client.d.ts +1 -1
  12. package/dist/client/fetch.client.server.d.ts +2 -0
  13. package/dist/client/fetch.client.types.d.ts +15 -15
  14. package/dist/client/fetch.client.utils.d.ts +5 -2
  15. package/dist/client/index.d.ts +1 -0
  16. package/dist/command/command.d.ts +11 -11
  17. package/dist/command/command.types.d.ts +27 -26
  18. package/dist/command/command.utils.d.ts +4 -4
  19. package/dist/dispatcher/dispatcher.d.ts +3 -3
  20. package/dist/dispatcher/dispatcher.events.d.ts +2 -2
  21. package/dist/dispatcher/dispatcher.types.d.ts +7 -7
  22. package/dist/dispatcher/dispatcher.utils.d.ts +3 -3
  23. package/dist/effect/fetch.effect.d.ts +4 -4
  24. package/dist/effect/fetch.effect.types.d.ts +7 -7
  25. package/dist/index.cjs.js +1 -1
  26. package/dist/index.cjs.js.map +4 -4
  27. package/dist/index.d.ts +1 -0
  28. package/dist/index.esm.js +1 -1
  29. package/dist/index.esm.js.map +4 -4
  30. package/dist/managers/app/app.manager.constants.d.ts +2 -2
  31. package/dist/managers/app/app.manager.d.ts +3 -4
  32. package/dist/managers/app/app.manager.types.d.ts +3 -4
  33. package/dist/managers/command/command.manager.d.ts +21 -21
  34. package/dist/managers/command/command.manager.events.d.ts +3 -3
  35. package/dist/managers/command/command.manager.types.d.ts +5 -5
  36. package/dist/managers/logger/logger.manager.d.ts +3 -3
  37. package/dist/managers/logger/logger.manager.types.d.ts +12 -12
  38. package/dist/managers/logger/logger.manager.utils.d.ts +1 -1
  39. package/dist/types/fetch.types.d.ts +16 -15
  40. package/dist/types/helpers.types.d.ts +5 -5
  41. package/dist/types/http.types.d.ts +1 -1
  42. package/package.json +5 -5
  43. package/release.config.js +0 -2
  44. package/src/builder/builder.ts +48 -50
  45. package/src/builder/builder.types.ts +1 -5
  46. package/src/cache/cache.ts +24 -17
  47. package/src/cache/cache.types.ts +1 -1
  48. package/src/client/fetch.client.bindings.ts +5 -5
  49. package/src/client/fetch.client.browser.ts +89 -0
  50. package/src/client/fetch.client.server.ts +105 -0
  51. package/src/client/fetch.client.ts +5 -94
  52. package/src/client/fetch.client.utils.ts +31 -0
  53. package/src/client/index.ts +1 -0
  54. package/src/command/command.ts +60 -18
  55. package/src/command/command.types.ts +14 -14
  56. package/src/index.ts +1 -0
  57. package/src/managers/app/app.manager.ts +7 -5
  58. package/src/managers/app/app.manager.types.ts +2 -4
  59. package/src/managers/logger/logger.manager.ts +1 -1
  60. package/src/types/fetch.types.ts +32 -12
@@ -1,5 +1,5 @@
1
- import { RequiredKeys } from "../../types";
2
- import { AppManagerOptionsType } from "./..";
1
+ import { RequiredKeys } from "types";
2
+ import { AppManagerOptionsType } from "managers";
3
3
  export declare enum AppEvents {
4
4
  focus = "focus",
5
5
  blur = "blur",
@@ -1,7 +1,6 @@
1
1
  /// <reference types="node" />
2
2
  import EventEmitter from "events";
3
- import { BuilderInstance } from "../../builder";
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(builder: BuilderInstance, options?: AppManagerOptionsType);
29
+ constructor(options?: AppManagerOptionsType);
31
30
  private setInitialFocus;
32
31
  private setInitialOnline;
33
32
  setFocused: (isFocused: boolean) => void;
@@ -1,7 +1,6 @@
1
- import { BuilderInstance } from "../../builder";
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, builder: BuilderInstance) => void;
6
- onlineEvent?: (setOnline: (isOnline: boolean) => void, builder: BuilderInstance) => 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("./..").CommandLoadingEventType) => void;
11
- emitRequestStart: (queueKey: string, requestId: string, details: import("./..").CommandEventDetails<import("../..").CommandInstance>) => void;
12
- emitResponseStart: (queueKey: string, requestId: string, details: import("./..").CommandEventDetails<import("../..").CommandInstance>) => void;
13
- emitUploadProgress: (queueKey: string, requestId: string, values: import("../..").FetchProgressType, details: import("./..").CommandEventDetails<import("../..").CommandInstance>) => void;
14
- emitDownloadProgress: (queueKey: string, requestId: string, values: import("../..").FetchProgressType, details: import("./..").CommandEventDetails<import("../..").CommandInstance>) => void;
15
- emitResponse: (cacheKey: string, requestId: string, response: import("../..").ClientResponseType<unknown, unknown>, details: import("./..").CommandResponseDetails) => void;
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("./..").CommandEventDetails<T>) => void;
18
- onLoading: (queueKey: string, callback: (values: import("./..").CommandLoadingEventType) => void) => VoidFunction;
19
- onLoadingById: (requestId: string, callback: (values: import("./..").CommandLoadingEventType) => void) => VoidFunction;
20
- onRequestStart: <T_1 extends import("../..").CommandInstance>(queueKey: string, callback: (details: import("./..").CommandEventDetails<T_1>) => void) => VoidFunction;
21
- onRequestStartById: <T_2 extends import("../..").CommandInstance>(requestId: string, callback: (details: import("./..").CommandEventDetails<T_2>) => void) => VoidFunction;
22
- onResponseStart: <T_3 extends import("../..").CommandInstance>(queueKey: string, callback: (details: import("./..").CommandEventDetails<T_3>) => void) => VoidFunction;
23
- onResponseStartById: <T_4 extends import("../..").CommandInstance>(requestId: string, callback: (details: import("./..").CommandEventDetails<T_4>) => void) => VoidFunction;
24
- onUploadProgress: <T_5 extends import("../..").CommandInstance = import("../..").CommandInstance>(queueKey: string, callback: (values: import("../..").FetchProgressType, details: import("./..").CommandEventDetails<T_5>) => void) => VoidFunction;
25
- onUploadProgressById: <T_6 extends import("../..").CommandInstance = import("../..").CommandInstance>(requestId: string, callback: (values: import("../..").FetchProgressType, details: import("./..").CommandEventDetails<T_6>) => void) => VoidFunction;
26
- onDownloadProgress: <T_7 extends import("../..").CommandInstance = import("../..").CommandInstance>(queueKey: string, callback: (values: import("../..").FetchProgressType, details: import("./..").CommandEventDetails<T_7>) => void) => VoidFunction;
27
- onDownloadProgressById: <T_8 extends import("../..").CommandInstance = import("../..").CommandInstance>(requestId: string, callback: (values: import("../..").FetchProgressType, details: import("./..").CommandEventDetails<T_8>) => void) => VoidFunction;
28
- onResponse: <ResponseType_1, ErrorType>(cacheKey: string, callback: (response: import("../..").ClientResponseType<ResponseType_1, ErrorType>, details: import("./..").CommandResponseDetails) => void) => VoidFunction;
29
- onResponseById: <ResponseType_2, ErrorType_1>(requestId: string, callback: (response: import("../..").ClientResponseType<ResponseType_2, ErrorType_1>, details: import("./..").CommandResponseDetails) => void) => VoidFunction;
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("./..").CommandEventDetails<T_9>) => void) => VoidFunction;
33
- onRemoveById: <T_10 extends import("../..").CommandInstance = import("../..").CommandInstance>(requestId: string, callback: (details: import("./..").CommandEventDetails<T_10>) => void) => VoidFunction;
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 "../../client";
4
- import { CommandInstance } from "../../command";
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 "../../command";
2
- export declare type CommandLoadingEventType = {
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 declare type CommandEventDetails<T extends CommandInstance> = {
9
+ export type CommandEventDetails<T extends CommandInstance> = {
10
10
  requestId: string;
11
11
  command: T;
12
12
  };
13
- export declare type CommandResponseDetails = {
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 declare type CommandRemoveDetails = {
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 "../../builder";
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 "../../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 = {
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 declare type LogType = {
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 declare type LoggerLevelType = "error" | "warning" | "info" | "debug";
20
- export declare type LoggerMessageType = string | Record<string, unknown> | Array<unknown>;
21
- export declare type LoggerRequestEventData = {
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 declare type LoggerResponseEventData = {
25
+ export type LoggerResponseEventData = {
26
26
  requestId: string;
27
27
  command: CommandInstance;
28
28
  response: ClientResponseType<unknown, unknown>;
@@ -1,3 +1,3 @@
1
- import { LoggerFunctionType } from "./..";
1
+ import { LoggerFunctionType } from "managers";
2
2
  export declare const getTime: () => string;
3
3
  export declare const logger: LoggerFunctionType;
@@ -1,15 +1,16 @@
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;
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 declare type NegativeTypes = null | undefined;
2
- export declare type NullableType<T> = T | NegativeTypes;
3
- export declare type NullableKeys<T> = {
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 declare type NonNullableKeys<T> = {
6
+ export type NonNullableKeys<T> = {
7
7
  [P in keyof T]-?: NonNullable<T[P]>;
8
8
  };
9
- export declare type RequiredKeys<T> = {
9
+ export type RequiredKeys<T> = {
10
10
  [P in keyof T]-?: Exclude<T[P], NegativeTypes>;
11
11
  };
@@ -1 +1 @@
1
- export declare type HttpMethodsType = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
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.7.0",
3
+ "version": "2.0.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
- "dependencies": {
39
- "events": "^3.3.0"
40
- },
39
+ "peerDependencies": {},
40
+ "dependencies": {},
41
41
  "devDependencies": {},
42
42
  "lint-staged": {
43
43
  "*.{js,jsx,ts,tsx}": [
44
- "yarn jest --bail --findRelatedTests --passWithNoTests"
44
+ "yarn jest --passWithNoTests"
45
45
  ]
46
46
  },
47
47
  "keywords": [
package/release.config.js CHANGED
@@ -1,8 +1,6 @@
1
1
  // eslint-disable-next-line @typescript-eslint/no-var-requires
2
2
  const config = require("../../release.config");
3
3
 
4
- // We want to publish only single release notes
5
4
  const newConfig = { ...config, publish: undefined, success: undefined, fail: undefined };
6
- newConfig.release.plugins.push(["@semantic-release/git"]);
7
5
 
8
6
  module.exports = newConfig;
@@ -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 baseUrl: string;
37
- readonly isNodeJS: boolean;
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 { baseUrl, isNodeJS, client, appManager, cache, fetchDispatcher, submitDispatcher } = this.options;
89
- this.baseUrl = baseUrl;
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(this);
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
- baseUrl: string;
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
  */
@@ -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
- if (!this.lazyStorage || !data) return syncData;
158
-
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 isValidData = data.clearKey === this.clearKey;
163
-
164
- if (!isValidData) {
165
- this.lazyStorage.delete(cacheKey);
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
- if (isNewestData && !isStaleData && isValidData) {
168
- this.storage.set<Response, Error>(cacheKey, data);
169
- this.events.emitCacheData<Response, Error>(cacheKey, data);
170
- return data;
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
- if (cacheData && cacheData.garbageCollection) {
200
- const timeLeft = cacheData.cacheTime + cacheData.details.timestamp - +new Date();
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 && cacheData.garbageCollection) {
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 && cacheData.garbageCollection) {
218
+ } else if (this.builder.appManager.isOnline) {
212
219
  this.logger.info("Garbage collecting cache element", { cacheKey });
213
220
  this.delete(cacheKey);
214
221
  }
@@ -35,7 +35,7 @@ export type CacheValueType<Response = any, Error = any> = {
35
35
  details: CommandResponseDetails;
36
36
  cacheTime: number;
37
37
  clearKey: string;
38
- garbageCollection: boolean;
38
+ garbageCollection: number;
39
39
  };
40
40
 
41
41
  // Storage
@@ -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 { baseUrl, commandManager, loggerManager, headerMapper, payloadMapper } = cmd.builder;
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 = baseUrl + endpoint;
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