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

74 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 {
GetAuthorDocument,
AuthorsDocument,
CreateAuthorDocument,
type AuthorsQuery,
type GetAuthorQuery,
type CreateAuthorMutation,
} from "../../shared/generated/graphql";
const router = Router();
// GET /api/authors
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,
search: req.query.search as string | undefined,
countryId: req.query.countryId as string | undefined,
};
const client = req.gql || graphqlClient;
const { authors } = await client.request<AuthorsQuery>(
AuthorsDocument,
variables
);
res.json(authors);
} catch (error) {
respondWithError(res, error, "Failed to fetch authors");
}
});
// GET /api/authors/:id (use id instead of slug to align with GraphQL schema)
router.get("/:id", async (req: GqlRequest, res) => {
try {
const client = req.gql || graphqlClient;
const { author } = await client.request<GetAuthorQuery>(
GetAuthorDocument,
{
id: req.params.id,
}
);
if (!author) return res.status(404).json({ message: "Author not found" });
res.json(author);
} catch (error) {
respondWithError(res, error, "Failed to fetch author");
}
});
// POST /api/authors
router.post("/", async (req: GqlRequest, res) => {
try {
const client = req.gql || graphqlClient;
const { createAuthor } = await client.request<CreateAuthorMutation>(
CreateAuthorDocument,
{
input: req.body,
}
);
res.status(201).json(createAuthor);
} catch (error) {
respondWithError(res, error, "Failed to create author");
}
});
export default router;