Skip to content

Commit 07c915b

Browse files
authored
Merge pull request #26 from 3imed-jaberi/3imed-jaberi-update-pkg
Revive koa-qs ..
2 parents bf1225e + e22ef88 commit 07c915b

8 files changed

Lines changed: 104 additions & 85 deletions

File tree

.gitignore

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,25 @@
1+
# OS #
2+
###################
3+
.DS_Store
4+
.idea
5+
Thumbs.db
6+
tmp/
7+
temp/
8+
9+
10+
# Node.js #
11+
###################
112
node_modules
13+
package-lock.json
14+
yarn.lock
15+
npm-debug.log
16+
yarn-debug.log
17+
yarn-error.log
18+
dist
19+
20+
21+
# NYC #
22+
###################
223
coverage
24+
*.lcov
25+
.nyc_output

.mocharc.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extension": ["js"],
3+
"require": "should",
4+
"reporter": "spec",
5+
"timeout": "2000",
6+
"watch-files": ["test/**/*.js"],
7+
"exit": "true"
8+
}

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

.travis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
language: node_js
22
node_js:
3-
- 7
43
- 8
5-
- 9
64
- 10
5+
- 12
6+
- stable
77
script:
8-
- npm run test-travis
8+
- npm run ci
99
after_script:
1010
- npm install coveralls@2
1111
- cat ./coverage/lcov.info | coveralls

README.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
[![build status][travis-image]][travis-url]
55
[![Test coverage][coveralls-image]][coveralls-url]
66
[![David deps][david-image]][david-url]
7-
[![iojs version][iojs-image]][iojs-url]
87
[![node version][node-image]][node-url]
98
[![npm download][download-image]][download-url]
109

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

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

3027
Simply wrap a koa app with this module:
3128

3229
```js
33-
var koa = require('koa')
34-
var app = koa()
30+
// Koa 1.x.x
31+
const koa = require('koa')
32+
const app = koa()
33+
require('koa-qs')(app)
34+
// Koa 2.x.x
35+
const Koa = require('koa')
36+
const app = new Koa()
3537
require('koa-qs')(app)
3638
```
3739

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

122124
[MIT](LICENSE)
123-
124-
125-
[qs]: https://github.com/hapijs/qs

index.js

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,14 @@
1-
var merge = require('merge-descriptors');
1+
const merge = require('merge-descriptors');
22

