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.

ParameterTypeRequiredDescription
name string Required Name of the URL parameter (must match the :param in the route path).

Supported types: string, number, boolean, Date

Example TypeScript
@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.

ParameterTypeRequiredDescription
name string Required Name of the query parameter.
opts.optional boolean Optional If true, the parameter is not required. Defaults to false.
Example TypeScript
@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.

ParameterTypeRequiredDescription
schema z.ZodSchema Required The Zod schema to validate the request body against.
Example TypeScript
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 };
}
⚠️ Important

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.

ParameterTypeRequiredDescription
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.
Example TypeScript
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.

Example TypeScript
@RequiresAuth()
@Get('/me')
async getProfile(@CurrentUser() user: any) {
  return user;
}

@Request()

Injects the raw Fastify request object. Only one per method.

Example TypeScript
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.

Example TypeScript
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.

Example TypeScript
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

DecoratorTargetParametersDescription
@Path(name)Parametername requiredExtract URL path parameter
@Query(name, opts?)Parametername requiredExtract query string parameter
@Body(schema)Parameterschema requiredValidate & extract request body
@File(name, opts?)Parametername requiredExtract multipart file
@CurrentUser()ParameterExtract authenticated user
@Request()ParameterInject raw Fastify request
@Reply()ParameterInject raw Fastify reply
@Middleware(...fns)Class / Methodfns requiredAttach middleware functions