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 @@ -103,16 +103,16 @@ export class ExpressTemplateService extends TemplateService<ExpressApiHandlerPar
}

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

if (response.headersSent) {
return;
}
Object.keys(headers).forEach((name: string) => {

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,7 @@ export class HapiTemplateService extends TemplateService<HapiApiHandlerParameter
}

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

const tsoaResponsed = Object.getOwnPropertyDescriptor(h, hapiTsoaResponsed);
if (tsoaResponsed) {
Expand All @@ -129,7 +127,7 @@ 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) => {
Object.keys(headers || {}).forEach((name: string) => {
response.header(name, headers[name]);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,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 { context, next, statusCode, data, headers } = params;

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

context.set(headers);
context.set(headers || {});

Object.defineProperty(context.response, koaTsoaResponsed, {
value: true,
writable: false,
Expand Down
Loading