Skip to content

Commit

Permalink
refactor: change request method from lowercase to UPPERCASE
Browse files Browse the repository at this point in the history
  • Loading branch information
aralroca committed Oct 3, 2023
1 parent 3609e29 commit 9c129b0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
12 changes: 6 additions & 6 deletions docs/02-building-your-application/01-routing/05-api-routes.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ API routes provide a solution to build a **public API** with Brisa.

Any file inside the folder `src/api` is mapped to `/api/*` and will be treated as an API endpoint. They are server-side only bundles and won't increase your client-side bundle size.

You can export any lowercase [request method](https://en.wikipedia.org/wiki/HTTP#Request_methods): `get`, `post`, `patch`, `put`, `delete`, etc.
You can export any uppercase [request method](https://en.wikipedia.org/wiki/HTTP#Request_methods): `GET`, `POST`, `PATCH`, `PUT`, `DELETE`, etc.

For example, the following API `get` endpoint returns a JSON response with a status code of `200`:
For example, the following API `GET` endpoint returns a JSON response with a status code of `200`:

```ts filename="src/api/hello.ts" switcher
import { type RequestContext } from "brisa";

export function get(request: RequestContext) {
export function GET(request: RequestContext) {
const responseData = JSON.stringify({
message: "Hello world from Brisa!"
})
Expand All @@ -38,7 +38,7 @@ We have access to the route through the `RequestContext` and we can access both
```ts filename="src/api/user/[username].ts" switcher
import { type RequestContext } from "brisa";

export function get({ route: { query, params } }: RequestContext) {
export function GET({ route: { query, params } }: RequestContext) {
const { id } = params
return new Response(`Hello world ${query.username} with id=${id}!`);
}
Expand All @@ -64,7 +64,7 @@ Example:
```ts filename="src/api/user/[username].ts" switcher
import { type RequestContext } from "brisa";

export function get({ i18n, route: { query, params } }: RequestContext) {
export function GET({ i18n, route: { query, params } }: RequestContext) {
const { id } = params
return new Response(i18n.t('hello', { name: params.username, id }));
}
Expand Down Expand Up @@ -114,7 +114,7 @@ Catch all routes can be made optional by including the parameter in double brack
You can set CORS headers on a `Response` using the standard Web API methods:

```ts
export async function get(request: Request) {
export async function GET(request: Request) {
return new Response('Hello, Brisa!', {
status: 200,
headers: {
Expand Down
6 changes: 4 additions & 2 deletions src/cli/serve.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,13 @@ async function handleRequest(req: RequestContext, isAnAsset: boolean) {
// API
if (isApi && api?.route && !api?.isReservedPathname) {
const module = await import(api.route.filePath);
const method = req.method.toLowerCase();
const method = req.method.toUpperCase();

req.route = api.route;

return module[method]?.(req);
const response = module[method]?.(req)

if (response) return response;
}

// Assets
Expand Down

0 comments on commit 9c129b0

Please sign in to comment.