@nestia/fetcher 2.5.0-dev.20240131 → 2.5.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/lib/NestiaSimulator.d.ts +13 -0
- package/lib/NestiaSimulator.js +62 -0
- package/lib/NestiaSimulator.js.map +1 -0
- 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/NestiaSimulator.ts +82 -0
- 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
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export declare namespace NestiaSimulator {
|
|
2
|
+
interface IProps {
|
|
3
|
+
host: string;
|
|
4
|
+
path: string;
|
|
5
|
+
method: "GET" | "POST" | "PATCH" | "PUT" | "DELETE";
|
|
6
|
+
contentType: string;
|
|
7
|
+
}
|
|
8
|
+
const assert: (props: IProps) => {
|
|
9
|
+
param: (name: string) => <T>(task: () => T) => void;
|
|
10
|
+
query: <T_1>(task: () => T_1) => void;
|
|
11
|
+
body: <T_2>(task: () => T_2) => void;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NestiaSimulator = void 0;
|
|
4
|
+
var HttpError_1 = require("./HttpError");
|
|
5
|
+
var NestiaSimulator;
|
|
6
|
+
(function (NestiaSimulator) {
|
|
7
|
+
NestiaSimulator.assert = function (props) {
|
|
8
|
+
return {
|
|
9
|
+
param: param(props),
|
|
10
|
+
query: query(props),
|
|
11
|
+
body: body(props),
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
var param = function (props) {
|
|
15
|
+
return function (name) {
|
|
16
|
+
return function (task) {
|
|
17
|
+
validate(function (exp) { return "URL parameter \"".concat(name, "\" is not ").concat(exp.expected, " type."); })(props)(task);
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
var query = function (props) {
|
|
22
|
+
return function (task) {
|
|
23
|
+
return validate(function () { return "Request query parameters are not following the promised type."; })(props)(task);
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
var body = function (props) {
|
|
27
|
+
return function (task) {
|
|
28
|
+
return validate(function () { return "Request body is not following the promised type."; })(props)(task);
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
var validate = function (message, path) {
|
|
32
|
+
return function (props) {
|
|
33
|
+
return function (task) {
|
|
34
|
+
try {
|
|
35
|
+
task();
|
|
36
|
+
}
|
|
37
|
+
catch (exp) {
|
|
38
|
+
if (isTypeGuardError(exp))
|
|
39
|
+
throw new HttpError_1.HttpError(props.method, props.host + props.path, 400, {
|
|
40
|
+
"Content-Type": props.contentType,
|
|
41
|
+
}, JSON.stringify({
|
|
42
|
+
method: exp.method,
|
|
43
|
+
path: path !== null && path !== void 0 ? path : exp.path,
|
|
44
|
+
expected: exp.expected,
|
|
45
|
+
value: exp.value,
|
|
46
|
+
message: message(exp),
|
|
47
|
+
}));
|
|
48
|
+
throw exp;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
})(NestiaSimulator || (exports.NestiaSimulator = NestiaSimulator = {}));
|
|
54
|
+
var isTypeGuardError = function (input) {
|
|
55
|
+
return "string" === typeof input.method &&
|
|
56
|
+
(undefined === input.path || "string" === typeof input.path) &&
|
|
57
|
+
"string" === typeof input.expected &&
|
|
58
|
+
"string" === typeof input.name &&
|
|
59
|
+
"string" === typeof input.message &&
|
|
60
|
+
(undefined === input.stack || "string" === typeof input.stack);
|
|
61
|
+
};
|
|
62
|
+
//# sourceMappingURL=NestiaSimulator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"NestiaSimulator.js","sourceRoot":"","sources":["../src/NestiaSimulator.ts"],"names":[],"mappings":";;;AAAA,yCAAwC;AAExC,IAAiB,eAAe,CAgE/B;AAhED,WAAiB,eAAe;IAQjB,sBAAM,GAAG,UAAC,KAAa;QAClC,OAAO;YACL,KAAK,EAAE,KAAK,CAAC,KAAK,CAAC;YACnB,KAAK,EAAE,KAAK,CAAC,KAAK,CAAC;YACnB,IAAI,EAAE,IAAI,CAAC,KAAK,CAAC;SAClB,CAAC;IACJ,CAAC,CAAC;IACF,IAAM,KAAK,GACT,UAAC,KAAa;QACd,OAAA,UAAC,IAAY;YACb,OAAA,UAAI,IAAa;gBACf,QAAQ,CAAC,UAAC,GAAG,IAAK,OAAA,0BAAkB,IAAI,uBAAY,GAAG,CAAC,QAAQ,WAAQ,EAAtD,CAAsD,CAAC,CACvE,KAAK,CACN,CAAC,IAAI,CAAC,CAAC;YACV,CAAC;QAJD,CAIC;IALD,CAKC,CAAC;IAEJ,IAAM,KAAK,GACT,UAAC,KAAa;QACd,OAAA,UAAI,IAAa;YACf,OAAA,QAAQ,CACN,cAAM,OAAA,+DAA+D,EAA/D,CAA+D,CACtE,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC;QAFd,CAEc;IAHhB,CAGgB,CAAC;IAEnB,IAAM,IAAI,GACR,UAAC,KAAa;QACd,OAAA,UAAI,IAAa;YACf,OAAA,QAAQ,CAAC,cAAM,OAAA,kDAAkD,EAAlD,CAAkD,CAAC,CAAC,KAAK,CAAC,CACvE,IAAI,CACL;QAFD,CAEC;IAHH,CAGG,CAAC;IAEN,IAAM,QAAQ,GACZ,UAAC,OAAwC,EAAE,IAAa;QACxD,OAAA,UAAC,KAAa;YACd,OAAA,UAAI,IAAa;gBACf,IAAI,CAAC;oBACH,IAAI,EAAE,CAAC;gBACT,CAAC;gBAAC,OAAO,GAAG,EAAE,CAAC;oBACb,IAAI,gBAAgB,CAAC,GAAG,CAAC;wBACvB,MAAM,IAAI,qBAAS,CACjB,KAAK,CAAC,MAAM,EACZ,KAAK,CAAC,IAAI,GAAG,KAAK,CAAC,IAAI,EACvB,GAAG,EACH;4BACE,cAAc,EAAE,KAAK,CAAC,WAAW;yBAClC,EACD,IAAI,CAAC,SAAS,CAAC;4BACb,MAAM,EAAE,GAAG,CAAC,MAAM;4BAClB,IAAI,EAAE,IAAI,aAAJ,IAAI,cAAJ,IAAI,GAAI,GAAG,CAAC,IAAI;4BACtB,QAAQ,EAAE,GAAG,CAAC,QAAQ;4BACtB,KAAK,EAAE,GAAG,CAAC,KAAK;4BAChB,OAAO,EAAE,OAAO,CAAC,GAAG,CAAC;yBACtB,CAAC,CACH,CAAC;oBACJ,MAAM,GAAG,CAAC;gBACZ,CAAC;YACH,CAAC;QAtBD,CAsBC;IAvBD,CAuBC,CAAC;AACN,CAAC,EAhEgB,eAAe,+BAAf,eAAe,QAgE/B;AAED,IAAM,gBAAgB,GAAG,UAAC,KAAU;IAClC,OAAA,QAAQ,KAAK,OAAO,KAAK,CAAC,MAAM;QAChC,CAAC,SAAS,KAAK,KAAK,CAAC,IAAI,IAAI,QAAQ,KAAK,OAAO,KAAK,CAAC,IAAI,CAAC;QAC5D,QAAQ,KAAK,OAAO,KAAK,CAAC,QAAQ;QAClC,QAAQ,KAAK,OAAO,KAAK,CAAC,IAAI;QAC9B,QAAQ,KAAK,OAAO,KAAK,CAAC,OAAO;QACjC,CAAC,SAAS,KAAK,KAAK,CAAC,KAAK,IAAI,QAAQ,KAAK,OAAO,KAAK,CAAC,KAAK,CAAC;AAL9D,CAK8D,CAAC"}
|
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
|
+
}
|