-
Notifications
You must be signed in to change notification settings - Fork 513
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1545 from jackey8616/refactor/hbs-template-interface
Refactor/hbs template logics extract to classes with implement interface
- Loading branch information
Showing
13 changed files
with
377 additions
and
315 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
packages/cli/src/routeGeneration/templates/express/expressTemplateService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { Request as ExRequest, Response as ExResponse } from 'express'; | ||
import { FieldErrors, HttpStatusCodeLiteral, TsoaResponse, ValidateError, ValidationService } from "@tsoa/runtime"; | ||
|
||
import { TemplateService, isController } from '../templateService'; | ||
|
||
export class ExpressTemplateService implements TemplateService<ExRequest, ExResponse> { | ||
private readonly validationService: ValidationService; | ||
|
||
constructor( | ||
readonly models: any, | ||
private readonly minimalSwaggerConfig: any, | ||
) { | ||
this.validationService = new ValidationService(models); | ||
} | ||
|
||
promiseHandler(controllerObj: any, promise: any, response: ExResponse, successStatus: any, next: any) { | ||
return Promise.resolve(promise) | ||
.then((data: any) => { | ||
let statusCode = successStatus; | ||
let headers; | ||
if (isController(controllerObj)) { | ||
headers = controllerObj.getHeaders(); | ||
statusCode = controllerObj.getStatus() || statusCode; | ||
} | ||
|
||
// WARNING: This file was auto-generated with tsoa. Please do not modify it. Re-run tsoa to re-generate this file: https://github.com/lukeautry/tsoa | ||
|
||
this.returnHandler(response, headers, statusCode, data) | ||
}) | ||
.catch((error: any) => next(error)); | ||
} | ||
|
||
returnHandler(response: any, headers: any = {}, statusCode?: number, data?: any) { | ||
if (response.headersSent) { | ||
return; | ||
} | ||
Object.keys(headers).forEach((name: string) => { | ||
response.set(name, headers[name]); | ||
}); | ||
if (data && typeof data.pipe === 'function' && data.readable && typeof data._read === 'function') { | ||
response.status(statusCode || 200) | ||
data.pipe(response); | ||
} else if (data !== null && data !== undefined) { | ||
response.status(statusCode || 200).json(data); | ||
} else { | ||
response.status(statusCode || 204).end(); | ||
} | ||
} | ||
|
||
responder(response: any): TsoaResponse<HttpStatusCodeLiteral, unknown> { | ||
return (status, data, headers) => { | ||
this.returnHandler(response, headers, status, data); | ||
}; | ||
} | ||
|
||
getValidatedArgs(args: any, request: ExRequest, response: ExResponse): any[] { | ||
const fieldErrors: FieldErrors = {}; | ||
const values = Object.keys(args).map((key) => { | ||
const name = args[key].name; | ||
switch (args[key].in) { | ||
case 'request': | ||
return request; | ||
case 'query': | ||
return this.validationService.ValidateParam(args[key], request.query[name], name, fieldErrors, undefined, this.minimalSwaggerConfig); | ||
case 'queries': | ||
return this.validationService.ValidateParam(args[key], request.query, name, fieldErrors, undefined, this.minimalSwaggerConfig); | ||
case 'path': | ||
return this.validationService.ValidateParam(args[key], request.params[name], name, fieldErrors, undefined, this.minimalSwaggerConfig); | ||
case 'header': | ||
return this.validationService.ValidateParam(args[key], request.header(name), name, fieldErrors, undefined, this.minimalSwaggerConfig); | ||
case 'body': | ||
return this.validationService.ValidateParam(args[key], request.body, name, fieldErrors, undefined, this.minimalSwaggerConfig); | ||
case 'body-prop': | ||
return this.validationService.ValidateParam(args[key], request.body[name], name, fieldErrors, 'body.', this.minimalSwaggerConfig); | ||
case 'formData': | ||
if (args[key].dataType === 'file') { | ||
return this.validationService.ValidateParam(args[key], request.file, name, fieldErrors, undefined, this.minimalSwaggerConfig); | ||
} else if (args[key].dataType === 'array' && args[key].array.dataType === 'file') { | ||
return this.validationService.ValidateParam(args[key], request.files, name, fieldErrors, undefined, this.minimalSwaggerConfig); | ||
} else { | ||
return this.validationService.ValidateParam(args[key], request.body[name], name, fieldErrors, undefined, this.minimalSwaggerConfig); | ||
} | ||
case 'res': | ||
return this.responder(response); | ||
} | ||
}); | ||
|
||
if (Object.keys(fieldErrors).length > 0) { | ||
throw new ValidateError(fieldErrors, ''); | ||
} | ||
return values; | ||
} | ||
} |
Oops, something went wrong.