polkadot-middleware

A middleware pattern for polkadot

Usage no npm install needed!

<script type="module">
  import polkadotMiddleware from 'https://cdn.skypack.dev/polkadot-middleware';
</script>

README

npm i polkadot-middleware

So you're using the awesome polkadot server, but you're missing that sweet middleware action – it's a pain to have to compose all those functions yourself, isn't it?

polkadot(
    handleErrors(
        setCacheControl(
            (req, res) => 'Sup dawg'
        )
    )
).listen(8080)

function handleErrors(next) {
    return async(req, res) => {
        try {
            return await next(req, res)
        } catch (err) {
            res.statusCode = 500

            return err.message || err
        }
    }
}

function setCacheControl(next) {
    return async(req, res) => {
        res.setHeader(`Cache-Control`, `public, max-age=` + 3600)
        return next(req, res)
    }
}

Well, until TC39 figures out the pipeline operator, you can use this library to give yourself that sweet linear middleware experience.

const polkadot = require('polkadot')
const middleware = require('polkadot-middleware')

middleware(
    polkadot,
    handleErrors,
    setCacheControl,
    (req, res) => 'Sup dawg'
).listen(8080)

License

WTFPL