Skip to content

Commit

Permalink
refactor(app:): split return type conditions (#434)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Jul 10, 2023
1 parent f74a9b5 commit 091f326
Showing 1 changed file with 35 additions and 18 deletions.
53 changes: 35 additions & 18 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,35 +110,52 @@ export function createAppEventHandler(stack: Stack, options: AppOptions) {
if (layer.match && !layer.match(event.node.req.url as string, event)) {
continue;
}

const val = await layer.handler(event);

// Already handled
if (event.handled) {
return;
}
const type = typeof val;
if (type === "string") {
return send(event, val, MIMES.html);
} else if (isStream(val)) {
return sendStream(event, val);
} else if (val === null) {

// Empty Content
if (val === null) {
event.node.res.statusCode = 204;
return send(event);
} else if (
type === "object" ||
type === "boolean" ||
type === "number" /* IS_JSON */
) {
}

if (val) {
// Stream
if (isStream(val)) {
return sendStream(event, val);
}

// Buffer
if (val.buffer) {
return send(event, val);
} else if (val instanceof Error) {
}

// Error
if (val instanceof Error) {
throw createError(val);
} else {
return send(
event,
JSON.stringify(val, undefined, spacing),
MIMES.json
);
}
}

const valType = typeof val;

// HTML String
if (valType === "string") {
return send(event, val, MIMES.html);
}

// JSON Response
if (
valType === "object" ||
valType === "boolean" ||
valType === "number"
) {
return send(event, JSON.stringify(val, undefined, spacing), MIMES.json);
}
}
if (!event.handled) {
throw createError({
Expand Down

0 comments on commit 091f326

Please sign in to comment.