# express-restful-api

> ## Installation

Latest version **14.1.4** (published 2018-11-30) · MIT license · 0 weekly downloads

## Install

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

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 14.1.4 |
| Published | 2018-11-30 |
| First published | 2015-12-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 16 |
| Unpacked size | 1.2 MB |
| Known vulnerabilities | 0 (+8 in 2 direct dependencies) |
| Install scripts | no |
| Author | sideroad |
| Maintainers | sideroad |
| Keywords | express, RESTful, API |

## Links

- npm: https://www.npmjs.com/package/express-restful-api
- npm.io page: https://npm.io/package/express-restful-api

## Dependencies (16)

- [async](https://npm.io/package/async.md) ^0.9.0
- [redis](https://npm.io/package/redis.md) ^0.12.1
- [apidoc](https://npm.io/package/apidoc.md) ^0.17.7
- [lodash](https://npm.io/package/lodash.md) ^4.17.5
- [moment](https://npm.io/package/moment.md) ^2.10.6
- [express](https://npm.io/package/express.md) ^4.13.4
- [unroute](https://npm.io/package/unroute.md) 0.0.2
- [camelize](https://npm.io/package/camelize.md) ^1.0.0
- [fs-extra](https://npm.io/package/fs-extra.md) ^0.26.2
- [mongoose](https://npm.io/package/mongoose.md) ^4.3.0
- [passport](https://npm.io/package/passport.md) ^0.3.2
- [pluralize](https://npm.io/package/pluralize.md) ^1.2.1
- [express-session](https://npm.io/package/express-session.md) ^1.13.0
- [method-override](https://npm.io/package/method-override.md) ^2.3.5
- [passport-github2](https://npm.io/package/passport-github2.md) ^0.1.10
- [passport-facebook](https://npm.io/package/passport-facebook.md) ^2.1.0

## Recent versions

- 14.1.4 (latest) — 2018-11-30
- 14.1.3 — 2018-11-24
- 14.1.2 — 2017-12-12
- 14.1.1 — 2017-10-09
- 14.1.0 — 2017-10-09
- 14.0.0 — 2017-10-09
- 13.3.1 — 2017-10-09
- 13.3.0 — 2017-10-08
- 13.2.0 — 2017-10-08
- 13.1.2 — 2017-10-08
- 13.1.1 — 2017-10-08
- 13.1.0 — 2017-10-08
- 13.0.4 — 2017-10-08
- 13.0.3 — 2017-10-07
- 13.0.2 — 2017-10-07
- … 83 more at https://npm.io/package/express-restful-api/versions

## README

# Creating Simple Express RESTful API

## Installation

```sh
npm i --save express-restful-api
```

## Usage

```js
var express = require('express'),
    app = express(),
    bodyParser = require('body-parser'),
    creator = require('express-restful-api');

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

// register router
app.use('/', creator.router({
  mongo: process.env.MONGO_URL,
  schemas: {

    // register company model
    company: {
      name: {
        uniq: true,
        required: true,
        pattern: /^[a-zA-Z _]+$/,
        desc: "Company name",
        invalid: "Only alphabets number spaces allowed"
      },
      president: {
        type: 'instance',
        relation: 'person'
      },
      members: {
        type: 'children',
        relation: 'person'
      }
    },

    // register person model
    person: {
      name: {
        uniq: true,
        required: true,
        pattern: /^[a-zA-Z _]+$/
      },
      company: {
        type: 'parent',
        relation: 'company.members'
      },
      age: {
        type: 'number'
      }
    }
  }
}));

// create API document, if needed
creator.doc({
  "name": "RESTful API",
  "version": JSON.parse( fs.readFileSync('./package.json') ).version,
  "description": "API specification",
  "title": "API doc",
  "url" : "//express-restful-api-sample.herokuapp.com",
  "sampleUrl": "//express-restful-api-sample.herokuapp.com",
  "template": {
    "withCompare": false,
    "withGenerator": true,
    "jQueryAjaxSetup": {
      xhrFields: {
        withCredentials: true
      }
    }
  },
  "dest": __dirname + '/public/doc'
});
```

### creator.router
We can specified parameters below

|Name     |Type          |Default  | Description                                                                                                         |
|:--------|:-------------|:--------|:--------------------------------------------------------------------------------------------------------------------|
|uniq     |Boolean       |false    |This data will use to create ID. If multiple keys have `uniq` of true, ID will be `${key1}-${key2}`                  |
|required |Boolean       |false    |Enable or Disable to validate POST data. If the value is empty, 400 status code will be response                     |
|type     |String        |'string' |POST data will be store following the type. 'string', 'number', 'date', 'children', 'parent', 'relation' can be used.                                  |
|pattern   |String, RegExp|undefined|Enable or Disable to validate POST data. If the value does not match with `pattern`, 400 status code will be response |
|relation |String        |undefined|instance: The key will be have relationship with specified key. the value could be have only single value. <br><br> children: The key will be have relationship with specified key. the value could be have multiple values. <br><br> parent: If the model have relationship as children, The key should have as `parent` of `${parent model}.${key}`.|
|desc     |String        |undefined|API document use the value for description                                                                           |
|invalid  |String        |undefined|When the data is invalid, return message                                                                             |

creator.router creates CRUD below
- Get instance ( GET )
- Get collection ( GET )
- Get child collection of a instance ( GET )
- Validate parameters ( GET )
- Create instance ( POST )
- Update instance ( POST )
- Delete instance ( DELETE )
- Delete collection ( DELETE )

### Field control
You can specify response field by `fields` parameter.
Each fields should be separated with comma.

```
// Only name, age response fields should be response
...fields=name,age
```

You can expand response field by `expands` parameter which has relation of `parent` or `instance` attribution type.

##### Example
Original resource fields of person
```
{
  "name": "sideroad",
  "company": {
    "id": "8ab3de2",
    "href": "/apis/companies/8ab3de2"
  }
}
```

Fetche with `expands=company` parameter
```
{
  "name": "sideroad",
  "company": {
    "id": "8ab3de2",
    "name": "FooBar",
    "establishedAt": "2017-10-01T00:00:00+09:00",
    "createdAt": "2017-10-01T00:00:00+09:00",
    "updatedAt": "2017-10-01T00:00:00+09:00"
  }
}
```

### Fetching collection
#### Sorting
Your can specify sort order of collection.
- `+` or not specify operand sorted by parameter ascending.
- `-` operand sorted by parameter descending.
You can specify multiple prioritized order separated with comma.
Notice: operand should be encoded with URL parameter such as `%2B` in case of `+`.

```
// Get collection order by name asc, age desc.
...orderBy=name,-age
```
#### Filtering
##### type of `string`
You can use wildcard to get collection.

```
// Get collection which has name end with road
...name=*road
```

You can use comma to get collection as OR condition
```
// Get collection which value equal sideroad OR roadside
...name=sideroad,roadside
```

##### type of `number` or `date`
You can get collection filtered by range.

```
// Get collection which created the instance between 1, Dec and 5, Dec
...createdAt=[2015-12-01,2015-12-05]
```

```
// Get collection which has value between 10 and 20
...age=[10,20]
```

You can use comma to get collection as OR condition
```
// Get collection which value equal sideroad OR roadside
...age=10,20
```

### creator.doc
API document will be generated. `dest` should be specified destination of the document.
See [apidoc](https://github.com/apidoc/apidoc) to check other parameter

## Example
[express-restful-api-sample](https://github.com/sideroad/express-restful-api-sample)

## Influences
API strongly influenced great architecture [Beautiful REST + JSON APIs](http://www.slideshare.net/stormpath/rest-jsonapis)

## Change log
### 14.0.0
Change Validate method from 'GET' to 'POST'

### 13.0.0
Change property name from `schema` to `schemas`

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