domenicoven d0eea7e8cf
remove koa-qs dependency as it is not anymore maintained (#5922)
* remove koa-qs dependency as it is not anymore maintained

Signed-off-by: domenico ventura <domenico@digitware.it>

Removing dependency from koa-qs

Signed-off-by: domenico ventura <domenico@digitware.it>

* cleaning the code

Signed-off-by: domenico ventura <domenico@digitware.it>

* removed merge dependency and use object.defineProperty

Signed-off-by: domenico ventura <domenico@digitware.it>

* remove koa-qs dependency as it is not anymore maintained

Signed-off-by: domenico ventura <domenico@digitware.it>

Removing dependency from koa-qs

Signed-off-by: domenico ventura <domenico@digitware.it>

* cleaning the code

Signed-off-by: domenico ventura <domenico@digitware.it>

* removed merge dependency and use object.defineProperty

Signed-off-by: domenico ventura <domenico@digitware.it>

* removing arrow functions

Signed-off-by: domenico ventura <domenico@digitware.it>

* removing arrow functions

Signed-off-by: domenico ventura <domenico@digitware.it>

Co-authored-by: domenico ventura <domenico@digitware.it>
2020-04-24 14:09:27 +02:00

54 lines
1.0 KiB
JavaScript

'use strict';
const body = require('koa-body');
const qs = require('qs');
/**
* Body parser hook
*/
const addQsParser = app => {
Object.defineProperty(app.request, 'query', {
configurable: false,
enumerable: true,
/*
* Get parsed query-string.
*/
get() {
const qstr = this.querystring;
const cache = (this._querycache = this._querycache || {});
return cache[qstr] || (cache[qstr] = qs.parse(qstr));
},
/*
* Set query-string as an object.
*/
set(obj) {
this.querystring = qs.stringify(obj);
},
});
return app;
};
module.exports = strapi => {
return {
/**
* Initialize the hook
*/
initialize() {
strapi.app.use((ctx, next) => {
// disable for graphql
// TODO: find a better way later
if (ctx.url === '/graphql') return next();
return body({
patchKoa: true,
...strapi.config.middleware.settings.parser,
})(ctx, next);
});
addQsParser(strapi.app);
},
};
};