Greatly reduce repeated code

This commit is contained in:
2023-10-19 16:50:48 -04:00
parent 47e639be43
commit dd2d9f2672
34 changed files with 196 additions and 481 deletions

View File

@ -1,3 +1,5 @@
import { jsonError } from "../../../common.js";
export async function onRequestPost(context: RequestContext) {
const { pathname } = new URL(context.request.url);
@ -8,12 +10,7 @@ export async function onRequestPost(context: RequestContext) {
const { permissions } = context.data.current_user;
if (!(permissions & (1 << 0)) && !(permissions & (1 << 11)))
return new Response('{"error":"Forbidden"}', {
headers: {
"content-type": "application/json",
},
status: 403,
});
return jsonError("Forbidden", 403);
const { body } = context.data;
const id = context.params.id as string;
@ -23,13 +20,7 @@ export async function onRequestPost(context: RequestContext) {
if (!pathname.endsWith("/ban")) {
const key = await context.env.DATA.get(`appeal_${id}`);
if (!key)
return new Response('{"error":"No appeal with that ID exists"}', {
headers: {
"content-type": "application/json",
},
status: 404,
});
if (!key) return jsonError("No appeal with that ID exists", 404);
context.data.appeal = JSON.parse(key);
}
@ -38,12 +29,7 @@ export async function onRequestPost(context: RequestContext) {
body.feedback &&
(typeof body.feedback !== "string" || body.feedback.length > 512)
)
return new Response('{"error":"Invalid feedback"}', {
headers: {
"content-type": "application/json",
},
status: 400,
});
return jsonError("Invalid feedback", 400);
return await context.next();
}