Skip to content

Commit 33409ea

Browse files
authored
Big get request (#4993)
* adds middleware to convert post request containing __aposGetWithQuery in get request * uses new middleware in media manager * asks for first page when requesting images through IDs
1 parent 7a7dcca commit 33409ea

3 files changed

Lines changed: 30 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
### Adds
66

77
* Adds keyboard shortcuts for manipulating widgets in areas. Includes Cut, Copy, Paste, Delete, and Duplicate.
8+
* Adds a new way to make `GET` requests with a large query string. It can become a `POST` request containing the key `__aposGetWithQuery` in its body.
9+
A middleware checks for this key and converts the request back to a `GET` request with the right `req.query` property.
810

911
### Changes
1012

modules/@apostrophecms/express/index.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,27 @@ module.exports = {
417417
bodyParserJson: bodyParser.json({
418418
limit: '16mb',
419419
...(self.options.bodyParser && self.options.bodyParser.json)
420-
})
420+
}),
421+
// Supports POST that are supposed to be GET requests
422+
// when the query string is too big, we convert it back to GET here.
423+
convertPostToGetWithQuery(req, res, next) {
424+
if (req.method === 'POST' && req.body?.__aposGetWithQuery) {
425+
req.method = 'GET';
426+
req.query = req.body.__aposGetWithQuery;
427+
delete req.body;
428+
if (!req.url.includes('?')) {
429+
req.url = req.url + '?' + qs.stringify(req.query);
430+
} else {
431+
const [ url, queryString ] = req.url.split('?');
432+
const firstPart = queryString.endsWith('&')
433+
? queryString
434+
: (queryString ? `${queryString}&` : '');
435+
req.url = `${url}?${firstPart}${qs.stringify(req.query)}`;
436+
}
437+
}
438+
439+
return next();
440+
}
421441
};
422442
},
423443

modules/@apostrophecms/image/ui/apos/components/AposMediaManager.vue

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,11 +348,14 @@ export default {
348348
page: this.currentPage,
349349
viewContext: this.relationshipField ? 'relationship' : 'manage'
350350
};
351+
351352
// Used for batch tagging update
352353
if (options._ids) {
353354
qs._ids = options._ids;
354355
qs.perPage = options._ids.length;
356+
qs.page = 1;
355357
}
358+
356359
const filtered = !!Object.keys(this.filterValues).length;
357360
if (this.moduleOptions && Array.isArray(this.moduleOptions.filters)) {
358361
this.moduleOptions.filters.forEach(filter => {
@@ -374,8 +377,10 @@ export default {
374377
delete qs[prop];
375378
};
376379
}
377-
const apiResponse = await apos.http.get(this.moduleOptions.action, {
378-
qs,
380+
const apiResponse = await apos.http.post(this.moduleOptions.action, {
381+
body: {
382+
__aposGetWithQuery: qs
383+
},
379384
draft: true
380385
});
381386

0 commit comments

Comments
 (0)