rebell-core
v1.0.7
Published
Core for using rebell framework, including logs, decorator, middleware, message
Downloads
10
Readme
rebell-core
Core for using rebell framework, including logs, decorator, middleware, message
Route
core/decorator/route.decorator.ts is decorator for set route handler express if route decorator is empty the route will be using function name endpoint will be generate using path folder name
example:
- app
- test
endpoint will be set api/test
GET
example:
class TestController { @Get() //Default parameter when not declare create endpoint using method Name public async getAllTest(req: Request, res: Response, next: NextFunction) { res.json({ message: 'OK'}) } }
POST
example:
class TestController { @Post() //Default parameter when not declare create endpoint using method Name public async postTest(req: Request, res: Response, next: NextFunction) { res.json({ message: 'OK'}) } }
DELETE
example:
class TestController { @Delete() //Default parameter when not declare create endpoint using method Name public async deleteTest(req: Request, res: Response, next: NextFunction) { res.json({ message: 'OK'}) } }
OPTIONS
example:
class TestController { @Options() //Default parameter when not declare create endpoint using method Name public async optionsTest(req: Request, res: Response, next: NextFunction) { res.json({ message: 'OK'}) } }
PUT
example:
class TestController { @Put() //Default parameter when not declare create endpoint using method Name public async putTest(req: Request, res: Response, next: NextFunction) { res.json({ message: 'OK'}) } }
core/decorator/request.decorator.ts is decorator for validate data for valid request data, in this function run check validate Body, Query and validating using schema AJV
ValidateBody
example:
class TestController { @ValidateBody(Schema) //Default parameter when not declare create endpoint using method Name public async postTest(req: Request, res: Response, next: NextFunction) { res.json({ message: 'OK'}) } }
ValidateQuery
example:
class TestController { @ValidateQuery(Schema) //Default parameter when not declare create endpoint using method Name public async postTest(req: Request, res: Response, next: NextFunction) { res.json({ message: 'OK'}) } }
Middleware
core/middleware.ts reference from Express Middleware for create response handler
- loggerMiddleware
/**
*
* this is loggerMiddleware for logging request handlers
*
* @param request : express request
* @param response : express response
* @param next : express next function
*/
public async loggerMiddleware(request: Request, response: Response, next: NextFunction): Promise<void> {
logger.debug(`Run ${request.path}`);
logger.info(`Request ${request.path}`, JSON.stringify({
path: request.path,
method: request.method,
data: { ...request.body, ...request.query, ...request.params }
}));
next()
}
- responseMiddleware
/**
*
* This is responseMiddleware for intercepting response handlers from controller
* This value is dynamic, you can modify in result if not throw to exception
*
* @param request : express request
* @param response : express response
* @param next : express next function
*/
public async responseMiddleware(request: Request, response: Response, next: NextFunction): Promise<void> {
const oldJSON = response.json;
response.json = (data: any = {
statusCode: EHttpStatusCode.OK,
status: EStatus.SUCCESS,
message: ESuccessMessage.FETCH
}): any => {
data = {
statusCode: EHttpStatusCode.OK,
status: EStatus.SUCCESS,
message: ESuccessMessage.FETCH,
...data
}
logger.info(`Response ${request.path}`, JSON.stringify(data))
if (data && data.status === EStatus.FAILED)
return oldJSON.call(response.status(data.statusCode), {
status: data.status,
message: data.message,
errorCode: data.errorCode,
detail: data.detail
} as IResponseTypes)
else
return oldJSON.call(response.status(data.statusCode), {
status: data.status,
message: data.message,
detail: data.detail
} as IResponseTypes);
}
next()
}
- errorMiddleware
/**
*
* This is errorMiddleware for logging errors response from controller and next to responseMiddleware
* This value is dynamic, you can modify in exception Error
*
* @param error : any
* @param request : express request
* @param response : express response
* @param next : express next function
*/
public async errorMiddleware(error: any | '' | null | undefined, request: Request, response: Response, next: NextFunction): Promise<any> {
response.json({
statusCode: EHttpStatusCode.INTERNAL_SERVER_ERROR,
status: EStatus.FAILED,
message: error.message,
errorCode: error.errorCode,
detail: error.detail
} as IResponseTypes)
}
Response
core/type.ts is interface for declare all type used in this app
export interface IResponseTypes {
statusCode: EHttpStatusCode;
status: EStatus;
message: ESuccessMessage | EErrorMessage;
errorMessage: any;
errorCode: any;
detail?: any;
}
this interface use in the response.json() for create response success
example:
const result: IResponseTypes = {
statusCode: HttpStatusCode.OK,
status: Status.SUCCESS,
message: Message.FETCH
}
response.json(result)
and use next() for create response error
example:
const result: IResponseTypes = {
statusCode: HttpStatusCode.INTERNAL_SERVER_ERROR,
status: Status.FAILED,
message: Message.NOT_HANDLED,
detail: error.message
}
next(result)
Logs
core/logs.ts function for create logging
| Name | Function |
| --- | --- |
| logger.silly
| logger.silly()
|
| logger.debug
| logger.debug()
|
| logger.trace
| logger.trace()
|
| logger.info
| logger.info()
|
| logger.warn
| logger.warn()
|
| logger.error
| logger.error()
|
| logger.fatal
| logger.fatal()
|