-
-
Notifications
You must be signed in to change notification settings - Fork 375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
urlEncodePathParser #438
urlEncodePathParser #438
Changes from 4 commits
f4243ee
d903e7c
b222e72
50109cc
aca52f5
75e428e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# Middy http-urlencode-body-parser middleware | ||
|
||
<div align="center"> | ||
<img alt="Middy logo" src="https://github.com/middyjs/middy/master/img/middy-logo.png"/> | ||
</div> | ||
|
||
<div align="center"> | ||
<p><strong>HTTP URLencode body parser middleware for the middy framework, the stylish Node.js middleware engine for AWS Lambda</strong></p> | ||
</div> | ||
|
||
<div align="center"> | ||
<p> | ||
<a href="http://badge.fury.io/js/%40middy%2Fhttp-urlencode-path-parser"> | ||
<img src="https://badge.fury.io/js/%40middy%2Fhttp-urlencode-path-parser.svg" alt="npm version" style="max-width:100%;"> | ||
</a> | ||
<a href="https://snyk.io/test/github/middyjs/middy"> | ||
<img src="https://snyk.io/test/github/middyjs/middy/badge.svg" alt="Known Vulnerabilities" data-canonical-src="https://snyk.io/test/github/middyjs/middy" style="max-width:100%;"> | ||
</a> | ||
<a href="https://standardjs.com/"> | ||
<img src="https://img.shields.io/badge/code_style-standard-brightgreen.svg" alt="Standard Code Style" style="max-width:100%;"> | ||
</a> | ||
<a href="https://greenkeeper.io/"> | ||
<img src="https://badges.greenkeeper.io/middyjs/middy.svg" alt="Greenkeeper badge" style="max-width:100%;"> | ||
</a> | ||
<a href="https://gitter.im/middyjs/Lobby"> | ||
<img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter" style="max-width:100%;"> | ||
</a> | ||
</p> | ||
</div> | ||
|
||
This middleware automatically parses HTTP requests with URL-encoded paths. This can happen when using path variables (ie `/{name}/`) for an endpoint and the UI `encodeURIComponent` the values before making the request. | ||
|
||
|
||
## Install | ||
|
||
To install this middleware you can use NPM: | ||
|
||
```bash | ||
npm install --save @middy/http-urlencode-path-parser | ||
``` | ||
|
||
|
||
## Options | ||
|
||
None | ||
|
||
|
||
## Sample usage | ||
|
||
```javascript | ||
const middy = require('@middy/core') | ||
const httpUrlEncodePathParser = require('@middy/http-urlencode-path-parser') | ||
|
||
const handler = middy((event, context, cb) => { | ||
cb(null, event.body) // propagates the body as response | ||
}) | ||
|
||
handler.use(httpUrlEncodePathParser({extended: false})) | ||
|
||
// When Lambda runs the handler with a sample event... | ||
const event = { | ||
|
||
pathParameters: { | ||
name: encodeURIComponent('Mîddy') | ||
} | ||
} | ||
|
||
handler(event, {}, (_, body) => { | ||
expect(body).toEqual({ | ||
name: 'Mîddy' | ||
}) | ||
}) | ||
``` | ||
|
||
|
||
## Middy documentation and examples | ||
|
||
For more documentation and examples, refers to the main [Middy monorepo on GitHub](https://github.com/middyjs/middy) or [Middy official website](https://middy.js.org). | ||
|
||
|
||
## Contributing | ||
|
||
Everyone is very welcome to contribute to this repository. Feel free to [raise issues](https://github.com/middyjs/middy/issues) or to [submit Pull Requests](https://github.com/middyjs/middy/pulls). | ||
|
||
|
||
## License | ||
|
||
Licensed under [MIT License](LICENSE). Copyright (c) 2017-2018 Luciano Mammino and the [Middy team](https://github.com/middyjs/middy/graphs/contributors). | ||
|
||
<a href="https://app.fossa.io/projects/git%2Bgithub.51.al%2Fmiddyjs%2Fmiddy?ref=badge_large"> | ||
<img src="https://app.fossa.io/api/projects/git%2Bgithub.51.al%2Fmiddyjs%2Fmiddy.svg?type=large" alt="FOSSA Status" style="max-width:100%;"> | ||
</a> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const middy = require('../../core') | ||
const urlEncodePathParser = require('../') | ||
|
||
describe('📦 Middleware URL Encoded Path Parser', () => { | ||
test('It should decode simple url encoded requests', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would add test for bad case as well here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added in the example from #429 |
||
const handler = middy((event, context, cb) => { | ||
cb(null, event.pathParameters) // propagates the body as response | ||
}) | ||
|
||
handler.use(urlEncodePathParser()) | ||
|
||
// invokes the handler | ||
const event = { | ||
pathParameters: { | ||
char: encodeURIComponent('Mîddy') | ||
} | ||
} | ||
|
||
handler(event, {}, (_, body) => { | ||
expect(body).toEqual({ | ||
char: 'Mîddy' | ||
}) | ||
}) | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import middy from '../core' | ||
|
||
declare function urlEncodePathParser(): middy.IMiddyMiddlewareObject; | ||
|
||
export default urlEncodePathParser |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module.exports = () => ({ | ||
before: (handler, next) => { | ||
if (handler.event.pathParameters) { | ||
for (let key in handler.event.pathParameters) { | ||
try { | ||
handler.event.pathParameters[key] = decodeURIComponent(handler.event.pathParameters[key]) | ||
} catch (e) { | ||
console.error(e) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about you introduce flag here, for example
Just a suggestion, this is something that I would find useful if I were to use this plugin - otherwise you won't know if this function worked or not There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I opted to a throw an error, so that the error handler middleware can take care of it. |
||
} | ||
} | ||
} | ||
|
||
next() | ||
} | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't utilize extended field anywhere
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it was copied from body parser, removed.