npm.io
1.0.7 • Published 9 years ago

hinos-requestmatcher

Licence
ISC
Version
1.0.7
Deps
0
Vulns
0
Weekly
0

hi-requestmatcher

A plugin cast type for request as query, params, body before get data in controller hinos

Install

npm i hinos-requestmatcher -S

Examples

Typescript

File controller

import { GET, POST, PUT, DELETE, HEAD } from 'hinos-route';
import { BODYPARSER } from 'hinos-bodyparser';
import { MATCHER } from 'hinos-requestmatcher';

export default class ChartController {

    @POST('/chart/:id')
    @BODYPARSER() // Parse body first
    @MATCHER({ // Cast data type
        params: {
            id: Mongo.uuid    
        }, 
        body: {
            name: String,
            des: String,
            id: id => +id; // Customize parser, can use async, await
        }
    })
    static async add({
        params, // get after parse and match
		body // get after parse and match
	}) {
        const rs: Chart = await ChartService.insert(body);
        return rs;
    }
}