-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from Foxy/release/1.10.0
chore: release v1.10.0
- Loading branch information
Showing
7 changed files
with
46 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* Returns the last path segment, which is usually an ID. | ||
* If it's a non-NaN numeric ID (true for most Foxy resources) then a `number` is returned. | ||
* Otherwise returns a string or `null` if the path is empty. | ||
* | ||
* @param uri `self` link on a resource, e.g. `https://api.foxy.io/stores/123` | ||
* @returns resource ID or `null` if not found | ||
*/ | ||
export function getResourceId(uri: string): string | number | null { | ||
try { | ||
const idAsString = new URL(uri).pathname.split('/').pop() || undefined; | ||
if (idAsString === undefined) return null; | ||
const idAsInt = parseInt(idAsString); | ||
return isNaN(idAsInt) ? idAsString : idAsInt; | ||
} catch { | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -119,8 +119,8 @@ describe('Core', () => { | |
|
||
testGET<{ child: never }>({ | ||
label: 'GETs resolved URL with filters', | ||
query: { filters: ['foo=bar', 'baz=qux'] }, | ||
target: 'https://example.com/?foo=bar&baz=qux', | ||
query: { filters: ['foo=bar', 'baz=qux[email protected]'] }, | ||
target: 'https://example.com/?foo=bar&baz=qux%2Bone%40example.com', | ||
}); | ||
|
||
it('errors when .get() is called with incorrect query', async () => { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { getResourceId } from '../../src/core'; | ||
|
||
describe('Core', () => { | ||
describe('getResourceId', () => { | ||
it('supports numeric IDs', () => { | ||
expect(getResourceId('https://api.foxy.io/stores/123')).toEqual(123); | ||
}); | ||
|
||
it('supports string IDs', () => { | ||
expect(getResourceId('https://api.foxy.io/stores/AB1')).toEqual('AB1'); | ||
}); | ||
|
||
it("returns null if there's no ID", () => { | ||
expect(getResourceId('https://api.foxy.io')).toBeNull(); | ||
}); | ||
|
||
it('returns null if the provided URI is invalid', () => { | ||
expect(getResourceId('this is definitely not a URI')).toBeNull(); | ||
}); | ||
}); | ||
}); |