13.2.4 • Published 1 year ago

arrest v13.2.4

Weekly downloads
180
License
MIT
Repository
github
Last release
1 year ago

arrest

Swagger REST framework for Node.js, with support for MongoDB and JSON-Schema

travis build Coverage Status npm version

Arrest lets you write RESTful web services in minutes. It automatically generates a Swagger description of the API and support input validation using JSON-Schemas.

Highlight features:

  • Compatible with Express 4.x
  • Implements simple CRUD semantics on MongoDB
  • Supports querying object with the RQL syntax
  • Input validation with JSON-Schema
  • Oauth2 scope checks per operation

Note for 1.3 users: arrest 3.x is a complete rewrite of the old module and it's not backwards compatible.

How to Install

npm install arrest

Super Simple Example

The following sample application shows how to create a simple REST API, using a MongoDB collection as the data store. In the sample, the path /tests is linked to a tests collection on a MongoDB instance running on localhost:

const arrest = require('arrest');
const api = new arrest.API();

api.addResource(new arrest.MongoResource('mongodb://localhost:27017', { name: 'Test' }));

api.listen(3000);

The Swagger specification of the API you just created is available at http://localhost:3000/swagger.json

Now you can query your data collection like this:

curl "http://localhost:3000/tests"

You can add a new item:

curl "http://localhost:3000/tests" -H "Content-Type: application/json" -X POST -d '{ "name": "Jimbo", "surname": "Johnson" }'

You can query a specific item by appeding the identifier of the record (the _id attribute):

curl "http://localhost:3000/tests/51acc04f196573941f000002"

You can update an item:

curl "http://localhost:3000/tests/51acc04f196573941f000002" -H "Content-Type: application/json" -X PUT -d '{ "name": "Jimbo", "surname": "Smith" }'

And finally you can delete an item:

curl "http://localhost:3000/tests/51acc04f196573941f000002" -X DELETE

Creating an API

An API is a collection of Resources, each supporting one or more Operations.

In arrest you create an API by creating an instance of the base API class or of a derived class. You then add instances of the Resource class or a derived one. Each resource contains its supported Routes, that is a collection of instances of classes derived from the abstract Operation, which represents an operation to be executed when an HTTP method is called on a path.

The following code demonstrates this three level structure:

const arrest = require('arrest');
const api = new arrest.API();

const operation1 = function(req, res, next) {
  res.json({ data: 'this is operation 1' });
}
const operation2 = function(req, res, next) {
  res.json({ data: 'this is operation 2' });
}
const operation3 = function(req, res, next) {
  res.json({ data: 'this is operation 3' });
}
const resource1 = new arrest.Resource({
  name: 'SomeResource',
  routes: {
    '/': {
      get: operation1,
      post: operation2
    },
    '/some-path': {
      put: operation3
    }
  }
})

api.addResource(resource1);
api.listen(3000);

The API above supports the following operations:

  • GET on http://localhost/some-resources
  • POST on http://localhost/some-resources
  • PUT on http://localhost/some-resources/some-path

Please note how the some-resources path was automatically constructed using the name of the resource SomeResource, making it plural and converting the camelcase in a dash-separated name. This default behaviour can be changed specifying the namePlural and path when creating the resource (e.g. new Resource({ name: 'OneResource', namePlural: 'ManyResources', path: 'my_resources' }))

Another other way to produce the same result is:

const arrest = require('arrest');
const api = new arrest.API();

const resource1 = new arrest.Resource({ name: 'SomeResource' });

resource1.addOperation('/', 'get', function(req, res, next) {
  res.json({ data: 'this is operation 1' });
});
resource1.addOperation('/', 'post', function(req, res, next) {
  res.json({ data: 'this is operation 2' });
});
resource1.addOperation('/some-path', 'put', function(req, res, next) {
  res.json({ data: 'this is operation 3' });
});

api.addResource(resource1);
api.listen(3000);

In real world applications, where resources and operation are in fact more complex, you will want to create class that extend the basic classes in arrest, like in the next example:

const arrest = require('arrest');

class MyOperation extends arrest.Operation {
  constructor(resource, path, method) {
    super('op1', resource, path, method);
  }
  handler(req, res, next) {
    res.json({ data: 'this is a custom operation' });
  }
}

class MyResource extends arrest.Resource {
  constructor() {
    super();
    this.addOperation(new MyOperation(this, '/', 'get'));
  }
}

class MyAPI extends arrest.API {
  constructor() {
    super({
      info: {
        title: 'This is a custom API',
        version: '0.9.5'
      }
    });
    this.addResource(new MyResource());
  }
}

const api = new MyAPI();
api.listen(3000);

