Skip to content

Commit e6498b0

Browse files
docs(router): add examples of API routes (#610)
Co-authored-by: Deepak Paul <[email protected]>
1 parent e7d261a commit e6498b0

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

apps/docs-app/docs/features/api/overview.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,108 @@ With this configuration, Analog exposes the API routes under the `/services` pre
7474

7575
A route defined in `src/server/routes/v1/hello.ts` can now be accessed at `/services/v1/hello`.
7676

77+
## Dynamic API Routes
78+
79+
Dynamic API routes are defined by using the filename as the route path enclosed in square brackets. Parameters can be accessed via `event.context.params`.
80+
81+
```ts
82+
// /server/routes/v1/hello/[name].ts
83+
import { defineEventHandler } from 'h3';
84+
85+
export default defineEventHandler(
86+
(event: H3Event) => `Hello ${event.context.params?.['name']}!`
87+
);
88+
```
89+
90+
Another way to access route parameters is by using the `getRouterParam` function.
91+
92+
```ts
93+
// /server/routes/v1/hello/[name].ts
94+
import { defineEventHandler, getRouterParam } from 'h3';
95+
96+
export default defineEventHandler((event) => {
97+
const name = getRouterParam(event, 'name');
98+
return `Hello, ${name}!`;
99+
});
100+
```
101+
102+
## Specific HTTP request method
103+
104+
File names can be suffixed with `.get`, `.post`, `.put`, `.delete`, etc. to match the specific HTTP request method.
105+
106+
### GET
107+
108+
```ts
109+
// /server/routes/v1/users/[id].get.ts
110+
import { defineEventHandler, getRouterParam } from 'h3';
111+
112+
export default defineEventHandler(async (event) => {
113+
const id = getRouterParam(event, 'id');
114+
// TODO: fetch user by id
115+
return `User profile of ${id}!`;
116+
});
117+
```
118+
119+
### POST
120+
121+
```ts
122+
// /server/routes/v1/users.post.ts
123+
import { defineEventHandler, readBody } from 'h3';
124+
125+
export default defineEventHandler(async (event) => {
126+
const body = await readBody(event);
127+
// TODO: Handle body and add user
128+
return { updated: true };
129+
});
130+
```
131+
132+
The [h3 JSDocs](https://www.jsdocs.io/package/h3#package-index-functions) provide more info and utilities, including readBody.
133+
134+
## Requests with Query Parameters
135+
136+
Sample query `/api/v1/query?param1=Analog&param2=Angular`
137+
138+
```ts
139+
// routes/v1/query.ts
140+
import { defineEventHandler, getQuery } from 'h3';
141+
142+
export default defineEventHandler((event) => {
143+
const { param1, param2 } = getQuery(event);
144+
return `Hello, ${param1} and ${param2}!`;
145+
});
146+
```
147+
148+
## Catch-all Routes
149+
150+
Catch-all routes are helpful for fallback route handling.
151+
152+
```ts
153+
// routes/[...].ts
154+
export default defineEventHandler((event) => `Default page`);
155+
```
156+
157+
## Error Handling
158+
159+
If no errors are thrown, a status code of 200 OK will be returned. Any uncaught errors will return a 500 Internal Server Error HTTP Error.
160+
To return other error codes, throw an exception with createError
161+
162+
```ts
163+
// routes/v1/[id].ts
164+
import { defineEventHandler, getRouterParam, createError } from 'h3';
165+
166+
export default defineEventHandler((event) => {
167+
const param = getRouterParam(event, 'id');
168+
const id = parseInt(param ? param : '');
169+
if (!Number.isInteger(id)) {
170+
throw createError({
171+
statusCode: 400,
172+
statusMessage: 'ID should be an integer',
173+
});
174+
}
175+
return `ID is ${id}`;
176+
});
177+
```
178+
77179
## More Info
78180

79181
API routes are powered by [Nitro](https://nitro.unjs.io). See the Nitro docs for more examples around building API routes.

0 commit comments

Comments
 (0)