Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade dependencies #25

Closed
wants to merge 103 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
103 commits
Select commit Hold shift + click to select a range
dc3b71a
Update README.md
sratsimba May 24, 2018
4386057
Merge branch 'dev-2.0.0'
sratsimba May 25, 2018
c5ca809
Updates README
sratsimba May 25, 2018
805a4b8
Updates README
sratsimba May 25, 2018
9d2e22b
Merge branch 'master' of github.com:sitrakary/hapi-auto-route
sratsimba May 25, 2018
fec038b
Fix readme
sratsimba May 25, 2018
d7094a1
2.0.0
sratsimba May 25, 2018
9e5acb8
Fix package files
sratsimba May 26, 2018
af21f95
2.0.1
sratsimba May 26, 2018
5c42aff
Reset
sratsimba May 29, 2018
f6f76d4
Don't ignore yarn.lock
sratsimba May 29, 2018
6f336f3
Remove unused .labrc.js
sratsimba May 29, 2018
ad0bbee
Update dependencies.
sratsimba May 29, 2018
a775221
Fix spelling.
sratsimba May 29, 2018
5a4bf2a
Configure tests.
sratsimba May 29, 2018
79c59c2
Plugin exists.
sratsimba May 29, 2018
6ab485e
Create plugin interface.
sratsimba May 29, 2018
a118270
Create basic plugin.
sratsimba May 29, 2018
b4d8dae
Validate params.
sratsimba May 29, 2018
65bfb5e
Refactor
sratsimba May 29, 2018
829e721
Apply default value to plugin options.
sratsimba May 29, 2018
5125123
Reorganize project folders.
sratsimba May 29, 2018
76fe352
Refactor
sratsimba May 29, 2018
265bcbe
Add config property.
sratsimba May 30, 2018
7e8422c
Add property routes.
sratsimba May 30, 2018
d9fe02b
returns routes file path and set AutoRoute routes property.
sratsimba May 30, 2018
c0d2af5
Load route objects. And fix loadFiles bug.
sratsimba May 30, 2018
f5872e8
Add method to load prefix.
sratsimba May 30, 2018
dbe4e12
Split prefix by os specific directory separator.
sratsimba May 30, 2018
43d5268
Refactor
sratsimba May 30, 2018
bcdf800
Replace '.' prefix with empty string
sratsimba May 30, 2018
f3fa7ae
Add getPrefix function
sratsimba May 30, 2018
40f425f
Big refactoring
sratsimba May 30, 2018
7a8bc01
Fix dependencies versions
sratsimba May 30, 2018
2bb5c83
Prefix route paths
sratsimba May 30, 2018
980da96
apply prefix on an array of route objects
sratsimba May 30, 2018
71f9f7d
Change AutoRoute constructor interface.
sratsimba May 30, 2018
40524c1
Rebase
sratsimba May 31, 2018
1add722
Done.
sratsimba Jun 1, 2018
77b921f
Fix package.json file
sratsimba Jun 1, 2018
3ea1b5b
Remove unnecessary file
sratsimba Jun 1, 2018
9e00795
Fix issue #5
sratsimba Jun 1, 2018
0df3e4a
Keep changelog and update read me.
sratsimba Jun 1, 2018
fe24bfd
Merge pull request #6 from sitrakary/dev
sratsimba Jun 1, 2018
877927f
2.0.2
sratsimba Jun 1, 2018
f7ea349
Fixes #10
sratsimba Aug 27, 2018
9ae61ce
Fixes linting
sratsimba Aug 27, 2018
833f4cd
ignore logs
sratsimba Aug 27, 2018
7e6a0df
Release v2.0.3
sratsimba Aug 27, 2018
95ace90
Update node version to be compatible with version required by eslint
sratsimba Sep 19, 2018
942012c
Update README.md
sratsimba Sep 26, 2018
76dd08d
Upgrade joi
sratsimba Oct 17, 2018
681a180
v2.0.4
sratsimba Oct 17, 2018
5b13bcc
Merge branch 'master' of github.com:sitrakary/hapi-auto-route
sratsimba Oct 17, 2018
c20b672
Fix #11
bableharry Feb 13, 2019
dd79eba
2.0.5
bableharry Feb 13, 2019
1717f41
Update repos
bableharry Feb 14, 2019
dda1bf0
Merge pull request #12 from hsitraka/update
mpeperos Feb 14, 2019
d182493
2.0.6
bableharry Feb 14, 2019
4dbb688
update README
bableharry Feb 14, 2019
bd25116
Fixes #13: Update deprecated package from hapi :arrow_up:
sratsimba Jun 6, 2019
dee4809
2.0.7
sratsimba Jun 6, 2019
bdaceb5
Update LICENSE
sratsimba Jun 19, 2019
35dfe22
Update README.md
sratsimba Aug 14, 2019
0099ac3
Update README.md
sratsimba Aug 14, 2019
f24e29b
Update README.md
sratsimba Aug 14, 2019
76a4769
feat: support absolute routes_dir
zhaoyao91 Aug 29, 2019
c914737
Update .gitignore
zhaoyao91 Aug 30, 2019
8f4d803
Updates dependencies and documentation
sratsimba Aug 30, 2019
458a3fc
Make routes_dir option required
sratsimba Aug 30, 2019
f599489
Merge branch 'patch-issue-14' of https://github.com/zhaoyao91/hapi-au…
sratsimba Aug 30, 2019
ebf334f
Update test
sratsimba Aug 30, 2019
552eec0
3.0.0
sratsimba Aug 30, 2019
76e5c2f
Update package.json
sratsimba Aug 30, 2019
83604c7
Update README.md
sratsimba Aug 30, 2019
23a2609
Update README.md
sratsimba Aug 30, 2019
ac2f0f1
Update README.md
sratsimba Aug 30, 2019
c541e2b
Updates changelog
sratsimba Aug 30, 2019
7e0b8dd
3.0.1
sratsimba Aug 30, 2019
bc074ee
Fix basic usage
sratsimba Aug 30, 2019
4582b56
3.0.2
sratsimba Aug 30, 2019
1ce876c
Fix issue #17, add dependency on @hapi/hoek
Sep 11, 2019
e7c04ce
Use wildcard for versioning hoek dependency
Sep 14, 2019
7db6871
Merge pull request #18 from colepanike/master
sratsimba Sep 15, 2019
b73fbce
3.0.3
Oct 21, 2019
fb23a44
Merge branch 'master' of https://github.com/colepanike/hapi-auto-route
Oct 21, 2019
981e77b
Merge pull request #19 from colepanike/master
sratsimba Oct 25, 2019
e0c6041
Update docs
cooxe Oct 25, 2019
9b8d111
Reverse for easy versionning
cooxe Oct 25, 2019
7376dd2
3.0.3
cooxe Oct 25, 2019
b94a7c9
3.0.4
cooxe Oct 25, 2019
c8f207f
release v3.0.4
cooxe Oct 25, 2019
27e9a77
Include undeclared dev dep
Dec 13, 2019
c67ac21
Allow routes_dir to be an array of base dirs
Dec 13, 2019
0e90ae9
3.1.0
Dec 13, 2019
a11d706
Update documentation
Dec 13, 2019
2e3292f
Merge branch 'dev' of github.com:sitraka-hq/hapi-auto-route into dev
Dec 13, 2019
f355f9b
Fix the way promises are handled here.
Dec 16, 2019
30b3d4c
Only add valid routes
Dec 16, 2019
73d49fa
Test compliance
Dec 16, 2019
2952171
Merge pull request #24 from sitraka-hq/dev
sratsimba Jan 6, 2020
fc58940
Upgrade dependencies
cooxe Apr 22, 2024
5029bce
Update Licenses and readme
cooxe Apr 22, 2024
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
20 changes: 20 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Build And Test

