@nestia/fetcher 3.0.0-dev.20231209 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/README.md +12 -9
  2. package/lib/{internal/AesPkcs5.d.ts → AesPkcs5.d.ts} +2 -2
  3. package/lib/{internal/AesPkcs5.js → AesPkcs5.js} +8 -16
  4. package/lib/AesPkcs5.js.map +1 -0
  5. package/lib/EncryptedFetcher.d.ts +3 -4
  6. package/lib/EncryptedFetcher.js +8 -11
  7. package/lib/EncryptedFetcher.js.map +1 -1
  8. package/lib/IConnection.d.ts +11 -2
  9. package/lib/IEncryptionPassword.d.ts +2 -8
  10. package/lib/IFetchEvent.d.ts +11 -0
  11. package/lib/IFetchEvent.js +21 -0
  12. package/lib/IFetchEvent.js.map +1 -0
  13. package/lib/{internal/IFetchRoute.d.ts → IFetchRoute.d.ts} +1 -1
  14. package/lib/{internal/IFetchRoute.js.map → IFetchRoute.js.map} +1 -1
  15. package/lib/NestiaSimulator.d.ts +13 -0
  16. package/lib/NestiaSimulator.js +62 -0
  17. package/lib/NestiaSimulator.js.map +1 -0
  18. package/lib/PlainFetcher.d.ts +3 -4
  19. package/lib/PlainFetcher.js +2 -2
  20. package/lib/PlainFetcher.js.map +1 -1
  21. package/lib/Resolved.d.ts +5 -5
  22. package/lib/index.d.ts +3 -1
  23. package/lib/index.js +3 -1
  24. package/lib/index.js.map +1 -1
  25. package/lib/internal/FetcherBase.d.ts +5 -6
  26. package/lib/internal/FetcherBase.js +143 -70
  27. package/lib/internal/FetcherBase.js.map +1 -1
  28. package/package.json +2 -2
  29. package/src/{internal/AesPkcs5.ts → AesPkcs5.ts} +50 -66
  30. package/src/EncryptedFetcher.ts +174 -179
  31. package/src/HttpError.ts +85 -85
  32. package/src/IConnection.ts +247 -237
  33. package/src/IEncryptionPassword.ts +50 -56
  34. package/src/IFetchEvent.ts +31 -0
  35. package/src/{internal/IFetchRoute.ts → IFetchRoute.ts} +62 -62
  36. package/src/IPropagation.ts +102 -102
  37. package/src/IRandomGenerator.ts +38 -38
  38. package/src/NestiaSimulator.ts +82 -0
  39. package/src/PlainFetcher.ts +105 -106
  40. package/src/Primitive.ts +136 -135
  41. package/src/Resolved.ts +119 -116
  42. package/src/index.ts +9 -7
  43. package/src/internal/FetcherBase.ts +124 -72
  44. package/src/internal/Singleton.ts +20 -20
  45. package/lib/internal/AesPkcs5.js.map +0 -1
  46. /package/lib/{internal/IFetchRoute.js → IFetchRoute.js} +0 -0
