From a50ee4a792e170edafe7ac39edc4a91f312ca1f2 Mon Sep 17 00:00:00 2001 From: Junior Oliveira Date: Sun, 13 May 2018 14:36:03 -0300 Subject: [PATCH 1/6] add option to use swagger documentation as resource type --- src/index.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 02c36213..c782c29f 100755 --- a/src/index.js +++ b/src/index.js @@ -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'; @@ -14,6 +15,7 @@ program .option('-p, --hydra-prefix [hydraPrefix]', 'The hydra prefix used by the API', 'hydra:') .option('-g, --generator [generator]', 'The generator to use, one of "react", "react-native", "vue", "admin-on-rest"', 'react') .option('-t, --template-directory [templateDirectory]', 'The templates directory base to use. Final directory will be ${templateDirectory}/${generator}', `${__dirname}/../templates/`) + .option('-s, --resource-type [resourceType]', 'Hydra or Swagger', 'hydra') .parse(process.argv); if (2 !== program.args.length && (!process.env.API_PLATFORM_CLIENT_GENERATOR_ENTRYPOINT || !process.env.API_PLATFORM_CLIENT_GENERATOR_OUTPUT)) { @@ -29,7 +31,14 @@ const generator = generators(program.generator)({ }); const resourceToGenerate = program.resource ? program.resource.toLowerCase() : null; -parseHydraDocumentation(entrypoint).then(ret => { +const parser = entrypoint => { + if (program.resourceType && program.resourceType === 'swagger') { + return parseSwaggerDocumentation(entrypoint) + } + return parseHydraDocumentation(entrypoint) +} + +parser(entrypoint).then(ret => { for (let resource of ret.api.resources) { const nameLc = resource.name.toLowerCase(); const titleLc = resource.title.toLowerCase(); From 6fab443f65016738eb638673a03ab107dd26a8bd Mon Sep 17 00:00:00 2001 From: Junior Oliveira Date: Sun, 10 Jun 2018 20:16:26 -0300 Subject: [PATCH 2/6] adjust commands to swagger parser following reviewer suggestions --- src/index.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/index.js b/src/index.js index c782c29f..6353309a 100755 --- a/src/index.js +++ b/src/index.js @@ -15,7 +15,7 @@ program .option('-p, --hydra-prefix [hydraPrefix]', 'The hydra prefix used by the API', 'hydra:') .option('-g, --generator [generator]', 'The generator to use, one of "react", "react-native", "vue", "admin-on-rest"', 'react') .option('-t, --template-directory [templateDirectory]', 'The templates directory base to use. Final directory will be ${templateDirectory}/${generator}', `${__dirname}/../templates/`) - .option('-s, --resource-type [resourceType]', 'Hydra or Swagger', 'hydra') + .option('-f, --format [hydra|swagger]', '"hydra" or "swagger', 'hydra') .parse(process.argv); if (2 !== program.args.length && (!process.env.API_PLATFORM_CLIENT_GENERATOR_ENTRYPOINT || !process.env.API_PLATFORM_CLIENT_GENERATOR_OUTPUT)) { @@ -32,10 +32,9 @@ const generator = generators(program.generator)({ const resourceToGenerate = program.resource ? program.resource.toLowerCase() : null; const parser = entrypoint => { - if (program.resourceType && program.resourceType === 'swagger') { - return parseSwaggerDocumentation(entrypoint) - } - return parseHydraDocumentation(entrypoint) + const parseDocumentation = 'swagger' === program.format ? parseSwaggerDocumentation : parseHydraDocumentation; + + return parseDocumentation(entrypoint) } parser(entrypoint).then(ret => { From 461b5340e8abe48ba7bc490356cb3a594159a086 Mon Sep 17 00:00:00 2001 From: Junior Oliveira Date: Sun, 24 Jun 2018 13:34:16 -0300 Subject: [PATCH 3/6] eslint fixes --- src/index.js | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/index.js b/src/index.js index 7fd3fae3..d7d11730 100755 --- a/src/index.js +++ b/src/index.js @@ -32,11 +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" - ) + .option("-f, --format [hydra|swagger]", '"hydra" or "swagger', "hydra") .parse(process.argv); if ( @@ -61,12 +57,13 @@ const resourceToGenerate = program.resource : null; const parser = entrypoint => { - const parseDocumentation = 'swagger' === program.format - ? parseSwaggerDocumentation - : parseHydraDocumentation; + const parseDocumentation = + "swagger" === program.format + ? parseSwaggerDocumentation + : parseHydraDocumentation; - return parseDocumentation(entrypoint) -} + return parseDocumentation(entrypoint); +}; parser(entrypoint) .then(ret => { From 15d8bda7928c8cf47d7c86b486fac4b1976865cc Mon Sep 17 00:00:00 2001 From: Junior Oliveira Date: Mon, 9 Jul 2018 09:18:11 -0300 Subject: [PATCH 4/6] update @api-platform/api-doc-parser to new version with swagger parser --- package.json | 2 +- yarn.lock | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index b2e90315..102955b2 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/yarn.lock b/yarn.lock index b524fb23..c67af31c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" From 2b70362b5857172e74a65337e73c191a0fe2793d Mon Sep 17 00:00:00 2001 From: Junior Oliveira Date: Wed, 11 Jul 2018 20:50:09 -0300 Subject: [PATCH 5/6] add gen test for swagger parser --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 102955b2..35c7864c 100644 --- a/package.json +++ b/package.json @@ -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": { From a071627abf71fdeb7c2d088586044102a6485987 Mon Sep 17 00:00:00 2001 From: Junior Oliveira Date: Wed, 11 Jul 2018 20:52:14 -0300 Subject: [PATCH 6/6] readme update with swagger support --- README.md | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 5dc0a8a3..50d67390 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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)