The API above supports GETs on http://localhost/my-resources (note how the path was in this case constructed automatically from the name of the class MyResource).

By the default, arrest APIs add a special route /swagger.json that returns the Swagger description of the API: the Swagger object is populated with the properties of the API object, Resources are converted into Swagger Tags and Operations are mapped to Swagger Operations.

Data validation

arrest supports JSON-Schema for data validation. Validation rules are set using the Swagger specification. For instance, the following code show how to validate the body of a POST and the query paramters of a GET:

class MyOperation1 extends arrest.Operation {
  constructor(resource, path, method) {
    super('op1', resource, path, method);
    this.setInfo({
      parameters: [
        {
          name: 'body',
          in: 'body',
          required: true,
          schema: {
            type: 'object',
            required: [ 'name' ],
            additionalProperties: false,
            properties: {
              name: {
                type: 'string'
              },
              surname: {
                type: 'string'
              }
            }
          }
        }
      ]
    });
  }
  handler(req, res, next) {
    res.json({ data: 'this is a op1' });
  }
}

class MyOperation2 extends arrest.Operation {
  constructor(resource, path, method) {
    super('op2', resource, path, method);
    this.setInfo({
      parameters: [
        {
          name: 'lang',
          in: 'query',
          type: 'string',
          required: true
        },
        {
          name: 'count',
          in: 'query',
          type: 'integer'
        }
      ]
    });
  }
  handler(req, res, next) {
    res.json({ data: 'this is a op2' });
  }
}

class MyResource extends arrest.Resource {
  constructor() {
    super();
    this.addOperation(new MyOperation1(this, '/', 'post'));
    this.addOperation(new MyOperation2(this, '/', 'get'));
  }
}

Omitting the body or passing an invalid body (e.g. an object without the name property) when POSTing to http://localhost/my-resources will return an error. Likewise GETting without a lang parameter or with a count set to anything other than a number will fail.

Scopes and security validators

TBA

Creating an API with a MongoDB data store

TBA (default api routes)

Using arrest with express

TBA

Debugging

TBA

API documentation

TBA

13.2.4

1 year ago

13.2.3

1 year ago

13.2.2

2 years ago

13.2.1

2 years ago

13.1.0

2 years ago

13.2.0

2 years ago

10.3.0

2 years ago

13.0.2

2 years ago

13.0.0

2 years ago

13.0.1

2 years ago

12.1.1

2 years ago

12.0.0

2 years ago

12.0.1

2 years ago

12.1.0

2 years ago

10.2.1-es2017.1

2 years ago

10.2.0

2 years ago

10.2.1

2 years ago

10.2.2

2 years ago

11.0.0

2 years ago

10.0.0

3 years ago

10.1.4

2 years ago

10.1.5

2 years ago

10.1.0

3 years ago

10.1.1

2 years ago

10.1.2

2 years ago

10.1.3

2 years ago

9.9.3

3 years ago

9.9.2

3 years ago

9.9.0

3 years ago

9.9.1

3 years ago

9.8.2

3 years ago

9.8.1

3 years ago

9.8.0

3 years ago

9.8.3

3 years ago

9.7.0

3 years ago

9.6.1

3 years ago

9.6.0

3 years ago

9.5.4

3 years ago

9.4.0

3 years ago

9.3.3

4 years ago

9.3.2

4 years ago

9.3.1

4 years ago

9.3.0

4 years ago

9.2.1

4 years ago

9.2.0

4 years ago

9.1.2

4 years ago

9.1.1

4 years ago

9.1.0

4 years ago

9.0.1

4 years ago

9.0.0

4 years ago

8.1.4

4 years ago

8.1.5

4 years ago

8.1.3

4 years ago

8.1.2

4 years ago

8.1.1

4 years ago

8.1.0

4 years ago

8.0.0

5 years ago

8.0.0-rc.31

5 years ago

8.0.0-rc.30

5 years ago

8.0.0-rc.29

5 years ago

8.0.0-rc.28

5 years ago

8.0.0-rc.27

5 years ago

7.5.2

5 years ago

8.0.0-rc.26

5 years ago

7.5.1

5 years ago

8.0.0-rc.25

5 years ago

8.0.0-rc.24

5 years ago

8.0.0-rc.23

5 years ago

8.0.0-rc.22

5 years ago

8.0.0-rc.21

5 years ago

8.0.0-rc.20

5 years ago

8.0.0-rc.19

5 years ago

8.0.0-rc.18

5 years ago

8.0.0-rc.17

5 years ago

8.0.0-rc.16

5 years ago

8.0.0-rc.15

5 years ago

8.0.0-rc.14

5 years ago

8.0.0-rc13

5 years ago

8.0.0-rc.13

