Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 23 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

Lumie is a lightweight module that allows you to set up a scalable controllers architecture for your nodejs project.

✅ Maintainable<br>
✅ Scalable<br>
✅ Quick setup<br>
✅ Easily testable<br>
✅ Maintainable`<br>`
✅ Scalable`<br>`
✅ Quick setup`<br>`
✅ Easily testable`<br>`

## 💾 INSTALLATION

Expand All @@ -19,12 +19,12 @@ npm install lumie

## 🔩 HOW IT WORKS

**Lumie** goes through the files and folders inside your controllers directory to find what we call "routing definitions".<br>
Each controllers are defined in files, which export their routing definitions [( example )](https://github.com/Alex-Levacher/Lumie/tree/master/example)<br><br>
**Lumie** goes through the files and folders inside your controllers directory to find what we call "routing definitions".`<br>`
Each controllers are defined in files, which export their routing definitions [( example )](https://github.com/Alex-Levacher/Lumie/tree/master/example)`<br><br>`
By default, we use the name of the file that exports the routing definition to name the route

`/` > `controllers` > `cars.js` will create the endpoints `/cars/*`<br>
`/` > `controllers` > `admin` > `rules.js` will create the endpoints `/admin/rules/*`<br>
`/` > `controllers` > `cars.js` will create the endpoints `/cars/*<br>`
`/` > `controllers` > `admin` > `rules.js` will create the endpoints `/admin/rules/*<br>`
`/` > `controllers` > `users` > `users.routing.js` will create the endpoints `/users/*`

## ⚙️ CONFIGURATION
Expand All @@ -51,14 +51,14 @@ const server = app.listen(3000, "127.0.0.1", () => {

### Options

| Name | type | default value | Description |
| -------------------- | ---------- | ------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| **verbose** | `boolean` | `false` | Will print or not the routes name in the console |
| **preURL** | `string` | `/` | Prefix your route URLs |
| **ignore** | `string[]` | `null` | Lumie will not try to find a routing definition in those files. |
| **controllers_path** | `string` | none, this option is required | The path of your controllers folder. |
| **routing_files** | `string` | `*.routing` | How you want to name routing files. |
| **permissions** | `function` | `null` | A function that takes in parameter a **level access** and returns an [**express middleware**](https://expressjs.com/en/guide/using-middleware.html). This is useful if you want to restrict access for some URLs. With this option enabled, you will be able to set in each route configuration an option level that will be passed to your permission function [( example )](https://github.com/Alex-Levacher/Lumie/blob/master/example/permissions.js) |
| Name | type | default value | Description |
| -------------------------- | ------------ | ----------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **verbose** | `boolean` | `false` | Will print or not the routes name in the console |
| **preURL** | `string` | `/` | Prefix your route URLs |
| **ignore** | `string[]` | `null` | Lumie will not try to find a routing definition in those files. |
| **controllers_path** | `string` | none, this option is required | The path of your controllers folder. |
| **routing_files** | `string` | `*.routing` | How you want to name routing files. |
| **permissions** | `function` | `null` | A function that takes in parameter a**level access** and returns an [**express middleware**](https://expressjs.com/en/guide/using-middleware.html). This is useful if you want to restrict access for some URLs. With this option enabled, you will be able to set in each route configuration an option level that will be passed to your permission function [( example )](https://github.com/Alex-Levacher/Lumie/blob/master/example/permissions.js) |

## 🌲FILE STRUCTURE

Expand Down Expand Up @@ -97,7 +97,8 @@ module.exports = {
post: {
middlewares: postCars.middlewares,
action: postCars.action,
level: 'public'
level: 'public',
comment: "This ia a comment"
},
get: {
action: getCars.getAll,
Expand All @@ -119,6 +120,7 @@ module.exports = {
action: < function(req, res) >,
level: < parameters of you permission function >, // Optional
middlewares: < Array(function(req, res, next)) >// Optional
comment: < string < // Optional
}
}
```
Expand All @@ -128,7 +130,7 @@ module.exports = {
There is **2** common way to create a controller with Lumie, you can take a look [here](https://github.com/Alex-Levacher/Lumie/blob/master/example/controllers) to learn how to implement them.

* **Minimal** ([sample](https://github.com/Alex-Levacher/Lumie/blob/master/example/controllers/simple-ctrl.js)): You only create one file which takes as name, the name of the controller you want to create. Then you define the routing definition and the functions. This method is recommended if you plan to have a small controller with few actions.
* **Structured** ([sample](https://github.com/Alex-Levacher/Lumie/tree/master/example/controllers/users)): You create a new directory with the name of the controller. Inside, you create:<br>
* **Structured** ([sample](https://github.com/Alex-Levacher/Lumie/tree/master/example/controllers/users)): You create a new directory with the name of the controller. Inside, you create:`<br>`
* `[your-controller-name].routing.js` which contains the routing definition
* `[your-controller-name].actions.js` which contains the action functions of the controller.
* `[your-controller-name].spec.js` This one is optional
Expand All @@ -149,12 +151,13 @@ Here are the next features planned, let me know if you have some ideas
* Create a CLI to generate new controllers / projects

## ☕️ SUPPORT

You can support the project by

* Star our GitHub repo ⭐️
* [Suggest ideas](https://github.com/Alex-Levacher/Lumie/issues) to help me promote Lumie 🌎

If you are struggling to setup Lumie, you found a bug or if you have some improvement ideas, feel free to [create an issue](https://github.com/Alex-Levacher/Lumie/issues)<br><br>

If you are struggling to setup Lumie, you found a bug or if you have some improvement ideas, feel free to [create an issue](https://github.com/Alex-Levacher/Lumie/issues)`<br><br>`

## ⚖️ LICENSE

Expand Down
3 changes: 2 additions & 1 deletion example/controllers/users/users.routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ module.exports = {
'/:id': {
get: {
action: getOne,
level: 'public'
level: 'public',
comment: "This is a comment"
}
}
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
},
"license": "MIT",
"dependencies": {
"express": "^4.17.2",
"micromatch": "^3.1.10"
},
"devDependencies": {
Expand All @@ -56,4 +57,4 @@
"test:src",
"test:example"
]
}
}
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ function browseControllerObject(ctrlDefinition, path) {
level: action[verbName].level,
path: cpath.join(path, actionName),
permissions: _options.permissions,
middlewares: action[verbName].middlewares
middlewares: action[verbName].middlewares,
comment: action[verbName].comment
});
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Route {
this.path = options.path;
this.permissions = options.permissions;
this.middlewares = options.middlewares || [];
this.comment = options.comment || '';
}

create(app) {
Expand All @@ -24,7 +25,7 @@ class Route {
}

desc() {
return `\t${this.level}\t${this.verb}\t[${joinPathSlash(this.path)}]`;
return `\t${this.level}\t${this.verb}\t[${joinPathSlash(this.path)}]\t${this.comment}`;
}
}

Expand Down
5 changes: 3 additions & 2 deletions test/route.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ test('desc should work', (t) => {
level: 'public',
path: '/test/',
permissions: level => level,
middlewares: undefined
middlewares: undefined,
comment: 'This is a comment'
};
const route = new Route(prepareRoute);
const desc = route.desc();
t.is(desc, '\tpublic\tget\t[/test/]');
t.is(desc, '\tpublic\tget\t[/test/]\tThis is a comment');
});