|
| 1 | +[](https://travis-ci.org/adesege/express-api-versioning) |
| 2 | +[](https://coveralls.io/github/adesege/express-api-versioning) |
| 3 | +[](https://codeclimate.com/github/adesege/express-api-versioning) |
| 4 | +[](https://codeclimate.com/github/adesege/express-api-versioning/coverage) |
| 5 | +[](https://codeclimate.com/github/adesege/express-api-versioning) |
| 6 | + |
| 7 | +# Express API Versioning |
| 8 | + |
| 9 | +Express API versioning is an express middleware that dynamically loads different API versions seamlessly depending on the version number specified in the request url. |
| 10 | + |
| 11 | +It is written in Javascript ES6 syntax and it is further transpiled down to Javascript ES5 using babel. |
| 12 | + |
| 13 | +# Installation |
| 14 | + |
| 15 | +- `npm install express-api-versioning --save` |
| 16 | +- `express` is required as a peer dependency. |
| 17 | + |
| 18 | +# Usage |
| 19 | + |
| 20 | +### Using ES6 |
| 21 | + |
| 22 | +` import expressApiVersioning from 'express-api-versioning';` |
| 23 | +### Using ES5 |
| 24 | + |
| 25 | +` const expressApiVersioning = require('express-api-versioning');` |
| 26 | +### Basic Usage |
| 27 | + |
| 28 | + ```js |
| 29 | +app.use(expressApiVersioning({ |
| 30 | + instance: app, // passes an instance of express to the entry point |
| 31 | + apiPath: path.join(__dirname, './api'), // absolute path to the api directory |
| 32 | + test: /\/api\/(v[0-9]+)[a-z0-9/_+-]*/, // regular expression to get the version number from the url |
| 33 | + entryPoint: 'app.js' // entry point exports a function which takes an instance of express as parameter. |
| 34 | +})); |
| 35 | +``` |
| 36 | +### Advanced Usage |
| 37 | + |
| 38 | +```js |
| 39 | +app.use(expressApiVersioning({ |
| 40 | + instance: app, // passes an instance of express to the entry point |
| 41 | + apiPath: path.join(__dirname, './api'), // absolute path to the api directory |
| 42 | + test: /\/endpoint\/(v[0-9]+)[a-z0-9/_+-]*/, // regular expression to get the version number from the url, |
| 43 | + entryPoint: 'index.js' // entry point exports a function which takes an instance of express as parameter. |
| 44 | +})); |
| 45 | +``` |
| 46 | +- Check the [src/example](/src/sample) directory to understand the folder structure. |
| 47 | + |
| 48 | +# Error Handling |
| 49 | + |
| 50 | +The middleware will throw an exception when; |
| 51 | +1. the `apiPath` is not specified |
| 52 | +1. an express `instance` is not specified and not a function |
| 53 | +1. the api version cannot be found in the `api directory`. |
| 54 | + |
| 55 | +# Issue Reporting |
| 56 | + |
| 57 | +Please use the [issues](/issues) page to report an issue. |
| 58 | + |
| 59 | +# API References |
| 60 | + |
| 61 | +`Express API Versioning` is highly customizable and does not introduce complexities to your application folder structure. It takes a configuration object as parameter. |
| 62 | + |
| 63 | +| Configuration item | Default | Description | |
| 64 | +| ------ | ------ | ------- | |
| 65 | +| test | /\/api\/(v[0-9]+)[a-z0-9/_+-]*/ | Regular expression to get the version number from the request url. It gets the version number from the first group of the regex match. |
| 66 | +| entryPoint | app.js | Entry point for each api version. It exports a function which takes an instance of express as parameter. |
| 67 | +| apiPath | empty string | Absolute path to each api version container/directory. This is usually `path.join(__dirname, './api')`. |
| 68 | +| instance | null | An instance of express app which will be passed down to the entry point for usage. |
| 69 | + |
| 70 | +# Todo |
| 71 | + |
| 72 | +- Create a boilerplate for `Express API Versioning` with `Sequelize` as ORM |
| 73 | + |
| 74 | +# Author |
| 75 | + |
| 76 | +**Temitayo Fadojutimi** is a Software Developer at Andela and he dedicates his expertise to solving practical problems in the society. He tweets at [@adesege_](http://twitter.com/adesege_) |
| 77 | + |
| 78 | +# Testing |
| 79 | + |
| 80 | +If you will like to test this package, |
| 81 | +- Clone this repo or `npm install express-api-versioning` |
| 82 | +- cd into the package directory and run `npm install` |
| 83 | +- Then run `npm test` to run the test. |
| 84 | + |
| 85 | +# Contributing |
| 86 | + |
| 87 | +Thank you for your interest in contributing to this package. I currently accept contributions from everyone but I expect that standards are maintained. |
| 88 | +To contribute, |
| 89 | +1. Fork the project |
| 90 | +1. Create a feature branch, branch away from `master` |
| 91 | +1. Write tests, using `Mocha and Chai` or any other testing frameworks, and code |
| 92 | +1. If you have multiple commits please combine them into a few logically organized commits by [squashing them](git-squash) |
| 93 | +1. Push the commit(s) to your fork |
| 94 | +1. Submit a merge request (MR) to the `master` branch |
| 95 | +1. The MR title should describe the change you want to make |
| 96 | +1. The MR description should give a motive for your change and the method you used to achieve it. |
| 97 | + 1. Mention the issue(s) your merge request solves, using the `Solves #XXX` or |
| 98 | + `Closes #XXX` syntax to auto-close the issue(s) once the merge request will |
| 99 | + be merged. |
| 100 | +1. Be prepared to answer questions and incorporate feedback even if requests for this arrive weeks or months after your MR submission |
| 101 | + 1. If a discussion has been addressed, select the "Resolve discussion" button beneath it to mark it resolved. |
| 102 | +1. When writing commit messages please follow |
| 103 | + [these guidelines](http://chris.beams.io/posts/git-commit). |
0 commit comments