Skip to content

Commit

Permalink
order by compound field
Browse files Browse the repository at this point in the history
  • Loading branch information
PawelSuwinski committed Feb 15, 2024
1 parent 47e05b0 commit 2f489e1
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 30 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 3.4.5

* Fix validation errors shown as "Server communication error" when creating an entity

## 3.4.4

* Enum support in field guesser
Expand Down
3 changes: 2 additions & 1 deletion jest.setup.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import '@testing-library/jest-dom';
import { TextEncoder } from 'util';
import { TextEncoder, TextDecoder } from 'util';
// eslint-disable-next-line import/no-extraneous-dependencies
import { Request } from 'node-fetch';

global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder as any;
global.Request = Request as any;
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@api-platform/admin",
"version": "3.4.4",
"version": "3.4.5",
"description": "Automatic administration interface for Hydra-enabled APIs.",
"files": [
"*.md",
Expand All @@ -18,7 +18,7 @@
"license": "MIT",
"sideEffects": false,
"dependencies": {
"@api-platform/api-doc-parser": "^0.16.1",
"@api-platform/api-doc-parser": "^0.16.2",
"history": "^5.0.0",
"jsonld": "^8.1.0",
"lodash.isplainobject": "^4.0.6",
Expand Down
43 changes: 42 additions & 1 deletion src/hydra/dataProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ describe('Transform a React Admin request to an Hydra request', () => {
perPage: 30,
},
sort: {
order: '',
order: 'ASC',
field: '',
},
filter: {
Expand Down Expand Up @@ -603,4 +603,45 @@ describe('Transform a React Admin request to an Hydra request', () => {
'http://localhost/entrypoint/comments?order%5Bid%5D=ASC&page=1&itemsPerPage=30',
);
});

test('React Admin get list with compound order', async () => {
mockFetchHydra.mockClear();
mockFetchHydra.mockReturnValueOnce(
Promise.resolve({
status: 200,
headers: new Headers(),
json: {
'hydra:member': [
{ '@id': '/comments/423' },
{ '@id': '/comments/976' },
],
'hydra:totalItems': 2,
'hydra:view': {
'@id': '/comments?page=1',
'@type': 'hydra:PartialCollectionView',
'hydra:first': '/comments?page=1',
'hydra:last': '/comments?page=1',
'hydra:next': '/comments?page=1',
},
},
}),
);
const result = await dataProvider.current.getList('comments', {
pagination: { page: 1, perPage: 30 },
sort: { field: 'text, id', order: 'DESC' },
filter: false,
});
expect(result).toEqual({
data: [
{ '@id': '/comments/423', id: '/comments/423' },
{ '@id': '/comments/976', id: '/comments/976' },
],
total: 2,
});
const url = mockFetchHydra.mock.calls?.[0]?.[0] ?? new URL('https://foo');
expect(url).toBeInstanceOf(URL);
expect(url.toString()).toEqual(
'http://localhost/entrypoint/comments?order%5Btext%5D=DESC&order%5Bid%5D=DESC&page=1&itemsPerPage=30',
);
});
});
6 changes: 4 additions & 2 deletions src/hydra/dataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,9 @@ function dataProvider(
} = params as GetListParams | GetManyReferenceParams;

if (order && field) {
url.searchParams.set(`order[${field}]`, order);
field.split(',').forEach((fieldName) => {
url.searchParams.set(`order[${fieldName.trim()}]`, order);
});
}
if (page) url.searchParams.set('page', page.toString());
if (perPage) url.searchParams.set('itemsPerPage', perPage.toString());
Expand Down Expand Up @@ -722,7 +724,7 @@ function dataProvider(
page: 1,
},
filter: { id: params.ids },
sort: { field: '', order: '' },
sort: { field: '', order: 'ASC' },
}).then(({ data }) => ({ data }));
}

Expand Down
23 changes: 15 additions & 8 deletions src/hydra/fetchHydra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
getDocumentationUrlFromHeaders,
} from '@api-platform/api-doc-parser';
import jsonld from 'jsonld';
import type { NodeObject } from 'jsonld';
import type { ContextDefinition, NodeObject } from 'jsonld';
import type { JsonLdObj } from 'jsonld/jsonld-spec';
import type { HttpClientOptions, HydraHttpClientResponse } from '../types.js';

Expand Down Expand Up @@ -40,21 +40,28 @@ function fetchHydra(

delete (body as NodeObject).trace;

const documentLoader = (input: string) =>
fetchJsonLd(input, authOptions).then((response) => {
const documentLoader = (input: string) => {
const loaderOptions = authOptions;
loaderOptions.method = 'GET';
delete loaderOptions.body;

return fetchJsonLd(input, loaderOptions).then((response) => {
if (!('body' in response)) {
throw new Error(
'An empty response was received when expanding JSON-LD error document.',
);
}
return response;
});
};

return documentLoader(getDocumentationUrlFromHeaders(headers))
.then((response) =>
jsonld.expand(body, {
expandContext: response.document as ContextDefinition,
}),
)

return jsonld
.expand(body, {
base: getDocumentationUrlFromHeaders(headers),
documentLoader,
})
.then((json) =>
Promise.reject(
new HttpError(
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export {
fetchHydra,
} from './hydra/index.js';
export type { HydraAdminProps } from './hydra/index.js';
export { darkTheme, lightTheme } from './layout/index.js';
export {
OpenApiAdmin,
dataProvider as openApiDataProvider,
Expand Down
32 changes: 16 additions & 16 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,22 +226,22 @@ export type ApiPlatformAdminDataProviderTypeParams<T extends DataProviderType> =
T extends typeof GET_LIST
? ApiPlatformAdminGetListParams
: T extends typeof GET_ONE
? ApiPlatformAdminGetOneParams
: T extends typeof GET_MANY
? ApiPlatformAdminGetManyParams
: T extends typeof GET_MANY_REFERENCE
? ApiPlatformAdminGetManyReferenceParams
: T extends typeof UPDATE
? ApiPlatformAdminUpdateParams
: T extends typeof UPDATE_MANY
? ApiPlatformAdminUpdateManyParams
: T extends typeof CREATE
? ApiPlatformAdminCreateParams
: T extends typeof DELETE
? ApiPlatformAdminDeleteParams
: T extends typeof DELETE_MANY
? ApiPlatformAdminDeleteManyParams
: never;
? ApiPlatformAdminGetOneParams
: T extends typeof GET_MANY
? ApiPlatformAdminGetManyParams
: T extends typeof GET_MANY_REFERENCE
? ApiPlatformAdminGetManyReferenceParams
: T extends typeof UPDATE
? ApiPlatformAdminUpdateParams
: T extends typeof UPDATE_MANY
? ApiPlatformAdminUpdateManyParams
: T extends typeof CREATE
? ApiPlatformAdminCreateParams
: T extends typeof DELETE
? ApiPlatformAdminDeleteParams
: T extends typeof DELETE_MANY
? ApiPlatformAdminDeleteManyParams
: never;

export interface ApiPlatformAdminDataProviderFactoryParams {
entrypoint: string;
Expand Down

0 comments on commit 2f489e1

Please sign in to comment.