Home Reference Source Repository

Beerio API

Build Status Dependency Status devDependency Status document Known Vulnerabilities

This project uses the following tools:

 - ECMAScript6      # Written in ES6 (ES2015)
 - ExpressJS        # Web development framework
 - Passport         # Authentication middleware
 - MongoDB          # Document store database
 - Gulp             # Build system
 - BabelJS          # JavaScript compiler
 - Travis-CI        # Continuous Integration
 - Apiary           # RESTfull API documentation
 - Dredd            # RESTfull testing
 - ESDoc            # Inline documentation
 - ESLint           # JavaScript linter

Installation

  1. Install MongoDB including.
  2. Install NodeJS.
  3. Clone the repository with: git clone https://github.com/SCThijsse/beerio-api.git.
  4. Install gulp globally with [sudo] npm install -g gulp.
  5. Install dependencies of Beerio API with cd beerio-api and npm install.
  6. Build the ES5 code with gulp build.

Manual

Checkout the manual to find out more about this project. Or checkout the documentation for the API routes here.

License

MIT License

Copyright (c) 2017 - Sjoerd Thijsse

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

License

License

MIT License

Copyright (c) 2017 - Sjoerd Thijsse

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Installation

Installation

  1. Install MongoDB including.
  2. Install NodeJS.
  3. Clone the repository with: git clone https://github.com/SCThijsse/beerio-api.git.
  4. Install gulp globally with [sudo] npm install -g gulp.
  5. Install dependencies of Beerio API with cd beerio-api and npm install.
  6. Build the ES5 code with gulp build.

Usage

Usage

In this usage section you can read about Beerio API and its functionality through the CLI.

npm

The following commands are defined in the package.json:

 $ npm run build                        # Build the project (same as $ gulp build).
 $ npm run dev                          # Run the development version of the API.
 $ npm run dist                         # Run the build version of the API.
 $ npm run docs                         # Generate documenation of the API.
 $ npm run forever                      # Run the API with the 'forever' module.
 $ npm run lint                         # Lint code with eslint.
 $ npm run test                         # Run tests with dredd.

Gulp

The following commands are defined by Gulp:

 $ gulp build                           # Transpile the ES6 source code to ES5.
 $ gulp clean                           # Delete the 'build' directory.
 $ gulp default                         # The default gulp task (same as 'gulp build').
 $ gulp watch                           # Watch for any changes in the 'src' directory.

Example

Example

In this example section you can read about the routes that are provided by Beerio API.

Index

GET - http://localhost:5000/

Gives some basic information about the server on which the API is running on.

Example output:

{
  "repo": "https://github.com/SCThijsse/beerio-api.git",
  "server": "serv01",
  "totalBeers": 564,
  "uptime": 84956,
  "version": "1.0.0",
  "commit": "2dce73e"
}

Users

POST - http://localhost:5000/register

Register a new user.

Example body:

{
  "firstname": "First Name",
  "lastname": "Last Name",
  "email": "[email protected]",
  "username": "Username",
  "password": "Password"
}

Example output:

{
  "user": {
    "_id": "Username",
    "firstname": "First Name",
    "lastname": "Last Name",
    "email": "[email protected]",
    "username": "Username",
    "password": "$2a$10$PSP.dm61vtvarDg/HBjC9.XzGryjHrxtocGNnU9CjHWdtMWmz.gZe",
    "beers": []
  },
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiJVc2VybmFtZSIsInVzZXJuYW1lIjoiVXNlcm5hbWUiLCJleHAiOjE0OTIwMDcyOTMsImlhdCI6MTQ4NjgyNjg5M30.dMzBRl--fpIIuRt5Yeu1OmdXsxOzJ55NpVl0jxckZ_g"
}

POST - http://localhost:5000/login*

Login with a user.

Example body:

{
  "username": "Username",
  "password": "Password"
}

Example output:

{
  "user": {
    "_id": "Username",
    "firstname": "First Name",
    "lastname": "Last Name",
    "email": "[email protected]",
    "username": "Username",
    "password": "$2a$10$PSP.dm61vtvarDg/HBjC9.XzGryjHrxtocGNnU9CjHWdtMWmz.gZe",
    "beers": []
  },
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiJVc2VybmFtZSIsInVzZXJuYW1lIjoiVXNlcm5hbWUiLCJleHAiOjE0OTIwMDcyOTMsImlhdCI6MTQ4NjgyNjg5M30.dMzBRl--fpIIuRt5Yeu1OmdXsxOzJ55NpVl0jxckZ_g"
}

GET - http://localhost:5000/users/{username}

Get a single user based on the unique username

Example header:

Authorization: Bearer <token>

Example output:

{
  "_id": "Username",
  "firstname": "First Name",
  "lastname": "Last Name",
  "email": "[email protected]",
  "username": "Username",
  "password": "Encrypted Password",
  "beers": [
    "Beer Name"
  ]
}

