npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

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() |