-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 6ff5a7e
Showing
12 changed files
with
3,892 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
version: 2 | ||
|
||
defaults: &defaults | ||
working_directory: ~/repo | ||
docker: | ||
- image: circleci/node:10 | ||
|
||
jobs: | ||
install: | ||
<<: *defaults | ||
steps: | ||
- checkout | ||
- restore_cache: | ||
keys: | ||
- node-modules | ||
- run: npm install | ||
- save_cache: | ||
paths: | ||
- node_modules | ||
key: node-modules | ||
- persist_to_workspace: | ||
root: ~/repo | ||
paths: . | ||
test: | ||
<<: *defaults | ||
steps: | ||
- attach_workspace: | ||
at: ~/repo | ||
- run: | ||
name: Setup environment variables | ||
command: | | ||
echo 'export COVERALLS_SERVICE_JOB_ID="$CIRCLE_BUILD_NUM"' >> $BASH_ENV | ||
- run: | ||
name: Run lint | ||
command: npm run test:lint | ||
- run: | ||
name: Run tests & coverage | ||
command: npm run test:coveralls | ||
publish: | ||
<<: *defaults | ||
steps: | ||
- attach_workspace: | ||
at: ~/repo | ||
- run: | ||
name: Authenticate on npm registry | ||
command: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc | ||
- run: | ||
name: Publish on npm registry | ||
command: npm publish --access=public | ||
|
||
workflows: | ||
version: 2 | ||
main-flow: | ||
jobs: | ||
- install: | ||
filters: | ||
tags: | ||
only: | ||
- /.*/ | ||
- test: | ||
requires: | ||
- install | ||
filters: | ||
tags: | ||
only: | ||
- /.*/ | ||
- publish: | ||
context: Npm | ||
requires: | ||
- test | ||
filters: | ||
branches: | ||
ignore: | ||
- /.*/ | ||
tags: | ||
only: /v[0-9]+(\.[0-9]+)*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.DS_Store | ||
.nyc_output | ||
coverage | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
* | ||
!src/**/* | ||
!README.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2019 WebInMove | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
# Barabara | ||
|
||
[![CircleCI](https://circleci.com/gh/webinmove/barabara.svg?style=svg)](https://circleci.com/gh/webinmove/barabara) | ||
[![Coverage Status](https://coveralls.io/repos/github/webinmove/barabara/badge.svg?branch=master)](https://coveralls.io/github/webinmove/barabara?branch=master) | ||
[![npm version](https://img.shields.io/npm/v/barabara.svg)](https://www.npmjs.com/package/barabara) | ||
[![Dependency Status](https://img.shields.io/david/webinmove/barabara.svg?style=flat-square)](https://david-dm.org/webinmove/barabara) | ||
|
||
Automatic router for simple REST json api | ||
|
||
Barabara will automatically create one or more Express router for you reading the content of your controller folder(s). | ||
|
||
## Installation | ||
|
||
```sh | ||
$ npm install barabara | ||
``` | ||
|
||
## Usage | ||
|
||
### Import in your project | ||
```js | ||
// require Barabara | ||
const { Barabara } = require('barabara'); | ||
// import Barabara | ||
import { Barabara } from 'barabara'; | ||
``` | ||
|
||
### Create an instance | ||
|
||
```js | ||
const express = require('express'); | ||
|
||
const barabara = new Barabara( | ||
// Router class | ||
express.Router, | ||
// Mapping between controller actions & http verbs | ||
{ | ||
exists: 'head', | ||
read: 'get', | ||
create: 'post', | ||
update: 'put', | ||
partial: 'patch', | ||
destroy: 'delete' | ||
} | ||
); | ||
``` | ||
|
||
With this mapping Barabara will look up for corresponding controller actions and create the following routes: | ||
|
||
- **HEAD** `/my-controller` will call myController.exists(params, meta) | ||
- **HEAD** `/my-controller/:id` will call myController.exists(params, meta) | ||
- **GET** `/my-controller` will call myController.read(params, meta) | ||
- **GET** `/my-controller/:id` will call myController.read(params, meta) | ||
- **POST** `/my-controller` will call myController.create(params, meta) | ||
- **PUT** `/my-controller/:id` will call myController.update(params, meta) | ||
- **PATCH** `/my-controller/:id` will call myController.partial(params, meta) | ||
- **DELETE** `/my-controller/:id` will call myController.destroy(params, meta) | ||
|
||
### Create routers | ||
|
||
```js | ||
// Here we add req.user in meta parameter of controller actions methods | ||
const authRouter = barabara.createRouter(path.join(__dirname, 'controllers/auth'), [ 'user' ]); | ||
const v1ApiRouter = barabara.createRouter(path.join(__dirname, 'controllers/v1')); | ||
const v2ApiRouter = barabara.createRouter(path.join(__dirname, 'controllers/v2')); | ||
|
||
const app = express(); | ||
|
||
app.use('/auth', authRouter); | ||
app.use('/v1', authMiddleware, v1ApiRouter); | ||
app.use('/v2', authMiddleware, v2ApiRouter); | ||
``` | ||
|
||
### Expectation | ||
|
||
Barabara expect controller action's methods to have the following signature: | ||
|
||
`actionName(params: Object, meta: Object)` | ||
|
||
Exemple of controller implementation : | ||
|
||
```js | ||
module.exports = { | ||
exists: async (params, meta) => { /* ... */ }, | ||
read: async (params, meta) => { /* ... */ }, | ||
create: async (params, meta) => { /* ... */ }, | ||
update: async (params, meta) => { /* ... */ }, | ||
partial: async (params, meta) => { /* ... */ }, | ||
destroy: async (params, meta) => { /* ... */ }, | ||
// This won't be mapped to a route | ||
internal: () => { /* ... */ } | ||
}; | ||
``` | ||
*Note: not all the methods need to be implemented* | ||
|
||
`req.query`, `req.body`, `req.params` will be merged in `params` (in this order). | ||
While `meta` will contains other `req` properties if specified in `createRouter`. | ||
`meta` will contain `req.user` here: | ||
|
||
```js | ||
const authRouter = barabara.createRouter( | ||
path.join(__dirname, 'controllers/auth'), [ 'user' ] | ||
); | ||
``` | ||
|
||
## Npm scripts | ||
|
||
### Running code formating | ||
|
||
```sh | ||
$ npm run format | ||
``` | ||
|
||
### Running tests | ||
|
||
```sh | ||
$ npm test | ||
``` | ||
|
||
### Running lint tests | ||
|
||
```sh | ||
$ npm test:lint | ||
``` | ||
|
||
### Running coverage tests | ||
|
||
```sh | ||
$ npm test:cover | ||
``` | ||
|
||
This will create a coverage folder with all the report in `coverage/index.html` | ||
|
||
### Running all tests | ||
|
||
```sh | ||
$ npm test:all | ||
``` | ||
|
||
*Note: that's the one you want to use most of the time* | ||
|
||
## Reporting bugs and contributing | ||
|
||
If you want to report a bug or request a feature, please open an issue. | ||
If want to help us improve barabara, fork and make a pull request. | ||
Please use commit format as described [here](https://github.com/angular/angular.js/blob/master/DEVELOPERS.md#-git-commit-guidelines). | ||
And don't forget to run `npm run format` before pushing commit. | ||
|
||
## Repository | ||
|
||
- [https://github.com/webinmove/barabara](https://github.com/webinmove/barabara) | ||
|
||
## License | ||
|
||
The MIT License (MIT) | ||
|
||
Copyright (c) 2019 WebInMove | ||
|
||
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. |
Oops, something went wrong.