@nestia/fetcher 10.0.1 → 11.0.0-dev.20260305
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/LICENSE +21 -0
- package/README.md +93 -93
- package/package.json +20 -13
- package/src/AesPkcs5.ts +41 -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/NestiaSimulator.ts +82 -82
- package/src/PlainFetcher.ts +105 -105
- package/src/index.ts +12 -7
- package/src/internal/FetcherBase.ts +235 -235
- package/lib/AesPkcs5.d.ts +0 -30
- package/lib/AesPkcs5.js +0 -49
- package/lib/AesPkcs5.js.map +0 -1
- package/lib/EncryptedFetcher.d.ts +0 -47
- package/lib/EncryptedFetcher.js +0 -139
- package/lib/EncryptedFetcher.js.map +0 -1
- package/lib/FormDataInput.d.ts +0 -70
- package/lib/FormDataInput.js +0 -3
- package/lib/FormDataInput.js.map +0 -1
- package/lib/HttpError.d.ts +0 -1
- package/lib/HttpError.js +0 -6
- package/lib/HttpError.js.map +0 -1
- package/lib/IConnection.d.ts +0 -165
- package/lib/IConnection.js +0 -3
- package/lib/IConnection.js.map +0 -1
- package/lib/IEncryptionPassword.d.ts +0 -41
- package/lib/IEncryptionPassword.js +0 -3
- package/lib/IEncryptionPassword.js.map +0 -1
- package/lib/IFetchEvent.d.ts +0 -11
- package/lib/IFetchEvent.js +0 -21
- package/lib/IFetchEvent.js.map +0 -1
- package/lib/IFetchRoute.d.ts +0 -46
- package/lib/IFetchRoute.js +0 -3
- package/lib/IFetchRoute.js.map +0 -1
- package/lib/IPropagation.d.ts +0 -69
- package/lib/IPropagation.js +0 -3
- package/lib/IPropagation.js.map +0 -1
- package/lib/MigrateFetcher.d.ts +0 -19
- package/lib/MigrateFetcher.js +0 -179
- package/lib/MigrateFetcher.js.map +0 -1
- package/lib/NestiaSimulator.d.ts +0 -13
- package/lib/NestiaSimulator.js +0 -62
- package/lib/NestiaSimulator.js.map +0 -1
- package/lib/PlainFetcher.d.ts +0 -46
- package/lib/PlainFetcher.js +0 -89
- package/lib/PlainFetcher.js.map +0 -1
- package/lib/index.d.ts +0 -7
- package/lib/index.js +0 -24
- package/lib/index.js.map +0 -1
- package/lib/internal/FetcherBase.d.ts +0 -1
- package/lib/internal/FetcherBase.js +0 -348
- package/lib/internal/FetcherBase.js.map +0 -1
- package/src/MigrateFetcher.ts +0 -118
|
@@ -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
|
+
};
|
package/lib/AesPkcs5.d.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Utility class for the AES-128/256 encryption.
|
|
3
|
-
*
|
|
4
|
-
* - AES-128/256
|
|
5
|
-
* - CBC mode
|
|
6
|
-
* - PKCS#5 Padding
|
|
7
|
-
* - Base64 Encoding
|
|
8
|
-
*
|
|
9
|
-
* @author Jeongho Nam - https://github.com/samchon
|
|
10
|
-
*/
|
|
11
|
-
export declare namespace AesPkcs5 {
|
|
12
|
-
/**
|
|
13
|
-
* Encrypt data
|
|
14
|
-
*
|
|
15
|
-
* @param data Target data
|
|
16
|
-
* @param key Key value of the encryption.
|
|
17
|
-
* @param iv Initializer Vector for the encryption
|
|
18
|
-
* @returns Encrypted data
|
|
19
|
-
*/
|
|
20
|
-
function encrypt(data: string, key: string, iv: string): string;
|
|
21
|
-
/**
|
|
22
|
-
* Decrypt data.
|
|
23
|
-
*
|
|
24
|
-
* @param data Target data
|
|
25
|
-
* @param key Key value of the decryption.
|
|
26
|
-
* @param iv Initializer Vector for the decryption
|
|
27
|
-
* @returns Decrypted data.
|
|
28
|
-
*/
|
|
29
|
-
function decrypt(data: string, key: string, iv: string): string;
|
|
30
|
-
}
|
package/lib/AesPkcs5.js
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.AesPkcs5 = void 0;
|
|
7
|
-
var crypto_1 = __importDefault(require("crypto"));
|
|
8
|
-
/**
|
|
9
|
-
* Utility class for the AES-128/256 encryption.
|
|
10
|
-
*
|
|
11
|
-
* - AES-128/256
|
|
12
|
-
* - CBC mode
|
|
13
|
-
* - PKCS#5 Padding
|
|
14
|
-
* - Base64 Encoding
|
|
15
|
-
*
|
|
16
|
-
* @author Jeongho Nam - https://github.com/samchon
|
|
17
|
-
*/
|
|
18
|
-
var AesPkcs5;
|
|
19
|
-
(function (AesPkcs5) {
|
|
20
|
-
/**
|
|
21
|
-
* Encrypt data
|
|
22
|
-
*
|
|
23
|
-
* @param data Target data
|
|
24
|
-
* @param key Key value of the encryption.
|
|
25
|
-
* @param iv Initializer Vector for the encryption
|
|
26
|
-
* @returns Encrypted data
|
|
27
|
-
*/
|
|
28
|
-
function encrypt(data, key, iv) {
|
|
29
|
-
var bytes = key.length * 8;
|
|
30
|
-
var cipher = crypto_1.default.createCipheriv("AES-".concat(bytes, "-CBC"), key, iv);
|
|
31
|
-
return cipher.update(data, "utf8", "base64") + cipher.final("base64");
|
|
32
|
-
}
|
|
33
|
-
AesPkcs5.encrypt = encrypt;
|
|
34
|
-
/**
|
|
35
|
-
* Decrypt data.
|
|
36
|
-
*
|
|
37
|
-
* @param data Target data
|
|
38
|
-
* @param key Key value of the decryption.
|
|
39
|
-
* @param iv Initializer Vector for the decryption
|
|
40
|
-
* @returns Decrypted data.
|
|
41
|
-
*/
|
|
42
|
-
function decrypt(data, key, iv) {
|
|
43
|
-
var bytes = key.length * 8;
|
|
44
|
-
var decipher = crypto_1.default.createDecipheriv("AES-".concat(bytes, "-CBC"), key, iv);
|
|
45
|
-
return decipher.update(data, "base64", "utf8") + decipher.final("utf8");
|
|
46
|
-
}
|
|
47
|
-
AesPkcs5.decrypt = decrypt;
|
|
48
|
-
})(AesPkcs5 || (exports.AesPkcs5 = AesPkcs5 = {}));
|
|
49
|
-
//# sourceMappingURL=AesPkcs5.js.map
|
package/lib/AesPkcs5.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"AesPkcs5.js","sourceRoot":"","sources":["../src/AesPkcs5.ts"],"names":[],"mappings":";;;;;;AAAA,kDAA4B;AAE5B;;;;;;;;;GASG;AACH,IAAiB,QAAQ,CAoCxB;AApCD,WAAiB,QAAQ;IACvB;;;;;;;OAOG;IACH,SAAgB,OAAO,CAAC,IAAY,EAAE,GAAW,EAAE,EAAU;QAC3D,IAAM,KAAK,GAAW,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC;QACrC,IAAM,MAAM,GAAkB,gBAAM,CAAC,cAAc,CACjD,cAAO,KAAK,SAAM,EAClB,GAAG,EACH,EAAE,CACH,CAAC;QACF,OAAO,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,CAAC,GAAG,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;IACxE,CAAC;IARe,gBAAO,UAQtB,CAAA;IAED;;;;;;;OAOG;IACH,SAAgB,OAAO,CAAC,IAAY,EAAE,GAAW,EAAE,EAAU;QAC3D,IAAM,KAAK,GAAW,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC;QACrC,IAAM,QAAQ,GAAoB,gBAAM,CAAC,gBAAgB,CACvD,cAAO,KAAK,SAAM,EAClB,GAAG,EACH,EAAE,CACH,CAAC;QACF,OAAO,QAAQ,CAAC,MAAM,CAAC,IAAI,EAAE,QAAQ,EAAE,MAAM,CAAC,GAAG,QAAQ,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;IAC1E,CAAC;IARe,gBAAO,UAQtB,CAAA;AACH,CAAC,EApCgB,QAAQ,wBAAR,QAAQ,QAoCxB"}
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { IConnection } from "./IConnection";
|
|
2
|
-
import { IFetchRoute } from "./IFetchRoute";
|
|
3
|
-
import { IPropagation } from "./IPropagation";
|
|
4
|
-
/**
|
|
5
|
-
* Utility class for `fetch` functions used in `@nestia/sdk` with encryption.
|
|
6
|
-
*
|
|
7
|
-
* `EncryptedFetcher` is a utility class designed for SDK functions generated by
|
|
8
|
-
* [`@nestia/sdk`](https://nestia.io/docs/sdk/sdk), interacting with the remote
|
|
9
|
-
* HTTP API encrypted by AES-PKCS algorithm. In other words, this is a
|
|
10
|
-
* collection of dedicated `fetch()` functions for `@nestia/sdk` with
|
|
11
|
-
* encryption.
|
|
12
|
-
*
|
|
13
|
-
* For reference, `EncryptedFetcher` class being used only when target
|
|
14
|
-
* controller method is encrypting body data by `@EncryptedRoute` or
|
|
15
|
-
* `@EncryptedBody` decorators. If those decorators are not used,
|
|
16
|
-
* {@link PlainFetcher} class would be used instead.
|
|
17
|
-
*
|
|
18
|
-
* @author Jeongho Nam - https://github.com/samchon
|
|
19
|
-
*/
|
|
20
|
-
export declare namespace EncryptedFetcher {
|
|
21
|
-
/**
|
|
22
|
-
* Fetch function only for `HEAD` method.
|
|
23
|
-
*
|
|
24
|
-
* @param connection Connection information for the remote HTTP server
|
|
25
|
-
* @param route Route information about the target API
|
|
26
|
-
* @returns Nothing because of `HEAD` method
|
|
27
|
-
*/
|
|
28
|
-
function fetch(connection: IConnection, route: IFetchRoute<"HEAD">): Promise<void>;
|
|
29
|
-
/**
|
|
30
|
-
* Fetch function only for `GET` method.
|
|
31
|
-
*
|
|
32
|
-
* @param connection Connection information for the remote HTTP server
|
|
33
|
-
* @param route Route information about the target API
|
|
34
|
-
* @returns Response body data from the remote API
|
|
35
|
-
*/
|
|
36
|
-
function fetch<Output>(connection: IConnection, route: IFetchRoute<"GET">): Promise<Output>;
|
|
37
|
-
/**
|
|
38
|
-
* Fetch function for the `POST`, `PUT`, `PATCH` and `DELETE` methods.
|
|
39
|
-
*
|
|
40
|
-
* @param connection Connection information for the remote HTTP server
|
|
41
|
-
* @param route Route information about the target API
|
|
42
|
-
* @returns Response body data from the remote API
|
|
43
|
-
*/
|
|
44
|
-
function fetch<Input, Output>(connection: IConnection, route: IFetchRoute<"POST" | "PUT" | "PATCH" | "DELETE">, input?: Input, stringify?: (input: Input) => string): Promise<Output>;
|
|
45
|
-
function propagate<Output extends IPropagation<any, any>>(connection: IConnection, route: IFetchRoute<"GET" | "HEAD">): Promise<Output>;
|
|
46
|
-
function propagate<Input, Output extends IPropagation<any, any>>(connection: IConnection, route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">, input?: Input, stringify?: (input: Input) => string): Promise<Output>;
|
|
47
|
-
}
|