https://github.com/aliencreations/alien-node-api-utils
Helper functions for API handling on NodeJS
https://github.com/aliencreations/alien-node-api-utils
Last synced: 7 months ago
JSON representation
Helper functions for API handling on NodeJS
- Host: GitHub
- URL: https://github.com/aliencreations/alien-node-api-utils
- Owner: AlienCreations
- License: mit
- Created: 2015-07-22T23:54:01.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2023-12-31T15:32:10.000Z (about 2 years ago)
- Last Synced: 2025-05-17T14:42:05.436Z (8 months ago)
- Language: JavaScript
- Size: 112 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# alien-node-api-utils
Helper functions for API handling on NodeJS. The functions are pure and curried with Ramda.
[](https://travis-ci.org/AlienCreations/alien-node-api-utils) [](https://coveralls.io/github/AlienCreations/alien-node-api-utils?branch=master) [](https://npmjs.org/package/alien-node-api-utils) [](https://david-dm.org/AlienCreations/alien-node-api-utils)
## Install
```
$ npm install alien-node-api-utils --save
```
Run the specs
```
$ npm test
```
## Usage
```js
// Example API route such as '/users' which could reasonably leverage a
// 'user' model which would return a promise or catch with an error object.
// The error object passed in the catch should include a 'statusCode' property
// that is specific to the respective error. If it does not, the api utils
// will default to 500.
// Note: Because the util functions are curried, we can keep them pure and by
// invoking with req and res, as shown below.
var apiUtils = require('alien-node-api-utils'),
userModel = require('./models/user');
function getUsers(req, res, next) {
userModel.getUsers().
then(apiUtils.jsonResponseSuccess(req, res)).
catch(apiUtils.jsonResponseError(req, res, next));
};
module.exports = getUsers;
```