@novice1/routing 1.0.11 → 1.1.3

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.
@@ -1,28 +1,29 @@
1
- /**
2
- *
3
- * @param {*} value
4
- * @param {string} [type]
5
- * @param {Function} [fn]
6
- * @param {boolean} [throwErrorIfFoundUnvalidType]
7
- */
8
- function toArray(value, type, fn, throwErrorIfFoundUnvalidType) {
9
- if (!Array.isArray(value)) {
10
- value = [value];
11
- }
12
-
13
- if (type && throwErrorIfFoundUnvalidType) {
14
- if(value.some(v => typeof v !== type)){
15
- throw new TypeError(`Expected type '${type}'`);
16
- }
17
- }
18
- if (type && !throwErrorIfFoundUnvalidType) {
19
- value = value.filter(v => typeof v === type);
20
- }
21
- if (typeof fn === 'function') {
22
- value = value.filter(fn);
23
- }
24
-
25
- return value;
26
- }
27
-
1
+ /**
2
+ *
3
+ * @param {*} value
4
+ * @param {string} [type]
5
+ * @param {Function} [fn]
6
+ * @param {boolean} [throwErrorIfFoundUnvalidType]
7
+ * @returns {Array}
8
+ */
9
+ function toArray(value, type, fn, throwErrorIfFoundUnvalidType) {
10
+ if (!Array.isArray(value)) {
11
+ value = [value];
12
+ }
13
+
14
+ if (type && throwErrorIfFoundUnvalidType) {
15
+ if(value.some(v => typeof v !== type)){
16
+ throw new TypeError(`Expected type '${type}'`);
17
+ }
18
+ }
19
+ if (type && !throwErrorIfFoundUnvalidType) {
20
+ value = value.filter(v => typeof v === type);
21
+ }
22
+ if (typeof fn === 'function') {
23
+ value = value.filter(fn);
24
+ }
25
+
26
+ return value;
27
+ }
28
+
28
29
  module.exports = toArray;
