@nestia/core 0.1.6 → 0.1.7

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 (3) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +336 -0
  3. package/package.json +1 -1
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2020 Jeongho Nam
3
+ Copyright (c) 2022 Jeongho Nam
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md ADDED
@@ -0,0 +1,336 @@
1
+ # Nestia Core
2
+ [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/samchon/nestia/blob/master/LICENSE)
3
+ [![npm version](https://img.shields.io/npm/v/@nestia/core.svg)](https://www.npmjs.com/package/@nestia/core)
4
+ [![Downloads](https://img.shields.io/npm/dm/typia.svg)](https://www.npmjs.com/package/@nestia/core)
5
+ [![Build Status](https://github.com/samchon/typia/workflows/build/badge.svg)](https://github.com/samchon/nestia/actions?query=workflow%3Abuild)
6
+ [![Guide Documents](https://img.shields.io/badge/wiki-documentation-forestgreen)](https://github.com/samchon/nestia/wiki)
7
+
8
+ ```bash
9
+ npx nestia setup
10
+ ```
11
+
12
+ Super-easy and super-fast validator decorators for NestJS.
13
+
14
+ `@nestia/core` is a transformer library of NestJS, supporing super-easy and super-fast validation decorators, by using [typia](https://github.com/samchon/typia). Comparing validation speed with `class-validator`, `@nestia/core` is maximum **15,000x times faster** and it even supports every TypeScript types.
15
+
16
+ Furthremore, `@nestia/core` can use pure interface typed DTO with **only one line**. 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 **super-easy** and **super-fast** through with pure TypeScript types.
17
+
18
+ ```typescript
19
+ import { Controller } from "@nestjs/common";
20
+ import { TypedBody, TypedRoute } from "@nestia/core";
21
+
22
+ import { IBbsArticle } from "@bbs-api/structures/IBbsArticle";
23
+
24
+ @Controller("bbs/articles")
25
+ export class BbsArticlesController {
26
+ /**
27
+ * `TypedRoute.Post()`: safe `JSON.stringify()` with type validation.
28
+ *
29
+ * Furthermore, its 10x times faster than native `JSON.stringify()` function.
30
+ */
31
+ @TypedRoute.Post()
32
+ public async store(
33
+ /**
34
+ * Super-fast request body validator through `TypedBody()`.
35
+ *
36
+ * It also requires only one line.
37
+ */
38
+ @TypedBody() input: IBbsArticle.IStore
39
+ ): Promise<IBbsArticle>;
40
+ }
41
+ ```
42
+
43
+ ```typescript
44
+ /**
45
+ * You don't need any extra dedication like:
46
+ *
47
+ * - `@nestjs/swagger`: JSON schema definition
48
+ * - `class-transformer`: class definition with decorator function class
49
+ *
50
+ * Just enjoy the pure interface type as DTO
51
+ */
52
+ export interface IBbsArticle {
53
+ /**
54
+ * @format uuid
55
+ */
56
+ id: string;
57
+ writer: string;
58
+ title: string;
59
+ content: string;
60
+ created_at: string;
61
+ }
62
+ export namespace IBbsArticle {
63
+ export interface IStore {
64
+ writer: string;
65
+ title: string;
66
+ content: string;
67
+ }
68
+ }
69
+ ```
70
+
71
+ ![Benchmark](https://github.com/samchon/typia/raw/master/benchmark/results/11th%20Gen%20Intel(R)%20Core(TM)%20i5-1135G7%20%40%202.40GHz/images/is.svg)
72
+
73
+ > 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)
74
+ >
75
+ > `@nestia/core` is providing super-easy and super-fast validator by wrapping [typia](https://github.com/samchon/typia)
76
+
77
+
78
+
79
+
80
+
81
+ ## Setup
82
+ ### Setup Wizard
83
+ ```bash
84
+ # setup both @nestia/core and @nestia/sdk
85
+ npx nestia setup
86
+
87
+ # setup @nestia/core only
88
+ npx nestia setup
89
+ ```
90
+
91
+ 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.
92
+
93
+ 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:
94
+
95
+ ```bash
96
+ npx ttsc
97
+ npx ts-node -C ttypescript src/index.ts
98
+ ```
99
+
100
+ > In the automated setup process, you can specialize package manager like `yarn` instead of `npm` by adding `--module yarn` argument. You also can specialize transformation compiler by using [`--module ts-patch`](https://github.com/nonara/ts-patch) argument. Default compiler is [`ttypescrpit`](https://github.com/cevek/ttypescript).
101
+ >
102
+ > ```bash
103
+ > npx nestia setup \
104
+ > --compiler (ttypescript|ts-patch)
105
+ > --module (npm|pnpm|yarn)
106
+ >
107
+ > npx nestia setup
108
+ > npx nestia setup --module yarn
109
+ > npx nestia setup --compiler ts-patch
110
+ > ```
111
+
112
+ ### NPM Packages
113
+ If you want to install and configure manually, install `@nestia/core` module first.
114
+
115
+ 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`.
116
+
117
+ ```bash
118
+ npm install --save @nestia/core
119
+
120
+ # ENSURE THOSE PACKAGES ARE INSTALLED
121
+ npm install --save-dev typescript
122
+ npm install --save-dev ttypescript
123
+ npm install --save-dev ts-node
124
+ ```
125
+
126
+ ### `tsconfig.json`
127
+ After the installation, you've to configure `tsconfig.json` file like below.
128
+
129
+ Add a property transform and its value as `@nestia/core/lib/transform` into `compilerOptions.plugins` array. Also, do same thing on `typia/lib/transform` value. When configuring, I recommend you to use the strict option, to enforce developers to distinguish whether each property is nullable or undefindable.
130
+
131
+ ```json
132
+ {
133
+ "compilerOptions": {
134
+ "strict": true,
135
+ "plugins": [
136
+ {
137
+ "transform": "@nestia/core/lib/transform",
138
+ // "validate": "assert", // "assert" | "is" | "validate"
139
+ // "stringify": "is", // null | "stringify" | "assert" | "is" | "validate"
140
+ },
141
+ { "transform": "typia/lib/transform" }
142
+ ]
143
+ }
144
+ }
145
+ ```
146
+
147
+ Also, you can configure additional properties like `validate` and `stringify`.
148
+
149
+ Through the `validate` property, you can specialize which validation algorithm of [typia](https://github.com/samchon/typia) to be used. Default is `assert` function and if you choose `is` function instead, the validation speed would be extremely faster, but any reason why would be provided when wrong typed data comes. Otherwise you select `validate` function, its validation speed would be slower, but most detailed reasons would be provided.
150
+
151
+ By specializing `stringify` property, you can specialize which JSON stringify function of [typia](https://github.com/samchon/typia) would be used. Default is `assert`, but if choose `null` instead, it would be replaced to `JSON.stringify()` function. Otherwise you configure it as `stringify`, fastest logic would be used, but unexpected behavior would be happend when wrong typed data comes.
152
+
153
+ ```typescript
154
+ // RUNTIME VALIDATORS
155
+ export function is<T>(input: unknown | T): boolean; // returns boolean
156
+ export function assert<T>(input: unknown | T): T; // throws TypeGuardError
157
+ export function validate<T>(input: unknown | T): IValidation<T>; // detailed
158
+
159
+ // FAST STRINGIFY FUNCTIONS
160
+ export function stringify<T>(input: T): string; // unsafe, but very fast
161
+ export function assertStringify<T>(input: T): string; // assert + stringify
162
+ export function isStringify<T>(input: T): string | null; // is + stringify
163
+ export function validateStringify<T>(input: T): IValidation<T>; // validate +
164
+ ```
165
+
166
+
167
+
168
+
169
+ ## Features
170
+ ```typescript
171
+ import { Controller } from "@nestjs/common";
172
+ import { TypedBody, TypedRoute } from "@nestia/core";
173
+
174
+ import { IBbsArticle } from "@bbs-api/structures/IBbsArticle";
175
+
176
+ @Controller("bbs/articles")
177
+ export class BbsArticlesController {
178
+ /**
179
+ * `TypedRoute.Post()`: safe `JSON.stringify()` with type validation.
180
+ *
181
+ * Furthermore, its 10x times faster than native `JSON.stringify()` function.
182
+ */
183
+ @TypedRoute.Post()
184
+ public async store(
185
+ /**
186
+ * Super-fast request body validator through `TypedBody()`.
187
+ *
188
+ * It also requires only one line.
189
+ */
190
+ @TypedBody() input: IBbsArticle.IStore
191
+ ): Promise<IBbsArticle>;
192
+ }
193
+ ```
194
+
195
+ ### TypedBody
196
+ `TypedBody` is a decorator function for `application/json` typed request body.
197
+
198
+ Also, it supports super-fast validation pipe, which is 15,000x times faster then ordinary `nest.Body()` decorator using `class-validator`.
199
+
200
+ ### TypedRoute
201
+ `TypedRoute` is a decorator function for `application/json` typed reponse body.
202
+
203
+ Also, it supports safe and fast JSON stringify pipe, which is maximum 10x times faster than native `JSON.stringify()` function and it is even type safe.
204
+
205
+ ### Encryption
206
+ `@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.
207
+
208
+ - AES-128/256
209
+ - CBC mode
210
+ - PKCS #5 Padding
211
+ - Base64 Encoding
212
+
213
+
214
+
215
+
216
+
217
+ ## Appendix
218
+ ### Nestia SDK
219
+ ```bash
220
+ npx nestia swagger
221
+ npx nestia sdk
222
+ ```
223
+
224
+ When you adapt this `@nestia/core`, you can't use `@nestjs/swagger` more. Instead, I support `@nestia/sdk`, which is much more stable and powerful then before. Through this `@nestia/sdk` module, you can generate `swagger.json` and even generating SDK library is possible.
225
+
226
+ For reference, SDK (Software Development Kit) library means a library which can be utilized by TypeScript client developer conveniently. When you generate SDK library through `@nestia/sdk`, `@nestia/sdk` will analyze your backend server code and generate codes like below:
227
+
228
+ #### `BbsArticlesController.ts`
229
+ ```typescript
230
+ import { Controller } from "@nestjs/common";
231
+ import { TypedBody, TypedRoute } from "@nestia/core";
232
+
233
+ import { IBbsArticle } from "@bbs-api/structures/IBbsArticle";
234
+
235
+ @Controller("bbs/articles")
236
+ export class BbsArticlesController {
237
+ /**
238
+ * `TypedRoute.Post()`: safe `JSON.stringify()` with type validation.
239
+ *
240
+ * Furthermore, its 10x times faster than native `JSON.stringify()` function.
241
+ */
242
+ @TypedRoute.Post()
243
+ public async store(
244
+ /**
245
+ * Super-fast request body validator through `TypedBody()`.
246
+ *
247
+ * It also requires only one line.
248
+ */
249
+ @TypedBody() input: IBbsArticle.IStore
250
+ ): Promise<IBbsArticle>;
251
+ }
252
+ ```
253
+
254
+ #### `src/functional/bbs/articles/index.ts`
255
+ ```typescript
256
+ import { Fetcher, IConnection } from "@nestia/fetcher";
257
+ import { IBbsArticle } from "../../../structures/IBbsArticle";
258
+
259
+ export function store(
260
+ connection: api.IConnection,
261
+ input: IBbsArticle.IStore
262
+ ): Promise<IBbsArticle> {
263
+ return Fetcher.fetch(
264
+ connection,
265
+ store.ENCRYPTED,
266
+ store.METHOD,
267
+ store.path(),
268
+ input
269
+ );
270
+ }
271
+ export namespace store {
272
+ export const METHOD = "POST" as const;
273
+ export function path(): string {
274
+ return "/bbs/articles";
275
+ }
276
+ }
277
+ ```
278
+
279
+ #### SDK utilization code
280
+ ```typescript
281
+ import api from "@bbs-api";
282
+ import typia from "typia";
283
+
284
+ export async function test_bbs_article_store(connection: api.IConnection) {
285
+ const article: IBbsArticle = await api.functional.bbs.articles.store(
286
+ connection,
287
+ {
288
+ name: "John Doe",
289
+ title: "some title",
290
+ content: "some content",
291
+ }
292
+ );
293
+ typia.assert(article);
294
+ console.log(article);
295
+ }
296
+ ```
297
+
298
+ ### Typia
299
+ > https://github.com/samchon/typia
300
+ >
301
+ > `@nestia/core` is wrapping `typia` and the `typia` is:
302
+
303
+ [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/samchon/typia/blob/master/LICENSE)
304
+ [![npm version](https://img.shields.io/npm/v/typia.svg)](https://www.npmjs.com/package/typia)
305
+ [![Downloads](https://img.shields.io/npm/dm/typia.svg)](https://www.npmjs.com/package/typia)
306
+ [![Build Status](https://github.com/samchon/typia/workflows/build/badge.svg)](https://github.com/samchon/typia/actions?query=workflow%3Abuild)
307
+ [![Guide Documents](https://img.shields.io/badge/wiki-documentation-forestgreen)](https://github.com/samchon/typia/wiki)
308
+
309
+ ```typescript
310
+ // RUNTIME VALIDATORS
311
+ export function is<T>(input: unknown | T): input is T; // returns boolean
312
+ export function assert<T>(input: unknown | T): T; // throws TypeGuardError
313
+ export function validate<T>(input: unknown | T): IValidation<T>; // detailed
314
+
315
+ // STRICT VALIDATORS
316
+ export function equals<T>(input: unknown | T): input is T;
317
+ export function assertEquals<T>(input: unknown | T): T;
318
+ export function validateEquals<T>(input: unknown | T): IValidation<T>;
319
+
320
+ // JSON
321
+ export function application<T>(): IJsonApplication; // JSON schema
322
+ export function assertParse<T>(input: string): T; // type safe parser
323
+ export function assertStringify<T>(input: T): string; // safe and faster
324
+ // +) isParse, validateParse
325
+ // +) stringify, isStringify, validateStringify
326
+ ```
327
+
328
+ `typia` is a transformer library of TypeScript, supporting below features:
329
+
330
+ - Super-fast Runtime Validators
331
+ - Safe JSON parse and fast stringify functions
332
+ - JSON schema generator
333
+
334
+ All functions in `typia` require **only one line**. You don't need any extra dedication like JSON schema definitions or decorator function calls. Just call `typia` function with only one line like `typia.assert<T>(input)`.
335
+
336
+ Also, as `typia` performs AOT (Ahead of Time) compilation skill, its performance is much faster than other competitive libaries. For an example, when comparing validate function `is()` with other competitive libraries, `typia` is maximum **15,000x times faster** than `class-validator`.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nestia/core",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "description": "Super-fast validation decorators of NestJS",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",