Skip to content

Commit 74c5b08

Browse files
arojuniordunglas
authored andcommitted
Add option to use swagger documentation as resource type (#76)
* add option to use swagger documentation as resource type * adjust commands to swagger parser following reviewer suggestions * eslint fixes * update @api-platform/api-doc-parser to new version with swagger parser * add gen test for swagger parser * readme update with swagger support
1 parent 5de3ff4 commit 74c5b08

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[![npm version](https://badge.fury.io/js/%40api-platform%2Fclient-generator.svg)](https://badge.fury.io/js/%40api-platform%2Fclient-generator)
55
[![MIT Licence](https://badges.frapsoft.com/os/mit/mit.svg?v=103)](https://opensource.org/licenses/mit-license.php)
66

7-
API Platform Client Generator is a generator to scaffold app with Create-Retrieve-Update-Delete features for any API exposing a Hydra documentation for:
7+
API Platform Client Generator is a generator to scaffold app with Create-Retrieve-Update-Delete features for any API exposing a Hydra or Swagger documentation for:
88
* React/Redux
99
* Vue.js
1010

@@ -16,7 +16,15 @@ Works especially well with APIs built with the [API Platform](https://api-platfo
1616

1717
## Usage
1818

19-
generate-api-platform-client https://demo.api-platform.com/ output/ --resource Book
19+
**Hydra**
20+
```sh
21+
generate-api-platform-client https://demo.api-platform.com/ output/ --resource Book
22+
```
23+
24+
**Swagger**
25+
```sh
26+
generate-api-platform-client https://demo.api-platform.com/ output/ --resource Book --format swagger
27+
```
2028

2129
## Features
2230

@@ -25,7 +33,7 @@ Works especially well with APIs built with the [API Platform](https://api-platfo
2533
* A creation form
2634
* An edition form
2735
* A deletion button
28-
* Use the Hydra API documentation to generate the code
36+
* Use the Hydra or Swagger API documentation to generate the code
2937
* Generate the suitable HTML5 input type (`number`, `date`...) according to the type of the API property
3038
* Display of the server-side validation errors under the related input (if using API Platform Core)
3139
* Client-side validation (`required` attributes)

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"tmp": "^0.0.31"
3434
},
3535
"dependencies": {
36-
"@api-platform/api-doc-parser": "^0.4",
36+
"@api-platform/api-doc-parser": "^0.5.0",
3737
"babel-runtime": "^6.23.0",
3838
"chalk": "^2.1.0",
3939
"commander": "^2.9.0",
@@ -51,6 +51,7 @@
5151
"build": "babel src -d lib --ignore '*.test.js'",
5252
"watch": "babel --watch src -d lib --ignore '*.test.js'",
5353
"test-gen": "rm -rf ./tmp && npm run build && ./lib/index.js https://demo.api-platform.com ./tmp/react && ./lib/index.js https://demo.api-platform.com ./tmp/react-native -g react-native && ./lib/index.js https://demo.api-platform.com ./tmp/vue -g vue && ./lib/index.js https://demo.api-platform.com ./tmp/admin-on-rest -g admin-on-rest",
54+
"test-gen-swagger": "rm -rf ./tmp && npm run build && ./lib/index.js https://demo.api-platform.com/docs.json ./tmp/react -f swagger && ./lib/index.js https://demo.api-platform.com/docs.json ./tmp/react-native -g react-native -f swagger && ./lib/index.js https://demo.api-platform.com/docs.json ./tmp/vue -g vue -f swagger && ./lib/index.js https://demo.api-platform.com/docs.json ./tmp/admin-on-rest -g admin-on-rest -f swagger",
5455
"test-gen-env": "rm -rf ./tmp && npm run build && API_PLATFORM_CLIENT_GENERATOR_ENTRYPOINT=https://demo.api-platform.com API_PLATFORM_CLIENT_GENERATOR_OUTPUT=./tmp ./lib/index.js"
5556
},
5657
"bin": {

src/index.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import "isomorphic-fetch";
44
import program from "commander";
55
import parseHydraDocumentation from "@api-platform/api-doc-parser/lib/hydra/parseHydraDocumentation";
6+
import parseSwaggerDocumentation from "@api-platform/api-doc-parser/lib/swagger/parseSwaggerDocumentation";
67
import { version } from "../package.json";
78
import generators from "./generators";
89

@@ -31,6 +32,7 @@ program
3132
"The templates directory base to use. Final directory will be ${templateDirectory}/${generator}",
3233
`${__dirname}/../templates/`
3334
)
35+
.option("-f, --format [hydra|swagger]", '"hydra" or "swagger', "hydra")
3436
.parse(process.argv);
3537

3638
if (
@@ -54,7 +56,16 @@ const resourceToGenerate = program.resource
5456
? program.resource.toLowerCase()
5557
: null;
5658

57-
parseHydraDocumentation(entrypoint)
59+
const parser = entrypoint => {
60+
const parseDocumentation =
61+
"swagger" === program.format
62+
? parseSwaggerDocumentation
63+
: parseHydraDocumentation;
64+
65+
return parseDocumentation(entrypoint);
66+
};
67+
68+
parser(entrypoint)
5869
.then(ret => {
5970
ret.api.resources
6071
.filter(({ deprecated }) => !deprecated)

yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
# yarn lockfile v1
33

44

5-
"@api-platform/api-doc-parser@^0.4":
6-
version "0.4.0"
7-
resolved "https://registry.yarnpkg.com/@api-platform/api-doc-parser/-/api-doc-parser-0.4.0.tgz#92f24a445aaa2a8fac78e3a2069a78414854e9bb"
5+
"@api-platform/api-doc-parser@^0.5.0":
6+
version "0.5.0"
7+
resolved "https://registry.yarnpkg.com/@api-platform/api-doc-parser/-/api-doc-parser-0.5.0.tgz#c8d2d860937d5c795577dec33f7914567f5ec9eb"
88
dependencies:
99
babel-runtime "^6.23.0"
1010
jsonld "^0.4.11"

0 commit comments

Comments
 (0)