@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.
- package/README.md +3 -2
- 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 +11 -11
- package/dist/command/command.types.d.ts +27 -26
- 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 -15
- package/dist/types/helpers.types.d.ts +5 -5
- package/dist/types/http.types.d.ts +1 -1
- package/package.json +5 -5
- package/release.config.js +0 -2
- 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 +60 -18
- package/src/command/command.types.ts +14 -14
- 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
- package/src/types/fetch.types.ts +32 -12
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { getClientBindings, defaultTimeout, ClientResponseType, ClientType } from "client";
|
|
2
|
+
import { parseErrorResponse, parseResponse } from "./fetch.client.utils";
|
|
3
|
+
|
|
4
|
+
export const browserClient: ClientType = async (command, requestId) => {
|
|
5
|
+
const {
|
|
6
|
+
fullUrl,
|
|
7
|
+
headers,
|
|
8
|
+
payload,
|
|
9
|
+
config,
|
|
10
|
+
createAbortListener,
|
|
11
|
+
onBeforeRequest,
|
|
12
|
+
onRequestStart,
|
|
13
|
+
onRequestProgress,
|
|
14
|
+
onRequestEnd,
|
|
15
|
+
onResponseStart,
|
|
16
|
+
onResponseProgress,
|
|
17
|
+
onSuccess,
|
|
18
|
+
onError,
|
|
19
|
+
onResponseEnd,
|
|
20
|
+
onTimeoutError,
|
|
21
|
+
} = await getClientBindings(command, requestId);
|
|
22
|
+
|
|
23
|
+
const { method } = command;
|
|
24
|
+
|
|
25
|
+
const xhr = new XMLHttpRequest();
|
|
26
|
+
xhr.timeout = defaultTimeout;
|
|
27
|
+
|
|
28
|
+
const abort = () => xhr.abort();
|
|
29
|
+
|
|
30
|
+
return new Promise<ClientResponseType<unknown, unknown>>((resolve) => {
|
|
31
|
+
// Inject xhr options
|
|
32
|
+
Object.entries(config).forEach(([name, value]) => {
|
|
33
|
+
xhr[name] = value;
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
// Open connection
|
|
37
|
+
xhr.open(method, fullUrl, true);
|
|
38
|
+
|
|
39
|
+
// Set Headers
|
|
40
|
+
Object.entries(headers).forEach(([name, value]) => xhr.setRequestHeader(name, value));
|
|
41
|
+
|
|
42
|
+
// Listen to abort signal
|
|
43
|
+
const unmountListener = createAbortListener(abort, resolve);
|
|
44
|
+
|
|
45
|
+
// Request handlers
|
|
46
|
+
xhr.upload.onprogress = onRequestProgress;
|
|
47
|
+
|
|
48
|
+
// Response handlers
|
|
49
|
+
xhr.onloadstart = (): void => {
|
|
50
|
+
onRequestEnd();
|
|
51
|
+
onResponseStart();
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
xhr.onprogress = onResponseProgress;
|
|
55
|
+
|
|
56
|
+
xhr.onloadend = () => {
|
|
57
|
+
onResponseEnd();
|
|
58
|
+
unmountListener();
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
xhr.ontimeout = () => onTimeoutError;
|
|
62
|
+
|
|
63
|
+
// Data handler
|
|
64
|
+
xhr.onreadystatechange = (e: Event) => {
|
|
65
|
+
const event = e as unknown as ProgressEvent<XMLHttpRequest>;
|
|
66
|
+
const finishedState = 4;
|
|
67
|
+
|
|
68
|
+
if (event.target && event.target.readyState === finishedState) {
|
|
69
|
+
const { status } = event.target;
|
|
70
|
+
const isSuccess = String(status).startsWith("2") || String(status).startsWith("3");
|
|
71
|
+
|
|
72
|
+
if (isSuccess) {
|
|
73
|
+
const data = parseResponse(event.target.response);
|
|
74
|
+
onSuccess(data, status, resolve);
|
|
75
|
+
} else {
|
|
76
|
+
// delay to finish after onabort/ontimeout
|
|
77
|
+
const data = parseErrorResponse(event.target.response);
|
|
78
|
+
onError(data, status, resolve);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
// Start request
|
|
84
|
+
onBeforeRequest();
|
|
85
|
+
onRequestStart();
|
|
86
|
+
|
|
87
|
+
xhr.send(payload);
|
|
88
|
+
});
|
|
89
|
+
};
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import http from "http";
|
|
2
|
+
import stream from "stream";
|
|
3
|
+
|
|
4
|
+
import { getClientBindings, defaultTimeout, ClientResponseType, ClientType, ClientDefaultOptionsType } from "client";
|
|
5
|
+
import { parseErrorResponse, parseResponse, getUploadSize, getStreamPayload } from "./fetch.client.utils";
|
|
6
|
+
|
|
7
|
+
export const serverClient: ClientType = async (command, requestId) => {
|
|
8
|
+
const {
|
|
9
|
+
fullUrl,
|
|
10
|
+
headers,
|
|
11
|
+
payload,
|
|
12
|
+
config,
|
|
13
|
+
createAbortListener,
|
|
14
|
+
onBeforeRequest,
|
|
15
|
+
onRequestStart,
|
|
16
|
+
onRequestProgress,
|
|
17
|
+
onRequestEnd,
|
|
18
|
+
onResponseStart,
|
|
19
|
+
onResponseProgress,
|
|
20
|
+
onSuccess,
|
|
21
|
+
onTimeoutError,
|
|
22
|
+
onError,
|
|
23
|
+
onResponseEnd,
|
|
24
|
+
} = await getClientBindings<ClientDefaultOptionsType>(command, requestId);
|
|
25
|
+
|
|
26
|
+
const { method } = command;
|
|
27
|
+
|
|
28
|
+
return new Promise<ClientResponseType<unknown, unknown>>((resolve) => {
|
|
29
|
+
const execute = async () => {
|
|
30
|
+
const options: http.RequestOptions = {
|
|
31
|
+
path: fullUrl,
|
|
32
|
+
method,
|
|
33
|
+
headers: headers as http.RequestOptions["headers"],
|
|
34
|
+
timeout: defaultTimeout,
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// Inject xhr options
|
|
38
|
+
Object.entries(config).forEach(([name, value]) => {
|
|
39
|
+
options[name] = value;
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
let unmountListener = () => null;
|
|
43
|
+
onBeforeRequest();
|
|
44
|
+
|
|
45
|
+
const totalUploadBytes = payload ? Number(getUploadSize(payload)) : 0;
|
|
46
|
+
let uploadedBytes = 0;
|
|
47
|
+
|
|
48
|
+
const payloadChunks = await getStreamPayload(payload);
|
|
49
|
+
|
|
50
|
+
const request = http.request(options, (response) => {
|
|
51
|
+
response.setEncoding("utf8");
|
|
52
|
+
|
|
53
|
+
let chunks = "";
|
|
54
|
+
const totalDownloadBytes = Number(response.headers["content-length"]);
|
|
55
|
+
let downloadedBytes = 0;
|
|
56
|
+
|
|
57
|
+
response.on("data", (chunk) => {
|
|
58
|
+
if (!chunks) onResponseStart();
|
|
59
|
+
downloadedBytes += chunk.length;
|
|
60
|
+
chunks += chunk;
|
|
61
|
+
onResponseProgress({ total: totalDownloadBytes, loaded: downloadedBytes });
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
response.on("end", () => {
|
|
65
|
+
const { statusCode } = response;
|
|
66
|
+
const isSuccess = String(statusCode).startsWith("2") || String(statusCode).startsWith("3");
|
|
67
|
+
|
|
68
|
+
if (isSuccess) {
|
|
69
|
+
const data = parseResponse(chunks);
|
|
70
|
+
onSuccess(data, statusCode, resolve);
|
|
71
|
+
} else {
|
|
72
|
+
// delay to finish after onabort/ontimeout
|
|
73
|
+
const data = parseErrorResponse(chunks);
|
|
74
|
+
onError(data, statusCode, resolve);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
unmountListener();
|
|
78
|
+
onResponseEnd();
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
unmountListener = createAbortListener(request.destroy, resolve);
|
|
83
|
+
|
|
84
|
+
request.on("timeout", () => onTimeoutError(resolve));
|
|
85
|
+
request.on("error", (error) => onError(error, 0, resolve));
|
|
86
|
+
|
|
87
|
+
if (payloadChunks) {
|
|
88
|
+
const readableStream = stream.Readable.from(payloadChunks, { objectMode: false })
|
|
89
|
+
.on("data", (chunk) => {
|
|
90
|
+
if (!uploadedBytes) onRequestStart();
|
|
91
|
+
uploadedBytes += chunk.length;
|
|
92
|
+
onRequestProgress({ total: totalUploadBytes, loaded: uploadedBytes });
|
|
93
|
+
})
|
|
94
|
+
.on("end", () => {
|
|
95
|
+
onRequestEnd();
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
readableStream.pipe(request);
|
|
99
|
+
} else {
|
|
100
|
+
request.end();
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
execute();
|
|
104
|
+
});
|
|
105
|
+
};
|
|
@@ -1,98 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { browserClient, ClientType } from "client";
|
|
2
|
+
import { serverClient } from "./fetch.client.server";
|
|
3
3
|
|
|
4
4
|
export const fetchClient: ClientType = async (command, requestId) => {
|
|
5
|
-
if (!window
|
|
6
|
-
|
|
5
|
+
if (command.builder.appManager.isNodeJs || !window?.XMLHttpRequest) {
|
|
6
|
+
return serverClient(command, requestId);
|
|
7
7
|
}
|
|
8
|
-
|
|
9
|
-
const {
|
|
10
|
-
fullUrl,
|
|
11
|
-
headers,
|
|
12
|
-
payload,
|
|
13
|
-
config,
|
|
14
|
-
createAbortListener,
|
|
15
|
-
onBeforeRequest,
|
|
16
|
-
onRequestStart,
|
|
17
|
-
onRequestProgress,
|
|
18
|
-
onRequestEnd,
|
|
19
|
-
onResponseStart,
|
|
20
|
-
onResponseProgress,
|
|
21
|
-
onSuccess,
|
|
22
|
-
// onAbortError,
|
|
23
|
-
// onTimeoutError,
|
|
24
|
-
onError,
|
|
25
|
-
onResponseEnd,
|
|
26
|
-
} = await getClientBindings(command, requestId);
|
|
27
|
-
|
|
28
|
-
const { method } = command;
|
|
29
|
-
|
|
30
|
-
const xhr = new XMLHttpRequest();
|
|
31
|
-
xhr.timeout = defaultTimeout;
|
|
32
|
-
|
|
33
|
-
const abort = () => xhr.abort();
|
|
34
|
-
|
|
35
|
-
return new Promise<ClientResponseType<unknown, unknown>>((resolve) => {
|
|
36
|
-
// Inject xhr options
|
|
37
|
-
Object.entries(config).forEach(([name, value]) => {
|
|
38
|
-
xhr[name] = value;
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
// Open connection
|
|
42
|
-
xhr.open(method, fullUrl, true);
|
|
43
|
-
|
|
44
|
-
// Set Headers
|
|
45
|
-
Object.entries(headers).forEach(([name, value]) => xhr.setRequestHeader(name, value));
|
|
46
|
-
|
|
47
|
-
// Listen to abort signal
|
|
48
|
-
const unmountListener = createAbortListener(abort, resolve);
|
|
49
|
-
|
|
50
|
-
// Request handlers
|
|
51
|
-
xhr.upload.onprogress = onRequestProgress;
|
|
52
|
-
|
|
53
|
-
// Response handlers
|
|
54
|
-
xhr.onloadstart = (): void => {
|
|
55
|
-
onRequestEnd();
|
|
56
|
-
onResponseStart();
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
xhr.onprogress = onResponseProgress;
|
|
60
|
-
|
|
61
|
-
xhr.onloadend = () => {
|
|
62
|
-
onResponseEnd();
|
|
63
|
-
unmountListener();
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
// Error listeners
|
|
67
|
-
// xhr.onabort = () => onAbortError(resolve);
|
|
68
|
-
// xhr.ontimeout = () => onTimeoutError(resolve);
|
|
69
|
-
// xhr.upload.onabort = () => onAbortError(resolve);
|
|
70
|
-
// xhr.upload.ontimeout = () => onTimeoutError(resolve);
|
|
71
|
-
|
|
72
|
-
// Data handler
|
|
73
|
-
xhr.onreadystatechange = (e: Event) => {
|
|
74
|
-
const event = e as unknown as ProgressEvent<XMLHttpRequest>;
|
|
75
|
-
const finishedState = 4;
|
|
76
|
-
|
|
77
|
-
if (event.target && event.target.readyState === finishedState) {
|
|
78
|
-
const { status } = event.target;
|
|
79
|
-
const isSuccess = String(status).startsWith("2") || String(status).startsWith("3");
|
|
80
|
-
|
|
81
|
-
if (isSuccess) {
|
|
82
|
-
const data = parseResponse(event.target.response);
|
|
83
|
-
onSuccess(data, status, resolve);
|
|
84
|
-
} else {
|
|
85
|
-
// delay to finish after onabort/ontimeout
|
|
86
|
-
const data = parseErrorResponse(event.target.response);
|
|
87
|
-
onError(data, status, resolve);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
// Start request
|
|
93
|
-
onBeforeRequest();
|
|
94
|
-
onRequestStart();
|
|
95
|
-
|
|
96
|
-
xhr.send(payload);
|
|
97
|
-
});
|
|
8
|
+
return browserClient(command, requestId);
|
|
98
9
|
};
|
|
@@ -26,3 +26,34 @@ export const parseResponse = (response: string | unknown) => {
|
|
|
26
26
|
export const parseErrorResponse = <T extends CommandInstance>(response: unknown): ExtractError<T> => {
|
|
27
27
|
return response ? parseResponse(response) : getErrorMessage();
|
|
28
28
|
};
|
|
29
|
+
|
|
30
|
+
// Request
|
|
31
|
+
|
|
32
|
+
export const getUploadSize = (payload: FormData | string) => {
|
|
33
|
+
if (payload instanceof FormData) {
|
|
34
|
+
return Array.from(payload.values())
|
|
35
|
+
.map((value) => (typeof value === "string" ? value.length : value.size))
|
|
36
|
+
.reduce((a, b) => a + b, 0);
|
|
37
|
+
}
|
|
38
|
+
return payload.length;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export const fileToBuffer = (file: File | Blob) => {
|
|
42
|
+
return new Promise<Uint8Array>((resolve) => {
|
|
43
|
+
const reader = new FileReader();
|
|
44
|
+
reader.onload = () => {
|
|
45
|
+
resolve(new Uint8Array(reader.result as ArrayBufferLike));
|
|
46
|
+
};
|
|
47
|
+
reader.readAsArrayBuffer(file);
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export const getStreamPayload = (payload: FormData | string | null) => {
|
|
52
|
+
if (payload instanceof FormData) {
|
|
53
|
+
const parts = Array.from(payload.values()).map((value) =>
|
|
54
|
+
typeof value === "string" ? value : fileToBuffer(value),
|
|
55
|
+
);
|
|
56
|
+
return Promise.all(parts);
|
|
57
|
+
}
|
|
58
|
+
return payload;
|
|
59
|
+
};
|
package/src/client/index.ts
CHANGED
package/src/command/command.ts
CHANGED
|
@@ -10,6 +10,7 @@ import {
|
|
|
10
10
|
ExtractRouteParams,
|
|
11
11
|
commandSendRequest,
|
|
12
12
|
CommandCurrentType,
|
|
13
|
+
CommandMapperType,
|
|
13
14
|
} from "command";
|
|
14
15
|
import { Builder } from "builder";
|
|
15
16
|
import { getUniqueRequestId } from "utils";
|
|
@@ -53,7 +54,7 @@ export class Command<
|
|
|
53
54
|
cancelable: boolean;
|
|
54
55
|
retry: number;
|
|
55
56
|
retryTime: number;
|
|
56
|
-
garbageCollection:
|
|
57
|
+
garbageCollection: number;
|
|
57
58
|
cache: boolean;
|
|
58
59
|
cacheTime: number;
|
|
59
60
|
queued: boolean;
|
|
@@ -85,7 +86,7 @@ export class Command<
|
|
|
85
86
|
MappedData
|
|
86
87
|
>
|
|
87
88
|
| undefined,
|
|
88
|
-
readonly dataMapper?:
|
|
89
|
+
readonly dataMapper?: CommandMapperType<RequestDataType, MappedData>,
|
|
89
90
|
) {
|
|
90
91
|
const {
|
|
91
92
|
endpoint,
|
|
@@ -96,7 +97,7 @@ export class Command<
|
|
|
96
97
|
cancelable = false,
|
|
97
98
|
retry = 0,
|
|
98
99
|
retryTime = 500,
|
|
99
|
-
garbageCollection =
|
|
100
|
+
garbageCollection = DateInterval.minute * 5,
|
|
100
101
|
cache = true,
|
|
101
102
|
cacheTime = DateInterval.minute * 5,
|
|
102
103
|
queued = false,
|
|
@@ -147,13 +148,13 @@ export class Command<
|
|
|
147
148
|
return this.clone({ auth });
|
|
148
149
|
};
|
|
149
150
|
|
|
150
|
-
public setParams = (params:
|
|
151
|
-
return this.clone<HasData, true, HasQuery>({ params });
|
|
151
|
+
public setParams = <P extends ExtractRouteParams<EndpointType>>(params: P) => {
|
|
152
|
+
return this.clone<HasData, P extends null ? false : true, HasQuery>({ params });
|
|
152
153
|
};
|
|
153
154
|
|
|
154
|
-
public setData = (data:
|
|
155
|
+
public setData = <D extends RequestDataType>(data: D) => {
|
|
155
156
|
const modifiedData = this.dataMapper?.(data) || data;
|
|
156
|
-
return this.clone<true, HasParams, HasQuery, MappedData>({
|
|
157
|
+
return this.clone<D extends null ? false : true, HasParams, HasQuery, MappedData>({
|
|
157
158
|
data: modifiedData as CommandData<RequestDataType, MappedData>,
|
|
158
159
|
});
|
|
159
160
|
};
|
|
@@ -232,8 +233,8 @@ export class Command<
|
|
|
232
233
|
return this.clone({ offline });
|
|
233
234
|
};
|
|
234
235
|
|
|
235
|
-
public setDataMapper = <DataMapper
|
|
236
|
-
return this.clone<HasData, HasParams, HasQuery
|
|
236
|
+
public setDataMapper = <DataMapper extends (data: RequestDataType) => any>(mapper: DataMapper) => {
|
|
237
|
+
return this.clone<HasData, HasParams, HasQuery>(undefined, mapper);
|
|
237
238
|
};
|
|
238
239
|
|
|
239
240
|
private paramsMapper = (
|
|
@@ -307,7 +308,7 @@ export class Command<
|
|
|
307
308
|
D extends true | false = HasData,
|
|
308
309
|
P extends true | false = HasParams,
|
|
309
310
|
Q extends true | false = HasQuery,
|
|
310
|
-
|
|
311
|
+
NewMappedData = MappedData,
|
|
311
312
|
>(
|
|
312
313
|
options?: CommandCurrentType<
|
|
313
314
|
ResponseType,
|
|
@@ -316,9 +317,9 @@ export class Command<
|
|
|
316
317
|
GlobalErrorType | LocalErrorType,
|
|
317
318
|
EndpointType,
|
|
318
319
|
ClientOptions,
|
|
319
|
-
|
|
320
|
+
NewMappedData
|
|
320
321
|
>,
|
|
321
|
-
mapper?:
|
|
322
|
+
mapper?: CommandMapperType<RequestDataType, NewMappedData>,
|
|
322
323
|
): Command<
|
|
323
324
|
ResponseType,
|
|
324
325
|
RequestDataType,
|
|
@@ -330,7 +331,7 @@ export class Command<
|
|
|
330
331
|
D,
|
|
331
332
|
P,
|
|
332
333
|
Q,
|
|
333
|
-
|
|
334
|
+
NewMappedData
|
|
334
335
|
> {
|
|
335
336
|
const dump = this.dump();
|
|
336
337
|
const commandDump: CommandCurrentType<
|
|
@@ -340,7 +341,7 @@ export class Command<
|
|
|
340
341
|
GlobalErrorType | LocalErrorType,
|
|
341
342
|
EndpointType,
|
|
342
343
|
ClientOptions,
|
|
343
|
-
|
|
344
|
+
NewMappedData
|
|
344
345
|
> = {
|
|
345
346
|
...dump,
|
|
346
347
|
...options,
|
|
@@ -356,6 +357,8 @@ export class Command<
|
|
|
356
357
|
data: (options?.data || this.data) as any,
|
|
357
358
|
};
|
|
358
359
|
|
|
360
|
+
const mapperFn = (mapper || this.dataMapper) as typeof mapper;
|
|
361
|
+
|
|
359
362
|
const cloned = new Command<
|
|
360
363
|
ResponseType,
|
|
361
364
|
RequestDataType,
|
|
@@ -367,8 +370,8 @@ export class Command<
|
|
|
367
370
|
D,
|
|
368
371
|
P,
|
|
369
372
|
Q,
|
|
370
|
-
|
|
371
|
-
>(this.builder, this.commandOptions, commandDump,
|
|
373
|
+
NewMappedData
|
|
374
|
+
>(this.builder, this.commandOptions, commandDump, mapperFn);
|
|
372
375
|
|
|
373
376
|
return cloned;
|
|
374
377
|
}
|
|
@@ -490,10 +493,12 @@ export class Command<
|
|
|
490
493
|
};
|
|
491
494
|
}
|
|
492
495
|
|
|
493
|
-
//
|
|
496
|
+
// /**
|
|
497
|
+
// * Typescript test cases
|
|
498
|
+
// */
|
|
494
499
|
|
|
495
500
|
// const builder = new Builder({
|
|
496
|
-
//
|
|
501
|
+
// url: "http://localhost:3000",
|
|
497
502
|
// });
|
|
498
503
|
|
|
499
504
|
// const getUsers = builder.createCommand<{ id: string }[]>()({
|
|
@@ -516,6 +521,19 @@ export class Command<
|
|
|
516
521
|
// endpoint: "/users/:id",
|
|
517
522
|
// });
|
|
518
523
|
|
|
524
|
+
// const mappedReq = builder
|
|
525
|
+
// .createCommand<{ id: string }, { name: string }>()({
|
|
526
|
+
// method: "POST",
|
|
527
|
+
// endpoint: "/users",
|
|
528
|
+
// })
|
|
529
|
+
// .setDataMapper((data) => {
|
|
530
|
+
// const formData = new FormData();
|
|
531
|
+
// formData.append("key", data.name);
|
|
532
|
+
// return formData;
|
|
533
|
+
// });
|
|
534
|
+
|
|
535
|
+
// // ================>
|
|
536
|
+
|
|
519
537
|
// // OK
|
|
520
538
|
// getUsers.send({ queryParams: "" });
|
|
521
539
|
// getUsers.setQueryParams("").send();
|
|
@@ -524,6 +542,8 @@ export class Command<
|
|
|
524
542
|
// getUsers.send({ params: "" });
|
|
525
543
|
// getUsers.setQueryParams("").send({ queryParams: "" });
|
|
526
544
|
|
|
545
|
+
// // ================>
|
|
546
|
+
|
|
527
547
|
// // OK
|
|
528
548
|
// getUser.send({ params: { id: "" }, queryParams: "" });
|
|
529
549
|
// getUser.setParams({ id: "" }).send({ queryParams: "" });
|
|
@@ -531,6 +551,10 @@ export class Command<
|
|
|
531
551
|
// getUser.send({ queryParams: "" });
|
|
532
552
|
// getUser.send();
|
|
533
553
|
// getUser.setParams({ id: "" }).send({ params: { id: "" } });
|
|
554
|
+
// getUser.setParams(null).send();
|
|
555
|
+
// getUser.send({ params: { id: null } });
|
|
556
|
+
|
|
557
|
+
// // ================>
|
|
534
558
|
|
|
535
559
|
// // OK
|
|
536
560
|
// postUser.send({ data: { name: "" } });
|
|
@@ -538,15 +562,19 @@ export class Command<
|
|
|
538
562
|
// // Fail
|
|
539
563
|
// postUser.send({ queryParams: "" });
|
|
540
564
|
// postUser.send({ data: null });
|
|
565
|
+
// postUser.setData(null).send();
|
|
541
566
|
// postUser.send();
|
|
542
567
|
// postUser.setData({ name: "" }).send({ data: { name: "" } });
|
|
543
568
|
|
|
569
|
+
// // ================>
|
|
570
|
+
|
|
544
571
|
// // OK
|
|
545
572
|
// patchUser.send({ params: { id: "" }, data: { name: "" } });
|
|
546
573
|
// patchUser.setParams({ id: "" }).setData({ name: "" }).send();
|
|
547
574
|
// // Fail
|
|
548
575
|
// patchUser.send({ queryParams: "" });
|
|
549
576
|
// patchUser.send({ data: null });
|
|
577
|
+
// patchUser.setData(null).send();
|
|
550
578
|
// patchUser.send();
|
|
551
579
|
// patchUser
|
|
552
580
|
// .setParams({ id: "" })
|
|
@@ -556,3 +584,17 @@ export class Command<
|
|
|
556
584
|
// .setParams({ id: "" })
|
|
557
585
|
// .setData({ name: "" })
|
|
558
586
|
// .send({ params: { id: "" } });
|
|
587
|
+
|
|
588
|
+
// // ================>
|
|
589
|
+
|
|
590
|
+
// // OK
|
|
591
|
+
// mappedReq.send({ data: { name: "" } });
|
|
592
|
+
// mappedReq.setData({ name: "" }).send();
|
|
593
|
+
// // Fail
|
|
594
|
+
// mappedReq.send({ queryParams: "" });
|
|
595
|
+
// mappedReq.send({ data: undefined });
|
|
596
|
+
// mappedReq.setData(null).send();
|
|
597
|
+
// mappedReq.setData(null).send({ data: null, queryParams: () => null });
|
|
598
|
+
// mappedReq.send();
|
|
599
|
+
// mappedReq.send({ data: new FormData() });
|
|
600
|
+
// mappedReq.setData({ name: "" }).send({ data: { name: "" } });
|
|
@@ -45,7 +45,7 @@ export type CommandDump<
|
|
|
45
45
|
cancelable: boolean;
|
|
46
46
|
retry: number;
|
|
47
47
|
retryTime: number;
|
|
48
|
-
garbageCollection:
|
|
48
|
+
garbageCollection: number;
|
|
49
49
|
cache: boolean;
|
|
50
50
|
cacheTime: number;
|
|
51
51
|
queued: boolean;
|
|
@@ -106,7 +106,7 @@ export type CommandConfig<GenericEndpoint extends string, ClientOptions> = {
|
|
|
106
106
|
/**
|
|
107
107
|
* Should we trigger garbage collection or leave data in memory
|
|
108
108
|
*/
|
|
109
|
-
garbageCollection?:
|
|
109
|
+
garbageCollection?: number;
|
|
110
110
|
/**
|
|
111
111
|
* Should we save the response to cache
|
|
112
112
|
*/
|
|
@@ -161,8 +161,10 @@ export type CommandConfig<GenericEndpoint extends string, ClientOptions> = {
|
|
|
161
161
|
deduplicateTime?: number;
|
|
162
162
|
};
|
|
163
163
|
|
|
164
|
+
export type CommandMapperType<RequestDataType, MappedData> = (data: RequestDataType) => MappedData;
|
|
165
|
+
|
|
164
166
|
export type CommandData<RequestDataType, MappedData> =
|
|
165
|
-
| (MappedData extends
|
|
167
|
+
| (MappedData extends never ? RequestDataType : MappedData)
|
|
166
168
|
| NegativeTypes;
|
|
167
169
|
|
|
168
170
|
export type CommandCurrentType<
|
|
@@ -205,7 +207,10 @@ export type FetchOptionsType<ClientOptions> = Omit<
|
|
|
205
207
|
/**
|
|
206
208
|
* It will check if the query params are already set
|
|
207
209
|
*/
|
|
208
|
-
export type FetchQueryParamsType<
|
|
210
|
+
export type FetchQueryParamsType<
|
|
211
|
+
QueryParamsType extends ClientQueryParamsType | string,
|
|
212
|
+
HasQuery extends true | false = false,
|
|
213
|
+
> = HasQuery extends true
|
|
209
214
|
? { queryParams?: NegativeTypes }
|
|
210
215
|
: {
|
|
211
216
|
queryParams?: QueryParamsType | string;
|
|
@@ -216,24 +221,21 @@ export type FetchQueryParamsType<QueryParamsType, HasQuery extends true | false
|
|
|
216
221
|
*/
|
|
217
222
|
export type FetchParamsType<
|
|
218
223
|
EndpointType extends string,
|
|
219
|
-
HasParams extends true | false
|
|
224
|
+
HasParams extends true | false,
|
|
220
225
|
> = ExtractRouteParams<EndpointType> extends NegativeTypes
|
|
221
226
|
? { params?: NegativeTypes }
|
|
222
227
|
: true extends HasParams
|
|
223
228
|
? { params?: NegativeTypes }
|
|
224
|
-
: { params: ExtractRouteParams<EndpointType
|
|
229
|
+
: { params: NonNullable<ExtractRouteParams<EndpointType>> };
|
|
225
230
|
|
|
226
231
|
/**
|
|
227
232
|
* If the command data is not filled it will throw an error
|
|
228
233
|
*/
|
|
229
|
-
export type FetchRequestDataType<
|
|
230
|
-
RequestDataType,
|
|
231
|
-
HasData extends true | false = false,
|
|
232
|
-
> = RequestDataType extends NegativeTypes
|
|
234
|
+
export type FetchRequestDataType<RequestDataType, HasData extends true | false> = RequestDataType extends NegativeTypes
|
|
233
235
|
? { data?: NegativeTypes }
|
|
234
236
|
: HasData extends true
|
|
235
237
|
? { data?: NegativeTypes }
|
|
236
|
-
: { data: RequestDataType };
|
|
238
|
+
: { data: NonNullable<RequestDataType> };
|
|
237
239
|
|
|
238
240
|
export type CommandQueueOptions = {
|
|
239
241
|
dispatcherType?: "auto" | "fetch" | "submit";
|
|
@@ -262,9 +264,7 @@ export type FetchSendActionsType<Command extends CommandInstance> = {
|
|
|
262
264
|
onRemove?: (details: CommandEventDetails<Command>) => void;
|
|
263
265
|
};
|
|
264
266
|
|
|
265
|
-
export type FetchMethodType<Command extends CommandInstance> = FetchType<Command>["data"] extends
|
|
266
|
-
? (options?: FetchType<Command>) => Promise<ClientResponseType<ExtractResponse<Command>, ExtractError<Command>>>
|
|
267
|
-
: FetchType<Command>["data"] extends NegativeTypes
|
|
267
|
+
export type FetchMethodType<Command extends CommandInstance> = FetchType<Command>["data"] extends NegativeTypes
|
|
268
268
|
? FetchType<Command>["params"] extends NegativeTypes
|
|
269
269
|
? (options?: FetchType<Command>) => Promise<ClientResponseType<ExtractResponse<Command>, ExtractError<Command>>>
|
|
270
270
|
: (options: FetchType<Command>) => Promise<ClientResponseType<ExtractResponse<Command>, ExtractError<Command>>>
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import EventEmitter from "events";
|
|
2
2
|
|
|
3
|
-
import {
|
|
4
|
-
import { appManagerInitialOptions, AppManagerOptionsType, getAppManagerEvents } from "managers";
|
|
3
|
+
import { appManagerInitialOptions, AppManagerOptionsType, getAppManagerEvents, hasDocument } from "managers";
|
|
5
4
|
|
|
6
5
|
/**
|
|
7
6
|
* App manager handles main application states - focus and online. Those two values can answer questions:
|
|
@@ -16,10 +15,11 @@ export class AppManager {
|
|
|
16
15
|
emitter = new EventEmitter();
|
|
17
16
|
events = getAppManagerEvents(this.emitter);
|
|
18
17
|
|
|
18
|
+
isNodeJs: boolean;
|
|
19
19
|
isOnline: boolean;
|
|
20
20
|
isFocused: boolean;
|
|
21
21
|
|
|
22
|
-
constructor(public
|
|
22
|
+
constructor(public options?: AppManagerOptionsType) {
|
|
23
23
|
const {
|
|
24
24
|
focusEvent = appManagerInitialOptions.focusEvent,
|
|
25
25
|
onlineEvent = appManagerInitialOptions.onlineEvent,
|
|
@@ -30,8 +30,10 @@ export class AppManager {
|
|
|
30
30
|
this.setInitialFocus(initiallyFocused);
|
|
31
31
|
this.setInitialOnline(initiallyOnline);
|
|
32
32
|
|
|
33
|
-
focusEvent(this.setFocused
|
|
34
|
-
onlineEvent(this.setOnline
|
|
33
|
+
focusEvent(this.setFocused);
|
|
34
|
+
onlineEvent(this.setOnline);
|
|
35
|
+
|
|
36
|
+
this.isNodeJs = !hasDocument();
|
|
35
37
|
}
|
|
36
38
|
|
|
37
39
|
private setInitialFocus = async (initValue: Exclude<AppManagerOptionsType["initiallyFocused"], undefined>) => {
|
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
import { BuilderInstance } from "builder";
|
|
2
|
-
|
|
3
1
|
export type AppManagerOptionsType = {
|
|
4
2
|
initiallyFocused?: boolean | (() => boolean | Promise<boolean>);
|
|
5
3
|
initiallyOnline?: boolean | (() => boolean | Promise<boolean>);
|
|
6
|
-
focusEvent?: (setFocused: (isFocused: boolean) => void
|
|
7
|
-
onlineEvent?: (setOnline: (isOnline: boolean) => void
|
|
4
|
+
focusEvent?: (setFocused: (isFocused: boolean) => void) => void;
|
|
5
|
+
onlineEvent?: (setOnline: (isOnline: boolean) => void) => void;
|
|
8
6
|
};
|