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 { body } = context.data;
@ -5,23 +7,9 @@ export async function onRequestPost(context: RequestContext) {
!Array.isArray(body) ||
body.find((attachment) => typeof attachment !== "string")
)
return new Response(
'{"error":"Request body must be an array of strings"}',
{
headers: {
"content-type": "application/json",
},
status: 400,
},
);
return jsonError("Request body must be an array of strings", 400);
if (body.length > 3)
return new Response('{"error":"Too many video ids"}', {
headers: {
"content-type": "application/json",
},
status: 400,
});
if (body.length > 3) return jsonError("Too many video ids", 400);
const kvPromises = [];
@ -31,12 +19,7 @@ export async function onRequestPost(context: RequestContext) {
const kvResults = await Promise.allSettled(kvPromises);
if (kvResults.find((result) => result.status === "rejected"))
return new Response('{"error":"Failed to check status of attachments"}', {
headers: {
"content-type": "application/json",
},
status: 500,
});
return jsonError("Failed to check status of attachments", 500);
return new Response(null, {
status: kvResults.find((result) => result !== null) ? 409 : 204,