This project contains some useful utilities for building a restful API with express or nestjs. I manly use it for my personal projects. Feel free to contribute or fork this project
This project contains some predefined response classes, the idea behind it is to have a uniform response. Specifically, this library has three predefined response classes. The Success-, Failed-, and ErrorResponse. Each of these responses have a status, message and data field. The status field defines the status of the response, the available response status are here defined. The message field should contain a short message for the api consumer. The data field can contain some additional data for the api consumer.
each response class extends the AbstractResponse class.
@Controller('/api/v1/response')
export class ResponseController {
@Get('/success')
public success(
@Res() response: Response
): SucessResponse {
return new SuccessResponse('This is a success response').send(response);
}
@Get('/fail')
public failed(
@Res() response: Response
): FailResponse {
return new FailResponse('This is a fail response').send(response);
}
@Get('/error')
public error(
@Res() response: Response
): FailResponse {
return new ErrorResponse(ResponseCode.INTERNAL_SERVER_ERROR, 'This is a error response').send(response);
}
}This library also contains a basic logger class.
NestJS docs: https://docs.nestjs.com/techniques/logger
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useLogger(app.get(Logger));
await app.listen(1000);
}If you want to log incoming requests u can use the HttpInterceptor. It will log all incoming HTTP requests in the console.
NestJS docs: https://docs.nestjs.com/interceptors
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useGlobalInterceptors(new HttpInterceptor());
await app.listen(1000);
}// Return a response code value based on the provided http code
ResponseCodeUtility.getByHttpCode(200);