Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dmarzzucco/hanlderstatuscodeshttpsforexpress
Class module to handle HTTP responses for Express, using standard status codes. The ResponseStatusHTTPS class makes it easy to create consistent and structured responses for different situations in a web application.
https://github.com/dmarzzucco/hanlderstatuscodeshttpsforexpress
api backend express-js nodejs nosql solid-principles sql typescript
Last synced: about 1 month ago
JSON representation
Class module to handle HTTP responses for Express, using standard status codes. The ResponseStatusHTTPS class makes it easy to create consistent and structured responses for different situations in a web application.
- Host: GitHub
- URL: https://github.com/dmarzzucco/hanlderstatuscodeshttpsforexpress
- Owner: DMarzzucco
- Created: 2024-09-08T22:24:20.000Z (2 months ago)
- Default Branch: main
- Last Pushed: 2024-09-08T22:44:34.000Z (2 months ago)
- Last Synced: 2024-09-30T12:23:29.715Z (about 2 months ago)
- Topics: api, backend, express-js, nodejs, nosql, solid-principles, sql, typescript
- Language: TypeScript
- Homepage:
- Size: 5.86 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Hanlder Status Codes HTTPS for Express
This module provides a class to handle HTTP responses for Express, using standard status codes. The ResponseStatusHTTPS class makes it easy to create consistent and structured responses for different situations in a web application.
## Objective
The main goal of this module is to provide a standardized and consistent way to manage HTTP responses in an application. The implementation of this class is guided by SOLID principles and design parameters to:
- **Keep code clean:** Providing clear and specific methods for each type of response, avoiding code duplication and promoting reuse.
- **Ensure scalability:** Allowing easy addition of new types of responses or modification of existing ones without affecting other parts of the system.
- **Ensure high quality:** Applying design principles that improve the readability and maintainability of the code.
## Examples in Service
### First Option
```TS
async getByid(id: number): Promise> {
try {
const user = await prisma.users.findUnique({ where: { id: id } })
if (!user) {
return ResponseStatudsHTTPS.notFound(`User not found`)
}
return ResponseStatudsHTTPS.succes(user)
} catch (error: any) {
return ResponseStatudsHTTPS.errorServer(error.message)
}
}```
### Second Option```TS
async getByid(id: number): Promise> {
try {
const user = await prisma.users.findUnique({ where: { id: id } })
if (!user) {
return ResponseStatudsHTTPS.createResponse({status:Code.NOT_FOUND, message:"User not found"})
}
return ResponseStatudsHTTPS.createResponse({status:Code.OK, data:user})
} catch (error: any) {
return ResponseStatudsHTTPS.createResponse({status:Code.INTERNAL_SERVER_ERROR, message: error.message})
}
}```
## Examples in Controllers
```TS
public async getByid(req: Request, res: Response) {
const { id } = req.params
const result = await this.service.getByid(Number(id))
return res.status(result.statusCode).json(result.body)
}
```## Interface
```TS
interface ServiceResponse {
statusCode: httpsStatusCode; //Export Enums
body: {
message?: string;
data?: T;
}
}```
## Handler Error for NestJs
```TS
public async getByid(id: number): Promise {
try {
const user = await prisma.users.findUnique({ where: { id: id } })
if (!user) {
throw new ErrorManager({ type: "NOT_FOUND", message: "User not found" })
}
return user
} catch (error: any) {
throw new ErrorManager.createSignatureError(error.message)
}
}
```## Author
Made by Dario Marzzucco (@darmarzz)