Skip to content

Commit 168d4b9

Browse files
authored
Merge pull request #2 from SingularityX-ai/yaml_support
Yaml support
2 parents c0b0a78 + b23ff6e commit 168d4b9

4 files changed

Lines changed: 98 additions & 69 deletions

File tree

README.md

Lines changed: 43 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1+
# Penify Open API Code Gen
12

2-
# Open API Code Gen
3-
4-
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.
3+
`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.
54

65
## Features
76

@@ -10,7 +9,7 @@ Openapi-code-gen is a JavaScript library designed to convert OpenAPI schemas int
109

1110
## Prerequisites
1211

13-
Before installing `openapi-code-gen`, ensure you have the following installed:
12+
Before installing `penify-oapi-codegen`, ensure you have the following installed:
1413

1514
- [Node.js](https://nodejs.org/) (version 12 or higher)
1615
- npm (Node package manager)
@@ -23,82 +22,75 @@ npm install -g openapi-to-postmanv2
2322

2423
## Installation
2524

26-
To install `openapi-code-gen`, run the following command:
25+
To install `penify-oapi-codegen`, run the following command:
2726

2827
```bash
29-
npm install openapi-code-gen
28+
npm install penify-oapi-codegen
3029
```
3130

3231
## Usage
3332

34-
Once installed, you can use `openapi-code-gen` from the command line to generate code examples from your OpenAPI schemas.
33+
Once installed, you can use `penify-oapi-codegen` from the command line to generate code examples from your OpenAPI schemas.
3534

3635
### Generate Code Examples
3736

3837
```bash
39-
openapi-code-gen generate -i 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
4039
```
4140

4241
### Options
4342

44-
- `generate`: Generates code examples from an OpenAPI schema.
45-
- `-i, --input <path>`: Path to the OpenAPI schema file (required).
46-
- `-l, --language <language>`: Programming language for the code example (required).
47-
- `-v, --variant <variant>`: Variant of the code generator for the specified language (required).
48-
- `-o, --output <path>`: Path to the output code example file (required).
43+
- `-s, --source <path>`: Path to the OpenAPI schema file (required).
44+
- `-l, --language <language>`: Programming language for the code example (optional).
45+
- `-v, --variant <variant>`: Variant of the code generator for the specified language (optional).
46+
- `-o, --output <path>`: Path to the output code example file (optional).
47+
- `-s,`: To show list of supported languages
4948

5049
## Supported Code Generators
5150

5251
The tool supports generating code examples for the following languages and variants:
5352

54-
| Language | Variant |
55-
|---------------|---------------|
56-
| C | libcurl |
57-
| C# | HttpClient |
58-
| C# | RestSharp |
59-
| cURL | cURL |
60-
| Dart | http |
61-
| Go | Native |
62-
| HTTP | HTTP |
63-
| Java | OkHttp |
64-
| Java | Unirest |
65-
| JavaScript | Fetch |
66-
| JavaScript | jQuery |
67-
| JavaScript | XHR |
68-
| Kotlin | OkHttp |
69-
| NodeJs | Axios |
70-
| NodeJs | Native |
71-
| NodeJs | Request |
72-
| NodeJs | Unirest |
73-
| Objective-C | NSURLSession |
74-
| OCaml | Cohttp |
75-
| PHP | cURL |
76-
| PHP | Guzzle |
77-
| PHP | pecl_http |
78-
| PHP | HTTP_Request2 |
79-
| PowerShell | RestMethod |
80-
| Python | http.client |
81-
| Python | Requests |
82-
| R | httr |
83-
| R | RCurl |
84-
| Rust | Reqwest |
85-
| Ruby | Net:HTTP |
86-
| Shell | Httpie |
87-
| Shell | wget |
88-
| 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 |
8981

9082
## Examples
9183

9284
### Example 1: Generate Python Requests Code Example
9385

9486
```bash
95-
openapi-code-gen generate -i ./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
9688
```
9789

9890
### Example 2: Generate JavaScript Fetch Code Example
9991

10092
```bash
101-
openapi-code-gen generate -i ./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
10294
```
10395

10496
## Contributing
@@ -111,7 +103,7 @@ This project is licensed under the ISC License.
111103

112104
## Author
113105

114-
[Your Name]
106+
sumansaurabh
115107

116108
---
117109

package.json

Lines changed: 4 additions & 3 deletions
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": {
@@ -10,21 +10,22 @@
1010
"test": "jest"
1111
},
1212
"bin": {
13-
"openapi-code-gen": "src/index.js"
13+
"penify-oapi-codegen": "src/index.js"
1414
},
1515
"keywords": [],
1616
"author": "",
1717
"license": "ISC",
1818
"dependencies": {
1919
"openapi-to-postmanv2": "4.21.0",
20-
"postman-code-generators": "1.10.1",
20+
"postman-code-generators": "^1.0.2",
2121
"postman-collection": "^4.4.0"
2222
},
2323
"devDependencies": {
2424
"@eslint/js": "^9.4.0",
2525
"eslint": "^9.4.0",
2626
"globals": "^15.4.0",
2727
"jest": "^29.7.0",
28+
"js-yaml": "^4.1.0",
2829
"uuid": "^9.0.1"
2930
},
3031
"peerDependencies": {

src/OpenAPIHelper.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,21 @@ const fs = require('fs');
22
const { Collection, Item } = require('postman-collection');
33
const codegen = require('postman-code-generators');
44
const { execSync } = require('child_process');
5+
const yaml = require('js-yaml');
6+
const path = require('path');
57

68
class OpenAPIHelper {
79
static convertOpenAPIToPostman(openAPIPath, postmanOutputPath) {
810
execSync(`openapi2postmanv2 -s ${openAPIPath} -o ${postmanOutputPath}`, { stdio: 'inherit' });
911
}
1012

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+
1120
static generateSampleCode(postmanCollectionPath, language = null, variant = null) {
1221
const collection = JSON.parse(fs.readFileSync(postmanCollectionPath).toString());
1322

@@ -37,7 +46,7 @@ class OpenAPIHelper {
3746
console.error(error);
3847
} else {
3948
const fileName = `${operationId}_${language}_${variant}.pm`;
40-
fs.writeFileSync(`sample_code/${fileName}`, snippet);
49+
fs.writeFileSync(`/tmp/sample_code/${fileName}`, snippet);
4150
}
4251
});
4352
};
@@ -48,7 +57,6 @@ class OpenAPIHelper {
4857
const method = item.request.method.toLowerCase(); // eg: "get"
4958
const operationId = `${name.join('_')}_${url.join('_')}_${method}`;
5059
return operationId;
51-
5260
}
5361

5462
function processItems(items) {
@@ -71,10 +79,18 @@ class OpenAPIHelper {
7179
}
7280

7381
static addSampleCodeToOpenAPI(openAPIPath, outputAPIPath) {
74-
const openapiSchema = JSON.parse(fs.readFileSync(openAPIPath).toString());
82+
let openapiSchema;
83+
const ext = path.extname(openAPIPath);
84+
85+
if (ext === '.yaml' || ext === '.yml') {
86+
openapiSchema = yaml.load(fs.readFileSync(openAPIPath, 'utf8'));
87+
} else {
88+
openapiSchema = JSON.parse(fs.readFileSync(openAPIPath).toString());
89+
}
90+
7591
const sampleCode = {};
7692

77-
fs.readdirSync('sample_code').forEach(file => {
93+
fs.readdirSync('/tmp/sample_code').forEach(file => {
7894
const code = fs.readFileSync(`/tmp/sample_code/${file}`).toString();
7995
const [operationId, lang, vari] = file.replace('.pm', '').split('~');
8096
if (!sampleCode[operationId]) {
@@ -98,7 +114,11 @@ class OpenAPIHelper {
98114
});
99115
});
100116

101-
fs.writeFileSync(outputAPIPath, JSON.stringify(openapiSchema, null, 2));
117+
if (ext === '.yaml' || ext === '.yml') {
118+
fs.writeFileSync(outputAPIPath, yaml.dump(openapiSchema));
119+
} else {
120+
fs.writeFileSync(outputAPIPath, JSON.stringify(openapiSchema, null, 2));
121+
}
102122
}
103123
}
104124

src/index.js

Lines changed: 26 additions & 10 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',
14-
describe: 'Path to the OpenAPI JSON file',
15-
type: 'string',
16-
demandOption: true
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',
14+
describe: 'Path to the OpenAPI JSON or YAML file',
15+
type: 'string'
1716
})
1817
.option('l', {
1918
alias: 'language',
@@ -30,21 +29,38 @@ 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;
39-
const outputAPIPath = argv.o || path.resolve(`${path.basename(openAPIPath)}_with_code.json`);
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+
56+
const outputAPIPath = argv.o || path.resolve(`${path.basename(openAPIPath, path.extname(openAPIPath))}_with_code.json`);
4057

4158
if (!openAPIPath) {
42-
console.error('Please provide the path to the OpenAPI JSON file.');
59+
console.error('Please provide the path to the OpenAPI file.');
4360
process.exit(1);
4461
}
4562

4663
const resolvedOpenAPIPath = path.resolve(openAPIPath);
47-
const file_name = path.basename(resolvedOpenAPIPath);
4864
const guid = uuidv4();
4965

5066
const postmanOutputPath = path.resolve(`/tmp/openapi_schema_postman_${guid}.json`);

0 commit comments

Comments
 (0)