UNPKG

@tiscode/node-base

Version:

TISCODE Base Module for NodeJS Applications

103 lines (73 loc) 2.59 kB
# TISCODE Node Base <details> <summary>Version Releases</summary> ### Version: v0.2.1 * Hotfix **cannot set headers after they are sent** ### Version: v0.2.0 * ExpressAuthenticatedHandler with jsonwebtoken * ExpressPublicHandler * Handlers must implement **handle** method ### Version: v0.1.0 * HttpServer app with peer support for graphql * Http Event Types * Express adapter (for Handler constructor and Command Factory constructor) * Express default handler and abstract handler * Command and Command Factory abstracts * **iif** helper function * **ifnull** helper function </details> ### Installation `npm i @tiscode/node-base` or `yarn add @tiscode/node-base` ## Usage <details> <summary>How to use <b>ExpressAuthenticatedHandler</b>?</summary> <details style="padding-left: 20px"> <summary>With <b>node-base</b> for <b><i>direct</i></b> routes ( post / put / get / delete / patch / all )</summary> ```js import { App } from '@tiscode/node-base'; import MyCommandFactory from './factories/commands/my-command'; const expressAuthenticatedHandler = App.Http.Express.adapter(App.Http.Express.authenticatedHandler, MyCommandFactory); const app = new App.Http.Server(); app.on('started', () => { app.attachRouter({ path: '/my-endpoint', method: 'post', // you can change this to 'put', 'get', 'delete', 'patch' or 'all' router: expressAuthenticatedHandler }); }); app.listen(3000); ``` </details> <details style="padding-left: 20px"> <summary>With <b>node-base</b> for <b><i>use</i></b> routers</summary> ```js import { Router } from 'express'; import { App } from '@tiscode/node-base'; import MyCommandFactory from './factories/commands/my-command'; const expressAuthenticatedHandler = App.Http.Express.adapter(App.Http.Express.authenticatedHandler, MyCommandFactory); const app = new App.Http.Server(); const router = Router(); router.post('/my-endpoint', expressAuthenticatedHandler); app.on('started', () => { app.attachRouter({ path: '/api', // default is '/' method: 'use', router: expressAuthenticatedHandler }); }); app.listen(3000); ``` </details> <details style="padding-left: 20px"> <summary>With <b>express</b></summary> ```js import express from 'express'; import { App } from '@tiscode/node-base'; import MyCommandFactory from './factories/commands/my-command'; const expressAuthenticatedHandler = App.Http.Express.adapter(App.Http.Express.authenticatedHandler, MyCommandFactory); const app = express(); app.post('/my-endpoint', expressAuthenticatedHandler); app.listen(3000); ``` </details> </details>