feat: annotated client routes

This commit is contained in:
DecDuck
2025-08-10 15:51:10 +10:00
parent 824b4e708b
commit 7c234067a5
31 changed files with 479 additions and 389 deletions

View File

@ -1,7 +1,17 @@
import { type } from "arktype";
import { throwingArktype } from "~/server/arktype";
import { defineClientEventHandler } from "~/server/internal/clients/event-handler";
import userLibraryManager from "~/server/internal/userlibrary";
export default defineClientEventHandler(async (h3, { fetchUser }) => {
const EntryRemove = type({
id: "string",
}).configure(throwingArktype);
/**
* Remove entry from user's collection
* @param id Collection ID
*/
export default defineClientEventHandler(async (h3, { fetchUser, body }) => {
const user = await fetchUser();
const id = getRouterParam(h3, "id");
@ -11,10 +21,7 @@ export default defineClientEventHandler(async (h3, { fetchUser }) => {
statusMessage: "ID required in route params",
});
const body = await readBody(h3);
const gameId = body.id;
if (!gameId)
throw createError({ statusCode: 400, statusMessage: "Game ID required" });
const successful = await userLibraryManager.collectionRemove(
gameId,
@ -27,4 +34,4 @@ export default defineClientEventHandler(async (h3, { fetchUser }) => {
statusMessage: "Collection not found",
});
return {};
});
}, EntryRemove);

View File

@ -1,7 +1,17 @@
import { type } from "arktype";
import { throwingArktype } from "~/server/arktype";
import { defineClientEventHandler } from "~/server/internal/clients/event-handler";
import userLibraryManager from "~/server/internal/userlibrary";
export default defineClientEventHandler(async (h3, { fetchUser }) => {
const AddEntry = type({
id: "string",
}).configure(throwingArktype);
/**
* Add entry to collection by game ID
* @param id Collection ID
*/
export default defineClientEventHandler(async (h3, { fetchUser, body }) => {
const user = await fetchUser();
const id = getRouterParam(h3, "id");
@ -11,10 +21,7 @@ export default defineClientEventHandler(async (h3, { fetchUser }) => {
statusMessage: "ID required in route params",
});
const body = await readBody(h3);
const gameId = body.id;
if (!gameId)
throw createError({ statusCode: 400, statusMessage: "Game ID required" });
return await userLibraryManager.collectionAdd(gameId, id, user.id);
});
}, AddEntry);

View File

@ -1,15 +1,14 @@
import { defineClientEventHandler } from "~/server/internal/clients/event-handler";
import userLibraryManager from "~/server/internal/userlibrary";
/**
* Delete collection by ID
* @param id Collection ID
*/
export default defineClientEventHandler(async (h3, { fetchUser }) => {
const user = await fetchUser();
const id = getRouterParam(h3, "id");
if (!id)
throw createError({
statusCode: 400,
statusMessage: "ID required in route params",
});
const id = getRouterParam(h3, "id")!;
// Verify collection exists and user owns it
// Will not return the default collection
@ -27,5 +26,5 @@ export default defineClientEventHandler(async (h3, { fetchUser }) => {
});
await userLibraryManager.deleteCollection(id);
return { success: true };
return;
});

View File

@ -1,15 +1,14 @@
import { defineClientEventHandler } from "~/server/internal/clients/event-handler";
import userLibraryManager from "~/server/internal/userlibrary";
/**
* Fetch collection by ID
* @param id Collection ID
*/
export default defineClientEventHandler(async (h3, { fetchUser }) => {
const user = await fetchUser();
const id = getRouterParam(h3, "id");
if (!id)
throw createError({
statusCode: 400,
statusMessage: "ID required in route params",
});
const id = getRouterParam(h3, "id")!;
// Fetch specific collection
// Will not return the default collection

View File

@ -1,15 +1,20 @@
import { type } from "arktype";
import { throwingArktype } from "~/server/arktype";
import { defineClientEventHandler } from "~/server/internal/clients/event-handler";
import userLibraryManager from "~/server/internal/userlibrary";
export default defineClientEventHandler(async (h3, { fetchUser }) => {
const EntryDelete = type({
id: "string",
}).configure(throwingArktype);
/**
* Remove game from user's library
*/
export default defineClientEventHandler(async (h3, { fetchUser, body }) => {
const user = await fetchUser();
const body = await readBody(h3);
const gameId = body.id;
if (!gameId)
throw createError({ statusCode: 400, statusMessage: "Game ID required" });
await userLibraryManager.libraryRemove(gameId, user.id);
return {};
});
}, EntryDelete);

View File

@ -1,15 +1,21 @@
import { type } from "arktype";
import { throwingArktype } from "~/server/arktype";
import { defineClientEventHandler } from "~/server/internal/clients/event-handler";
import userLibraryManager from "~/server/internal/userlibrary";
export default defineClientEventHandler(async (h3, { fetchUser }) => {
const AddEntry = type({
id: "string",
}).configure(throwingArktype);
/**
* Add game to user's library
*/
export default defineClientEventHandler(async (h3, { fetchUser, body }) => {
const user = await fetchUser();
const body = await readBody(h3);
const gameId = body.id;
if (!gameId)
throw createError({ statusCode: 400, statusMessage: "Game ID required" });
// Add the game to the default collection
await userLibraryManager.libraryAdd(gameId, user.id);
return {};
});
}, AddEntry);

View File

@ -1,6 +1,9 @@
import { defineClientEventHandler } from "~/server/internal/clients/event-handler";
import userLibraryManager from "~/server/internal/userlibrary";
/**
* Fetch user's library
*/
export default defineClientEventHandler(async (h3, { fetchUser }) => {
const user = await fetchUser();

View File

@ -1,6 +1,9 @@
import { defineClientEventHandler } from "~/server/internal/clients/event-handler";
import userLibraryManager from "~/server/internal/userlibrary";
/**
* Fetch all of user's collections
*/
export default defineClientEventHandler(async (h3, { fetchUser }) => {
const user = await fetchUser();

View File

@ -1,14 +1,19 @@
import { type } from "arktype";
import { throwingArktype } from "~/server/arktype";
import { defineClientEventHandler } from "~/server/internal/clients/event-handler";
import userLibraryManager from "~/server/internal/userlibrary";
export default defineClientEventHandler(async (h3, { fetchUser }) => {
const CreateCollection = type({
name: "string",
}).configure(throwingArktype);
/**
* Create new collection
*/
export default defineClientEventHandler(async (h3, { fetchUser, body }) => {
const user = await fetchUser();
const body = await readBody(h3);
const name = body.name;
if (!name)
throw createError({ statusCode: 400, statusMessage: "Requires name" });
// Create the collection using the manager
const newCollection = await userLibraryManager.collectionCreate(
@ -16,4 +21,4 @@ export default defineClientEventHandler(async (h3, { fetchUser }) => {
user.id,
);
return newCollection;
});
}, CreateCollection);