Skip to content

Commit

Permalink
New parseQueryString config option
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardo-veras committed Dec 21, 2021
1 parent 8221def commit d66a9c7
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 4 deletions.
60 changes: 59 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ const server = new slsRouter.Server({
auth : { //Optional
method: 'jwt', //Default 'jwt'
function: null //Default null
}
},
parseQueryString: false //Optional, default false (v1.1.0+)
});
server.route({
Expand Down Expand Up @@ -111,6 +112,63 @@ Used to indicate if all responses data should be wraped before the final respons

If set to `false` the `reply` will always act as `reply.raw().response(content)`.

##### server.options.parseQueryString `boolean | optional` (v1.1.0+)
Default value: `false`

If set to `true` the system will auto try to parse first the query string values using `JSON.parse()` and then parse the fields using [qs.parse()](https://github.com/ljharb/qs).


When is `false`:
```javascript
// GET /endpoint?foo[bar]=baz
request.query = {
"foo[bar]": "baz"
};
```

When is `true`:
```javascript
// GET /endpoint?foo[bar]=baz
request.query = {
"foo": {
"bar": "baz"
}
};
// GET /endpoint?foo[bar]=1&foo[baz]=2
request.query = {
"foo": {
"bar": 1,
"baz": 2
}
};
// GET /endpoint?foo[bar][baz]=foobarbaz
request.query = {
"foo": {
"bar": {
"baz": "foobarbaz"
}
}
};
// GET /endpoint?foo={"bar": "baz"}
request.query = {
"foo": {
"bar": "baz"
}
};
// GET /endpoint?foo[bar]={"baz": "foobarbaz"}
request.query = {
"foo": {
"bar": {
"baz": "foobarbaz"
}
}
};
```

##### server.options.auth `object | optional`
Default value: `{ method : 'jwt', function : null }`

Expand Down
3 changes: 2 additions & 1 deletion lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ exports = module.exports = class {
}
this.Joi = options.Joi;
this.joiOptions = options.joiOptions || null;
this.parseQueryString = options.parseQueryString === true;
this.auth = options.auth || {};
this.validationAllowUnknown = (options.validationAllowUnknown === true);
this.instances = [];
Expand All @@ -49,7 +50,7 @@ exports = module.exports = class {
const reply = new Reply();
if(!route) return reply.response("Route not found").code(404);

const request = new Request(route, event);
const request = new Request(route, event, this);
const server = this.instances.length?this.instances[0]:{ auth : {} };

for(let k in request.payload){
Expand Down
21 changes: 19 additions & 2 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,25 @@

const Qs = require('qs');

const QueryStringParser = (event, core) => {

if((core||{}).parseQueryString !== true){
return event.queryStringParameters || {};
}

const query = { ...event.queryStringParameters };

for(const key in query){
try { query[key] = JSON.parse(query[key]); }
catch(e){ }
}

return Qs.parse(query);

}

exports = module.exports = class {
constructor(route, event) {
constructor(route, event, core) {
let payload = {};
if(event.body){
try { payload = JSON.parse(event.body); }
Expand All @@ -24,7 +41,7 @@ exports = module.exports = class {
headers : event.headers,
method : event.httpMethod,
path : event.path,
query : event.queryStringParameters || {},
query : QueryStringParser(event, core),
params : route.params,
payload : payload,
raw : event.body
Expand Down

0 comments on commit d66a9c7

Please sign in to comment.