33
module.exports = function (app, mode) {
44
mode = mode || 'extended';
5-
var qs = require('querystring');
6-
if (mode === 'extended') {
7-
qs = require('qs');
8-
}
9-
var converter = null;
10-
if (mode === 'strict') {
11-
converter = function (value) {
12-
if (!Array.isArray(value)) {
13-
return [value];
14-
}
15-
return value;
16-
};
17-
} else if (mode === 'first') {
18-
converter = function (value) {
19-
if (Array.isArray(value)) {
20-
return value[0];
21-
}
22-
return value;
23-
};
24-
}
5+
const qs = (mode === 'extended') ? require('qs') : require('querystring');
6+
7+
const converter = (
8+
mode === 'strict' && function (value) { return !Array.isArray(value) ? [value] : value }
9+
||
10+
mode === 'first' && function (value) { return Array.isArray(value) ? value[0] : value }
11+
);
2512

2613
merge(app.request, {
2714

@@ -31,17 +18,16 @@ module.exports = function (app, mode) {
3118
* @return {Object}
3219
* @api public
3320
*/
34-
3521
get query() {
36-
var str = this.querystring;
22+
let str = this.querystring;
3723
if (!str) return {};
3824

39-
var c = this._querycache = this._querycache || {};
40-
var query = c[str];
25+
let c = this._querycache = this._querycache || {};
26+
let query = c[str];
4127
if (!query) {
4228
c[str] = query = qs.parse(str);
4329
if (converter) {
44-
for (var key in query) {
30+
for (let key in query) {
4531
query[key] = converter(query[key]);
4632
}
4733
}
@@ -55,11 +41,10 @@ module.exports = function (app, mode) {
5541
* @param {Object} obj
5642
* @api public
5743
*/
58-
5944
set query(obj) {
6045
this.querystring = qs.stringify(obj);
6146
},
6247
});
6348

6449
return app;
65-
};
50+
};

package.json

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
"name": "koa-qs",
33
"description": "qs for koa",
44
"version": "2.0.0",
5+
"files": [
6+
"index.js"
7+
],
58
"author": {
69
"name": "Jonathan Ong",
710
"email": "me@jongleberry.com",
@@ -11,27 +14,27 @@
1114
"license": "MIT",
1215
"repository": "koajs/qs",
1316
"dependencies": {
14-
"merge-descriptors": "~0.0.2",
15-
"qs": "~2.3.3"
17+
"merge-descriptors": "^1.0.1",
18+
"qs": "^6.9.4"
1619
},
1720
"devDependencies": {
18-
"istanbul-harmony": "*",
19-
"koa": "0",
20-
"mocha": "2",
21-
"should": "3",
22-
"supertest": "0",
23-
"urllib": "2"
21+
"koa": "^2.11.0",
22+
"koa-convert": "^1.2.0",
23+
"mocha": "^7.1.2",
24+
"nyc": "^15.0.1",
25+
"rimraf": "^3.0.2",
26+
"should": "^13.2.3",
27+
"supertest": "^4.0.2",
28+
"urllib": "^2.34.2"
2429
},
2530
"scripts": {
26-
"test": "NODE_ENV=test mocha --harmony --require should --reporter spec",
27-
"test-cov": "NODE_ENV=test node --harmony ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- --require should",
28-
"test-travis": "NODE_ENV=test node --harmony ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha --report lcovonly -- --require should"
31+
"test": "mocha",
32+
"precoverage": "rimraf .nyc_output coverage",
33+
"coverage": "nyc --reporter=lcov --reporter=text-lcov npm test",
34+
"postcoverage":"nyc report",
35+
"ci": "npm run coverage"
2936
},
3037
"engines": {
31-
"node": ">= 0.11",
32-
"iojs": ">= 1.0.0"
33-
},
34-
"files": [
35-
"index.js"
36-
]
37-
}
38+
"node": ">= 8"
39+
}
40+
}

test/test.js renamed to test/test.spec.js

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,61 @@
1-
var request = require('supertest')
2-
var koa = require('koa')
3-
var urllib = require('urllib')
4-
5-
var qs = require('..')
1+
const request = require('supertest')
2+
const Koa = require('koa')
3+
const urllib = require('urllib')
4+
const convert = require('koa-convert')
5+
const qs = require('..')
66

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

11-
app.use(function* (next) {
11+
app.use(convert(function* (next) {
1212
try {
1313
yield* next
1414
} catch (err) {
1515
console.error(err.stack)
1616
}
17-
})
17+
}))
1818

19-
app.use(function* () {
19+
app.use(convert(function* () {
2020
this.query.should.eql({
21-
a: [1, 2, 3]
21+
a: ['1', '2', '3']
2222
})
2323
this.query = {
24-
a: [1, 2]
24+
a: ['1', '2']
2525
}
2626
this.query.should.eql({
27-
a: [1, 2]
27+
a: ['1', '2']
2828
})
2929
this.querystring = 'a[0]=1&a[1]=2&a[2]=3'
3030
this.query.should.eql({
31-
a: [1, 2, 3]
31+
a: ['1', '2', '3']
3232
})
3333

3434
this.status = 204
35-
})
35+
}))
3636

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

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

45-
app.use(function* () {
45+
app.use(convert(function* () {
4646
this.body = this.query;
47-
})
47+
}))
4848

49-
var host
49+
let host
5050
before(function (done) {
5151
app.listen(0, function () {
52-
host = 'http://localhost:' + this.address().port
52+
host = `http://localhost:${this.address().port}`
5353
done()
5454
})
5555
})
5656

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

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

88-
app.use(function* () {
88+
app.use(convert(function* () {
8989
this.body = this.query;
90-
})
90+
}))
9191

92-
var host
92+
let host
9393
before(function (done) {
9494
app.listen(0, function () {
95-
host = 'http://localhost:' + this.address().port
95+
host = `http://localhost:${this.address().port}`
9696
done()
9797
})
9898
})
9999

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

0 commit comments

Comments
 (0)