@nestia/fetcher 2.4.3 → 2.4.4-dev.20240109

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nestia/fetcher",
3
- "version": "2.4.3",
3
+ "version": "2.4.4-dev.20240109",
4
4
  "description": "Fetcher library of Nestia SDK",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
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,237 +1,237 @@
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 { 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
+ }