Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

abstract-express-router

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

abstract-express-router - npm Package Compare versions

Comparing version 1.2.0 to 1.3.0

dist/__tests__/data/test-config.js

12

lib/__tests__/index-test.js
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 }
{
"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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc