Skip to content
This repository has been archived by the owner on Aug 23, 2023. It is now read-only.

Commit

Permalink
feat(buildFetchUrl): enable optional ids via question mark
Browse files Browse the repository at this point in the history
  • Loading branch information
PatNeedham committed Apr 28, 2020
1 parent 5449b7d commit 82e1f54
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ In an ideal world, your REST API follows all the [right patterns and best practi
To use the functions that operate on collections, your API and config needs to meet some basic requirements:

* The URL specified on the `fetch` config should return the full collection when the `/:id` is omitted.
* URLs can have optional path parameters by including `?`, such as `${process.env.HOST_URL}/users/:userId?/books`.
* The API must return an array of resource objects. If it returns an object (common in paged APIs), you can use the `transformData` config function to return the array field of the object instead.
* Each object in the array must have a unique ID field, normally named `id`. If it is not named `id`, then the field name must be set in the `idKey` prop on the resource config.

Expand Down
23 changes: 23 additions & 0 deletions __tests__/helpers/url.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,29 @@ describe('buildFetchUrl', () => {
expect(url).toBe('http://api.domain.com/users/123/posts');
});

it('should handle empty optional ids by removing extra forward slashes or question marks', () => {
const baseUrl = 'http://api.domain.com/users/:userId?/posts/:postId';
const url = buildFetchUrl({ url: baseUrl, id: { someId: 'huh' } });
expect(url).toBe('http://api.domain.com/users/posts');
});

it('should handle non-empty optional ids', () => {
const baseUrl = 'http://api.domain.com/users/:userId?/posts/:postId';
const url = buildFetchUrl({ url: baseUrl, id: { userId: '123', someId: 'huh' } });
expect(url).toBe('http://api.domain.com/users/123/posts');
});

it('omits `:user?` when id field is not passed', () => {
const baseUrl = 'http://api.domain.com/users/:user?/comments';
const url = buildFetchUrl({
url: baseUrl,
opts: {
query: { someModifier: 'questionable?' },
},
});
expect(url).toBe('http://api.domain.com/users/comments?someModifier=questionable?');
});

it('should add query params', () => {
const baseUrl = 'http://api.domain.com/users/:userId';
const url = buildFetchUrl({
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export function replaceUrlParams({ url, id }) {
export function buildFetchUrl({ url, id, opts }) {
const [protocolAndDomain, remainderUrl] = splitUrlByProtocolAndDomain(url);

let builtUrl = replaceUrlParams({ url: remainderUrl, id });
let builtUrl = replaceUrlParams({ url: remainderUrl, id }).replace(/\?\//g, '/');
builtUrl = addQueryParams({ url: builtUrl, opts });

return protocolAndDomain + builtUrl;
Expand Down

0 comments on commit 82e1f54

Please sign in to comment.