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:
$ npx sauf make:request CreateUserRequest$ pnpx sauf make:request CreateUserRequest$ bunx sauf make:request CreateUserRequestLaratype 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
$ npx sauf make:request CreateUserRequest UpdateProfileRequest$ pnpx sauf make:request CreateUserRequest UpdateProfileRequest$ bunx sauf make:request CreateUserRequest UpdateProfileRequestWriting 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.
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:
$ npx sauf route:list$ pnpx sauf route:list$ yarn sauf route:list$ bun sauf route:list