Skip to content

Commit

Permalink
init(Barabara): Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
OlivierCuyp authored and satblip committed Aug 8, 2019
0 parents commit 6ff5a7e
Show file tree
Hide file tree
Showing 12 changed files with 3,892 additions and 0 deletions.
76 changes: 76 additions & 0 deletions .circleci/config.yml
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]+)*/
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.DS_Store
.nyc_output
coverage
node_modules
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*
!src/**/*
!README.md
21 changes: 21 additions & 0 deletions LICENSE
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.
175 changes: 175 additions & 0 deletions README.md
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.
Loading

0 comments on commit 6ff5a7e

Please sign in to comment.