0.0.8 • Published 3 years ago
endie v0.0.8
Overview
Express middleware has no robust support for Typescript. Express middleware relies on either built-in ever-present properties like req.cookies (i.e. cookie-parser), or on modifying the global type declarations of Express.js (i.e. express-fileupload).
endie provides Express middleware behavior via a plugin system that is fully type-enforced, leaving nothing to guess-work, without any of these type workarounds.
Usage Overview
This section shows a simple example of the usage of endie.
import { createEndie, createPlugin } from 'endie'
import express from 'express'
// Plugin to parse the cookies of a request
const cookiePlugin = createPlugin()
  .setPre({
    exec: ({ req }) => ({ cookies: myCookieParser(req) }),
  })
  .build()
// Plugin to get the user making a request
const identityPlugin = createPlugin()
  .setPre({
    exec: ({ req }) => ({ user: myAuthService.identify(req) }),
  })
  .build()
// Plugin to log any errors after the handling of a request
const errorLoggingPlugin = createPlugin()
  .setPost({
    exec: ({ req, error }) => ({ user: myLoggingService.log(req, error) }),
  })
  .build()
const endie = createEndie()
  .addPlugin(cookiePlugin)
  .addPlugin(identityPlugin)
  .addPlugin(errorLoggingPlugin)
  .addPlugin(p => p.setPost({
    exec: ({ ... }) => ...,
  }))
const fooEndpoint = endie.create({
  logRequest: true,
  handler: async o => {
    console.log(o.m.cookies) // { cookies: ..., user: ...}
  }
})
const app = express().get('/foo', fooEndpoint)