Skip to content

Commit

Permalink
Merge pull request #28 from lifeomic/undefined-params
Browse files Browse the repository at this point in the history
Clean up query params
  • Loading branch information
swain authored Jun 6, 2022
2 parents 13e6d64 + 516c737 commit 24f2b5d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 21 deletions.
24 changes: 14 additions & 10 deletions src/generate-axios-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,16 @@ const substituteParams = (url, params) =>
url
);
const removePathParams = (url, params) =>
Object.entries(params).reduce(
(accum, [name, value]) =>
url.includes(":" + name) ? accum : { ...accum, [name]: value },
{}
);
const removePathParams = (url, params, encode) =>
Object.entries(params)
.filter(([key, value]) => value !== undefined)
.reduce(
(accum, [name, value]) =>
url.includes(":" + name)
? accum
: { ...accum, [name]: encode ? encodeURIComponent(value) : value },
{}
);
const parseQueryParamsFromPagingLink = (link) => {
const params = new URLSearchParams(link.split("?")[1]);
Expand All @@ -89,20 +93,20 @@ class Client {
this.client = client;
}
getPosts(params, config) {
getPosts(data, config) {
return this.client.request({
...config,
method: "GET",
params: removePathParams("/posts", params),
url: substituteParams("/posts", params),
params: removePathParams("/posts", data, true),
url: substituteParams("/posts", data),
});
}
putPost(data, config) {
return this.client.request({
...config,
method: "PUT",
data: removePathParams("/posts/:id", data),
data: removePathParams("/posts/:id", data, false),
url: substituteParams("/posts/:id", data),
});
}
Expand Down
29 changes: 18 additions & 11 deletions src/generate-axios-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,16 @@ const substituteParams = (url, params) =>
url
);
const removePathParams = (url, params) =>
Object.entries(params).reduce(
(accum, [name, value]) =>
url.includes(':' + name) ? accum : { ...accum, [name]: value },
{}
);
const removePathParams = (url, params, encode) =>
Object.entries(params)
.filter(([key, value]) => value !== undefined)
.reduce(
(accum, [name, value]) =>
url.includes(':' + name)
? accum
: { ...accum, [name]: encode ? encodeURIComponent(value) : value },
{}
);
const parseQueryParamsFromPagingLink = (link) => {
const params = new URLSearchParams(link.split('?')[1]);
Expand All @@ -89,15 +93,18 @@ class ${outputClass} {
${Object.entries(spec.Endpoints)
.map(([endpoint, { Name }]) => {
const [method, url] = endpoint.split(' ');
const paramsName = method === 'GET' ? 'params' : 'data';
const useQueryParams = ['GET', 'DELETE'].includes(method);
return `
${Name}(${paramsName}, config) {
${Name}(data, config) {
return this.client.request({
...config,
method: '${method}',
${paramsName}: removePathParams('${url}', ${paramsName}),
url: substituteParams('${url}', ${paramsName}),
${
useQueryParams
? `params: removePathParams('${url}', data, true),`
: `data: removePathParams('${url}', data, false),`
}
url: substituteParams('${url}', data),
})
}
`;
Expand Down
28 changes: 28 additions & 0 deletions src/integration.axios.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,34 @@ describe('integration tests', () => {
});
});

test('generated code URI-encodes query parameters', async () => {
const { client, request } = await prepare();

await client.listPosts({ filter: 'some/evil/string' });

expect(request).toHaveBeenCalledTimes(1);
expect(request).toHaveBeenCalledWith({
method: 'GET',
url: '/posts/list',
params: {
filter: 'some%2Fevil%2Fstring',
},
});
});

test('generated code does not send undefined query parameters', async () => {
const { client, request } = await prepare();

await client.listPosts({ filter: undefined });

expect(request).toHaveBeenCalledTimes(1);
expect(request).toHaveBeenCalledWith({
method: 'GET',
url: '/posts/list',
params: {},
});
});

test('pagination', async () => {
const { client, request } = await prepare();

Expand Down

0 comments on commit 24f2b5d

Please sign in to comment.