monkeytype/backend/utils/monkey-response.js
2022-03-03 16:31:57 -05:00

27 lines
794 B
JavaScript

import { getCodesRangeStart } from "../constants/monkey-status-codes";
export class MonkeyResponse {
constructor(message, data, status = 200) {
this.message = message;
this.data = data ?? null;
this.status = status;
}
}
export function handleMonkeyResponse(handlerData, res) {
const isMonkeyResponse = handlerData instanceof MonkeyResponse;
const monkeyResponse = !isMonkeyResponse
? new MonkeyResponse("ok", handlerData)
: handlerData;
const { message, data, status } = monkeyResponse;
res.status(status);
if (status >= getCodesRangeStart()) res.statusMessage = message;
res.monkeyMessage = message; // so that we can see message in swagger stats
if ([301, 302].includes(status)) {
return res.redirect(data);
}
res.json({ message, data });
}