Skip to content

Commit a66cd6c

Browse files
authored
fix(events): use relative route paths to match Fastify prefix registration (#229)
All five route handlers in eventRoutes defined absolute /api/events* paths while app.ts also registered the plugin with prefix: '/api/events'. Fastify concatenates registration prefix and route path, producing double-prefixed endpoints (/api/events/api/events, /api/events/api/events/:slug, etc.) that are unreachable in production. Strip the /api/events prefix from every route definition so paths are relative (/, /:slug, /:slug/join, /:slug/leave, /:slug/attendees), consistent with every other route plugin in the codebase. Update the test buildApp() to register with { prefix: '/api/events' }, matching production. Inject URLs in existing tests already use the full /api/events/* paths and require no changes. Fixes #224.
1 parent af0b2a8 commit a66cd6c

2 files changed

Lines changed: 11 additions & 9 deletions

File tree

apps/backend/src/__tests__/event.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ async function buildApp(): Promise<FastifyInstance> {
7878
return mockJwtVerify();
7979
});
8080

81-
await app.register(eventRoutes);
81+
// Register with the same prefix used in production (app.ts) so that
82+
// tests exercise routes at their real paths — /api/events, /api/events/:slug, etc.
83+
await app.register(eventRoutes, { prefix: '/api/events' });
8284
await app.ready();
8385
return app;
8486
}

apps/backend/src/routes/event.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ type PaginatedAttendeesResponse = {
3636
}
3737

3838
export async function eventRoutes(app:FastifyInstance) {
39-
app.post('/api/events' , async(request: FastifyRequest<{
39+
app.post('/' , async(request: FastifyRequest<{
4040
Body: {
4141
name: string,
42-
description?: string,
43-
startDate: string,
42+
description?: string,
43+
startDate: string,
4444
location: string,
45-
endDate: string,
45+
endDate: string,
4646
isPublic?: boolean
4747
}}>, reply: FastifyReply) => {
4848
let decoded;
@@ -98,7 +98,7 @@ export async function eventRoutes(app:FastifyInstance) {
9898
})
9999

100100
//Returns event details and attendees count
101-
app.get('/api/events/:slug', async(request: FastifyRequest<{Params: {slug: string}}>, reply: FastifyReply) => {
101+
app.get('/:slug', async(request: FastifyRequest<{Params: {slug: string}}>, reply: FastifyReply) => {
102102
const paramsSlug = request.params.slug;
103103
const details = await app.prisma.event.findUnique({
104104
where: {
@@ -132,7 +132,7 @@ export async function eventRoutes(app:FastifyInstance) {
132132
return response;
133133
})
134134

135-
app.post('/api/events/:slug/join' , async(request: FastifyRequest<{Params: {slug: string}}>, reply: FastifyReply) => {
135+
app.post('/:slug/join' , async(request: FastifyRequest<{Params: {slug: string}}>, reply: FastifyReply) => {
136136
let decoded;
137137
try {
138138
decoded = await request.jwtVerify() as any;
@@ -172,7 +172,7 @@ export async function eventRoutes(app:FastifyInstance) {
172172

173173
})
174174

175-
app.delete('/api/events/:slug/leave',async(request: FastifyRequest<{Params: {slug: string}}>, reply: FastifyReply) => {
175+
app.delete('/:slug/leave',async(request: FastifyRequest<{Params: {slug: string}}>, reply: FastifyReply) => {
176176
let decoded;
177177
try {
178178
decoded = await request.jwtVerify() as any
@@ -211,7 +211,7 @@ export async function eventRoutes(app:FastifyInstance) {
211211
}
212212
})
213213

214-
app.get('/api/events/:slug/attendees', async(request: FastifyRequest<{Params: {slug: string}, Querystring: {page?:string; limit?: string}}>, reply: FastifyReply) => {
214+
app.get('/:slug/attendees', async(request: FastifyRequest<{Params: {slug: string}, Querystring: {page?:string; limit?: string}}>, reply: FastifyReply) => {
215215
const paramsSlug = request.params.slug;
216216
const page = Math.max(1, Number(request.query.page) || 1);
217217
const limit = Math.min(50, Number(request.query.limit) || 10);

0 commit comments

Comments
 (0)