Skip to content

Commit

Permalink
feat: add a MethodDecorator to wrap json function
Browse files Browse the repository at this point in the history
  • Loading branch information
himself65 committed May 10, 2019
1 parent 8833d04 commit 8af6a0c
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/decorators/http_methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,49 @@ import is = require('is');
// tslint:disable-next-line
import 'reflect-metadata';
import BaseContext from '../core/base_context';
import { Daruk } from "../typings/daruk";
import { CONTROLLER_FUNC_NAME, CONTROLLER_PATH } from './constants';

/**
* @desc 将函数的返回打包到 ctx.body,并返回 application/json 类型
* @return MethodDecorator - 装饰器
* @example
* class Class {
* @json()
* index(ctx) {
* return {
* foo: 1
* }
* }
* }
* // the same as
* class Class {
* async index(ctx, next) {
* ctx.body = {
* foo: 1
* }
* ctx.type = 'application/json'
* await next()
* }
* }
*/
export function json (type = 'application/json') {
return (proto: BaseContext, propertyKey: string, descriptor: PropertyDescriptor) => {
const oldFunc = descriptor.value;

descriptor.value = async function jsonWrap(ctx: Daruk.Context, next: () => Promise<void>) {
const val = await oldFunc(ctx);
// 确保是Object类型
ctx.body = { ...val };
ctx.type = type;
await next();
};
};
}

// json的大写别名
export const JSON = json;

/**
* @desc 生成 http method 装饰器
* @param {string} method - http method,如 get、post、head
Expand Down
12 changes: 12 additions & 0 deletions test/apps/decorators/controllers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
del,
get,
head,
json,
JSON,
middleware,
options,
patch,
Expand Down Expand Up @@ -45,6 +47,16 @@ export default class Index extends BaseController {
public async put(ctx: Daruk.Context, next: Function) {
ctx.body = '';
}
@json()
@get('/json1')
public json() {
return { foo: 1 };
}
@get('/json2')
@JSON()
public JSON() {
return { foo: 1 };
}

@middleware('routeMiddleware')
@get('/middleware')
Expand Down
12 changes: 12 additions & 0 deletions test/decorators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ describe('decorators', () => {
.put('/put')
.expect(code200, done);
});
it('decorator @json', (done) => {
request(server)
.get('/json1')
.expect(code200)
.expect({ foo: 1 }, done);
});
it('decorator @JSON', (done) => {
request(server)
.get('/json2')
.expect(code200)
.expect({ foo: 1 }, done);
});

it('decorator "@middleware"', (done) => {
request(server)
Expand Down
3 changes: 3 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ declare module 'daruk' {
export const head: MethodDecoratorFunc;
export const all: MethodDecoratorFunc;

export const json: MethodDecoratorFunc;
export const JSON: MethodDecoratorFunc;

export const middleware: (middlewareName: string) => MethodDecorator;

type PropDecoratorFunc = (field?: string) => PropertyDecorator;
Expand Down

0 comments on commit 8af6a0c

Please sign in to comment.