Skip to content

Add option to use swagger documentation as resource type #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![npm version](https://badge.fury.io/js/%40api-platform%2Fclient-generator.svg)](https://badge.fury.io/js/%40api-platform%2Fclient-generator)
[![MIT Licence](https://badges.frapsoft.com/os/mit/mit.svg?v=103)](https://opensource.org/licenses/mit-license.php)

API Platform Client Generator is a generator to scaffold app with Create-Retrieve-Update-Delete features for any API exposing a Hydra documentation for:
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:
* React/Redux
* Vue.js

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

## Usage

generate-api-platform-client https://demo.api-platform.com/ output/ --resource Book
**Hydra**
```sh
generate-api-platform-client https://demo.api-platform.com/ output/ --resource Book
```

**Swagger**
```sh
generate-api-platform-client https://demo.api-platform.com/ output/ --resource Book --format swagger
```

## Features

Expand All @@ -25,7 +33,7 @@ Works especially well with APIs built with the [API Platform](https://api-platfo
* A creation form
* An edition form
* A deletion button
* Use the Hydra API documentation to generate the code
* Use the Hydra or Swagger API documentation to generate the code
* Generate the suitable HTML5 input type (`number`, `date`...) according to the type of the API property
* Display of the server-side validation errors under the related input (if using API Platform Core)
* Client-side validation (`required` attributes)
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"tmp": "^0.0.31"
},
"dependencies": {
"@api-platform/api-doc-parser": "^0.4",
"@api-platform/api-doc-parser": "^0.5.0",
"babel-runtime": "^6.23.0",
"chalk": "^2.1.0",
"commander": "^2.9.0",
Expand All @@ -51,6 +51,7 @@
"build": "babel src -d lib --ignore '*.test.js'",
"watch": "babel --watch src -d lib --ignore '*.test.js'",
"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",
"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",
"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"
},
"bin": {
Expand Down
13 changes: 12 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import "isomorphic-fetch";
import program from "commander";
import parseHydraDocumentation from "@api-platform/api-doc-parser/lib/hydra/parseHydraDocumentation";
import parseSwaggerDocumentation from "@api-platform/api-doc-parser/lib/swagger/parseSwaggerDocumentation";
import { version } from "../package.json";
import generators from "./generators";

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

if (
Expand All @@ -54,7 +56,16 @@ const resourceToGenerate = program.resource
? program.resource.toLowerCase()
: null;

parseHydraDocumentation(entrypoint)
const parser = entrypoint => {
const parseDocumentation =
"swagger" === program.format
? parseSwaggerDocumentation
: parseHydraDocumentation;

return parseDocumentation(entrypoint);
};

parser(entrypoint)
.then(ret => {
ret.api.resources
.filter(({ deprecated }) => !deprecated)
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
# yarn lockfile v1


"@api-platform/api-doc-parser@^0.4":
version "0.4.0"
resolved "https://registry.yarnpkg.com/@api-platform/api-doc-parser/-/api-doc-parser-0.4.0.tgz#92f24a445aaa2a8fac78e3a2069a78414854e9bb"
"@api-platform/api-doc-parser@^0.5.0":
version "0.5.0"
resolved "https://registry.yarnpkg.com/@api-platform/api-doc-parser/-/api-doc-parser-0.5.0.tgz#c8d2d860937d5c795577dec33f7914567f5ec9eb"
dependencies:
babel-runtime "^6.23.0"
jsonld "^0.4.11"
Expand Down