@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,237 +1,247 @@
1
- /// <reference lib="dom" />
2
- import { IEncryptionPassword } from "./IEncryptionPassword";
3
- import { IRandomGenerator } from "./IRandomGenerator";
4
-
5
- /**
6
- * Connection information.
7
- *
8
- * `IConnection` is an interface ttype who represents connection information of the
9
- * remote HTTP server. You can target the remote HTTP server by wring the
10
- * {@link IConnection.host} variable down. Also, you can configure special header values
11
- * by specializing the {@link IConnection.headers} variable.
12
- *
13
- * If the remote HTTP server encrypts or decrypts its body data through the AES-128/256
14
- * algorithm, specify the {@link IConnection.encryption} with {@link IEncryptionPassword}
15
- * or {@link IEncryptionPassword.Closure} variable.
16
- *
17
- * @author Jenogho Nam - https://github.com/samchon
18
- * @author Seungjun We - https://github.com/SeungjunWe
19
- */
20
- export interface IConnection<Headers extends object = {}> {
21
- /**
22
- * Host address of the remote HTTP server.
23
- */
24
- host: string;
25
-
26
- /**
27
- * Header values delivered to the remote HTTP server.
28
- */
29
- headers?: Record<string, IConnection.HeaderValue> &
30
- IConnection.Headerify<Headers>;
31
-
32
- /**
33
- * Use simulation mode.
34
- *
35
- * If you configure this property to be `true` or assign an {@link IRandomGenerator}
36
- * instance, your SDK library does not send any request to remote backend server,
37
- * but just returns random data generated by `typia.random<T>()` function with
38
- * request data validation.
39
- *
40
- * By the way, to utilize this simulation mode, SDK library must be generated with
41
- * {@link INestiaConfig.simulate} option, too. Open `nestia.config.ts` file, and
42
- * configure {@link INestiaConfig.simulate} property to be `true`. Them, newly
43
- * generated SDK library would have a built-in mock-up data generator.
44
- *
45
- * @default false
46
- */
47
- simulate?: boolean | Partial<IRandomGenerator>;
48
-
49
- /**
50
- * Additional options for the `fetch` function.
51
- */
52
- options?: IConnection.IOptions;
53
-
54
- /**
55
- * Encryption password of its closure function.
56
- *
57
- * Define it only when target backend server is encrypting body data through
58
- * `@EncryptedRoute` or `@EncryptedBody` decorators of `@nestia/core` for
59
- * security reason.
60
- */
61
- encryption?: IEncryptionPassword | IEncryptionPassword.Closure;
62
-
63
- /**
64
- * Custom fetch function.
65
- *
66
- * If you want to use custom `fetch` function instead of built-in function,
67
- * assign your custom `fetch` function into this property.
68
- */
69
- fetch?: typeof fetch;
70
- }
71
- export namespace IConnection {
72
- /**
73
- * Addiotional options for the `fetch` function.
74
- *
75
- * Almost same with {@link RequestInit} type of the {@link fetch} function,
76
- * but `body`, `headers` and `method` properties are omitted.
77
- *
78
- * The reason why defining duplicated definition of {@link RequestInit}
79
- * is for legacy NodeJS environments, which does not have the {@link fetch}
80
- * function type.
81
- */
82
- export interface IOptions {
83
- /**
84
- * A string indicating how the request will interact with the browser's
85
- * cache to set request's cache.
86
- */
87
- cache?:
88
- | "default"
89
- | "force-cache"
90
- | "no-cache"
91
- | "no-store"
92
- | "only-if-cached"
93
- | "reload";
94
-
95
- /**
96
- * A string indicating whether credentials will be sent with the request
97
- * always, never, or only when sent to a same-origin URL. Sets request's
98
- * credentials.
99
- */
100
- credentials?: "omit" | "same-origin" | "include";
101
-
102
- /**
103
- * A cryptographic hash of the resource to be fetched by request.
104
- *
105
- * Sets request's integrity.
106
- */
107
- integrity?: string;
108
-
109
- /**
110
- * A boolean to set request's keepalive.
111
- */
112
- keepalive?: boolean;
113
-
114
- /**
115
- * A string to indicate whether the request will use CORS, or will be
116
- * restricted to same-origin URLs.
117
- *
118
- * Sets request's mode.
119
- */
120
- mode?: "cors" | "navigate" | "no-cors" | "same-origin";
121
-
122
- /**
123
- * A string indicating whether request follows redirects, results in
124
- * an error upon encountering a redirect, or returns the redirect
125
- * (in an opaque fashion).
126
- *
127
- * Sets request's redirect.
128
- */
129
- redirect?: "error" | "follow" | "manual";
130
-
131
- /**
132
- * A string whose value is a same-origin URL, "about:client", or the
133
- * empty string, to set request's referrer.
134
- */
135
- referrer?: string;
136
-
137
- /**
138
- * A referrer policy to set request's referrerPolicy.
139
- */
140
- referrerPolicy?:
141
- | ""
142
- | "no-referrer"
143
- | "no-referrer-when-downgrade"
144
- | "origin"
145
- | "origin-when-cross-origin"
146
- | "same-origin"
147
- | "strict-origin"
148
- | "strict-origin-when-cross-origin"
149
- | "unsafe-url";
150
-
151
- /**
152
- * An AbortSignal to set request's signal.
153
- */
154
- signal?: AbortSignal | null;
155
- }
156
-
157
- /**
158
- * Type of allowed header values.
159
- *
160
- * Only atomic or array of atomic values are allowed.
161
- */
162
- export type HeaderValue =
163
- | string
164
- | boolean
165
- | number
166
- | bigint
167
- | string
168
- | Array<boolean>
169
- | Array<number>
170
- | Array<bigint>
171
- | Array<number>
172
- | Array<string>;
173
-
174
- /**
175
- * Type of headers
176
- *
177
- * `Headerify` removes every properties that are not allowed in the
178
- * HTTP headers type.
179
- *
180
- * Below are list of prohibited in HTTP headers.
181
- *
182
- * 1. Value type one of {@link HeaderValue}
183
- * 2. Key is "set-cookie", but value is not an Array type
184
- * 3. Key is one of them, but value is Array type
185
- * - "age"
186
- * - "authorization"
187
- * - "content-length"
188
- * - "content-type"
189
- * - "etag"
190
- * - "expires"
191
- * - "from"
192
- * - "host"
193
- * - "if-modified-since"
194
- * - "if-unmodified-since"
195
- * - "last-modified"
196
- * - "location"
197
- * - "max-forwards"
198
- * - "proxy-authorization"
199
- * - "referer"
200
- * - "retry-after"
201
- * - "server"
202
- * - "user-agent"
203
- */
204
- export type Headerify<T extends object> = {
205
- [P in keyof T]?: T[P] extends HeaderValue | undefined
206
- ? P extends string
207
- ? Lowercase<P> extends "set-cookie"
208
- ? T[P] extends Array<HeaderValue>
209
- ? T[P] | undefined
210
- : never
211
- : Lowercase<P> extends
212
- | "age"
213
- | "authorization"
214
- | "content-length"
215
- | "content-type"
216
- | "etag"
217
- | "expires"
218
- | "from"
219
- | "host"
220
- | "if-modified-since"
221
- | "if-unmodified-since"
222
- | "last-modified"
223
- | "location"
224
- | "max-forwards"
225
- | "proxy-authorization"
226
- | "referer"
227
- | "retry-after"
228
- | "server"
229
- | "user-agent"
230
- ? T[P] extends Array<HeaderValue>
231
- ? never
232
- : T[P] | undefined
233
- : T[P] | undefined
234
- : never
235
- : never;
236
- };
237
- }
1
+ /// <reference lib="dom" />
2
+ import { IEncryptionPassword } from "./IEncryptionPassword";
3
+ import { IFetchEvent } from "./IFetchEvent";
4
+ import { IRandomGenerator } from "./IRandomGenerator";
5
+
6
+ /**
7
+ * Connection information.
8
+ *
9
+ * `IConnection` is an interface ttype who represents connection information of the
10
+ * remote HTTP server. You can target the remote HTTP server by wring the
11
+ * {@link IConnection.host} variable down. Also, you can configure special header values
12
+ * by specializing the {@link IConnection.headers} variable.
13
+ *
14
+ * If the remote HTTP server encrypts or decrypts its body data through the AES-128/256
15
+ * algorithm, specify the {@link IConnection.encryption} with {@link IEncryptionPassword}
16
+ * or {@link IEncryptionPassword.Closure} variable.
17
+ *
18
+ * @author Jenogho Nam - https://github.com/samchon
19
+ * @author Seungjun We - https://github.com/SeungjunWe
20
+ */
21
+ export interface IConnection<Headers extends object = {}> {
22
+ /**
23
+ * Host address of the remote HTTP server.
24
+ */
25
+ host: string;
26
+
27
+ /**
28
+ * Header values delivered to the remote HTTP server.
29
+ */
30
+ headers?: Record<string, IConnection.HeaderValue> &
31
+ IConnection.Headerify<Headers>;
32
+
33
+ /**
34
+ * Use simulation mode.
35
+ *
36
+ * If you configure this property to be `true` or assign an {@link IRandomGenerator}
37
+ * instance, your SDK library does not send any request to remote backend server,
38
+ * but just returns random data generated by `typia.random<T>()` function with
39
+ * request data validation.
40
+ *
41
+ * By the way, to utilize this simulation mode, SDK library must be generated with
42
+ * {@link INestiaConfig.simulate} option, too. Open `nestia.config.ts` file, and
43
+ * configure {@link INestiaConfig.simulate} property to be `true`. Them, newly
44
+ * generated SDK library would have a built-in mock-up data generator.
45
+ *
46
+ * @default false
47
+ */
48
+ simulate?: boolean | Partial<IRandomGenerator>;
49
+
50
+ /**
51
+ * Logger function.
52
+ *
53
+ * This function is called when the fetch event is completed.
54
+ *
55
+ * @param event Event information of the fetch event.
56
+ */
57
+ logger?: (event: IFetchEvent) => Promise<void>;
58
+
59
+ /**
60
+ * Encryption password of its closure function.
61
+ *
62
+ * Define it only when target backend server is encrypting body data through
63
+ * `@EncryptedRoute` or `@EncryptedBody` decorators of `@nestia/core` for
64
+ * security reason.
65
+ */
66
+ encryption?: IEncryptionPassword | IEncryptionPassword.Closure;
67
+
68
+ /**
69
+ * Additional options for the `fetch` function.
70
+ */
71
+ options?: IConnection.IOptions;
72
+
73
+ /**
74
+ * Custom fetch function.
75
+ *
76
+ * If you want to use custom `fetch` function instead of built-in function,
77
+ * assign your custom `fetch` function into this property.
78
+ */
79
+ fetch?: typeof fetch;
80
+ }
81
+ export namespace IConnection {
82
+ /**
83
+ * Addiotional options for the `fetch` function.
84
+ *
85
+ * Almost same with {@link RequestInit} type of the {@link fetch} function,
86
+ * but `body`, `headers` and `method` properties are omitted.
87
+ *
88
+ * The reason why defining duplicated definition of {@link RequestInit}
89
+ * is for legacy NodeJS environments, which does not have the {@link fetch}
90
+ * function type.
91
+ */
92
+ export interface IOptions {
93
+ /**
94
+ * A string indicating how the request will interact with the browser's
95
+ * cache to set request's cache.
96
+ */
97
+ cache?:
98
+ | "default"
99
+ | "force-cache"
100
+ | "no-cache"
101
+ | "no-store"
102
+ | "only-if-cached"
103
+ | "reload";
104
+
105
+ /**
106
+ * A string indicating whether credentials will be sent with the request
107
+ * always, never, or only when sent to a same-origin URL. Sets request's
108
+ * credentials.
109
+ */
110
+ credentials?: "omit" | "same-origin" | "include";
111
+
112
+ /**
113
+ * A cryptographic hash of the resource to be fetched by request.
114
+ *
115
+ * Sets request's integrity.
116
+ */
117
+ integrity?: string;
118
+
119
+ /**
120
+ * A boolean to set request's keepalive.
121
+ */
122
+ keepalive?: boolean;
123
+
124
+ /**
125
+ * A string to indicate whether the request will use CORS, or will be
126
+ * restricted to same-origin URLs.
127
+ *
128
+ * Sets request's mode.
129
+ */
130
+ mode?: "cors" | "navigate" | "no-cors" | "same-origin";
131
+
132
+ /**
133
+ * A string indicating whether request follows redirects, results in
134
+ * an error upon encountering a redirect, or returns the redirect
135
+ * (in an opaque fashion).
136
+ *
137
+ * Sets request's redirect.
138
+ */
139
+ redirect?: "error" | "follow" | "manual";
140
+
141
+ /**
142
+ * A string whose value is a same-origin URL, "about:client", or the
143
+ * empty string, to set request's referrer.
144
+ */
145
+ referrer?: string;
146
+
147
+ /**
148
+ * A referrer policy to set request's referrerPolicy.
149
+ */
150
+ referrerPolicy?:
151
+ | ""
152
+ | "no-referrer"
153
+ | "no-referrer-when-downgrade"
154
+ | "origin"
155
+ | "origin-when-cross-origin"
156
+ | "same-origin"
157
+ | "strict-origin"
158
+ | "strict-origin-when-cross-origin"
159
+ | "unsafe-url";
160
+
161
+ /**
162
+ * An AbortSignal to set request's signal.
163
+ */
164
+ signal?: AbortSignal | null;
165
+ }
166
+
167
+ /**
168
+ * Type of allowed header values.
169
+ *
170
+ * Only atomic or array of atomic values are allowed.
171
+ */
172
+ export type HeaderValue =
173
+ | string
174
+ | boolean
175
+ | number
176
+ | bigint
177
+ | string
178
+ | Array<boolean>
179
+ | Array<number>
180
+ | Array<bigint>
181
+ | Array<number>
182
+ | Array<string>;
183
+
184
+ /**
185
+ * Type of headers
186
+ *
187
+ * `Headerify` removes every properties that are not allowed in the
188
+ * HTTP headers type.
189
+ *
190
+ * Below are list of prohibited in HTTP headers.
191
+ *
192
+ * 1. Value type one of {@link HeaderValue}
193
+ * 2. Key is "set-cookie", but value is not an Array type
194
+ * 3. Key is one of them, but value is Array type
195
+ * - "age"
196
+ * - "authorization"
197
+ * - "content-length"
198
+ * - "content-type"
199
+ * - "etag"
200
+ * - "expires"
201
+ * - "from"
202
+ * - "host"
203
+ * - "if-modified-since"
204
+ * - "if-unmodified-since"
205
+ * - "last-modified"
206
+ * - "location"
207
+ * - "max-forwards"
208
+ * - "proxy-authorization"
209
+ * - "referer"
210
+ * - "retry-after"
211
+ * - "server"
212
+ * - "user-agent"
213
+ */
214
+ export type Headerify<T extends object> = {
215
+ [P in keyof T]?: T[P] extends HeaderValue | undefined
216
+ ? P extends string
217
+ ? Lowercase<P> extends "set-cookie"
218
+ ? T[P] extends Array<HeaderValue>
219
+ ? T[P] | undefined
220
+ : never
221
+ : Lowercase<P> extends
222
+ | "age"
223
+ | "authorization"
224
+ | "content-length"
225
+ | "content-type"
226
+ | "etag"
227
+ | "expires"
228
+ | "from"
229
+ | "host"
230
+ | "if-modified-since"
231
+ | "if-unmodified-since"
232
+ | "last-modified"
233
+ | "location"
234
+ | "max-forwards"
235
+ | "proxy-authorization"
236
+ | "referer"
237
+ | "retry-after"
238
+ | "server"
239
+ | "user-agent"
240
+ ? T[P] extends Array<HeaderValue>
241
+ ? never
242
+ : T[P] | undefined
243
+ : T[P] | undefined
244
+ : never
245
+ : never;
246
+ };
247
+ }
@@ -1,56 +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 type IProps = IDecodeProps | IEncodeProps;
46
- export interface IDecodeProps {
47
- headers: Record<string, IConnection.HeaderValue | undefined>;
48
- body: Uint8Array;
49
- direction: "decode";
50
- }
51
- export interface IEncodeProps {
52
- headers: Record<string, IConnection.HeaderValue | undefined>;
53
- body: string;
54
- direction: "encode";
55
- }
56
- }
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
+ }
@@ -0,0 +1,31 @@
1
+ // import { IConnection } from "./IConnection";
2
+ import { IFetchRoute } from "./IFetchRoute";
3
+
4
+ export interface IFetchEvent {
5
+ route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">;
6
+ path: string;
7
+ status: number | null;
8
+ input: any;
9
+ output: any;
10
+ started_at: Date;
11
+ respond_at: Date | null;
12
+ completed_at: Date;
13
+ }
14
+ // export namespace IFetchEvent {
15
+ // export interface IFunction {
16
+ // (connection: IConnection, ...args: any[]): Promise<any>;
17
+ // METADATA: {
18
+ // method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "HEAD" | "OPTIONS";
19
+ // path: string;
20
+ // request: null | {
21
+ // type: string;
22
+ // encrypted: boolean;
23
+ // };
24
+ // response: null | {
25
+ // type: string;
26
+ // encrypted: boolean;
27
+ // };
28
+ // };
29
+ // status: null | number;
30
+ // }
31
+ // }