Greatly reduce repeated code
This commit is contained in:
@ -1,15 +1,12 @@
|
||||
import { jsonError } from "../../../common.js";
|
||||
|
||||
export async function onRequest(context: RequestContext) {
|
||||
if (
|
||||
![1 << 3, 1 << 4, 1 << 12].find(
|
||||
(int) => context.data.current_user?.permissions & int,
|
||||
)
|
||||
)
|
||||
return new Response('{"error":"Forbidden"}', {
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
status: 401,
|
||||
});
|
||||
return jsonError("Forbidden", 403);
|
||||
|
||||
return await context.next();
|
||||
}
|
||||
|
@ -1,13 +1,10 @@
|
||||
import { jsonError } from "../../../common.js";
|
||||
|
||||
export async function onRequest(context: RequestContext) {
|
||||
if (
|
||||
![1 << 4, 1 << 12].find((p) => context.data.current_user?.permissions & p)
|
||||
)
|
||||
return new Response('{"error":"Forbidden"}', {
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
status: 403,
|
||||
});
|
||||
return jsonError("Forbidden", 403);
|
||||
|
||||
return await context.next();
|
||||
}
|
||||
|
@ -1,3 +1,5 @@
|
||||
import { jsonError } from "../../../common.js";
|
||||
|
||||
export async function onRequestDelete(context: RequestContext) {
|
||||
const { id } = context.data.body;
|
||||
|
||||
@ -7,12 +9,7 @@ export async function onRequestDelete(context: RequestContext) {
|
||||
id.length > 19 ||
|
||||
id.length < 17
|
||||
)
|
||||
return new Response('{"error":"Invalid ID"}', {
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
status: 400,
|
||||
});
|
||||
return jsonError("Invalid ID", 400);
|
||||
|
||||
await context.env.DATA.delete(`etmember_${id}`);
|
||||
await context.env.D1.prepare("DELETE FROM et_members WHERE id = ?;")
|
||||
@ -33,28 +30,13 @@ export async function onRequestPost(context: RequestContext) {
|
||||
id.length > 19 ||
|
||||
id.length < 17
|
||||
)
|
||||
return new Response('{"error":"Invalid user ID"}', {
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
status: 400,
|
||||
});
|
||||
return jsonError("Invalid user ID", 400);
|
||||
|
||||
if (typeof name !== "string" || !name.length || name.length > 32)
|
||||
return new Response('{"error":"Invalid name"}', {
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
status: 400,
|
||||
});
|
||||
return jsonError("Invalid name", 400);
|
||||
|
||||
if (await context.env.DATA.get(`etmember_${id}`))
|
||||
return new Response('{"error":"User is already a member"}', {
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
status: 400,
|
||||
});
|
||||
return jsonError("User is already a member", 400);
|
||||
|
||||
const createdAt = Date.now();
|
||||
const addingUser = context.data.current_user.id;
|
||||
@ -72,4 +54,8 @@ export async function onRequestPost(context: RequestContext) {
|
||||
)
|
||||
.bind(createdAt, addingUser, id, name)
|
||||
.run();
|
||||
|
||||
return new Response(null, {
|
||||
status: 204,
|
||||
});
|
||||
}
|
||||
|
Reference in New Issue
Block a user