tercul-frontend/server/routes/work.ts
google-labs-jules[bot] 1dcd8f076c
feat: Fix TypeScript errors and improve type safety (#6)
This commit addresses 275 TypeScript compilation errors and improves type safety, code quality, and maintainability across the frontend codebase.

The following issues have been resolved:
- Standardized `translationId` to `number`
- Fixed missing properties on annotation types
- Resolved `tags` type mismatch
- Corrected `country` type mismatch
- Addressed date vs. string mismatches
- Fixed variable hoisting issues
- Improved server-side type safety
- Added missing null/undefined checks
- Fixed arithmetic operations on non-numbers
- Resolved `RefObject` type issues

Note: I was unable to verify the frontend changes due to local setup issues with the development server. The server would not start, and I was unable to run the Playwright tests.

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
2025-11-27 18:48:47 +01:00

73 lines
2.0 KiB
TypeScript

import { Router } from "express";
import type { Request } from "express";
import { graphqlClient } from "../lib/graphqlClient";
import { respondWithError } from "../lib/error";
import {
GetWorkDocument,
WorksDocument,
CreateWorkDocument,
type GetWorkQuery,
type WorksQuery,
type CreateWorkMutation,
} from "../../shared/generated/graphql";
const router = Router();
// GET /api/works
interface GqlRequest extends Request {
gql?: typeof graphqlClient;
}
router.get("/", async (req: GqlRequest, res) => {
try {
const variables = {
limit: req.query.limit ? Number(req.query.limit) : undefined,
offset: req.query.offset ? Number(req.query.offset) : undefined,
language: req.query.language as string | undefined,
authorId: req.query.authorId as string | undefined,
tagId: req.query.tagId as string | undefined,
search: req.query.search as string | undefined,
};
const client = req.gql || graphqlClient;
const { works } = await client.request<WorksQuery>(
WorksDocument,
variables
);
res.json(works);
} catch (error) {
respondWithError(res, error, "Failed to fetch works");
}
});
// GET /api/works/:id
router.get("/:id", async (req: GqlRequest, res) => {
try {
const client = req.gql || graphqlClient;
const { work } = await client.request<GetWorkQuery>(GetWorkDocument, {
id: req.params.id,
});
if (!work) return res.status(404).json({ message: "Work not found" });
res.json(work);
} catch (error) {
respondWithError(res, error, "Failed to fetch work");
}
});
// POST /api/works
router.post("/", async (req: GqlRequest, res) => {
try {
const client = req.gql || graphqlClient;
const { createWork } = await client.request<CreateWorkMutation>(
CreateWorkDocument,
{
input: req.body,
}
);
res.status(201).json(createWork);
} catch (error) {
respondWithError(res, error, "Failed to create work");
}
});
export default router;