Greatly reduce repeated code
This commit is contained in:
@ -1,3 +1,5 @@
|
||||
import { jsonError, jsonResponse } from "../../../common.js";
|
||||
|
||||
export async function onRequestGet(context: RequestContext) {
|
||||
const types: { [k: string]: { permissions: number[]; prefix: string } } = {
|
||||
appeal: {
|
||||
@ -26,12 +28,7 @@ export async function onRequestGet(context: RequestContext) {
|
||||
(p) => context.data.current_user.permissions & p,
|
||||
)
|
||||
)
|
||||
return new Response('{"error":"You cannot use this filter"}', {
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
status: 403,
|
||||
});
|
||||
return jsonError("You cannot use this filter", 403);
|
||||
|
||||
let item: {
|
||||
[k: string]: any;
|
||||
@ -48,19 +45,11 @@ export async function onRequestGet(context: RequestContext) {
|
||||
type === "report" &&
|
||||
(await context.env.DATA.get(`reportprocessing_${itemId}`))
|
||||
)
|
||||
return new Response('{"error":"Report is processing"}', {
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
status: 409,
|
||||
});
|
||||
return jsonError("Report is processing", 409);
|
||||
|
||||
if (item) delete item.user?.email;
|
||||
|
||||
return new Response(item ? JSON.stringify(item) : '{"error":"Not found"}', {
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
status: item ? 200 : 404,
|
||||
});
|
||||
return item
|
||||
? jsonResponse(JSON.stringify(item))
|
||||
: jsonError("Not found", 404);
|
||||
}
|
||||
|
@ -1,3 +1,5 @@
|
||||
import { jsonError, jsonResponse } from "../../common.js";
|
||||
|
||||
export async function onRequestGet(context: RequestContext) {
|
||||
const { searchParams } = new URL(context.request.url);
|
||||
const before = parseInt(searchParams.get("before") || `${Date.now()}`);
|
||||
@ -21,28 +23,13 @@ export async function onRequestGet(context: RequestContext) {
|
||||
const { current_user: currentUser } = context.data;
|
||||
|
||||
if (!entryType || !types[entryType])
|
||||
return new Response('{"error":"Invalid filter type"}', {
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
status: 400,
|
||||
});
|
||||
return jsonError("Invalid filter type", 400);
|
||||
|
||||
if (!permissions[entryType].find((p) => currentUser.permissions & p))
|
||||
return new Response('{"error":"You cannot use this filter"}', {
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
status: 403,
|
||||
});
|
||||
return jsonError("You cannot use this filter", 403);
|
||||
|
||||
if (isNaN(before) || before > Date.now())
|
||||
return new Response('{"error":"Invalid `before` parameter"}', {
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
status: 400,
|
||||
});
|
||||
return jsonError("Invalid `before` parameter", 400);
|
||||
|
||||
const prefix = types[entryType];
|
||||
const table = tables[entryType];
|
||||
@ -77,9 +64,5 @@ export async function onRequestGet(context: RequestContext) {
|
||||
}
|
||||
}
|
||||
|
||||
return new Response(JSON.stringify(items.filter((v) => v !== null)), {
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
});
|
||||
return jsonResponse(JSON.stringify(items.filter((v) => v !== null)));
|
||||
}
|
||||
|
Reference in New Issue
Block a user