You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: readme.md
+29-56Lines changed: 29 additions & 56 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -32,17 +32,16 @@ Luckily, TachiJS tackles those problems. If you have other ideas, please create
32
32
### Install tachijs
33
33
34
34
```sh
35
-
npm i tachijs reflect-metadata
35
+
npm i tachijs
36
36
```
37
37
38
-
Add two compiler options, `experimentalDecorators`and `emitDecoratorMetadata`, to `tsconfig.json`.
38
+
Enable `experimentalDecorators`of `compilerOptions` to `tsconfig.json`.
39
39
40
40
```json
41
41
{
42
42
"compilerOptions": {
43
43
...
44
44
"experimentalDecorators": true,
45
-
"emitDecoratorMetadata": true,
46
45
...
47
46
}
48
47
}
@@ -261,43 +260,7 @@ We also provide `reqHeaders`, `reqCookies` and `reqSession` for `req.headers`, `
261
260
262
261
#### Body validation
263
262
264
-
`@reqBody` supports validation via `class-validator`.
265
-
266
-
Please install `class-validator` package first.
267
-
268
-
```sh
269
-
npm install class-validator
270
-
```
271
-
272
-
```ts
273
-
import { IsString } from'class-validator'
274
-
275
-
classPostDTO {
276
-
@IsString()
277
-
title:string
278
-
279
-
@IsString()
280
-
content:string
281
-
}
282
-
283
-
284
-
@controller('/posts')
285
-
classPostController() {
286
-
@httpPost('/')
287
-
// Tachijs can access `PostDTO` via reflect-metadata.
288
-
async create(@reqBody() body:PostDTO) {
289
-
// `body` is already validated and transformed into an instance of `PostDTO`.
290
-
// So we don't need any extra validation.
291
-
const post =awaitPost.create({
292
-
...body
293
-
})
294
-
295
-
return {
296
-
post
297
-
}
298
-
}
299
-
}
300
-
```
263
+
It has been deprecated from v1. We'll provide this feature as a separated module.
301
264
302
265
#### Custom parameter decorators!
303
266
@@ -355,27 +318,28 @@ export function cookieSetter() {
355
318
}
356
319
```
357
320
358
-
##### `design:paramtype`
321
+
##### `meta.paramType`
359
322
360
-
Moreover, tachijs exposes metadata of parameters to forth argument. So you can make your custom validator for query with `class-transformer-validator` like below. (`req.body`is also using this.)
323
+
If you are using `reflect-metadata`, tachijs exposes `paramType`to the forth argument, `meta`, of `handlerParam` from `design:paramtypes`. With this feature, you could access argument types on runtime. The below example is validating and transforming query with DTO class by class-validator.
//It is `Object` if the param type is unknown or any.
333
+
//Now tachijs will expose `paramType`.
334
+
//If the param type is unknown or any, paramType will become `Object`.
371
335
returnmeta.paramType!==Object
372
336
?transformAndValidate(meta.paramType, req.query)
373
337
:req.query
374
338
})
375
339
}
376
340
377
341
// Validator class
378
-
classSearchQuery {
342
+
classSearchQueryDTO {
379
343
@IsString()
380
344
title:string
381
345
}
@@ -384,10 +348,10 @@ class SearchQuery {
384
348
classPostController {
385
349
@httpGet('/search')
386
350
// Provide the validator class to param type.
387
-
// tachijs can access it via `reflect-metadata`.
388
-
search(@validatedQuery() query:SearchQuery) {
351
+
search(@validatedQuery() query:SearchQueryDTO) {
389
352
// Now `query` is type-safe
390
-
// because it has been validated and transformed into an instance of SearchQuery.
353
+
// because it has been validated and transformed into an instance of SearchQueryDTO.
354
+
// If validation errors happen, tachijs will pass the error into `next` so you can handle it easily by your error request handler.
391
355
const { title } =query
392
356
393
357
return {
@@ -397,6 +361,20 @@ class PostController {
397
361
}
398
362
```
399
363
364
+
To enable it, you have to install `reflect-metadata` and to apply `emitDecoratorMetadata` of `compilerOptions` to `tsconfig.json`.
365
+
366
+
```sh
367
+
npm i reflect-metadata
368
+
```
369
+
370
+
```json
371
+
{
372
+
"compilerOptions": {
373
+
"emitDecoratorMetadata": true
374
+
}
375
+
}
376
+
```
377
+
400
378
To know more, see `@handlerParam` api documentation below.
401
379
402
380
### Redirection, Rendering via pug and others...
@@ -513,8 +491,6 @@ class HomeController {
513
491
}
514
492
```
515
493
516
-
> `#httpContext`, `#inject` and `#injector` will be deprecated from v1.0.0. Please use `#context`
517
-
518
494
#### Customize result
519
495
520
496
If you want to have customized result behavior, you can do it with `BaseResult`.
@@ -854,28 +830,25 @@ export type HandlerParamSelector<T> = (
854
830
interfaceHandlerParamMeta<T> {
855
831
index:number
856
832
selector:HandlerParamSelector<T>
857
-
paramType:any
833
+
paramType?:any
858
834
}
859
835
```
860
836
861
837
-`index` Number index of the parameter.
862
838
-`selector` Its selector.
863
839
-`paramType` metadata from `design:paramtypes`.
864
840
865
-
#### `@reqBody(validator?: any)`
841
+
#### `@reqBody()`
866
842
867
843
Inject `req.body`.
868
844
869
-
-`validator` Optional. A class with decorators of `class-validator`. tachijs will validate `req.body` with it and transform `req.body` into the validator class. If `validator` is not given but the parameter has a class validator as its param type, tachijs will use it via `reflect-metadata`.
0 commit comments