@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
package/src/mocker/mocker.ts
CHANGED
|
@@ -1,92 +1,126 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { getAdapterBindings, AdapterInstance, RequestProcessingError } from "adapter";
|
|
2
|
+
import { ExtractAdapterExtraType, ExtractAdapterStatusType } from "types";
|
|
3
3
|
|
|
4
|
-
export const mocker = async <T extends AdapterInstance
|
|
5
|
-
request
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
| "createAbortListener"
|
|
25
|
-
| "onResponseProgress"
|
|
26
|
-
| "onRequestProgress"
|
|
27
|
-
| "onResponseStart"
|
|
28
|
-
| "onBeforeRequest"
|
|
29
|
-
| "onRequestStart"
|
|
30
|
-
| "onSuccess"
|
|
31
|
-
>,
|
|
32
|
-
) => {
|
|
33
|
-
const mock = request.mock.next();
|
|
34
|
-
const result = mock.value instanceof Function ? await mock.value(request) : mock.value;
|
|
4
|
+
export const mocker = async <T extends AdapterInstance>({
|
|
5
|
+
request,
|
|
6
|
+
requestId,
|
|
7
|
+
onError,
|
|
8
|
+
onResponseEnd,
|
|
9
|
+
onTimeoutError,
|
|
10
|
+
onRequestEnd,
|
|
11
|
+
createAbortListener,
|
|
12
|
+
onResponseProgress,
|
|
13
|
+
onRequestProgress,
|
|
14
|
+
onResponseStart,
|
|
15
|
+
onBeforeRequest,
|
|
16
|
+
onRequestStart,
|
|
17
|
+
onSuccess,
|
|
18
|
+
onAbortError,
|
|
19
|
+
adapter,
|
|
20
|
+
}: Awaited<ReturnType<typeof getAdapterBindings<T>>>) => {
|
|
21
|
+
if (!request.unstable_mock) {
|
|
22
|
+
throw new RequestProcessingError("Mock should be defined when calling mocker");
|
|
23
|
+
}
|
|
35
24
|
|
|
36
|
-
|
|
37
|
-
const { data, status = 200, success = true, extra, config } = result;
|
|
38
|
-
const {
|
|
39
|
-
requestTime = 20,
|
|
40
|
-
responseTime = 20,
|
|
41
|
-
totalUploaded = 1,
|
|
42
|
-
totalDownloaded = 1,
|
|
43
|
-
timeout = false,
|
|
44
|
-
} = config || {};
|
|
25
|
+
let thrown = false;
|
|
45
26
|
|
|
46
|
-
|
|
27
|
+
const {
|
|
28
|
+
requestTime = 20,
|
|
29
|
+
responseTime = 20,
|
|
30
|
+
totalUploaded = 1000,
|
|
31
|
+
totalDownloaded = 1000,
|
|
32
|
+
timeout,
|
|
33
|
+
} = request.unstable_mock.config;
|
|
34
|
+
const result = await request.unstable_mock.fn({ request, requestId });
|
|
47
35
|
|
|
48
|
-
|
|
49
|
-
onRequestStart();
|
|
36
|
+
const { data, error, status, success = true, extra = request.client.adapter.defaultExtra } = result;
|
|
50
37
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
const currentTime = Math.min(totalTime, +new Date() - dataStart);
|
|
59
|
-
currentlyLoaded += currentlyLoaded + chunkSize >= totalSize ? totalSize - currentlyLoaded : chunkSize;
|
|
60
|
-
if (currentTime >= totalTime) {
|
|
61
|
-
resolveProgress(true);
|
|
62
|
-
clearInterval(timer);
|
|
63
|
-
} else {
|
|
64
|
-
progressFunction({
|
|
65
|
-
total: totalSize,
|
|
66
|
-
loaded: currentlyLoaded,
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
}, interval);
|
|
70
|
-
});
|
|
38
|
+
createAbortListener({
|
|
39
|
+
status: adapter.systemErrorStatus,
|
|
40
|
+
extra: adapter.systemErrorExtra,
|
|
41
|
+
onAbort: () => {
|
|
42
|
+
thrown = true;
|
|
43
|
+
},
|
|
44
|
+
});
|
|
71
45
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
46
|
+
onBeforeRequest();
|
|
47
|
+
onRequestStart();
|
|
48
|
+
|
|
49
|
+
const progress = (
|
|
50
|
+
totalTime: number,
|
|
51
|
+
totalSize: number,
|
|
52
|
+
progressFunction: typeof onResponseProgress | typeof onRequestProgress,
|
|
53
|
+
) =>
|
|
54
|
+
new Promise((resolveProgress) => {
|
|
55
|
+
if (thrown) {
|
|
56
|
+
resolveProgress(true);
|
|
57
|
+
return;
|
|
81
58
|
}
|
|
82
|
-
};
|
|
83
59
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
60
|
+
const interval = 20;
|
|
61
|
+
const dataStart = +new Date();
|
|
62
|
+
const intervals = Math.ceil(totalTime / interval);
|
|
63
|
+
const chunkSize = Math.ceil(totalSize / intervals);
|
|
64
|
+
let currentlyLoaded = 0;
|
|
65
|
+
const timer = setInterval(function handleProgressInterval() {
|
|
66
|
+
if (thrown) {
|
|
67
|
+
resolveProgress(true);
|
|
68
|
+
clearInterval(timer);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const currentTime = Math.min(totalTime, +new Date() - dataStart);
|
|
72
|
+
currentlyLoaded += currentlyLoaded + chunkSize >= totalSize ? totalSize - currentlyLoaded : chunkSize;
|
|
73
|
+
progressFunction({
|
|
74
|
+
total: totalSize,
|
|
75
|
+
loaded: currentTime >= totalTime ? totalSize : currentlyLoaded,
|
|
76
|
+
});
|
|
89
77
|
|
|
78
|
+
if (currentTime >= totalTime) {
|
|
79
|
+
resolveProgress(true);
|
|
80
|
+
clearInterval(timer);
|
|
81
|
+
}
|
|
82
|
+
}, interval);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
if (typeof timeout === "number") {
|
|
86
|
+
setTimeout(() => {
|
|
87
|
+
thrown = true;
|
|
88
|
+
onTimeoutError({
|
|
89
|
+
status: 0 as ExtractAdapterStatusType<T>,
|
|
90
|
+
extra: extra as ExtractAdapterExtraType<T>,
|
|
91
|
+
});
|
|
92
|
+
}, timeout);
|
|
93
|
+
}
|
|
94
|
+
if (!thrown) {
|
|
95
|
+
await progress(requestTime, totalUploaded, onRequestProgress);
|
|
96
|
+
}
|
|
97
|
+
if (!thrown) {
|
|
98
|
+
onRequestEnd();
|
|
99
|
+
onResponseStart();
|
|
100
|
+
}
|
|
101
|
+
if (!thrown) {
|
|
102
|
+
await progress(responseTime, totalDownloaded, onResponseProgress);
|
|
103
|
+
}
|
|
104
|
+
if (thrown) {
|
|
105
|
+
onAbortError({
|
|
106
|
+
status: adapter.systemErrorStatus,
|
|
107
|
+
extra: adapter.systemErrorExtra,
|
|
108
|
+
});
|
|
109
|
+
} else if (success || (error && data)) {
|
|
110
|
+
onSuccess({
|
|
111
|
+
data,
|
|
112
|
+
error,
|
|
113
|
+
status: status as ExtractAdapterStatusType<T>,
|
|
114
|
+
extra: extra as ExtractAdapterExtraType<T>,
|
|
115
|
+
});
|
|
116
|
+
} else {
|
|
117
|
+
onError({
|
|
118
|
+
error,
|
|
119
|
+
status: status as ExtractAdapterStatusType<T>,
|
|
120
|
+
extra: extra as ExtractAdapterExtraType<T>,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
if (!thrown) {
|
|
90
124
|
onResponseEnd();
|
|
91
|
-
}
|
|
125
|
+
}
|
|
92
126
|
};
|
|
@@ -1,29 +1,33 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { AdapterInstance, ResponseType } from "adapter";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
data: Response | Response[] | (() => Response);
|
|
5
|
-
status?: number | string;
|
|
6
|
-
success?: boolean;
|
|
3
|
+
type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
7
4
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
5
|
+
export type MockerConfigType = {
|
|
6
|
+
/** Informs how long the request should take before returning a timeout error (in milliseconds) */
|
|
7
|
+
timeout?: number;
|
|
8
|
+
/** Simulates how long the request to the server should take (in milliseconds) */
|
|
9
|
+
requestTime?: number;
|
|
10
|
+
/** Indicates how long the response from the server should take (in milliseconds).
|
|
11
|
+
* If their combined total takes longer than provided timeout, each value will be automatically
|
|
12
|
+
* adjusted to last half of the timeout time */
|
|
13
|
+
responseTime?: number;
|
|
14
|
+
/** total number of 'bytes' to be uploaded. */
|
|
15
|
+
totalUploaded?: number;
|
|
16
|
+
/** total number of 'bytes' to be downloaded. */
|
|
17
|
+
totalDownloaded?: number;
|
|
16
18
|
};
|
|
17
19
|
|
|
18
|
-
export type
|
|
19
|
-
|
|
|
20
|
-
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
20
|
+
export type MockResponseType<Response, Error, AdapterType extends AdapterInstance> = PartialBy<
|
|
21
|
+
Omit<ResponseType<Response, Error, AdapterType>, "data" | "error" | "responseTimestamp" | "requestTimestamp">,
|
|
22
|
+
"extra" | "success"
|
|
23
|
+
> &
|
|
24
|
+
(
|
|
25
|
+
| {
|
|
26
|
+
data: Response;
|
|
27
|
+
error?: Error;
|
|
28
|
+
}
|
|
29
|
+
| {
|
|
30
|
+
data?: Response;
|
|
31
|
+
error: Error;
|
|
32
|
+
}
|
|
33
|
+
);
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { PluginMethodParameters, PluginMethods, PluginOptionsType } from "plugin";
|
|
2
|
+
import { ClientInstance } from "client";
|
|
3
|
+
|
|
4
|
+
export class Plugin<Client extends ClientInstance = ClientInstance, PluginData = void> {
|
|
5
|
+
public name: string;
|
|
6
|
+
public data: PluginData;
|
|
7
|
+
|
|
8
|
+
public client: Client | undefined = undefined;
|
|
9
|
+
|
|
10
|
+
private pluginMethods: Partial<PluginMethods<Client>> = {};
|
|
11
|
+
|
|
12
|
+
constructor(public config: PluginOptionsType<PluginData>) {
|
|
13
|
+
this.name = config.name;
|
|
14
|
+
if (config.data) {
|
|
15
|
+
this.data = config.data;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
initialize = (client: Client) => {
|
|
20
|
+
this.client = client;
|
|
21
|
+
return this;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
trigger = <Key extends keyof PluginMethods<Client>>(method: Key, data: PluginMethodParameters<Key, Client>) => {
|
|
25
|
+
const callback = this.pluginMethods[method];
|
|
26
|
+
if (callback) {
|
|
27
|
+
callback(data as any);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/* -------------------------------------------------------------------------------------------------
|
|
32
|
+
* Plugin lifecycle
|
|
33
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Callback that will be executed when plugin is mounted
|
|
37
|
+
*/
|
|
38
|
+
onMount = (callback: PluginMethods<Client>["onMount"]) => {
|
|
39
|
+
this.pluginMethods.onMount = callback;
|
|
40
|
+
return this;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Callback that will be executed when plugin is unmounted
|
|
45
|
+
*/
|
|
46
|
+
onUnmount = (callback: PluginMethods<Client>["onUnmount"]) => {
|
|
47
|
+
this.pluginMethods.onUnmount = callback;
|
|
48
|
+
return this;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/* -------------------------------------------------------------------------------------------------
|
|
52
|
+
* Request lifecycle
|
|
53
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Callback that will be executed when request is created
|
|
57
|
+
*/
|
|
58
|
+
onRequestCreate = (callback: PluginMethods<Client>["onRequestCreate"]) => {
|
|
59
|
+
this.pluginMethods.onRequestCreate = callback;
|
|
60
|
+
return this;
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Callback that will be executed when request gets triggered
|
|
64
|
+
*/
|
|
65
|
+
onRequestTrigger = (callback: PluginMethods<Client>["onRequestTrigger"]) => {
|
|
66
|
+
this.pluginMethods.onRequestTrigger = callback;
|
|
67
|
+
return this;
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* Callback that will be executed when request starts
|
|
71
|
+
*/
|
|
72
|
+
onRequestStart = (callback: PluginMethods<Client>["onRequestStart"]) => {
|
|
73
|
+
this.pluginMethods.onRequestStart = callback;
|
|
74
|
+
return this;
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* Callback that will be executed when response is successful
|
|
78
|
+
*/
|
|
79
|
+
onRequestSuccess = (callback: PluginMethods<Client>["onRequestSuccess"]) => {
|
|
80
|
+
this.pluginMethods.onRequestSuccess = callback;
|
|
81
|
+
return this;
|
|
82
|
+
};
|
|
83
|
+
/**
|
|
84
|
+
* Callback that will be executed when response is failed
|
|
85
|
+
*/
|
|
86
|
+
onRequestError = (callback: PluginMethods<Client>["onRequestError"]) => {
|
|
87
|
+
this.pluginMethods.onRequestError = callback;
|
|
88
|
+
return this;
|
|
89
|
+
};
|
|
90
|
+
/**
|
|
91
|
+
* Callback that will be executed when response is finished
|
|
92
|
+
*/
|
|
93
|
+
onRequestFinished = (callback: PluginMethods<Client>["onRequestFinished"]) => {
|
|
94
|
+
this.pluginMethods.onRequestFinished = callback;
|
|
95
|
+
return this;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
/* -------------------------------------------------------------------------------------------------
|
|
99
|
+
* Dispatcher lifecycle
|
|
100
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
101
|
+
|
|
102
|
+
onDispatcherCleared = (callback: PluginMethods<Client>["onDispatcherCleared"]) => {
|
|
103
|
+
this.pluginMethods.onDispatcherCleared = callback;
|
|
104
|
+
return this;
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
onDispatcherQueueDrained = (callback: PluginMethods<Client>["onDispatcherQueueDrained"]) => {
|
|
108
|
+
this.pluginMethods.onDispatcherQueueDrained = callback;
|
|
109
|
+
return this;
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
onDispatcherQueueRunning = (callback: PluginMethods<Client>["onDispatcherQueueRunning"]) => {
|
|
113
|
+
this.pluginMethods.onDispatcherQueueRunning = callback;
|
|
114
|
+
return this;
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
onDispatcherItemAdded = (callback: PluginMethods<Client>["onDispatcherItemAdded"]) => {
|
|
118
|
+
this.pluginMethods.onDispatcherItemAdded = callback;
|
|
119
|
+
return this;
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
onDispatcherItemDeleted = (callback: PluginMethods<Client>["onDispatcherItemDeleted"]) => {
|
|
123
|
+
this.pluginMethods.onDispatcherItemDeleted = callback;
|
|
124
|
+
return this;
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
onDispatcherQueueCreated = (callback: PluginMethods<Client>["onDispatcherQueueCreated"]) => {
|
|
128
|
+
this.pluginMethods.onDispatcherQueueCreated = callback;
|
|
129
|
+
return this;
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
onDispatcherQueueCleared = (callback: PluginMethods<Client>["onDispatcherQueueCleared"]) => {
|
|
133
|
+
this.pluginMethods.onDispatcherQueueCleared = callback;
|
|
134
|
+
return this;
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
/* -------------------------------------------------------------------------------------------------
|
|
138
|
+
* Cache lifecycle
|
|
139
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
140
|
+
|
|
141
|
+
onCacheItemChange = (callback: PluginMethods<Client>["onCacheItemChange"]) => {
|
|
142
|
+
this.pluginMethods.onCacheItemChange = callback;
|
|
143
|
+
return this;
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
onCacheItemDelete = (callback: PluginMethods<Client>["onCacheItemDelete"]) => {
|
|
147
|
+
this.pluginMethods.onCacheItemDelete = callback;
|
|
148
|
+
return this;
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
/* -------------------------------------------------------------------------------------------------
|
|
152
|
+
* Adapter lifecycle
|
|
153
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
154
|
+
|
|
155
|
+
onAdapterFetch = (callback: PluginMethods<Client>["onAdapterFetch"]) => {
|
|
156
|
+
this.pluginMethods.onAdapterFetch = callback;
|
|
157
|
+
return this;
|
|
158
|
+
};
|
|
159
|
+
}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { RequestInstance } from "request";
|
|
2
|
+
import { ResponseErrorType, ResponseType, ResponseSuccessType, AdapterInstance } from "adapter";
|
|
3
|
+
import { Plugin } from "plugin";
|
|
4
|
+
import {
|
|
5
|
+
ExtractAdapterType,
|
|
6
|
+
ExtractErrorType,
|
|
7
|
+
ExtractResponseType,
|
|
8
|
+
ExtendRequest,
|
|
9
|
+
ExtractClientAdapterType,
|
|
10
|
+
} from "types";
|
|
11
|
+
import { ClientInstance } from "client";
|
|
12
|
+
import { Cache, CacheValueType } from "cache";
|
|
13
|
+
import { Dispatcher, QueueDataType, QueueItemType } from "dispatcher";
|
|
14
|
+
|
|
15
|
+
export type PluginLifecycle = "trigger" | "start" | "success" | "error" | "finished";
|
|
16
|
+
export type PluginInstance = Plugin<any, any>;
|
|
17
|
+
export type PluginRequest<C extends ClientInstance> = ExtendRequest<
|
|
18
|
+
RequestInstance,
|
|
19
|
+
{
|
|
20
|
+
client: C;
|
|
21
|
+
}
|
|
22
|
+
>;
|
|
23
|
+
|
|
24
|
+
export type PluginOptionsType<PluginData> = {
|
|
25
|
+
/**
|
|
26
|
+
* Name of the plugin
|
|
27
|
+
*/
|
|
28
|
+
name: string;
|
|
29
|
+
/**
|
|
30
|
+
* Data stored in a plugin
|
|
31
|
+
*/
|
|
32
|
+
data?: PluginData;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export type PluginMethods<Client extends ClientInstance> = {
|
|
36
|
+
/* -------------------------------------------------------------------------------------------------
|
|
37
|
+
* Plugin lifecycle
|
|
38
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
39
|
+
|
|
40
|
+
onMount?: (data: { client: Client }) => void;
|
|
41
|
+
onUnmount?: (data: { client: Client }) => void;
|
|
42
|
+
|
|
43
|
+
/* -------------------------------------------------------------------------------------------------
|
|
44
|
+
* Request lifecycle
|
|
45
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
46
|
+
|
|
47
|
+
onRequestCreate?: (data: { request: PluginRequest<Client> }) => void;
|
|
48
|
+
onRequestTrigger?: (data: { request: PluginRequest<Client> }) => void;
|
|
49
|
+
onRequestStart?: (data: { request: PluginRequest<Client> }) => void;
|
|
50
|
+
onRequestSuccess?: (data: {
|
|
51
|
+
response: ResponseSuccessType<
|
|
52
|
+
ExtractResponseType<PluginRequest<Client>>,
|
|
53
|
+
ExtractAdapterType<PluginRequest<Client>>
|
|
54
|
+
>;
|
|
55
|
+
request: PluginRequest<Client>;
|
|
56
|
+
}) => void;
|
|
57
|
+
onRequestError?: (data: {
|
|
58
|
+
response: ResponseErrorType<ExtractErrorType<PluginRequest<Client>>, ExtractAdapterType<PluginRequest<Client>>>;
|
|
59
|
+
request: PluginRequest<Client>;
|
|
60
|
+
}) => void;
|
|
61
|
+
onRequestFinished?: (data: {
|
|
62
|
+
response: ResponseType<
|
|
63
|
+
ExtractResponseType<PluginRequest<Client>>,
|
|
64
|
+
ExtractErrorType<PluginRequest<Client>>,
|
|
65
|
+
ExtractAdapterType<PluginRequest<Client>>
|
|
66
|
+
>;
|
|
67
|
+
request: PluginRequest<Client>;
|
|
68
|
+
}) => void;
|
|
69
|
+
|
|
70
|
+
/* -------------------------------------------------------------------------------------------------
|
|
71
|
+
* Cache lifecycle
|
|
72
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
73
|
+
|
|
74
|
+
onCacheItemChange?: <Requests extends { cacheKey: string; response: any; error: any }>(data: {
|
|
75
|
+
cache: Cache<ExtractClientAdapterType<Client>>;
|
|
76
|
+
cacheKey: Requests["cacheKey"];
|
|
77
|
+
prevData: CacheValueType<Requests["response"], Requests["error"], ExtractClientAdapterType<Client>> | null;
|
|
78
|
+
newData: CacheValueType<Requests["response"], Requests["error"], ExtractClientAdapterType<Client>>;
|
|
79
|
+
}) => void;
|
|
80
|
+
onCacheItemDelete?: (data: { cacheKey: string; cache: Cache<ExtractClientAdapterType<Client>> }) => void;
|
|
81
|
+
onCacheItemInvalidate?: (data: { cacheKey: string; cache: Cache<ExtractClientAdapterType<Client>> }) => void;
|
|
82
|
+
|
|
83
|
+
/* -------------------------------------------------------------------------------------------------
|
|
84
|
+
* Dispatcher lifecycle
|
|
85
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
86
|
+
|
|
87
|
+
onDispatcherCleared?: (data: { dispatcher: Dispatcher<ExtractClientAdapterType<Client>> }) => void;
|
|
88
|
+
onDispatcherQueueDrained?: (data: {
|
|
89
|
+
dispatcher: Dispatcher<ExtractClientAdapterType<Client>>;
|
|
90
|
+
queue: QueueDataType<
|
|
91
|
+
ExtendRequest<
|
|
92
|
+
RequestInstance,
|
|
93
|
+
{
|
|
94
|
+
client: Client;
|
|
95
|
+
}
|
|
96
|
+
>
|
|
97
|
+
>;
|
|
98
|
+
}) => void;
|
|
99
|
+
onDispatcherItemAdded?: (data: {
|
|
100
|
+
dispatcher: Dispatcher<ExtractClientAdapterType<Client>>;
|
|
101
|
+
queue: QueueDataType<
|
|
102
|
+
ExtendRequest<
|
|
103
|
+
RequestInstance,
|
|
104
|
+
{
|
|
105
|
+
client: Client;
|
|
106
|
+
}
|
|
107
|
+
>
|
|
108
|
+
>;
|
|
109
|
+
queueItem: QueueItemType<
|
|
110
|
+
ExtendRequest<
|
|
111
|
+
RequestInstance,
|
|
112
|
+
{
|
|
113
|
+
client: Client;
|
|
114
|
+
}
|
|
115
|
+
>
|
|
116
|
+
>;
|
|
117
|
+
}) => void;
|
|
118
|
+
onDispatcherItemDeleted?: (data: {
|
|
119
|
+
dispatcher: Dispatcher<ExtractClientAdapterType<Client>>;
|
|
120
|
+
queue: QueueDataType<
|
|
121
|
+
ExtendRequest<
|
|
122
|
+
RequestInstance,
|
|
123
|
+
{
|
|
124
|
+
client: Client;
|
|
125
|
+
}
|
|
126
|
+
>
|
|
127
|
+
>;
|
|
128
|
+
queueItem: QueueItemType<
|
|
129
|
+
ExtendRequest<
|
|
130
|
+
RequestInstance,
|
|
131
|
+
{
|
|
132
|
+
client: Client;
|
|
133
|
+
}
|
|
134
|
+
>
|
|
135
|
+
>;
|
|
136
|
+
}) => void;
|
|
137
|
+
onDispatcherQueueRunning?: (data: {
|
|
138
|
+
dispatcher: Dispatcher<ExtractClientAdapterType<Client>>;
|
|
139
|
+
queue: QueueDataType<
|
|
140
|
+
ExtendRequest<
|
|
141
|
+
RequestInstance,
|
|
142
|
+
{
|
|
143
|
+
client: Client;
|
|
144
|
+
}
|
|
145
|
+
>
|
|
146
|
+
>;
|
|
147
|
+
status: "paused" | "stopped" | "running";
|
|
148
|
+
}) => void;
|
|
149
|
+
onDispatcherQueueCreated?: (data: {
|
|
150
|
+
dispatcher: Dispatcher<ExtractClientAdapterType<Client>>;
|
|
151
|
+
queue: QueueDataType<
|
|
152
|
+
ExtendRequest<
|
|
153
|
+
RequestInstance,
|
|
154
|
+
{
|
|
155
|
+
client: Client;
|
|
156
|
+
}
|
|
157
|
+
>
|
|
158
|
+
>;
|
|
159
|
+
}) => void;
|
|
160
|
+
onDispatcherQueueCleared?: (data: {
|
|
161
|
+
dispatcher: Dispatcher<ExtractClientAdapterType<Client>>;
|
|
162
|
+
queue: QueueDataType<
|
|
163
|
+
ExtendRequest<
|
|
164
|
+
RequestInstance,
|
|
165
|
+
{
|
|
166
|
+
client: Client;
|
|
167
|
+
}
|
|
168
|
+
>
|
|
169
|
+
>;
|
|
170
|
+
}) => void;
|
|
171
|
+
|
|
172
|
+
/* -------------------------------------------------------------------------------------------------
|
|
173
|
+
* Adapter lifecycle
|
|
174
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
175
|
+
|
|
176
|
+
onAdapterFetch?: (data: { request: RequestInstance; requestId: string; adapter: AdapterInstance }) => void;
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
export type PluginMethodParameters<Key extends keyof PluginMethods<Client>, Client extends ClientInstance> = Parameters<
|
|
180
|
+
NonNullable<PluginMethods<Client>[Key]>
|
|
181
|
+
>[0];
|