on: [push]

jobs:
build:

runs-on: ubuntu-latest
strategy:
matrix:
node-version: ['14.21.3', '21.7.3']

steps:
- uses: actions/checkout@v4
- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: '20.x'
- run: npm install
- run: npm test
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
node_modules/
coverage.html
*.log
yarn.lock
coverage.html
package-lock.json
.idea
5 changes: 0 additions & 5 deletions .labrc.js

This file was deleted.

1 change: 0 additions & 1 deletion .nvmrc

This file was deleted.

46 changes: 46 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# hapi-auto-route Change Log

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

## 3.0.0

- `routes_dir` requires an absolute path and must set in plugin registration.
- Updates dependencies.
- Updates documentation.

### 3.1.0

- `routes_dir` accepts an array of parameters.
- Updates documentation.

### 3.0.1

- Updates documentation.

### 3.0.2

- add dependency on @hapi/hoek

### 3.0.3

- Update documentation

### 3.0.4

- Fix unpublished version


### 2.0.3

- Fix issues [#10](https://github.com/sitrakary/hapi-auto-route/issues/10).
- Fix lint errors.

### 2.0.2

- Fix issues [#5](https://github.com/sitrakary/hapi-auto-route/issues/5).

### 2.0.1

- Update dependencies.
4 changes: 2 additions & 2 deletions LICENCE.md → LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2018 Sitraka Ratsimba
Copyright (c) 2024 Sitraka Ratsimba

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand All @@ -17,4 +17,4 @@ 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.
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 changes: 22 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
# hapi-auto-route

[![Semver](http://img.shields.io/SemVer/2.0.0.png)](http://semver.org/spec/v2.0.0.html)
[![Dependency Status](https://david-dm.org/sitrakary/hapi-auto-route.svg)](https://david-dm.org/sitrakary/hapi-auto-route)
[![devDependency Status](https://david-dm.org/sitrakay/hapi-auto-route/dev-status.svg)](https://david-dm.org/sitrakay/hapi-auto-route#info=devDependencies)
![npm](https://img.shields.io/npm/v/hapi-auto-route?style=for-the-badge)
![Travis (.org)](https://img.shields.io/travis/sitraka-hq/hapi-auto-route?style=for-the-badge)

[![NPM](https://nodei.co/npm/hapi-auto-route.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/hapi-auto-route/)

hapi-auto-route is a hapi plugin that lets you load route objects automatically from a directory.
hapi-auto-route is a hapi plugin that lets you load route objects automatically by specifying the root directory/directories containing the routes. And allow routes path to be prefixed.

Maintainer: [Sitraka Ratsimba](https://github.com/cooxe)

## Installation

For Hapi `v17.x.x`:
For Hapi `>= v17`:

```bash
npm i -S hapi-auto-route
Expand Down Expand Up @@ -38,27 +39,33 @@ package.json
```javascript
// routes/home.js
'use strict';

module.exports = {
method: 'GET',
path: '/',
handler: (request, h) => 'Hello';
}
handler: (request, h) => 'Hello'
};
```

```javascript
// server.js
'use strict';

const Hapi = require('hapi');
const Path = require('path');
const Hapi = require('@hapi/hapi');

const server = Hapi.Server({
port: 3000,
host: 'localhost'
});

const init = async () => {
await server.register(require('hapi-auto-route'));
await server.register({
plugin: require('hapi-auto-route'),
options: {
routes_dir: Path.join(__dirname, 'routes')
}
});
await server.start();
console.log(`Server is running at: ${server.info.uri}`);
};
Expand All @@ -73,17 +80,19 @@ init();

Now, you can start the server and see `Hello` at `http://localhost:3000`.

You can also provide an array of absolute paths if you want to auto-import from multiple base directories.


## API

- `routes_dir`: directory where routes are located. `routes_dir` should be relative to `process.cwd()`. Defaults to `'routes'`.
- `routes_dir`: absolute path(s) to routes directory/directories. `required`
- `pattern`: glob pattern used to find route files. Defaults to `**/!(_)*.js`.
- `use_prefix`: Use directory tree as prefix. Defaults to `true`.
- `use_prefix`: Use directory tree as prefix. Defaults to `false`.

## Contributing

If you find a bug in the source code or a mistake in the documentation, you can help us by [submitting an issue](https://github.com/sitrakay/hapi-auto-route/issues) or a [pull request](https://github.com/sitrakay/hapi-auto-route/pulls) with a fix.

## Licence

This project is licensed under the MIT License - see the [LICENSE.txt](https://github.com/sitrakay/hapi-auto-route/blob/master/LICENCE.md) file for details.
This project is licensed under the MIT License - see the [LICENSE](https://github.com/sitrakay/hapi-auto-route/blob/master/LICENSE) file for details.
31 changes: 16 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
'use strict';

const { promisify } = require('util');
const Route = require('./lib/route.js');
const Option = require('./lib/option');
const Path = require('path');

const AutoRoute = require('./lib/auto-route');

exports.plugin = {
pkg: require('./package.json'),
register: async (server, options) => {

let routes = [];
const valideOptions = Option.validate(options);
const getAllRoute = promisify(Route.getAll);
const files = await getAllRoute(valideOptions);
files.forEach((file) => {
const opts = AutoRoute.validateOptions(options);

let prefixes = [];

const files = await AutoRoute.getFiles(opts.routes_dir, opts.pattern);
let routes = AutoRoute.getRoutes(files);

if (opts.use_prefix) {
prefixes = AutoRoute.getPrefixes(files, Path.resolve(opts.routes_dir));
routes = AutoRoute.updatePaths(prefixes, routes, server.settings.router.stripTrailingSlash);
}

routes = Route.load(valideOptions, file);
if (valideOptions.use_prefix) {
routes.forEach((route) => {
routes.forEach((route) => {

route.path = Route.getPathPrefix(valideOptions, file) + route.path;
});
}
server.route(routes);
server.route(route);
});
}
};
101 changes: 101 additions & 0 deletions lib/auto-route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
'use strict';

const Glob = require('glob');
const Hoek = require('@hapi/hoek');
const Joi = require('joi');
const Path = require('path');
const Util = require('util');
const Prefix = require('./prefix');

module.exports.getFiles = (baseDir, pattern) => {

if (Array.isArray(baseDir)) {
return new Promise((resolve) => {

Promise.all(baseDir.map((d) => this.getFiles(d, pattern)))
.then((value) => resolve(Hoek.flatten(value)));
});
}

const absolutePattern = Path.join(baseDir, pattern);
const glob = Util.promisify(Glob);
return glob(absolutePattern, {});
};

module.exports.getRoutes = (filepaths) => {

const routeSchema = Joi.object().keys({
method: Joi.string().required(),
path: Joi.string().required()
// Technically handler is required as well, either in the method or in its options, but that gets sticky with plugins.
}).xor('handler', 'options').unknown(true);

const multipleRoutesAllowed = Joi.alternatives(routeSchema, Joi.array().items(routeSchema));

const files = filepaths.map((p) => require(Path.resolve(p)));

const routes = files.filter((file) => {

const result = multipleRoutesAllowed.validate(file);
return !Object.keys(result).includes('error');
});

return routes.map((file) => Hoek.clone(file));
};

module.exports.getPrefixes = (files, baseDir) => {

return files.map((file) => {

return Prefix.parse(file, baseDir).reduce((current, next) => {

return current + '/' + next;
}, '');
});
};

module.exports.updatePaths = (prefixes, routes, stripTrailingSlash) => {

const update = (route, index, removeTrailingSlash) => {

const newRoute = Hoek.clone(route);

if (prefixes[index] === '/') {
return newRoute;
}

newRoute.path = prefixes[index] + route.path;
if (removeTrailingSlash && newRoute.path.endsWith('/')) {
newRoute.path = newRoute.path.slice(0, -1);
}

return newRoute;
};

return routes.map((route, index) => {

if (Array.isArray(route)) {
return route.map((innerRoute, _index) => {

return update(innerRoute, index, stripTrailingSlash);
});
}

return update(route, index, stripTrailingSlash);
});
};

module.exports.validateOptions = (options) => {

const optionsSchema = Joi.object().keys({
routes_dir: Joi.alternatives().try(Joi.string(), Joi.array().items(Joi.string())).required(),
pattern: Joi.string().default('**/!(_)*.js'),
use_prefix: Joi.boolean().default(false)
});

const result = optionsSchema.validate(options);

Hoek.assert(!Object.keys(result).includes('error'), 'Invalid options are passed to hapi-auto-route');

return result.value;
};
22 changes: 0 additions & 22 deletions lib/option.js

This file was deleted.

28 changes: 28 additions & 0 deletions lib/prefix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

const Path = require('path');
const Hoek = require('@hapi/hoek');

module.exports.parse = (file, baseDir) => {

Hoek.assert(Path.isAbsolute(baseDir), 'baseDir must be an absolute path');

const absolutePath = Path.dirname(Path.relative(baseDir, file));
const prefixes = absolutePath.split(Path.sep);
return prefixes.map((prefix) => {

return prefix === '.' ? '' : prefix;
});
};

module.exports.joinPrefix = (prefixes) => {

return prefixes.reduce((joined, prefix) => {

if (prefix === '') {
return '';
}

return joined + '/' + prefix;
}, '');
};
Loading