Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: avoid useless create new empty object #1692

Closed
wants to merge 10 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,16 @@ export class ExpressTemplateService extends TemplateService<ExpressApiHandlerPar

protected returnHandler(params: ExpressReturnHandlerParameters) {
const { response, statusCode, data } = params;
let { headers } = params;
headers = headers || {};
const { headers } = params;
jackey8616 marked this conversation as resolved.
Show resolved Hide resolved

if (response.headersSent) {
return;
}
Object.keys(headers).forEach((name: string) => {
response.set(name, headers[name]);
});
if( headers ){
Object.keys(headers).forEach((name: string) => {
response.set(name, headers[name]);
});
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This if statement is also can combine with or operator like:
Object.keys(headers || {}).forEach ...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want avoid unecessary creation of new empty object

if (data && typeof data.pipe === 'function' && data.readable && typeof data._read === 'function') {
response.status(statusCode || 200);
data.pipe(response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ export class HapiTemplateService extends TemplateService<HapiApiHandlerParameter

protected returnHandler(params: HapiReturnHandlerParameters) {
const { h, statusCode, data } = params;
let { headers } = params;
headers = headers || {};
const { headers } = params;

const tsoaResponsed = Object.getOwnPropertyDescriptor(h, hapiTsoaResponsed);
if (tsoaResponsed) {
Expand All @@ -129,9 +128,11 @@ export class HapiTemplateService extends TemplateService<HapiApiHandlerParameter

const response = data !== null && data !== undefined ? h.response(data).code(200) : h.response('').code(204);

Object.keys(headers).forEach((name: string) => {
response.header(name, headers[name]);
});
if( headers ){
Object.keys(headers).forEach((name: string) => {
response.header(name, headers[name]);
});
}

if (statusCode) {
response.code(statusCode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ export class KoaTemplateService extends TemplateService<KoaApiHandlerParameters,

protected returnHandler(params: KoaReturnHandlerParameters): Promise<any> | Context | undefined {
const { context, next, statusCode, data } = params;
let { headers } = params;
headers = headers || {};
const { headers } = params;

const isResponsed = Object.getOwnPropertyDescriptor(context.response, koaTsoaResponsed);
if (!context.headerSent && !isResponsed) {
Expand All @@ -128,7 +127,9 @@ export class KoaTemplateService extends TemplateService<KoaApiHandlerParameters,
context.status = statusCode;
}

context.set(headers);
if( headers ){
context.set(headers);
}
Object.defineProperty(context.response, koaTsoaResponsed, {
value: true,
writable: false,
Expand Down
Loading