Parameter Decorators
Nova provides a rich set of parameter decorators to extract data from incoming requests — path params, query strings, request body, files, and more.
@Path(name)
Extracts a value from the URL path parameters. The type is automatically cast based on the TypeScript type annotation.
| Parameter | Type | Required | Description |
|---|---|---|---|
name | string | Required | Name of the URL parameter (must match the :param in the route path). |
Supported types: string, number, boolean, Date
@Get('/:id')
async findById(@Path('id') id: number) {
// `id` is automatically cast to number
return { id };
} @Query(name, opts?)
Extracts a value from the query string. Supports optional queries.
| Parameter | Type | Required | Description |
|---|---|---|---|
name | string | Required | Name of the query parameter. |
opts.optional | boolean | Optional | If true, the parameter is not required. Defaults to false. |
@Get('/search')
async search(
@Query('term') term: string,
@Query('page', { optional: true }) page?: number,
) {
// GET /search?term=nova&page=2
return { term, page };
} @Body(schema)
Validates and extracts the request body using a Zod schema. Only one @Body per method is allowed.
| Parameter | Type | Required | Description |
|---|---|---|---|
schema | z.ZodSchema | Required | The Zod schema to validate the request body against. |
const CreateUserSchema = z.object({
name: z.string().min(2),
email: z.string().email(),
age: z.number().optional(),
});
@Post()
async create(
@Body(CreateUserSchema) data: z.infer<typeof CreateUserSchema>
) {
// data is validated and typed
return { id: 1, ...data };
} The request must have Content-Type: application/json or application/x-www-form-urlencoded. Otherwise, a validation error is returned.
@File(name, opts?)
Extracts a file from a multipart/form-data request.
| Parameter | Type | Required | Description |
|---|---|---|---|
name | string | Required | The name of the file field in the form data. |
opts.optional | boolean | Optional | If true, the file is not required. Defaults to false. |
import { File, MultipartFile } from '@abrahambass/nova';
@Post('/upload')
async upload(
@File('avatar') avatar: MultipartFile,
@File('cover', { optional: true }) cover?: MultipartFile,
) {
// avatar is required, cover is optional
return { filename: avatar.filename };
} @CurrentUser()
Extracts the authenticated user from req.user. Only one per method.
@RequiresAuth()
@Get('/me')
async getProfile(@CurrentUser() user: any) {
return user;
} @Request()
Injects the raw Fastify request object. Only one per method.
import type { NovaRequest } from '@abrahambass/nova';
@Get('/info')
async info(@Request() req: NovaRequest) {
return { ip: req.ip, method: req.method };
} @Reply()
Injects the raw Fastify reply object. Only one per method.
import type { NovaResponse } from '@abrahambass/nova';
@Get('/download')
async download(@Reply() reply: NovaResponse) {
reply.header('Content-Type', 'application/octet-stream');
return reply.send(buffer);
} @Middleware(...fns)
Attaches middleware functions to a controller (class-level) or a specific method.
import { Middleware, Controller, Get } from '@abrahambass/nova';
const logRequest = async (req, reply) => {
console.log(`Request: ${req.method} ${req.url}`);
};
// Controller-level middleware (applies to all methods)
@Middleware(logRequest)
@Controller('/users')
class UserController {
// Method-level middleware (only this method)
@Middleware(anotherMiddleware)
@Get()
async findAll() { ... }
} Decorator Summary
| Decorator | Target | Parameters | Description |
|---|---|---|---|
@Path(name) | Parameter | name required | Extract URL path parameter |
@Query(name, opts?) | Parameter | name required | Extract query string parameter |
@Body(schema) | Parameter | schema required | Validate & extract request body |
@File(name, opts?) | Parameter | name required | Extract multipart file |
@CurrentUser() | Parameter | — | Extract authenticated user |
@Request() | Parameter | — | Inject raw Fastify request |
@Reply() | Parameter | — | Inject raw Fastify reply |
@Middleware(...fns) | Class / Method | fns required | Attach middleware functions |