Skip to content

Commit a0a2dbc

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 c0b0a78 commit a0a2dbc

4 files changed

Lines changed: 35 additions & 25 deletions

File tree

README.md

Lines changed: 14 additions & 16 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,29 +22,28 @@ 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 -s path/to/your/openapi/schema.yaml -l language -v variant -o path/to/output/code_example
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).
4947

5048
## Supported Code Generators
5149

@@ -92,13 +90,13 @@ The tool supports generating code examples for the following languages and varia
9290
### Example 1: Generate Python Requests Code Example
9391

9492
```bash
95-
openapi-code-gen generate -i ./schemas/api.yaml -l Python -v Requests -o ./examples/python_requests_example.py
93+
penify-oapi-codegen -s ./schemas/api.yaml -l Python -v Requests -o ./examples/python_requests_example.py
9694
```
9795

9896
### Example 2: Generate JavaScript Fetch Code Example
9997

10098
```bash
101-
openapi-code-gen generate -i ./schemas/api.yaml -l JavaScript -v Fetch -o ./examples/js_fetch_example.js
99+
penify-oapi-codegen -s ./schemas/api.yaml -l JavaScript -v Fetch -o ./examples/js_fetch_example.js
102100
```
103101

104102
## Contributing
@@ -111,7 +109,7 @@ This project is licensed under the ISC License.
111109

112110
## Author
113111

114-
[Your Name]
112+
sumansaurabh
115113

116114
---
117115

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
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: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ 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');
56

67
class OpenAPIHelper {
78
static convertOpenAPIToPostman(openAPIPath, postmanOutputPath) {
@@ -37,7 +38,7 @@ class OpenAPIHelper {
3738
console.error(error);
3839
} else {
3940
const fileName = `${operationId}_${language}_${variant}.pm`;
40-
fs.writeFileSync(`sample_code/${fileName}`, snippet);
41+
fs.writeFileSync(`/tmp/sample_code/${fileName}`, snippet);
4142
}
4243
});
4344
};
@@ -48,7 +49,6 @@ class OpenAPIHelper {
4849
const method = item.request.method.toLowerCase(); // eg: "get"
4950
const operationId = `${name.join('_')}_${url.join('_')}_${method}`;
5051
return operationId;
51-
5252
}
5353

5454
function processItems(items) {
@@ -71,10 +71,18 @@ class OpenAPIHelper {
7171
}
7272

7373
static addSampleCodeToOpenAPI(openAPIPath, outputAPIPath) {
74-
const openapiSchema = JSON.parse(fs.readFileSync(openAPIPath).toString());
74+
let openapiSchema;
75+
const ext = path.extname(openAPIPath);
76+
77+
if (ext === '.yaml' || ext === '.yml') {
78+
openapiSchema = yaml.load(fs.readFileSync(openAPIPath, 'utf8'));
79+
} else {
80+
openapiSchema = JSON.parse(fs.readFileSync(openAPIPath).toString());
81+
}
82+
7583
const sampleCode = {};
7684

77-
fs.readdirSync('sample_code').forEach(file => {
85+
fs.readdirSync('/tmp/sample_code').forEach(file => {
7886
const code = fs.readFileSync(`/tmp/sample_code/${file}`).toString();
7987
const [operationId, lang, vari] = file.replace('.pm', '').split('~');
8088
if (!sampleCode[operationId]) {
@@ -98,7 +106,11 @@ class OpenAPIHelper {
98106
});
99107
});
100108

101-
fs.writeFileSync(outputAPIPath, JSON.stringify(openapiSchema, null, 2));
109+
if (ext === '.yaml' || ext === '.yml') {
110+
fs.writeFileSync(outputAPIPath, yaml.dump(openapiSchema));
111+
} else {
112+
fs.writeFileSync(outputAPIPath, JSON.stringify(openapiSchema, null, 2));
113+
}
102114
}
103115
}
104116

src/index.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const argv = yargs(hideBin(process.argv))
1111
.usage('Usage: penify-oapi-codegen -s <source> -l <language> -v <variant> -o <output>')
1212
.option('s', {
1313
alias: 'source',
14-
describe: 'Path to the OpenAPI JSON file',
14+
describe: 'Path to the OpenAPI JSON or YAML file',
1515
type: 'string',
1616
demandOption: true
1717
})
@@ -36,15 +36,14 @@ const argv = yargs(hideBin(process.argv))
3636
const openAPIPath = argv.s;
3737
const language = argv.l || null;
3838
const variant = argv.v || null;
39-
const outputAPIPath = argv.o || path.resolve(`${path.basename(openAPIPath)}_with_code.json`);
39+
const outputAPIPath = argv.o || path.resolve(`${path.basename(openAPIPath, path.extname(openAPIPath))}_with_code.json`);
4040

4141
if (!openAPIPath) {
42-
console.error('Please provide the path to the OpenAPI JSON file.');
42+
console.error('Please provide the path to the OpenAPI file.');
4343
process.exit(1);
4444
}
4545

4646
const resolvedOpenAPIPath = path.resolve(openAPIPath);
47-
const file_name = path.basename(resolvedOpenAPIPath);
4847
const guid = uuidv4();
4948

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

0 commit comments

Comments
 (0)