Skip to content

Commit c36d35b

Browse files
authored
replace remaining any usages with proper typings (#579)
Signed-off-by: lost_fanatic <bighna2005@gmail.com>
1 parent afdbb88 commit c36d35b

1 file changed

Lines changed: 34 additions & 23 deletions

File tree

apps/backend/src/routes/cards.ts

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,30 @@ interface _CardWithLinks {
4848
cardLinks: CardLinkWithPlatform[];
4949
}
5050

51+
function hasErrorCode(
52+
error: unknown,
53+
code: string,
54+
): error is { code: string } {
55+
return (
56+
typeof error === 'object' &&
57+
error !== null &&
58+
'code' in error &&
59+
typeof error.code === 'string' &&
60+
error.code === code
61+
);
62+
}
63+
5164
export async function cardRoutes(app: FastifyInstance): Promise<void> {
5265
app.addHook('preHandler', async (request, reply) => {
53-
const server = request.server as any;
66+
const server = request.server;
5467
if (typeof server?.authenticate === 'function') { await server.authenticate(request, reply); return }
55-
if (typeof (app as any).authenticate === 'function') { await (app as any).authenticate(request, reply); return }
68+
if (typeof app.authenticate === 'function') { await app.authenticate(request, reply); return }
5669
try { await request.jwtVerify() } catch (_e) { reply.status(401).send({ error: 'Unauthorized' }) }
5770
});
5871

5972
// ─── List Cards ───
6073
app.get('/', async (request: FastifyRequest, reply: FastifyReply): Promise<CardResponse[] | void> => {
61-
const userId = (request.user as { id: string }).id;
74+
const userId = request.user.id;
6275
try {
6376
return await cardService.listCards(app, userId)
6477
} catch (error) {
@@ -68,7 +81,7 @@ export async function cardRoutes(app: FastifyInstance): Promise<void> {
6881

6982
// ─── Creates Card ───
7083
app.post('/', async (request: FastifyRequest<{ Body: CreateCardBody }>, reply: FastifyReply): Promise<Card | void> => {
71-
const userId = (request.user as { id: string }).id;
84+
const userId = request.user.id;
7285
const parsed = createCardSchema.safeParse(request.body);
7386

7487
if (!parsed.success) {
@@ -78,47 +91,45 @@ export async function cardRoutes(app: FastifyInstance): Promise<void> {
7891
try {
7992
const card = await cardService.createCard(app, userId, parsed.data)
8093
return reply.status(201).send(card)
81-
} catch (error: any) {
82-
if (error?.code === 'OWNERSHIP'){return reply.status(403).send({ error: 'One or more links do not belong to your account' })}
94+
} catch (error) {
95+
if (hasErrorCode(error, 'OWNERSHIP')) {return reply.status(403).send({ error: 'One or more links do not belong to your account' })}
8396
return handleDbError(error, request, reply)
8497
}
8598
});
8699

87100
// ─── Update Card ───
88-
app.put('/:id/update', async (request: FastifyRequest<{ Params: CardParams; Body: UpdateCardBody }>, reply: FastifyReply) => {
89-
const userId = (request.user as { id: string }).id;
101+
102+
app.put('/:id', async (request: FastifyRequest<{ Params: CardParams; Body: UpdateCardBody }>, reply: FastifyReply): Promise<CardResponse> => {
103+
const userId = request.user.id;
90104
const { id } = request.params;
91105

92106
try {
93107
const parsed = updateCardSchema.safeParse(request.body)
94108
if (!parsed.success) {return reply.status(400).send({ error: 'Validation failed', details: parsed.error.flatten() })}
95109
const updated = await cardService.updateCard(app, userId, id, parsed.data)
96-
return reply.status(200).send(updated)
97-
} catch (error: any) {
98-
if(error.code === 'NOT_FOUND'){
99-
return reply.status(404).send({
100-
error: error.message,
101-
});
102-
}
103-
app.log.error(error)
104-
handleDbError(error,request,reply)
110+
if (!updated) {return reply.status(404).send({ error: 'Card not found' })}
111+
return updated
112+
} catch (error) {
113+
if (hasErrorCode(error, 'OWNERSHIP')) {return reply.status(403).send({ error: 'One or more links do not belong to your account' })}
114+
return handleDbError(error, request, reply)
105115
}
106116
});
107117

108118
// ─── Delete Card ───
109-
app.delete('/:id/delete', async (request: FastifyRequest<{ Params: CardParams }>, reply: FastifyReply): Promise<void> => {
110-
const userId = (request.user as { id: string }).id;
119+
120+
app.delete('/:id', async (request: FastifyRequest<{ Params: CardParams }>, reply: FastifyReply): Promise<void> => {
121+
const userId = request.user.id;
111122
const { id } = request.params;
112123

113124
try {
114125
await cardService.deleteCard(app, userId, id)
115126
return reply.status(204).send()
116-
} catch (error:any) {
117-
if (error?.code === 'NOT_FOUND') {
127+
} catch (error) {
128+
if (hasErrorCode(error, 'NOT_FOUND')) {
118129
return reply.status(404).send({ error: 'Card not found' });
119130
}
120131

121-
if (error?.code === 'LAST_CARD') {
132+
if (hasErrorCode(error, 'LAST_CARD')) {
122133
return reply.status(400).send({
123134
error: 'Cannot delete the last remaining card. A user must have at least one card.',
124135
});
@@ -129,7 +140,7 @@ export async function cardRoutes(app: FastifyInstance): Promise<void> {
129140

130141
// ─── Set Default Card ───
131142
app.put('/:id/default', async (request: FastifyRequest<{ Params: CardParams }>, reply: FastifyReply): Promise<object | void> => {
132-
const userId = (request.user as { id: string }).id;
143+
const userId = request.user.id;
133144
const { id } = request.params;
134145

135146
try {

0 commit comments

Comments
 (0)