Skip to content

Commit d11f792

Browse files
committed
chore: Update npm dependencies, generate sample code for all languages and variants in OpenAPIHelper, and refactor OpenAPIHelper to use JavaScript
1 parent 92b5f63 commit d11f792

5 files changed

Lines changed: 133 additions & 3 deletions

File tree

.github/workflows/publish.yaml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: Publish to npm
2+
3+
on:
4+
release:
5+
types: [published]
6+
7+
jobs:
8+
publish:
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- name: Checkout code
13+
uses: actions/checkout@v2
14+
15+
- name: Set up Node.js
16+
uses: actions/setup-node@v2
17+
with:
18+
node-version: '14'
19+
registry-url: 'https://registry.npmjs.org'
20+
21+
- name: Install dependencies
22+
run: npm install
23+
24+
- name: Lint and Test
25+
run: |
26+
npm run lint
27+
npm run test
28+
29+
- name: Publish to npm
30+
run: npm publish
31+
env:
32+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
33+
34+
- name: Publish to Yarn
35+
run: yarn publish
36+
env:
37+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

__tests__/OpenAPIHelper.test.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// __tests__/OpenAPIHelper.test.js
2+
3+
const fs = require('fs');
4+
const { execSync } = require('child_process');
5+
const OpenAPIHelper = require('../src/OpenAPIHelper');
6+
7+
jest.mock('fs');
8+
jest.mock('child_process');
9+
10+
describe('OpenAPIHelper', () => {
11+
beforeEach(() => {
12+
jest.clearAllMocks();
13+
});
14+
15+
describe('convertOpenAPIToPostman', () => {
16+
it('should call execSync with the correct command', () => {
17+
const openAPIPath = 'openapi.json';
18+
const postmanOutputPath = '/tmp/openapi_schema_postman.json';
19+
20+
OpenAPIHelper.convertOpenAPIToPostman(openAPIPath, postmanOutputPath);
21+
22+
expect(execSync).toHaveBeenCalledWith(
23+
`openapi2postmanv2 -s ${openAPIPath} -o ${postmanOutputPath}`,
24+
{ stdio: 'inherit' }
25+
);
26+
});
27+
});
28+
29+
describe('generateSampleCode', () => {
30+
it('should generate code for all languages and variants if no specific language and variant are provided', () => {
31+
const postmanCollectionPath = '/tmp/postman_collection.json';
32+
const collection = {
33+
item: [
34+
{
35+
request: {
36+
name: 'Get Users',
37+
method: 'GET',
38+
url: { path: ['api', 'v1', 'users'] }
39+
}
40+
}
41+
]
42+
};
43+
44+
fs.readFileSync.mockReturnValue(JSON.stringify(collection));
45+
fs.existsSync.mockReturnValue(false);
46+
fs.mkdirSync.mockReturnValue();
47+
48+
OpenAPIHelper.generateSampleCode(postmanCollectionPath);
49+
50+
expect(fs.readFileSync).toHaveBeenCalledWith(postmanCollectionPath);
51+
expect(fs.mkdirSync).toHaveBeenCalledWith('/tmp/sample_code');
52+
expect(fs.writeFileSync).toHaveBeenCalled();
53+
});
54+
55+
it('should generate code for specific language and variant if provided', () => {
56+
const postmanCollectionPath = '/tmp/postman_collection.json';
57+
const collection = {
58+
item: [
59+
{
60+
request: {
61+
name: 'Get Users',
62+
method: 'GET',
63+
url: { path: ['api', 'v1', 'users'] }
64+
}
65+
}
66+
]
67+
};
68+
69+
fs.readFileSync.mockReturnValue(JSON.stringify(collection));
70+
fs.existsSync.mockReturnValue(true);
71+
72+
OpenAPIHelper.generateSampleCode(postmanCollectionPath, 'nodejs', 'axios');
73+
74+
expect(fs.readFileSync).toHaveBeenCalledWith(postmanCollectionPath);
75+
expect(fs.writeFileSync).toHaveBeenCalled();
76+
});
77+
});
78+
});

eslint.config.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = [
2+
{
3+
rules: {
4+
semi: "error",
5+
"prefer-const": "error"
6+
}
7+
}
8+
];

package.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
"description": "A JavaScript library to convert OpenAPI schema to Postman collection and generate sample code.",
55
"main": "src/index.js",
66
"scripts": {
7-
"start": "node src/index.js"
7+
"start": "node src/index.js",
8+
"lint": "eslint .",
9+
"lint:fix": "eslint . --fix",
10+
"test": "jest"
811
},
912
"bin": {
1013
"openapi-code-gen": "src/index.js"
@@ -13,11 +16,15 @@
1316
"author": "",
1417
"license": "ISC",
1518
"dependencies": {
16-
"openapi-to-postmanv2": "^4.21.0",
19+
"openapi-to-postmanv2": "4.21.0",
1720
"postman-code-generators": "1.10.1",
1821
"postman-collection": "^4.4.0"
1922
},
2023
"devDependencies": {
24+
"@eslint/js": "^9.4.0",
25+
"eslint": "^9.4.0",
26+
"globals": "^15.4.0",
27+
"jest": "^29.7.0",
2128
"uuid": "^9.0.1"
2229
},
2330
"peerDependencies": {

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const outputAPIPath = path.resolve(`${file_name}_with_code.json`);
2323
console.log("Executing step 1.");
2424
OpenAPIHelper.convertOpenAPIToPostman(resolvedOpenAPIPath, postmanOutputPath);
2525
console.log("Executing step 2.");
26-
OpenAPIHelper.generateSampleCode(postmanOutputPath);
26+
OpenAPIHelper.generateSampleCode(postmanOutputPath, language, variant);
2727
console.log("Executing step 3.");
2828
OpenAPIHelper.addSampleCodeToOpenAPI(resolvedOpenAPIPath, outputAPIPath);
2929

0 commit comments

Comments
 (0)