Skip to content

Commit

Permalink
fix: allow for resource url parameter (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe authored Oct 8, 2019
1 parent 218ead7 commit a3ddd5b
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/apirequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ async function createAPIRequestAsync<T>(parameters: APIRequestParams) {
* In a previous version of this API, the request body was stuffed in a field
* named `resource`. This caused lots of problems, because it's not uncommon
* to have an actual named parameter required which is also named `resource`.
* This mean that users would have to use `resource_` in those cases, which
* This meant that users would have to use `resource_` in those cases, which
* pretty much nobody figures out on their own. The request body is now
* documented as being in the `requestBody` property, but we also need to keep
* using `resource` for reasons of back-compat. Cases that need to be covered
Expand All @@ -106,8 +106,14 @@ async function createAPIRequestAsync<T>(parameters: APIRequestParams) {
* - user provides just a `requestBody`
* - user provides both a `requestBody` and a `resource`
*/
const resource = params.requestBody ? params.requestBody : params.resource;
if (!params.requestBody && params.resource) {
let resource = params.requestBody;
if (
!params.requestBody &&
params.resource &&
(!parameters.requiredParams.includes('resource') ||
typeof params.resource !== 'string')
) {
resource = params.resource;
delete params.resource;
}
delete params.requestBody;
Expand Down
89 changes: 89 additions & 0 deletions test/test.apirequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,95 @@ describe('createAPIRequest', () => {
assert(result);
});

it('should not populate resource parameter in URL, if not required parameter', async () => {
const scope = nock(url)
.post('/')
.reply(200, fakeResponse);
const result = await createAPIRequest<FakeParams>({
options: {
url,
method: 'POST',
},
params: {
resource: {
foo: 'bar',
},
},
requiredParams: [],
pathParams: [],
context: fakeContext,
});
scope.done();
assert.strictEqual(result.data, fakeResponse as {});
assert(result);
});

it('should not populate resource parameter in URL, if it is an object', async () => {
const scope = nock(url)
.post('/')
.reply(200, fakeResponse);
try {
const result = await createAPIRequest<FakeParams>({
options: {
url,
method: 'POST',
},
params: {
resource: {
foo: 'bar',
},
},
requiredParams: ['resource'],
pathParams: [],
context: fakeContext,
});
} catch (err) {
assert.match(err.message, /Missing required parameters: resource/);
}
});

it('should not populate resource parameter in URL, if it is an object', async () => {
const scope = nock(url)
.post('/')
.reply(200, fakeResponse);
try {
const result = await createAPIRequest<FakeParams>({
options: {
url,
method: 'POST',
},
params: {
resource: {
foo: 'bar',
},
},
requiredParams: ['resource'],
pathParams: [],
context: fakeContext,
});
} catch (err) {
assert.match(err.message, /Missing required parameters: resource/);
}
});

it('should populate resource parameter in URL, if it is required', async () => {
const scope = nock(`${url}`)
.get(`/?resource=blerg`)
.reply(200, fakeResponse);
const result = await createAPIRequest<FakeParams>({
options: {url},
params: {
resource: 'blerg',
},
requiredParams: ['resource'],
pathParams: [],
context: fakeContext,
});
scope.done();
assert.strictEqual(result.data, fakeResponse as {});
assert(result);
});

it('should include directives in the user agent', async () => {
const scope = nock(url)
.get('/')
Expand Down

0 comments on commit a3ddd5b

Please sign in to comment.