Status code
Status code is a way to represent the result of a request. These HTTP responses indicate whether a specific HTTP request has been successfully completed. Responses are grouped in five classes:
- Informational responses (100-199)
- Successful responses (200-299)
- Redirection messages (300-399)
- Client error responses (400-499)
- Server error responses (500-599)
You can find more details about the status code in the MDN Web Docs.
The status codes listed below are defined by RFC 9110
List of status codes
ExpressoTS provides a list of status codes that you can use in your application represented by the StatusCode enum.
Usage example
Example of how to use the status code in a controller, use case, using res object, @http decorator etc.
Controller example
import { StatusCode } from "@expressots/core";
@controller("/user/create")
class CreateUserController {
    constructor(@inject(CreateUserUseCase) private createUserUseCase: CreateUserUseCase) {}
    @Post("/")
    execute(
        @body() data: ICreateUserRequestDTO,
        @response() res: Response
    ) {
        return this.createUserUseCase.execute(data);
    }
}
Using res object
import { StatusCode } from "@expressots/core";
@controller("/")
class ExampleController {
    @Get("/")
    execute(@response() res: Response) {
        res.status(StatusCode.OK).send("Hello World!");
    }
}
Using @http decorator
import { StatusCode } from "@expressots/core";
@controller("/")
class ExampleController {
    @Get("/")
    @Http(StatusCode.OK)
    execute() {
        return "Hello World!";
    }
}
Use case example
import { StatusCode } from "@expressots/core";
@provide(CreateUserUseCase)
class CreateUserUseCase {
    constructor(private userRepository: UserRepository) {}
    execute(data: ICreateUserRequestDTO): ICreateUserResponseDTO | null {
        try {
            const { name, email } = data;
            const user: User | null = this.userRepository.create(new User(name, email));
            if (!user) {
                Report.Error("User already exists", StatusCode.BadRequest, "create-user-usecase");
            }
            let response: ICreateUserResponseDTO;
            if (user !== null) {
                response = {
                    id: user.Id,
                    name: user.name,
                    email: user.email,
                    status: "success",
                };
                return response;
            }
            return null;
        } catch (error: any) {
            throw error;
        }
    }
}
Support us ❤️
ExpressoTS is an MIT-licensed open source project. It's an independent project with ongoing development made possible thanks to your support. If you'd like to help, please read our support guide.