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

84 lines
2.2 KiB
TypeScript

import { Router } from "express";
import type { Request } from "express";
import { graphqlClient } from "../lib/graphqlClient";
import { respondWithError } from "../lib/error";
import {
GetLikeDocument,
LikesDocument,
CreateLikeDocument,
DeleteLikeDocument,
type GetLikeQuery,
type LikesQuery,
type CreateLikeMutation,
type DeleteLikeMutation,
} from "../../shared/generated/graphql";
interface GqlRequest extends Request {
gql?: typeof graphqlClient;
}
const router = Router();
// GET /api/likes/:id
router.get("/:id", async (req: GqlRequest, res) => {
try {
const client = req.gql || graphqlClient;
const { like } = await client.request<GetLikeQuery>(GetLikeDocument, {
id: req.params.id,
});
if (!like) return res.status(404).json({ message: "Like not found" });
res.json(like);
} catch (error) {
respondWithError(res, error, "Failed to fetch like");
}
});
// GET /api/likes
router.get("/", async (req: GqlRequest, res) => {
try {
const client = req.gql || graphqlClient;
const { likes } = await client.request<LikesQuery>(LikesDocument, {
workId: req.query.workId as string | undefined,
translationId: req.query.translationId as string | undefined,
commentId: req.query.commentId as string | undefined,
});
res.json(likes);
} catch (error) {
respondWithError(res, error, "Failed to fetch likes");
}
});
// POST /api/likes
router.post("/", async (req: GqlRequest, res) => {
try {
const client = req.gql || graphqlClient;
const { createLike } = await client.request<CreateLikeMutation>(
CreateLikeDocument,
{
input: req.body,
}
);
res.status(201).json(createLike);
} catch (error) {
respondWithError(res, error, "Failed to create like");
}
});
// DELETE /api/likes/:id
router.delete("/:id", async (req: GqlRequest, res) => {
try {
const client = req.gql || graphqlClient;
const { deleteLike } = await client.request<DeleteLikeMutation>(
DeleteLikeDocument,
{
id: req.params.id,
}
);
res.json({ success: deleteLike });
} catch (error) {
respondWithError(res, error, "Failed to delete like");
}
});
export default router;