# thinware

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

Latest version **0.2.0** (published 2019-10-30) · MIT license · 0 weekly downloads

## Install

```sh
npm install thinware
pnpm add thinware
yarn add thinware
bun add thinware
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2019-10-30 |
| First published | 2019-10-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 4.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Alex Webb |
| Maintainers | alexwebb2 |
| Keywords | thin, middleware, wrapper, express |

## Links

- npm: https://www.npmjs.com/package/thinware
- Repository: https://github.com/aiwebb/thinware
- Homepage: https://github.com/aiwebb/thinware#readme
- Issues: https://github.com/aiwebb/thinware/issues
- npm.io page: https://npm.io/package/thinware

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 0.2.0 (latest) — 2019-10-30
- 0.1.0 — 2019-10-30
- 0.0.4 — 2019-10-20
- 0.0.3 — 2019-10-19
- 0.0.2 — 2019-10-19
- 0.0.1 — 2019-10-19

## README

# thinware

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

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

## Installation
```bash
# 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:

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

Here's what your endpoint might look like:

```js
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`:

```js
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`:

```js
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](https://github.com/aiwebb/errdrop):

```js
const Error = require('errdrop')

module.exports = name => {
  if (!name) {
    throw new Error.BadRequest('name is required')
  }

  return `Hello, ${name}!`
}
```

---
_Source: https://npm.io/package/thinware · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
