Skip to content

Commit 5bdb7b3

Browse files
committed
Client Generator: Mercure support and various improvements
1 parent da18285 commit 5bdb7b3

File tree

14 files changed

+150
-81
lines changed

14 files changed

+150
-81
lines changed

client-generator/images/demo.gif

974 KB
Loading
320 KB
Loading
255 KB
Loading
Loading
524 KB
Loading
263 KB
Loading

client-generator/index.md

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,32 @@
11
# The API Platform Client Generator
22

3-
API Platform Client Generator is a generator for scaffolding apps with Create-Retrieve-Update-Delete features for any API exposing a Hydra documentation. Currently the following targets are available:
3+
Client Generator is the fastest way to scaffold fully-featured webapps and native mobile apps from APIs supporting the [Hydra](http://www.hydra-cg.com/) format.
44

5-
* React/Redux
6-
* React Native
7-
* Vue.js
5+
![Screencast](images/demo.gif)
86

9-
The generator works especially well with APIs built with the [API Platform](https://api-platform.com) framework.
7+
*Generated React and React Native apps communicating updated in real time*
8+
9+
It is able to generate apps using the following frontend stacks:
10+
11+
* [React with Redux](react.md)
12+
* [React Native](react-native.md)
13+
* [Vue.js](vuejs.md)
14+
15+
Client Generator works especially well with APIs built with the [API Platform](https://api-platform.com) framework.
1016

1117
## Features
1218

13-
* Generate high-quality ES6 components and files built with [React](https://facebook.github.io/react/), [Redux](http://redux.js.org), [React Router](https://reacttraining.com/react-router/) and [Redux Form](http://redux-form.com/) including:
14-
* A list view
15-
* A creation form
16-
* An editing form
17-
* A delete button
18-
* Use the Hydra API documentation to generate the code
19-
* Generate the suitable HTML5 input type (`number`, `date`...) according to the type of the API property
20-
* Display of the server-side validation errors under the related input (if using API Platform Core)
21-
* Client-side validation (`required` attributes)
22-
* The generated HTML is compatible with [Bootstrap](https://getbootstrap.com/) and includes mandatory classes
23-
* The generated HTML code is accessible to people with disabilities ([ARIA](https://www.w3.org/WAI/intro/aria) support)
24-
* The Redux and the React Router configuration is also generated
19+
* Generate high-quality ES6:
20+
* list view (with pagination)
21+
* detail view
22+
* creation form
23+
* edition form
24+
* delete button
25+
* Support to-one and to-many relations
26+
* Use the appropriate input type (`number`, `date`...)
27+
* Client-side validation
28+
* Subscribe to data updates pushed by server supporting [the Mercure protocol](https://mercure.rocks)
29+
* Display server-side validation errors under the related input (if using API Platform Core)
30+
* Integration with [Bootstrap](https://getbootstrap.com/) and [FontAwesome](https://fontawesome.com/) (Progressive Web Apps)
31+
* Integration with [React Native Elements](https://react-native-training.github.io/react-native-elements/)
32+
* Accessible to people with disabilities ([ARIA](https://www.w3.org/WAI/intro/aria) support in webapps)

client-generator/react-native.md

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,44 @@
1-
React Native generator
2-
======================
1+
# React Native generator
32

4-
Create a React Native application using [React Community's Create React Native App](https://github.com/react-community/create-react-native-app)
3+
![List](images/list.png)
4+
5+
## Install
6+
7+
To use this generator you need a proper [Node.js](https://nodejs.org/) and [Yarn](https://yarnpkg.com/) (or NPM) installation.
8+
To run the command line tool, we also recommend using [npx](https://www.npmjs.com/package/npx).
9+
10+
Create a React Native application using [Expo CLI](https://docs.expo.io/versions/latest/workflow/expo-cli).
511

612
```bash
7-
$ yarn add -g expo-cli
13+
$ yarn global add expo-cli
814
$ expo init my-app
15+
# When asked, choose to use the blank template
916
$ cd my-app
1017
```
1118

12-
Install: React Native Elements, React Native Router Flux, React Native Vector Icons, Redux, React Redux, Redux Thunk, Redux Form, Prop Types
19+
Install the requires dependencies:
1320

1421
```bash
15-
$ yarn add redux react-redux redux-thunk redux-form react-native-elements react-native-router-flux
16-
react-native-vector-icons prop-types
22+
$ yarn add redux react-redux redux-thunk redux-form react-native-elements react-native-router-flux react-native-vector-icons prop-types whatwg-url buffer react-native-event-source
1723
```
1824

19-
Install the generator globally:
25+
## Generating a Native App
26+
27+
In the app directory, generate the files for the resource you want:
2028

2129
```bash
22-
$ yarn global add @api-platform/client-generator
30+
$ npx @api-platform/client-generator https://demo.api-platform.com . -g react-native --resource book
2331
```
2432

25-
In the app directory, generate the files for the resource you want:
33+
Replace the URL by the entrypoint of your Hydra-enabled API.
34+
Omit the resource flag to generate files for all resource types exposed by the API.
2635

27-
```
28-
$ generate-api-platform-client https://demo.api-platform.com -g react-native --resource foo
29-
# Replace the URL by the entrypoint of your Hydra-enabled API
30-
# Omit the resource flag to generate files for all resource types exposed by the API
31-
```
32-
Create **Router.js** file to import all routes
36+
Create a `Router.js` file to import all routes:
3337

3438
```javascript
3539
import React from 'react';
3640
import { Router, Stack } from 'react-native-router-flux';
37-
//replace "book" with the name of resource type
41+
// Replace "book" with the name of the resource type
3842
import BookRoutes from './routes/book';
3943

4044
const RouterComponent = () => {
@@ -49,16 +53,31 @@ const RouterComponent = () => {
4953

5054
export default RouterComponent;
5155
```
52-
Here is an example of **App.js**
56+
57+
Here is an example of a `App.js` file:
5358

5459
```javascript
5560
import React, { Component } from 'react';
5661
import { Provider } from 'react-redux';
5762
import thunk from 'redux-thunk';
58-
import { createStore, applyMiddleware, combineReducers} from 'redux';
63+
import { createStore, applyMiddleware, combineReducers } from 'redux';
5964
import { View } from 'react-native';
6065
import {reducer as form} from 'redux-form';
61-
//replace "book" with the name of resource type
66+
67+
// see https://github.com/facebook/react-native/issues/14796
68+
import { Buffer } from 'buffer';
69+
global.Buffer = Buffer;
70+
71+
// see https://github.com/facebook/react-native/issues/16434
72+
import { URL, URLSearchParams } from 'whatwg-url';
73+
global.URL = URL;
74+
global.URLSearchParams = URLSearchParams;
75+
76+
// see https://github.com/facebook/react-native/issues/12890
77+
import RNEventSource from 'react-native-event-source';
78+
global.EventSource = RNEventSource;
79+
80+
// Replace "book" with the name of resource type
6281
import book from './reducers/book';
6382
import Router from './Router';
6483

@@ -80,11 +99,12 @@ export default class App extends Component {
8099
```
81100

82101
The code is ready to be executed!
102+
83103
```bash
84104
$ expo start
85105
```
86106

87-
#### Example of running application on IOS simulator
107+
## Screenshots in iOS Simulator
88108

89-
![List](images/list.png) ![Show](images/item.png)
90-
![Add](images/adnew.png) ![Delete](images/del.png)
109+
![List](images/react-native/list.png) ![Show](images/react-native/show.png)
110+
![Add](images/react-native/add.png) ![Delete](images/react-native/delete.png)

client-generator/react.md

Lines changed: 81 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,113 @@
1-
# React generator
1+
# React Generator
22

3-
Create a React application using [Facebook's Create React App](https://github.com/facebookincubator/create-react-app):
3+
![List screenshot](images/react/list.png)
44

5-
$ create-react-app my-app
6-
$ cd my-app
5+
The React Client Generator generate a Progressive Web App built with battle-tested libraries from the ecosystem:
76

8-
Install React Router, Redux, React Redux, React Router Redux, Redux Form and Redux Thunk (to handle AJAX requests):
7+
* [React](https://facebook.github.io/react/)
8+
* [Redux](http://redux.js.org)
9+
* [React Router](https://reacttraining.com/react-router/)
10+
* [Redux Form](http://redux-form.com/)
911

10-
$ yarn add redux react-redux redux-thunk redux-form react-router-dom react-router-redux prop-types
12+
It is designed to generate code that works seamlessly with [Facebook's Create React App](https://github.com/facebook/create-react-app).
1113

12-
Install the generator globally:
14+
## Install
1315

14-
$ yarn global add @api-platform/client-generator
16+
The easiest way to get started is to install [the API Platform distribution](../distribution/index.md).
17+
It contains the React Client Generator, all dependencies it needs, a Progressive Web App skeleton generated with Create React App,
18+
a development Docker container to serve the webapp, and all the API Platform components you may need, including an API server
19+
supporting Hydra.
1520

16-
Reference the Bootstrap CSS stylesheet in `public/index.html` (optional):
21+
If you use the API Platform, jump to the next section!
22+
Alternatively, you can generate a skeleton and install the generator using [npx](https://www.npmjs.com/package/npx).
23+
To run it, you need a proper [Node.js](https://nodejs.org/) and [Yarn](https://yarnpkg.com/) (or NPM) installation.
1724

18-
Bootstrap 3 - last release 0.1.15
19-
```html
20-
<!-- ... -->
21-
<title>React App</title>
25+
Bootstrap a React application:
2226

23-
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
24-
</head>
25-
<!-- ... -->
27+
```bash
28+
$ npx create-react-app client
29+
$ cd client
30+
```
31+
32+
Install the required dependencies:
33+
34+
```bash
35+
$ yarn add redux react-redux redux-thunk redux-form react-router-dom connected-react-router prop-types lodash
36+
```
37+
38+
Optionally, install Bootstrap and Font Awesome to get an app that looks good:
39+
40+
```bash
41+
$ yarn add redux bootstrap font-awesome
42+
```
43+
44+
Finally, start the integrated web server:
45+
46+
```bash
47+
$ yarn start
2648
```
27-
Bootstrap 4 - from release 0.1.16
2849

29-
```html
30-
<!-- ... -->
31-
<title>React App</title>
50+
## Generating a Progressive Web App
51+
52+
If you use the API Platform distribution, generating all the code you need for a given resource is as simple as running the following command:
3253

33-
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
34-
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
35-
</head>
36-
<!-- ... -->
54+
```bash
55+
$ docker-compose exec client generate-api-platform-client --resource book
3756
```
3857

39-
In the app directory, generate the files for the resource you want:
58+
Omit the resource flag to generate files for all resource types exposed by the API.
59+
60+
If you don't use a standalone installation, run the following command instead:
4061

4162
```bash
42-
$ generate-api-platform-client https://demo.api-platform.com src/ --resource book
63+
$ npx @api-platform/client-generator https://demo.api-platform.com src/ --resource book
4364
# Replace the URL by the entrypoint of your Hydra-enabled API
44-
# Omit the resource flag to generate files for all resource types exposed by the API
4565
```
4666

47-
The code is ready to be executed! Register the generated reducers and components in the `index.js` file, here is an example:
67+
The code has been generated, and is ready to be executed!
68+
Register the reducers and the routes in the `client/src/index.js` file:
4869

4970
```javascript
5071
import React from 'react';
5172
import ReactDOM from 'react-dom';
52-
import * as serviceWorker from './serviceWorker';
53-
5473
import { createStore, combineReducers, applyMiddleware } from 'redux';
5574
import { Provider } from 'react-redux';
5675
import thunk from 'redux-thunk';
5776
import { reducer as form } from 'redux-form';
58-
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
77+
import { Route, Switch } from 'react-router-dom';
5978
import createBrowserHistory from 'history/createBrowserHistory';
60-
import { syncHistoryWithStore, routerReducer as routing } from 'react-router-redux'
61-
79+
import {
80+
ConnectedRouter,
81+
connectRouter,
82+
routerMiddleware
83+
} from 'connected-react-router';
84+
import 'bootstrap/dist/css/bootstrap.css';
85+
import 'font-awesome/css/font-awesome.css';
86+
import * as serviceWorker from './serviceWorker';
6287
// Replace "book" with the name of the resource type
6388
import book from './reducers/book/';
6489
import bookRoutes from './routes/book';
6590

91+
const history = createBrowserHistory();
6692
const store = createStore(
67-
combineReducers({routing, form, book}), // Don't forget to register the reducers here
68-
applyMiddleware(thunk),
93+
combineReducers({
94+
router: connectRouter(history),
95+
form,
96+
book
97+
/* Replace book with the name of the resource type */
98+
}),
99+
applyMiddleware(routerMiddleware(history), thunk)
69100
);
70101

71-
const history = syncHistoryWithStore(createBrowserHistory(), store);
72-
73102
ReactDOM.render(
74103
<Provider store={store}>
75-
<Router history={history}>
104+
<ConnectedRouter history={history}>
76105
<Switch>
77106
{bookRoutes}
78-
<Route render={() => <h1>Not Found</h1>}/>
107+
{/* Replace bookRooutes with the name of the resource type */}
108+
<Route render={() => <h1>Not Found</h1>} />
79109
</Switch>
80-
</Router>
110+
</ConnectedRouter>
81111
</Provider>,
82112
document.getElementById('root')
83113
);
@@ -87,3 +117,14 @@ ReactDOM.render(
87117
// Learn more about service workers: http://bit.ly/CRA-PWA
88118
serviceWorker.unregister();
89119
```
120+
121+
Go to `https://localhost/books/` to start using your app.
122+
That's all!
123+
124+
## Screenshots
125+
126+
![List](images/react/list.png)
127+
![Pagination](images/react/list-pagination.png)
128+
![Show](images/react/show.png)
129+
![Edit](images/react/edit.png)
130+
![Delete](images/react/delete.png)

client-generator/vuejs.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Vue.js generator
1+
# Vue.js Generator
22

33
Create a Vue.js application using [vue-cli](https://github.com/vuejs/vue-cli):
44

0 commit comments

Comments
 (0)