mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-02-02 12:00:10 +08:00
7ef0c424d6
* Migrate some utils to TS * Add argument type * Fix logic * Refactor math functions * Rename function
34 lines
787 B
TypeScript
34 lines
787 B
TypeScript
import { Response } from "express";
|
|
import { isCustomCode } from "../constants/monkey-status-codes";
|
|
|
|
export class MonkeyResponse {
|
|
message: string;
|
|
data: any;
|
|
status: number;
|
|
|
|
constructor(message?: string, data?: any, status = 200) {
|
|
this.message = message ?? "ok";
|
|
this.data = data ?? null;
|
|
this.status = status;
|
|
}
|
|
}
|
|
|
|
export function handleMonkeyResponse(
|
|
monkeyResponse: MonkeyResponse,
|
|
res: Response
|
|
): void {
|
|
const { message, data, status } = monkeyResponse;
|
|
|
|
res.status(status);
|
|
if (isCustomCode(status)) {
|
|
res.statusMessage = message;
|
|
}
|
|
|
|
//@ts-ignore ignored so that we can see message in swagger stats
|
|
res.monkeyMessage = message;
|
|
if ([301, 302].includes(status)) {
|
|
return res.redirect(data);
|
|
}
|
|
|
|
res.json({ message, data });
|
|
}
|