import send , { SendOptions } from 'koa-send' ; import path from 'path' ; import { HttpStatusCodes } from '../Server' ; import { ContextInterface } from '../Interface' ; import { ResponseStrategies } from './ResponseStrategies' ; export class Response { protected ctx : ContextInterface ; protected headers : { [ header ] : string | Array < string > ; } = { } ; protected strategy : string = ResponseStrategies . Json ; protected statusCode : HttpStatusCodes ; protected meta : any ; protected data : any ; protected message : string ; protected constructor ( { message , data , meta , statusCode } ) { this . data = data ; this . meta = meta ; this . message = message ; this . statusCode = statusCode ; } public setData ( data ) { this . data = data ; return this ; } public getData ( ) { return this . data ; } public async patchContext ( ctx ) { this . ctx = ctx ; this . applyStatusCode ( this . statusCode ) . applyHeaders ( ) ; if ( this . strategy === ResponseStrategies . File ) { await this . sendFile ( ) ; } else if ( this . strategy === ResponseStrategies . Html ) { ctx . type = 'html' ; this . applyBody ( this . data ) ; } else { this . applyBody ( this . format ( ) ) ; } if ( typeof this . apply === 'function' ) { await this . apply ( ) ; } } protected async sendFile ( ) { const { location , options = { } } = this . meta ; const { dir , base } = path . parse ( location ) ; if ( options . root ) { return await send ( this . ctx , location , options ) ; } await send ( this . ctx , base , Object . assign ( { root : dir } , options ) ) ; } public applyStatusCode ( statusCode ) { this . ctx . status = statusCode ; return this ; } public setStatusCode ( statusCode ) { this . statusCode = statusCode ; return this ; } public getStatusCode ( ) { return this . statusCode ; } protected applyBody ( body ) { if ( body ) { this . ctx . body = body ; } return this ; } public setHeaders ( headers ) { this . headers = headers ; return this ; } public addHeaders ( headers ) { Reflect . ownKeys ( headers ) . forEach ( ( header ) => this . setHeader ( header , headers [ header ] ) ) ; return this ; } public setHeader ( header , value ) { this . headers [ header ] = value ; return this ; } public appendHeader ( header , value ) { this . headers [ header ] = [ ] . concat ( this . headers [ header ] , value ) ; } public removeHeader ( header ) { delete this . headers [ header ] ; return this ; } public applyHeaders ( ) { Reflect . ownKeys ( this . headers ) . forEach ( ( header ) => this . ctx . set ( header , this . headers [ header ] ) ) ; return this ; } public file ( location , options ? ) { this . strategy = ResponseStrategies . File ; this . meta = { location , options } ; return this ; } public json ( data ) { this . strategy = ResponseStrategies . Json ; this . data = data ; return this ; } public html ( data ) { this . strategy = ResponseStrategies . Html ; this . data = data ; return this ; } protected apply ( ) { return ; } protected format ( ) { return ; } } export interface ResponseArgumentsInterface { statusCode : HttpStatusCodes ; message ? : string ; data ? : any ; meta ? : any ; }