# exp-rest

> REST API for Express and Bookshelf.js

Latest version **0.1.1** (published 2016-11-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install exp-rest
pnpm add exp-rest
yarn add exp-rest
bun add exp-rest
```

## 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.1.1 |
| Published | 2016-11-13 |
| First published | 2016-11-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | DK |
| Maintainers | mrxh8 |
| Keywords | rest, restful, api, node, express, bookshelf |

## Links

- npm: https://www.npmjs.com/package/exp-rest
- Repository: https://github.com/kvokov/exprest
- Homepage: https://github.com/kvokov/exprest#readme
- Issues: https://github.com/kvokov/exprest/issues
- npm.io page: https://npm.io/package/exp-rest

## Dependencies (2)

- [express](https://npm.io/package/express.md) ^4.14.0
- [body-parser](https://npm.io/package/body-parser.md) ^1.15.2

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 0.1.1 (latest) — 2016-11-13
- 0.1.0 — 2016-11-13

## README

# ExpREST

[![npm](https://img.shields.io/npm/v/exp-rest.svg?style=flat)](https://www.npmjs.com/package/exp-rest)  [![Libraries.io for GitHub](https://img.shields.io/librariesio/github/kvokov/exprest.svg?style=flat)]() [![license](https://img.shields.io/github/license/mashape/apistatus.svg)](LICENSE)

ExpREST helps you build RESTful API with [Express](http://expressjs.com/) and [Bookshelf.js](http://bookshelfjs.org/). Easily create a flexible REST interface for your models, and extend these with your application specific logic.

Try on example - [exprest-example](https://github.com/kvokov/exprest-example)

## Installation
```bash
$ npm install exp-rest --save
```

## Quick Start

Lets say you have a Bookshelf model called `User`

```javascript
let User = bookshelf.Model.extend({
    tableName: 'users'
});
```

To create CRUD endpoints for your models you just need to pass resources array to ExpREST

```javascript
let app = require('express')();
let exprest = require('exp-rest');

app.use(exprest({resources: [User]}));

app.listen(3000);
```

And you gets these endpoints

| Method | URL          | Action                  |
| :----- | :------------| :-----------------------|
| GET    | `/users`     | Get list of users       |
| GET    | `/users/:id` | Get a user by id        |
| POST   | `/users`     | Create a new user       |
| PUT    | `/users/:id` | Update an existing user |
| DELETE | `/users/:id` | Delete an existing user |

**That's it!** :sunglasses:

## Documentation

- [Configuration](#configuration)
- [Endpoints](#endpoints)
- [Hooks](#hooks)
- [Examples](#examples)

### Configuration

### Endpoints

#### Get list of entities

> GET `/entities`

Listing resources support filtering, searching, sorting, and pagination.

#### Get an entity by identifier

> GET `/entities/:identifier`

#### Create a new entity

> POST `/entities`

#### Update an existing entity

> PUT `/entities/:identifier`

#### Delete an existing entity

> DELETE `/entities/:identifier`

### Hooks

Hooks let you extend and override default endpoint behaviors.

`before` hooks are executed before the default database action, such as fetch, save, or delete. 

`after` hooks are executed after all database actions are complete.

Hook names are generated based on endpoint configurations. This list is based on a `/users` endpoint where `itemName = user` and `collectionName = users`

| Hook Name                 | Request                   | Arguments                             |
| :-------------------------| :------------------------ | :------------------------------------ |
| `beforeGetUsers`          | GET `/users`              | [req, res, userModel]                 |
| `afterGetUsers`           | GET `/users`              | [req, res, userCollection]            |
| `beforeGetUser`           | GET `/users/:id`          | [req, res, userModel]                 |
| `afterGetUser`            | GET `/users/:id`          | [req, res, userModel]                 |
| `beforeCreateUser`        | POST `/users`             | [req, res, userModel]                 |
| `afterCreateUser`         | POST `/users`             | [req, res, userModel]                 |
| `beforeUpdateUser`        | PUT `/users/:id`          | [req, res, userModel]                 |
| `afterUpdateUser`         | PUT `/users/:id`          | [req, res, userModel]                 |
| `beforeDeleteUser`        | DELETE `/users/:id`       | [req, res, userModel]                 |
| `afterDeleteUser`         | DELETE `/users/:id`       | [req, res, userModel]                 |
| `beforeGetRelatedThings`  | GET `/users/:id/things`   | [req, res, thingModel]                |
| `afterGetRelatedThings`   | GET `/users/:id/things`   | [req, res, thingsCollection]          |
| `beforeRelatedThing`      | POST `/users/:id/things`  | [req, res, userModel]                 |
| `afterRelateThing`        | POST `/users/:id/things`  | [req, res, userModel, thingModel]     |

`req` and `res` are an Express [request](http://expressjs.com/4x/api.html#request) and [response](http://expressjs.com/4x/api.html#response)

`userModel` is an instance of a [bookshelf model](http://bookshelfjs.org/#Model)

`userCollection` is an instance of a [bookshelf collection](http://bookshelfjs.org/#Collection)

#### Adding hooks

#### How to use hooks

## License

[MIT](LICENSE)

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