@@ -4,15 +4,15 @@ import {BaseDriver} from "../BaseDriver";
44import { MiddlewareMetadata } from "../../metadata/MiddlewareMetadata" ;
55import { ParamMetadata } from "../../metadata/ParamMetadata" ;
66import { UseMetadata } from "../../metadata/UseMetadata" ;
7- import { classToPlain } from "class-transformer" ;
87import { KoaMiddlewareInterface } from "./KoaMiddlewareInterface" ;
98import { AuthorizationCheckerNotDefinedError } from "../../error/AuthorizationCheckerNotDefinedError" ;
109import { AccessDeniedError } from "../../error/AccessDeniedError" ;
1110import { isPromiseLike } from "../../util/isPromiseLike" ;
1211import { getFromContainer } from "../../container" ;
1312import { RoleChecker } from "../../RoleChecker" ;
1413import { AuthorizationRequiredError } from "../../error/AuthorizationRequiredError" ;
15- import { NotFoundError , HttpError } from "../../index" ;
14+ import { HttpError , NotFoundError } from "../../index" ;
15+
1616const cookie = require ( "cookie" ) ;
1717const templateUrl = require ( "template-url" ) ;
1818
@@ -81,7 +81,7 @@ export class KoaDriver extends BaseDriver {
8181 const checkResult = actionMetadata . authorizedRoles instanceof Function ?
8282 getFromContainer < RoleChecker > ( actionMetadata . authorizedRoles ) . check ( action ) :
8383 this . authorizationChecker ( action , actionMetadata . authorizedRoles ) ;
84-
84+
8585 const handleError = ( result : any ) => {
8686 if ( ! result ) {
8787 let error = actionMetadata . authorizedRoles . length === 0 ? new AuthorizationRequiredError ( action ) : new AccessDeniedError ( action ) ;
@@ -90,7 +90,7 @@ export class KoaDriver extends BaseDriver {
9090 return next ( ) ;
9191 }
9292 } ;
93-
93+
9494 if ( isPromiseLike ( checkResult ) ) {
9595 return checkResult
9696 . then ( result => handleError ( result ) )
@@ -213,21 +213,14 @@ export class KoaDriver extends BaseDriver {
213213 */
214214 handleSuccess ( result : any , action : ActionMetadata , options : Action ) : void {
215215
216+ // if the action returned the context or the response object itself, short-circuits
217+ if ( result && ( result === options . response || result === options . context ) ) {
218+ return options . next ( ) ;
219+ }
220+
216221 // transform result if needed
217222 result = this . transformResult ( result , action , options ) ;
218223
219- // set http status code
220- if ( result === undefined && action . undefinedResultCode && action . undefinedResultCode instanceof Function ) {
221- throw new ( action . undefinedResultCode as any ) ( options ) ;
222- }
223- else if ( result === null && action . nullResultCode ) {
224- if ( action . nullResultCode instanceof Function ) {
225- throw new ( action . nullResultCode as any ) ( options ) ;
226- }
227- } else if ( action . successHttpCode ) {
228- options . response . status = action . successHttpCode ;
229- }
230-
231224 if ( action . redirect ) { // if redirect is set then do it
232225 if ( typeof result === "string" ) {
233226 options . response . redirect ( result ) ;
@@ -238,32 +231,24 @@ export class KoaDriver extends BaseDriver {
238231 }
239232 } else if ( action . renderedTemplate ) { // if template is set then render it // TODO: not working in koa
240233 const renderOptions = result && result instanceof Object ? result : { } ;
241-
234+
242235 this . koa . use ( async function ( ctx : any , next : any ) {
243236 await ctx . render ( action . renderedTemplate , renderOptions ) ;
244237 } ) ;
245238 }
246239 else if ( result === undefined ) { // throw NotFoundError on undefined response
247- const notFoundError = new NotFoundError ( ) ;
248- if ( action . undefinedResultCode ) {
249- notFoundError . httpCode = action . undefinedResultCode as number ;
240+ if ( action . undefinedResultCode instanceof Function ) {
241+ throw new ( action . undefinedResultCode as any ) ( options ) ;
242+
243+ } else if ( ! action . undefinedResultCode ) {
244+ throw new NotFoundError ( ) ;
250245 }
251- throw notFoundError ;
252246 }
253247 else if ( result === null ) { // send null response
254- if ( action . isJsonTyped ) {
255- options . response . body = null ;
256- } else {
257- options . response . body = null ;
258- }
259-
260- // Setting `null` as a `response.body` means to koa that there is no content to return
261- // so we must reset the status codes here.
262- if ( action . nullResultCode ) {
263- options . response . status = action . nullResultCode ;
264- } else {
265- options . response . status = 204 ;
266- }
248+ if ( action . nullResultCode instanceof Function )
249+ throw new ( action . nullResultCode as any ) ( options ) ;
250+
251+ options . response . body = null ;
267252 }
268253 else if ( result instanceof Uint8Array ) { // check if it's binary data (typed array)
269254 options . response . body = Buffer . from ( result as any ) ;
@@ -275,7 +260,22 @@ export class KoaDriver extends BaseDriver {
275260 options . response . body = result ;
276261 }
277262 }
278-
263+
264+ // set http status code
265+ if ( result === undefined && action . undefinedResultCode ) {
266+ console . log ( action . undefinedResultCode ) ;
267+ options . response . status = action . undefinedResultCode ;
268+ }
269+ else if ( result === null && action . nullResultCode ) {
270+ options . response . status = action . nullResultCode ;
271+
272+ } else if ( action . successHttpCode ) {
273+ options . response . status = action . successHttpCode ;
274+
275+ } else if ( options . response . body === null ) {
276+ options . response . status = 204 ;
277+ }
278+
279279 // apply http headers
280280 Object . keys ( action . headers ) . forEach ( name => {
281281 options . response . set ( name , action . headers [ name ] ) ;
@@ -290,12 +290,6 @@ export class KoaDriver extends BaseDriver {
290290 handleError ( error : any , action : ActionMetadata | undefined , options : Action ) {
291291 return new Promise ( ( resolve , reject ) => {
292292 if ( this . isDefaultErrorHandlingEnabled ) {
293- // set http status
294- if ( error instanceof HttpError && error . httpCode ) {
295- options . response . status = error . httpCode ;
296- } else {
297- options . response . status = 500 ;
298- }
299293
300294 // apply http headers
301295 if ( action ) {
@@ -311,6 +305,13 @@ export class KoaDriver extends BaseDriver {
311305 options . response . body = this . processTextError ( error ) ;
312306 }
313307
308+ // set http status
309+ if ( error instanceof HttpError && error . httpCode ) {
310+ options . response . status = error . httpCode ;
311+ } else {
312+ options . response . status = 500 ;
313+ }
314+
314315 return resolve ( ) ;
315316 }
316317 return reject ( error ) ;
0 commit comments