@@ -1,179 +1,174 @@
1
- import { IConnection } from "./IConnection";
2
- import { IEncryptionPassword } from "./IEncryptionPassword";
3
- import { IPropagation } from "./IPropagation";
4
- import { Primitive } from "./Primitive";
5
- import { AesPkcs5 } from "./internal/AesPkcs5";
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 | Uint8Array,
81
- ) =>
82
- (connection.encryption as IEncryptionPassword.Closure)({
83
- headers,
84
- body: body as any,
85
- direction,
86
- })
87
- : () => () => connection.encryption as IEncryptionPassword;
88
- return FetcherBase.fetch({
89
- className: "EncryptedFetcher",
90
- encode:
91
- route.request?.encrypted === true
92
- ? (input, headers) => {
93
- const p = closure("encode")(headers, input);
94
- return AesPkcs5.encrypt(
95
- (stringify ?? JSON.stringify)(input),
96
- p.key,
97
- p.iv,
98
- );
99
- }
100
- : (input) => input,
101
- decode:
102
- route.response?.encrypted === true
103
- ? (input, headers) => {
104
- const p: IEncryptionPassword = closure("decode")(headers, input);
105
- const str: string = new TextDecoder().decode(
106
- AesPkcs5.decrypt(input as Uint8Array, p.key, p.iv),
107
- );
108
- return str.length ? JSON.parse(str) : str;
109
- }
110
- : (input) => input,
111
- })(connection, route, input, stringify);
112
- }
113
-
114
- export function propagate<Output extends IPropagation<any, any>>(
115
- connection: IConnection,
116
- route: IFetchRoute<"GET" | "HEAD">,
117
- ): Promise<Output>;
118
-
119
- export function propagate<Input, Output extends IPropagation<any, any>>(
120
- connection: IConnection,
121
- route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
122
- input?: Input,
123
- stringify?: (input: Input) => string,
124
- ): Promise<Output>;
125
-
126
- export async function propagate<Input, Output extends IPropagation<any, any>>(
127
- connection: IConnection,
128
- route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
129
- input?: Input,
130
- stringify?: (input: Input) => string,
131
- ): Promise<Output> {
132
- if (
133
- (route.request?.encrypted === true || route.response?.encrypted) &&
134
- connection.encryption === undefined
135
- )
136
- throw new Error(
137
- "Error on EncryptedFetcher.propagate(): the encryption password has not been configured.",
138
- );
139
- const closure =
140
- typeof connection.encryption === "function"
141
- ? (direction: "encode" | "decode") =>
142
- (
143
- headers: Record<string, IConnection.HeaderValue | undefined>,
144
- body: string | Uint8Array,
145
- ) =>
146
- (connection.encryption as IEncryptionPassword.Closure)({
147
- headers,
148
- body: body as any,
149
- direction,
150
- })
151
- : () => () => connection.encryption as IEncryptionPassword;
152
- return FetcherBase.propagate({
153
- className: "EncryptedFetcher",
154
- encode:
155
- route.request?.encrypted === true
156
- ? (input, headers) => {
157
- const p = 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 binary: Uint8Array = input as Uint8Array;
169
- const p: IEncryptionPassword = closure("decode")(headers, binary);
170
- if (!binary.length) return;
171
- const str: string = new TextDecoder().decode(
172
- AesPkcs5.decrypt(binary, p.key, p.iv),
173
- );
174
- return str.length ? JSON.parse(str) : str;
175
- }
176
- : (input) => input,
177
- })(connection, route, input, stringify) as Promise<Output>;
178
- }
179
- }
1
+ import { AesPkcs5 } from "./AesPkcs5";
2
+ import { IConnection } from "./IConnection";
3
+ import { IEncryptionPassword } from "./IEncryptionPassword";
4
+ import { IFetchRoute } from "./IFetchRoute";
5
+ import { IPropagation } from "./IPropagation";
6
+ import { FetcherBase } from "./internal/FetcherBase";
7
+
8
+ /**
9
+ * Utility class for `fetch` functions used in `@nestia/sdk` with encryption.
10
+ *
11
+ * `EncryptedFetcher` is a utility class designed for SDK functions generated by
12
+ * [`@nestia/sdk`](https://nestia.io/docs/sdk/sdk), interacting with the remote
13
+ * HTTP API encrypted by AES-PKCS algorithm. In other words, this is a collection of
14
+ * dedicated `fetch()` functions for `@nestia/sdk` with encryption.
15
+ *
16
+ * For reference, `EncryptedFetcher` class being used only when target controller
17
+ * method is encrypting body data by `@EncryptedRoute` or `@EncryptedBody` decorators.
18
+ * If those decorators are not used, {@link PlainFetcher} class would be used instead.
19
+ *
20
+ * @author Jeongho Nam - https://github.com/samchon
21
+ */
22
+ export namespace EncryptedFetcher {
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<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<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<Output> {
67
+ if (
68
+ (route.request?.encrypted === true || route.response?.encrypted) &&
69
+ connection.encryption === undefined
70
+ )
71
+ throw new Error(
72
+ "Error on EncryptedFetcher.fetch(): the encryption password has not been configured.",
73
+ );
74
+ const closure =
75
+ typeof connection.encryption === "function"
76
+ ? (direction: "encode" | "decode") =>
77
+ (
78
+ headers: Record<string, IConnection.HeaderValue | undefined>,
79
+ body: string,
80
+ ) =>
81
+ (connection.encryption as IEncryptionPassword.Closure)({
82
+ headers,
83
+ body,
84
+ direction,
85
+ })
86
+ : () => () => connection.encryption as IEncryptionPassword;
87
+
88
+ return FetcherBase.fetch({
89
+ className: "EncryptedFetcher",
90
+ encode:
91
+ route.request?.encrypted === true
92
+ ? (input, headers) => {
93
+ const p: IEncryptionPassword = closure("encode")(headers, input);
94
+ return AesPkcs5.encrypt(
95
+ (stringify ?? JSON.stringify)(input),
96
+ p.key,
97
+ p.iv,
98
+ );
99
+ }
100
+ : (input) => input,
101
+ decode:
102
+ route.response?.encrypted === true
103
+ ? (input, headers) => {
104
+ const p: IEncryptionPassword = closure("decode")(headers, input);
105
+ const s: string = AesPkcs5.decrypt(input, p.key, p.iv);
106
+ return s.length ? JSON.parse(s) : s;
107
+ }
108
+ : (input) => input,
109
+ })(connection, route, input, stringify);
110
+ }
111
+
112
+ export function propagate<Output extends IPropagation<any, any>>(
113
+ connection: IConnection,
114
+ route: IFetchRoute<"GET" | "HEAD">,
115
+ ): Promise<Output>;
116
+
117
+ export function propagate<Input, Output extends IPropagation<any, any>>(
118
+ connection: IConnection,
119
+ route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
120
+ input?: Input,
121
+ stringify?: (input: Input) => string,
122
+ ): Promise<Output>;
123
+
124
+ export async function propagate<Input, Output extends IPropagation<any, any>>(
125
+ connection: IConnection,
126
+ route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
127
+ input?: Input,
128
+ stringify?: (input: Input) => string,
129
+ ): Promise<Output> {
130
+ if (
131
+ (route.request?.encrypted === true || route.response?.encrypted) &&
132
+ connection.encryption === undefined
133
+ )
134
+ throw new Error(
135
+ "Error on EncryptedFetcher.propagate(): the encryption password has not been configured.",
136
+ );
137
+ const closure =
138
+ typeof connection.encryption === "function"
139
+ ? (direction: "encode" | "decode") =>
140
+ (
141
+ headers: Record<string, IConnection.HeaderValue | undefined>,
142
+ body: string,
143
+ ) =>
144
+ (connection.encryption as IEncryptionPassword.Closure)({
145
+ headers,
146
+ body,
147
+ direction,
148
+ })
149
+ : () => () => connection.encryption as IEncryptionPassword;
150
+
151
+ return FetcherBase.propagate({
152
+ className: "EncryptedFetcher",
153
+ encode:
154
+ route.request?.encrypted === true
155
+ ? (input, headers) => {
156
+ const p: IEncryptionPassword = closure("encode")(headers, input);
157
+ return AesPkcs5.encrypt(
158
+ (stringify ?? JSON.stringify)(input),
159
+ p.key,
160
+ p.iv,
161
+ );
162
+ }
163
+ : (input) => input,
164
+ decode:
165
+ route.response?.encrypted === true
166
+ ? (input, headers) => {
167
+ const p: IEncryptionPassword = closure("decode")(headers, input);
168
+ const s: string = AesPkcs5.decrypt(input, p.key, p.iv);
169
+ return s.length ? JSON.parse(s) : s;
170
+ }
171
+ : (input) => input,
172
+ })(connection, route, input, stringify) as Promise<Output>;
173
+ }
174
+ }
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;