diff --git a/packages/brisa/src/__fixtures__/api/index.ts b/packages/brisa/src/__fixtures__/api/index.ts new file mode 100644 index 000000000..a40b7dd07 --- /dev/null +++ b/packages/brisa/src/__fixtures__/api/index.ts @@ -0,0 +1,12 @@ +import type { RequestContext } from '@/types'; + +export function GET(request: RequestContext) { + return new Response(JSON.stringify({ hello: 'world' }), { + headers: { 'content-type': 'application/json' }, + }); +} + +export async function POST(request: RequestContext) { + const data = await request.json(); + return new Response(JSON.stringify(data)); +} diff --git a/packages/brisa/src/cli/serve/serve-options.test.tsx b/packages/brisa/src/cli/serve/serve-options.test.tsx index 6d8973c3e..1d5388385 100644 --- a/packages/brisa/src/cli/serve/serve-options.test.tsx +++ b/packages/brisa/src/cli/serve/serve-options.test.tsx @@ -882,6 +882,64 @@ describe.each(BASE_PATHS)('CLI: serve %s', (basePath) => { expect(json).toEqual({ hello: 'world' }); }); + it('should be possible to fetch an api route GET from root', async () => { + globalThis.mockConstants = { + ...globalThis.mockConstants, + IS_PRODUCTION: true, + I18N_CONFIG: undefined, + }; + + const response = await testRequest( + new Request(`http:///localhost:1234${basePath}/api`), + ); + const json = await response.json(); + + expect(response.status).toBe(200); + expect(json).toEqual({ hello: 'world' }); + }); + + it('should be possible to fetch an api route POST from root', async () => { + globalThis.mockConstants = { + ...globalThis.mockConstants, + IS_PRODUCTION: true, + I18N_CONFIG: undefined, + }; + + const response = await testRequest( + new Request(`http:///localhost:1234${basePath}/api`, { + method: 'POST', + body: JSON.stringify({ hello: 'world' }), + }), + ); + const json = await response.json(); + + expect(response.status).toBe(200); + expect(json).toEqual({ hello: 'world' }); + }); + + it('should be possible to fetch an api route GET from root (with i18n)', async () => { + const response = await testRequest( + new Request(`http:///localhost:1234${basePath}/es/api`), + ); + const json = await response.json(); + + expect(response.status).toBe(200); + expect(json).toEqual({ hello: 'world' }); + }); + + it('should be possible to fetch an api route POST from root (with i18n)', async () => { + const response = await testRequest( + new Request(`http:///localhost:1234${basePath}/es/api`, { + method: 'POST', + body: JSON.stringify({ hello: 'world' }), + }), + ); + const json = await response.json(); + + expect(response.status).toBe(200); + expect(json).toEqual({ hello: 'world' }); + }); + it('should not be possible to fetch an api route GET without the correct basePath', async () => { globalThis.mockConstants = { ...globalThis.mockConstants, diff --git a/packages/brisa/src/cli/serve/serve-options.tsx b/packages/brisa/src/cli/serve/serve-options.tsx index 6af5401b4..d97d04ab2 100644 --- a/packages/brisa/src/cli/serve/serve-options.tsx +++ b/packages/brisa/src/cli/serve/serve-options.tsx @@ -246,9 +246,9 @@ export async function getServeOptions() { ) { const locale = req.i18n.locale; const url = new URL(req.finalURL); - const pathname = url.pathname; + const firstPathnamePart = url.pathname.split('/')[locale ? 2 : 1]; const { route, isReservedPathname } = pagesRouter.match(req); - const isApi = pathname.startsWith(locale ? `/${locale}/api/` : '/api/'); + const isApi = firstPathnamePart === 'api'; const api = isApi ? rootRouter.match(req) : null; req.route = (isApi ? api?.route : route) as MatchedBrisaRoute;