Skip to content

Commit

Permalink
Merge pull request #27 from 3imed-jaberi/add-opts-support
Browse files Browse the repository at this point in the history
Add opts support
  • Loading branch information
niftylettuce authored May 27, 2020
2 parents 07c915b + 15d4042 commit 8dc4c48
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 3 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
The MIT License (MIT)

Copyright (c) 2014 Jonathan Ong me@jongleberry.com
Copyright (c) 2015 koajs and other contributors
Copyright (c) 2015-present koajs and other contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const merge = require('merge-descriptors');

module.exports = function (app, mode) {
module.exports = function (app, mode, options) {
mode = mode || 'extended';
const qs = (mode === 'extended') ? require('qs') : require('querystring');

Expand All @@ -25,7 +25,7 @@ module.exports = function (app, mode) {
let c = this._querycache = this._querycache || {};
let query = c[str];
if (!query) {
c[str] = query = qs.parse(str);
c[str] = query = qs.parse(str, options);
if (converter) {
for (let key in query) {
query[key] = converter(query[key]);
Expand Down
12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@
"url": "http://jongleberry.com",
"twitter": "https://twitter.com/jongleberry"
},
"contributors": [
{
"name": "Nick Baugh",
"email": "niftylettuce@gmail.com",
"url": "http://niftylettuce.com/"
},
{
"name": "Imed Jaberi",
"email": "imed_jebari@hotmail.fr",
"url": "https://www.3imed-jaberi.com/"
}
],
"license": "MIT",
"repository": "koajs/qs",
"dependencies": {
Expand Down
47 changes: 47 additions & 0 deletions test/test.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,51 @@ describe('Koa Querystring', function () {
})
})
})

describe('custom qs options', function () {
it('should correctly parse numbers and booleans', function (done) {
let app = qs(new Koa(), null, {
decoder: function (str) {
switch (str) {
case 'true': return true;
case 'false': return false;
case 'null': return null;
}

if (/^[\d.]+$/.test(str)) {
return Number(str);
}

return str;
}
})

app.use(convert(function* (next) {
try {
yield* next
} catch (err) {
console.error(err.stack)
}
}))

app.use(convert(function* () {
this.body = this.query;
}))

request(app.listen())
.get('/?a=str&b=1&c=3.1415&d=true&e=false&f=null&g=2.7182e')
.expect(function (res) {
res.body.should.eql({
a: 'str',
b: 1,
c: 3.1415,
d: true,
e: false,
f: null,
g: '2.7182e',
})
})
.expect(200, done);
})
})
})

0 comments on commit 8dc4c48

Please sign in to comment.