Skip to content

Commit

Permalink
🤘
Browse files Browse the repository at this point in the history
  • Loading branch information
jsumners committed Sep 7, 2017
0 parents commit 8ef0af4
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 2
trim_trailing_whitespace = true

# [*.md]
# trim_trailing_whitespace = false
47 changes: 47 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Logs
logs
*.log
npm-debug.log*

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
node_modules

# Optional npm cache directory
.npm

# Optional REPL history
.node_repl_history

# 0x
.__browserify_string_empty.js
profile-*

# tap --cov
.nyc_output/

# JetBrains IntelliJ IDEA
.idea/
*.iml

# VS Code
.vscode/
46 changes: 46 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# fastify-formbody

A simple plugin for [Fastify][fastify] that adds a content type parser for
the content type `application/x-www-form-urlencoded`.

[fastify]: http://www.fastify.io/

## Example

Given the following code:

```
const fastify = require('fastify')()
fastify.register(require('fastify-formbody'), {}, (err) => {
if (err) throw err
})
fastify.post('/', (req, reply) => {
reply.send(req.body)
})
fastify.listen(8000, (err) => {
if (err) throw err
})
```

And a `POST` body of:

```
foo=foo&bar=bar&answer=42
```

The sent reply would be the object:

```
{
foo: 'foo',
bar: 'bar',
answer: 42
}
```

## License

[MIT License](http://jsumners.mit-license.org/)
20 changes: 20 additions & 0 deletions formbody.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict'

const fp = require('fastify-plugin')
const qs = require('qs')

const internals = {}
internals.contentParser = function (req, done) {
let body = ''
req.on('error', done)
req.on('data', (data) => { body = body + data })
req.on('end', () => { return done(qs.parse(body)) })
}

function formBodyPlugin (fastify, options, next) {
fastify.addContentTypeParser('application/x-www-form-urlencoded', internals.contentParser)
next()
}

module.exports = fp(formBodyPlugin, '>=0.15.0')
module.exports.internals = internals
34 changes: 34 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "fastify-formbody",
"version": "1.0.0",
"description": "A module for Fastify to parse x-www-form-urlencoded bodies",
"main": "formbody.js",
"scripts": {
"test": "tap 'test/**/*.test.js'"
},
"repository": {
"type": "git",
"url": "git+ssh://[email protected]/jsumners/fastify-formbody.git"
},
"keywords": [
"fastify",
"x-www-form-urlencoded"
],
"author": "James Sumners <[email protected]>",
"license": "MIT",
"bugs": {
"url": "https://github.com/jsumners/fastify-formbody/issues"
},
"homepage": "https://github.com/jsumners/fastify-formbody#readme",
"devDependencies": {
"abstract-logging": "^1.0.0",
"fastify": "^0.26.2",
"request": "^2.81.0",
"standard": "^10.0.3",
"tap": "^10.7.2"
},
"dependencies": {
"fastify-plugin": "^0.1.0",
"qs": "^6.5.0"
}
}
41 changes: 41 additions & 0 deletions test/integration.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict'

const tap = require('tap')
const test = tap.test
const fastify = require('fastify')({logger: require('abstract-logging')})
const request = require('request')
const plugin = require('../')

test('successful route', (t) => {
t.plan(1)
try {
fastify.post('/test1', (req, res) => {
res.send(req.body)
})
t.pass()
} catch (e) {
t.fail(e.message)
}
})

fastify.register(plugin, (err) => { if (err) tap.error(err) })

fastify.listen(0, (err) => {
if (err) tap.error(err)
fastify.server.unref()

const reqOpts = {
method: 'POST',
baseUrl: 'http://localhost:' + fastify.server.address().port
}
const req = request.defaults(reqOpts)

test('success route succeeds', (t) => {
t.plan(3)
req({uri: '/test1', form: {foo: 'foo'}}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.deepEqual(JSON.parse(body), {foo: 'foo'})
})
})
})

0 comments on commit 8ef0af4

Please sign in to comment.