@hyper-fetch/core 6.0.1 → 7.0.1
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 +8 -3
- package/README.md +5 -5
- package/dist/browser/index.js +3714 -0
- package/dist/browser/index.js.map +7 -0
- package/dist/index.d.ts +1669 -1091
- package/dist/index.js +3952 -0
- package/dist/index.js.map +7 -0
- package/package.json +16 -18
- package/src/adapter/adapter.bindings.ts +261 -165
- package/src/adapter/adapter.ts +480 -8
- package/src/adapter/adapter.types.ts +111 -139
- package/src/adapter/adapter.utils.ts +71 -25
- package/src/adapter/index.ts +1 -2
- package/src/cache/cache.events.ts +58 -14
- package/src/cache/cache.ts +158 -91
- package/src/cache/cache.types.ts +15 -25
- package/src/cache/cache.utils.ts +17 -10
- package/src/client/client.create.ts +17 -0
- package/src/client/client.ts +247 -266
- package/src/client/client.types.ts +43 -21
- package/src/client/client.utils.ts +7 -168
- package/src/client/index.ts +1 -0
- package/src/constants/http.constants.ts +6 -6
- package/src/constants/time.constants.ts +10 -10
- package/src/dispatcher/dispatcher.constants.ts +5 -5
- package/src/dispatcher/dispatcher.events.ts +85 -20
- package/src/dispatcher/dispatcher.ts +265 -199
- package/src/dispatcher/dispatcher.types.ts +6 -7
- package/src/dispatcher/dispatcher.utils.ts +26 -10
- package/src/http-adapter/http-adapter.browser.ts +101 -0
- package/src/http-adapter/http-adapter.constants.ts +8 -0
- package/src/http-adapter/http-adapter.server.ts +134 -0
- package/src/http-adapter/http-adapter.ts +11 -0
- package/src/http-adapter/http-adapter.types.ts +86 -0
- package/src/http-adapter/http-adapter.utils.ts +177 -0
- package/src/http-adapter/index.ts +4 -0
- package/src/index.ts +2 -1
- package/src/managers/app/app.manager.constants.ts +4 -4
- package/src/managers/app/app.manager.events.ts +12 -12
- package/src/managers/app/app.manager.ts +10 -7
- package/src/managers/app/app.manager.utils.ts +12 -2
- package/src/managers/logger/logger.manager.constants.ts +13 -18
- package/src/managers/logger/logger.manager.ts +37 -49
- package/src/managers/logger/logger.manager.types.ts +32 -21
- package/src/managers/logger/logger.manager.utils.ts +23 -17
- package/src/managers/request/request.manager.events.ts +222 -109
- package/src/managers/request/request.manager.ts +5 -2
- package/src/managers/request/request.manager.types.ts +35 -7
- package/src/managers/request/request.manager.utils.ts +29 -17
- package/src/mocker/mocker.ts +115 -81
- package/src/mocker/mocker.types.ts +29 -25
- package/src/plugin/index.ts +2 -0
- package/src/plugin/plugin.ts +159 -0
- package/src/plugin/plugin.types.ts +181 -0
- package/src/request/request.ts +366 -323
- package/src/request/request.types.ts +144 -148
- package/src/request/request.utils.ts +35 -32
- package/src/types/adapter.types.ts +63 -0
- package/src/types/client.types.ts +3 -3
- package/src/types/helpers.types.ts +16 -3
- package/src/types/hydration.types.ts +16 -0
- package/src/types/index.ts +3 -1
- package/src/types/request.types.ts +73 -0
- package/src/utils/emitter.utils.ts +52 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/uuid.utils.ts +4 -2
- package/tsconfig.base.json +4 -5
- package/tsconfig.json +5 -4
- package/dist/browser/index.cjs.js +0 -2420
- package/dist/browser/index.cjs.js.map +0 -7
- package/dist/browser/index.esm.js +0 -2395
- package/dist/browser/index.esm.js.map +0 -7
- package/dist/index.cjs.js +0 -2579
- package/dist/index.cjs.js.map +0 -7
- package/dist/index.esm.js +0 -2474
- package/dist/index.esm.js.map +0 -7
- package/src/adapter/adapter.browser.ts +0 -100
- package/src/adapter/adapter.constants.ts +0 -8
- package/src/adapter/adapter.server.ts +0 -93
- package/src/effect/index.ts +0 -2
- package/src/effect/request.effect.ts +0 -31
- package/src/effect/request.effect.types.ts +0 -44
- package/src/types/fetch.types.ts +0 -50
|
@@ -1,6 +1,11 @@
|
|
|
1
|
-
export type
|
|
1
|
+
export type Prettify<T> = {
|
|
2
|
+
[K in keyof T]: T[K];
|
|
3
|
+
} & {};
|
|
2
4
|
|
|
3
|
-
|
|
5
|
+
// DO NOT ADD `void` to this type, it will break the type generation
|
|
6
|
+
export type EmptyTypes = null | undefined;
|
|
7
|
+
|
|
8
|
+
export type NullableType<T> = T | EmptyTypes;
|
|
4
9
|
|
|
5
10
|
export type NullableKeys<T> = {
|
|
6
11
|
[P in keyof T]-?: NullableType<T[P]>;
|
|
@@ -11,5 +16,13 @@ export type NonNullableKeys<T> = {
|
|
|
11
16
|
};
|
|
12
17
|
|
|
13
18
|
export type RequiredKeys<T> = {
|
|
14
|
-
[P in keyof T]-?: Exclude<T[P],
|
|
19
|
+
[P in keyof T]-?: Exclude<T[P], EmptyTypes>;
|
|
15
20
|
};
|
|
21
|
+
|
|
22
|
+
export type TypeWithDefaults<
|
|
23
|
+
Types extends Record<string, any>,
|
|
24
|
+
Key extends keyof Types,
|
|
25
|
+
Value,
|
|
26
|
+
> = Key extends keyof Types ? Exclude<Types[Key], EmptyTypes | void | never> : Value;
|
|
27
|
+
|
|
28
|
+
export type SyncOrAsync<T> = T | Promise<T>;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { AdapterInstance, ResponseType } from "adapter";
|
|
2
|
+
import { RequestCacheType } from "cache";
|
|
3
|
+
import { RequestInstance } from "request";
|
|
4
|
+
|
|
5
|
+
export type HydrationOptions = RequestCacheType<RequestInstance> & {
|
|
6
|
+
override: boolean;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export type HydrateDataType<Data = any, Error = any, Adapter extends AdapterInstance = any> = HydrationOptions & {
|
|
10
|
+
/** Hydration timestamp */
|
|
11
|
+
timestamp: number;
|
|
12
|
+
/** Hydrated response */
|
|
13
|
+
response: ResponseType<Data, Error, Adapter>;
|
|
14
|
+
/** Hydrated flag */
|
|
15
|
+
hydrated: true;
|
|
16
|
+
};
|
package/src/types/index.ts
CHANGED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { ResponseType } from "adapter";
|
|
2
|
+
import { ExtractRouteParams, Request, RequestInstance } from "request";
|
|
3
|
+
import { ExtractClientAdapterType, ExtractClientGlobalError } from "./client.types";
|
|
4
|
+
import { Client, ClientInstance } from "client";
|
|
5
|
+
import { TypeWithDefaults } from "./helpers.types";
|
|
6
|
+
import { ExtractAdapterEndpointType } from "./adapter.types";
|
|
7
|
+
|
|
8
|
+
export type ExtractAdapterResolvedType<T extends RequestInstance> = ResponseType<
|
|
9
|
+
ExtractResponseType<T>,
|
|
10
|
+
ExtractErrorType<T>,
|
|
11
|
+
ExtractAdapterType<T>
|
|
12
|
+
>;
|
|
13
|
+
|
|
14
|
+
export type ExtractResponseType<T> = T extends Request<infer D, any, any, any, any, any, any, any, any> ? D : never;
|
|
15
|
+
|
|
16
|
+
export type ExtractPayloadType<T> = T extends Request<any, infer D, any, any, any, any, any, any, any> ? D : never;
|
|
17
|
+
|
|
18
|
+
export type ExtractQueryParamsType<T> = T extends Request<any, any, infer Q, any, any, any, any, any, any> ? Q : never;
|
|
19
|
+
|
|
20
|
+
export type ExtractErrorType<T> =
|
|
21
|
+
T extends Request<any, any, any, infer G, any, infer C, any, any, any> ? G | ExtractClientGlobalError<C> : never;
|
|
22
|
+
|
|
23
|
+
export type ExtractGlobalErrorType<T> =
|
|
24
|
+
T extends Request<any, any, any, any, any, infer C, any, any, any> ? ExtractClientGlobalError<C> : never;
|
|
25
|
+
|
|
26
|
+
export type ExtractLocalErrorType<T> = T extends Request<any, any, any, infer E, any, any, any, any, any> ? E : never;
|
|
27
|
+
|
|
28
|
+
export type ExtractParamsType<T> =
|
|
29
|
+
T extends Request<any, any, any, any, infer E, any, any, any, any>
|
|
30
|
+
? E extends string
|
|
31
|
+
? ExtractRouteParams<E>
|
|
32
|
+
: never
|
|
33
|
+
: never;
|
|
34
|
+
|
|
35
|
+
export type ExtractEndpointType<T> = T extends Request<any, any, any, any, infer E, any, any, any, any> ? E : never;
|
|
36
|
+
|
|
37
|
+
export type ExtractClientType<T> = T extends Request<any, any, any, any, any, infer C, any, any, any> ? C : never;
|
|
38
|
+
|
|
39
|
+
export type ExtractAdapterType<T> =
|
|
40
|
+
T extends Request<any, any, any, any, any, infer C, any, any, any> ? ExtractClientAdapterType<C> : never;
|
|
41
|
+
|
|
42
|
+
export type ExtractHasPayloadType<T> = T extends Request<any, any, any, any, any, any, infer D, any, any> ? D : never;
|
|
43
|
+
|
|
44
|
+
export type ExtractHasParamsType<T> = T extends Request<any, any, any, any, any, any, any, infer P, any> ? P : never;
|
|
45
|
+
|
|
46
|
+
export type ExtractHasQueryParamsType<T> =
|
|
47
|
+
T extends Request<any, any, any, any, any, any, any, any, infer Q> ? Q : never;
|
|
48
|
+
|
|
49
|
+
export type ExtendRequest<
|
|
50
|
+
Req extends RequestInstance,
|
|
51
|
+
Properties extends {
|
|
52
|
+
response?: any;
|
|
53
|
+
payload?: any;
|
|
54
|
+
queryParams?: any;
|
|
55
|
+
globalError?: any;
|
|
56
|
+
localError?: any;
|
|
57
|
+
endpoint?: ExtractAdapterEndpointType<ExtractAdapterType<Req>>;
|
|
58
|
+
client?: Client<any, any>;
|
|
59
|
+
hasData?: true | false;
|
|
60
|
+
hasParams?: true | false;
|
|
61
|
+
hasQuery?: true | false;
|
|
62
|
+
},
|
|
63
|
+
> = Request<
|
|
64
|
+
TypeWithDefaults<Properties, "response", ExtractResponseType<Req>>,
|
|
65
|
+
TypeWithDefaults<Properties, "payload", ExtractPayloadType<Req>>,
|
|
66
|
+
TypeWithDefaults<Properties, "queryParams", ExtractQueryParamsType<Req>>,
|
|
67
|
+
TypeWithDefaults<Properties, "localError", ExtractLocalErrorType<Req>>,
|
|
68
|
+
Properties["endpoint"] extends string ? Properties["endpoint"] : ExtractEndpointType<Req>,
|
|
69
|
+
Properties["client"] extends ClientInstance ? Properties["client"] : ExtractClientType<Req>,
|
|
70
|
+
Properties["hasData"] extends true ? true : ExtractHasPayloadType<Req>,
|
|
71
|
+
Properties["hasParams"] extends true ? true : ExtractHasParamsType<Req>,
|
|
72
|
+
Properties["hasQuery"] extends true ? true : ExtractHasQueryParamsType<Req>
|
|
73
|
+
>;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import Emitter from "events";
|
|
2
|
+
|
|
3
|
+
const getListenName = (event: string | symbol) => `listen_${String(event)}`;
|
|
4
|
+
|
|
5
|
+
export class EventEmitter extends Emitter {
|
|
6
|
+
emitCallbacks: Array<(event: string, data: any, isTriggeredExternally?: true) => void> = [];
|
|
7
|
+
|
|
8
|
+
// eslint-disable-next-line @typescript-eslint/no-useless-constructor
|
|
9
|
+
constructor(options?: ConstructorParameters<typeof Emitter>[0]) {
|
|
10
|
+
super(options);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
emit(type: string, data: any, isTriggeredExternally: boolean) {
|
|
14
|
+
const params: [string, any, true?] = [type, data];
|
|
15
|
+
|
|
16
|
+
if (isTriggeredExternally) {
|
|
17
|
+
params.push(isTriggeredExternally);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
this.emitCallbacks?.forEach((callback) => callback(...params));
|
|
21
|
+
return super.emit(...params);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
onEmit = (callback: (event: string, data: any, isTriggeredExternally?: true) => void) => {
|
|
25
|
+
this.emitCallbacks?.push(callback);
|
|
26
|
+
return () => {
|
|
27
|
+
this.emitCallbacks = this.emitCallbacks?.filter((cb) => cb !== callback);
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
onListener = (event: string, listener: (count: number) => void) => {
|
|
32
|
+
super.on(getListenName(event), listener);
|
|
33
|
+
return () => {
|
|
34
|
+
super.off(getListenName(event), listener);
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
on = (event: string | symbol, listener: (...args: any[]) => void) => {
|
|
39
|
+
super.on(event, listener);
|
|
40
|
+
super.emit(getListenName(event), super.listeners(event).length);
|
|
41
|
+
return this;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
off = (event: string | symbol, listener: (...args: any[]) => void) => {
|
|
45
|
+
super.off(event, listener);
|
|
46
|
+
super.emit(getListenName(event), super.listeners(event).length);
|
|
47
|
+
return this;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
addListener = this.on;
|
|
51
|
+
removeListener = this.off;
|
|
52
|
+
}
|
package/src/utils/index.ts
CHANGED
package/src/utils/uuid.utils.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { RequestInstance } from "request";
|
|
2
|
+
|
|
3
|
+
export const getUniqueRequestId = (request: Pick<RequestInstance, "queryKey">) => {
|
|
4
|
+
return `${request.queryKey}_${Date.now().toString(36)}_${Math.random().toString(36).substring(2)}`;
|
|
3
5
|
};
|
package/tsconfig.base.json
CHANGED
|
@@ -4,10 +4,9 @@
|
|
|
4
4
|
"rootDir": "./src",
|
|
5
5
|
"baseUrl": "./src",
|
|
6
6
|
"paths": {
|
|
7
|
-
|
|
8
|
-
"@
|
|
7
|
+
// We set double browser adapters because this file is used only for the browser build
|
|
8
|
+
"@browser-adapter": ["./http-adapter/http-adapter.browser.ts"],
|
|
9
|
+
"@server-adapter": ["./http-adapter/http-adapter.browser.ts"]
|
|
9
10
|
}
|
|
10
|
-
}
|
|
11
|
-
"include": ["./src/index.ts", "__tests__/*"],
|
|
12
|
-
"exclude": ["dist", "jest.config.ts"]
|
|
11
|
+
}
|
|
13
12
|
}
|
package/tsconfig.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"extends": "../../tsconfig.json",
|
|
3
3
|
"compilerOptions": {
|
|
4
|
-
"rootDir": ".",
|
|
5
4
|
"baseUrl": "./src",
|
|
6
5
|
"paths": {
|
|
7
|
-
|
|
8
|
-
"@
|
|
6
|
+
// This build produce the isomorphic build
|
|
7
|
+
"@browser-adapter": ["./http-adapter/http-adapter.browser.ts"],
|
|
8
|
+
"@server-adapter": ["./http-adapter/http-adapter.server.ts"],
|
|
9
|
+
"@hyper-fetch/core": ["../src"]
|
|
9
10
|
}
|
|
10
11
|
},
|
|
11
|
-
"exclude": ["dist", "jest.config.ts"]
|
|
12
|
+
"exclude": ["node_modules", "dist", "jest.config.ts", "__tests__"]
|
|
12
13
|
}
|