Skip to content

Request

Request is an important part of handling HTTP requests in the application. It provides methods and properties to access data from the request, such as params, headers, and body.

In Laratype, Request classes are typically used to validate and filter input data from users before it is processed by the controller. This helps ensure that the received data is valid and secure.

INFO

Request classes are typically placed in the src/http/requests directory. You can create custom Request classes to handle specific requests, for example, CreateUserRequest or UpdateProfileRequest.

Creating a Request

Generate Request

You can use the Sauf command to create a new request:

sh
$ npx sauf make:request CreateUserRequest
sh
$ pnpx sauf make:request CreateUserRequest
sh
$ bunx sauf make:request CreateUserRequest

Laratype will create a new request file in the src/http/requests/ directory named CreateUserRequest.ts.

TIP

You can create multiple requests at once by passing multiple names.

See more
sh
$ npx sauf make:request CreateUserRequest UpdateProfileRequest
sh
$ pnpx sauf make:request CreateUserRequest UpdateProfileRequest
sh
$ bunx sauf make:request CreateUserRequest UpdateProfileRequest

Writing Request

Laratype uses Zod to define validation rules in the Request class. You can override the rules method to return a Zod schema describing the validation rules for the request.

ts
import { Request } from "@laratype/http";
import { z } from "zod";

export default class CreateUserRequest extends Request {
  public rules() {
    return z.object({
      email: z.string(),
      password: z.string().min(8),
      name: z.string(),
      firstName: z.string(),
      lastName: z.string(),
      age: z.number().min(18)
    })
  }
}

See more about Request API here

Commands

You can check the list of registered routes and related requests using the Sauf command:

sh
$ npx sauf route:list
sh
$ pnpx sauf route:list
sh
$ yarn sauf route:list
sh
$ bun sauf route:list