Skip to content

Commit

Permalink
Merge pull request #26 from 3imed-jaberi/3imed-jaberi-update-pkg
Browse files Browse the repository at this point in the history
Revive koa-qs ..
  • Loading branch information
niftylettuce committed May 27, 2020
2 parents bf1225e + e22ef88 commit 07c915b
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 85 deletions.
23 changes: 23 additions & 0 deletions .gitignore
@@ -1,2 +1,25 @@
# OS #
###################
.DS_Store
.idea
Thumbs.db
tmp/
temp/


# Node.js #
###################
node_modules
package-lock.json
yarn.lock
npm-debug.log
yarn-debug.log
yarn-error.log
dist


# NYC #
###################
coverage
*.lcov
.nyc_output
8 changes: 8 additions & 0 deletions .mocharc.json
@@ -0,0 +1,8 @@
{
"extension": ["js"],
"require": "should",
"reporter": "spec",
"timeout": "2000",
"watch-files": ["test/**/*.js"],
"exit": "true"
}
1 change: 1 addition & 0 deletions .npmrc
@@ -0,0 +1 @@
package-lock=false
6 changes: 3 additions & 3 deletions .travis.yml
@@ -1,11 +1,11 @@
language: node_js
node_js:
- 7
- 8
- 9
- 10
- 12
- stable
script:
- npm run test-travis
- npm run ci
after_script:
- npm install coveralls@2
- cat ./coverage/lcov.info | coveralls
19 changes: 9 additions & 10 deletions README.md
Expand Up @@ -4,7 +4,6 @@
[![build status][travis-image]][travis-url]
[![Test coverage][coveralls-image]][coveralls-url]
[![David deps][david-image]][david-url]
[![iojs version][iojs-image]][iojs-url]
[![node version][node-image]][node-url]
[![npm download][download-image]][download-url]

Expand All @@ -16,22 +15,25 @@
[coveralls-url]: https://coveralls.io/r/koajs/qs?branch=master
[david-image]: https://img.shields.io/david/koajs/qs.svg?style=flat-square
[david-url]: https://david-dm.org/koajs/qs
[iojs-image]: https://img.shields.io/badge/io.js-%3E=_1.0-yellow.svg?style=flat-square
[iojs-url]: http://iojs.org/
[node-image]: https://img.shields.io/badge/node.js-%3E=_0.11-green.svg?style=flat-square
[node-image]: https://img.shields.io/badge/node.js-%3E=_8-green.svg?style=flat-square
[node-url]: http://nodejs.org/download/
[download-image]: https://img.shields.io/npm/dm/koa-qs.svg?style=flat-square
[download-url]: https://npmjs.org/package/koa-qs

