Skip to content

Commit

Permalink
fix: hydra prefix not mandatory
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Oct 1, 2024
1 parent f3331e9 commit ffc3c9a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 7 deletions.
57 changes: 57 additions & 0 deletions src/hydra/dataProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -645,4 +645,61 @@ describe('Transform a React Admin request to an Hydra request', () => {
'http://localhost/entrypoint/comments?order%5Btext%5D=DESC&order%5Bid%5D=DESC&page=1&itemsPerPage=30',
);
});

test('React Admin get list without hydra prefix', async () => {
mockFetchHydra.mockClear();
mockFetchHydra.mockReturnValue(
Promise.resolve({
status: 200,
headers: new Headers(),
json: { 'member': [], 'totalItems': 3 },
}),
);
await dataProvider.current.getList('resource', {
pagination: {
page: 1,
perPage: 30,
},
sort: {
order: 'ASC',
field: '',
},
filter: {
simple: 'foo',
nested: { param: 'bar' },
sub_nested: { sub: { param: true } },
array: ['/iri/1', '/iri/2'],
nested_array: { nested: ['/nested_iri/1', '/nested_iri/2'] },
exists: { foo: true },
nested_date: { date: { before: '2000' } },
nested_range: { range: { between: '12.99..15.99' } },
},
searchParams: { pagination: 'true' },
});
const searchParams = Array.from(
mockFetchHydra.mock.calls?.[0]?.[0]?.searchParams.entries() ?? [],
);
expect(searchParams[0]).toEqual(['pagination', 'true']);
expect(searchParams[1]).toEqual(['page', '1']);
expect(searchParams[2]).toEqual(['itemsPerPage', '30']);
expect(searchParams[3]).toEqual(['simple', 'foo']);
expect(searchParams[4]).toEqual(['nested.param', 'bar']);
expect(searchParams[5]).toEqual(['sub_nested.sub.param', 'true']);
expect(searchParams[6]).toEqual(['array[0]', '/iri/1']);
expect(searchParams[7]).toEqual(['array[1]', '/iri/2']);
expect(searchParams[8]).toEqual([
'nested_array.nested[0]',
'/nested_iri/1',
]);
expect(searchParams[9]).toEqual([
'nested_array.nested[1]',
'/nested_iri/2',
]);
expect(searchParams[10]).toEqual(['exists[foo]', 'true']);
expect(searchParams[11]).toEqual(['nested_date.date[before]', '2000']);
expect(searchParams[12]).toEqual([
'nested_range.range[between]',
'12.99..15.99',
]);
});
});
20 changes: 13 additions & 7 deletions src/hydra/dataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ class ReactAdminDocument implements ApiPlatformAdminRecord {
}
}

export const prefixHydraKey = (hasPrefix: boolean = true, str: string) => hasPrefix ? 'hydra:'+str : str;

/**
* Local cache containing embedded documents.
* It will be used to prevent useless extra HTTP query if the relation is displayed.
Expand Down Expand Up @@ -551,16 +553,20 @@ function dataProvider(
new Error(`An empty response was received for "${type}".`),
);
}
if (!('hydra:member' in response.json)) {
if (
!('hydra:member' in response.json) ||
!('member' in response.json)
) {
return Promise.reject(
new Error(`Response doesn't have a "hydra:member" field.`),
);
}
const hasPrefix = 'hydra:member' in response.json ? true : false;

Check failure on line 564 in src/hydra/dataProvider.ts

View workflow job for this annotation

GitHub Actions / Continuous integration

'hasPrefix' is declared but its value is never read.
// TODO: support other prefixes than "hydra:"
// eslint-disable-next-line no-case-declarations
const hydraCollection = response.json as HydraCollection;
return Promise.resolve(
hydraCollection['hydra:member'].map((document) =>
hydraCollection[prefixHydraKey('member')].map((document) =>

Check failure on line 569 in src/hydra/dataProvider.ts

View workflow job for this annotation

GitHub Actions / Continuous integration

Object is possibly 'null' or 'undefined'.

Check failure on line 569 in src/hydra/dataProvider.ts

View workflow job for this annotation

GitHub Actions / Continuous integration

Expected 2 arguments, but got 1.

Check failure on line 569 in src/hydra/dataProvider.ts

View workflow job for this annotation

GitHub Actions / Continuous integration

Property 'map' does not exist on type 'string | number | boolean | NodeObject | GraphObject | ({ "@Index"?: string | undefined; "@context"?: OrArray<string | ContextDefinition | null> | undefined; } & { ...; }) | ... 15 more ... | { ...; }'.

Check failure on line 569 in src/hydra/dataProvider.ts

View workflow job for this annotation

GitHub Actions / Continuous integration

Parameter 'document' implicitly has an 'any' type.
transformJsonLdDocumentToReactAdminDocument(
document,
true,
Expand All @@ -577,17 +583,17 @@ function dataProvider(
),
)
.then((data) => {
if (hydraCollection['hydra:totalItems'] !== undefined) {
if (hydraCollection[prefixHydraKey('totalItems')] !== undefined) {

Check failure on line 586 in src/hydra/dataProvider.ts

View workflow job for this annotation

GitHub Actions / Continuous integration

Expected 2 arguments, but got 1.
return {
data,
total: hydraCollection['hydra:totalItems'],
total: hydraCollection[prefixHydraKey('totalItems')],

Check failure on line 589 in src/hydra/dataProvider.ts

View workflow job for this annotation

GitHub Actions / Continuous integration

Expected 2 arguments, but got 1.
};
}
if (hydraCollection['hydra:view']) {
if (hydraCollection[prefixHydraKey('view')]) {

Check failure on line 592 in src/hydra/dataProvider.ts

View workflow job for this annotation

GitHub Actions / Continuous integration

Expected 2 arguments, but got 1.
const pageInfo = {
hasNextPage: !!hydraCollection['hydra:view']['hydra:next'],
hasNextPage: !!hydraCollection[prefixHydraKey('view')][prefixHydraKey('next')],

Check failure on line 594 in src/hydra/dataProvider.ts

View workflow job for this annotation

GitHub Actions / Continuous integration

Object is possibly 'null' or 'undefined'.
hasPreviousPage:
!!hydraCollection['hydra:view']['hydra:previous'],
!!hydraCollection[prefixHydraKey('view')][prefixHydraKey('previous')],
};
return {
data,
Expand Down

0 comments on commit ffc3c9a

Please sign in to comment.