-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added query and header parameter aliasing
- Loading branch information
Showing
4 changed files
with
661 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
[](http://commitizen.github.io/cz-cli/) | ||
 | ||
|
||
# route-param-alias | ||
Express.js middleware to substitute route parameters with values from other parts of the request. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
Oops, something went wrong.