thinware

0.2.0 • Public • Published

thinware

A thin middleware wrapper for connecting server-agnostic application logic to req/res/next plumbing.

thinware(
  fn,  // or path to module that returns a function
  args // or function that produces args from req
)

Installation

# npm 
npm i --save thinware
 
# yarn 
yarn add thinware

Example

Let's say we have some server-agnostic application logic in lib/hello.js that we want to expose via an API endpoint:

module.exports = name => `Hello, ${name}!`

Here's what your endpoint might look like:

app.get('say-hello', async (req, res) => {
  const hello = require('./lib/hello')
 
  try {
    const result = await hello(req.query.name)
    res.send(result)
  }
  catch (err) {
    res.status(500).send(err.message)
  }
})

And here's the equivalent with thinware:

app.get('say-hello', thinware('./lib/hello', req => req.query.name))

thinware.next()

By default, next is not used. To invoke next instead of res.send, use thinware.next:

app.get('/important-data',
  // Invokes next() with result instead of res.send()
  thinware.next(verifyToken, req => req.headers['x-token']),
 
  // Invokes res.send() with result
  thinware(getData, req => req.query)
)

errdrop support

When thinware returns errors via res, it checks for a .status property on the error before falling back to 500 Internal Server Error.

This can be done manually, or with a module like errdrop:

const Error = require('errdrop')
 
module.exports = name => {
  if (!name) {
    throw new Error.BadRequest('name is required')
  }
 
  return `Hello, ${name}!`
}

Package Sidebar

Install

npm i thinware

Weekly Downloads

2

Version

0.2.0

License

MIT

Unpacked Size

5.03 kB

Total Files

4

Last publish

Collaborators

  • alexwebb2