# express-api-helper

> Simple API helper module for Express apps.

Latest version **0.0.5** (published 2015-07-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install express-api-helper
pnpm add express-api-helper
yarn add express-api-helper
bun add express-api-helper
```

## 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.0.5 |
| Published | 2015-07-17 |
| First published | 2013-03-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 0.8.0 |
| Dependencies | 1 |
| Known vulnerabilities | 0 (+2 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 19 |
| Author | Ganesh 'GP' Prasannah |
| Maintainers | bryandragon, gprasannah |
| Keywords | express, connect, api |

## Links

- npm: https://www.npmjs.com/package/express-api-helper
- Repository: https://github.com/paambaati/express-api-helper
- Homepage: https://github.com/paambaati/express-api-helper#readme
- Issues: https://github.com/paambaati/express-api-helper/issues
- npm.io page: https://npm.io/package/express-api-helper

## Dependencies (1)

- [express](https://npm.io/package/express.md) ~4.13.1

## Recent versions

- 0.0.5 (latest) — 2015-07-17
- 0.0.4 — 2015-01-14
- 0.0.3 — 2014-03-09
- 0.0.2 — 2014-03-06
- 0.0.1 — 2013-03-10

## README

# express-api-helper

Simple API helper module for [Express](http://expressjs.com) apps.

[![Build Status](https://secure.travis-ci.org/paambaati/express-api-helper.png)](http://travis-ci.org/paambaati/express-api-helper)

## API

### `ok(req, res, data)`

Respond with `200 OK` and JSON-encoded data.

* `req` express Request
* `res` express Response
* `data` Object

### `badRequest(req, res, errors)`

Respond with `400 Bad Request` and JSON-encoded error object, `{message:String,errors:Array}`.

* `req` express Request
* `res` express Response
* `data` Array (of String) or String

### `unauthorized(req, res)`

Respond with `401 Unauthorized` and JSON-encoded error object, `{message:String}`.

* `req` express Request
* `res` express Response

### `forbidden(req, res)`

Respond with `403 Forbidden` and JSON-encoded error object, `{message:String}`.

* `req` express Request
* `res` express Response

### `notFound(req, res)`

Respond with `404 Not Found` and JSON-encoded error object, `{message:String}`.

* `req` express Request
* `res` express Response

### `unsupportedAction(req, res)`

Respond with `405 Method Not Allowed` and JSON-encoded error object, `{message:String}`.

* `req` express Request
* `res` express Response

### `invalid(req, res, errors)`

Respond with `422 Unprocessable Entity` and JSON-encoded error object, `{message:String,errors:Array}`.

* `req` express Request
* `res` express Response
* `errors` Array (of String) or String

### `serverError(req, res, error)`

Respond with `500 Internal Server Error` and JSON-encoded error object, `{message:String,error:Object}`.

* `req` express Request
* `res` express Response
* `error` Object

### `requireParams(req, res, params, callback)`

Require that listed parameters are present. Checks for presence of each parameter in `req.body` object if using `express.bodyParser` middleware; otherwise checks for presence of each parameter in `req.params` or `req.query`. If any parameters are missing, invokes `badRequest` with an array of error messages with the form `"Missing required parameter: %s"`.

* `req` express Request
* `res` express Response
* `params` Array (of String) or String
* `callback(err)` Function

### `requireHeaders(req, res, headers, callback)`

Require that listed headers are present. Checks for presence of each header in `req.headers`. If any parameters are missing, invokes `badRequest` with an array of error messages with the form `"Missing required header parameter: %s"`.

* `req` express Request
* `res` express Response
* `headers` Array (of String) of String
* `callback(err)` Function 

## Example

Sample usage:

```javascript
var http = require('http'),
    express = require('express'),
    bodyParser = require('body-parser'),
    api = require('express-api-helper'),
    app = express(),
    Post = require('./models/post');

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: true
}));

app.all('/api/*', function (req, res, next) {
  if (!req.user) return api.unauthorized(req, res);
  next();
});

app.post('/api/posts', function (req, res) {
  api.requireParams(req, res, ['title', 'content', 'authorId'], function (err) {
    if (err) return api.serverError(req, res, err);
    var payload = {
      title: req.body.title,
      content: req.body.content,
      authorId: req.body.authorId
    };
    Post.create(payload, function (err, post) {
      if (err) return api.serverError(req, res, err);
      api.ok(req, res, post.toJSON());
    });
  });
});

app.get('/api/posts', function (req, res) {
  Post.find({}, function (err, posts) {
    if (err) return api.serverError(req, res, err);
    api.ok(req, res, posts.toJSON());
  });
});

app.get('/api/posts/:id', function (req, res) {
  Post.findById(req.params.id, function (err, post) {
    if (err) return api.serverError(req, res, err);
    if (!post) return api.notFound(req, res);
    api.ok(req, res, post.toJSON());
  })
});

http.createServer(app).listen(3000, function () {
  console.log("Express API listening on 3000");
});
```

## Running Tests

To run the tests, clone the repository and install the dev dependencies:

```bash
git clone git://github.com/paambaati/express-api-helper.git
cd express-api-helper && npm install
make test
```

## License

[MIT](LICENSE)

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