Skip to content

Commit b23ff6e

Browse files
committed
chore: Update penify-oapi-codegen package name, improve command-line argument handling, and generate sample code for all languages and variants in OpenAPIHelper
1 parent f173263 commit b23ff6e

4 files changed

Lines changed: 64 additions & 45 deletions

File tree

README.md

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Once installed, you can use `penify-oapi-codegen` from the command line to gener
3535
### Generate Code Examples
3636

3737
```bash
38-
penify-oapi-codegen -s path/to/your/openapi/schema.yaml -l language -v variant -o path/to/output/code_example
38+
penify-oapi-codegen -i path/to/your/openapi/schema.json -l language -v variant -o path/to/output/schema_with_code.json
3939
```
4040

4141
### Options
@@ -44,59 +44,53 @@ penify-oapi-codegen -s path/to/your/openapi/schema.yaml -l language -v variant -
4444
- `-l, --language <language>`: Programming language for the code example (optional).
4545
- `-v, --variant <variant>`: Variant of the code generator for the specified language (optional).
4646
- `-o, --output <path>`: Path to the output code example file (optional).
47+
- `-s,`: To show list of supported languages
4748

4849
## Supported Code Generators
4950

5051
The tool supports generating code examples for the following languages and variants:
5152

52-
| Language | Variant |
53-
|---------------|---------------|
54-
| C | libcurl |
55-
| C# | HttpClient |
56-
| C# | RestSharp |
57-
| cURL | cURL |
58-
| Dart | http |
59-
| Go | Native |
60-
| HTTP | HTTP |
61-
| Java | OkHttp |
62-
| Java | Unirest |
63-
| JavaScript | Fetch |
64-
| JavaScript | jQuery |
65-
| JavaScript | XHR |
66-
| Kotlin | OkHttp |
67-
| NodeJs | Axios |
68-
| NodeJs | Native |
69-
| NodeJs | Request |
70-
| NodeJs | Unirest |
71-
| Objective-C | NSURLSession |
72-
| OCaml | Cohttp |
73-
| PHP | cURL |
74-
| PHP | Guzzle |
75-
| PHP | pecl_http |
76-
| PHP | HTTP_Request2 |
77-
| PowerShell | RestMethod |
78-
| Python | http.client |
79-
| Python | Requests |
80-
| R | httr |
81-
| R | RCurl |
82-
| Rust | Reqwest |
83-
| Ruby | Net:HTTP |
84-
| Shell | Httpie |
85-
| Shell | wget |
86-
| Swift | URLSession |
53+
| Language | Variant |
54+
|--------------- |----------------|
55+
| csharp | RestSharp |
56+
| curl | cURL |
57+
| go | Native |
58+
| http | HTTP |
59+
| java | OkHttp |
60+
| java | Unirest |
61+
| javascript | Fetch |
62+
| javascript | jQuery |
63+
| javascript | XHR |
64+
| c | libcurl |
65+
| nodejs | Axios |
66+
| nodejs | Native |
67+
| nodejs | Request |
68+
| nodejs | Unirest |
69+
| objective-c | NSURLSession |
70+
| ocaml | Cohttp |
71+
| php | cURL |
72+
| php | HTTP_Request2 |
73+
| php | pecl_http |
74+
| powershell | RestMethod |
75+
| python | http.client |
76+
| python | Requests |
77+
| ruby | Net::HTTP |
78+
| shell | Httpie |
79+
| shell | wget |
80+
| swift | URLSession |
8781

8882
## Examples
8983

9084
### Example 1: Generate Python Requests Code Example
9185

9286
```bash
93-
penify-oapi-codegen -s ./schemas/api.yaml -l Python -v Requests -o ./examples/python_requests_example.py
87+
penify-oapi-codegen -s ./schemas/api.json -l python -v Requests -o ./examples/schema_python_requests_example.json
9488
```
9589

9690
### Example 2: Generate JavaScript Fetch Code Example
9791

9892
```bash
99-
penify-oapi-codegen -s ./schemas/api.yaml -l JavaScript -v Fetch -o ./examples/js_fetch_example.js
93+
penify-oapi-codegen -s ./schemas/api.json -l javascript -v Fetch -o ./examples/schema_js_fetch_example.json
10094
```
10195

10296
## Contributing

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "penify-oapi-codegen",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "A JavaScript library to convert OpenAPI schema to Postman collection and generate sample code.",
55
"main": "src/index.js",
66
"scripts": {

src/OpenAPIHelper.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,20 @@ const { Collection, Item } = require('postman-collection');
33
const codegen = require('postman-code-generators');
44
const { execSync } = require('child_process');
55
const yaml = require('js-yaml');
6+
const path = require('path');
67

78
class OpenAPIHelper {
89
static convertOpenAPIToPostman(openAPIPath, postmanOutputPath) {
910
execSync(`openapi2postmanv2 -s ${openAPIPath} -o ${postmanOutputPath}`, { stdio: 'inherit' });
1011
}
1112

13+
static getSupportedLanguagesAndVariants() {
14+
const languages = codegen.getLanguageList();
15+
const supportedLanguages = [];
16+
languages.forEach(lang => lang.variants.forEach(vari => supportedLanguages.push({ language: lang.key, variant: vari.key }))) ;
17+
return supportedLanguages;
18+
}
19+
1220
static generateSampleCode(postmanCollectionPath, language = null, variant = null) {
1321
const collection = JSON.parse(fs.readFileSync(postmanCollectionPath).toString());
1422

src/index.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ const { hideBin } = require('yargs/helpers');
88

99
// Parse command-line arguments
1010
const argv = yargs(hideBin(process.argv))
11-
.usage('Usage: penify-oapi-codegen -s <source> -l <language> -v <variant> -o <output>')
12-
.option('s', {
13-
alias: 'source',
11+
.usage('Usage: penify-oapi-codegen -i <input> -l <language> -v <variant> -o <output> \n penify-oapi-codegen -s')
12+
.option('i', {
13+
alias: 'input',
1414
describe: 'Path to the OpenAPI JSON or YAML file',
15-
type: 'string',
16-
demandOption: true
15+
type: 'string'
1716
})
1817
.option('l', {
1918
alias: 'language',
@@ -30,12 +29,30 @@ const argv = yargs(hideBin(process.argv))
3029
describe: 'Output file path (optional)',
3130
type: 'string'
3231
})
32+
.option('s', {
33+
alias: 'supported-languages',
34+
describe: 'Get the list of supported languages',
35+
type: 'boolean'
36+
})
3337
.help()
3438
.argv;
3539

36-
const openAPIPath = argv.s;
40+
const openAPIPath = argv.i;
3741
const language = argv.l || null;
3842
const variant = argv.v || null;
43+
const showSupportedLanguages = argv.s || null;
44+
45+
if (showSupportedLanguages) {
46+
const supportedLanguages = OpenAPIHelper.getSupportedLanguagesAndVariants();
47+
console.log('| Language | Variant |');
48+
console.log('|----------------|----------------|');
49+
supportedLanguages.forEach(item => {
50+
console.log(`| ${item.language.padEnd(14)} | ${item.variant.padEnd(14)} |`);
51+
});
52+
process.exit(0);
53+
}
54+
55+
3956
const outputAPIPath = argv.o || path.resolve(`${path.basename(openAPIPath, path.extname(openAPIPath))}_with_code.json`);
4057

4158
if (!openAPIPath) {

0 commit comments

Comments
 (0)