tercul-frontend/server/routes/translation.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

111 lines
3.0 KiB
TypeScript

import { Router } from "express";
import type { Request } from "express";
import { graphqlClient } from "../lib/graphqlClient";
import { respondWithError } from "../lib/error";
import {
GetTranslationDocument,
TranslationsDocument,
CreateTranslationDocument,
UpdateTranslationDocument,
DeleteTranslationDocument,
type GetTranslationQuery,
type TranslationsQuery,
type CreateTranslationMutation,
type UpdateTranslationMutation,
type DeleteTranslationMutation,
} from "../../shared/generated/graphql";
interface GqlRequest extends Request {
gql?: typeof graphqlClient;
}
const router = Router();
// GET /api/translations/:id
router.get("/:id", async (req: GqlRequest, res) => {
try {
const client = req.gql || graphqlClient;
const { translation } = await client.request<GetTranslationQuery>(
GetTranslationDocument,
{
id: req.params.id,
}
);
if (!translation)
return res.status(404).json({ message: "Translation not found" });
res.json(translation);
} catch (error) {
respondWithError(res, error, "Failed to fetch translation");
}
});
// GET /api/translations
router.get("/", async (req: GqlRequest, res) => {
try {
const client = req.gql || graphqlClient;
const { translations } = await client.request<TranslationsQuery>(
TranslationsDocument,
{
workId: req.query.workId as string,
language: req.query.language as string | undefined,
limit: req.query.limit ? Number(req.query.limit) : undefined,
offset: req.query.offset ? Number(req.query.offset) : undefined,
}
);
res.json(translations);
} catch (error) {
respondWithError(res, error, "Failed to fetch translations");
}
});
// POST /api/translations
router.post("/", async (req: GqlRequest, res) => {
try {
const client = req.gql || graphqlClient;
const { createTranslation } = await client.request<CreateTranslationMutation>(
CreateTranslationDocument,
{
input: req.body,
}
);
res.status(201).json(createTranslation);
} catch (error) {
respondWithError(res, error, "Failed to create translation");
}
});
// PUT /api/translations/:id
router.put("/:id", async (req: GqlRequest, res) => {
try {
const client = req.gql || graphqlClient;
const { updateTranslation } = await client.request<UpdateTranslationMutation>(
UpdateTranslationDocument,
{
id: req.params.id,
input: req.body,
}
);
res.json(updateTranslation);
} catch (error) {
respondWithError(res, error, "Failed to update translation");
}
});
// DELETE /api/translations/:id
router.delete("/:id", async (req: GqlRequest, res) => {
try {
const client = req.gql || graphqlClient;
const { deleteTranslation } = await client.request<DeleteTranslationMutation>(
DeleteTranslationDocument,
{
id: req.params.id,
}
);
res.json({ success: deleteTranslation });
} catch (error) {
respondWithError(res, error, "Failed to delete translation");
}
});
export default router;