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

refactor(app:): split return type conditions #434

Merged
merged 1 commit into from
Jul 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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