npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

expro

v0.1.0

Published

Build middleware for expres more expressive.

Downloads

5

Readme

expro

Build Status

Build express middleware more expressive.

Install with:

npm install expro

And make a test:

npm test

APIs

expro(fn1, fn2, fn3 ...)

To create middleware chain for express.

let mw = expro(
  (req, res, next) => next(),
  (req, res, next) => next(),
  ...
  (req, res, next) => next(),
);

let app = express();
app.use(mw);

expro.async(fn)

To create promise for async process.

expro
  .async(done => fs.readFile('./config', done))
  .then(content => {
    // do with file content ...
  });

expro.await(fn)

To create expro await middleware.

expro.await(req => async(req));

expro.with(selector1, selector2, ...)

To create expro args selectors context.

expro
  .with(x => x.params.id, x => x.body)
  .await((id, body) => async(id, body));

Or use property selectors:

expro
  .with('params.id', 'body')
  .await((id, body) => async(id, body));

expro.header(field [, value])

To create middleware to write/overwrite response header.

expro.header('Content-Type', 'text/plain');

expro.header({
  'Content-Type': 'text/plain',
  'Content-Length': '123'
});

expro.status(number)

To create middleware to write/overwrite response status code.

expro(
  expro.await(req => async(req)),
  expro.status(201),
  expro.formatResult()
);

expro.mapResult(mapper)

To create middleware to map the result into another form.

expro(
  expro.await(req => Promise.resolve({ foo: 'bar' })),
  expro.mapResult(s => {
    return { code: 'OK', data: s };
  }),
  expro.formatResult()
);

// -> { result: { code: 'OK', data: { foo: 'bar' } } }

expro.wrap(key)

To create middleware to wrap result in the given key.

expro(
  expro.await(req => Promise.resolve({ foo: 'bar' })),
  expro.wrap('data'),
  expro.formatResult()
);

// -> { result: { data: { foo: 'bar' } } }

expro.formatResult([formatter])

To create middleware to send formatted result. Default to send result in json.

expro(
  expro.await(req => Promise.resolve({ foo: 'bar' })),
  expro.formatResult()
);

// -> { result: { foo: 'bar' } }

Can be use as application-level middleware:

let app = express();

app.get('/', expro.await(req => Promise.resolve({ foo: 'bar' })));

app.use(expro.formatResult());

And with customized formatter:

let app = express();

app.get('/', expro.await(req => Promise.resolve({ foo: 'bar' })));

app.use(expro.formatResult({
  json: (res, result) => {
    res.json({ code: res.statusCode, data: result });
  },
  xml: (res, result) => {
    let json = {
      code: res.statusCode,
      data: result
    };
    let xml = xmlParser.toXml(json);
    res.send(xml);
  },
  default: (res, result) => {
    res.status(406).send('Not Acceptable');
  }
}));

expro.formatError([formatter])

Create middleware to send formatted error result. Default to send error in json.

expro(
  expro.await(req => Promise.reject(Error('oops'))),
  expro.formatError()
);

// -> { error: { message: 'oops' } }

Can be use as application-level middleware:

let app = express();

app.get('/', expro.await(req => Promise.reject(Error('oops'))));

app.use(expro.formatError());

And with customized formatter:

let app = express();

app.get('/', expro.await(req => Promise.reject(Error('oops'))));

app.use(expro.formatError({
  json: (err, result) => {
    res.status(err.statusCode).send({
      code: err.statusCode,
      error_message: err.message
    });
  },
  xml: (res, result) => {
    let json = {
      code: err.statusCode,
      error_message: err.message
    };
    let xml = xmlParser.toXml(json);
    res.status(err.statusCode).send(xml);
  },
  default: (res, result) => {
    res.status(406).send('Not Acceptable');
  }
}));

License

MIT