Skip to content

Commit 2f60790

Browse files
committed
Update readme.md
1 parent 96f3d32 commit 2f60790

1 file changed

Lines changed: 29 additions & 56 deletions

File tree

readme.md

Lines changed: 29 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,16 @@ Luckily, TachiJS tackles those problems. If you have other ideas, please create
3232
### Install tachijs
3333

3434
```sh
35-
npm i tachijs reflect-metadata
35+
npm i tachijs
3636
```
3737

38-
Add two compiler options, `experimentalDecorators` and `emitDecoratorMetadata`, to `tsconfig.json`.
38+
Enable `experimentalDecorators` of `compilerOptions` to `tsconfig.json`.
3939

4040
```json
4141
{
4242
"compilerOptions": {
4343
...
4444
"experimentalDecorators": true,
45-
"emitDecoratorMetadata": true,
4645
...
4746
}
4847
}
@@ -261,43 +260,7 @@ We also provide `reqHeaders`, `reqCookies` and `reqSession` for `req.headers`, `
261260

262261
#### Body validation
263262

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-
class PostDTO {
276-
@IsString()
277-
title: string
278-
279-
@IsString()
280-
content: string
281-
}
282-
283-
284-
@controller('/posts')
285-
class PostController() {
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 = await Post.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.
301264

302265
#### Custom parameter decorators!
303266

@@ -355,27 +318,28 @@ export function cookieSetter() {
355318
}
356319
```
357320

358-
##### `design:paramtype`
321+
##### `meta.paramType`
359322

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.
361324

362325
```ts
326+
import 'reflect-metadata'
363327
import { controller, httpGet, handlerParam } from 'tachijs'
364328
import { IsString } from 'class-validator'
365329
import { transformAndValidate } from 'class-transformer-validator'
366330

367331
function validatedQuery() {
368332
return handlerParam((req, res, next, meta) => {
369-
// meta.paramType is from `design:paramtypes`.
370-
// 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`.
371335
return meta.paramType !== Object
372336
? transformAndValidate(meta.paramType, req.query)
373337
: req.query
374338
})
375339
}
376340

377341
// Validator class
378-
class SearchQuery {
342+
class SearchQueryDTO {
379343
@IsString()
380344
title: string
381345
}
@@ -384,10 +348,10 @@ class SearchQuery {
384348
class PostController {
385349
@httpGet('/search')
386350
// 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) {
389352
// 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.
391355
const { title } = query
392356

393357
return {
@@ -397,6 +361,20 @@ class PostController {
397361
}
398362
```
399363

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+
400378
To know more, see `@handlerParam` api documentation below.
401379

402380
### Redirection, Rendering via pug and others...
@@ -513,8 +491,6 @@ class HomeController {
513491
}
514492
```
515493

516-
> `#httpContext`, `#inject` and `#injector` will be deprecated from v1.0.0. Please use `#context`
517-
518494
#### Customize result
519495

520496
If you want to have customized result behavior, you can do it with `BaseResult`.
@@ -854,28 +830,25 @@ export type HandlerParamSelector<T> = (
854830
interface HandlerParamMeta<T> {
855831
index: number
856832
selector: HandlerParamSelector<T>
857-
paramType: any
833+
paramType?: any
858834
}
859835
```
860836

861837
- `index` Number index of the parameter.
862838
- `selector` Its selector.
863839
- `paramType` metadata from `design:paramtypes`.
864840

865-
#### `@reqBody(validator?: any)`
841+
#### `@reqBody()`
866842

867843
Inject `req.body`.
868844

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`.
870-
871845
```ts
872846
import { controller, httpPost, reqBody } from 'tachijs'
873847

874848
@controller('/post')
875849
class PostController {
876850
@httpPost('/')
877-
// Identically same to `create(@reqBody(PostDTO) post: PostDTO)`
878-
create(@reqBody() post: PostDTO) {
851+
create(@reqBody() post: any) {
879852
...
880853
}
881854
}

0 commit comments

Comments
 (0)