5 years ago

8.0.0-rc12

5 years ago

8.0.0-rc11

5 years ago

8.0.0-rc10

5 years ago

8.0.0-rc9

5 years ago

8.0.0-rc8

5 years ago

8.0.0-rc7

5 years ago

8.0.0-rc6

5 years ago

8.0.0-rc5

5 years ago

8.0.0-rc4

5 years ago

8.0.0-rc3

5 years ago

8.0.0-rc2

5 years ago

8.0.0-rc1

5 years ago

7.5.0

6 years ago

7.4.0

6 years ago

7.3.0

6 years ago

7.2.0

6 years ago

7.1.0

6 years ago

7.0.2

6 years ago

7.0.1

6 years ago

7.0.0

7 years ago

6.0.3

7 years ago

6.0.2

7 years ago

6.0.1

7 years ago

6.0.0

7 years ago

5.6.0

7 years ago

5.5.1

7 years ago

5.5.0

7 years ago

5.4.8

7 years ago

5.4.7

7 years ago

5.4.6

7 years ago

5.4.5

7 years ago

5.4.4

7 years ago

5.4.3

7 years ago

5.4.2

7 years ago

5.4.1

7 years ago

5.4.0

7 years ago

5.3.0

7 years ago

5.2.2

7 years ago

5.2.1

7 years ago

5.2.0

7 years ago

5.1.0

7 years ago

5.0.0

7 years ago

4.0.2

7 years ago

4.0.1

7 years ago

4.0.0

7 years ago

3.2.3

7 years ago

3.2.2

7 years ago

3.2.1

7 years ago

3.2.0

7 years ago

3.1.1

7 years ago

3.1.0

7 years ago

3.0.0

7 years ago

2.3.0-beta.40

7 years ago

2.3.0-beta.39

7 years ago

2.3.0-beta.38

7 years ago

2.3.0-beta.37

7 years ago

2.3.0-beta.36

7 years ago

2.3.0-beta.35

7 years ago

2.3.0-beta.34

7 years ago

2.3.0-beta.33

7 years ago

2.3.0-beta.32

7 years ago

2.3.0-beta.31

7 years ago

2.3.0-beta.30

8 years ago

2.3.0-beta.29

8 years ago

2.3.0-beta.28

8 years ago

2.3.0-beta.27

8 years ago

2.3.0-beta.26

8 years ago

2.3.0-beta.25

8 years ago

2.3.0-beta.24

8 years ago

2.3.0-beta.23

8 years ago

2.3.0-beta.22

8 years ago

2.3.0-beta.21

8 years ago

2.3.0-beta.20

8 years ago

2.3.0-beta.19

8 years ago

2.3.0-beta.18

8 years ago

2.3.0-beta.17

8 years ago

2.3.0-beta.16

8 years ago

2.3.0-beta.15

8 years ago

2.3.0-beta.14

8 years ago

2.3.0-beta.13

8 years ago

2.3.0-beta.12

8 years ago

2.3.0-beta.11

8 years ago

2.3.0-beta.10

8 years ago

2.3.0-beta.9

8 years ago

2.3.0-beta.8

8 years ago

2.3.0-beta.7

8 years ago

2.3.0-beta.6

8 years ago

2.3.0-beta.5

8 years ago

2.3.0-beta.2

8 years ago

2.3.0-beta.1

8 years ago

2.2.3

8 years ago

2.2.2

8 years ago

2.2.1

8 years ago

2.2.0

8 years ago

2.1.0-beta.10

8 years ago

2.1.0-beta.9

8 years ago

2.1.0-beta.8

8 years ago

2.1.0-beta.7

8 years ago

2.1.0-beta.6

8 years ago

2.1.0-beta.5

8 years ago

2.1.0-beta.4

8 years ago

2.1.0-beta.3

8 years ago

2.1.0-beta.2

8 years ago

2.1.0-beta.1

8 years ago

2.0.0-beta.1

8 years ago

2.0.7

8 years ago

2.0.6

8 years ago

2.0.5

8 years ago

2.0.4

8 years ago

2.0.3

8 years ago

2.0.2

8 years ago

2.0.1

8 years ago

2.0.0

8 years ago

1.1.4

10 years ago

1.1.3

10 years ago

1.1.2

10 years ago

1.1.1

10 years ago

1.1.0

10 years ago

1.0.11

10 years ago

1.0.10

10 years ago

1.0.9

10 years ago

1.0.8

10 years ago

1.0.7

11 years ago

1.0.6

11 years ago

1.0.5

11 years ago

1.0.4

11 years ago

1.0.3

11 years ago

1.0.2

11 years ago

1.0.1

11 years ago

1.0.0

11 years ago