@nestia/fetcher 2.5.8 → 2.5.9-dev.20240223

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/README.md CHANGED
@@ -6,17 +6,19 @@
6
6
  [![Downloads](https://img.shields.io/npm/dm/@nestia/core.svg)](https://www.npmjs.com/package/@nestia/core)
7
7
  [![Build Status](https://github.com/samchon/nestia/workflows/build/badge.svg)](https://github.com/samchon/nestia/actions?query=workflow%3Abuild)
8
8
  [![Guide Documents](https://img.shields.io/badge/guide-documents-forestgreen)](https://nestia.io/docs/)
9
+ [![Discord Badge](https://img.shields.io/badge/discord-NestJS/Nestia-d91965?style=flat&labelColor=5866f2&logo=discord&logoColor=white&link=https://discord.com/channels/520622812742811698/1197293125434093701)](https://discord.com/channels/520622812742811698/1181877086797967420)
9
10
 
10
11
  Nestia is a set of helper libraries for NestJS, supporting below features:
11
12
 
12
- - `@nestia/core`: super-fast decorators
13
+ - `@nestia/core`: Super-fast decorators
13
14
  - `@nestia/sdk`:
14
15
  - Swagger generator evolved than ever
15
16
  - SDK library generator for clients
16
17
  - Mockup Simulator for client applications
17
18
  - Automatic E2E test functions generator
18
- - `@nestia/migrate`: migration from Swagger to NestJS
19
- - `nestia`: just CLI (command line interface) tool
19
+ - `@nestia/migrate`: Migration from Swagger to NestJS
20
+ - `@nestia/editor`: Online TypeScript Swagger Editor
21
+ - `nestia`: Just CLI (command line interface) tool
20
22
 
21
23
  > **Note**
22
24
  >
@@ -25,8 +27,8 @@ Nestia is a set of helper libraries for NestJS, supporting below features:
25
27
  > - Runtime validator is **20,000x faster** than `class-validator`
26
28
  > - JSON serialization is **200x faster** than `class-transformer`
27
29
  > - Software Development Kit
28
- > - SDK is a collection of `fetch` functions with type definitions like [tRPC](https://trpc.io/)
29
- > - Mockup simulator means embedded backend simulator in SDK
30
+ > - Collection of typed `fetch` functions with DTO structures like [tRPC](https://trpc.io/)
31
+ > - Mockup simulator means embedded backend simulator in the SDK
30
32
  > - similar with [msw](https://mswjs.io/), but fully automated
31
33
 
32
34
  ![nestia-sdk-demo](https://user-images.githubusercontent.com/13158709/215004990-368c589d-7101-404e-b81b-fbc936382f05.gif)
@@ -59,7 +61,7 @@ Check out the document in the [website](https://nestia.io/docs/):
59
61
  - [TypedRoute](https://nestia.io/docs/core/TypedRoute/)
60
62
  - [TypedBody](https://nestia.io/docs/core/TypedBody/)
61
63
  - [TypedParam](https://nestia.io/docs/core/TypedParam/)
62
- - [TypedQuery](https://nestia.io/docs/core/TypedRoute/)
64
+ - [TypedQuery](https://nestia.io/docs/core/TypedQuery/)
63
65
  - [TypedHeaders](https://nestia.io/docs/core/TypedHeaders/)
64
66
  - [TypedException](https://nestia.io/docs/core/TypedException/)
65
67
  - Generators
@@ -68,6 +70,7 @@ Check out the document in the [website](https://nestia.io/docs/):
68
70
  - [E2E Functions](https://nestia.io/docs/sdk/e2e/)
69
71
  - [Mockup Simulator](https://nestia.io/docs/sdk/simulator/)
70
72
  - [Swagger to NestJS](https://nestia.io/docs/migrate/)
73
+ - [TypeScript Swagger Editor](https://nestia.io/docs/editor/)
71
74
 
72
75
  ### 🔗 Appendix
73
76
  - [⇲ Benchmark Result](https://github.com/samchon/nestia/tree/master/benchmark/results/11th%20Gen%20Intel(R)%20Core(TM)%20i5-1135G7%20%40%202.40GHz)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nestia/fetcher",
3
- "version": "2.5.8",
3
+ "version": "2.5.9-dev.20240223",
4
4
  "description": "Fetcher library of Nestia SDK",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
@@ -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
+ }
@@ -1,102 +1,102 @@
1
- import { Primitive } from "./Primitive";
2
-
3
- /**
4
- * Propagation type.
5
- *
6
- * `IPropagation` is a type gathering all possible status codes and their body
7
- * data types as a discriminated union type. You can specify the status code and
8
- * its body data type just by using conditional statement like below.
9
- *
10
- * ```typescript
11
- * type Output = IPropagation<{
12
- * 200: ISeller.IAuthorized;
13
- * 400: TypeGuardError.IProps;
14
- * >};
15
- *
16
- * const output: Output = await sdk.sellers.authenticate.join(input);
17
- * if (output.success) {
18
- * // automatically casted to "ISeller.IAuthorized" type
19
- * const authorized: ISeller.IAuthorized = output.data;
20
- * } else if (output.status === 400) {
21
- * // automatically casted to "TypeGuardError.IProps" type
22
- * const error: TypeGuardError.IProps = output.data;
23
- * } else {
24
- * // unknown type when out of pre-defined status codes
25
- * const result: unknown = output.data;
26
- * }
27
- * ```
28
- *
29
- * For reference, this `IPropagation` type is utilized by SDK library generated by
30
- * `@nestia/sdk`, when you've configured {@link INestiaConfig.propagate} to be `true`.
31
- * In that case, SDK functions generated by `@nestia/sdk` no more returns response DTO
32
- * typed data directly, but returns this `IPropagation` typed object instead.
33
- *
34
- * @template StatusMap Map of status code and its body data type.
35
- * @template Success Default success status code.
36
- * @author Jeongho Nam - https://github.com/samchon
37
- */
38
- export type IPropagation<
39
- StatusMap extends {
40
- [P in IPropagation.Status]?: any;
41
- },
42
- Success extends number = 200 | 201,
43
- > =
44
- | {
45
- [P in keyof StatusMap]: IPropagation.IBranch<
46
- P extends Success ? true : false,
47
- P,
48
- StatusMap[P]
49
- >;
50
- }[keyof StatusMap]
51
- | IPropagation.IBranch<false, unknown, unknown>;
52
- export namespace IPropagation {
53
- /**
54
- * Type of configurable status codes.
55
- *
56
- * The special characters like `2XX`, `3XX`, `4XX`, `5XX` are meaning the range
57
- * of status codes. If `5XX` is specified, it means the status code is in the
58
- * range of `500` to `599`.
59
- */
60
- export type Status = number | "2XX" | "3XX" | "4XX" | "5XX";
61
-
62
- /**
63
- * Branch type of propagation.
64
- *
65
- * `IPropagation.IBranch` is a branch type composing `IPropagation` type,
66
- * which is gathering all possible status codes and their body data types
67
- * as a union type.
68
- */
69
- export interface IBranch<Success extends boolean, StatusValue, BodyData> {
70
- success: Success;
71
- status: StatusValue extends "2XX" | "3XX" | "4XX" | "5XX"
72
- ? StatusRange<StatusValue>
73
- : StatusValue extends number
74
- ? StatusValue
75
- : never;
76
- data: Primitive<BodyData>;
77
- headers: Record<string, string | string[]>;
78
- }
79
-
80
- /**
81
- * Range of status codes by the first digit.
82
- */
83
- export type StatusRange<T extends "2XX" | "3XX" | "4XX" | "5XX"> = T extends 0
84
- ? IntRange<200, 299>
85
- : T extends 3
86
- ? IntRange<300, 399>
87
- : T extends 4
88
- ? IntRange<400, 499>
89
- : IntRange<500, 599>;
90
-
91
- type IntRange<F extends number, T extends number> = Exclude<
92
- Enumerate<T>,
93
- Enumerate<F>
94
- >;
95
-
96
- type Enumerate<
97
- N extends number,
98
- Acc extends number[] = [],
99
- > = Acc["length"] extends N
100
- ? Acc[number]
101
- : Enumerate<N, [...Acc, Acc["length"]]>;
102
- }
1
+ import { Primitive } from "./Primitive";
2
+
3
+ /**
4
+ * Propagation type.
5
+ *
6
+ * `IPropagation` is a type gathering all possible status codes and their body
7
+ * data types as a discriminated union type. You can specify the status code and
8
+ * its body data type just by using conditional statement like below.
9
+ *
10
+ * ```typescript
11
+ * type Output = IPropagation<{
12
+ * 200: ISeller.IAuthorized;
13
+ * 400: TypeGuardError.IProps;
14
+ * >};
15
+ *
16
+ * const output: Output = await sdk.sellers.authenticate.join(input);
17
+ * if (output.success) {
18
+ * // automatically casted to "ISeller.IAuthorized" type
19
+ * const authorized: ISeller.IAuthorized = output.data;
20
+ * } else if (output.status === 400) {
21
+ * // automatically casted to "TypeGuardError.IProps" type
22
+ * const error: TypeGuardError.IProps = output.data;
23
+ * } else {
24
+ * // unknown type when out of pre-defined status codes
25
+ * const result: unknown = output.data;
26
+ * }
27
+ * ```
28
+ *
29
+ * For reference, this `IPropagation` type is utilized by SDK library generated by
30
+ * `@nestia/sdk`, when you've configured {@link INestiaConfig.propagate} to be `true`.
31
+ * In that case, SDK functions generated by `@nestia/sdk` no more returns response DTO
32
+ * typed data directly, but returns this `IPropagation` typed object instead.
33
+ *
34
+ * @template StatusMap Map of status code and its body data type.
35
+ * @template Success Default success status code.
36
+ * @author Jeongho Nam - https://github.com/samchon
37
+ */
38
+ export type IPropagation<
39
+ StatusMap extends {
40
+ [P in IPropagation.Status]?: any;
41
+ },
42
+ Success extends number = 200 | 201,
43
+ > =
44
+ | {
45
+ [P in keyof StatusMap]: IPropagation.IBranch<
46
+ P extends Success ? true : false,
47
+ P,
48
+ StatusMap[P]
49
+ >;
50
+ }[keyof StatusMap]
51
+ | IPropagation.IBranch<false, unknown, unknown>;
52
+ export namespace IPropagation {
53
+ /**
54
+ * Type of configurable status codes.
55
+ *
56
+ * The special characters like `2XX`, `3XX`, `4XX`, `5XX` are meaning the range
57
+ * of status codes. If `5XX` is specified, it means the status code is in the
58
+ * range of `500` to `599`.
59
+ */
60
+ export type Status = number | "2XX" | "3XX" | "4XX" | "5XX";
61
+
62
+ /**
63
+ * Branch type of propagation.
64
+ *
65
+ * `IPropagation.IBranch` is a branch type composing `IPropagation` type,
66
+ * which is gathering all possible status codes and their body data types
67
+ * as a union type.
68
+ */
69
+ export interface IBranch<Success extends boolean, StatusValue, BodyData> {
70
+ success: Success;
71
+ status: StatusValue extends "2XX" | "3XX" | "4XX" | "5XX"
72
+ ? StatusRange<StatusValue>
73
+ : StatusValue extends number
74
+ ? StatusValue
75
+ : never;
76
+ data: Primitive<BodyData>;
77
+ headers: Record<string, string | string[]>;
78
+ }
79
+
80
+ /**
81
+ * Range of status codes by the first digit.
82
+ */
83
+ export type StatusRange<T extends "2XX" | "3XX" | "4XX" | "5XX"> = T extends 0
84
+ ? IntRange<200, 299>
85
+ : T extends 3
86
+ ? IntRange<300, 399>
87
+ : T extends 4
88
+ ? IntRange<400, 499>
89
+ : IntRange<500, 599>;
90
+
91
+ type IntRange<F extends number, T extends number> = Exclude<
92
+ Enumerate<T>,
93
+ Enumerate<F>
94
+ >;
95
+
96
+ type Enumerate<
97
+ N extends number,
98
+ Acc extends number[] = [],
99
+ > = Acc["length"] extends N
100
+ ? Acc[number]
101
+ : Enumerate<N, [...Acc, Acc["length"]]>;
102
+ }
package/src/Primitive.ts CHANGED
@@ -1,136 +1,136 @@
1
- /**
2
- * Primitive type of JSON.
3
- *
4
- * `Primitive<T>` is a TMP (Type Meta Programming) type which converts
5
- * its argument as a primitive type within framework JSON.
6
- *
7
- * If the target argument is a built-in class which returns its origin primitive type
8
- * through the `valueOf()` method like the `String` or `Number`, its return type would
9
- * be the `string` or `number`. Otherwise, the built-in class does not have the
10
- * `valueOf()` method, the return type would be an empty object (`{}`).
11
- *
12
- * Otherwise, the target argument is a type of custom class, all of its custom method
13
- * would be erased and its prototype would be changed to the primitive `object`.
14
- * Therefore, return type of the TMP type finally be the primitive object.
15
- *
16
- * In addition, if the target argument is a type of custom class and it has a special
17
- * method `toJSON()`, return type of this `Primitive` would be not `Primitive<Instance>`
18
- * but `Primitive<ReturnType<Instance.toJSON>>`.
19
- *
20
- * Before | After
21
- * ------------------------|----------------------------------------
22
- * `Boolean` | `boolean`
23
- * `Number` | `number`
24
- * `String` | `string`
25
- * `Class` | `object`
26
- * `Class` with `toJSON()` | `Primitive<ReturnType<Class.toJSON>>`
27
- * Native Class | never
28
- * Others | No change
29
- *
30
- * @template Instance Target argument type.
31
- * @author Jeongho Nam - https://github.com/samchon
32
- * @author Kyungsu Kang - https://github.com/kakasoo
33
- * @author Michael - https://github.com/8471919
34
- */
35
- export type Primitive<T> =
36
- Equal<T, PrimitiveMain<T>> extends true ? T : PrimitiveMain<T>;
37
-
38
- type Equal<X, Y> = X extends Y ? (Y extends X ? true : false) : false;
39
-
40
- type PrimitiveMain<Instance> = Instance extends [never]
41
- ? never // (special trick for jsonable | null) type
42
- : ValueOf<Instance> extends bigint
43
- ? never
44
- : ValueOf<Instance> extends boolean | number | string
45
- ? ValueOf<Instance>
46
- : Instance extends Function
47
- ? never
48
- : ValueOf<Instance> extends object
49
- ? Instance extends object
50
- ? Instance extends NativeClass
51
- ? never
52
- : Instance extends IJsonable<infer Raw>
53
- ? ValueOf<Raw> extends object
54
- ? Raw extends object
55
- ? PrimitiveObject<Raw> // object would be primitified
56
- : never // cannot be
57
- : ValueOf<Raw> // atomic value
58
- : PrimitiveObject<Instance> // object would be primitified
59
- : never // cannot be
60
- : ValueOf<Instance>;
61
-
62
- type PrimitiveObject<Instance extends object> =
63
- Instance extends Array<infer T>
64
- ? IsTuple<Instance> extends true
65
- ? PrimitiveTuple<Instance>
66
- : PrimitiveMain<T>[]
67
- : {
68
- [P in keyof Instance]: PrimitiveMain<Instance[P]>;
69
- };
70
-
71
- type PrimitiveTuple<T extends readonly any[]> = T extends []
72
- ? []
73
- : T extends [infer F]
74
- ? [PrimitiveMain<F>]
75
- : T extends [infer F, ...infer Rest extends readonly any[]]
76
- ? [PrimitiveMain<F>, ...PrimitiveTuple<Rest>]
77
- : T extends [(infer F)?]
78
- ? [PrimitiveMain<F>?]
79
- : T extends [(infer F)?, ...infer Rest extends readonly any[]]
80
- ? [PrimitiveMain<F>?, ...PrimitiveTuple<Rest>]
81
- : [];
82
-
83
- type ValueOf<Instance> =
84
- IsValueOf<Instance, Boolean> extends true
85
- ? boolean
86
- : IsValueOf<Instance, Number> extends true
87
- ? number
88
- : IsValueOf<Instance, String> extends true
89
- ? string
90
- : Instance;
91
-
92
- type NativeClass =
93
- | Set<any>
94
- | Map<any, any>
95
- | WeakSet<any>
96
- | WeakMap<any, any>
97
- | Uint8Array
98
- | Uint8ClampedArray
99
- | Uint16Array
100
- | Uint32Array
101
- | BigUint64Array
102
- | Int8Array
103
- | Int16Array
104
- | Int32Array
105
- | BigInt64Array
106
- | Float32Array
107
- | Float64Array
108
- | ArrayBuffer
109
- | SharedArrayBuffer
110
- | DataView;
111
-
112
- type IsTuple<T extends readonly any[] | { length: number }> = [T] extends [
113
- never,
114
- ]
115
- ? false
116
- : T extends readonly any[]
117
- ? number extends T["length"]
118
- ? false
119
- : true
120
- : false;
121
-
122
- type IsValueOf<Instance, Object extends IValueOf<any>> = Instance extends Object
123
- ? Object extends IValueOf<infer U>
124
- ? Instance extends U
125
- ? false
126
- : true // not Primitive, but Object
127
- : false // cannot be
128
- : false;
129
-
130
- interface IValueOf<T> {
131
- valueOf(): T;
132
- }
133
-
134
- interface IJsonable<T> {
135
- toJSON(): T;
136
- }
1
+ /**
2
+ * Primitive type of JSON.
3
+ *
4
+ * `Primitive<T>` is a TMP (Type Meta Programming) type which converts
5
+ * its argument as a primitive type within framework JSON.
6
+ *
7
+ * If the target argument is a built-in class which returns its origin primitive type
8
+ * through the `valueOf()` method like the `String` or `Number`, its return type would
9
+ * be the `string` or `number`. Otherwise, the built-in class does not have the
10
+ * `valueOf()` method, the return type would be an empty object (`{}`).
11
+ *
12
+ * Otherwise, the target argument is a type of custom class, all of its custom method
13
+ * would be erased and its prototype would be changed to the primitive `object`.
14
+ * Therefore, return type of the TMP type finally be the primitive object.
15
+ *
16
+ * In addition, if the target argument is a type of custom class and it has a special
17
+ * method `toJSON()`, return type of this `Primitive` would be not `Primitive<Instance>`
18
+ * but `Primitive<ReturnType<Instance.toJSON>>`.
19
+ *
20
+ * Before | After
21
+ * ------------------------|----------------------------------------
22
+ * `Boolean` | `boolean`
23
+ * `Number` | `number`
24
+ * `String` | `string`
25
+ * `Class` | `object`
26
+ * `Class` with `toJSON()` | `Primitive<ReturnType<Class.toJSON>>`
27
+ * Native Class | never
28
+ * Others | No change
29
+ *
30
+ * @template Instance Target argument type.
31
+ * @author Jeongho Nam - https://github.com/samchon
32
+ * @author Kyungsu Kang - https://github.com/kakasoo
33
+ * @author Michael - https://github.com/8471919
34
+ */
35
+ export type Primitive<T> =
36
+ Equal<T, PrimitiveMain<T>> extends true ? T : PrimitiveMain<T>;
37
+
38
+ type Equal<X, Y> = X extends Y ? (Y extends X ? true : false) : false;
39
+
40
+ type PrimitiveMain<Instance> = Instance extends [never]
41
+ ? never // (special trick for jsonable | null) type
42
+ : ValueOf<Instance> extends bigint
43
+ ? never
44
+ : ValueOf<Instance> extends boolean | number | string
45
+ ? ValueOf<Instance>
46
+ : Instance extends Function
47
+ ? never
48
+ : ValueOf<Instance> extends object
49
+ ? Instance extends object
50
+ ? Instance extends NativeClass
51
+ ? never
52
+ : Instance extends IJsonable<infer Raw>
53
+ ? ValueOf<Raw> extends object
54
+ ? Raw extends object
55
+ ? PrimitiveObject<Raw> // object would be primitified
56
+ : never // cannot be
57
+ : ValueOf<Raw> // atomic value
58
+ : PrimitiveObject<Instance> // object would be primitified
59
+ : never // cannot be
60
+ : ValueOf<Instance>;
61
+
62
+ type PrimitiveObject<Instance extends object> =
63
+ Instance extends Array<infer T>
64
+ ? IsTuple<Instance> extends true
65
+ ? PrimitiveTuple<Instance>
66
+ : PrimitiveMain<T>[]
67
+ : {
68
+ [P in keyof Instance]: PrimitiveMain<Instance[P]>;
69
+ };
70
+
71
+ type PrimitiveTuple<T extends readonly any[]> = T extends []
72
+ ? []
73
+ : T extends [infer F]
74
+ ? [PrimitiveMain<F>]
75
+ : T extends [infer F, ...infer Rest extends readonly any[]]
76
+ ? [PrimitiveMain<F>, ...PrimitiveTuple<Rest>]
77
+ : T extends [(infer F)?]
78
+ ? [PrimitiveMain<F>?]
79
+ : T extends [(infer F)?, ...infer Rest extends readonly any[]]
80
+ ? [PrimitiveMain<F>?, ...PrimitiveTuple<Rest>]
81
+ : [];
82
+
83
+ type ValueOf<Instance> =
84
+ IsValueOf<Instance, Boolean> extends true
85
+ ? boolean
86
+ : IsValueOf<Instance, Number> extends true
87
+ ? number
88
+ : IsValueOf<Instance, String> extends true
89
+ ? string
90
+ : Instance;
91
+
92
+ type NativeClass =
93
+ | Set<any>
94
+ | Map<any, any>
95
+ | WeakSet<any>
96
+ | WeakMap<any, any>
97
+ | Uint8Array
98
+ | Uint8ClampedArray
99
+ | Uint16Array
100
+ | Uint32Array
101
+ | BigUint64Array
102
+ | Int8Array
103
+ | Int16Array
104
+ | Int32Array
105
+ | BigInt64Array
106
+ | Float32Array
107
+ | Float64Array
108
+ | ArrayBuffer
109
+ | SharedArrayBuffer
110
+ | DataView;
111
+
112
+ type IsTuple<T extends readonly any[] | { length: number }> = [T] extends [
113
+ never,
114
+ ]
115
+ ? false
116
+ : T extends readonly any[]
117
+ ? number extends T["length"]
118
+ ? false
119
+ : true
120
+ : false;
121
+
122
+ type IsValueOf<Instance, Object extends IValueOf<any>> = Instance extends Object
123
+ ? Object extends IValueOf<infer U>
124
+ ? Instance extends U
125
+ ? false
126
+ : true // not Primitive, but Object
127
+ : false // cannot be
128
+ : false;
129
+
130
+ interface IValueOf<T> {
131
+ valueOf(): T;
132
+ }
133
+
134
+ interface IJsonable<T> {
135
+ toJSON(): T;
136
+ }