Skip to content

Commit

Permalink
feat: add resource name in POST path requests (#338)
Browse files Browse the repository at this point in the history
* client: rename _constructGroupUri to _constructPostUri

* client: add the resource name in the path of POST requests

With this change, POST requests path will now include the resource
name (just as GET requests behave).
  • Loading branch information
pablopalacios authored Dec 17, 2021
1 parent 761bf2a commit 4b51d5c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
15 changes: 12 additions & 3 deletions libs/fetcher.client.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ function executeRequest(request, resolve, reject) {
requests: requests,
context: request.options.context,
}; // TODO: remove. leave here for now for backward compatibility
uri = request._constructGroupUri(uri);
uri = request._constructPostUri(uri);
allow_retry_post = request.operation === OP_READ;
return REST.post(
uri,
Expand Down Expand Up @@ -339,13 +339,22 @@ function executeRequest(request, resolve, reject) {

/**
* Build a final uri by adding query params to base uri from this.context
* @method _constructGroupUri
* @method _constructPostUri
* @param {String} uri the base uri
* @private
*/
Request.prototype._constructGroupUri = function (uri) {
Request.prototype._constructPostUri = function (uri) {
var query = [];
var final_uri = uri;

// We only want to append the resource if the uri is the fetchr
// one. If users set a custom uri (through clientConfig method or
// by passing a config obejct to the request), we should not
// modify it.
if (!this._clientConfig.uri) {
final_uri += '/' + this.resource;
}

forEach(
pickContext(this.options.context, this.options.contextPicker, 'POST'),
function eachContext(v, k) {
Expand Down
20 changes: 10 additions & 10 deletions tests/functional/fetchr.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,14 +270,14 @@ describe('client/server integration', () => {
expect(response).to.deep.equal({
statusCode: 0,
rawRequest: {
url: 'http://localhost:3001/',
url: 'http://localhost:3001/error',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Requested-With': 'XMLHttpRequest',
},
},
url: 'http://localhost:3001/',
url: 'http://localhost:3001/error',
timeout: 3000,
});
});
Expand All @@ -296,7 +296,7 @@ describe('client/server integration', () => {
meta: { foo: 'bar' },
output: { message: 'error' },
rawRequest: {
url: 'http://localhost:3000/api',
url: 'http://localhost:3000/api/error',
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand All @@ -305,7 +305,7 @@ describe('client/server integration', () => {
},
statusCode: 400,
timeout: 3000,
url: 'http://localhost:3000/api',
url: 'http://localhost:3000/api/error',
});
});

Expand All @@ -325,7 +325,7 @@ describe('client/server integration', () => {
meta: {},
output: { message: 'unexpected' },
rawRequest: {
url: 'http://localhost:3000/api',
url: 'http://localhost:3000/api/error',
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand All @@ -334,7 +334,7 @@ describe('client/server integration', () => {
},
statusCode: 500,
timeout: 3000,
url: 'http://localhost:3000/api',
url: 'http://localhost:3000/api/error',
});
});

Expand All @@ -348,14 +348,14 @@ describe('client/server integration', () => {
statusCode: 404,
body: { error: 'page not found' },
rawRequest: {
url: 'http://localhost:3000/non-existent',
url: 'http://localhost:3000/non-existent/item',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Requested-With': 'XMLHttpRequest',
},
},
url: 'http://localhost:3000/non-existent',
url: 'http://localhost:3000/non-existent/item',
timeout: 3000,
});
});
Expand All @@ -373,14 +373,14 @@ describe('client/server integration', () => {
expect(response).to.deep.equal({
statusCode: 0,
rawRequest: {
url: 'http://localhost:3000/api',
url: 'http://localhost:3000/api/error',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Requested-With': 'XMLHttpRequest',
},
},
url: 'http://localhost:3000/api',
url: 'http://localhost:3000/api/error',
timeout: 20,
});
});
Expand Down
10 changes: 8 additions & 2 deletions tests/unit/libs/fetcher.client.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,11 @@ describe('Client Fetcher', function () {
expect(req.url).to.contain('?_csrf=' + context._csrf);
} else if (req.method === 'POST') {
expect(req.url).to.equal(
DEFAULT_PATH + '?_csrf=' + context._csrf
DEFAULT_PATH +
'/' +
resource +
'?_csrf=' +
context._csrf
);
}
};
Expand All @@ -134,7 +138,7 @@ describe('Client Fetcher', function () {
expect(req.url).to.contain('_csrf=' + context._csrf);
} else if (req.method === 'POST') {
expect(req.url).to.contain(
corsPath + '/?_csrf=' + context._csrf
'/' + resource + '?_csrf=' + context._csrf
);
}
};
Expand Down Expand Up @@ -312,6 +316,8 @@ describe('Client Fetcher', function () {
} else if (req.method === 'POST') {
expect(req.url).to.equal(
DEFAULT_PATH +
'/' +
resource +
'?_csrf=' +
ctx._csrf +
'&random=' +
Expand Down

0 comments on commit 4b51d5c

Please sign in to comment.