Skip to content

Commit 1524ede

Browse files
authored
feat: add post method for the load endpoint (#982). Thanks to @RusovDmitriy
1 parent ae705a7 commit 1524ede

File tree

5 files changed

+43
-2
lines changed

5 files changed

+43
-2
lines changed

docs/Cube.js-Backend/REST-API.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,23 @@ Response
7878
Example request:
7979

8080
```bash
81+
# Request with http method GET
8182
curl \
8283
-H "Authorization: EXAMPLE-API-TOKEN" \
8384
-G \
8485
--data-urlencode 'query={"measures":["Users.count"]}' \
8586
http://localhost:4000/cubejs-api/v1/load
86-
````
87+
88+
# Request with http method POST
89+
# Use POST to fix problem with query length limits
90+
curl \
91+
-X POST
92+
-H "Content-Type: application/json" \
93+
-H "Authorization: EXAMPLE-API-TOKEN" \
94+
-G \
95+
--data '{"query": {"measures":["Users.count"]}}' \
96+
http://localhost:4000/cubejs-api/v1/load
97+
```
8798

8899
Example response:
89100

packages/cubejs-api-gateway/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const R = require('ramda');
33
const Joi = require('@hapi/joi');
44
const moment = require('moment');
55
const uuid = require('uuid/v4');
6+
const bodyParser = require('body-parser');
67

78
const dateParser = require('./dateParser');
89
const requestParser = require('./requestParser');
@@ -303,6 +304,15 @@ class ApiGateway {
303304
});
304305
}));
305306

307+
const jsonParser = bodyParser.json({ limit: '1mb' });
308+
app.post(`${this.basePath}/v1/load`, jsonParser, this.requestMiddleware, (async (req, res) => {
309+
await this.load({
310+
query: req.body.query,
311+
context: req.context,
312+
res: this.resToResultFn(res)
313+
});
314+
}));
315+
306316
app.get(`${this.basePath}/v1/subscribe`, this.requestMiddleware, (async (req, res) => {
307317
await this.load({
308318
query: req.query.query,

packages/cubejs-api-gateway/index.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,23 @@ describe(`API Gateway`, () => {
121121

122122
expect(res.body.query.order).toStrictEqual([{ id: 'Foo.bar', desc: false }, { id: 'Foo.foo', desc: true }]);
123123
});
124+
125+
test(`post http method for load route`, async () => {
126+
const query = {
127+
measures: ["Foo.bar"],
128+
order: [
129+
['Foo.bar', 'asc'],
130+
['Foo.foo', 'desc']
131+
],
132+
};
133+
const res = await request(app)
134+
.post(`/cubejs-api/v1/load`)
135+
.set('Content-type', 'application/json')
136+
.set('Authorization', 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.t-IDcSemACt8x4iTMCda8Yhe3iZaWbvV5XKSTbuAn0M')
137+
.send({ query })
138+
.expect(200);
139+
140+
expect(res.body.query.order).toStrictEqual([{ id: 'Foo.bar', desc: false }, { id: 'Foo.foo', desc: true }]);
141+
expect(res.body.query.measures).toStrictEqual(["Foo.bar"]);
142+
});
124143
});

packages/cubejs-api-gateway/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"types": "index.d.ts",
1919
"dependencies": {
2020
"@hapi/joi": "^15.1.1",
21+
"body-parser": "^1.19.0",
2122
"chrono-node": "1.4.4",
2223
"jsonwebtoken": "^8.3.0",
2324
"moment": "^2.24.0",

packages/cubejs-api-gateway/yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ binary-extensions@^2.0.0:
697697
resolved "https://registry.yarnpkg.com/binary-extensions/-/binary-extensions-2.1.0.tgz#30fa40c9e7fe07dbc895678cd287024dea241dd9"
698698
integrity sha512-1Yj8h9Q+QDF5FzhMs/c9+6UntbD5MkRfRwac8DoEm9ZfUBZ7tZ55YcGVAzEe4bXsdQHEk+s9S5wsOKVdZrw0tQ==
699699

700-
body-parser@1.19.0:
700+
body-parser@1.19.0, body-parser@^1.19.0:
701701
version "1.19.0"
702702
resolved "https://registry.yarnpkg.com/body-parser/-/body-parser-1.19.0.tgz#96b2709e57c9c4e09a6fd66a8fd979844f69f08a"
703703
integrity sha512-dhEPs72UPbDnAQJ9ZKMNTP6ptJaionhP5cBb541nXPlW60Jepo9RV/a4fX4XWW9CuFNK22krhrj1+rgzifNCsw==

0 commit comments

Comments
 (0)