abstract-express-router
Advanced tools
Comparing version 1.2.0 to 1.3.0
import { resetSpies, spies, testConfig } from './data/test-config' | ||
import { spy, stub } from 'sinon' | ||
import { createRouter } from '../..' | ||
import { createRouter } from '../createRouter' | ||
import { spy } from 'sinon' | ||
import supertest from 'supertest' | ||
@@ -55,3 +55,3 @@ | ||
.post('/api/branches') | ||
.send({ report: 'report', repository: 'foo', branch: '123'}) | ||
.send({ report: 'report', repository: 'foo', branch: '123' }) | ||
.then(result => { | ||
@@ -71,3 +71,3 @@ expect(spies.testMiddleware.callCount).toEqual(1) | ||
.post('/api/branches') | ||
.send({ report: 'report', repository: 'foo', branch: 'asd'}) | ||
.send({ report: 'report', repository: 'foo', branch: 'asd' }) | ||
.then(result => { | ||
@@ -87,3 +87,3 @@ expect(spies.testMiddleware.callCount).toEqual(1) | ||
.post('/api/pullrequests/asad') | ||
.send({ report: 'report', repository: 'foo', branch: 'asd'}) | ||
.send({ report: 'report', repository: 'foo', branch: 'asd' }) | ||
.then(result => { | ||
@@ -103,3 +103,3 @@ expect(spies.testMiddleware.callCount).toEqual(1) | ||
.post('/api/pullrequests/123/') | ||
.send({ report: 'report', repository: 'foo', base: '123', pullRequestId: 123}) | ||
.send({ report: 'report', repository: 'foo', base: '123', pullRequestId: 123 }) | ||
.then(result => { | ||
@@ -106,0 +106,0 @@ expect(spies.testMiddleware.callCount).toEqual(1) |
@@ -1,82 +0,5 @@ | ||
import express, { Router } from 'express' | ||
import { oneOfValidator, regexpValidator } from './middleware/validate' | ||
import bodyparser from 'body-parser' | ||
import compression from 'compression' | ||
import cookieParser from 'cookie-parser' | ||
import { createLogger } from './utils/log' | ||
import { createValidate } from './middleware/validate' | ||
import { createRouter } from './createRouter' | ||
const methods = ['get', 'post', 'put', 'delete'] | ||
export function createRouter(routerConfig, { logLevel, logger }) { | ||
const log = createLogger({ logLevel, logger }) | ||
const validate = createValidate(log) | ||
const router = express() | ||
router.use(cookieParser()) | ||
router.use(bodyparser.json()) | ||
router.use(bodyparser.urlencoded({ extended: true })) | ||
router.use(compression()) | ||
router.use((req, res, next) => { | ||
log(4, `${req.method} call to ${req.originalUrl}`) | ||
next() | ||
}) | ||
createSubrouter({ ...routerConfig }, log, validate, router) | ||
router.use((req, res) => { | ||
log(3, `Call to unknown path: ${req.path}`) | ||
res.status(404).send({ success: false, reason: 'Not_Found' }) | ||
}) | ||
return router | ||
} | ||
function createSubrouter(config, log, validate, router = Router({mergeParams: true})) { | ||
const { params, body, query, middleware } = config | ||
if (params || body || query) { | ||
router.use(validate({ params, body, query })) | ||
delete config.params | ||
delete config.body | ||
delete config.query | ||
} | ||
if (middleware) { | ||
if (!middleware.forEach || middleware.some(mw => typeof mw !== 'function')) { | ||
log(1, 'Invalid router config. Middleware needs to be an array of functions.') | ||
throw new Error('Invalid router config. Middleware needs to be an array of functions.') | ||
} | ||
middleware.forEach(mw => router.use(mw)) | ||
delete config.middleware | ||
} | ||
methods.forEach(method => { | ||
if (config[method]) { | ||
createEndpoint(router, method, config[method], log, validate) | ||
delete config[method] | ||
} | ||
}) | ||
Object.keys(config).forEach(key => { | ||
router.use('/' + key, createSubrouter(config[key], log, validate)) | ||
}) | ||
return router | ||
} | ||
function createEndpoint(router, method, config, log, validate) { | ||
const { params, body, query, controller, middleware = [] } = config | ||
if (!controller || typeof controller !== 'function') { | ||
log(1, 'Invalid router config. Endpoints need to define a controller function.') | ||
throw new Error('Invalid router config. Endpoints need to define a controller function.') | ||
} | ||
if (!middleware.forEach || middleware.some(mw => typeof mw !== 'function')) { | ||
log(1, 'Invalid router config. Middleware needs to be an array of functions.') | ||
throw new Error('Invalid router config. Middleware needs to be an array of functions.') | ||
} | ||
router[method]('/', validate({ params, body, query }), ...middleware, controller) | ||
} | ||
export { createRouter, regexpValidator, oneOfValidator } |
127
package.json
{ | ||
"name": "abstract-express-router", | ||
"version": "1.2.0", | ||
"description": "library to automagically create an express router-object with basic validation for params based on a javascript object", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/fochlac/abstract-express-router" | ||
}, | ||
"keywords": [ | ||
"express", | ||
"express-js", | ||
"express-router", | ||
"router", | ||
"routes", | ||
"validation", | ||
"request-mapping" | ||
], | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "jest", | ||
"coverage": "jest --coverage && codecov --token=866bf6d9-4fe7-4570-b7f3-cd2c4633c091", | ||
"release": "npm version minor --no-git-tag-version" | ||
}, | ||
"author": "Florian Riedel <low@fochlac.com>", | ||
"license": "ISC", | ||
"dependencies": { | ||
"body-parser": "^1.18.3", | ||
"compression": "^1.7.4", | ||
"cookie-parser": "^1.4.4", | ||
"express": "^4.16.4" | ||
}, | ||
"devDependencies": { | ||
"@babel/plugin-proposal-async-generator-functions": "^7.2.0", | ||
"@babel/plugin-proposal-class-properties": "^7.4.0", | ||
"@babel/plugin-proposal-object-rest-spread": "^7.4.3", | ||
"@babel/plugin-proposal-optional-catch-binding": "^7.2.0", | ||
"@babel/preset-env": "^7.4.3", | ||
"codecov": "^3.3.0", | ||
"jest": "^24.7.1", | ||
"sinon": "^7.3.1", | ||
"supertest": "^4.0.2" | ||
}, | ||
"jest": { | ||
"coverageDirectory": "./coverage/", | ||
"coveragePathIgnorePatterns": [ | ||
"<rootDir>/node_modules/" | ||
], | ||
"testMatch": [ | ||
"**/__tests__/*-test.js" | ||
], | ||
"testEnvironment": "node", | ||
"collectCoverageFrom": [ | ||
"lib/**/*.js" | ||
], | ||
"moduleFileExtensions": [ | ||
"js" | ||
], | ||
"moduleDirectories": [ | ||
"node_modules" | ||
] | ||
} | ||
"name": "abstract-express-router", | ||
"version": "1.3.0", | ||
"description": "library to automagically create an express router-object with basic validation for params based on a javascript object", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/fochlac/abstract-express-router" | ||
}, | ||
"keywords": [ | ||
"express", | ||
"express-js", | ||
"express-router", | ||
"router", | ||
"routes", | ||
"validation", | ||
"request-mapping" | ||
], | ||
"main": "dist/index.js", | ||
"scripts": { | ||
"test": "jest", | ||
"coverage": "jest --coverage && codecov --token=866bf6d9-4fe7-4570-b7f3-cd2c4633c091", | ||
"release": "npm version minor --no-git-tag-version", | ||
"build": "babel lib -d dist" | ||
}, | ||
"author": "Florian Riedel <low@fochlac.com>", | ||
"license": "ISC", | ||
"dependencies": { | ||
"@babel/cli": "^7.4.3", | ||
"body-parser": "^1.18.3", | ||
"compression": "^1.7.4", | ||
"cookie-parser": "^1.4.4", | ||
"express": "^4.16.4" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "^7.4.3", | ||
"@babel/plugin-proposal-async-generator-functions": "^7.2.0", | ||
"@babel/plugin-proposal-class-properties": "^7.4.0", | ||
"@babel/plugin-proposal-object-rest-spread": "^7.4.3", | ||
"@babel/plugin-proposal-optional-catch-binding": "^7.2.0", | ||
"@babel/preset-env": "^7.4.3", | ||
"codecov": "^3.3.0", | ||
"jest": "^24.7.1", | ||
"sinon": "^7.3.1", | ||
"supertest": "^4.0.2" | ||
}, | ||
"jest": { | ||
"coverageDirectory": "./coverage/", | ||
"coveragePathIgnorePatterns": [ | ||
"<rootDir>/node_modules/" | ||
], | ||
"testMatch": [ | ||
"**/__tests__/*-test.js" | ||
], | ||
"testEnvironment": "node", | ||
"collectCoverageFrom": [ | ||
"lib/**/*.js" | ||
], | ||
"moduleFileExtensions": [ | ||
"js" | ||
], | ||
"testPathIgnorePatterns": [ | ||
"node_modules", | ||
"dist" | ||
], | ||
"moduleDirectories": [ | ||
"node_modules" | ||
] | ||
} | ||
} |
# abstract-express-router | ||
[![codecov](https://codecov.io/gh/fochlac/abstract-express-router/branch/master/graph/badge.svg)](https://codecov.io/gh/fochlac/abstract-express-router) [![CircleCI](https://circleci.com/gh/fochlac/abstract-express-router.svg?style=svg)](https://circleci.com/gh/fochlac/abstract-express-router) | ||
[![codecov](https://codecov.io/gh/fochlac/abstract-express-router/branch/master/graph/badge.svg)](https://codecov.io/gh/fochlac/abstract-express-router) [![CircleCI](https://circleci.com/gh/fochlac/abstract-express-router.svg?style=svg)](https://circleci.com/gh/fochlac/abstract-express-router) [![npm](https://img.shields.io/npm/v/abstract-express-router.svg?style=svg)](https://www.npmjs.com/package/abstract-express-router) | ||
@@ -16,3 +16,3 @@ An framework for [express.js]( https://github.com/visionmedia/express ) which abstracts much of the syntax creating an express-router while keeping the core-functionality. This allows for a quick and clean creation of a router in a single file without writing repetitive code, while still keeping the full control by adding middlewares whereever needed. Also provides simple validation functionality for path and query parameters and for the body. | ||
Also make sure that you have Node.js 6 or newer in order to use it. | ||
Also make sure that you have Node.js 10 or newer in order to use it. | ||
@@ -19,0 +19,0 @@ ## Documentation |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
50611
26
1163
5
10
+ Added@babel/cli@^7.4.3
+ Added@ampproject/remapping@2.3.0(transitive)
+ Added@babel/cli@7.25.9(transitive)
+ Added@babel/code-frame@7.26.2(transitive)
+ Added@babel/compat-data@7.26.2(transitive)
+ Added@babel/core@7.26.0(transitive)
+ Added@babel/generator@7.26.2(transitive)
+ Added@babel/helper-compilation-targets@7.25.9(transitive)
+ Added@babel/helper-module-imports@7.25.9(transitive)
+ Added@babel/helper-module-transforms@7.26.0(transitive)
+ Added@babel/helper-string-parser@7.25.9(transitive)
+ Added@babel/helper-validator-identifier@7.25.9(transitive)
+ Added@babel/helper-validator-option@7.25.9(transitive)
+ Added@babel/helpers@7.26.0(transitive)
+ Added@babel/parser@7.26.2(transitive)
+ Added@babel/template@7.25.9(transitive)
+ Added@babel/traverse@7.25.9(transitive)
+ Added@babel/types@7.26.0(transitive)
+ Added@jridgewell/gen-mapping@0.3.5(transitive)
+ Added@jridgewell/resolve-uri@3.1.2(transitive)
+ Added@jridgewell/set-array@1.2.1(transitive)
+ Added@jridgewell/sourcemap-codec@1.5.0(transitive)
+ Added@jridgewell/trace-mapping@0.3.25(transitive)
+ Added@nicolo-ribaudo/chokidar-2@2.1.8-no-fsevents.3(transitive)
+ Addedanymatch@3.1.3(transitive)
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbinary-extensions@2.3.0(transitive)
+ Addedbrace-expansion@1.1.11(transitive)
+ Addedbraces@3.0.3(transitive)
+ Addedbrowserslist@4.24.2(transitive)
+ Addedcaniuse-lite@1.0.30001683(transitive)
+ Addedchokidar@3.6.0(transitive)
+ Addedcommander@6.2.1(transitive)
+ Addedconcat-map@0.0.1(transitive)
+ Addedconvert-source-map@2.0.0(transitive)
+ Addeddebug@4.3.7(transitive)
+ Addedelectron-to-chromium@1.5.64(transitive)
+ Addedescalade@3.2.0(transitive)
+ Addedfill-range@7.1.1(transitive)
+ Addedfs-readdir-recursive@1.1.0(transitive)
+ Addedfs.realpath@1.0.0(transitive)
+ Addedfsevents@2.3.3(transitive)
+ Addedgensync@1.0.0-beta.2(transitive)
+ Addedglob@7.2.3(transitive)
+ Addedglob-parent@5.1.2(transitive)
+ Addedglobals@11.12.0(transitive)
+ Addedinflight@1.0.6(transitive)
+ Addedis-binary-path@2.1.0(transitive)
+ Addedis-extglob@2.1.1(transitive)
+ Addedis-glob@4.0.3(transitive)
+ Addedis-number@7.0.0(transitive)
+ Addedjs-tokens@4.0.0(transitive)
+ Addedjsesc@3.0.2(transitive)
+ Addedjson5@2.2.3(transitive)
+ Addedlru-cache@5.1.1(transitive)
+ Addedmake-dir@2.1.0(transitive)
+ Addedminimatch@3.1.2(transitive)
+ Addednode-releases@2.0.18(transitive)
+ Addednormalize-path@3.0.0(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedpath-is-absolute@1.0.1(transitive)
+ Addedpicocolors@1.1.1(transitive)
+ Addedpicomatch@2.3.1(transitive)
+ Addedpify@4.0.1(transitive)
+ Addedreaddirp@3.6.0(transitive)
+ Addedsemver@5.7.26.3.1(transitive)
+ Addedslash@2.0.0(transitive)
+ Addedto-regex-range@5.0.1(transitive)
+ Addedupdate-browserslist-db@1.1.1(transitive)
+ Addedwrappy@1.0.2(transitive)
+ Addedyallist@3.1.1(transitive)