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

@pamstarter/epts-engine

v2.0.8

Published

Platform engine for express typescript starter

Downloads

11

Readme

Epts-Engine (Express-Typescript-Engine)

NOTE: Libraries are created to help me speed up the process of creating projects that can be personalized, you can like it or not, you can use it or ignore it, thanks.

Quickly create express apps, register routes and apply middleware right in the controller

Installation

  • With npm:

    $ npm install @pamstarter/epts-engine
  • With yarn:

    $ yarn add @pamstarter/epts-engine

Quick Start

Typescript

At file src/app.ts:

import EptsEngine from '@pamstarter/epts-engine';
import { controllers, expressConfig as express } from '~config/app.config';

const app = new EptsEngine({
   app: {
      port: 3000, // server will run at port
      url: 'http://localhost:3000' // config app url for getUrl helper
   },
   express,
   controllers,
});

// Initialize the application and start the server
app.init();

At file src/config/app.config.ts

import { IExpress, IRegisterController } from '@pamstarter/epts-engine';

import WelcomeController from '~controller/welcome.controller';

export const expressConfig: IExpress = {
   use: [
      // Use method like express.use()
   ],
   set: {
      // Use method like express.set()
   }
};

export const controllers: IRegisterController[] = [
   // Register controllers
   WelcomeController,
];

At file src/controllers/welcome.controller.ts

  • Controller Config
    • routePrefix: set path start at
    • routeMiddlewareGroups: set of middleware grouped and used within the controller, possibly one or more group names
    • routeMiddlewares: separate set of middleware with different purposes used in controller, can be one or more middleware
    • routes method: return an object whose key is the route method and the value will be an object whose key is the path and its value can be one or more router handlers
import { Controller, IControllerArgs, IRoutes } from '@pamstarter/epts-engine';
import { Request, Response } from 'express';

class WelcomeController extends Controller {
   routePrefix: string = '/welcome'
   routeMiddlewareGroup: 'middleware_group_name' // or [...'middleware_group_name']
   routeMiddlewares: IControllerArgs | IControllerArgs[] = (req, res, next) => {
      next();
   };

   routes(): IRoutes {
      return {
         get: {
            '/': this.helloWorld, // [GET] /welcome/
         },
      };
   }

   helloWorld(req: Request, res: Response) {
      res.json({
         message: 'hello world',
      });
   }
}

export default new WelcomeController;

API Reference

Application

Types:

interface IEptsEngineConfigurations {
   app?: {
      port?: string | number
      url?: string
   };
   express?: IExpress;
   middlewareGroup?: IMiddlewareGroups;
   controllers?: IRegisterController[];
   custom?: {
      errorHandler?: IHandleError
   };

   [path: string]: any;
}
IExpress Type:
interface IExpress {
   use?: IExpressUse;
   set?: IExpressSet;

   [index: string]: any;
}
IMiddlewareGroups Type:
interface IMiddlewareGroups {
   [groupName: string]: IControllerArgs[];
}
IRegisterController Type:
interface IRegisterController extends IController {
}
IHandleError Type:
type IHandleError = (error: HttpError, request: Request, response: Response, next: NextFunction) => any

Methods

init:

Initialize the application and start the server

start:

Start the server

stop:

Stop the server

Controller

Types:

interface IController {
   routePrefix?: string;
   routeMiddlewareGroup?: string | string[];
   routeMiddlewares?: IControllerArgs | IControllerArgs[];

   routes(): IRoutes;
}
IControllerArgs Type:
type IControllerArgs = (request: Request, response: Response, next: NextFunction) => any
IRoutes Type:
interface IRoutes {
   all?: IRouteOption;
   get?: IRouteOption;
   post?: IRouteOption;
   put?: IRouteOption;
   patch?: IRouteOption;
   delete?: IRouteOption;
   options?: IRouteOption;
   head?: IRouteOption;
}
IRouteOption Type:
interface IRouteOption {
   [path: string]: IControllerArgs | IControllerArgs[]
}

Properties

routePrefix:

Set route path starting from

  • @type: string | undefined
  • @default: undefined
routeMiddlewareGroup:

Use the middleware pool registered at the configuration app for the entire controller route

  • @type: string | string[] | undefined
  • @default: undefined
routeMiddlewares:

Use middleware for the whole controller route

  • @type: IControllerArgs | IControllerArgs[] | undefined
  • @default: undefined

Methods:

routes:

Register the routes

  • @return: IRoutes

Config

The config will be saved to the /.epts-engine/config.json file, the functions below only set the object, not save, if you want to save, use the save method after set, update and forget

Types:

interface IConfig {
   all(): object;

   has(key: IConfigKeyParam): boolean;

   get<T = IConfigValueReturn>(key: IConfigKeyParam, defaultValue: T): T;

   set<T = IConfigValueReturn>(key: string | object, value: T): IConfig;

