Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change GET to POST in read method at GridXhrModel #260

Merged
merged 1 commit into from
Apr 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/docs/grid-express-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Grid Express API helps to perform the next requests:
| Method | URL | Description |
|--------|-------------|--------------|
| GET | / | Get all records |
| POST | /read | Get all records (in case large query) |
| GET | /:recordId | Get particular record |
| PUT | / | Update multiple records |
| POST | / | Create a record |
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/grid-model-xhr.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ so `serverResponse.data` will be resolved.
{% endhighlight %}

Retrieves grid records from the server.
It sends `GET` request to the URI = `settings.api` parameter taken in the constructor).
It sends `GET` request (or `POST` if received query string too large) to the URI = `settings.api` parameter taken in the constructor).
In successful case(response status == 200) returns server response, otherwise throws an error.

**Parameters**:
Expand Down
53 changes: 42 additions & 11 deletions src/grid/models/GridExpressApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class GridExpressApi {

constructor() {
this.middlewares = {
read: [asyncHandler(async (req, res, next) => {
readGet: [asyncHandler(async (req, res, next) => {
const settings = {};
if (req.query.limit) {
settings.limit = parseInt(req.query.limit);
Expand All @@ -43,14 +43,29 @@ class GridExpressApi {
if (req.query.filters) {
settings.filters = JSON.parse(req.query.filters);
}
const model = this._getModel(req, res);
const result = this._result('read');
try {
const response = await model.read(settings);
result(null, response, req, res, next);
} catch (err) {
result(err, null, req, res, next);
await this._getModelResponse(req, res, next, settings, 'readGet');
})],
readPost: [asyncHandler(async (req, res, next) => {
const settings = {};
if (req.body.limit) {
settings.limit = parseInt(req.body.limit);
}
if (req.body.offset) {
settings.offset = parseInt(req.body.offset);
}
if (req.body.sort) {
settings.sort = req.body.sort;
}
if (req.body.fields) {
settings.fields = req.body.fields;
}
if (req.body.extra) {
settings.extra = req.body.extra;
}
if (req.body.filters) {
settings.filters = req.body.filters;
}
await this._getModelResponse(req, res, next, settings, 'readPost');
})],
validate: [asyncHandler(async (req, res, next) => {
const model = this._getModel(req, res);
Expand Down Expand Up @@ -114,7 +129,8 @@ class GridExpressApi {

getRouter() {
return new express.Router()
.get('/', this.middlewares.read)
.get('/', this.middlewares.readGet)
.post('/read', this.middlewares.readPost)
.post('/validation', this.middlewares.validate)
.get('/:recordId', this.middlewares.getRecord)
.put('/', this.middlewares.update)
Expand All @@ -124,8 +140,12 @@ class GridExpressApi {
});
}

read(middlewares) {
return this._addMidelwares('read', middlewares);
readGet(middlewares) {
return this._addMidelwares('readGet', middlewares);
}

readPost(middlewares) {
return this._addMidelwares('readPost', middlewares);
}

validate(middlewares) {
Expand Down Expand Up @@ -207,6 +227,17 @@ class GridExpressApi {
}
}
}

async _getModelResponse(req, res, next, settings, method) {
const model = this._getModel(req, res);
const result = this._result(method);
try {
const response = await model.read(settings);
result(null, response, req, res, next);
} catch (err) {
result(err, null, req, res, next);
}
}
}

export default GridExpressApi;
71 changes: 55 additions & 16 deletions src/grid/models/GridXhrModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,27 +73,25 @@ class GridXhrModel extends AbstractGridModel {
* @param {Array} [settings.extra] Record IDs, we need to get for sure
*/
async read(settings) {
const parsedUrl = url.parse(this._apiUrl, true);
const maxUriLengthForGetRequest = 2048;
const queryUrl = this._getQueryUrl(settings);
const queryBody = this._getQueryBody(settings);

parsedUrl.query.fields = JSON.stringify(settings.fields);
parsedUrl.query.offset = settings.offset || 0;
if (settings.limit) {
parsedUrl.query.limit = settings.limit;
}
if (settings.filters) {
parsedUrl.query.filters = JSON.stringify(settings.filters);
}
if (settings.sort) {
parsedUrl.query.sort = JSON.stringify(settings.sort);
}
if (settings.extra) {
parsedUrl.query.extra = JSON.stringify(settings.extra);
if (url.format(queryUrl).length > maxUriLengthForGetRequest) {
const parsedUrl = url.parse(this._apiUrl, true);
parsedUrl.pathname = url.resolve(parsedUrl.pathname, 'read');

return await this._xhr({
method: 'POST',
json: true,
uri: url.format(parsedUrl),
body: queryBody
});
}
delete parsedUrl.search;

const response = await this._xhr({
method: 'GET',
uri: url.format(parsedUrl)
uri: url.format(queryUrl)
});

return JSON.parse(response);
Expand Down Expand Up @@ -212,6 +210,47 @@ class GridXhrModel extends AbstractGridModel {
getValidationDependency(fields) {
return this._validator.getValidationDependency(fields);
}

_getQueryUrl(settings) {
const parsedUrl = url.parse(this._apiUrl, true);
parsedUrl.query.fields = JSON.stringify(settings.fields);
parsedUrl.query.offset = settings.offset || 0;
if (settings.limit) {
parsedUrl.query.limit = settings.limit;
}
if (settings.filters) {
parsedUrl.query.filters = JSON.stringify(settings.filters);
}
if (settings.sort) {
parsedUrl.query.sort = JSON.stringify(settings.sort);
}
if (settings.extra) {
parsedUrl.query.extra = JSON.stringify(settings.extra);
}
delete parsedUrl.search;

return parsedUrl;
}

_getQueryBody(settings) {
const requestBody = {};
requestBody.fields = settings.fields;
requestBody.offset = settings.offset || 0;
if (settings.limit) {
requestBody.limit = settings.limit;
}
if (settings.filters) {
requestBody.filters = settings.filters;
}
if (settings.sort) {
requestBody.sort = settings.sort;
}
if (settings.extra) {
requestBody.extra = settings.extra;
}

return requestBody;
}
}

export default GridXhrModel;