@nestia/core 0.1.8 → 0.2.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.
- package/README.md +95 -84
- package/package.json +5 -1
package/README.md
CHANGED
|
@@ -1,21 +1,18 @@
|
|
|
1
1
|
# Nestia Core
|
|
2
2
|
[](https://github.com/samchon/nestia/blob/master/LICENSE)
|
|
3
3
|
[](https://www.npmjs.com/package/@nestia/core)
|
|
4
|
-
[](https://www.npmjs.com/package/@nestia/core)
|
|
5
5
|
[](https://github.com/samchon/nestia/actions?query=workflow%3Abuild)
|
|
6
6
|
[](https://github.com/samchon/nestia/wiki)
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
npx nestia setup
|
|
10
|
-
```
|
|
8
|
+
Super-fast validation decorators for NestJS.
|
|
11
9
|
|
|
12
|
-
|
|
10
|
+
`@nestia/core` is a transformer library of NestJS, supporting super-fast validation decorators, by wrapping [typia](https://github.com/samchon/typia). Comparing validation speed with `class-validator`, `typia` is maximum **15,000x times faster** and it is even much safer.
|
|
13
11
|
|
|
14
|
-
`@nestia/core`
|
|
12
|
+
Furthermore, `@nestia/core` can use pure interface typed DTO with **only one line**.
|
|
15
13
|
|
|
16
|
-
|
|
14
|
+
Therefore, it does not require any extra dedication like defining JSON schema (`@nestjs/swagger`), or using class definition with decorator function calls (`class-validator`). Just enjoy the superfast decorators with pure TypeScript type.
|
|
17
15
|
|
|
18
|
-
#### `BbsArticlesController.ts`
|
|
19
16
|
```typescript
|
|
20
17
|
import { Controller } from "@nestjs/common";
|
|
21
18
|
import { TypedBody, TypedRoute } from "@nestia/core";
|
|
@@ -25,92 +22,59 @@ import { IBbsArticle } from "@bbs-api/structures/IBbsArticle";
|
|
|
25
22
|
@Controller("bbs/articles")
|
|
26
23
|
export class BbsArticlesController {
|
|
27
24
|
/**
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
25
|
+
* Store a new content.
|
|
26
|
+
*
|
|
27
|
+
* @param inupt Content to store
|
|
28
|
+
* @returns Newly archived article
|
|
31
29
|
*/
|
|
32
|
-
@TypedRoute.Post()
|
|
30
|
+
@TypedRoute.Post() // 10x faster and safer JSON.stringify()
|
|
33
31
|
public async store(
|
|
34
|
-
|
|
35
|
-
* Super-fast request body validator through `TypedBody()`.
|
|
36
|
-
*
|
|
37
|
-
* It also requires only one line.
|
|
38
|
-
*/
|
|
39
|
-
@TypedBody() input: IBbsArticle.IStore
|
|
32
|
+
@TypedBody() input: IBbsArticle.IStore // supoer-fast validator
|
|
40
33
|
): Promise<IBbsArticle>;
|
|
41
34
|
}
|
|
42
35
|
```
|
|
43
36
|
|
|
44
|
-
#### `IBbsArticle.ts`
|
|
45
|
-
```typescript
|
|
46
|
-
/**
|
|
47
|
-
* You don't need any extra dedication like:
|
|
48
|
-
*
|
|
49
|
-
* - `@nestjs/swagger`: JSON schema definition
|
|
50
|
-
* - `class-transformer`: class definition with decorator function class
|
|
51
|
-
*
|
|
52
|
-
* Just enjoy the pure interface type as DTO
|
|
53
|
-
*/
|
|
54
|
-
export interface IBbsArticle {
|
|
55
|
-
/**
|
|
56
|
-
* @format uuid
|
|
57
|
-
*/
|
|
58
|
-
id: string;
|
|
59
|
-
writer: string;
|
|
60
|
-
title: string;
|
|
61
|
-
content: string;
|
|
62
|
-
created_at: string;
|
|
63
|
-
}
|
|
64
|
-
export namespace IBbsArticle {
|
|
65
|
-
export interface IStore {
|
|
66
|
-
writer: string;
|
|
67
|
-
title: string;
|
|
68
|
-
content: string;
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
```
|
|
72
|
-
|
|
73
|
-
#### `typia` vs. `class-validator`
|
|
74
|
-
%20Core(TM)%20i5-1135G7%20%40%202.40GHz/images/is.svg)
|
|
75
|
-
|
|
76
|
-
> Measured on [Intel i5-1135g7, Surface Pro 8](https://github.com/samchon/typia/tree/master/benchmark/results/11th%20Gen%20Intel(R)%20Core(TM)%20i5-1135G7%20%40%202.40GHz#is)
|
|
77
37
|
|
|
78
38
|
|
|
79
39
|
|
|
40
|
+
## Setup
|
|
41
|
+
### Boilerplate Project
|
|
42
|
+
```bash
|
|
43
|
+
npx nestia start <directory>
|
|
44
|
+
```
|
|
80
45
|
|
|
46
|
+
Just run above command, then boilerplate project would be constructed.
|
|
81
47
|
|
|
82
|
-
## Setup
|
|
83
48
|
### Setup Wizard
|
|
84
49
|
```bash
|
|
85
50
|
# setup both @nestia/core and @nestia/sdk
|
|
86
51
|
npx nestia setup
|
|
87
52
|
|
|
88
53
|
# setup @nestia/core only
|
|
89
|
-
npx nestia setup
|
|
54
|
+
npx @nestia/core setup
|
|
90
55
|
```
|
|
91
56
|
|
|
92
57
|
When you run `npx nestia setup` command, all installation and configuration processes would be automatically done. If you want to setup `@nestia/core` only, run `npx @nestia/core setup` command instead.
|
|
93
58
|
|
|
94
|
-
After the setup has been completed, you can compile your backend server code by using `ttsc` command. If you want to run your TypeScript file directly through `ts-node`, add `-C ttypescript` argument like below:
|
|
59
|
+
After the setup has been fully completed, you can compile your backend server code by using `ttsc` command. If you want to run your TypeScript file directly through `ts-node`, add `-C ttypescript` argument like below:
|
|
95
60
|
|
|
96
61
|
```bash
|
|
97
62
|
npx ttsc
|
|
98
63
|
npx ts-node -C ttypescript src/index.ts
|
|
99
64
|
```
|
|
100
65
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
### NPM Packages
|
|
66
|
+
Also, you can specify package manager by `--manage` argument like below:
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
npx @nestia/core setup --manager npm
|
|
70
|
+
npx @nestia/core setup --manager pnpm
|
|
71
|
+
npx @nestia/core setup --manager yarn
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Manual Setup
|
|
75
|
+
If you want to install and configure `@nestia/core` manually, read [Guide Documents - Setup](https://github.com/samchon/nestia/wiki/Setup).
|
|
76
|
+
|
|
77
|
+
<!-- ### NPM Packages
|
|
114
78
|
If you want to install and configure manually, install `@nestia/core` module first.
|
|
115
79
|
|
|
116
80
|
Also, you need additional devDependencies to compile the TypeScript code with transformation. Therefore, install those all libraries `typescript`, `ttypescript` and `ts-node`. Inform that, `ttypescript` is not mis-writing. Do not forget to install the `ttypescript`.
|
|
@@ -162,7 +126,7 @@ export function stringify<T>(input: T): string; // unsafe, but very fast
|
|
|
162
126
|
export function assertStringify<T>(input: T): string; // assert + stringify
|
|
163
127
|
export function isStringify<T>(input: T): string | null; // is + stringify
|
|
164
128
|
export function validateStringify<T>(input: T): IValidation<T>; // validate +
|
|
165
|
-
```
|
|
129
|
+
``` -->
|
|
166
130
|
|
|
167
131
|
|
|
168
132
|
|
|
@@ -177,31 +141,33 @@ import { IBbsArticle } from "@bbs-api/structures/IBbsArticle";
|
|
|
177
141
|
@Controller("bbs/articles")
|
|
178
142
|
export class BbsArticlesController {
|
|
179
143
|
/**
|
|
180
|
-
*
|
|
181
|
-
*
|
|
182
|
-
*
|
|
144
|
+
* Store a new content.
|
|
145
|
+
*
|
|
146
|
+
* @param inupt Content to store
|
|
147
|
+
* @returns Newly archived article
|
|
183
148
|
*/
|
|
184
|
-
@TypedRoute.Post()
|
|
149
|
+
@TypedRoute.Post() // 10x faster and safer JSON.stringify()
|
|
185
150
|
public async store(
|
|
186
|
-
|
|
187
|
-
* Super-fast request body validator through `TypedBody()`.
|
|
188
|
-
*
|
|
189
|
-
* It also requires only one line.
|
|
190
|
-
*/
|
|
191
|
-
@TypedBody() input: IBbsArticle.IStore
|
|
151
|
+
@TypedBody() input: IBbsArticle.IStore // supoer-fast validator
|
|
192
152
|
): Promise<IBbsArticle>;
|
|
193
153
|
}
|
|
194
154
|
```
|
|
195
155
|
|
|
196
156
|
### TypedBody
|
|
197
|
-
`TypedBody` is a decorator function
|
|
157
|
+
`TypedBody()` is a decorator function of `application/json` typed request body.
|
|
198
158
|
|
|
199
|
-
Also, it supports super-fast validation pipe, which is 15,000x times faster then
|
|
159
|
+
Also, it supports super-fast validation pipe, which is maximum **15,000x times faster** then `nest.Body()` function using `class-validator`.
|
|
200
160
|
|
|
201
161
|
### TypedRoute
|
|
202
|
-
`TypedRoute` is a decorator
|
|
162
|
+
`TypedRoute` is a set of decorator functions for `application/json` typed response body.
|
|
203
163
|
|
|
204
|
-
Also, it supports safe and fast JSON stringify pipe, which is maximum 10x times faster than native `JSON.stringify()` function
|
|
164
|
+
Also, it supports safe and fast JSON stringify function pipe, which is maximum 10x times faster than native `JSON.stringify()` function. Furthermore, it is **type safe** through validation.
|
|
165
|
+
|
|
166
|
+
- `TypedRoute.Get()`
|
|
167
|
+
- `TypedRoute.Post()`
|
|
168
|
+
- `TypedRoute.Put()`
|
|
169
|
+
- `TypedRoute.Patch()`
|
|
170
|
+
- `TypedRoute.Delete()`
|
|
205
171
|
|
|
206
172
|
### Encryption
|
|
207
173
|
`@nestia/core` supports special decorator functions `EncryptedBody` and `EncryptedRout`. They're almost same with [TypedBody](#typedbody) and [TypedRoute](#typedroute), but there's only one thing different - it encrypts JSON data through AES-128/256 algorithm.
|
|
@@ -211,6 +177,49 @@ Also, it supports safe and fast JSON stringify pipe, which is maximum 10x times
|
|
|
211
177
|
- PKCS #5 Padding
|
|
212
178
|
- Base64 Encoding
|
|
213
179
|
|
|
180
|
+
### Comment Tags
|
|
181
|
+
You can enhance DTO type validation by writing comment tags.
|
|
182
|
+
|
|
183
|
+
If you want to know about it detaily, visit [Guide Documents of typia](https://github.com/samchon/typia/wiki/Runtime-Validators#comment-tags).
|
|
184
|
+
|
|
185
|
+
```typescript
|
|
186
|
+
export interface IBbsArticle {
|
|
187
|
+
/**
|
|
188
|
+
* @format uuid
|
|
189
|
+
*/
|
|
190
|
+
id: string;
|
|
191
|
+
|
|
192
|
+
writer: IBbsArticle.IWriter;
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* @minItems 1
|
|
196
|
+
*/
|
|
197
|
+
contents: IBbsArticle.IContent[];
|
|
198
|
+
}
|
|
199
|
+
export namespace IBbsArticle {
|
|
200
|
+
export interface IWriter {
|
|
201
|
+
/**
|
|
202
|
+
* @minLength 3
|
|
203
|
+
*/
|
|
204
|
+
name: string;
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* @format email
|
|
208
|
+
*/
|
|
209
|
+
email: string;
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* @pattern ^0[0-9]{7,16}
|
|
213
|
+
*/
|
|
214
|
+
mobile: string;
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* @minimum 18
|
|
218
|
+
*/
|
|
219
|
+
age: number;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
```
|
|
214
223
|
|
|
215
224
|
|
|
216
225
|
|
|
@@ -222,9 +231,11 @@ npx nestia swagger
|
|
|
222
231
|
npx nestia sdk
|
|
223
232
|
```
|
|
224
233
|
|
|
225
|
-
|
|
234
|
+
Automatic *SDK* and *Swagger* generator for `@nestia/core`.
|
|
235
|
+
|
|
236
|
+
With `@nestia/core`, you can boost up validation speed maximum **15,000x times faster**. However, as `@nestjs/swagger` does not support `@nestia/core`, you can't generate swagger documents from `@nestjs/swagger` more.
|
|
226
237
|
|
|
227
|
-
|
|
238
|
+
Instead, I provide you `@nestia/sdk` module, which can generate not only swagger documents, but also SDK (Software Development Kit) library.
|
|
228
239
|
|
|
229
240
|
#### `BbsArticlesController.ts`
|
|
230
241
|
```typescript
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nestia/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Super-fast validation decorators of NestJS",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"typings": "lib/index.d.ts",
|
|
@@ -60,6 +60,10 @@
|
|
|
60
60
|
"rxjs": "*",
|
|
61
61
|
"typia": "^3.4.7"
|
|
62
62
|
},
|
|
63
|
+
"peerDependencies": {
|
|
64
|
+
"ttypescript": ">= 1.5.15",
|
|
65
|
+
"typescript": ">= 4.5.2"
|
|
66
|
+
},
|
|
63
67
|
"files": [
|
|
64
68
|
"README.md",
|
|
65
69
|
"LICENSE",
|