Skip to content

Commit

Permalink
chore: Update penify-oapi-codegen package name, improve command-line …
Browse files Browse the repository at this point in the history
…argument handling, and generate sample code for all languages and variants in OpenAPIHelper
  • Loading branch information
sumansaurabh committed Jun 9, 2024
1 parent f173263 commit b23ff6e
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 45 deletions.
70 changes: 32 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -44,59 +44,53 @@ penify-oapi-codegen -s path/to/your/openapi/schema.yaml -l language -v variant -
- `-l, --language <language>`: Programming language for the code example (optional).
- `-v, --variant <variant>`: Variant of the code generator for the specified language (optional).
- `-o, --output <path>`: 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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
8 changes: 8 additions & 0 deletions src/OpenAPIHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
29 changes: 23 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <source> -l <language> -v <variant> -o <output>')
.option('s', {
alias: 'source',
.usage('Usage: penify-oapi-codegen -i <input> -l <language> -v <variant> -o <output> \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',
Expand All @@ -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) {
Expand Down

0 comments on commit b23ff6e

Please sign in to comment.