internal import assert = require ( 'assert' ) ; internal import is = require ( 'is' ) ; internal import koa = require ( 'koa' ) ; import { CONTROLLER_CLASS_PREFIX , CONTROLLER_DISABLED_CLASS , CONTROLLER_DISABLED_METHOD , CONTROLLER_REDIRECT_PATH } from './constants' ; desc param { string } path return export function prefix ( path ) { assert ( is . string ( path ) , `[Decorator @${ path }] parameter must be a string` ) ; return ( target ) => { Reflect . defineMetadata ( CONTROLLER_CLASS_PREFIX , path , target ) ; } ; } export function disabled ( ) { return ( proto , propertyKey ? , descriptor ? ) => { if ( propertyKey ) { const target = proto . constructor ; Reflect . defineMetadata ( CONTROLLER_DISABLED_METHOD , 'disabled' , target , propertyKey ) ; } else { Reflect . defineMetadata ( CONTROLLER_DISABLED_CLASS , 'disabled' , proto ) ; } } ; } param { string } path export function redirect ( path ) { assert ( is . string ( path ) , `[Decorator @${ path }] parameter must be a string` ) ; return ( proto , propertyKey , descriptor ) => { const target = proto . constructor ; Reflect . defineMetadata ( CONTROLLER_REDIRECT_PATH , path , target , propertyKey ) ; } ; } param { string } type example type type type type export function type ( type ) { assert ( is . string ( type ) , `[Decorator @${ type }] parameter must be a string` ) ; return ( proto , propertyKey , descriptor ) => { const oldFunc = descriptor . value ; descriptor . value = async function typeWrap ( ctx , next ) { await oldFunc . call ( this , ... arguments ) ; ctx . type = type ; await next ( ) ; } ; } ; } param { string | object } key param { string = } value export function header ( key , value ? ) { assert ( is . string ( key ) || is . object ( key ) , `[Decorator @${ key }] parameter must be a string or object` ) ; assert ( ( is . string ( key ) && is . string ( value ) ) || is . object ( key ) , `[Decorator @${ value }] parameter must be a string` ) ; let headers = { } ; if ( typeof key === 'string' && value ) { headers [ key ] = value ; } else if ( typeof key === 'object' ) { headers = key ; } return ( proto , propertyKey , descriptor ) => { const oldFunc = descriptor . value ; descriptor . value = async function headerWrap ( ctx , next ) { await oldFunc . call ( this , ... arguments ) ; ctx . set ( headers ) ; await next ( ) ; } ; } ; } export function cache ( callback ) { assert ( is . function ( callback ) , `[Decorator @${ callback }] parameter must be a function` ) ; return ( proto , propertyKey , descriptor ) => { const oldFunc = descriptor . value ; descriptor . value = async function cacheWrap ( ctx , next ) { let cacheKey = ctx . request . querystring ; let cacheData = await callback ( cacheKey ) ; if ( cacheData ) { ctx . body = cacheData ; } else { await oldFunc . call ( this , ... arguments ) ; await callback ( cacheKey , ctx . body ) ; } await next ( ) ; } ; } ; }