Skip to content

Commit

Permalink
fix: improve API detection to allow api root entrypoint
Browse files Browse the repository at this point in the history
  • Loading branch information
aralroca committed Oct 22, 2024
1 parent 52808cc commit c1432aa
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
12 changes: 12 additions & 0 deletions packages/brisa/src/__fixtures__/api/index.ts
Original file line number Diff line number Diff line change
@@ -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));
}
58 changes: 58 additions & 0 deletions packages/brisa/src/cli/serve/serve-options.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions packages/brisa/src/cli/serve/serve-options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit c1432aa

Please sign in to comment.