Skip to content

Commit

Permalink
Merge pull request #40 from Foxy/release/1.10.0
Browse files Browse the repository at this point in the history
chore: release v1.10.0
  • Loading branch information
brettflorio authored Jul 12, 2023
2 parents d7fb104 + 1d3e8df commit 3a18d5c
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
"webpack-node-externals": "^2.5.2"
},
"engines": {
"node": ">=10 <=18"
"node": ">=10 <=20"
},
"eslintConfig": {
"extends": [
Expand Down
4 changes: 2 additions & 2 deletions src/core/API/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ export class Node<TGraph extends Graph> {

if (filters !== undefined) {
filters.forEach((filter: string) => {
const params = new URLSearchParams(filter);
[...params.entries()].forEach(([key, value]) => url.searchParams.append(key, value));
const [key, value = ''] = filter.split('=');
if (key) url.searchParams.append(key, value);
});
}

Expand Down
18 changes: 18 additions & 0 deletions src/core/getResourceId.ts
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;
}
}
1 change: 1 addition & 0 deletions src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ export type { Graph } from './Graph';
export * as Nucleon from './Nucleon/index.js';

export { BooleanSelector } from './BooleanSelector.js';
export { getResourceId } from './getResourceId.js';
export { Rumour } from './Rumour/index.js';
export { API } from './API/index.js';
4 changes: 2 additions & 2 deletions tests/core/API/Node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
21 changes: 21 additions & 0 deletions tests/core/getResourceId.test.ts
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();
});
});
});

0 comments on commit 3a18d5c

Please sign in to comment.