import { Controller , ValidationService , FieldErrors , ValidateError , TsoaRoute , HttpStatusCodeLiteral , TsoaResponse } from '@tsoa/runtime' ; import { TodoController } from './controller/todo' ; import * as express from 'express' ; const models = { "Todo" : { "dataType" : "refObject" , "properties" : { "id" : { "dataType" : "string" , "required" : true } , "date_created" : { "dataType" : "datetime" , "required" : true } , "date_updated" : { "dataType" : "datetime" , "required" : true } , "text" : { "dataType" : "string" , "required" : true } , "done" : { "dataType" : "boolean" , "required" : true } } , "additionalProperties" : false } , "ITodo" : { "dataType" : "refObject" , "properties" : { "text" : { "dataType" : "string" , "required" : true } , "done" : { "dataType" : "boolean" , "required" : true } , "id" : { "dataType" : "string" , "required" : true } , "date_created" : { "dataType" : "datetime" , "required" : true } , "date_updated" : { "dataType" : "datetime" , "required" : true } } , "additionalProperties" : false } , "Pick_Todo.Exclude_keyofTodo.id-or-done-or-date_created-or-date_updated__" : { "dataType" : "refAlias" , "type" : { "dataType" : "nestedObjectLiteral" , "nestedProperties" : { "text" : { "dataType" : "string" , "required" : true } } , "validators" : { } } } , "Omit_Todo.id-or-done-or-date_created-or-date_updated_" : { "dataType" : "refAlias" , "type" : { "ref" : "Pick_Todo.Exclude_keyofTodo.id-or-done-or-date_created-or-date_updated__" , "validators" : { } } } , "TodoCreationParams" : { "dataType" : "refAlias" , "type" : { "ref" : "Omit_Todo.id-or-done-or-date_created-or-date_updated_" , "validators" : { } } } , "IUpdateTodoRequest" : { "dataType" : "refObject" , "properties" : { "done" : { "dataType" : "boolean" , "required" : true } } , "additionalProperties" : false } } ; const validationService = new ValidationService ( models ) ; export function RegisterRoutes ( app ) { app . get ( '/api/todos/:id' , function TodoController_getById ( request , response , next ) { const args = { id : { "in" : "path" , "name" : "id" , "required" : true , "dataType" : "string" } } ; let validatedArgs = [ ] ; try { validatedArgs = getValidatedArgs ( args , request , response ) ; } catch ( err ) { return next ( err ) ; } const controller = new TodoController ( ) ; const promise = controller . getById . apply ( controller , validatedArgs as any ) ; promiseHandler ( controller , promise , response , undefined , next ) ; } ) ; app . get ( '/api/todos' , function TodoController_get ( request , response , next ) { const args = { page : { "default" : 1 , "in" : "query" , "name" : "page" , "dataType" : "double" } , pageSize : { "default" : 10 , "in" : "query" , "name" : "pageSize" , "dataType" : "double" } } ; let validatedArgs = [ ] ; try { validatedArgs = getValidatedArgs ( args , request , response ) ; } catch ( err ) { return next ( err ) ; } const controller = new TodoController ( ) ; const promise = controller . get . apply ( controller , validatedArgs as any ) ; promiseHandler ( controller , promise , response , undefined , next ) ; } ) ; app . post ( '/api/todos' , function TodoController_create ( request , response , next ) { const args = { requestBody : { "in" : "body" , "name" : "requestBody" , "required" : true , "ref" : "TodoCreationParams" } } ; let validatedArgs = [ ] ; try { validatedArgs = getValidatedArgs ( args , request , response ) ; } catch ( err ) { return next ( err ) ; } const controller = new TodoController ( ) ; const promise = controller . create . apply ( controller , validatedArgs as any ) ; promiseHandler ( controller , promise , response , undefined , next ) ; } ) ; app . post ( '/api/todos/:id' , function TodoController_updateStatus ( request , response , next ) { const args = { id : { "in" : "path" , "name" : "id" , "required" : true , "dataType" : "string" } , requestBody : { "in" : "body" , "name" : "requestBody" , "required" : true , "ref" : "IUpdateTodoRequest" } } ; let validatedArgs = [ ] ; try { validatedArgs = getValidatedArgs ( args , request , response ) ; } catch ( err ) { return next ( err ) ; } const controller = new TodoController ( ) ; const promise = controller . updateStatus . apply ( controller , validatedArgs as any ) ; promiseHandler ( controller , promise , response , undefined , next ) ; } ) ; app . delete ( '/api/todos/:id' , function TodoController_delete ( request , response , next ) { const args = { id : { "in" : "path" , "name" : "id" , "required" : true , "dataType" : "string" } } ; let validatedArgs = [ ] ; try { validatedArgs = getValidatedArgs ( args , request , response ) ; } catch ( err ) { return next ( err ) ; } const controller = new TodoController ( ) ; const promise = controller . delete . apply ( controller , validatedArgs as any ) ; promiseHandler ( controller , promise , response , undefined , next ) ; } ) ; function isController ( object ) { return 'getHeaders' in object && 'getStatus' in object && 'setStatus' in object ; } function promiseHandler ( controllerObj , promise , response , successStatus , next ) { return Promise . resolve ( promise ) . then ( ( data ) => { let statusCode = successStatus ; let headers ; if ( isController ( controllerObj ) ) { headers = controllerObj . getHeaders ( ) ; statusCode = controllerObj . getStatus ( ) || statusCode ; } returnHandler ( response , statusCode , data , headers ) ; } ) . catch ( ( error ) => next ( error ) ) ; } function returnHandler ( response , statusCode ? , data ? , headers = { } ) { if ( response . headersSent ) { return ; } Object . keys ( headers ) . forEach ( ( name ) => { response . set ( name , headers [ name ] ) ; } ) ; if ( data && typeof data . pipe === 'function' && data . readable && typeof data . _read === 'function' ) { data . pipe ( response ) ; } else if ( data !== null && data !== undefined ) { response . status ( statusCode || 200 ) . json ( data ) ; } else { response . status ( statusCode || 204 ) . end ( ) ; } } function responder ( response ) { return function ( status , data , headers ) { returnHandler ( response , status , data , headers ) ; } ; } ; function getValidatedArgs ( args , request , response ) { const fieldErrors = { } ; const values = Object . keys ( args ) . map ( ( key ) => { const name = args [ key ] . name ; switch ( args [ key ] . in ) { case 'request' : return request ; case 'query' : return validationService . ValidateParam ( args [ key ] , request . query [ name ] , name , fieldErrors , undefined , { "noImplicitAdditionalProperties" : "throw-on-extras" } ) ; case 'path' : return validationService . ValidateParam ( args [ key ] , request . params [ name ] , name , fieldErrors , undefined , { "noImplicitAdditionalProperties" : "throw-on-extras" } ) ; case 'header' : return validationService . ValidateParam ( args [ key ] , request . header ( name ) , name , fieldErrors , undefined , { "noImplicitAdditionalProperties" : "throw-on-extras" } ) ; case 'body' : return validationService . ValidateParam ( args [ key ] , request . body , name , fieldErrors , undefined , { "noImplicitAdditionalProperties" : "throw-on-extras" } ) ; case 'body-prop' : return validationService . ValidateParam ( args [ key ] , request . body [ name ] , name , fieldErrors , 'body.' , { "noImplicitAdditionalProperties" : "throw-on-extras" } ) ; case 'formData' : if ( args [ key ] . dataType === 'file' ) { return validationService . ValidateParam ( args [ key ] , request . file , name , fieldErrors , undefined , { "noImplicitAdditionalProperties" : "throw-on-extras" } ) ; } else if ( args [ key ] . dataType === 'array' && args [ key ] . array . dataType === 'file' ) { return validationService . ValidateParam ( args [ key ] , request . files , name , fieldErrors , undefined , { "noImplicitAdditionalProperties" : "throw-on-extras" } ) ; } else { return validationService . ValidateParam ( args [ key ] , request . body [ name ] , name , fieldErrors , undefined , { "noImplicitAdditionalProperties" : "throw-on-extras" } ) ; } case 'res' : return responder ( response ) ; } } ) ; if ( Object . keys ( fieldErrors ) . length > 0 ) { throw new ValidateError ( fieldErrors , '' ) ; } return values ; } }