@ar4mirez/hapi-safe-route 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) 2016 Angel Ramirez &lt;angel@cuemby.com&gt;, Vladimir de Turckheim <angel@cuemby.com>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ 'Software'), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,26 @@
1
+ # hapi-safe-route
2
+
3
+ Register routes in a safer way avoiding server crash while running or at startup time.
4
+
5
+ [![Dependency Status](https://david-dm.org/ar4mirez/hapi-safe-route.svg)](https://david-dm.org/ar4mirez/hapi-safe-route)
6
+ [![devDependency Status](https://david-dm.org/ar4mirez/hapi-safe-route/dev-status.svg?theme=shields.io)](https://david-dm.org/ar4mirez/hapi-safe-route#info=devDependencies)
7
+ [![Build Status](https://travis-ci.org/ar4mirez/hapi-safe-route.svg?branch=master)](https://travis-ci.org/ar4mirez/hapi-safe-route)
8
+
9
+
10
+ ## Install
11
+
12
+ ```bash
13
+ $ npm install hapi-safe-route
14
+ ```
15
+
16
+
17
+ ## Usage
18
+
19
+ ```bash
20
+ $ echo "details coming soon"
21
+ ```
22
+
23
+
24
+ ## License
25
+
26
+ MIT
package/lib/index.js ADDED
@@ -0,0 +1,56 @@
1
+ 'use strict';
2
+
3
+ const Joi = require('joi');
4
+
5
+ const internals = {
6
+ schema: {
7
+ routes: Joi.alternatives().try(
8
+ Joi.object(),
9
+ Joi.array().items(Joi.object())
10
+ )
11
+ }
12
+ };
13
+
14
+ exports.plugin = {
15
+ pkg: require('../package.json'),
16
+ register: async (server) => {
17
+
18
+ server.expose('safeRoute', async (routes) => {
19
+
20
+ const { error, value } = internals.schema.routes.validate(routes);
21
+
22
+ if (error) {
23
+ throw error;
24
+ }
25
+
26
+ const existingRoutes = internals.getRouteInfo(server);
27
+ const newRoutes = internals.matchRoutes(value, existingRoutes);
28
+
29
+ if (newRoutes.length) {
30
+ server.route(newRoutes);
31
+ }
32
+
33
+ return newRoutes;
34
+ });
35
+ }
36
+ };
37
+
38
+ internals.getRouteInfo = (server) => {
39
+
40
+ return server.table().map((route) => ({
41
+ method: route.method.toUpperCase(),
42
+ path: route.path
43
+ }));
44
+ };
45
+
46
+ internals.matchRoutes = (routes, existingRoutes) => {
47
+
48
+ const routeArray = Array.isArray(routes) ? routes : [routes];
49
+
50
+ return routeArray.filter((route) => {
51
+
52
+ const method = route.method.toUpperCase();
53
+ const path = route.path;
54
+ return !existingRoutes.find((r) => r.method === method && r.path === path);
55
+ });
56
+ };
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@ar4mirez/hapi-safe-route",
3
+ "description": "Register routes in a safer way avoiding server crash while running or at startup time.",
4
+ "version": "2.0.1",
5
+ "author": [
6
+ "Angel Ramirez <angel@cuemby.com>",
7
+ "Vladimir de Turckheim <>"
8
+ ],
9
+ "main": "lib/index.js",
10
+ "engines": {
11
+ "node": ">=18.0.0"
12
+ },
13
+ "bugs": {
14
+ "url": "https://github.com/ar4mirez/hapi-safe-route/issues"
15
+ },
16
+ "homepage": "https://github.com/ar4mirez/hapi-safe-route#readme",
17
+ "license": "ISC",
18
+ "dependencies": {
19
+ "joi": "^17.13.3"
20
+ },
21
+ "devDependencies": {
22
+ "@hapi/code": "^9.0.3",
23
+ "@hapi/hapi": "^21.4.4",
24
+ "@hapi/lab": "^26.0.0"
25
+ },
26
+ "scripts": {
27
+ "test": "lab -v -c"
28
+ },
29
+ "repository": {
30
+ "type": "git",
31
+ "url": "https://github.com/ar4mirez/hapi-safe-route.git"
32
+ },
33
+ "files": [
34
+ "lib/",
35
+ "package.json",
36
+ "README.md"
37
+ ]
38
+ }