fastify-qs
Advanced tools
Comparing version
{ | ||
"name": "fastify-qs", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "Fastify plugin for querystring parsing with qs", | ||
@@ -20,3 +20,7 @@ "keywords": [ | ||
"homepage": "https://github.com/webdevium/fastify-qs#readme", | ||
"author": "Vano Devium <webdevium@gmail.com>", | ||
"author": { | ||
"name": "Vano Devium", | ||
"email": "vano.devium@gmail.com", | ||
"url": "https://www.devium.me/" | ||
}, | ||
"license": "MIT", | ||
@@ -23,0 +27,0 @@ "private": false, |
'use strict' | ||
const packageJson = require('./package.json') | ||
const fp = require('fastify-plugin') | ||
const qs = require('qs') | ||
function plugin (fastify, options, next) { | ||
const plugin = (fastify, options, next) => { | ||
fastify.addHook('onRequest', (request, reply, done) => { | ||
@@ -12,4 +13,4 @@ if (options && options.disabled) { | ||
const url = request.raw.url.replace(/\?{2,}/, '?') | ||
const queryExists = url.indexOf('?') | ||
const query = queryExists > -1 ? url.slice(queryExists + 1) : '' | ||
const querySymbolIndex = url.indexOf('?') | ||
const query = querySymbolIndex !== -1 ? url.slice(querySymbolIndex + 1) : '' | ||
request.query = qs.parse(query, options) | ||
@@ -23,3 +24,3 @@ done() | ||
fastify: '>= 1.0.0', | ||
name: 'fastify-qs' | ||
name: packageJson.name | ||
}) |
# fastify-qs | ||
[](https://travis-ci.org/webdevium/fastify-qs) | ||
[](https://github.com/webdevium/fastify-qs/issues) | ||
[](https://github.com/webdevium/fastify-qs/blob/master/LICENSE.MD) | ||
[](https://www.npmjs.com/package/fastify-qs) | ||
A plugin for [Fastify](https://fastify.io/) that adds support for parsing URL query parameters with [qs](https://www.npmjs.com/package/qs). | ||
Plugin accept any options from [qs.parse](https://www.npmjs.com/package/qs#parsing-objects) method | ||
Plugin accepts any options from [qs.parse](https://www.npmjs.com/package/qs#parsing-objects) method | ||
## Install | ||
``` | ||
npm i fastify-qs | ||
``` | ||
## Example | ||
@@ -10,0 +18,0 @@ |
@@ -18,3 +18,3 @@ 'use strict' | ||
{ options: null, querystring: null, expected: {} } | ||
].forEach(function (testData) { | ||
].forEach(testData => { | ||
test('parses querystring with qs: ' + testData.querystring, (t) => { | ||
@@ -40,7 +40,8 @@ t.plan(1) | ||
const port = fastify.server.address().port | ||
const queryString = testData.querystring ? '?' + testData.querystring : '' | ||
get.concat({ | ||
url: `http://127.0.0.1:${port}/${testData.querystring ? '?' + testData.querystring : ''}`, | ||
url: `http://127.0.0.1:${port}/${queryString}`, | ||
json: true | ||
}, function (err, res, data) { | ||
}, (err, res, data) => { | ||
if (err) { | ||
@@ -47,0 +48,0 @@ t.error(err) |
Sorry, the diff of this file is not supported yet
6162
10.43%67
3.08%40
25%