Skip to content

Commit

Permalink
feat: added query and header parameter aliasing
Browse files Browse the repository at this point in the history
  • Loading branch information
Aposhian committed Sep 4, 2020
1 parent 7f3e50c commit ffc9394
Show file tree
Hide file tree
Showing 4 changed files with 661 additions and 12 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)
![release](https://github.com/actions/hello-world/workflows/semantic-release/badge.svg)

# route-param-alias
Express.js middleware to substitute route parameters with values from other parts of the request.


72 changes: 72 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
const jwt = require('jsonwebtoken')

const getToken = ({ req, tokenLocation, tokenName }) => {
if (tokenLocation === 'header') {
return req.get(tokenName)
} else if (tokenLocation === 'query') {
if (!req.query) {
throw new Error('No query object found on request object')
}

return req.query[tokenName]
}
}

/**
* Get a particular value out of a JWT lying within a Express.js request object
* @param {Object} params.req Express.js request object
* @param {String} params.tokenLocation property of req where the token is to be found. ex: 'header' or 'query'
* @param {String} params.tokenName name of the token property or header
* @param {String} params.payloadKey name of the property in the JWT payload
* @return value from the JWT
*/
const getParamValue = ({ req, tokenLocation, tokenName, payloadKey }) => {
const token = getToken({ req, tokenLocation, tokenName })
const { [payloadKey]: value } = jwt.decode(token.replace('Bearer ', ''))
return value
}

/**
* Allows using route parameter aliases in a request. Aliases are mapped to values
* in the payload of a JWT provided elsewhere in the request
*
* Example:
* ```javascript
* const routeParamAlias = require('route-param-alias')
* const meConverter = routeParamAlias({
* alias: 'me',
* paramName: 'id',
* tokenLocation: 'header',
* tokenName: 'Authorization',
* payloadKey: 'sub'
* })
*
* app.get('/:id', (req, res) => {
* const payload = jwt.decode(req.headers.authorization)
*
* /// Assertions before applying middleware
* assert.equals(req.params.id, 'me')
* assert.not.equals(req.params.id, payload.sub)
*
* meConverter(req, res, () => {
* /// Assertions after applying middleware
* assert.not.equals(req.params.id, 'me')
* assert.equals(req.params.id, payload.sub)
* ...
* })
* })
* ```
*/
module.exports = ({
alias,
paramName,
tokenLocation = 'header',
tokenName = 'Authorization',
payloadKey
}) => (req, _, next) => {
if (req.params[paramName] === alias) {
req.params[paramName] = getParamValue({ req, tokenLocation, tokenName, payloadKey })
}

next()
}
Loading

0 comments on commit ffc9394

Please sign in to comment.