@nestia/fetcher 9.0.0-dev.20251107-2 → 9.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 +93 -93
- package/package.json +1 -1
- package/src/AesPkcs5.ts +49 -49
- package/src/EncryptedFetcher.ts +176 -176
- package/src/FormDataInput.ts +80 -80
- package/src/HttpError.ts +1 -1
- package/src/IConnection.ts +241 -241
- package/src/IEncryptionPassword.ts +44 -44
- package/src/IFetchEvent.ts +31 -31
- package/src/IFetchRoute.ts +60 -60
- package/src/IPropagation.ts +99 -99
- package/src/MigrateFetcher.ts +118 -118
- package/src/NestiaSimulator.ts +82 -82
- package/src/PlainFetcher.ts +105 -105
- package/src/index.ts +7 -7
- package/src/internal/FetcherBase.ts +235 -235
|
@@ -1,235 +1,235 @@
|
|
|
1
|
-
import { HttpError } from "../HttpError";
|
|
2
|
-
import { IConnection } from "../IConnection";
|
|
3
|
-
import { IFetchEvent } from "../IFetchEvent";
|
|
4
|
-
import { IFetchRoute } from "../IFetchRoute";
|
|
5
|
-
import { IPropagation } from "../IPropagation";
|
|
6
|
-
|
|
7
|
-
/** @internal */
|
|
8
|
-
export namespace FetcherBase {
|
|
9
|
-
export interface IProps {
|
|
10
|
-
className: string;
|
|
11
|
-
encode: (
|
|
12
|
-
input: any,
|
|
13
|
-
headers: Record<string, IConnection.HeaderValue | undefined>,
|
|
14
|
-
) => string;
|
|
15
|
-
decode: (
|
|
16
|
-
input: string,
|
|
17
|
-
headers: Record<string, IConnection.HeaderValue | undefined>,
|
|
18
|
-
) => any;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export const request =
|
|
22
|
-
(props: IProps) =>
|
|
23
|
-
async <Input, Output>(
|
|
24
|
-
connection: IConnection,
|
|
25
|
-
route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
|
|
26
|
-
input?: Input,
|
|
27
|
-
stringify?: (input: Input) => string,
|
|
28
|
-
): Promise<Output> => {
|
|
29
|
-
const result = await _Propagate("fetch")(props)(
|
|
30
|
-
connection,
|
|
31
|
-
route,
|
|
32
|
-
input,
|
|
33
|
-
stringify,
|
|
34
|
-
);
|
|
35
|
-
if ((result as any).success === false)
|
|
36
|
-
throw new HttpError(
|
|
37
|
-
route.method,
|
|
38
|
-
route.path,
|
|
39
|
-
result.status as any as number,
|
|
40
|
-
result.headers,
|
|
41
|
-
result.data as string,
|
|
42
|
-
);
|
|
43
|
-
return result.data as Output;
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
export const propagate =
|
|
47
|
-
(props: IProps) =>
|
|
48
|
-
async <Input>(
|
|
49
|
-
connection: IConnection,
|
|
50
|
-
route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
|
|
51
|
-
input?: Input,
|
|
52
|
-
stringify?: (input: Input) => string,
|
|
53
|
-
): Promise<IPropagation<any, any>> =>
|
|
54
|
-
_Propagate("propagate")(props)(connection, route, input, stringify);
|
|
55
|
-
|
|
56
|
-
/** @internal */
|
|
57
|
-
const _Propagate =
|
|
58
|
-
(method: string) =>
|
|
59
|
-
(props: IProps) =>
|
|
60
|
-
async <Input>(
|
|
61
|
-
connection: IConnection,
|
|
62
|
-
route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
|
|
63
|
-
input?: Input,
|
|
64
|
-
stringify?: (input: Input) => string,
|
|
65
|
-
): Promise<IPropagation<any, any>> => {
|
|
66
|
-
//----
|
|
67
|
-
// REQUEST MESSAGE
|
|
68
|
-
//----
|
|
69
|
-
// METHOD & HEADERS
|
|
70
|
-
const headers: Record<string, IConnection.HeaderValue | undefined> = {
|
|
71
|
-
...(connection.headers ?? {}),
|
|
72
|
-
};
|
|
73
|
-
if (input !== undefined) {
|
|
74
|
-
if (route.request?.type === undefined)
|
|
75
|
-
throw new Error(
|
|
76
|
-
`Error on ${props.className}.fetch(): no content-type being configured.`,
|
|
77
|
-
);
|
|
78
|
-
else if (route.request.type !== "multipart/form-data")
|
|
79
|
-
headers["Content-Type"] = route.request.type;
|
|
80
|
-
} else if (input === undefined && headers["Content-Type"] !== undefined)
|
|
81
|
-
delete headers["Content-Type"];
|
|
82
|
-
|
|
83
|
-
// INIT REQUEST DATA
|
|
84
|
-
const init: RequestInit = {
|
|
85
|
-
...(connection.options ?? {}),
|
|
86
|
-
method: route.method,
|
|
87
|
-
headers: (() => {
|
|
88
|
-
const output: [string, string][] = [];
|
|
89
|
-
for (const [key, value] of Object.entries(headers))
|
|
90
|
-
if (value === undefined) continue;
|
|
91
|
-
else if (Array.isArray(value))
|
|
92
|
-
for (const v of value) output.push([key, String(v)]);
|
|
93
|
-
else output.push([key, String(value)]);
|
|
94
|
-
return output;
|
|
95
|
-
})(),
|
|
96
|
-
};
|
|
97
|
-
|
|
98
|
-
// CONSTRUCT BODY DATA
|
|
99
|
-
if (input !== undefined)
|
|
100
|
-
init.body = props.encode(
|
|
101
|
-
// BODY TRANSFORM
|
|
102
|
-
route.request?.type === "application/x-www-form-urlencoded"
|
|
103
|
-
? request_query_body(input)
|
|
104
|
-
: route.request?.type === "multipart/form-data"
|
|
105
|
-
? request_form_data_body(input as any)
|
|
106
|
-
: route.request?.type !== "text/plain"
|
|
107
|
-
? (stringify ?? JSON.stringify)(input)
|
|
108
|
-
: input,
|
|
109
|
-
headers,
|
|
110
|
-
);
|
|
111
|
-
|
|
112
|
-
//----
|
|
113
|
-
// RESPONSE MESSAGE
|
|
114
|
-
//----
|
|
115
|
-
// URL SPECIFICATION
|
|
116
|
-
const path: string =
|
|
117
|
-
connection.host[connection.host.length - 1] !== "/" &&
|
|
118
|
-
route.path[0] !== "/"
|
|
119
|
-
? `/${route.path}`
|
|
120
|
-
: route.path;
|
|
121
|
-
const url: URL = new URL(`${connection.host}${path}`);
|
|
122
|
-
|
|
123
|
-
// DO FETCH
|
|
124
|
-
const event: IFetchEvent = {
|
|
125
|
-
route,
|
|
126
|
-
path,
|
|
127
|
-
status: null,
|
|
128
|
-
input,
|
|
129
|
-
output: undefined,
|
|
130
|
-
started_at: new Date(),
|
|
131
|
-
respond_at: null,
|
|
132
|
-
completed_at: null!,
|
|
133
|
-
};
|
|
134
|
-
try {
|
|
135
|
-
// TRY FETCH
|
|
136
|
-
const response: Response = await (connection.fetch ?? fetch)(
|
|
137
|
-
url.href,
|
|
138
|
-
init,
|
|
139
|
-
);
|
|
140
|
-
event.respond_at = new Date();
|
|
141
|
-
event.status = response.status;
|
|
142
|
-
|
|
143
|
-
// CONSTRUCT RESULT DATA
|
|
144
|
-
const result: IPropagation<any, any> = {
|
|
145
|
-
success:
|
|
146
|
-
response.status === 200 ||
|
|
147
|
-
response.status === 201 ||
|
|
148
|
-
response.status === route.status,
|
|
149
|
-
status: response.status,
|
|
150
|
-
headers: response_headers_to_object(response.headers),
|
|
151
|
-
data: undefined!,
|
|
152
|
-
} as any;
|
|
153
|
-
if ((result as any).success === false) {
|
|
154
|
-
// WHEN FAILED
|
|
155
|
-
result.data = await response.text();
|
|
156
|
-
const type = response.headers.get("content-type");
|
|
157
|
-
if (
|
|
158
|
-
method !== "fetch" &&
|
|
159
|
-
type &&
|
|
160
|
-
type.indexOf("application/json") !== -1
|
|
161
|
-
)
|
|
162
|
-
try {
|
|
163
|
-
result.data = JSON.parse(result.data);
|
|
164
|
-
} catch {}
|
|
165
|
-
} else {
|
|
166
|
-
// WHEN SUCCESS
|
|
167
|
-
if (route.method === "HEAD") result.data = undefined!;
|
|
168
|
-
else if (route.response?.type === "application/json") {
|
|
169
|
-
const text: string = await response.text();
|
|
170
|
-
result.data = text.length ? JSON.parse(text) : undefined;
|
|
171
|
-
} else if (
|
|
172
|
-
route.response?.type === "application/x-www-form-urlencoded"
|
|
173
|
-
) {
|
|
174
|
-
const query: URLSearchParams = new URLSearchParams(
|
|
175
|
-
await response.text(),
|
|
176
|
-
);
|
|
177
|
-
result.data = route.parseQuery ? route.parseQuery(query) : query;
|
|
178
|
-
} else
|
|
179
|
-
result.data = props.decode(await response.text(), result.headers);
|
|
180
|
-
}
|
|
181
|
-
event.output = result.data;
|
|
182
|
-
return result;
|
|
183
|
-
} catch (exp) {
|
|
184
|
-
throw exp;
|
|
185
|
-
} finally {
|
|
186
|
-
event.completed_at = new Date();
|
|
187
|
-
if (connection.logger)
|
|
188
|
-
try {
|
|
189
|
-
await connection.logger(event);
|
|
190
|
-
} catch {}
|
|
191
|
-
}
|
|
192
|
-
};
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
/** @internal */
|
|
196
|
-
const request_query_body = (input: any): URLSearchParams => {
|
|
197
|
-
const q: URLSearchParams = new URLSearchParams();
|
|
198
|
-
for (const [key, value] of Object.entries(input))
|
|
199
|
-
if (value === undefined) continue;
|
|
200
|
-
else if (Array.isArray(value))
|
|
201
|
-
value.forEach((elem) => q.append(key, String(elem)));
|
|
202
|
-
else q.set(key, String(value));
|
|
203
|
-
return q;
|
|
204
|
-
};
|
|
205
|
-
|
|
206
|
-
/** @internal */
|
|
207
|
-
const request_form_data_body = (input: Record<string, any>): FormData => {
|
|
208
|
-
const encoded: FormData = new FormData();
|
|
209
|
-
const append = (key: string) => (value: any) => {
|
|
210
|
-
if (value === undefined) return;
|
|
211
|
-
else if (typeof File === "function" && value instanceof File)
|
|
212
|
-
encoded.append(key, value, value.name);
|
|
213
|
-
else encoded.append(key, value);
|
|
214
|
-
};
|
|
215
|
-
for (const [key, value] of Object.entries(input))
|
|
216
|
-
if (Array.isArray(value)) value.map(append(key));
|
|
217
|
-
else append(key)(value);
|
|
218
|
-
return encoded;
|
|
219
|
-
};
|
|
220
|
-
|
|
221
|
-
/** @internal */
|
|
222
|
-
const response_headers_to_object = (
|
|
223
|
-
headers: Headers,
|
|
224
|
-
): Record<string, string | string[]> => {
|
|
225
|
-
const output: Record<string, string | string[]> = {};
|
|
226
|
-
headers.forEach((value, key) => {
|
|
227
|
-
if (key === "set-cookie") {
|
|
228
|
-
output[key] ??= [];
|
|
229
|
-
(output[key] as string[]).push(
|
|
230
|
-
...value.split(";").map((str) => str.trim()),
|
|
231
|
-
);
|
|
232
|
-
} else output[key] = value;
|
|
233
|
-
});
|
|
234
|
-
return output;
|
|
235
|
-
};
|
|
1
|
+
import { HttpError } from "../HttpError";
|
|
2
|
+
import { IConnection } from "../IConnection";
|
|
3
|
+
import { IFetchEvent } from "../IFetchEvent";
|
|
4
|
+
import { IFetchRoute } from "../IFetchRoute";
|
|
5
|
+
import { IPropagation } from "../IPropagation";
|
|
6
|
+
|
|
7
|
+
/** @internal */
|
|
8
|
+
export namespace FetcherBase {
|
|
9
|
+
export interface IProps {
|
|
10
|
+
className: string;
|
|
11
|
+
encode: (
|
|
12
|
+
input: any,
|
|
13
|
+
headers: Record<string, IConnection.HeaderValue | undefined>,
|
|
14
|
+
) => string;
|
|
15
|
+
decode: (
|
|
16
|
+
input: string,
|
|
17
|
+
headers: Record<string, IConnection.HeaderValue | undefined>,
|
|
18
|
+
) => any;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export const request =
|
|
22
|
+
(props: IProps) =>
|
|
23
|
+
async <Input, Output>(
|
|
24
|
+
connection: IConnection,
|
|
25
|
+
route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
|
|
26
|
+
input?: Input,
|
|
27
|
+
stringify?: (input: Input) => string,
|
|
28
|
+
): Promise<Output> => {
|
|
29
|
+
const result = await _Propagate("fetch")(props)(
|
|
30
|
+
connection,
|
|
31
|
+
route,
|
|
32
|
+
input,
|
|
33
|
+
stringify,
|
|
34
|
+
);
|
|
35
|
+
if ((result as any).success === false)
|
|
36
|
+
throw new HttpError(
|
|
37
|
+
route.method,
|
|
38
|
+
route.path,
|
|
39
|
+
result.status as any as number,
|
|
40
|
+
result.headers,
|
|
41
|
+
result.data as string,
|
|
42
|
+
);
|
|
43
|
+
return result.data as Output;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export const propagate =
|
|
47
|
+
(props: IProps) =>
|
|
48
|
+
async <Input>(
|
|
49
|
+
connection: IConnection,
|
|
50
|
+
route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
|
|
51
|
+
input?: Input,
|
|
52
|
+
stringify?: (input: Input) => string,
|
|
53
|
+
): Promise<IPropagation<any, any>> =>
|
|
54
|
+
_Propagate("propagate")(props)(connection, route, input, stringify);
|
|
55
|
+
|
|
56
|
+
/** @internal */
|
|
57
|
+
const _Propagate =
|
|
58
|
+
(method: string) =>
|
|
59
|
+
(props: IProps) =>
|
|
60
|
+
async <Input>(
|
|
61
|
+
connection: IConnection,
|
|
62
|
+
route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
|
|
63
|
+
input?: Input,
|
|
64
|
+
stringify?: (input: Input) => string,
|
|
65
|
+
): Promise<IPropagation<any, any>> => {
|
|
66
|
+
//----
|
|
67
|
+
// REQUEST MESSAGE
|
|
68
|
+
//----
|
|
69
|
+
// METHOD & HEADERS
|
|
70
|
+
const headers: Record<string, IConnection.HeaderValue | undefined> = {
|
|
71
|
+
...(connection.headers ?? {}),
|
|
72
|
+
};
|
|
73
|
+
if (input !== undefined) {
|
|
74
|
+
if (route.request?.type === undefined)
|
|
75
|
+
throw new Error(
|
|
76
|
+
`Error on ${props.className}.fetch(): no content-type being configured.`,
|
|
77
|
+
);
|
|
78
|
+
else if (route.request.type !== "multipart/form-data")
|
|
79
|
+
headers["Content-Type"] = route.request.type;
|
|
80
|
+
} else if (input === undefined && headers["Content-Type"] !== undefined)
|
|
81
|
+
delete headers["Content-Type"];
|
|
82
|
+
|
|
83
|
+
// INIT REQUEST DATA
|
|
84
|
+
const init: RequestInit = {
|
|
85
|
+
...(connection.options ?? {}),
|
|
86
|
+
method: route.method,
|
|
87
|
+
headers: (() => {
|
|
88
|
+
const output: [string, string][] = [];
|
|
89
|
+
for (const [key, value] of Object.entries(headers))
|
|
90
|
+
if (value === undefined) continue;
|
|
91
|
+
else if (Array.isArray(value))
|
|
92
|
+
for (const v of value) output.push([key, String(v)]);
|
|
93
|
+
else output.push([key, String(value)]);
|
|
94
|
+
return output;
|
|
95
|
+
})(),
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
// CONSTRUCT BODY DATA
|
|
99
|
+
if (input !== undefined)
|
|
100
|
+
init.body = props.encode(
|
|
101
|
+
// BODY TRANSFORM
|
|
102
|
+
route.request?.type === "application/x-www-form-urlencoded"
|
|
103
|
+
? request_query_body(input)
|
|
104
|
+
: route.request?.type === "multipart/form-data"
|
|
105
|
+
? request_form_data_body(input as any)
|
|
106
|
+
: route.request?.type !== "text/plain"
|
|
107
|
+
? (stringify ?? JSON.stringify)(input)
|
|
108
|
+
: input,
|
|
109
|
+
headers,
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
//----
|
|
113
|
+
// RESPONSE MESSAGE
|
|
114
|
+
//----
|
|
115
|
+
// URL SPECIFICATION
|
|
116
|
+
const path: string =
|
|
117
|
+
connection.host[connection.host.length - 1] !== "/" &&
|
|
118
|
+
route.path[0] !== "/"
|
|
119
|
+
? `/${route.path}`
|
|
120
|
+
: route.path;
|
|
121
|
+
const url: URL = new URL(`${connection.host}${path}`);
|
|
122
|
+
|
|
123
|
+
// DO FETCH
|
|
124
|
+
const event: IFetchEvent = {
|
|
125
|
+
route,
|
|
126
|
+
path,
|
|
127
|
+
status: null,
|
|
128
|
+
input,
|
|
129
|
+
output: undefined,
|
|
130
|
+
started_at: new Date(),
|
|
131
|
+
respond_at: null,
|
|
132
|
+
completed_at: null!,
|
|
133
|
+
};
|
|
134
|
+
try {
|
|
135
|
+
// TRY FETCH
|
|
136
|
+
const response: Response = await (connection.fetch ?? fetch)(
|
|
137
|
+
url.href,
|
|
138
|
+
init,
|
|
139
|
+
);
|
|
140
|
+
event.respond_at = new Date();
|
|
141
|
+
event.status = response.status;
|
|
142
|
+
|
|
143
|
+
// CONSTRUCT RESULT DATA
|
|
144
|
+
const result: IPropagation<any, any> = {
|
|
145
|
+
success:
|
|
146
|
+
response.status === 200 ||
|
|
147
|
+
response.status === 201 ||
|
|
148
|
+
response.status === route.status,
|
|
149
|
+
status: response.status,
|
|
150
|
+
headers: response_headers_to_object(response.headers),
|
|
151
|
+
data: undefined!,
|
|
152
|
+
} as any;
|
|
153
|
+
if ((result as any).success === false) {
|
|
154
|
+
// WHEN FAILED
|
|
155
|
+
result.data = await response.text();
|
|
156
|
+
const type = response.headers.get("content-type");
|
|
157
|
+
if (
|
|
158
|
+
method !== "fetch" &&
|
|
159
|
+
type &&
|
|
160
|
+
type.indexOf("application/json") !== -1
|
|
161
|
+
)
|
|
162
|
+
try {
|
|
163
|
+
result.data = JSON.parse(result.data);
|
|
164
|
+
} catch {}
|
|
165
|
+
} else {
|
|
166
|
+
// WHEN SUCCESS
|
|
167
|
+
if (route.method === "HEAD") result.data = undefined!;
|
|
168
|
+
else if (route.response?.type === "application/json") {
|
|
169
|
+
const text: string = await response.text();
|
|
170
|
+
result.data = text.length ? JSON.parse(text) : undefined;
|
|
171
|
+
} else if (
|
|
172
|
+
route.response?.type === "application/x-www-form-urlencoded"
|
|
173
|
+
) {
|
|
174
|
+
const query: URLSearchParams = new URLSearchParams(
|
|
175
|
+
await response.text(),
|
|
176
|
+
);
|
|
177
|
+
result.data = route.parseQuery ? route.parseQuery(query) : query;
|
|
178
|
+
} else
|
|
179
|
+
result.data = props.decode(await response.text(), result.headers);
|
|
180
|
+
}
|
|
181
|
+
event.output = result.data;
|
|
182
|
+
return result;
|
|
183
|
+
} catch (exp) {
|
|
184
|
+
throw exp;
|
|
185
|
+
} finally {
|
|
186
|
+
event.completed_at = new Date();
|
|
187
|
+
if (connection.logger)
|
|
188
|
+
try {
|
|
189
|
+
await connection.logger(event);
|
|
190
|
+
} catch {}
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** @internal */
|
|
196
|
+
const request_query_body = (input: any): URLSearchParams => {
|
|
197
|
+
const q: URLSearchParams = new URLSearchParams();
|
|
198
|
+
for (const [key, value] of Object.entries(input))
|
|
199
|
+
if (value === undefined) continue;
|
|
200
|
+
else if (Array.isArray(value))
|
|
201
|
+
value.forEach((elem) => q.append(key, String(elem)));
|
|
202
|
+
else q.set(key, String(value));
|
|
203
|
+
return q;
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
/** @internal */
|
|
207
|
+
const request_form_data_body = (input: Record<string, any>): FormData => {
|
|
208
|
+
const encoded: FormData = new FormData();
|
|
209
|
+
const append = (key: string) => (value: any) => {
|
|
210
|
+
if (value === undefined) return;
|
|
211
|
+
else if (typeof File === "function" && value instanceof File)
|
|
212
|
+
encoded.append(key, value, value.name);
|
|
213
|
+
else encoded.append(key, value);
|
|
214
|
+
};
|
|
215
|
+
for (const [key, value] of Object.entries(input))
|
|
216
|
+
if (Array.isArray(value)) value.map(append(key));
|
|
217
|
+
else append(key)(value);
|
|
218
|
+
return encoded;
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
/** @internal */
|
|
222
|
+
const response_headers_to_object = (
|
|
223
|
+
headers: Headers,
|
|
224
|
+
): Record<string, string | string[]> => {
|
|
225
|
+
const output: Record<string, string | string[]> = {};
|
|
226
|
+
headers.forEach((value, key) => {
|
|
227
|
+
if (key === "set-cookie") {
|
|
228
|
+
output[key] ??= [];
|
|
229
|
+
(output[key] as string[]).push(
|
|
230
|
+
...value.split(";").map((str) => str.trim()),
|
|
231
|
+
);
|
|
232
|
+
} else output[key] = value;
|
|
233
|
+
});
|
|
234
|
+
return output;
|
|
235
|
+
};
|