48 lines
924 B
TypeScript
48 lines
924 B
TypeScript
export type ApiFailure = {
|
|
success: false;
|
|
reason: string;
|
|
};
|
|
|
|
export type ApiSuccess<T extends Record<string, unknown>> = {
|
|
success: true;
|
|
reason: string;
|
|
} & T;
|
|
|
|
export type ApiResult<T extends Record<string, unknown>> = ApiSuccess<T> | ApiFailure;
|
|
|
|
export function jsonSuccess<T extends Record<string, unknown>>(
|
|
reason: string,
|
|
payload: T,
|
|
status = 200,
|
|
): Response {
|
|
return Response.json(
|
|
{ success: true, reason, ...payload },
|
|
{
|
|
status,
|
|
headers: {
|
|
"Cache-Control": "no-store",
|
|
},
|
|
},
|
|
);
|
|
}
|
|
|
|
export function jsonFailure(reason: string, status = 400): Response {
|
|
return Response.json(
|
|
{ success: false, reason },
|
|
{
|
|
status,
|
|
headers: {
|
|
"Cache-Control": "no-store",
|
|
},
|
|
},
|
|
);
|
|
}
|
|
|
|
export async function readJsonBody(request: Request): Promise<unknown> {
|
|
try {
|
|
return await request.json();
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|