   update<T = IConfigValueReturn>(key: IConfigKeyParam, updater: IConfigUpdater<T>): IConfig;

   forget(key: IConfigKeyParam): IConfig;

   save(): void;
}
IConfigKeyParam Type:
type IConfigKeyParam = string | string[]
IConfigValue Type:
type IConfigValue = IConfigKey | number | boolean
IConfigValueReturn Type:
type IConfigValueReturn = IConfigValue | IConfigValue[]
IConfigUpdater Type:
type IConfigUpdater<T = any> = (value: T) => T

Methods

get

Get configuration value

  • @type: T default string
  • @param:
    • name: key
    • type: string | string[]
  • @param:
    • name: defaultValue
    • type: T
    • default: null
  • @return: T
set

Create or replace configuration

  • @type: T default string
  • @param:
    • name: key
    • type: string | object
  • @param:
    • name: value
    • type: T
  • @return: IConfig
update

Update an existing configuration

  • @type: T default string
  • @param:
    • name: key
    • type: string | object
  • @param:
    • name: updater
    • type: IConfigUpdater<T>
  • @return: IConfig
has

Check the configuration exists

  • @param
    • name: key
    • type: string | string[]
  • @return: boolen
forget

Delete a configuration

  • @param
    • name: key
    • type: string | string[]
  • @return: boolen

Helper Methods

App

getRootDirectory

Get path from root directory

  • rest @param:
    • name: path
    • type: string
  • @return string
getEnv

Get the value configured in the environment variable

  • @param:
    • name: key
    • type: string
  • @param:
    • name: defaultValue
    • type: string | null
    • default: null
  • @return: string
getUrl

Get path starting from url defined at app.url

  • @param:
    • name: path
    • type: string | null
    • default: null
  • @return: string | null

Configuration

The config will be saved to the /.epts-engine/config.json file, the functions below only set the object, not save, if you want to save, use the save method after updateConfig, setConfig and forgetConfig

getConfig

Get configuration value

  • @type: T default string
  • @param:
    • name: key
    • type: string | string[]
  • @param:
    • name: defaultValue
    • type: T
    • default: null
  • @return: T

Example:

import { getConfig } from '@pamstarter/epts-engine';

getConfig<string>('app.port') // return '3000'
setConfig

Create or replace configuration

  • @type: T default string
  • @param:
    • name: key
    • type: string | object
  • @param:
    • name: value
    • type: T
  • @return: IConfig

Example One:

import { setConfig } from '@pamstarter/epts-engine';

setConfig<number>('app.port', 4000) // return IConfig

Example Two:

import { setConfig } from '@pamstarter/epts-engine';

setConfig({
   app: {
      port: 4000
   }
})

Example for save:

import { setConfig } from '@pamstarter/epts-engine';

setConfig<number>('app.port', 4000).save() // return void
updateConfig

Update an existing configuration

  • @type: T default string
  • @param:
    • name: key
    • type: string | object
  • @param:
    • name: updater
    • type: IConfigUpdater<T>
  • @return: IConfig

Updater type:

type IConfigUpdater<T = any> = (value: T) => T

Example:

import { updateConfig } from '@pamstarter/epts-engine';

// port 3000
updateConfig<number>('app.port', (port => {
   return port + 1 // 3001 
})) // return IConfig

Example for save:

import { updateConfig } from '@pamstarter/epts-engine';

updateConfig<number>('app.port', (port /* 3000 */ => {
   return port + 1 // 3001 
})).save() // return IConfig
hasConfig

Check the configuration exists

  • @param
    • name: key
    • type: string | string[]
  • @return: boolen

Example:

import { hasConfig } from '@pamstarter/epts-engine';

/*
  {
     app: {
        port: 3000,
        url: 'http://localhost:3000'
     }
  }
*/

hasConfig('app.port') // true
hasConfig('app.bar') // false
forgetConfig

Delete a configuration

  • @param
    • name: key
    • type: string | string[]
  • @return: boolen

Example:

import { forgetConfig } from '@pamstarter/epts-engine';

/*
  {
     app: {
        port: 3000,
        url: 'http://localhost:3000',
     },
      foo: 'bar'
  }
*/

forgetConfig('foo')

Example for save:

import { forgetConfig } from '@pamstarter/epts-engine';

/*
  {
     app: {
        port: 3000,
        url: 'http://localhost:3000',
     },
      foo: 'bar'
  }
*/

forgetConfig('foo').save()

Router

wrapperRouteHandler

Wrapper route handler to use when you want to wrap multiple route handlers

  • rest @param:
    • name: handlers
    • type: IRouteHandlerArgs
  • @return: IRouteHandler

IRouteHandlerArgs type:

type IRouteHandlerArgs = (IControllerArgs | IControllerArgs[])[]

IRouteHandler type:

type IRouteHandler = IControllerArgs | IControllerArgs[]