By default, Koa uses the native `querystring` module which does not provide nesting support.
This patches a koa app with nesting support via the [qs] support,
This patches a koa app with nesting support via the [qs](https://github.com/ljharb/qs) support,
which is also used by Connect and Express.

Simply wrap a koa app with this module:

```js
var koa = require('koa')
var app = koa()
// Koa 1.x.x
const koa = require('koa')
const app = koa()
require('koa-qs')(app)
// Koa 2.x.x
const Koa = require('koa')
const app = new Koa()
require('koa-qs')(app)
```

Expand Down Expand Up @@ -120,6 +122,3 @@ console.log('%j', this.query.p);
## License

[MIT](LICENSE)


[qs]: https://github.com/hapijs/qs
41 changes: 13 additions & 28 deletions index.js
@@ -1,27 +1,14 @@
var merge = require('merge-descriptors');
const merge = require('merge-descriptors');

module.exports = function (app, mode) {
mode = mode || 'extended';
var qs = require('querystring');
if (mode === 'extended') {
qs = require('qs');
}
var converter = null;
if (mode === 'strict') {
converter = function (value) {
if (!Array.isArray(value)) {
return [value];
}
return value;
};
} else if (mode === 'first') {
converter = function (value) {
if (Array.isArray(value)) {
return value[0];
}
return value;
};
}
const qs = (mode === 'extended') ? require('qs') : require('querystring');

const converter = (
mode === 'strict' && function (value) { return !Array.isArray(value) ? [value] : value }
||
mode === 'first' && function (value) { return Array.isArray(value) ? value[0] : value }
);

merge(app.request, {

Expand All @@ -31,17 +18,16 @@ module.exports = function (app, mode) {
* @return {Object}
* @api public
*/

get query() {
var str = this.querystring;
let str = this.querystring;
if (!str) return {};

var c = this._querycache = this._querycache || {};
var query = c[str];
let c = this._querycache = this._querycache || {};
let query = c[str];
if (!query) {
c[str] = query = qs.parse(str);
if (converter) {
for (var key in query) {
for (let key in query) {
query[key] = converter(query[key]);
}
}
Expand All @@ -55,11 +41,10 @@ module.exports = function (app, mode) {
* @param {Object} obj
* @api public
*/

set query(obj) {
this.querystring = qs.stringify(obj);
},
});

return app;
};
};
39 changes: 21 additions & 18 deletions package.json
Expand Up @@ -2,6 +2,9 @@
"name": "koa-qs",
"description": "qs for koa",
"version": "2.0.0",
"files": [
"index.js"
],
"author": {
"name": "Jonathan Ong",
"email": "me@jongleberry.com",
Expand All @@ -11,27 +14,27 @@
"license": "MIT",
"repository": "koajs/qs",
"dependencies": {
"merge-descriptors": "~0.0.2",
"qs": "~2.3.3"
"merge-descriptors": "^1.0.1",
"qs": "^6.9.4"
},
"devDependencies": {
"istanbul-harmony": "*",
"koa": "0",
"mocha": "2",
"should": "3",
"supertest": "0",
"urllib": "2"
"koa": "^2.11.0",
"koa-convert": "^1.2.0",
"mocha": "^7.1.2",
"nyc": "^15.0.1",
"rimraf": "^3.0.2",
"should": "^13.2.3",
"supertest": "^4.0.2",
"urllib": "^2.34.2"
},
"scripts": {
"test": "NODE_ENV=test mocha --harmony --require should --reporter spec",
"test-cov": "NODE_ENV=test node --harmony ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- --require should",
"test-travis": "NODE_ENV=test node --harmony ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha --report lcovonly -- --require should"
"test": "mocha",
"precoverage": "rimraf .nyc_output coverage",
"coverage": "nyc --reporter=lcov --reporter=text-lcov npm test",
"postcoverage":"nyc report",
"ci": "npm run coverage"
},
"engines": {
"node": ">= 0.11",
"iojs": ">= 1.0.0"
},
"files": [
"index.js"
]
}
"node": ">= 8"
}
}
52 changes: 26 additions & 26 deletions test/test.js → test/test.spec.js
@@ -1,61 +1,61 @@
var request = require('supertest')
var koa = require('koa')
var urllib = require('urllib')

var qs = require('..')
const request = require('supertest')
const Koa = require('koa')
const urllib = require('urllib')
const convert = require('koa-convert')
const qs = require('..')

describe('Koa Querystring', function () {
it('should work with extended mode by default', function (done) {
var app = qs(koa())
let app = qs(new Koa())

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

app.use(function* () {
app.use(convert(function* () {
this.query.should.eql({
a: [1, 2, 3]
a: ['1', '2', '3']
})
this.query = {
a: [1, 2]
a: ['1', '2']
}
this.query.should.eql({
a: [1, 2]
a: ['1', '2']
})
this.querystring = 'a[0]=1&a[1]=2&a[2]=3'
this.query.should.eql({
a: [1, 2, 3]
a: ['1', '2', '3']
})

this.status = 204
})
}))

request(app.listen())
.get('/?a[0]=1&a[1]=2&a[2]=3')
.expect(204, done)
})

describe('strict mode: array item', function () {
var app = qs(koa(), 'strict')
let app = qs(new Koa(), 'strict')

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

var host
let host
before(function (done) {
app.listen(0, function () {
host = 'http://localhost:' + this.address().port
host = `http://localhost:${this.address().port}`
done()
})
})

it('should return the query params array', function (done) {
urllib.request(host + '/foo?p=a,b&p=b,c&empty=&empty=&empty=&n=1&n=2&n=1&ok=true', {
urllib.request(`${host}/foo?p=a,b&p=b,c&empty=&empty=&empty=&n=1&n=2&n=1&ok=true`, {
dataType: 'json'
}, function (err, body, res) {
res.statusCode.should.equal(200)
Expand Down Expand Up @@ -83,22 +83,22 @@ describe('Koa Querystring', function () {
})

describe('first mode: first string item', function () {
var app = qs(koa(), 'first')
let app = qs(new Koa(), 'first')

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

var host
let host
before(function (done) {
app.listen(0, function () {
host = 'http://localhost:' + this.address().port
host = `http://localhost:${this.address().port}`
done()
})
})

it('should return the first query params string', function (done) {
urllib.request(host + '/foo?p=a,b&p=b,c&empty=&empty=&empty=&n=1&n=2&n=1&ok=true', {
urllib.request(`${host}/foo?p=a,b&p=b,c&empty=&empty=&empty=&n=1&n=2&n=1&ok=true`, {
dataType: 'json'
}, function (err, body, res) {
res.statusCode.should.equal(200)
Expand Down

0 comments on commit 07c915b

Please sign in to comment.