@nestia/fetcher 2.5.0-dev.20240130-7 → 2.5.0-dev.20240131
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/package.json +1 -1
- package/src/AesPkcs5.ts +50 -50
- package/src/EncryptedFetcher.ts +175 -175
- package/src/HttpError.ts +85 -85
- package/src/IEncryptionPassword.ts +50 -50
- package/src/IRandomGenerator.ts +38 -38
- package/src/PlainFetcher.ts +106 -106
- package/src/index.ts +7 -7
- package/src/internal/IFetchRoute.ts +61 -61
- package/src/internal/Singleton.ts +20 -20
package/package.json
CHANGED
package/src/AesPkcs5.ts
CHANGED
|
@@ -1,50 +1,50 @@
|
|
|
1
|
-
import crypto from "crypto";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Utility class for the AES-128/256 encryption.
|
|
5
|
-
*
|
|
6
|
-
* - AES-128/256
|
|
7
|
-
* - CBC mode
|
|
8
|
-
* - PKCS#5 Padding
|
|
9
|
-
* - Base64 Encoding
|
|
10
|
-
*
|
|
11
|
-
* @author Jeongho Nam - https://github.com/samchon
|
|
12
|
-
*/
|
|
13
|
-
export namespace AesPkcs5 {
|
|
14
|
-
/**
|
|
15
|
-
* Encrypt data
|
|
16
|
-
*
|
|
17
|
-
* @param data Target data
|
|
18
|
-
* @param key Key value of the encryption.
|
|
19
|
-
* @param iv Initializer Vector for the encryption
|
|
20
|
-
* @return Encrypted data
|
|
21
|
-
*/
|
|
22
|
-
export function encrypt(data: string, key: string, iv: string): string {
|
|
23
|
-
const bytes: number = key.length * 8;
|
|
24
|
-
const cipher: crypto.Cipher = crypto.createCipheriv(
|
|
25
|
-
`AES-${bytes}-CBC`,
|
|
26
|
-
key,
|
|
27
|
-
iv,
|
|
28
|
-
);
|
|
29
|
-
return cipher.update(data, "utf8", "base64") + cipher.final("base64");
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Decrypt data.
|
|
34
|
-
*
|
|
35
|
-
* @param data Target data
|
|
36
|
-
* @param key Key value of the decryption.
|
|
37
|
-
* @param iv Initializer Vector for the decryption
|
|
38
|
-
* @return Decrypted data.
|
|
39
|
-
*/
|
|
40
|
-
export function decrypt(data: string, key: string, iv: string): string {
|
|
41
|
-
const bytes: number = key.length * 8;
|
|
42
|
-
const decipher: crypto.Decipher = crypto.createDecipheriv(
|
|
43
|
-
`AES-${bytes}-CBC`,
|
|
44
|
-
key,
|
|
45
|
-
iv,
|
|
46
|
-
);
|
|
47
|
-
|
|
48
|
-
return decipher.update(data, "base64", "utf8") + decipher.final("utf8");
|
|
49
|
-
}
|
|
50
|
-
}
|
|
1
|
+
import crypto from "crypto";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Utility class for the AES-128/256 encryption.
|
|
5
|
+
*
|
|
6
|
+
* - AES-128/256
|
|
7
|
+
* - CBC mode
|
|
8
|
+
* - PKCS#5 Padding
|
|
9
|
+
* - Base64 Encoding
|
|
10
|
+
*
|
|
11
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
12
|
+
*/
|
|
13
|
+
export namespace AesPkcs5 {
|
|
14
|
+
/**
|
|
15
|
+
* Encrypt data
|
|
16
|
+
*
|
|
17
|
+
* @param data Target data
|
|
18
|
+
* @param key Key value of the encryption.
|
|
19
|
+
* @param iv Initializer Vector for the encryption
|
|
20
|
+
* @return Encrypted data
|
|
21
|
+
*/
|
|
22
|
+
export function encrypt(data: string, key: string, iv: string): string {
|
|
23
|
+
const bytes: number = key.length * 8;
|
|
24
|
+
const cipher: crypto.Cipher = crypto.createCipheriv(
|
|
25
|
+
`AES-${bytes}-CBC`,
|
|
26
|
+
key,
|
|
27
|
+
iv,
|
|
28
|
+
);
|
|
29
|
+
return cipher.update(data, "utf8", "base64") + cipher.final("base64");
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Decrypt data.
|
|
34
|
+
*
|
|
35
|
+
* @param data Target data
|
|
36
|
+
* @param key Key value of the decryption.
|
|
37
|
+
* @param iv Initializer Vector for the decryption
|
|
38
|
+
* @return Decrypted data.
|
|
39
|
+
*/
|
|
40
|
+
export function decrypt(data: string, key: string, iv: string): string {
|
|
41
|
+
const bytes: number = key.length * 8;
|
|
42
|
+
const decipher: crypto.Decipher = crypto.createDecipheriv(
|
|
43
|
+
`AES-${bytes}-CBC`,
|
|
44
|
+
key,
|
|
45
|
+
iv,
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
return decipher.update(data, "base64", "utf8") + decipher.final("utf8");
|
|
49
|
+
}
|
|
50
|
+
}
|
package/src/EncryptedFetcher.ts
CHANGED
|
@@ -1,175 +1,175 @@
|
|
|
1
|
-
import { AesPkcs5 } from "./AesPkcs5";
|
|
2
|
-
import { IConnection } from "./IConnection";
|
|
3
|
-
import { IEncryptionPassword } from "./IEncryptionPassword";
|
|
4
|
-
import { IPropagation } from "./IPropagation";
|
|
5
|
-
import { Primitive } from "./Primitive";
|
|
6
|
-
import { FetcherBase } from "./internal/FetcherBase";
|
|
7
|
-
import { IFetchRoute } from "./internal/IFetchRoute";
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Utility class for `fetch` functions used in `@nestia/sdk` with encryption.
|
|
11
|
-
*
|
|
12
|
-
* `EncryptedFetcher` is a utility class designed for SDK functions generated by
|
|
13
|
-
* [`@nestia/sdk`](https://nestia.io/docs/sdk/sdk), interacting with the remote
|
|
14
|
-
* HTTP API encrypted by AES-PKCS algorithm. In other words, this is a collection of
|
|
15
|
-
* dedicated `fetch()` functions for `@nestia/sdk` with encryption.
|
|
16
|
-
*
|
|
17
|
-
* For reference, `EncryptedFetcher` class being used only when target controller
|
|
18
|
-
* method is encrypting body data by `@EncryptedRoute` or `@EncryptedBody` decorators.
|
|
19
|
-
* If those decorators are not used, {@link PlainFetcher} class would be used instead.
|
|
20
|
-
*
|
|
21
|
-
* @author Jeongho Nam - https://github.com/samchon
|
|
22
|
-
*/
|
|
23
|
-
export namespace EncryptedFetcher {
|
|
24
|
-
/**
|
|
25
|
-
* Fetch function only for `HEAD` method.
|
|
26
|
-
*
|
|
27
|
-
* @param connection Connection information for the remote HTTP server
|
|
28
|
-
* @param route Route information about the target API
|
|
29
|
-
* @return Nothing because of `HEAD` method
|
|
30
|
-
*/
|
|
31
|
-
export function fetch(
|
|
32
|
-
connection: IConnection,
|
|
33
|
-
route: IFetchRoute<"HEAD">,
|
|
34
|
-
): Promise<void>;
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Fetch function only for `GET` method.
|
|
38
|
-
*
|
|
39
|
-
* @param connection Connection information for the remote HTTP server
|
|
40
|
-
* @param route Route information about the target API
|
|
41
|
-
* @return Response body data from the remote API
|
|
42
|
-
*/
|
|
43
|
-
export function fetch<Output>(
|
|
44
|
-
connection: IConnection,
|
|
45
|
-
route: IFetchRoute<"GET">,
|
|
46
|
-
): Promise<Primitive<Output>>;
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Fetch function for the `POST`, `PUT`, `PATCH` and `DELETE` methods.
|
|
50
|
-
*
|
|
51
|
-
* @param connection Connection information for the remote HTTP server
|
|
52
|
-
* @param route Route information about the target API
|
|
53
|
-
* @return Response body data from the remote API
|
|
54
|
-
*/
|
|
55
|
-
export function fetch<Input, Output>(
|
|
56
|
-
connection: IConnection,
|
|
57
|
-
route: IFetchRoute<"POST" | "PUT" | "PATCH" | "DELETE">,
|
|
58
|
-
input?: Input,
|
|
59
|
-
stringify?: (input: Input) => string,
|
|
60
|
-
): Promise<Primitive<Output>>;
|
|
61
|
-
|
|
62
|
-
export async function fetch<Input, Output>(
|
|
63
|
-
connection: IConnection,
|
|
64
|
-
route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
|
|
65
|
-
input?: Input,
|
|
66
|
-
stringify?: (input: Input) => string,
|
|
67
|
-
): Promise<Primitive<Output>> {
|
|
68
|
-
if (
|
|
69
|
-
(route.request?.encrypted === true || route.response?.encrypted) &&
|
|
70
|
-
connection.encryption === undefined
|
|
71
|
-
)
|
|
72
|
-
throw new Error(
|
|
73
|
-
"Error on EncryptedFetcher.fetch(): the encryption password has not been configured.",
|
|
74
|
-
);
|
|
75
|
-
const closure =
|
|
76
|
-
typeof connection.encryption === "function"
|
|
77
|
-
? (direction: "encode" | "decode") =>
|
|
78
|
-
(
|
|
79
|
-
headers: Record<string, IConnection.HeaderValue | undefined>,
|
|
80
|
-
body: string,
|
|
81
|
-
) =>
|
|
82
|
-
(connection.encryption as IEncryptionPassword.Closure)({
|
|
83
|
-
headers,
|
|
84
|
-
body,
|
|
85
|
-
direction,
|
|
86
|
-
})
|
|
87
|
-
: () => () => connection.encryption as IEncryptionPassword;
|
|
88
|
-
|
|
89
|
-
return FetcherBase.fetch({
|
|
90
|
-
className: "EncryptedFetcher",
|
|
91
|
-
encode:
|
|
92
|
-
route.request?.encrypted === true
|
|
93
|
-
? (input, headers) => {
|
|
94
|
-
const p: IEncryptionPassword = closure("encode")(headers, input);
|
|
95
|
-
return AesPkcs5.encrypt(
|
|
96
|
-
(stringify ?? JSON.stringify)(input),
|
|
97
|
-
p.key,
|
|
98
|
-
p.iv,
|
|
99
|
-
);
|
|
100
|
-
}
|
|
101
|
-
: (input) => input,
|
|
102
|
-
decode:
|
|
103
|
-
route.response?.encrypted === true
|
|
104
|
-
? (input, headers) => {
|
|
105
|
-
const p: IEncryptionPassword = closure("decode")(headers, input);
|
|
106
|
-
const s: string = AesPkcs5.decrypt(input, p.key, p.iv);
|
|
107
|
-
return s.length ? JSON.parse(s) : s;
|
|
108
|
-
}
|
|
109
|
-
: (input) => input,
|
|
110
|
-
})(connection, route, input, stringify);
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
export function propagate<Output extends IPropagation<any, any>>(
|
|
114
|
-
connection: IConnection,
|
|
115
|
-
route: IFetchRoute<"GET" | "HEAD">,
|
|
116
|
-
): Promise<Output>;
|
|
117
|
-
|
|
118
|
-
export function propagate<Input, Output extends IPropagation<any, any>>(
|
|
119
|
-
connection: IConnection,
|
|
120
|
-
route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
|
|
121
|
-
input?: Input,
|
|
122
|
-
stringify?: (input: Input) => string,
|
|
123
|
-
): Promise<Output>;
|
|
124
|
-
|
|
125
|
-
export async function propagate<Input, Output extends IPropagation<any, any>>(
|
|
126
|
-
connection: IConnection,
|
|
127
|
-
route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
|
|
128
|
-
input?: Input,
|
|
129
|
-
stringify?: (input: Input) => string,
|
|
130
|
-
): Promise<Output> {
|
|
131
|
-
if (
|
|
132
|
-
(route.request?.encrypted === true || route.response?.encrypted) &&
|
|
133
|
-
connection.encryption === undefined
|
|
134
|
-
)
|
|
135
|
-
throw new Error(
|
|
136
|
-
"Error on EncryptedFetcher.propagate(): the encryption password has not been configured.",
|
|
137
|
-
);
|
|
138
|
-
const closure =
|
|
139
|
-
typeof connection.encryption === "function"
|
|
140
|
-
? (direction: "encode" | "decode") =>
|
|
141
|
-
(
|
|
142
|
-
headers: Record<string, IConnection.HeaderValue | undefined>,
|
|
143
|
-
body: string,
|
|
144
|
-
) =>
|
|
145
|
-
(connection.encryption as IEncryptionPassword.Closure)({
|
|
146
|
-
headers,
|
|
147
|
-
body,
|
|
148
|
-
direction,
|
|
149
|
-
})
|
|
150
|
-
: () => () => connection.encryption as IEncryptionPassword;
|
|
151
|
-
|
|
152
|
-
return FetcherBase.propagate({
|
|
153
|
-
className: "EncryptedFetcher",
|
|
154
|
-
encode:
|
|
155
|
-
route.request?.encrypted === true
|
|
156
|
-
? (input, headers) => {
|
|
157
|
-
const p: IEncryptionPassword = closure("encode")(headers, input);
|
|
158
|
-
return AesPkcs5.encrypt(
|
|
159
|
-
(stringify ?? JSON.stringify)(input),
|
|
160
|
-
p.key,
|
|
161
|
-
p.iv,
|
|
162
|
-
);
|
|
163
|
-
}
|
|
164
|
-
: (input) => input,
|
|
165
|
-
decode:
|
|
166
|
-
route.response?.encrypted === true
|
|
167
|
-
? (input, headers) => {
|
|
168
|
-
const p: IEncryptionPassword = closure("decode")(headers, input);
|
|
169
|
-
const s: string = AesPkcs5.decrypt(input, p.key, p.iv);
|
|
170
|
-
return s.length ? JSON.parse(s) : s;
|
|
171
|
-
}
|
|
172
|
-
: (input) => input,
|
|
173
|
-
})(connection, route, input, stringify) as Promise<Output>;
|
|
174
|
-
}
|
|
175
|
-
}
|
|
1
|
+
import { AesPkcs5 } from "./AesPkcs5";
|
|
2
|
+
import { IConnection } from "./IConnection";
|
|
3
|
+
import { IEncryptionPassword } from "./IEncryptionPassword";
|
|
4
|
+
import { IPropagation } from "./IPropagation";
|
|
5
|
+
import { Primitive } from "./Primitive";
|
|
6
|
+
import { FetcherBase } from "./internal/FetcherBase";
|
|
7
|
+
import { IFetchRoute } from "./internal/IFetchRoute";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Utility class for `fetch` functions used in `@nestia/sdk` with encryption.
|
|
11
|
+
*
|
|
12
|
+
* `EncryptedFetcher` is a utility class designed for SDK functions generated by
|
|
13
|
+
* [`@nestia/sdk`](https://nestia.io/docs/sdk/sdk), interacting with the remote
|
|
14
|
+
* HTTP API encrypted by AES-PKCS algorithm. In other words, this is a collection of
|
|
15
|
+
* dedicated `fetch()` functions for `@nestia/sdk` with encryption.
|
|
16
|
+
*
|
|
17
|
+
* For reference, `EncryptedFetcher` class being used only when target controller
|
|
18
|
+
* method is encrypting body data by `@EncryptedRoute` or `@EncryptedBody` decorators.
|
|
19
|
+
* If those decorators are not used, {@link PlainFetcher} class would be used instead.
|
|
20
|
+
*
|
|
21
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
22
|
+
*/
|
|
23
|
+
export namespace EncryptedFetcher {
|
|
24
|
+
/**
|
|
25
|
+
* Fetch function only for `HEAD` method.
|
|
26
|
+
*
|
|
27
|
+
* @param connection Connection information for the remote HTTP server
|
|
28
|
+
* @param route Route information about the target API
|
|
29
|
+
* @return Nothing because of `HEAD` method
|
|
30
|
+
*/
|
|
31
|
+
export function fetch(
|
|
32
|
+
connection: IConnection,
|
|
33
|
+
route: IFetchRoute<"HEAD">,
|
|
34
|
+
): Promise<void>;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Fetch function only for `GET` method.
|
|
38
|
+
*
|
|
39
|
+
* @param connection Connection information for the remote HTTP server
|
|
40
|
+
* @param route Route information about the target API
|
|
41
|
+
* @return Response body data from the remote API
|
|
42
|
+
*/
|
|
43
|
+
export function fetch<Output>(
|
|
44
|
+
connection: IConnection,
|
|
45
|
+
route: IFetchRoute<"GET">,
|
|
46
|
+
): Promise<Primitive<Output>>;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Fetch function for the `POST`, `PUT`, `PATCH` and `DELETE` methods.
|
|
50
|
+
*
|
|
51
|
+
* @param connection Connection information for the remote HTTP server
|
|
52
|
+
* @param route Route information about the target API
|
|
53
|
+
* @return Response body data from the remote API
|
|
54
|
+
*/
|
|
55
|
+
export function fetch<Input, Output>(
|
|
56
|
+
connection: IConnection,
|
|
57
|
+
route: IFetchRoute<"POST" | "PUT" | "PATCH" | "DELETE">,
|
|
58
|
+
input?: Input,
|
|
59
|
+
stringify?: (input: Input) => string,
|
|
60
|
+
): Promise<Primitive<Output>>;
|
|
61
|
+
|
|
62
|
+
export async function fetch<Input, Output>(
|
|
63
|
+
connection: IConnection,
|
|
64
|
+
route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
|
|
65
|
+
input?: Input,
|
|
66
|
+
stringify?: (input: Input) => string,
|
|
67
|
+
): Promise<Primitive<Output>> {
|
|
68
|
+
if (
|
|
69
|
+
(route.request?.encrypted === true || route.response?.encrypted) &&
|
|
70
|
+
connection.encryption === undefined
|
|
71
|
+
)
|
|
72
|
+
throw new Error(
|
|
73
|
+
"Error on EncryptedFetcher.fetch(): the encryption password has not been configured.",
|
|
74
|
+
);
|
|
75
|
+
const closure =
|
|
76
|
+
typeof connection.encryption === "function"
|
|
77
|
+
? (direction: "encode" | "decode") =>
|
|
78
|
+
(
|
|
79
|
+
headers: Record<string, IConnection.HeaderValue | undefined>,
|
|
80
|
+
body: string,
|
|
81
|
+
) =>
|
|
82
|
+
(connection.encryption as IEncryptionPassword.Closure)({
|
|
83
|
+
headers,
|
|
84
|
+
body,
|
|
85
|
+
direction,
|
|
86
|
+
})
|
|
87
|
+
: () => () => connection.encryption as IEncryptionPassword;
|
|
88
|
+
|
|
89
|
+
return FetcherBase.fetch({
|
|
90
|
+
className: "EncryptedFetcher",
|
|
91
|
+
encode:
|
|
92
|
+
route.request?.encrypted === true
|
|
93
|
+
? (input, headers) => {
|
|
94
|
+
const p: IEncryptionPassword = closure("encode")(headers, input);
|
|
95
|
+
return AesPkcs5.encrypt(
|
|
96
|
+
(stringify ?? JSON.stringify)(input),
|
|
97
|
+
p.key,
|
|
98
|
+
p.iv,
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
: (input) => input,
|
|
102
|
+
decode:
|
|
103
|
+
route.response?.encrypted === true
|
|
104
|
+
? (input, headers) => {
|
|
105
|
+
const p: IEncryptionPassword = closure("decode")(headers, input);
|
|
106
|
+
const s: string = AesPkcs5.decrypt(input, p.key, p.iv);
|
|
107
|
+
return s.length ? JSON.parse(s) : s;
|
|
108
|
+
}
|
|
109
|
+
: (input) => input,
|
|
110
|
+
})(connection, route, input, stringify);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function propagate<Output extends IPropagation<any, any>>(
|
|
114
|
+
connection: IConnection,
|
|
115
|
+
route: IFetchRoute<"GET" | "HEAD">,
|
|
116
|
+
): Promise<Output>;
|
|
117
|
+
|
|
118
|
+
export function propagate<Input, Output extends IPropagation<any, any>>(
|
|
119
|
+
connection: IConnection,
|
|
120
|
+
route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
|
|
121
|
+
input?: Input,
|
|
122
|
+
stringify?: (input: Input) => string,
|
|
123
|
+
): Promise<Output>;
|
|
124
|
+
|
|
125
|
+
export async function propagate<Input, Output extends IPropagation<any, any>>(
|
|
126
|
+
connection: IConnection,
|
|
127
|
+
route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
|
|
128
|
+
input?: Input,
|
|
129
|
+
stringify?: (input: Input) => string,
|
|
130
|
+
): Promise<Output> {
|
|
131
|
+
if (
|
|
132
|
+
(route.request?.encrypted === true || route.response?.encrypted) &&
|
|
133
|
+
connection.encryption === undefined
|
|
134
|
+
)
|
|
135
|
+
throw new Error(
|
|
136
|
+
"Error on EncryptedFetcher.propagate(): the encryption password has not been configured.",
|
|
137
|
+
);
|
|
138
|
+
const closure =
|
|
139
|
+
typeof connection.encryption === "function"
|
|
140
|
+
? (direction: "encode" | "decode") =>
|
|
141
|
+
(
|
|
142
|
+
headers: Record<string, IConnection.HeaderValue | undefined>,
|
|
143
|
+
body: string,
|
|
144
|
+
) =>
|
|
145
|
+
(connection.encryption as IEncryptionPassword.Closure)({
|
|
146
|
+
headers,
|
|
147
|
+
body,
|
|
148
|
+
direction,
|
|
149
|
+
})
|
|
150
|
+
: () => () => connection.encryption as IEncryptionPassword;
|
|
151
|
+
|
|
152
|
+
return FetcherBase.propagate({
|
|
153
|
+
className: "EncryptedFetcher",
|
|
154
|
+
encode:
|
|
155
|
+
route.request?.encrypted === true
|
|
156
|
+
? (input, headers) => {
|
|
157
|
+
const p: IEncryptionPassword = closure("encode")(headers, input);
|
|
158
|
+
return AesPkcs5.encrypt(
|
|
159
|
+
(stringify ?? JSON.stringify)(input),
|
|
160
|
+
p.key,
|
|
161
|
+
p.iv,
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
: (input) => input,
|
|
165
|
+
decode:
|
|
166
|
+
route.response?.encrypted === true
|
|
167
|
+
? (input, headers) => {
|
|
168
|
+
const p: IEncryptionPassword = closure("decode")(headers, input);
|
|
169
|
+
const s: string = AesPkcs5.decrypt(input, p.key, p.iv);
|
|
170
|
+
return s.length ? JSON.parse(s) : s;
|
|
171
|
+
}
|
|
172
|
+
: (input) => input,
|
|
173
|
+
})(connection, route, input, stringify) as Promise<Output>;
|
|
174
|
+
}
|
|
175
|
+
}
|
package/src/HttpError.ts
CHANGED
|
@@ -1,85 +1,85 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* HTTP Error.
|
|
3
|
-
*
|
|
4
|
-
* `HttpError` is a type of error class who've been thrown by the remote HTTP server.
|
|
5
|
-
*
|
|
6
|
-
* @author Jeongho Nam - https://github.com/samchon
|
|
7
|
-
*/
|
|
8
|
-
export class HttpError extends Error {
|
|
9
|
-
/**
|
|
10
|
-
* @internal
|
|
11
|
-
*/
|
|
12
|
-
private body_: any = NOT_YET;
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Initializer Constructor.
|
|
16
|
-
*
|
|
17
|
-
* @param method Method of the HTTP request.
|
|
18
|
-
* @param path Path of the HTTP request.
|
|
19
|
-
* @param status Status code from the remote HTTP server.
|
|
20
|
-
* @param message Error message from the remote HTTP server.
|
|
21
|
-
*/
|
|
22
|
-
public constructor(
|
|
23
|
-
public readonly method:
|
|
24
|
-
| "GET"
|
|
25
|
-
| "DELETE"
|
|
26
|
-
| "POST"
|
|
27
|
-
| "PUT"
|
|
28
|
-
| "PATCH"
|
|
29
|
-
| "HEAD",
|
|
30
|
-
public readonly path: string,
|
|
31
|
-
public readonly status: number,
|
|
32
|
-
public readonly headers: Record<string, string | string[]>,
|
|
33
|
-
message: string,
|
|
34
|
-
) {
|
|
35
|
-
super(message);
|
|
36
|
-
|
|
37
|
-
// INHERITANCE POLYFILL
|
|
38
|
-
const proto: HttpError = new.target.prototype;
|
|
39
|
-
if (Object.setPrototypeOf) Object.setPrototypeOf(this, proto);
|
|
40
|
-
else (this as any).__proto__ = proto;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* `HttpError` to JSON.
|
|
45
|
-
*
|
|
46
|
-
* When you call `JSON.stringify()` function on current `HttpError` instance,
|
|
47
|
-
* this `HttpError.toJSON()` method would be automatically called.
|
|
48
|
-
*
|
|
49
|
-
* Also, if response body from the remote HTTP server forms a JSON object,
|
|
50
|
-
* this `HttpError.toJSON()` method would be useful because it returns the
|
|
51
|
-
* parsed JSON object about the {@link message} property.
|
|
52
|
-
*
|
|
53
|
-
* @template T Expected type of the response body.
|
|
54
|
-
* @returns JSON object of the `HttpError`.
|
|
55
|
-
*/
|
|
56
|
-
public toJSON<T>(): HttpError.IProps<T> {
|
|
57
|
-
if (this.body_ === NOT_YET)
|
|
58
|
-
try {
|
|
59
|
-
this.body_ = JSON.parse(this.message);
|
|
60
|
-
} catch {
|
|
61
|
-
this.body_ = this.message;
|
|
62
|
-
}
|
|
63
|
-
return {
|
|
64
|
-
method: this.method,
|
|
65
|
-
path: this.path,
|
|
66
|
-
status: this.status,
|
|
67
|
-
headers: this.headers,
|
|
68
|
-
message: this.body_,
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
export namespace HttpError {
|
|
73
|
-
/**
|
|
74
|
-
* Returned type of {@link HttpError.toJSON} method.
|
|
75
|
-
*/
|
|
76
|
-
export interface IProps<T> {
|
|
77
|
-
method: "GET" | "DELETE" | "POST" | "PUT" | "PATCH" | "HEAD";
|
|
78
|
-
path: string;
|
|
79
|
-
status: number;
|
|
80
|
-
headers: Record<string, string | string[]>;
|
|
81
|
-
message: T;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
const NOT_YET = {} as any;
|
|
1
|
+
/**
|
|
2
|
+
* HTTP Error.
|
|
3
|
+
*
|
|
4
|
+
* `HttpError` is a type of error class who've been thrown by the remote HTTP server.
|
|
5
|
+
*
|
|
6
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
7
|
+
*/
|
|
8
|
+
export class HttpError extends Error {
|
|
9
|
+
/**
|
|
10
|
+
* @internal
|
|
11
|
+
*/
|
|
12
|
+
private body_: any = NOT_YET;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Initializer Constructor.
|
|
16
|
+
*
|
|
17
|
+
* @param method Method of the HTTP request.
|
|
18
|
+
* @param path Path of the HTTP request.
|
|
19
|
+
* @param status Status code from the remote HTTP server.
|
|
20
|
+
* @param message Error message from the remote HTTP server.
|
|
21
|
+
*/
|
|
22
|
+
public constructor(
|
|
23
|
+
public readonly method:
|
|
24
|
+
| "GET"
|
|
25
|
+
| "DELETE"
|
|
26
|
+
| "POST"
|
|
27
|
+
| "PUT"
|
|
28
|
+
| "PATCH"
|
|
29
|
+
| "HEAD",
|
|
30
|
+
public readonly path: string,
|
|
31
|
+
public readonly status: number,
|
|
32
|
+
public readonly headers: Record<string, string | string[]>,
|
|
33
|
+
message: string,
|
|
34
|
+
) {
|
|
35
|
+
super(message);
|
|
36
|
+
|
|
37
|
+
// INHERITANCE POLYFILL
|
|
38
|
+
const proto: HttpError = new.target.prototype;
|
|
39
|
+
if (Object.setPrototypeOf) Object.setPrototypeOf(this, proto);
|
|
40
|
+
else (this as any).__proto__ = proto;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* `HttpError` to JSON.
|
|
45
|
+
*
|
|
46
|
+
* When you call `JSON.stringify()` function on current `HttpError` instance,
|
|
47
|
+
* this `HttpError.toJSON()` method would be automatically called.
|
|
48
|
+
*
|
|
49
|
+
* Also, if response body from the remote HTTP server forms a JSON object,
|
|
50
|
+
* this `HttpError.toJSON()` method would be useful because it returns the
|
|
51
|
+
* parsed JSON object about the {@link message} property.
|
|
52
|
+
*
|
|
53
|
+
* @template T Expected type of the response body.
|
|
54
|
+
* @returns JSON object of the `HttpError`.
|
|
55
|
+
*/
|
|
56
|
+
public toJSON<T>(): HttpError.IProps<T> {
|
|
57
|
+
if (this.body_ === NOT_YET)
|
|
58
|
+
try {
|
|
59
|
+
this.body_ = JSON.parse(this.message);
|
|
60
|
+
} catch {
|
|
61
|
+
this.body_ = this.message;
|
|
62
|
+
}
|
|
63
|
+
return {
|
|
64
|
+
method: this.method,
|
|
65
|
+
path: this.path,
|
|
66
|
+
status: this.status,
|
|
67
|
+
headers: this.headers,
|
|
68
|
+
message: this.body_,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
export namespace HttpError {
|
|
73
|
+
/**
|
|
74
|
+
* Returned type of {@link HttpError.toJSON} method.
|
|
75
|
+
*/
|
|
76
|
+
export interface IProps<T> {
|
|
77
|
+
method: "GET" | "DELETE" | "POST" | "PUT" | "PATCH" | "HEAD";
|
|
78
|
+
path: string;
|
|
79
|
+
status: number;
|
|
80
|
+
headers: Record<string, string | string[]>;
|
|
81
|
+
message: T;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const NOT_YET = {} as any;
|
|
@@ -1,50 +1,50 @@
|
|
|
1
|
-
import { IConnection } from "./IConnection";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Encryption password.
|
|
5
|
-
*
|
|
6
|
-
* `IEncryptionPassword` is a type of interface who represents encryption password used by
|
|
7
|
-
* the {@link Fetcher} with AES-128/256 algorithm. If your encryption password is not fixed
|
|
8
|
-
* but changes according to the input content, you can utilize the
|
|
9
|
-
* {@link IEncryptionPassword.Closure} function type.
|
|
10
|
-
*
|
|
11
|
-
* @author Jeongho Nam - https://github.com/samchon
|
|
12
|
-
*/
|
|
13
|
-
export interface IEncryptionPassword {
|
|
14
|
-
/**
|
|
15
|
-
* Secret key.
|
|
16
|
-
*/
|
|
17
|
-
key: string;
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Initialization Vector.
|
|
21
|
-
*/
|
|
22
|
-
iv: string;
|
|
23
|
-
}
|
|
24
|
-
export namespace IEncryptionPassword {
|
|
25
|
-
/**
|
|
26
|
-
* Type of a closure function returning the {@link IEncryptionPassword} object.
|
|
27
|
-
*
|
|
28
|
-
* `IEncryptionPassword.Closure` is a type of closure function who are returning the
|
|
29
|
-
* {@link IEncryptionPassword} object. It would be used when your encryption password
|
|
30
|
-
* be changed according to the input content.
|
|
31
|
-
*/
|
|
32
|
-
export interface Closure {
|
|
33
|
-
/**
|
|
34
|
-
* Encryption password getter.
|
|
35
|
-
*
|
|
36
|
-
* @param props Properties for predication
|
|
37
|
-
* @returns Encryption password
|
|
38
|
-
*/
|
|
39
|
-
(props: IProps): IEncryptionPassword;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Properties for the closure.
|
|
44
|
-
*/
|
|
45
|
-
export interface IProps {
|
|
46
|
-
headers: Record<string, IConnection.HeaderValue | undefined>;
|
|
47
|
-
body: string;
|
|
48
|
-
direction: "encode" | "decode";
|
|
49
|
-
}
|
|
50
|
-
}
|
|
1
|
+
import { IConnection } from "./IConnection";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Encryption password.
|
|
5
|
+
*
|
|
6
|
+
* `IEncryptionPassword` is a type of interface who represents encryption password used by
|
|
7
|
+
* the {@link Fetcher} with AES-128/256 algorithm. If your encryption password is not fixed
|
|
8
|
+
* but changes according to the input content, you can utilize the
|
|
9
|
+
* {@link IEncryptionPassword.Closure} function type.
|
|
10
|
+
*
|
|
11
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
12
|
+
*/
|
|
13
|
+
export interface IEncryptionPassword {
|
|
14
|
+
/**
|
|
15
|
+
* Secret key.
|
|
16
|
+
*/
|
|
17
|
+
key: string;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Initialization Vector.
|
|
21
|
+
*/
|
|
22
|
+
iv: string;
|
|
23
|
+
}
|
|
24
|
+
export namespace IEncryptionPassword {
|
|
25
|
+
/**
|
|
26
|
+
* Type of a closure function returning the {@link IEncryptionPassword} object.
|
|
27
|
+
*
|
|
28
|
+
* `IEncryptionPassword.Closure` is a type of closure function who are returning the
|
|
29
|
+
* {@link IEncryptionPassword} object. It would be used when your encryption password
|
|
30
|
+
* be changed according to the input content.
|
|
31
|
+
*/
|
|
32
|
+
export interface Closure {
|
|
33
|
+
/**
|
|
34
|
+
* Encryption password getter.
|
|
35
|
+
*
|
|
36
|
+
* @param props Properties for predication
|
|
37
|
+
* @returns Encryption password
|
|
38
|
+
*/
|
|
39
|
+
(props: IProps): IEncryptionPassword;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Properties for the closure.
|
|
44
|
+
*/
|
|
45
|
+
export interface IProps {
|
|
46
|
+
headers: Record<string, IConnection.HeaderValue | undefined>;
|
|
47
|
+
body: string;
|
|
48
|
+
direction: "encode" | "decode";
|
|
49
|
+
}
|
|
50
|
+
}
|
package/src/IRandomGenerator.ts
CHANGED
|
@@ -1,38 +1,38 @@
|
|
|
1
|
-
export interface IRandomGenerator {
|
|
2
|
-
boolean(): boolean;
|
|
3
|
-
integer(minimum?: number, maximum?: number): number;
|
|
4
|
-
number(minimum?: number, maximum?: number): number;
|
|
5
|
-
bigint(minimum?: bigint, maximum?: bigint): bigint;
|
|
6
|
-
string(length?: number): string;
|
|
7
|
-
array<T>(closure: (index: number) => T, count?: number): T[];
|
|
8
|
-
length(): number;
|
|
9
|
-
|
|
10
|
-
uuid(): string;
|
|
11
|
-
email(): string;
|
|
12
|
-
url(): string;
|
|
13
|
-
ipv4(): string;
|
|
14
|
-
ipv6(): string;
|
|
15
|
-
pattern(regex: RegExp): string;
|
|
16
|
-
date(minimum?: number, maximum?: number): string;
|
|
17
|
-
datetime(minimum?: number, maximum?: number): string;
|
|
18
|
-
|
|
19
|
-
customs?: IRandomGenerator.CustomMap;
|
|
20
|
-
}
|
|
21
|
-
export namespace IRandomGenerator {
|
|
22
|
-
export type CustomMap = {
|
|
23
|
-
[Type in keyof Customizable]?: (
|
|
24
|
-
tags: ICommentTag[],
|
|
25
|
-
) => Customizable[Type] | undefined;
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
export type Customizable = {
|
|
29
|
-
number: number;
|
|
30
|
-
string: string;
|
|
31
|
-
bigint: bigint;
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
export interface ICommentTag {
|
|
35
|
-
name: string;
|
|
36
|
-
value?: string;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
1
|
+
export interface IRandomGenerator {
|
|
2
|
+
boolean(): boolean;
|
|
3
|
+
integer(minimum?: number, maximum?: number): number;
|
|
4
|
+
number(minimum?: number, maximum?: number): number;
|
|
5
|
+
bigint(minimum?: bigint, maximum?: bigint): bigint;
|
|
6
|
+
string(length?: number): string;
|
|
7
|
+
array<T>(closure: (index: number) => T, count?: number): T[];
|
|
8
|
+
length(): number;
|
|
9
|
+
|
|
10
|
+
uuid(): string;
|
|
11
|
+
email(): string;
|
|
12
|
+
url(): string;
|
|
13
|
+
ipv4(): string;
|
|
14
|
+
ipv6(): string;
|
|
15
|
+
pattern(regex: RegExp): string;
|
|
16
|
+
date(minimum?: number, maximum?: number): string;
|
|
17
|
+
datetime(minimum?: number, maximum?: number): string;
|
|
18
|
+
|
|
19
|
+
customs?: IRandomGenerator.CustomMap;
|
|
20
|
+
}
|
|
21
|
+
export namespace IRandomGenerator {
|
|
22
|
+
export type CustomMap = {
|
|
23
|
+
[Type in keyof Customizable]?: (
|
|
24
|
+
tags: ICommentTag[],
|
|
25
|
+
) => Customizable[Type] | undefined;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export type Customizable = {
|
|
29
|
+
number: number;
|
|
30
|
+
string: string;
|
|
31
|
+
bigint: bigint;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export interface ICommentTag {
|
|
35
|
+
name: string;
|
|
36
|
+
value?: string;
|
|
37
|
+
}
|
|
38
|
+
}
|
package/src/PlainFetcher.ts
CHANGED
|
@@ -1,106 +1,106 @@
|
|
|
1
|
-
import { IConnection } from "./IConnection";
|
|
2
|
-
import { IPropagation } from "./IPropagation";
|
|
3
|
-
import { Primitive } from "./Primitive";
|
|
4
|
-
import { FetcherBase } from "./internal/FetcherBase";
|
|
5
|
-
import { IFetchRoute } from "./internal/IFetchRoute";
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Utility class for `fetch` functions used in `@nestia/sdk`.
|
|
9
|
-
*
|
|
10
|
-
* `PlainFetcher` is a utility class designed for SDK functions generated by
|
|
11
|
-
* [`@nestia/sdk`](https://nestia.io/docs/sdk/sdk), interacting with the remote
|
|
12
|
-
* HTTP sever API. In other words, this is a collection of dedicated `fetch()`
|
|
13
|
-
* functions for `@nestia/sdk`.
|
|
14
|
-
*
|
|
15
|
-
* For reference, `PlainFetcher` class does not encrypt or decrypt the body data
|
|
16
|
-
* at all. It just delivers plain data without any post processing. If you've
|
|
17
|
-
* defined a controller method through `@EncryptedRoute` or `@EncryptedBody`
|
|
18
|
-
* decorator, then {@liink EncryptedFetcher} class would be used instead.
|
|
19
|
-
*
|
|
20
|
-
* @author Jeongho Nam - https://github.com/samchon
|
|
21
|
-
*/
|
|
22
|
-
export namespace PlainFetcher {
|
|
23
|
-
/**
|
|
24
|
-
* Fetch function only for `HEAD` method.
|
|
25
|
-
*
|
|
26
|
-
* @param connection Connection information for the remote HTTP server
|
|
27
|
-
* @param route Route information about the target API
|
|
28
|
-
* @return Nothing because of `HEAD` method
|
|
29
|
-
*/
|
|
30
|
-
export function fetch(
|
|
31
|
-
connection: IConnection,
|
|
32
|
-
route: IFetchRoute<"HEAD">,
|
|
33
|
-
): Promise<void>;
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Fetch function only for `GET` method.
|
|
37
|
-
*
|
|
38
|
-
* @param connection Connection information for the remote HTTP server
|
|
39
|
-
* @param route Route information about the target API
|
|
40
|
-
* @return Response body data from the remote API
|
|
41
|
-
*/
|
|
42
|
-
export function fetch<Output>(
|
|
43
|
-
connection: IConnection,
|
|
44
|
-
route: IFetchRoute<"GET">,
|
|
45
|
-
): Promise<Primitive<Output>>;
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Fetch function for the `POST`, `PUT`, `PATCH` and `DELETE` methods.
|
|
49
|
-
*
|
|
50
|
-
* @param connection Connection information for the remote HTTP server
|
|
51
|
-
* @param route Route information about the target API
|
|
52
|
-
* @return Response body data from the remote API
|
|
53
|
-
*/
|
|
54
|
-
export function fetch<Input, Output>(
|
|
55
|
-
connection: IConnection,
|
|
56
|
-
route: IFetchRoute<"POST" | "PUT" | "PATCH" | "DELETE">,
|
|
57
|
-
input?: Input,
|
|
58
|
-
stringify?: (input: Input) => string,
|
|
59
|
-
): Promise<Primitive<Output>>;
|
|
60
|
-
|
|
61
|
-
export async function fetch<Input, Output>(
|
|
62
|
-
connection: IConnection,
|
|
63
|
-
route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
|
|
64
|
-
input?: Input,
|
|
65
|
-
stringify?: (input: Input) => string,
|
|
66
|
-
): Promise<Primitive<Output>> {
|
|
67
|
-
if (route.request?.encrypted === true || route.response?.encrypted === true)
|
|
68
|
-
throw new Error(
|
|
69
|
-
"Error on PlainFetcher.fetch(): PlainFetcher doesn't have encryption ability. Use EncryptedFetcher instead.",
|
|
70
|
-
);
|
|
71
|
-
return FetcherBase.fetch({
|
|
72
|
-
className: "PlainFetcher",
|
|
73
|
-
encode: (input) => input,
|
|
74
|
-
decode: (input) => input,
|
|
75
|
-
})(connection, route, input, stringify);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
export function propagate<Output extends IPropagation<any, any>>(
|
|
79
|
-
connection: IConnection,
|
|
80
|
-
route: IFetchRoute<"GET" | "HEAD">,
|
|
81
|
-
): Promise<Output>;
|
|
82
|
-
|
|
83
|
-
export function propagate<Input, Output extends IPropagation<any, any>>(
|
|
84
|
-
connection: IConnection,
|
|
85
|
-
route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
|
|
86
|
-
input?: Input,
|
|
87
|
-
stringify?: (input: Input) => string,
|
|
88
|
-
): Promise<Output>;
|
|
89
|
-
|
|
90
|
-
export async function propagate<Input, Output extends IPropagation<any, any>>(
|
|
91
|
-
connection: IConnection,
|
|
92
|
-
route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
|
|
93
|
-
input?: Input,
|
|
94
|
-
stringify?: (input: Input) => string,
|
|
95
|
-
): Promise<Output> {
|
|
96
|
-
if (route.request?.encrypted === true || route.response?.encrypted === true)
|
|
97
|
-
throw new Error(
|
|
98
|
-
"Error on PlainFetcher.propagate(): PlainFetcher doesn't have encryption ability. Use EncryptedFetcher instead.",
|
|
99
|
-
);
|
|
100
|
-
return FetcherBase.propagate({
|
|
101
|
-
className: "PlainFetcher",
|
|
102
|
-
encode: (input) => input,
|
|
103
|
-
decode: (input) => input,
|
|
104
|
-
})(connection, route, input, stringify) as Promise<Output>;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
1
|
+
import { IConnection } from "./IConnection";
|
|
2
|
+
import { IPropagation } from "./IPropagation";
|
|
3
|
+
import { Primitive } from "./Primitive";
|
|
4
|
+
import { FetcherBase } from "./internal/FetcherBase";
|
|
5
|
+
import { IFetchRoute } from "./internal/IFetchRoute";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Utility class for `fetch` functions used in `@nestia/sdk`.
|
|
9
|
+
*
|
|
10
|
+
* `PlainFetcher` is a utility class designed for SDK functions generated by
|
|
11
|
+
* [`@nestia/sdk`](https://nestia.io/docs/sdk/sdk), interacting with the remote
|
|
12
|
+
* HTTP sever API. In other words, this is a collection of dedicated `fetch()`
|
|
13
|
+
* functions for `@nestia/sdk`.
|
|
14
|
+
*
|
|
15
|
+
* For reference, `PlainFetcher` class does not encrypt or decrypt the body data
|
|
16
|
+
* at all. It just delivers plain data without any post processing. If you've
|
|
17
|
+
* defined a controller method through `@EncryptedRoute` or `@EncryptedBody`
|
|
18
|
+
* decorator, then {@liink EncryptedFetcher} class would be used instead.
|
|
19
|
+
*
|
|
20
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
21
|
+
*/
|
|
22
|
+
export namespace PlainFetcher {
|
|
23
|
+
/**
|
|
24
|
+
* Fetch function only for `HEAD` method.
|
|
25
|
+
*
|
|
26
|
+
* @param connection Connection information for the remote HTTP server
|
|
27
|
+
* @param route Route information about the target API
|
|
28
|
+
* @return Nothing because of `HEAD` method
|
|
29
|
+
*/
|
|
30
|
+
export function fetch(
|
|
31
|
+
connection: IConnection,
|
|
32
|
+
route: IFetchRoute<"HEAD">,
|
|
33
|
+
): Promise<void>;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Fetch function only for `GET` method.
|
|
37
|
+
*
|
|
38
|
+
* @param connection Connection information for the remote HTTP server
|
|
39
|
+
* @param route Route information about the target API
|
|
40
|
+
* @return Response body data from the remote API
|
|
41
|
+
*/
|
|
42
|
+
export function fetch<Output>(
|
|
43
|
+
connection: IConnection,
|
|
44
|
+
route: IFetchRoute<"GET">,
|
|
45
|
+
): Promise<Primitive<Output>>;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Fetch function for the `POST`, `PUT`, `PATCH` and `DELETE` methods.
|
|
49
|
+
*
|
|
50
|
+
* @param connection Connection information for the remote HTTP server
|
|
51
|
+
* @param route Route information about the target API
|
|
52
|
+
* @return Response body data from the remote API
|
|
53
|
+
*/
|
|
54
|
+
export function fetch<Input, Output>(
|
|
55
|
+
connection: IConnection,
|
|
56
|
+
route: IFetchRoute<"POST" | "PUT" | "PATCH" | "DELETE">,
|
|
57
|
+
input?: Input,
|
|
58
|
+
stringify?: (input: Input) => string,
|
|
59
|
+
): Promise<Primitive<Output>>;
|
|
60
|
+
|
|
61
|
+
export async function fetch<Input, Output>(
|
|
62
|
+
connection: IConnection,
|
|
63
|
+
route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
|
|
64
|
+
input?: Input,
|
|
65
|
+
stringify?: (input: Input) => string,
|
|
66
|
+
): Promise<Primitive<Output>> {
|
|
67
|
+
if (route.request?.encrypted === true || route.response?.encrypted === true)
|
|
68
|
+
throw new Error(
|
|
69
|
+
"Error on PlainFetcher.fetch(): PlainFetcher doesn't have encryption ability. Use EncryptedFetcher instead.",
|
|
70
|
+
);
|
|
71
|
+
return FetcherBase.fetch({
|
|
72
|
+
className: "PlainFetcher",
|
|
73
|
+
encode: (input) => input,
|
|
74
|
+
decode: (input) => input,
|
|
75
|
+
})(connection, route, input, stringify);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function propagate<Output extends IPropagation<any, any>>(
|
|
79
|
+
connection: IConnection,
|
|
80
|
+
route: IFetchRoute<"GET" | "HEAD">,
|
|
81
|
+
): Promise<Output>;
|
|
82
|
+
|
|
83
|
+
export function propagate<Input, Output extends IPropagation<any, any>>(
|
|
84
|
+
connection: IConnection,
|
|
85
|
+
route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
|
|
86
|
+
input?: Input,
|
|
87
|
+
stringify?: (input: Input) => string,
|
|
88
|
+
): Promise<Output>;
|
|
89
|
+
|
|
90
|
+
export async function propagate<Input, Output extends IPropagation<any, any>>(
|
|
91
|
+
connection: IConnection,
|
|
92
|
+
route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
|
|
93
|
+
input?: Input,
|
|
94
|
+
stringify?: (input: Input) => string,
|
|
95
|
+
): Promise<Output> {
|
|
96
|
+
if (route.request?.encrypted === true || route.response?.encrypted === true)
|
|
97
|
+
throw new Error(
|
|
98
|
+
"Error on PlainFetcher.propagate(): PlainFetcher doesn't have encryption ability. Use EncryptedFetcher instead.",
|
|
99
|
+
);
|
|
100
|
+
return FetcherBase.propagate({
|
|
101
|
+
className: "PlainFetcher",
|
|
102
|
+
encode: (input) => input,
|
|
103
|
+
decode: (input) => input,
|
|
104
|
+
})(connection, route, input, stringify) as Promise<Output>;
|
|
105
|
+
}
|
|
106
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export * from "./IConnection";
|
|
2
|
-
export * from "./IEncryptionPassword";
|
|
3
|
-
export * from "./IPropagation";
|
|
4
|
-
export * from "./IRandomGenerator";
|
|
5
|
-
export * from "./HttpError";
|
|
6
|
-
export * from "./Primitive";
|
|
7
|
-
export * from "./Resolved";
|
|
1
|
+
export * from "./IConnection";
|
|
2
|
+
export * from "./IEncryptionPassword";
|
|
3
|
+
export * from "./IPropagation";
|
|
4
|
+
export * from "./IRandomGenerator";
|
|
5
|
+
export * from "./HttpError";
|
|
6
|
+
export * from "./Primitive";
|
|
7
|
+
export * from "./Resolved";
|
|
@@ -1,61 +1,61 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Properties of remote API route.
|
|
3
|
-
*
|
|
4
|
-
* @author Jeongho Nam - https://github.com/samchon
|
|
5
|
-
*/
|
|
6
|
-
export interface IFetchRoute<
|
|
7
|
-
Method extends "HEAD" | "GET" | "POST" | "PUT" | "PATCH" | "DELETE",
|
|
8
|
-
> {
|
|
9
|
-
/**
|
|
10
|
-
* Method of the HTTP request.
|
|
11
|
-
*/
|
|
12
|
-
method: Method;
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Path of the HTTP request.
|
|
16
|
-
*/
|
|
17
|
-
path: string;
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Request body data info.
|
|
21
|
-
*/
|
|
22
|
-
request: Method extends "DELETE" | "POST" | "PUT" | "PATCH"
|
|
23
|
-
? IFetchRoute.IBody | null
|
|
24
|
-
: null;
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Response body data info.
|
|
28
|
-
*/
|
|
29
|
-
response: Method extends "HEAD" ? null : IFetchRoute.IBody;
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* When special status code being used.
|
|
33
|
-
*/
|
|
34
|
-
status: number | null;
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Parser of the query string.
|
|
38
|
-
*
|
|
39
|
-
* If content type of response body is `application/x-www-form-urlencoded`,
|
|
40
|
-
* then this `parseQuery` function would be called.
|
|
41
|
-
*
|
|
42
|
-
* If you've forgotten to configuring this `parseQuery` property about the
|
|
43
|
-
* `application/x-www-form-urlencoded` typed response body data, then
|
|
44
|
-
* only the `URLSearchParams` typed instance would be returned instead.
|
|
45
|
-
*/
|
|
46
|
-
parseQuery?(input: URLSearchParams): any;
|
|
47
|
-
}
|
|
48
|
-
export namespace IFetchRoute {
|
|
49
|
-
/**
|
|
50
|
-
* Metadata of body.
|
|
51
|
-
*
|
|
52
|
-
* Describes how content-type being used in body, and whether encrypted or not.
|
|
53
|
-
*/
|
|
54
|
-
export interface IBody {
|
|
55
|
-
type:
|
|
56
|
-
| "application/json"
|
|
57
|
-
| "application/x-www-form-urlencoded"
|
|
58
|
-
| "text/plain";
|
|
59
|
-
encrypted?: boolean;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Properties of remote API route.
|
|
3
|
+
*
|
|
4
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
5
|
+
*/
|
|
6
|
+
export interface IFetchRoute<
|
|
7
|
+
Method extends "HEAD" | "GET" | "POST" | "PUT" | "PATCH" | "DELETE",
|
|
8
|
+
> {
|
|
9
|
+
/**
|
|
10
|
+
* Method of the HTTP request.
|
|
11
|
+
*/
|
|
12
|
+
method: Method;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Path of the HTTP request.
|
|
16
|
+
*/
|
|
17
|
+
path: string;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Request body data info.
|
|
21
|
+
*/
|
|
22
|
+
request: Method extends "DELETE" | "POST" | "PUT" | "PATCH"
|
|
23
|
+
? IFetchRoute.IBody | null
|
|
24
|
+
: null;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Response body data info.
|
|
28
|
+
*/
|
|
29
|
+
response: Method extends "HEAD" ? null : IFetchRoute.IBody;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* When special status code being used.
|
|
33
|
+
*/
|
|
34
|
+
status: number | null;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Parser of the query string.
|
|
38
|
+
*
|
|
39
|
+
* If content type of response body is `application/x-www-form-urlencoded`,
|
|
40
|
+
* then this `parseQuery` function would be called.
|
|
41
|
+
*
|
|
42
|
+
* If you've forgotten to configuring this `parseQuery` property about the
|
|
43
|
+
* `application/x-www-form-urlencoded` typed response body data, then
|
|
44
|
+
* only the `URLSearchParams` typed instance would be returned instead.
|
|
45
|
+
*/
|
|
46
|
+
parseQuery?(input: URLSearchParams): any;
|
|
47
|
+
}
|
|
48
|
+
export namespace IFetchRoute {
|
|
49
|
+
/**
|
|
50
|
+
* Metadata of body.
|
|
51
|
+
*
|
|
52
|
+
* Describes how content-type being used in body, and whether encrypted or not.
|
|
53
|
+
*/
|
|
54
|
+
export interface IBody {
|
|
55
|
+
type:
|
|
56
|
+
| "application/json"
|
|
57
|
+
| "application/x-www-form-urlencoded"
|
|
58
|
+
| "text/plain";
|
|
59
|
+
encrypted?: boolean;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @internal
|
|
3
|
-
*/
|
|
4
|
-
export class Singleton<T> {
|
|
5
|
-
private value_: T | object;
|
|
6
|
-
|
|
7
|
-
public constructor(private readonly closure_: () => T) {
|
|
8
|
-
this.value_ = NOT_MOUNTED_YET;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
public get(): T {
|
|
12
|
-
if (this.value_ === NOT_MOUNTED_YET) this.value_ = this.closure_();
|
|
13
|
-
return this.value_ as T;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* @internal
|
|
19
|
-
*/
|
|
20
|
-
const NOT_MOUNTED_YET = {};
|
|
1
|
+
/**
|
|
2
|
+
* @internal
|
|
3
|
+
*/
|
|
4
|
+
export class Singleton<T> {
|
|
5
|
+
private value_: T | object;
|
|
6
|
+
|
|
7
|
+
public constructor(private readonly closure_: () => T) {
|
|
8
|
+
this.value_ = NOT_MOUNTED_YET;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
public get(): T {
|
|
12
|
+
if (this.value_ === NOT_MOUNTED_YET) this.value_ = this.closure_();
|
|
13
|
+
return this.value_ as T;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @internal
|
|
19
|
+
*/
|
|
20
|
+
const NOT_MOUNTED_YET = {};
|