Beers

POST - http://localhost:5000/beers

Insert a new beer into the database.

Example header:

Authorization: Bearer <token>

Example body:

{
  "name": "Beer Name",
  "style": "Beer Style",
  "type": "Beer Type",
  "abv": 1,
  "ebu": 1,
  "thumbnail": "https://somethumbnail.com/images.png"
}

Example output:

{
  "user": "Username",
  "name": "Beer Name",
  "style": "Beer Style",
  "type": "Beer Type",
  "abv": 1,
  "ebu": 1,
  "thumbnail": "https://somethumbnail.com/images.png",
  "_id": "589de779ff7fb61d2d6817d3",
  "votes": 0
}

GET - http://localhost:5000/beers?page=1&search=beer%20name&sort=name&order=-1

Get multiple beers based on search queries.

Query Strings

 - page           # Page number, shows 25 per page.
 - search         # Search on beer name
 - sort           # Order by 'name', 'style', 'type', 'abv', 'ebu'
 - order          # -1 for ascending, 1 for descending

Example output:

[{
    "_id": "589de779ff7fb61d2d6817d3",
    "name": "Beer Name",
    "style": "Beer Style",
    "type": "Beer Type",
    "thumbnail": ""
    "abv": 1,
    "ebu": 1,
    "thumbnail": "https://somethumbnail.com/image.png"
}]

GET - http://localhost:5000/beers/{beer}

Get a single beer based on the id.

Example output:

{
  "_id": "589de779ff7fb61d2d6817d3",
  "user": "Username",
  "name": "Beer Name",
  "style": "Beer Style",
  "type": "Beer Type",
  "abv": 1,
  "ebu": 1,
  "thumbnail": "https://somethumbnail.com/image.png",
  "votes": 0
}

PUT - http://localhost:5000/beers/{beer}

Update a beer based on the id.

Example headers:

Authorization: Bearer <token>

Example body:

{
  "name": "Beer Name2",
  "style": "Beer Style",
  "type": "Beer Type",
  "abv": 1,
  "ebu": 1,
  "thumbnail": "https://somethumbnail.com/image.png"
}

Example output:

{
  "_id": "589de779ff7fb61d2d6817d3",
  "user": "Username",
  "name": "Beer Name",
  "style": "Beer Style",
  "type": "Beer Type",
  "abv": 1,
  "ebu": 1,
  "thumbnail": "https://somethumbnail.com/image.png",
  "__v": 0,
  "votes": 0
}

DELETE - http://localhost:5000/beers/{beer}

Delete a beer based on the id.

Example headers:

Authorization: Bearer <token>

Example output:

{
  "_id": "589de779ff7fb61d2d6817d3",
  "user": "Username",
  "name": "Beer Name",
  "style": "Beer Style",
  "type": "Beer Type",
  "abv": 1,
  "ebu": 1,
  "thumbnail": "https://somethumbnail.com/image.png",
  "__v": 0,
  "votes": 0
}

POST - http://localhost:5000/beers/{beer}/vote

Increase the vote value for a beer, if you have already voted for a beer, the vote will decrease.

Example headers:

Authorization: Bearer <token>

Example output:

{
  "_id": "589de779ff7fb61d2d6817d3",
  "user": "Username",
  "name": "Beer Name",
  "style": "Beer Style",
  "type": "Beer Type",
  "abv": 1,
  "ebu": 1,
  "thumbnail": "https://somethumbnail.com/image.png",
  "__v": 0,
  "votes": 0
}

References

References

Class Summary

Static Public Class Summary
public

Class for CRUD operations for beers.

public

Class for starting the API.

public

Class for displaying information about the server the API is running on.

public

Class for CRUD operations for users.

Function Summary

Static Public Function Summary
public

createLogger(pretty: Boolean, verbose: Boolean, winston: winston): void

Function to create a global logger object based on the properties of the logger function.

public

createTemp(): void

Create the temporary directory.

public

doSetup(app: Express, pretty: Boolean, verbose: Boolean): void

Setup the Express service.

public

Execute a command from within the root folder.

public

resetLog(): void

Reset the default log file.

public

setupRoutes(app: ExpressJS): void

Setup ExpressJS routing.

Variable Summary

Static Public Variable Summary
public

The promise object to override the mongoose promise object.

public

The host of the server of the database.

public

The name of the database.

public

Check if this instance of the API is the master.

public

pageSize: Integer

The amount of object show per page.

public

port: Integer

The port on which the API will run on.

public

The name of the server.

public

The path to the temprary directory.

public

workers: Integer

The amount of workers on the cluster.

External Summary

Static Public External Summary
public

Create an Express Winston instance.

public

Winston: Wintson

Create a Winston instance.