chore: move more admin over to arktype validators

This commit is contained in:
DecDuck
2025-05-30 13:17:21 +10:00
parent 83a9b22d82
commit 85edc4cca2
16 changed files with 193 additions and 114 deletions

View File

@ -1,10 +1,11 @@
import { type } from "arktype";
import { throwingArktype } from "~/server/arktype";
import aclManager from "~/server/internal/acls";
import prisma from "~/server/internal/db/database";
const DeleteInvite = type({
id: "string",
});
}).configure(throwingArktype);
export default defineEventHandler<{
body: typeof DeleteInvite.infer;
@ -14,16 +15,7 @@ export default defineEventHandler<{
]);
if (!allowed) throw createError({ statusCode: 403 });
const body = DeleteInvite(await readBody(h3));
if (body instanceof type.errors) {
// hover out.summary to see validation errors
console.error(body.summary);
throw createError({
statusCode: 400,
statusMessage: body.summary,
});
}
const body = await readValidatedBody(h3, DeleteInvite);
await prisma.invitation.delete({ where: { id: body.id } });
return {};

View File

@ -1,13 +1,14 @@
import { type } from "arktype";
import { throwingArktype } from "~/server/arktype";
import aclManager from "~/server/internal/acls";
import prisma from "~/server/internal/db/database";
const CreateInvite = type({
isAdmin: "boolean",
username: "string",
email: "string.email",
isAdmin: "boolean?",
username: "string?",
email: "string.email?",
expires: "string.date.iso.parse",
});
}).configure(throwingArktype);
export default defineEventHandler<{
body: typeof CreateInvite.infer;
@ -17,16 +18,7 @@ export default defineEventHandler<{
]);
if (!allowed) throw createError({ statusCode: 403 });
const body = CreateInvite(await readBody(h3));
if (body instanceof type.errors) {
// hover out.summary to see validation errors
console.error(body.summary);
throw createError({
statusCode: 400,
statusMessage: body.summary,
});
}
const body = await readValidatedBody(h3, CreateInvite);
const invitation = await prisma.invitation.create({
data: body,

View File

@ -2,11 +2,12 @@ import aclManager from "~/server/internal/acls";
import prisma from "~/server/internal/db/database";
import objectHandler from "~/server/internal/objects";
import { type } from "arktype";
import { throwingArktype } from "~/server/arktype";
const DeleteGameImage = type({
gameId: "string",
imageId: "string",
});
}).configure(throwingArktype);
export default defineEventHandler<{
body: typeof DeleteGameImage.infer;
@ -14,14 +15,8 @@ export default defineEventHandler<{
const allowed = await aclManager.allowSystemACL(h3, ["game:image:delete"]);
if (!allowed) throw createError({ statusCode: 403 });
const body = DeleteGameImage(await readBody(h3));
if (body instanceof type.errors) {
console.error(h3.path, body.summary);
throw createError({
statusCode: 400,
statusMessage: body.summary,
});
}
const body = await readValidatedBody(h3, DeleteGameImage);
const gameId = body.gameId;
const imageId = body.imageId;

View File

@ -1,7 +1,7 @@
import aclManager from "~/server/internal/acls";
import prisma from "~/server/internal/db/database";
export default defineEventHandler(async (h3) => {
export default defineEventHandler<{ query: { id: string } }>(async (h3) => {
const allowed = await aclManager.allowSystemACL(h3, ["game:delete"]);
if (!allowed) throw createError({ statusCode: 403 });

View File

@ -3,6 +3,7 @@ import aclManager from "~/server/internal/acls";
import prisma from "~/server/internal/db/database";
import { handleFileUpload } from "~/server/internal/utils/handlefileupload";
export default defineEventHandler(async (h3) => {
const allowed = await aclManager.allowSystemACL(h3, ["game:update"]);
if (!allowed) throw createError({ statusCode: 403 });

View File

@ -1,27 +1,34 @@
import { type } from "arktype";
import { throwingArktype } from "~/server/arktype";
import aclManager from "~/server/internal/acls";
import prisma from "~/server/internal/db/database";
export default defineEventHandler(async (h3) => {
const allowed = await aclManager.allowSystemACL(h3, ["game:version:delete"]);
if (!allowed) throw createError({ statusCode: 403 });
const DeleteVersion = type({
id: "string",
versionName: "string",
}).configure(throwingArktype);
const body = await readBody(h3);
const gameId = body.id.toString();
const version = body.versionName.toString();
if (!gameId || !version)
throw createError({
statusCode: 400,
statusMessage: "Missing ID or versionName in body",
export default defineEventHandler<{ body: typeof DeleteVersion }>(
async (h3) => {
const allowed = await aclManager.allowSystemACL(h3, [
"game:version:delete",
]);
if (!allowed) throw createError({ statusCode: 403 });
const body = await readValidatedBody(h3, DeleteVersion);
const gameId = body.id.toString();
const version = body.versionName.toString();
await prisma.gameVersion.delete({
where: {
gameId_versionName: {
gameId: gameId,
versionName: version,
},
},
});
await prisma.gameVersion.delete({
where: {
gameId_versionName: {
gameId: gameId,
versionName: version,
},
},
});
return {};
});
return {};
},
);

View File

@ -1,41 +1,47 @@
import { type } from "arktype";
import { throwingArktype } from "~/server/arktype";
import aclManager from "~/server/internal/acls";
import prisma from "~/server/internal/db/database";
export default defineEventHandler(async (h3) => {
const allowed = await aclManager.allowSystemACL(h3, ["game:version:update"]);
if (!allowed) throw createError({ statusCode: 403 });
const UpdateVersionOrder = type({
id: "string",
versions: "string[]",
}).configure(throwingArktype);
const body = await readBody(h3);
const gameId = body.id?.toString();
// We expect an array of the version names for this game
const versions: string[] | undefined = body.versions;
if (!gameId || !versions || !Array.isArray(versions))
throw createError({
statusCode: 400,
statusMessage: "Missing id, versions or versions is not an array",
});
export default defineEventHandler<{ body: typeof UpdateVersionOrder }>(
async (h3) => {
const allowed = await aclManager.allowSystemACL(h3, [
"game:version:update",
]);
if (!allowed) throw createError({ statusCode: 403 });
const newVersions = await prisma.$transaction(
versions.map((versionName, versionIndex) =>
prisma.gameVersion.update({
where: {
gameId_versionName: {
gameId: gameId,
versionName: versionName,
const body = await readValidatedBody(h3, UpdateVersionOrder);
const gameId = body.id;
// We expect an array of the version names for this game
const versions = body.versions;
const newVersions = await prisma.$transaction(
versions.map((versionName, versionIndex) =>
prisma.gameVersion.update({
where: {
gameId_versionName: {
gameId: gameId,
versionName: versionName,
},
},
},
data: {
versionIndex: versionIndex,
},
select: {
versionIndex: true,
versionName: true,
platform: true,
delta: true,
},
}),
),
);
data: {
versionIndex: versionIndex,
},
select: {
versionIndex: true,
versionName: true,
platform: true,
delta: true,
},
}),
),
);
return newVersions;
});
return newVersions;
},
);

View File

@ -1,33 +1,39 @@
import { type } from "arktype";
import aclManager from "~/server/internal/acls";
import prisma from "~/server/internal/db/database";
import libraryManager from "~/server/internal/library";
import { parsePlatform } from "~/server/internal/utils/parseplatform";
const ImportVersion = type({
id: "string",
version: "string",
platform: "string",
launch: "string?",
launchArgs: "string?",
setup: "string?",
setupArgs: "string?",
onlySetup: "boolean?",
delta: "boolean?",
umuId: "string?",
});
export default defineEventHandler(async (h3) => {
const allowed = await aclManager.allowSystemACL(h3, ["import:version:new"]);
if (!allowed) throw createError({ statusCode: 403 });
const body = await readBody(h3);
const body = await readValidatedBody(h3, ImportVersion);
const gameId = body.id;
const versionName = body.version;
const platform = body.platform as string | undefined;
const launch = (body.launch ?? "") as string;
const launchArgs = (body.launchArgs ?? "") as string;
const setup = (body.setup ?? "") as string;
const setupArgs = (body.setupArgs ?? "") as string;
const onlySetup = body.onlySetup ?? (false as boolean);
const delta = (body.delta ?? false) as boolean;
const umuId = (body.umuId ?? "") as string;
if (!gameId || !versionName)
throw createError({
statusCode: 400,
statusMessage: "Game ID and version are required.",
});
if (!platform)
throw createError({ statusCode: 400, statusMessage: "Missing platform." });
const platform = body.platform;
const launch = body.launch ?? "";
const launchArgs = body.launchArgs ?? "";
const setup = body.setup ?? "";
const setupArgs = body.setupArgs ?? "";
const onlySetup = body.onlySetup ?? false;
const delta = body.delta ?? false;
const umuId = body.umuId ?? "";
const platformParsed = parsePlatform(platform);
if (!platformParsed)

View File

@ -59,5 +59,5 @@ export default defineEventHandler(async (h3) => {
const gameReadStream = fs.createReadStream(gameFile, { start, end: end - 1 }); // end needs to be offset by 1
return gameReadStream;
return sendStream(h3, gameReadStream);
});

14
server/arktype.ts Normal file
View File

@ -0,0 +1,14 @@
import { configure } from "arktype/config";
export const throwingArktype = configure({
onFail: (errors) => errors.throw(),
actual: () => ""
});
// be sure to specify both the runtime and static configs
declare global {
interface ArkEnv {
onFail: typeof throwingArktype.onFail;
}
}