@elartedelcodigo/express-response-handler
v1.0.0
Published
Librería para gestionar las respuestas de un servicio web creado con express.
Downloads
7
Maintainers
Readme
Librería para gestionar las respuestas de un servicio web creado con express.
Instalación
npm install @elartedelcodigo/express-response-handler
Ejemplo
app
└─ server.ts
Archivo server.ts
import express, { Express, NextFunction, Request, Response } from 'express'
import { ExpressLogger } from '@elartedelcodigo/express-logger'
import { ResponseHandler, Result, PreconditionFailedError } from '@elartedelcodigo/express-response-handler'
export class App {
private app: Express
constructor() {
this.app = express()
}
initialize() {
ExpressLogger.initialize(this.app)
ResponseHandler.initialize(this.app, {
logger: ExpressLogger.getInstance(),
})
// eslint-disable-next-line
this.app.get('/estado', (req: Request, res: Response, next: NextFunction) => {
try {
const result = { servicio: 'activo' }
return Result.ok(res, result)
} catch (err) {
return Result.fail(res, err)
}
})
// eslint-disable-next-line
this.app.get('/throw-error', (req: Request, res: Response, next: NextFunction) => {
try {
throw new PreconditionFailedError()
} catch (err) {
return Result.fail(res, err)
}
})
// eslint-disable-next-line @typescript-eslint/no-unused-vars
this.app.use((err: Error, req: Request, res: Response, next: NextFunction) => {
return Result.fail(res, err)
})
}
listen() {
return this.app.listen(3000)
}
}
Resultado 1:
curl --location 'http://localhost:3000/estado'
{
"status": 200,
"message": "Tarea completada exitosamente.",
"data": {
"servicio": "activo"
}
}
Resultado 2:
curl --location 'http://localhost:3000/throw-error'
{
"status": 412,
"message": "No se cumple con alguna condición que es necesaria para completar la tarea.",
"timestamp": 1682729565
}