Skip to content

Commit 0e64ffe

Browse files
committed
Clean code and refactor conditions tree
1 parent 7245f92 commit 0e64ffe

File tree

2 files changed

+23
-30
lines changed

2 files changed

+23
-30
lines changed

src/driver/express/ExpressDriver.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -223,18 +223,14 @@ export class ExpressDriver extends BaseDriver implements Driver {
223223
handleSuccess(result: any, action: ActionMetadata, options: Action): void {
224224

225225
// check if we need to transform result and do it
226-
if (this.useClassTransformer && result && result instanceof Object) {
226+
if (result && result instanceof Object && this.useClassTransformer) {
227227
const options = action.responseClassTransformOptions || this.classToPlainTransformOptions;
228228
result = classToPlain(result, options);
229229
}
230230

231231
// set http status code
232-
if (result === undefined) {
233-
if (action.undefinedResultCode) {
234-
if (action.undefinedResultCode instanceof Function) {
235-
throw new (action.undefinedResultCode as any)(options);
236-
}
237-
}
232+
if (result === undefined && action.undefinedResultCode && action.undefinedResultCode instanceof Function) {
233+
throw new (action.undefinedResultCode as any)(options);
238234
}
239235
else if (result === null) {
240236
if (action.nullResultCode) {
@@ -290,14 +286,14 @@ export class ExpressDriver extends BaseDriver implements Driver {
290286
}
291287
options.next();
292288
}
293-
else if (result === undefined) {
289+
else if (result === undefined) { // throw NotFoundError on undefined response
294290
const notFoundError = new NotFoundError();
295291
if (action.undefinedResultCode) {
296292
notFoundError.httpCode = action.undefinedResultCode as number;
297293
}
298294
throw notFoundError;
299295
}
300-
else { // send null/undefined response
296+
else { // send null response
301297
if (action.isJsonTyped) {
302298
options.response.json(null);
303299
} else {

src/driver/koa/KoaDriver.ts

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,10 @@ export class KoaDriver extends BaseDriver implements Driver {
211211
}
212212

213213
// set http status code
214-
if (action.undefinedResultCode && result === undefined) {
215-
if (action.undefinedResultCode instanceof Function) {
216-
throw new (action.undefinedResultCode as any)(options);
217-
}
214+
if (result === undefined && action.undefinedResultCode && action.undefinedResultCode instanceof Function) {
215+
throw new (action.undefinedResultCode as any)(options);
218216
}
219-
else if (action.nullResultCode && result === null) {
217+
else if (result === null && action.nullResultCode) {
220218
if (action.nullResultCode instanceof Function) {
221219
throw new (action.nullResultCode as any)(options);
222220
}
@@ -240,16 +238,16 @@ export class KoaDriver extends BaseDriver implements Driver {
240238

241239
return options.next();
242240

243-
} else if (action.renderedTemplate) { // if template is set then render it // todo: not working in koa
241+
} else if (action.renderedTemplate) { // if template is set then render it // TODO: not working in koa
244242
const renderOptions = result && result instanceof Object ? result : {};
245243

246244
this.koa.use(async function (ctx: any, next: any) {
247245
await ctx.render(action.renderedTemplate, renderOptions);
248246
});
249247

250248
return options.next();
251-
252-
} else if (result != null) { // send regular result
249+
}
250+
else if (result != null) { // send regular result
253251
if (result instanceof Object) {
254252
options.response.body = result;
255253
} else {
@@ -258,7 +256,14 @@ export class KoaDriver extends BaseDriver implements Driver {
258256

259257
return options.next();
260258
}
261-
else { // send null/undefined response
259+
else if (result === undefined) { // throw NotFoundError on undefined response
260+
const notFoundError = new NotFoundError();
261+
if (action.undefinedResultCode) {
262+
notFoundError.httpCode = action.undefinedResultCode as number;
263+
}
264+
throw notFoundError;
265+
}
266+
else { // send null response
262267
if (action.isJsonTyped) {
263268
options.response.body = null;
264269
} else {
@@ -267,18 +272,10 @@ export class KoaDriver extends BaseDriver implements Driver {
267272

268273
// Setting `null` as a `response.body` means to koa that there is no content to return
269274
// so we must reset the status codes here.
270-
if (result === null) {
271-
if (action.nullResultCode) {
272-
options.response.status = action.nullResultCode;
273-
} else {
274-
options.response.status = 204;
275-
}
276-
} else if (result === undefined) {
277-
const notFoundError = new NotFoundError();
278-
if (action.undefinedResultCode) {
279-
notFoundError.httpCode = action.undefinedResultCode as number;
280-
}
281-
throw notFoundError;
275+
if (action.nullResultCode) {
276+
options.response.status = action.nullResultCode;
277+
} else {
278+
options.response.status = 204;
282279
}
283280

284281
return options.next();

0 commit comments

Comments
 (0)