Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: hydra prefix not mandatory #584

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
}
}

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 @@
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 @@ -571,23 +577,23 @@
)
.then((data) =>
Promise.all(
data.map((hydraData) =>

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

View workflow job for this annotation

GitHub Actions / Continuous integration

Parameter 'hydraData' implicitly has an 'any' type.
convertHydraDataToReactAdminData(resource, hydraData),
),
),
)
.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