feat: add full-stack API persistence and RBAC
This commit is contained in:
32
modules/core/server/api.ts
Normal file
32
modules/core/server/api.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
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 });
|
||||
}
|
||||
|
||||
export function jsonFailure(reason: string, status = 400): Response {
|
||||
return Response.json({ success: false, reason }, { status });
|
||||
}
|
||||
|
||||
export async function readJsonBody(request: Request): Promise<unknown> {
|
||||
try {
|
||||
return await request.json();
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user