Skip to content

Commit 607d9ee

Browse files
authored
Merge pull request #152 from loris/v6
- Refactor: Migrate to ES Modules and update Node.js target (remove babel, etc) - Chore: Update dependencies and fix linting - Fix: Handle multiple query parameters with same key - Chore: Clean up project files and update README
2 parents a83e88d + 24703cb commit 607d9ee

File tree

9 files changed

+60
-65
lines changed

9 files changed

+60
-65
lines changed

.babelrc.js

Lines changed: 0 additions & 13 deletions
This file was deleted.

.eslintrc.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

.travis.yml

Lines changed: 0 additions & 7 deletions
This file was deleted.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# api-query-params
22

33
[![NPM version][npm-image]][npm-url]
4-
[![Build Status][travis-image]][travis-url]
4+
55
[![Coveralls Status][coveralls-image]][coveralls-url]
66
[![Dependency Status][depstat-image]][depstat-url]
77
[![Downloads][download-badge]][npm-url]

ava.config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
export default {
22
verbose: true,
3-
require: ['@babel/register'],
43
};

eslint.config.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import globals from "globals";
2+
import pluginJs from "@eslint/js";
3+
4+
export default [
5+
{
6+
languageOptions: {
7+
globals: {
8+
...globals.node,
9+
...globals.mocha,
10+
},
11+
},
12+
rules: {
13+
"no-unused-vars": ["error", { "varsIgnorePattern": "^_" }],
14+
},
15+
},
16+
pluginJs.configs.recommended,
17+
];

package.json

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
{
22
"name": "api-query-params",
3-
"version": "5.4.0",
3+
"version": "6.0.0",
44
"description": "Convert query parameters from API urls to MongoDB queries",
5-
"main": "dist/cjs/index.js",
6-
"types": "dist/cjs/types.d.ts",
5+
"main": "src/index.js",
6+
"types": "types.d.ts",
7+
"type": "module",
78
"files": [
8-
"dist/"
9+
"src/",
10+
"types.d.ts"
911
],
1012
"scripts": {
11-
"build": "babel ./src/index.js --out-file ./dist/cjs/index.js && shx cp ./types.d.ts ./dist/cjs/",
1213
"coverage": "nyc npm test && nyc report --reporter=html",
1314
"coveralls": "nyc npm test && nyc report --reporter=text-lcov | coveralls",
1415
"lint": "eslint ./src ./test",
1516
"postcoveralls": "rimraf ./coverage",
16-
"prebuild": "npm run lint && rimraf ./dist && mkdirp ./dist/cjs",
17-
"prepublish": "npm run build",
17+
"prepublish": "yarn run test",
1818
"release": "np",
1919
"test": "ava"
2020
},
21+
"engines": {
22+
"node": ">=18"
23+
},
2124
"repository": {
2225
"type": "git",
2326
"url": "git+https://github.com/loris/api-query-params.git"
@@ -35,20 +38,12 @@
3538
},
3639
"homepage": "https://github.com/loris/api-query-params#readme",
3740
"devDependencies": {
38-
"@babel/cli": "7.16.0",
39-
"@babel/core": "7.16.0",
40-
"@babel/plugin-proposal-object-rest-spread": "7.16.0",
41-
"@babel/preset-env": "7.16.4",
42-
"@babel/register": "7.16.0",
43-
"ava": "3.15.0",
44-
"coveralls": "3.1.1",
45-
"eslint": "7.9.0",
46-
"eslint-config-gowento": "7.0.0",
47-
"mkdirp": "1.0.4",
48-
"np": "7.6.0",
49-
"nyc": "15.1.0",
50-
"prettier": "2.4.1",
51-
"rimraf": "3.0.2",
52-
"shx": "^0.3.3"
41+
"ava": "^6.1.3",
42+
"coveralls": "^3.1.1",
43+
"eslint": "^9.4.0",
44+
"np": "^10.0.5",
45+
"nyc": "^17.0.0",
46+
"prettier": "^3.3.2",
47+
"rimraf": "^6.0.1"
5348
}
5449
}

src/index.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import qs from 'querystring';
1+
22

33
const builtInCasters = {
44
boolean: (val) => val === 'true',
@@ -119,7 +119,9 @@ const parseUnaries = (unaries, values = { plus: 1, minus: -1 }) => {
119119
const parseJSONString = (string) => {
120120
try {
121121
return JSON.parse(string);
122-
} catch (err) {
122+
}
123+
// eslint-disable-next-line no-unused-vars
124+
catch (_err) {
123125
return false;
124126
}
125127
};
@@ -287,7 +289,25 @@ const operators = [
287289

288290
const aqp = (query = '', options = {}) => {
289291
const result = {};
290-
const params = typeof query === 'string' ? qs.parse(query) : query;
292+
let params;
293+
294+
if (typeof query === 'string') {
295+
params = {};
296+
const urlSearchParams = new URLSearchParams(query);
297+
for (const [key, value] of urlSearchParams.entries()) {
298+
if (params[key]) {
299+
if (Array.isArray(params[key])) {
300+
params[key].push(value);
301+
} else {
302+
params[key] = [params[key], value];
303+
}
304+
} else {
305+
params[key] = value;
306+
}
307+
}
308+
} else {
309+
params = query;
310+
}
291311

292312
options.blacklist = options.blacklist || [];
293313

@@ -306,5 +326,4 @@ const aqp = (query = '', options = {}) => {
306326
return result;
307327
};
308328

309-
module.exports = aqp;
310-
module.exports.default = aqp;
329+
export default aqp;

test/index.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
import test from 'ava';
2-
import aqp from '../src';
2+
import aqp from '../src/index.js';
33

4-
const requiredAqp = require('../src');
54

6-
test('module imported using require', (t) => {
7-
const res = requiredAqp('key=value');
8-
t.truthy(res);
9-
});
105

116
test('filter: basic', (t) => {
127
const res = aqp('key=value');

0 commit comments

Comments
 (0)