Skip to content

Commit d0ba811

Browse files
authored
feat(admin): add Mercure documentation (#1479)
1 parent 59687a2 commit d0ba811

10 files changed

+140
-108
lines changed

admin/authentication-support.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ API Platform Admin delegates the authentication support to React Admin.
44
Refer to [the chapter dedicated to authentication in the React Admin documentation](https://marmelab.com/react-admin/Authentication.html)
55
for more information.
66

7-
In short, you have to tweak data provider and api documentation parser, like this:
7+
In short, you have to tweak the data provider and the api documentation parser like this:
88

99
```javascript
1010
// admin/src/App.js
1111

12-
import React from "react";
1312
import { Redirect, Route } from "react-router-dom";
1413
import { HydraAdmin, hydraDataProvider as baseHydraDataProvider, fetchHydra as baseFetchHydra, useIntrospection } from "@api-platform/admin";
1514
import parseHydraDocumentation from "@api-platform/api-doc-parser/lib/hydra/parseHydraDocumentation";
@@ -53,7 +52,12 @@ const apiDocumentationParser = async (entrypoint) => {
5352
throw result;
5453
}
5554
};
56-
const dataProvider = baseHydraDataProvider(entrypoint, fetchHydra, apiDocumentationParser);
55+
const dataProvider = baseHydraDataProvider({
56+
entrypoint,
57+
httpClient: fetchHydra,
58+
apiDocumentationParser,
59+
mercure: true, // or false if you don't use Mercure
60+
});
5761

5862
export default () => (
5963
<HydraAdmin
@@ -64,4 +68,4 @@ export default () => (
6468
);
6569
```
6670

67-
For the implementation of the auth provider, you can find a working example in the [API Platform's demo application](https://github.com/api-platform/demo/blob/master/admin/src/authProvider.js).
71+
For the implementation of the auth provider, you can find a working example in the [API Platform's demo application](https://github.com/api-platform/demo/blob/main/pwa/utils/authProvider.tsx).

admin/components.md

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
### AdminGuesser
66

7-
`<AdminGuesser>` renders automatically an [<Admin> component](https://marmelab.com/react-admin/Admin.html) for resources exposed by a web API documented with any format supported by `@api-platform/api-doc-parser` (for Hydra documented APIs,
8-
use the [<HydraAdmin> component](components.md#hydraadmin) instead).
9-
It also creates a [schema analyzer](components.md#schemaanalyzer) context, where the `schemaAnalyzer` service (for getting information about the provided API documentation) is stored.
7+
`<AdminGuesser>` renders automatically an [Admin component](https://marmelab.com/react-admin/Admin.html) for resources exposed by a web API documented with any format supported by `@api-platform/api-doc-parser` (for Hydra documented APIs,
8+
use the [HydraAdmin component](components.md#hydraadmin) instead).
9+
It also creates a [schema analyzer](components.md#schema-analyzer) context, where the `schemaAnalyzer` service (for getting information about the provided API documentation) is stored.
1010

11-
`<AdminGuesser>` renders all exposed resources by default, but you can choose what resource you want to render by passing [<ResourceGuesser> components](components.md#resourceguesser) as children.
11+
`<AdminGuesser>` renders all exposed resources by default, but you can choose what resource you want to render by passing [ResourceGuesser components](components.md#resourceguesser) as children.
1212
Deprecated resources are hidden by default, but you can add them back using an explicit `<ResourceGuesser>` component.
1313

1414
```javascript
@@ -45,7 +45,8 @@ export default App;
4545

4646
### ResourceGuesser
4747

48-
Based on React Admin [<Resource> component](https://marmelab.com/react-admin/Resource.html), `ResourceGuesser` provides default props [<CreateGuesser>](components.md#createguesser), [<ListGuesser>](components.md#listguesser), [<EditGuesser>](components.md#editguesser) and [<ShowGuesser>](components.md#showguesser).
48+
Based on React Admin [Resource component](https://marmelab.com/react-admin/Resource.html), `<ResourceGuesser>` provides default props [CreateGuesser](components.md#createguesser), [ListGuesser](components.md#listguesser), [EditGuesser](components.md#editguesser) and [ShowGuesser](components.md#showguesser).
49+
4950
Otherwise, you can pass it your own CRUD components using `create`, `list`, `edit`, `show` props.
5051

5152
```javascript
@@ -77,18 +78,18 @@ export default App;
7778
|------|--------|-------|----------|--------------------------|
7879
| name | string | - | yes | endpoint of the resource |
7980

80-
You can also use props accepted by React Admin [<Resource> component](https://marmelab.com/react-admin/Resource.html). For example, the props `list`, `show`, `create` or `edit`.
81+
You can also use props accepted by React Admin [Resource component](https://marmelab.com/react-admin/Resource.html). For example, the props `list`, `show`, `create` or `edit`.
8182

8283
## Page Components
8384

8485
### ListGuesser
8586

86-
Based on React Admin [<List>](https://marmelab.com/react-admin/List.html), ListGuesser displays a list of resources in a [<Datagrid>](https://marmelab.com/react-admin/List.html#the-datagrid-component), according to children passed to it (usually [<FieldGuesser>](components.md#fieldguesser) or any [field component](https://marmelab.com/react-admin/Fields.html#basic-fields)
87+
Based on React Admin [List](https://marmelab.com/react-admin/List.html), `<ListGuesser>` displays a list of resources in a [Datagrid](https://marmelab.com/react-admin/List.html#the-datagrid-component), according to children passed to it (usually [FieldGuesser](components.md#fieldguesser) or any [field component](https://marmelab.com/react-admin/Fields.html#basic-fields)
8788
available in React Admin).
8889

8990
Use `hasShow` and `hasEdit` props if you want to display `show` and `edit` buttons (both set to `true` by default).
9091

91-
By default, `<ListGuesser>` comes with [<Pagination>](components.md#pagination).
92+
By default, `<ListGuesser>` comes with [Pagination](components.md#pagination).
9293

9394
```javascript
9495
// BooksList.js
@@ -114,12 +115,12 @@ export const BooksList = props => (
114115
| resource | string | - | yes | endpoint of the resource |
115116
| filters | element | - | no | filters that can be applied to the list |
116117

117-
You can also use props accepted by React Admin [<List>](https://marmelab.com/react-admin/List.html).
118+
You can also use props accepted by React Admin [List](https://marmelab.com/react-admin/List.html).
118119

119120
### CreateGuesser
120121

121-
Displays a creation page for a single item. Uses React Admin [<Create>](https://marmelab.com/react-admin/CreateEdit.html) and [<SimpleForm>](https://marmelab.com/react-admin/CreateEdit.html#the-simpleform-component) components.
122-
For simple inputs, you can pass as children API Platform Admin [<InputGuesser>](components.md#inputguesser), or any React Admin [Input components](https://marmelab.com/react-admin/Inputs.html#input-components) for more complex inputs.
122+
Displays a creation page for a single item. Uses React Admin [Create](https://marmelab.com/react-admin/CreateEdit.html) and [SimpleForm](https://marmelab.com/react-admin/CreateEdit.html#the-simpleform-component) components.
123+
For simple inputs, you can pass as children API Platform Admin [InputGuesser](components.md#inputguesser), or any React Admin [Input components](https://marmelab.com/react-admin/Inputs.html#input-components) for more complex inputs.
123124

124125
```javascript
125126
// BooksCreate.js
@@ -143,12 +144,12 @@ export const BooksCreate = props => (
143144
| children | node or function | - | no | - |
144145
| resource | string | - | yes | endpoint of the resource |
145146

146-
You can also use props accepted by React Admin [<Create>](https://marmelab.com/react-admin/CreateEdit.html).
147+
You can also use props accepted by React Admin [Create](https://marmelab.com/react-admin/CreateEdit.html).
147148

148149
### EditGuesser
149150

150-
Displays an edition page for a single item. Uses React Admin [<Edit>](https://marmelab.com/react-admin/CreateEdit.html) and [<SimpleForm>](https://marmelab.com/react-admin/CreateEdit.html#the-simpleform-component) components.
151-
For simple inputs, you can use API Platform Admin [<InputGuesser>](components.md#inputguesser), or any React Admin [Input components](https://marmelab.com/react-admin/Inputs.html#input-components) for more complex inputs.
151+
Displays an edition page for a single item. Uses React Admin [Edit](https://marmelab.com/react-admin/CreateEdit.html) and [SimpleForm](https://marmelab.com/react-admin/CreateEdit.html#the-simpleform-component) components.
152+
For simple inputs, you can use API Platform Admin [InputGuesser](components.md#inputguesser), or any React Admin [Input components](https://marmelab.com/react-admin/Inputs.html#input-components) for more complex inputs.
152153

153154
```javascript
154155
// BooksEdit.js
@@ -172,11 +173,11 @@ export const BooksEdit = props => (
172173
| children | node or function | - | no | - |
173174
| resource | string | - | yes | endpoint of the resource |
174175

175-
You can also use props accepted by React Admin [<Edit>](https://marmelab.com/react-admin/CreateEdit.html).
176+
You can also use props accepted by React Admin [Edit](https://marmelab.com/react-admin/CreateEdit.html).
176177

177178
### ShowGuesser
178179

179-
Displays a detailed page for one item. Based on React Admin [<Show> component](https://marmelab.com/react-admin/Show.html). You can pass [<FieldGuesser>](components.md#fieldguesser) as children for simple fields, or use any of React Admin [basic fields](https://marmelab.com/react-admin/Fields.html#basic-fields) for more complex fields.
180+
Displays a detailed page for one item. Based on React Admin [Show component](https://marmelab.com/react-admin/Show.html). You can pass [FieldGuesser](components.md#fieldguesser) as children for simple fields, or use any of React Admin [basic fields](https://marmelab.com/react-admin/Fields.html#basic-fields) for more complex fields.
180181

181182
```javascript
182183
// BooksShow.js
@@ -200,13 +201,14 @@ export const BooksShow = props => (
200201
| children | node or function | - | no | - |
201202
| resource | string | - | yes | endpoint of the resource |
202203

203-
You can also use props accepted by React Admin [<Show> component](https://marmelab.com/react-admin/Show.html).
204+
You can also use props accepted by React Admin [Show component](https://marmelab.com/react-admin/Show.html).
204205

205206
## Hydra
206207

207208
### HydraAdmin
208209

209-
Creates a complete Admin, as [`<AdminGuesser>`](components.md#adminguesser), but configured specially for [Hydra](https://www.hydra-cg.com/). If you want to use other formats (see supported formats: `@api-platform/api-doc-parser`) use [<AdminGuesser>](components.md#adminguesser) instead.
210+
Creates a complete Admin, as [AdminGuesser](components.md#adminguesser), but configured specially for [Hydra](https://www.hydra-cg.com/).
211+
If you want to use other formats (see supported formats: `@api-platform/api-doc-parser`) use [AdminGuesser](components.md#adminguesser) instead.
210212

211213
```javascript
212214
// App.js
@@ -217,6 +219,7 @@ const App = () => (
217219
entrypoint={entrypoint}
218220
dataProvider={dataProvider}
219221
authProvider={authProvider}
222+
mercure={true}
220223
>
221224
<ResourceGuesser name="books" />
222225
{ /* ... */ }
@@ -228,13 +231,20 @@ export default App;
228231

229232
#### HydraAdmin Props
230233

231-
| Name | Type | Value | required | Description |
232-
|------------|--------|-------|----------|-----------------------|
233-
| entrypoint | string | - | yes | entrypoint of the API |
234+
| Name | Type | Value | required | Description |
235+
|------------|----------------|-------|----------|------------------------------|
236+
| entrypoint | string | - | yes | entrypoint of the API |
237+
| mercure | boolean|object | * | yes | configuration to use Mercure |
238+
239+
\* `false` to explicitly disable, `true` to enable with default parameters or an object with the following properties:
240+
- `hub`: the URL to your Mercure hub
241+
- `jwt`: a subscriber JWT to access your Mercure hub
242+
- `topicUrl`: the topic URL of your resources
234243

235244
### Data Provider
236245

237-
Based on React Admin `create`, `delete`, `getList`, `getManyReference`, `getOne`, `update` methods, the `dataProvider` is used by API Platform Admin to communicate with the API. In addition, the specific `introspect` method parses your API documentation.
246+
Based on React Admin `create`, `delete`, `getList`, `getManyReference`, `getOne`, `update` methods, the `dataProvider` is used by API Platform Admin to communicate with the API.
247+
In addition, the specific `introspect` method parses your API documentation.
238248
Note that the `dataProvider` can be overridden to fit your API needs.
239249

240250
### Schema Analyzer
@@ -245,13 +255,15 @@ Analyses your resources and retrieves their types according to the [Schema.org](
245255

246256
### Pagination
247257

248-
Set by default in the [<ListGuesser> component](components.md#listguesser), the `Pagination` component uses React Admin [<Pagination> component](https://marmelab.com/react-admin/List.html#pagination).
249-
By default, it renders 30 items per page and displays a navigation UI. If you want to change the number of items per page or disable the pagination, see the [Pagination documentation](../core/pagination.md).
258+
Set by default in the [ListGuesser component](components.md#listguesser), the `Pagination` component uses React Admin [Pagination component](https://marmelab.com/react-admin/List.html#pagination).
259+
By default, it renders 30 items per page and displays a navigation UI.
260+
If you want to change the number of items per page or disable the pagination, see the [Pagination documentation](../core/pagination.md).
250261
It is also capable to handle partial pagination.
251262

252263
### FieldGuesser
253264

254-
Renders fields according to their types, using the [schema analyzer](components.md#schemaanalyzer). Based on React Admin [field components](https://marmelab.com/react-admin/Fields.html).
265+
Renders fields according to their types, using the [schema analyzer](components.md#schemaanalyzer).
266+
Based on React Admin [field components](https://marmelab.com/react-admin/Fields.html).
255267

256268
```javascript
257269
// BooksShow.js

admin/customizing.md

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,18 @@ To do so, you can use the React components provided by API Platform Admin itself
66

77
## Customizing the Admin's Main Page and the Resource List
88

9-
By default, API Platform Admin automatically builds a tailored [`<Resource>` component](https://marmelab.com/react-admin/Resource.html) (and all its appropriate children) for each resource type exposed by a web API.
9+
By default, API Platform Admin automatically builds a tailored [Resource component](https://marmelab.com/react-admin/Resource.html) (and all its appropriate children) for each resource type exposed by a web API.
1010
Under the hood it uses the `@api-platform/api-doc-parser` library to parse the API documentation. The API documentation can use Hydra, OpenAPI and any other format supported by the library.
1111
Resources are listed in the order they appear in the machine-readable documentation.
1212

1313
However, it's also possible to display only specific resources, and to order them, while still benefiting from all discovery features provided by API Platform Admin.
1414
To cherry-pick the resources to make available through the admin, pass a list of `<ResourceGuesser>` components as children of the root component:
1515

1616
```javascript
17-
import React from "react";
1817
import { HydraAdmin, ResourceGuesser } from "@api-platform/admin";
1918

2019
export default () => (
21-
<HydraAdmin entrypoint="https://demo.api-platform.com">
20+
<HydraAdmin entrypoint="https://demo.api-platform.com" mercure={true}>
2221
<ResourceGuesser name="books" />
2322
<ResourceGuesser name="reviews" />
2423

@@ -28,14 +27,13 @@ export default () => (
2827
);
2928
```
3029

31-
Instead of using the `<ResourceGuesser>` component provided by API Platform Admin, you can also pass custom React Admin's [`<Resource>` components](https://marmelab.com/react-admin/Resource.html), or any other React components that are supported by React Admin's [`<Admin>`](https://marmelab.com/react-admin/Admin.html).
30+
Instead of using the `<ResourceGuesser>` component provided by API Platform Admin, you can also pass custom React Admin's [Resource components](https://marmelab.com/react-admin/Resource.html), or any other React components that are supported by React Admin's [Admin](https://marmelab.com/react-admin/Admin.html).
3231

3332
## Customizing the List View
3433

3534
The list view can be customized following the same pattern:
3635

3736
```javascript
38-
import React from "react";
3937
import {
4038
HydraAdmin,
4139
ResourceGuesser,
@@ -54,7 +52,7 @@ const ReviewsList = props => (
5452
);
5553

5654
export default () => (
57-
<HydraAdmin entrypoint="https://demo.api-platform.com">
55+
<HydraAdmin entrypoint="https://demo.api-platform.com" mercure={true}>
5856
<ResourceGuesser name="reviews" list={ReviewsList} />
5957
{/* ... */}
6058
</HydraAdmin>
@@ -70,7 +68,6 @@ In addition to the `<FieldGuesser>` component, [all React Admin Fields component
7068
For the show view:
7169

7270
```javascript
73-
import React from "react";
7471
import {
7572
HydraAdmin,
7673
ResourceGuesser,
@@ -93,7 +90,7 @@ const ReviewsShow = props => (
9390
);
9491

9592
export default () => (
96-
<HydraAdmin entrypoint="https://demo.api-platform.com">
93+
<HydraAdmin entrypoint="https://demo.api-platform.com" mercure={true}>
9794
<ResourceGuesser name="reviews" show={ReviewsShow} />
9895
{/* ... */}
9996
</HydraAdmin>
@@ -107,7 +104,6 @@ In addition to the `<FieldGuesser>` component, [all React Admin Fields component
107104
Again, the same logic applies to forms. Here is how to customize the create form:
108105

109106
```javascript
110-
import React from "react";
111107
import {
112108
HydraAdmin,
113109
ResourceGuesser,
@@ -130,7 +126,7 @@ const ReviewsCreate = props => (
130126
);
131127

132128
export default () => (
133-
<HydraAdmin entrypoint="https://demo.api-platform.com">
129+
<HydraAdmin entrypoint="https://demo.api-platform.com" mercure={true}>
134130
<ResourceGuesser name="reviews" create={ReviewsCreate} />
135131
{/* ... */}
136132
</HydraAdmin>
@@ -146,7 +142,6 @@ For instance, using an autocomplete input is straightforward, [check out the ded
146142
Finally, you can customize the edit form the same way:
147143

148144
```javascript
149-
import React from "react";
150145
import {
151146
HydraAdmin,
152147
ResourceGuesser,
@@ -169,7 +164,7 @@ const ReviewsEdit = props => (
169164
);
170165

171166
export default () => (
172-
<HydraAdmin entrypoint="https://demo.api-platform.com">
167+
<HydraAdmin entrypoint="https://demo.api-platform.com" mercure={true}>
173168
<ResourceGuesser edit={ReviewsEdit}/>
174169
{/* ... */}
175170
</HydraAdmin>
@@ -183,6 +178,6 @@ For instance, using an autocomplete input is straightforward, [checkout the dedi
183178
## Going Further
184179

185180
API Platform is built on top of [React Admin](https://marmelab.com/react-admin/).
186-
You can use all the features provided by the underlying library with API Platform Admin, including support for [file upload](https://marmelab.com/react-admin/DataProviders.html#decorating-your-data-provider-example-of-file-upload), [authentication](https://marmelab.com/react-admin/Authentication.html), [authorization](https://marmelab.com/react-admin/Authorization.html) and deeper customization.
181+
You can use all the features provided by the underlying library with API Platform Admin, including support for [authentication](https://marmelab.com/react-admin/Authentication.html), [authorization](https://marmelab.com/react-admin/Authorization.html) and deeper customization.
187182

188183
To learn more about these capabilities, refer to [the React Admin documentation](https://marmelab.com/react-admin/).

admin/file-upload.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const MediaObjectsCreate = props => (
2525
);
2626

2727
export default () => (
28-
<HydraAdmin entrypoint="https://demo.api-platform.com">
28+
<HydraAdmin entrypoint="https://demo.api-platform.com" mercure={true}>
2929
<ResourceGuesser name="media_objects" create={MediaObjectsCreate} />
3030
{/* ... */}
3131
</HydraAdmin>

admin/getting-started.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,12 @@ To initialize API Platform Admin, register it in your application.
3030
For instance, if you used Create React App, replace the content of `src/App.js` by:
3131

3232
```javascript
33-
import React from "react";
3433
import { HydraAdmin } from "@api-platform/admin";
3534

3635
// Replace with your own API entrypoint
3736
// For instance if https://example.com/api/books is the path to the collection of book resources, then the entrypoint is https://example.com/api
3837
export default () => (
39-
<HydraAdmin entrypoint="https://demo.api-platform.com" />
38+
<HydraAdmin entrypoint="https://demo.api-platform.com" mercure={true} />
4039
);
4140
```
4241

0 commit comments

Comments
 (0)