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 { 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