Skip to content

Commit 28221bf

Browse files
committed
API Platform CRUD Generator documentation
1 parent 46e3ca8 commit 28221bf

File tree

5 files changed

+146
-0
lines changed

5 files changed

+146
-0
lines changed

deployment/docker.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ To install it, run the following commands (Docker must be installed on your syst
1212
Your project will be accessible at the URL `http://127.0.0.1`.
1313

1414
Previous chapter: [Deploying an API Platform App on Heroku](heroku.md)
15+
16+
Next chapter: [API Platform CRUD Generator: Introduction](../generate-crud/index.md)

generate-crud/index.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# The API Platform CRUD Generator
2+
3+
API Platform Crud Generator is a generator to scaffold a React/Redux app with Create-Retrieve-Update-Delete features for any API exposing a Hydra documentation.
4+
Works especially well with APIs built with the [API Platform](https://api-platform.com) framework.
5+
6+
## Features
7+
8+
* 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:
9+
* A list view
10+
* A creation form
11+
* An edition form
12+
* A deletion button
13+
* Use the Hydra API documentation to generate the code
14+
* Generate the suitable HTML5 input type (`number`, `date`...) according to the type of the API property
15+
* Display of the server-side validation errors under the related input (if using API Platform Core)
16+
* Client-side validation (`required` attributes)
17+
* The generated HTML is compatible with [Bootstrap](https://getbootstrap.com/) and include mandatory classes
18+
* The generated HTML code is accessible to people with disabilities ([ARIA](https://www.w3.org/WAI/intro/aria) support)
19+
* The Redux and the React Router configuration is also generated
20+
21+
Previous chapter: [Using API Platform with Docker](../deployment/docker.md)
22+
23+
Next chapter: [Installation and Usage](installation-and-usage.md)
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Installation and Usage
2+
3+
Create a React application using [Facebook's Create React App](https://github.com/facebookincubator/create-react-app):
4+
5+
$ create-react-app my-app
6+
$ cd my-app
7+
8+
Install React Router, Redux, React Redux, React Router Redux, Redux Form and Redux Thunk (to handle AJAX requests):
9+
10+
$ yarn add redux react-redux redux-thunk redux-form react-router-dom react-router-redux prop-types
11+
12+
Install the generator globally:
13+
14+
$ yarn global add api-platform-generate-crud
15+
16+
Reference the Bootstrap CSS stylesheet in `public/index.html` (optional):
17+
18+
```html
19+
<!-- ... -->
20+
<title>React App</title>
21+
22+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
23+
</head>
24+
<!-- ... -->
25+
```
26+
27+
In the app directory, generate the files for the resource you want:
28+
29+
$ api-platform-generate-crud https://demo.api-platform.com src/ --resource foo
30+
# Replace the URL by the entrypoint of your Hydra-enabled API
31+
# Omit the resource flag to generate files for all resource types exposed by the API
32+
33+
The code is ready to be executed! Register the generated reducers and components in the `index.js` file, here is an example:
34+
35+
```javascript
36+
import React from 'react';
37+
import ReactDom from 'react-dom';
38+
import { createStore, combineReducers, applyMiddleware } from 'redux';
39+
import { Provider } from 'react-redux';
40+
import thunk from 'redux-thunk';
41+
import { reducer as form } from 'redux-form';
42+
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
43+
import createBrowserHistory from 'history/createBrowserHistory';
44+
import { syncHistoryWithStore, routerReducer as routing } from 'react-router-redux'
45+
46+
// Replace "foo" by the name of the resource type
47+
import foo from './reducers/foo/';
48+
import fooRoutes from './routes/foo';
49+
50+
const store = createStore(
51+
combineReducers({routing, form, foo}), // Don't forget to register the reducers here
52+
applyMiddleware(thunk),
53+
);
54+
55+
const history = syncHistoryWithStore(createBrowserHistory(), store);
56+
57+
ReactDom.render(
58+
<Provider store={store}>
59+
<Router history={history}>
60+
<Switch>
61+
{fooRoutes}
62+
<Route render={() => <h1>Not Found</h1>}/>
63+
</Switch>
64+
</Router>
65+
</Provider>,
66+
document.getElementById('root')
67+
);
68+
```
69+
70+
Previous chapter: [Introduction](index.md)
71+
72+
Next chapter: [Troubleshooting](troubleshooting.md)

generate-crud/troubleshooting.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Troubleshooting
2+
3+
* The generator does not perform any authentication, so you must ensure that all referenced hydra paths for your API are
4+
accessible anonymously. If you are using API Platform this will at least include:
5+
6+
```
7+
api_entrypoint ANY ANY ANY /{index}.{_format}
8+
api_doc ANY ANY ANY /docs.{_format}
9+
api_jsonld_context ANY ANY ANY /contexts/{shortName}.{_format}
10+
```
11+
12+
* If you receive `Error: The class http://www.w3.org/ns/hydra/core#ApiDocumentation doesn't exist.` you may have
13+
specified the documentation URL instead of the entrypoint. For example if you are using API Platform and your
14+
documentation URL is at [https://demo.api-platform.com/docs](https://demo.api-platform.com/docs) the entry point is
15+
likely at [https://demo.api-platform.com](https://demo.api-platform.com). You can see an example of the expected
16+
response from an entrypoint in your browser by clicking visiting
17+
[https://demo.api-platform.com/index.jsonld](https://demo.api-platform.com/index.jsonld).
18+
19+
* If you receive `TypeError: Cannot read property '@type' of undefined` or `TypeError: Cannot read property '0'
20+
of undefined` check that the URL you specified is accessible and returns jsonld. You can check from the command line
21+
you are using by running something like `curl https://demo.api-platform.com/`.
22+
23+
* If you receive a message like this:
24+
25+
```
26+
{ Error
27+
at done (/usr/local/share/.config/yarn/global/node_modules/jsonld/js/jsonld.js:6851:19)
28+
at <anonymous>
29+
at process._tickCallback (internal/process/next_tick.js:188:7)
30+
name: 'jsonld.InvalidUrl',
31+
message: 'Dereferencing a URL did not result in a JSON object. The response was valid JSON, but it was not a JSON object.',
32+
details:
33+
{ code: 'invalid remote context',
34+
url: 'https://demo.api-platform.com/contexts/Entrypoint',
35+
cause: null } }
36+
```
37+
38+
Check access to the specified url, in this case `https://demo.api-platform.com/contexts/Entrypoint`, use curl to check
39+
access and the response `curl https://demo.api-platform.com/contexts/Entrypoint`. In the above case an "Access Denied"
40+
message in JSON format was being returned.
41+
42+
Previous chapter: [Installation and Usage](installation-and-usage.md)

index.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@
103103
2. [Deploying an API Platform App on Heroku](deployment/heroku.md)
104104
3. [Using API Platform with Docker](deployment/docker.md)
105105

106+
## CRUD Generator: Scaffold a React/Redux App with CRUD Features
107+
108+
1. [Introduction](generate-crud/index.md)
109+
1. [Features](generate-crud/index.md#features)
110+
2. [Installation and Usage](generate-crud/installation-and-usage.md)
111+
3. [Troubleshooting](generate-crud/troubleshooting.md)
112+
106113
## Extra
107114

108115
1. [The project's philosophy](philosophy.md)

0 commit comments

Comments
 (0)