From a0a2dbc9cbff0c1d24b11e1dbc29aabb78f908c0 Mon Sep 17 00:00:00 2001 From: sumansaurabh Date: Sun, 9 Jun 2024 02:17:50 +0530 Subject: [PATCH 1/4] chore: Update penify-oapi-codegen package name, improve command-line argument handling, and generate sample code for all languages and variants in OpenAPIHelper --- README.md | 30 ++++++++++++++---------------- package.json | 1 + src/OpenAPIHelper.js | 22 +++++++++++++++++----- src/index.js | 7 +++---- 4 files changed, 35 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index beaa80e..cc48d94 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,6 @@ +# Penify Open API Code Gen -# Open API Code Gen - -Openapi-code-gen is a JavaScript library designed to convert OpenAPI schemas into various code examples. This tool simplifies the process of generating client libraries in different programming languages based on your API documentation. +`penify-oapi-codegen` is a JavaScript library designed to convert OpenAPI schemas into various code examples. This tool simplifies the process of generating client libraries in different programming languages based on your API documentation. ## Features @@ -10,7 +9,7 @@ Openapi-code-gen is a JavaScript library designed to convert OpenAPI schemas int ## Prerequisites -Before installing `openapi-code-gen`, ensure you have the following installed: +Before installing `penify-oapi-codegen`, ensure you have the following installed: - [Node.js](https://nodejs.org/) (version 12 or higher) - npm (Node package manager) @@ -23,29 +22,28 @@ npm install -g openapi-to-postmanv2 ## Installation -To install `openapi-code-gen`, run the following command: +To install `penify-oapi-codegen`, run the following command: ```bash -npm install openapi-code-gen +npm install penify-oapi-codegen ``` ## Usage -Once installed, you can use `openapi-code-gen` from the command line to generate code examples from your OpenAPI schemas. +Once installed, you can use `penify-oapi-codegen` from the command line to generate code examples from your OpenAPI schemas. ### Generate Code Examples ```bash -openapi-code-gen generate -i path/to/your/openapi/schema.yaml -l language -v variant -o path/to/output/code_example +penify-oapi-codegen -s path/to/your/openapi/schema.yaml -l language -v variant -o path/to/output/code_example ``` ### Options -- `generate`: Generates code examples from an OpenAPI schema. - - `-i, --input `: Path to the OpenAPI schema file (required). - - `-l, --language `: Programming language for the code example (required). - - `-v, --variant `: Variant of the code generator for the specified language (required). - - `-o, --output `: Path to the output code example file (required). +- `-s, --source `: Path to the OpenAPI schema file (required). +- `-l, --language `: Programming language for the code example (optional). +- `-v, --variant `: Variant of the code generator for the specified language (optional). +- `-o, --output `: Path to the output code example file (optional). ## Supported Code Generators @@ -92,13 +90,13 @@ The tool supports generating code examples for the following languages and varia ### Example 1: Generate Python Requests Code Example ```bash -openapi-code-gen generate -i ./schemas/api.yaml -l Python -v Requests -o ./examples/python_requests_example.py +penify-oapi-codegen -s ./schemas/api.yaml -l Python -v Requests -o ./examples/python_requests_example.py ``` ### Example 2: Generate JavaScript Fetch Code Example ```bash -openapi-code-gen generate -i ./schemas/api.yaml -l JavaScript -v Fetch -o ./examples/js_fetch_example.js +penify-oapi-codegen -s ./schemas/api.yaml -l JavaScript -v Fetch -o ./examples/js_fetch_example.js ``` ## Contributing @@ -111,7 +109,7 @@ This project is licensed under the ISC License. ## Author -[Your Name] +sumansaurabh --- diff --git a/package.json b/package.json index 0ae6841..2bfcbd0 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "eslint": "^9.4.0", "globals": "^15.4.0", "jest": "^29.7.0", + "js-yaml": "^4.1.0", "uuid": "^9.0.1" }, "peerDependencies": { diff --git a/src/OpenAPIHelper.js b/src/OpenAPIHelper.js index 79831f3..3bec56d 100644 --- a/src/OpenAPIHelper.js +++ b/src/OpenAPIHelper.js @@ -2,6 +2,7 @@ const fs = require('fs'); const { Collection, Item } = require('postman-collection'); const codegen = require('postman-code-generators'); const { execSync } = require('child_process'); +const yaml = require('js-yaml'); class OpenAPIHelper { static convertOpenAPIToPostman(openAPIPath, postmanOutputPath) { @@ -37,7 +38,7 @@ class OpenAPIHelper { console.error(error); } else { const fileName = `${operationId}_${language}_${variant}.pm`; - fs.writeFileSync(`sample_code/${fileName}`, snippet); + fs.writeFileSync(`/tmp/sample_code/${fileName}`, snippet); } }); }; @@ -48,7 +49,6 @@ class OpenAPIHelper { const method = item.request.method.toLowerCase(); // eg: "get" const operationId = `${name.join('_')}_${url.join('_')}_${method}`; return operationId; - } function processItems(items) { @@ -71,10 +71,18 @@ class OpenAPIHelper { } static addSampleCodeToOpenAPI(openAPIPath, outputAPIPath) { - const openapiSchema = JSON.parse(fs.readFileSync(openAPIPath).toString()); + let openapiSchema; + const ext = path.extname(openAPIPath); + + if (ext === '.yaml' || ext === '.yml') { + openapiSchema = yaml.load(fs.readFileSync(openAPIPath, 'utf8')); + } else { + openapiSchema = JSON.parse(fs.readFileSync(openAPIPath).toString()); + } + const sampleCode = {}; - fs.readdirSync('sample_code').forEach(file => { + fs.readdirSync('/tmp/sample_code').forEach(file => { const code = fs.readFileSync(`/tmp/sample_code/${file}`).toString(); const [operationId, lang, vari] = file.replace('.pm', '').split('~'); if (!sampleCode[operationId]) { @@ -98,7 +106,11 @@ class OpenAPIHelper { }); }); - fs.writeFileSync(outputAPIPath, JSON.stringify(openapiSchema, null, 2)); + if (ext === '.yaml' || ext === '.yml') { + fs.writeFileSync(outputAPIPath, yaml.dump(openapiSchema)); + } else { + fs.writeFileSync(outputAPIPath, JSON.stringify(openapiSchema, null, 2)); + } } } diff --git a/src/index.js b/src/index.js index e6ed380..e3f030b 100755 --- a/src/index.js +++ b/src/index.js @@ -11,7 +11,7 @@ const argv = yargs(hideBin(process.argv)) .usage('Usage: penify-oapi-codegen -s -l -v -o ') .option('s', { alias: 'source', - describe: 'Path to the OpenAPI JSON file', + describe: 'Path to the OpenAPI JSON or YAML file', type: 'string', demandOption: true }) @@ -36,15 +36,14 @@ const argv = yargs(hideBin(process.argv)) const openAPIPath = argv.s; const language = argv.l || null; const variant = argv.v || null; -const outputAPIPath = argv.o || path.resolve(`${path.basename(openAPIPath)}_with_code.json`); +const outputAPIPath = argv.o || path.resolve(`${path.basename(openAPIPath, path.extname(openAPIPath))}_with_code.json`); if (!openAPIPath) { - console.error('Please provide the path to the OpenAPI JSON file.'); + console.error('Please provide the path to the OpenAPI file.'); process.exit(1); } const resolvedOpenAPIPath = path.resolve(openAPIPath); -const file_name = path.basename(resolvedOpenAPIPath); const guid = uuidv4(); const postmanOutputPath = path.resolve(`/tmp/openapi_schema_postman_${guid}.json`); From c35bdff7b58a5e324ba9c8171a7b964bfc8220e6 Mon Sep 17 00:00:00 2001 From: sumansaurabh Date: Sun, 9 Jun 2024 02:20:14 +0530 Subject: [PATCH 2/4] chore: Update package name to penify-oapi-codegen in bin section of package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2bfcbd0..443b2dc 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "test": "jest" }, "bin": { - "openapi-code-gen": "src/index.js" + "penify-oapi-codegen": "src/index.js" }, "keywords": [], "author": "", From f173263f6dc94102536bef53d434747c288889de Mon Sep 17 00:00:00 2001 From: sumansaurabh Date: Sun, 9 Jun 2024 02:22:10 +0530 Subject: [PATCH 3/4] chore: Update postman-code-generators npm dependency to version 1.0.2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 443b2dc..5c9daf8 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "license": "ISC", "dependencies": { "openapi-to-postmanv2": "4.21.0", - "postman-code-generators": "1.10.1", + "postman-code-generators": "^1.0.2", "postman-collection": "^4.4.0" }, "devDependencies": { From b23ff6ee393fb76c28ecc57b8cd7cb8fd0b897e4 Mon Sep 17 00:00:00 2001 From: sumansaurabh Date: Sun, 9 Jun 2024 09:21:31 +0530 Subject: [PATCH 4/4] chore: Update penify-oapi-codegen package name, improve command-line argument handling, and generate sample code for all languages and variants in OpenAPIHelper --- README.md | 70 ++++++++++++++++++++------------------------ package.json | 2 +- src/OpenAPIHelper.js | 8 +++++ src/index.js | 29 ++++++++++++++---- 4 files changed, 64 insertions(+), 45 deletions(-) diff --git a/README.md b/README.md index cc48d94..47e03a8 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ Once installed, you can use `penify-oapi-codegen` from the command line to gener ### Generate Code Examples ```bash -penify-oapi-codegen -s path/to/your/openapi/schema.yaml -l language -v variant -o path/to/output/code_example +penify-oapi-codegen -i path/to/your/openapi/schema.json -l language -v variant -o path/to/output/schema_with_code.json ``` ### Options @@ -44,59 +44,53 @@ penify-oapi-codegen -s path/to/your/openapi/schema.yaml -l language -v variant - - `-l, --language `: Programming language for the code example (optional). - `-v, --variant `: Variant of the code generator for the specified language (optional). - `-o, --output `: Path to the output code example file (optional). +- `-s,`: To show list of supported languages ## Supported Code Generators The tool supports generating code examples for the following languages and variants: -| Language | Variant | -|---------------|---------------| -| C | libcurl | -| C# | HttpClient | -| C# | RestSharp | -| cURL | cURL | -| Dart | http | -| Go | Native | -| HTTP | HTTP | -| Java | OkHttp | -| Java | Unirest | -| JavaScript | Fetch | -| JavaScript | jQuery | -| JavaScript | XHR | -| Kotlin | OkHttp | -| NodeJs | Axios | -| NodeJs | Native | -| NodeJs | Request | -| NodeJs | Unirest | -| Objective-C | NSURLSession | -| OCaml | Cohttp | -| PHP | cURL | -| PHP | Guzzle | -| PHP | pecl_http | -| PHP | HTTP_Request2 | -| PowerShell | RestMethod | -| Python | http.client | -| Python | Requests | -| R | httr | -| R | RCurl | -| Rust | Reqwest | -| Ruby | Net:HTTP | -| Shell | Httpie | -| Shell | wget | -| Swift | URLSession | +| Language | Variant | +|--------------- |----------------| +| csharp | RestSharp | +| curl | cURL | +| go | Native | +| http | HTTP | +| java | OkHttp | +| java | Unirest | +| javascript | Fetch | +| javascript | jQuery | +| javascript | XHR | +| c | libcurl | +| nodejs | Axios | +| nodejs | Native | +| nodejs | Request | +| nodejs | Unirest | +| objective-c | NSURLSession | +| ocaml | Cohttp | +| php | cURL | +| php | HTTP_Request2 | +| php | pecl_http | +| powershell | RestMethod | +| python | http.client | +| python | Requests | +| ruby | Net::HTTP | +| shell | Httpie | +| shell | wget | +| swift | URLSession | ## Examples ### Example 1: Generate Python Requests Code Example ```bash -penify-oapi-codegen -s ./schemas/api.yaml -l Python -v Requests -o ./examples/python_requests_example.py +penify-oapi-codegen -s ./schemas/api.json -l python -v Requests -o ./examples/schema_python_requests_example.json ``` ### Example 2: Generate JavaScript Fetch Code Example ```bash -penify-oapi-codegen -s ./schemas/api.yaml -l JavaScript -v Fetch -o ./examples/js_fetch_example.js +penify-oapi-codegen -s ./schemas/api.json -l javascript -v Fetch -o ./examples/schema_js_fetch_example.json ``` ## Contributing diff --git a/package.json b/package.json index 5c9daf8..495f0fb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "penify-oapi-codegen", - "version": "1.0.0", + "version": "1.0.1", "description": "A JavaScript library to convert OpenAPI schema to Postman collection and generate sample code.", "main": "src/index.js", "scripts": { diff --git a/src/OpenAPIHelper.js b/src/OpenAPIHelper.js index 3bec56d..c29abcf 100644 --- a/src/OpenAPIHelper.js +++ b/src/OpenAPIHelper.js @@ -3,12 +3,20 @@ const { Collection, Item } = require('postman-collection'); const codegen = require('postman-code-generators'); const { execSync } = require('child_process'); const yaml = require('js-yaml'); +const path = require('path'); class OpenAPIHelper { static convertOpenAPIToPostman(openAPIPath, postmanOutputPath) { execSync(`openapi2postmanv2 -s ${openAPIPath} -o ${postmanOutputPath}`, { stdio: 'inherit' }); } + static getSupportedLanguagesAndVariants() { + const languages = codegen.getLanguageList(); + const supportedLanguages = []; + languages.forEach(lang => lang.variants.forEach(vari => supportedLanguages.push({ language: lang.key, variant: vari.key }))) ; + return supportedLanguages; + } + static generateSampleCode(postmanCollectionPath, language = null, variant = null) { const collection = JSON.parse(fs.readFileSync(postmanCollectionPath).toString()); diff --git a/src/index.js b/src/index.js index e3f030b..dd54ee1 100755 --- a/src/index.js +++ b/src/index.js @@ -8,12 +8,11 @@ const { hideBin } = require('yargs/helpers'); // Parse command-line arguments const argv = yargs(hideBin(process.argv)) - .usage('Usage: penify-oapi-codegen -s -l -v -o ') - .option('s', { - alias: 'source', + .usage('Usage: penify-oapi-codegen -i -l -v -o \n penify-oapi-codegen -s') + .option('i', { + alias: 'input', describe: 'Path to the OpenAPI JSON or YAML file', - type: 'string', - demandOption: true + type: 'string' }) .option('l', { alias: 'language', @@ -30,12 +29,30 @@ const argv = yargs(hideBin(process.argv)) describe: 'Output file path (optional)', type: 'string' }) + .option('s', { + alias: 'supported-languages', + describe: 'Get the list of supported languages', + type: 'boolean' + }) .help() .argv; -const openAPIPath = argv.s; +const openAPIPath = argv.i; const language = argv.l || null; const variant = argv.v || null; +const showSupportedLanguages = argv.s || null; + +if (showSupportedLanguages) { + const supportedLanguages = OpenAPIHelper.getSupportedLanguagesAndVariants(); + console.log('| Language | Variant |'); + console.log('|----------------|----------------|'); + supportedLanguages.forEach(item => { + console.log(`| ${item.language.padEnd(14)} | ${item.variant.padEnd(14)} |`); + }); + process.exit(0); +} + + const outputAPIPath = argv.o || path.resolve(`${path.basename(openAPIPath, path.extname(openAPIPath))}_with_code.json`); if (!openAPIPath) {