package/package.json CHANGED
@@ -1,29 +1,32 @@
1
- {
2
- "name": "@novice1/routing",
3
- "private": false,
4
- "version": "1.0.11",
5
- "description": "",
6
- "main": "index.js",
7
- "keywords": [
8
- "routing",
9
- "express",
10
- "route",
11
- "router"
12
- ],
13
- "repository": {
14
- "type": "git",
15
- "url": "https://github.com/kisiwu/novice-routing.git"
16
- },
17
- "scripts": {
18
- "test": "kaukau --config test/config/testconfig.js"
19
- },
20
- "author": "demingongo",
21
- "license": "MIT",
22
- "dependencies": {
23
- "express": "^4.17.1",
24
- "extend": "^3.0.2"
25
- },
26
- "devDependencies": {
27
- "kaukau": "^0.2.2"
28
- }
29
- }
1
+ {
2
+ "name": "@novice1/routing",
3
+ "private": false,
4
+ "version": "1.1.3",
5
+ "description": "",
6
+ "main": "index.js",
7
+ "keywords": [
8
+ "routing",
9
+ "express",
10
+ "route",
11
+ "router"
12
+ ],
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "https://github.com/kisiwu/novice-routing.git"
16
+ },
17
+ "scripts": {
18
+ "test": "kaukau --config test/config/testconfig.js"
19
+ },
20
+ "author": "demingongo",
21
+ "license": "MIT",
22
+ "dependencies": {
23
+ "array-flatten": "^3.0.0",
24
+ "express": "^4.17.3",
25
+ "extend": "^3.0.2"
26
+ },
27
+ "devDependencies": {
28
+ "@types/express": "^4.17.13",
29
+ "chai": "^4.3.6",
30
+ "kaukau": "^2.0.0"
31
+ }
32
+ }
@@ -1,29 +0,0 @@
1
- name: Node CI
2
-
3
- on:
4
- push:
5
- branches:
6
- - master
7
-
8
- jobs:
9
- build:
10
-
11
- runs-on: ubuntu-latest
12
-
13
- strategy:
14
- matrix:
15
- node-version: [10.x, 12.x, 14.x, 15.x]
16
-
17
- steps:
18
- - uses: actions/checkout@v1
19
- - name: Use Node.js ${{ matrix.node-version }}
20
- uses: actions/setup-node@v1
21
- with:
22
- node-version: ${{ matrix.node-version }}
23
- - name: npm install, build, and test
24
- run: |
25
- npm ci
26
- npm run build --if-present
27
- npm test
28
- env:
29
- CI: true
@@ -1,17 +0,0 @@
1
- module.exports = {
2
- "enableLogs": false,
3
- "exitOnFail": true,
4
- "directory": "test/tests",
5
- "options": {
6
- "bail": false,
7
- "fullTrace": true,
8
- "grep": "",
9
- "ignoreLeaks": false,
10
- "reporter": "spec",
11
- "retries": 0,
12
- "slow": 100,
13
- "timeout": 2000,
14
- "ui": "bdd",
15
- "color": true
16
- }
17
- };
@@ -1,3 +0,0 @@
1
- require('chai/register-assert'); // Using Assert style
2
- require('chai/register-expect'); // Using Expect style
3
- require('chai/register-should'); // Using Should style
@@ -1,52 +0,0 @@
1
- var router = require('../../index')();
2
-
3
- describe("Define routes and meta", () => {
4
-
5
- router.all('/*', function allMiddleware (req, res, next) {
6
- next()
7
- });
8
- // define the about route
9
- router.get({
10
- path: '/about',
11
- name: 'about',
12
- tags: ['About']
13
- }, function aboutToDo(req, res) {
14
- res.json(req.meta)
15
- });
16
- // define the post route
17
- router.post({
18
- path: '/post',
19
- name: 'Post',
20
- description: 'Post a comment',
21
- tags: 'Comments'
22
- }, function postToDo(req, res) {
23
- res.json(req.meta)
24
- });
25
-
26
- it("should have registered 'all' route", function() {
27
- expect(router.stack[0].route.path).to.equal('/*');
28
- });
29
-
30
- it("should have registered 'get' route", function() {
31
- expect(router.stack[1].route.path).to.equal('/about');
32
- });
33
-
34
- it("should have registered 'post' route", function() {
35
- expect(router.stack[2].route.path).to.equal('/post');
36
- });
37
-
38
- it("should have registered 'get' meta", function() {
39
- expect(router.stack[1].route.meta.name).to.equal('about');
40
- expect(router.stack[1].route.meta.tags)
41
- .to.be.an('array', 'meta.tags in not an array')
42
- .to.eql(['About']);
43
- });
44
-
45
- it("should have registered 'post' meta", function() {
46
- expect(router.stack[2].route.meta.name).to.equal('Post');
47
- expect(router.stack[2].route.meta.description).to.equal('Post a comment');
48
- expect(router.stack[2].route.meta.tags)
49
- .to.be.an('array', 'meta.tags in not an array')
50
- .to.eql(['Comments']);
51
- });
52
- });
@@ -1,29 +0,0 @@
1
- var router = require('../../index')();
2
-
3
- describe("Define routes and meta with 'route' method", () => {
4
- // define the homepage
5
- router.route({
6
- path: '/',
7
- description: 'homepage for example',
8
- name: 'homepage',
9
- tags: ['Index', 'Examples']
10
- }).get(function one(req, res, next) {
11
- next();
12
- }, function two(req, res) {
13
- res.send('home page')
14
- }).post(function two(req, res) {
15
- res.send('post to home page')
16
- });
17
-
18
- it("should have registered route", function() {
19
- expect(router.stack[0].route.path).to.equal('/');
20
- });
21
-
22
- it("should have registered meta", function() {
23
- expect(router.stack[0].route.meta.name).to.equal('homepage');
24
- expect(router.stack[0].route.meta.description).to.equal('homepage for example');
25
- expect(router.stack[0].route.meta.tags)
26
- .to.be.an('array', 'meta.tags in not an array')
27
- .to.eql(['Index', 'Examples']);
28
- });
29
- });
@@ -1,73 +0,0 @@
1
- var router = require('../../index')();
2
-
3
- describe("Define 'auth' routes and register middlewares using 'setAuthHandlers'", () => {
4
-
5
- // register handlers for authentication
6
- // for all routes with:
7
- // - 'auth' set to true
8
- // - registered to this router
9
- router.setAuthHandlersIfNone(function authMiddleware(req, res, next){
10
- // auth user
11
- next();
12
- },function authMiddleware2(req, res, next){
13
- // auth user
14
- next();
15
- });
16
-
17
- // define a route with 'auth' set to false/undefined
18
- router.get({
19
- path: '/comments',
20
- name: 'Comments',
21
- description: 'List of comment',
22
- tags: 'Comments'
23
- }, function list(req, res) {
24
- res.json(req.meta)
25
- });
26
-
27
- // define a route with 'auth' set to true
28
- router.put({
29
- path: '/comments/:id',
30
- name: 'Edit comment',
31
- description: 'Update a comment',
32
- auth: true,
33
- tags: 'Comments'
34
- }, function update(req, res) {
35
- res.json(req.meta)
36
- });
37
-
38
-
39
- it("should NOT have added 'auth' Layers for route with 'auth' to false", function() {
40
- expect(router.stack[0].route.meta.name).to.equal('Comments');
41
- expect(router.stack[0].route.stack)
42
- .to.be.an('array', 'route is missing stack of Layers')
43
- .to.have.lengthOf(2);
44
- //- check Layers
45
- expect(router.stack[0].route.stack[0].type)
46
- .to.be.an('undefined');
47
- expect(router.stack[0].route.stack[1].type)
48
- .to.be.an('undefined');
49
- });
50
-
51
- it("should have added 'auth' Layers for route with 'auth' to true", function() {
52
- expect(router.stack[1].route.meta.name).to.equal('Edit comment');
53
- expect(router.stack[1].route.stack)
54
- .to.be.an('array', 'route is missing stack of Layers')
55
- .to.have.lengthOf(4, 'number of Layers is different than expected');
56
-
57
- //- check Layers
58
- expect(router.stack[1].route.stack[0].type)
59
- .to.be.an('undefined');
60
- expect(router.stack[1].route.stack[1].type)
61
- .to.eql('auth');
62
- expect(router.stack[1].route.stack[2].type)
63
- .to.eql('auth');
64
- expect(router.stack[1].route.stack[3].type)
65
- .to.be.an('undefined');
66
-
67
- //- check if the right handlers are in the 'auth' Layers
68
- expect(router.stack[1].route.stack[1].name)
69
- .to.eql('authMiddleware');
70
- expect(router.stack[1].route.stack[2].name)
71
- .to.eql('authMiddleware2');
72
- });
73
- });
@@ -1,74 +0,0 @@
1
- var router = require('../../index')();
2
-
3
- describe("Register middlewares using 'setValidators'", () => {
4
-
5
- router.setAuthHandlers(function authMiddleware(req, res, next){
6
- // auth user
7
- next();
8
- },function authMiddleware2(req, res, next){
9
- // auth user
10
- next();
11
- });
12
-
13
- router.setValidators(function customValidator(req, res, next){
14
- next();
15
- });
16
-
17
- // define a route with 'auth' set to false/undefined
18
- router.get({
19
- path: '/comments',
20
- name: 'Comments',
21
- description: 'List of comment',
22
- tags: 'Comments'
23
- }, function list(req, res) {
24
- res.json(req.meta)
25
- });
26
-
27
- // define a route with 'auth' set to true
28
- router.put({
29
- path: '/comments/:id',
30
- name: 'Edit comment',
31
- description: 'Update a comment',
32
- auth: true,
33
- tags: 'Comments'
34
- }, function update(req, res) {
35
- res.json(req.meta)
36
- });
37
-
38
-
39
- it("should have added 'validator' Layers", function() {
40
- expect(router.stack[0].route.meta.name).to.equal('Comments');
41
- expect(router.stack[0].route.stack)
42
- .to.be.an('array', 'route is missing stack of Layers')
43
- .to.have.lengthOf(3);
44
- //- check Layers
45
- expect(router.stack[0].route.stack[0].type)
46
- .to.be.an('undefined');
47
- expect(router.stack[0].route.stack[1].type)
48
- .to.eql('validator');
49
- expect(router.stack[0].route.stack[2].type)
50
- .to.be.an('undefined');
51
- });
52
-
53
- it("should have added 'validator' Layers after 'auth' Layers", function() {
54
- expect(router.stack[1].route.meta.name).to.equal('Edit comment');
55
- expect(router.stack[1].route.stack)
56
- .to.be.an('array', 'route is missing stack of Layers')
57
- .to.have.lengthOf(5, 'number of Layers is different than expected');
58
-
59
- //- check Layers
60
- expect(router.stack[1].route.stack[0].type)
61
- .to.be.an('undefined');
62
- expect(router.stack[1].route.stack[1].type)
63
- .to.eql('auth');
64
- expect(router.stack[1].route.stack[2].type)
65
- .to.eql('auth');
66
- expect(router.stack[1].route.stack[3].type)
67
- .to.eql('validator');
68
- expect(router.stack[1].route.stack[4].type)
69
- .to.be.an('undefined');
70
-
71
- expect(router.stack[1].route.stack[3].name)
72
- .to.eql('customValidator');
73
- });
74
- });
@@ -1,49 +0,0 @@
1
- var router = require('../../index')();
2
-
3
- describe("Register middlewares with meta 'preValidators'", () => {
4
-
5
- router.setAuthHandlers(function authMiddleware(req, res, next){
6
- // auth user
7
- next();
8
- },function authMiddleware2(req, res, next){
9
- // auth user
10
- next();
11
- });
12
-
13
- router.setValidators(function customValidator(req, res, next){
14
- next();
15
- });
16
-
17
- // define a route with preValidators
18
- router.get({
19
- path: '/comments',
20
- name: 'Comments',
21
- description: 'List of comment',
22
- auth: true,
23
- preValidators: function prevalidator(req, res, next) {next()},
24
- tags: 'Comments'
25
- }, function list(req, res) {
26
- res.json(req.meta)
27
- });
28
-
29
-
30
- it("should have added 'preValidator' layers before 'validator' Layers and after 'auth' Layers", function() {
31
- expect(router.stack[0].route.meta.name).to.equal('Comments');
32
- expect(router.stack[0].route.stack)
33
- .to.be.an('array', 'route is missing stack of Layers')
34
- .to.have.lengthOf(6, 'number of Layers is not correct');
35
- //- check Layers
36
- expect(router.stack[0].route.stack[0].type)
37
- .to.be.an('undefined');
38
- expect(router.stack[0].route.stack[1].type)
39
- .to.eql('auth');
40
- expect(router.stack[0].route.stack[2].type)
41
- .to.eql('auth');
42
- expect(router.stack[0].route.stack[3].type)
43
- .to.eql('preValidator');
44
- expect(router.stack[0].route.stack[4].type)
45
- .to.eql('validator');
46
- expect(router.stack[0].route.stack[5].type)
47
- .to.be.an('undefined');
48
- });
49
- });