@nestia/fetcher 7.0.3 → 7.1.1-dev.20250714

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.
@@ -1,174 +1,174 @@
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.request({
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
- }
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.request({
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
+ }
@@ -1,87 +1,87 @@
1
- /**
2
- * FormData input type.
3
- *
4
- * `FormDataInput<T>` is a type for the input of the `FormData` request, casting
5
- * `File` property value type as an union of `File` and {@link FormDataInput.IFileProps},
6
- * especially for the React Native environment.
7
- *
8
- * You know what? In the React Native environment, `File` class is not supported.
9
- * Therefore, when composing a `FormData` request, you have to put the URI address
10
- * of the local filesystem with file name and content type that is represented by the
11
- * {@link FormDataInput.IFileProps} type.
12
- *
13
- * This `FormDataInput<T>` type is designed for that purpose. If the property value
14
- * type is a `File` class, it converts it to an union type of `File` and
15
- * {@link FormDataInput.IFileProps} type. Also, if the property value type is an array
16
- * of `File` class, it converts it to an array of union type of `File` and
17
- * {@link FormDataInput.IFileProps} type too.
18
- *
19
- * Before | After
20
- * ----------|------------------------
21
- * `boolean` | `boolean`
22
- * `bigint` | `bigint`
23
- * `number` | `number`
24
- * `string` | `string`
25
- * `File` | `File \| IFileProps`
26
- *
27
- * @template T Target object type.
28
- * @author Jeongho Nam - https://github.com/samchon
29
- */
30
- export type FormDataInput<T extends object> =
31
- T extends Array<any>
32
- ? never
33
- : T extends Function
34
- ? never
35
- : {
36
- [P in keyof T]: T[P] extends Array<infer U>
37
- ? FormDataInput.Value<U>[]
38
- : FormDataInput.Value<T[P]>;
39
- };
40
- export namespace FormDataInput {
41
- /**
42
- * Value type of the `FormDataInput`.
43
- *
44
- * `Value<T>` is a type for the property value defined in the `FormDataInput`.
45
- *
46
- * If the original value type is a `File` class, `Value<T>` converts it to an union
47
- * type of `File` and {@link IFileProps} type which is a structured data for the
48
- * URI file location in the React Native environment.
49
- */
50
- export type Value<T> = T extends File ? T | IFileProps : T;
51
-
52
- /**
53
- * Properties of a file.
54
- *
55
- * In the React Native, this `IFileProps` structured data can replace the `File`
56
- * class instance in the `FormData` request.
57
- *
58
- * Just put the {@link uri URI address} of the local file system with the file's
59
- * {@link name} and {@link type}. It would be casted to the `File` class instance
60
- * automatically in the `FormData` request.
61
- *
62
- * Note that, this `IFileProps` type works only in the React Native environment.
63
- * If you are developing a Web or NodeJS application, you have to utilize the
64
- * `File` class instance directly.
65
- */
66
- export interface IFileProps {
67
- /**
68
- * URI address of the file.
69
- *
70
- * In the React Native, the URI address in the local file system can replace
71
- * the `File` class instance. If
72
- *
73
- * @format uri
74
- */
75
- uri: string;
76
-
77
- /**
78
- * Name of the file.
79
- */
80
- name: string;
81
-
82
- /**
83
- * Content type of the file.
84
- */
85
- type: string;
86
- }
87
- }
1
+ /**
2
+ * FormData input type.
3
+ *
4
+ * `FormDataInput<T>` is a type for the input of the `FormData` request, casting
5
+ * `File` property value type as an union of `File` and {@link FormDataInput.IFileProps},
6
+ * especially for the React Native environment.
7
+ *
8
+ * You know what? In the React Native environment, `File` class is not supported.
9
+ * Therefore, when composing a `FormData` request, you have to put the URI address
10
+ * of the local filesystem with file name and content type that is represented by the
11
+ * {@link FormDataInput.IFileProps} type.
12
+ *
13
+ * This `FormDataInput<T>` type is designed for that purpose. If the property value
14
+ * type is a `File` class, it converts it to an union type of `File` and
15
+ * {@link FormDataInput.IFileProps} type. Also, if the property value type is an array
16
+ * of `File` class, it converts it to an array of union type of `File` and
17
+ * {@link FormDataInput.IFileProps} type too.
18
+ *
19
+ * Before | After
20
+ * ----------|------------------------
21
+ * `boolean` | `boolean`
22
+ * `bigint` | `bigint`
23
+ * `number` | `number`
24
+ * `string` | `string`
25
+ * `File` | `File \| IFileProps`
26
+ *
27
+ * @template T Target object type.
28
+ * @author Jeongho Nam - https://github.com/samchon
29
+ */
30
+ export type FormDataInput<T extends object> =
31
+ T extends Array<any>
32
+ ? never
33
+ : T extends Function
34
+ ? never
35
+ : {
36
+ [P in keyof T]: T[P] extends Array<infer U>
37
+ ? FormDataInput.Value<U>[]
38
+ : FormDataInput.Value<T[P]>;
39
+ };
40
+ export namespace FormDataInput {
41
+ /**
42
+ * Value type of the `FormDataInput`.
43
+ *
44
+ * `Value<T>` is a type for the property value defined in the `FormDataInput`.
45
+ *
46
+ * If the original value type is a `File` class, `Value<T>` converts it to an union
47
+ * type of `File` and {@link IFileProps} type which is a structured data for the
48
+ * URI file location in the React Native environment.
49
+ */
50
+ export type Value<T> = T extends File ? T | IFileProps : T;
51
+
52
+ /**
53
+ * Properties of a file.
54
+ *
55
+ * In the React Native, this `IFileProps` structured data can replace the `File`
56
+ * class instance in the `FormData` request.
57
+ *
58
+ * Just put the {@link uri URI address} of the local file system with the file's
59
+ * {@link name} and {@link type}. It would be casted to the `File` class instance
60
+ * automatically in the `FormData` request.
61
+ *
62
+ * Note that, this `IFileProps` type works only in the React Native environment.
63
+ * If you are developing a Web or NodeJS application, you have to utilize the
64
+ * `File` class instance directly.
65
+ */
66
+ export interface IFileProps {
67
+ /**
68
+ * URI address of the file.
69
+ *
70
+ * In the React Native, the URI address in the local file system can replace
71
+ * the `File` class instance. If
72
+ *
73
+ * @format uri
74
+ */
75
+ uri: string;
76
+
77
+ /**
78
+ * Name of the file.
79
+ */
80
+ name: string;
81
+
82
+ /**
83
+ * Content type of the file.
84
+ */
85
+ type: string;
86
+ }
87
+ }
package/src/HttpError.ts CHANGED
@@ -1 +1 @@
1
- export { HttpError } from "@samchon/openapi";
1
+ export { HttpError } from "@samchon/openapi";