@grafana/openapi-to-k6 0.2.5 → 0.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/tests.yaml +1 -0
- package/README.md +6 -4
- package/dist/cli.js +27 -12
- package/dist/errors.js +10 -0
- package/dist/generator/index.js +50 -3
- package/dist/helper.js +32 -0
- package/examples/basic_schema/single/simpleAPI.ts +1 -1
- package/examples/basic_schema/split/simpleAPI.schemas.ts +1 -1
- package/examples/basic_schema/split/simpleAPI.ts +1 -1
- package/examples/basic_schema/tags/default.ts +1 -1
- package/examples/basic_schema/tags/simpleAPI.schemas.ts +1 -1
- package/examples/form_data_schema/single/formDataAPI.ts +1 -1
- package/examples/form_data_schema/split/formDataAPI.schemas.ts +1 -1
- package/examples/form_data_schema/split/formDataAPI.ts +1 -1
- package/examples/form_data_schema/tags/default.ts +1 -1
- package/examples/form_data_schema/tags/formDataAPI.schemas.ts +1 -1
- package/examples/form_url_encoded_data_schema/single/formURLEncodedAPI.ts +1 -1
- package/examples/form_url_encoded_data_schema/split/formURLEncodedAPI.schemas.ts +1 -1
- package/examples/form_url_encoded_data_schema/split/formURLEncodedAPI.ts +1 -1
- package/examples/form_url_encoded_data_schema/tags/default.ts +1 -1
- package/examples/form_url_encoded_data_schema/tags/formURLEncodedAPI.schemas.ts +1 -1
- package/examples/form_url_encoded_data_with_query_params_schema/single/formURLEncodedAPIWithQueryParameters.ts +1 -1
- package/examples/form_url_encoded_data_with_query_params_schema/split/formURLEncodedAPIWithQueryParameters.schemas.ts +1 -1
- package/examples/form_url_encoded_data_with_query_params_schema/split/formURLEncodedAPIWithQueryParameters.ts +1 -1
- package/examples/form_url_encoded_data_with_query_params_schema/tags/default.ts +1 -1
- package/examples/form_url_encoded_data_with_query_params_schema/tags/formURLEncodedAPIWithQueryParameters.schemas.ts +1 -1
- package/examples/get_request_with_path_parameters_schema/single/simpleAPI.ts +1 -1
- package/examples/get_request_with_path_parameters_schema/split/simpleAPI.schemas.ts +1 -1
- package/examples/get_request_with_path_parameters_schema/split/simpleAPI.ts +1 -1
- package/examples/get_request_with_path_parameters_schema/tags/default.ts +1 -1
- package/examples/get_request_with_path_parameters_schema/tags/simpleAPI.schemas.ts +1 -1
- package/examples/headers_schema/single/headerDemoAPI.ts +1 -1
- package/examples/headers_schema/split/headerDemoAPI.schemas.ts +1 -1
- package/examples/headers_schema/split/headerDemoAPI.ts +1 -1
- package/examples/headers_schema/tags/default.ts +1 -1
- package/examples/headers_schema/tags/headerDemoAPI.schemas.ts +1 -1
- package/examples/no_title_schema/single/k6Client.ts +1 -1
- package/examples/no_title_schema/split/k6Client.schemas.ts +1 -1
- package/examples/no_title_schema/split/k6Client.ts +1 -1
- package/examples/no_title_schema/tags/default.ts +1 -1
- package/examples/no_title_schema/tags/k6Client.schemas.ts +1 -1
- package/examples/post_request_with_query_params/single/exampleAPI.ts +1 -1
- package/examples/post_request_with_query_params/split/exampleAPI.schemas.ts +1 -1
- package/examples/post_request_with_query_params/split/exampleAPI.ts +1 -1
- package/examples/post_request_with_query_params/tags/default.ts +1 -1
- package/examples/post_request_with_query_params/tags/exampleAPI.schemas.ts +1 -1
- package/examples/query_params_schema/single/exampleAPI.ts +1 -1
- package/examples/query_params_schema/split/exampleAPI.schemas.ts +1 -1
- package/examples/query_params_schema/split/exampleAPI.ts +1 -1
- package/examples/query_params_schema/tags/default.ts +1 -1
- package/examples/query_params_schema/tags/exampleAPI.schemas.ts +1 -1
- package/examples/simple_post_request_schema/single/exampleAPI.ts +1 -1
- package/examples/simple_post_request_schema/split/exampleAPI.schemas.ts +1 -1
- package/examples/simple_post_request_schema/split/exampleAPI.ts +1 -1
- package/examples/simple_post_request_schema/tags/default.ts +1 -1
- package/examples/simple_post_request_schema/tags/exampleAPI.schemas.ts +1 -1
- package/package.json +1 -1
- package/src/cli.ts +32 -14
- package/src/errors.ts +6 -0
- package/src/generator/index.ts +67 -2
- package/src/helper.ts +40 -0
- package/src/type.d.ts +1 -0
- package/tests/e2e/schema.json +1 -0
- package/tests/e2e/single-tag-filter/k6Script.ts +50 -0
- package/tests/e2e/tags/k6Script.ts +3 -3
- package/tests/functional-tests/fixtures/tags_filtering.json +141 -0
- package/tests/functional-tests/generator.test.ts +159 -3
- package/tests/helper.test.ts +59 -0
|
@@ -33,6 +33,7 @@ jobs:
|
|
|
33
33
|
npm run dev -- ./tests/e2e/schema.json ./tests/e2e/single/sdk.ts --disable-analytics --mode single
|
|
34
34
|
npm run dev -- ./tests/e2e/schema.json ./tests/e2e/split/sdk.ts --disable-analytics --mode split
|
|
35
35
|
npm run dev -- ./tests/e2e/schema.json ./tests/e2e/tags/sdk.ts --disable-analytics --mode tags
|
|
36
|
+
npm run dev -- ./tests/e2e/schema.json ./tests/e2e/single-tag-filter/sdk.ts --disable-analytics --mode single --only-tags ItemsHeader
|
|
36
37
|
- name: Start Mockoon CLI
|
|
37
38
|
uses: mockoon/cli-action@v2
|
|
38
39
|
with:
|
package/README.md
CHANGED
|
@@ -60,10 +60,12 @@ Following are some of the configuration options supported by the tool.
|
|
|
60
60
|
3. `tags`: This modes splits your OpenAPI schema based on the tags and generates a separate client for each tag. If a route has no tag set, it will be available in `default.ts` file.
|
|
61
61
|
|
|
62
62
|
To check how the output looks for each mode, check out the [examples](./examples) directory.
|
|
63
|
-
2. `--
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
63
|
+
2. `--only-tags`: Filter the generated client to only include routes with specific tags from your OpenAPI schema. Multiple tags can be specified to include routes matching any of those tags. Routes without tags will be excluded. This is useful for generating focused clients that only contain the endpoints you need.
|
|
64
|
+
e.g. `openapi-to-k6 <path-to-openapi-schema> <output path> --only-tags ItemsHeader` will generate a client with only the routes that have the `ItemsHeader` tag. Multiple tags can be specified by using multiple `--only-tags` flags or by separating them with spaces.
|
|
65
|
+
3. `--disable-analytics`: Disable anonymous usage analytics reporting which helping making the tool better. You can also set an environment variable `DISABLE_ANALYTICS=true` to disable the analytics.
|
|
66
|
+
4. `--include-sample-script`: Generate a sample k6 script.
|
|
67
|
+
5. `--verbose` or `-v` : Enable verbose logging to see more detailed logging output.
|
|
68
|
+
6. `--help` or `-h` : Show help message.
|
|
67
69
|
|
|
68
70
|
## Developing locally
|
|
69
71
|
|
package/dist/cli.js
CHANGED
|
@@ -17,6 +17,7 @@ const chalk_1 = __importDefault(require("chalk"));
|
|
|
17
17
|
const commander_1 = require("commander");
|
|
18
18
|
const analytics_1 = require("./analytics");
|
|
19
19
|
const constants_1 = require("./constants");
|
|
20
|
+
const errors_1 = require("./errors");
|
|
20
21
|
const generator_1 = __importDefault(require("./generator"));
|
|
21
22
|
const helper_1 = require("./helper");
|
|
22
23
|
const logger_1 = require("./logger");
|
|
@@ -35,23 +36,30 @@ function validateMode(value) {
|
|
|
35
36
|
return value;
|
|
36
37
|
}
|
|
37
38
|
function generateSDK(_a) {
|
|
38
|
-
return __awaiter(this, arguments, void 0, function* ({ openApiPath, outputDir, shouldGenerateSampleK6Script, analyticsData, mode, }) {
|
|
39
|
-
logger_1.logger.logMessage('Generating TypeScript client for k6...\n'
|
|
40
|
-
|
|
41
|
-
|
|
39
|
+
return __awaiter(this, arguments, void 0, function* ({ openApiPath, outputDir, shouldGenerateSampleK6Script, analyticsData, mode, tags, }) {
|
|
40
|
+
logger_1.logger.logMessage('Generating TypeScript client for k6...\n' +
|
|
41
|
+
'OpenAPI schema: ' +
|
|
42
|
+
chalk_1.default.cyan(openApiPath) +
|
|
43
|
+
'\n' +
|
|
44
|
+
'Output: ' +
|
|
45
|
+
chalk_1.default.cyan(outputDir) +
|
|
46
|
+
'\n' +
|
|
47
|
+
((tags === null || tags === void 0 ? void 0 : tags.length)
|
|
48
|
+
? 'Filtering by tag(s): ' + chalk_1.default.cyan(tags.join(', '))
|
|
49
|
+
: '') +
|
|
50
|
+
'\n');
|
|
42
51
|
yield (0, generator_1.default)({
|
|
43
52
|
openApiPath,
|
|
44
53
|
outputDir,
|
|
45
54
|
shouldGenerateSampleK6Script,
|
|
46
55
|
analyticsData,
|
|
47
56
|
mode,
|
|
57
|
+
tags,
|
|
48
58
|
});
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
logger_1.logger.logMessage(`TypeScript client generated successfully.`, chalk_1.default.green);
|
|
54
|
-
}
|
|
59
|
+
const message = shouldGenerateSampleK6Script
|
|
60
|
+
? 'TypeScript client and sample k6 script generated successfully.'
|
|
61
|
+
: 'TypeScript client generated successfully.';
|
|
62
|
+
logger_1.logger.logMessage(message, chalk_1.default.green);
|
|
55
63
|
});
|
|
56
64
|
}
|
|
57
65
|
program
|
|
@@ -61,6 +69,7 @@ program
|
|
|
61
69
|
.argument('<openApiPath>', 'Path or URL for the OpenAPI schema file')
|
|
62
70
|
.argument('<outputDir>', 'Directory where the SDK should be generated')
|
|
63
71
|
.option('-m, --mode <string>', `mode to use for generating the client. Valid values - ${Object.values(constants_1.Mode).join(', ')}`, validateMode, constants_1.Mode.SINGLE)
|
|
72
|
+
.option('--only-tags <filters...>', 'list of tags to filter on. Generated client will only include operations with these tags')
|
|
64
73
|
.option('-v, --verbose', 'enable verbose mode to show debug logs')
|
|
65
74
|
.option('--include-sample-script', 'generate a sample k6 script')
|
|
66
75
|
.option('--disable-analytics', 'disable anonymous usage data collection')
|
|
@@ -89,11 +98,17 @@ program
|
|
|
89
98
|
shouldGenerateSampleK6Script: !!options.includeSampleScript,
|
|
90
99
|
analyticsData,
|
|
91
100
|
mode: options.mode,
|
|
101
|
+
tags: options.onlyTags,
|
|
92
102
|
});
|
|
93
103
|
}
|
|
94
104
|
catch (error) {
|
|
95
|
-
|
|
96
|
-
|
|
105
|
+
if (error instanceof errors_1.NoFilesGeneratedError) {
|
|
106
|
+
logger_1.logger.logMessage(error.message, chalk_1.default.yellow);
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
logger_1.logger.error('Failed to generate SDK:');
|
|
110
|
+
console.error(error);
|
|
111
|
+
}
|
|
97
112
|
}
|
|
98
113
|
if (!shouldDisableAnalytics && analyticsData) {
|
|
99
114
|
logger_1.logger.debug('Reporting following usage analytics data:');
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NoFilesGeneratedError = void 0;
|
|
4
|
+
class NoFilesGeneratedError extends Error {
|
|
5
|
+
constructor(message = 'No files were generated') {
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = 'NoFilesGeneratedError';
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
exports.NoFilesGeneratedError = NoFilesGeneratedError;
|
package/dist/generator/index.js
CHANGED
|
@@ -16,6 +16,7 @@ const fs_1 = __importDefault(require("fs"));
|
|
|
16
16
|
const orval_1 = __importDefault(require("orval"));
|
|
17
17
|
const path_1 = __importDefault(require("path"));
|
|
18
18
|
const constants_1 = require("../constants");
|
|
19
|
+
const errors_1 = require("../errors");
|
|
19
20
|
const helper_1 = require("../helper");
|
|
20
21
|
const logger_1 = require("../logger");
|
|
21
22
|
const k6Client_1 = require("./k6Client");
|
|
@@ -31,7 +32,28 @@ const generatedFileHeaderGenerator = (info) => {
|
|
|
31
32
|
];
|
|
32
33
|
};
|
|
33
34
|
const afterAllFilesWriteHandler = (filePaths) => __awaiter(void 0, void 0, void 0, function* () {
|
|
35
|
+
const removeSingleFile = (filePath) => {
|
|
36
|
+
try {
|
|
37
|
+
fs_1.default.unlinkSync(filePath);
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
// This is non-critical, so we just log it
|
|
41
|
+
logger_1.logger.debug(`afterAllFilesWriteHandler ~ Error deleting file ${filePath}: ${error}`);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
const emptyFileList = [];
|
|
34
45
|
for (const filePath of filePaths) {
|
|
46
|
+
// There is a bug in the orval library which generates empty files when tags filter is applied
|
|
47
|
+
// and no matching endpoints are found and used mode is split or single.
|
|
48
|
+
// It generates the file with only the header comment.
|
|
49
|
+
// Hence, we manually remove those empty files.
|
|
50
|
+
// Issue link - https://github.com/orval-labs/orval/issues/1691
|
|
51
|
+
if ((0, helper_1.hasOnlyComments)(fs_1.default.readFileSync(filePath, 'utf-8'))) {
|
|
52
|
+
emptyFileList.push(filePath);
|
|
53
|
+
// Delete the file
|
|
54
|
+
removeSingleFile(filePath);
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
35
57
|
yield (0, helper_1.formatFileWithPrettier)(filePath);
|
|
36
58
|
const fileName = path_1.default.basename(filePath);
|
|
37
59
|
if (fileName === '.ts') {
|
|
@@ -43,16 +65,32 @@ const afterAllFilesWriteHandler = (filePaths) => __awaiter(void 0, void 0, void
|
|
|
43
65
|
fs_1.default.renameSync(filePath, newPath);
|
|
44
66
|
}
|
|
45
67
|
}
|
|
68
|
+
if (emptyFileList.length > 0) {
|
|
69
|
+
logger_1.logger.debug(`afterAllFilesWriteHandler ~ The following files were empty and removed: ${emptyFileList.join(', ')}`);
|
|
70
|
+
}
|
|
71
|
+
// Return the list of generated file paths excluding the empty files
|
|
72
|
+
const filteredFilePaths = filePaths.filter((filePath) => !emptyFileList.includes(filePath));
|
|
73
|
+
// Check if all the filtered file path end with `.schemas.ts`
|
|
74
|
+
if (filteredFilePaths.every((filePath) => filePath.endsWith('.schemas.ts'))) {
|
|
75
|
+
// If yes we should remove them as only schemas files is not needed
|
|
76
|
+
filteredFilePaths.map(removeSingleFile);
|
|
77
|
+
return [];
|
|
78
|
+
}
|
|
79
|
+
return filteredFilePaths;
|
|
46
80
|
});
|
|
47
|
-
exports.default = (_a) => __awaiter(void 0, [_a], void 0, function* ({ openApiPath, outputDir, shouldGenerateSampleK6Script, analyticsData, mode, }) {
|
|
81
|
+
exports.default = (_a) => __awaiter(void 0, [_a], void 0, function* ({ openApiPath, outputDir, shouldGenerateSampleK6Script, analyticsData, mode, tags, }) {
|
|
48
82
|
/**
|
|
49
83
|
* Note!
|
|
50
84
|
* 1. override.requestOptions is not supported for the custom K6 client
|
|
51
85
|
* 2. override.mutator is not supported for the custom K6 client
|
|
52
86
|
*/
|
|
87
|
+
const generatedFilePaths = [];
|
|
53
88
|
yield outputOverrider.redirectOutputToNullStream(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
54
89
|
yield (0, orval_1.default)({
|
|
55
|
-
input:
|
|
90
|
+
input: {
|
|
91
|
+
target: openApiPath,
|
|
92
|
+
filters: { tags: tags && tags.length > 0 ? tags : undefined },
|
|
93
|
+
},
|
|
56
94
|
output: {
|
|
57
95
|
target: outputDir,
|
|
58
96
|
mode: mode,
|
|
@@ -63,8 +101,17 @@ exports.default = (_a) => __awaiter(void 0, [_a], void 0, function* ({ openApiPa
|
|
|
63
101
|
headers: true,
|
|
64
102
|
},
|
|
65
103
|
hooks: {
|
|
66
|
-
afterAllFilesWrite:
|
|
104
|
+
afterAllFilesWrite: (filePaths) => __awaiter(void 0, void 0, void 0, function* () {
|
|
105
|
+
const filteredFilePaths = yield afterAllFilesWriteHandler(filePaths);
|
|
106
|
+
generatedFilePaths.push(...filteredFilePaths);
|
|
107
|
+
}),
|
|
67
108
|
},
|
|
68
109
|
});
|
|
69
110
|
}));
|
|
111
|
+
if (generatedFilePaths.length === 0) {
|
|
112
|
+
const tagsMessage = (tags === null || tags === void 0 ? void 0 : tags.length) && tags.length > 0
|
|
113
|
+
? ` Applied tag filter(s): ${tags.join(', ')}`
|
|
114
|
+
: '';
|
|
115
|
+
throw new errors_1.NoFilesGeneratedError(`No files were generated.${tagsMessage}`);
|
|
116
|
+
}
|
|
70
117
|
});
|
package/dist/helper.js
CHANGED
|
@@ -17,8 +17,10 @@ exports.formatFileWithPrettier = formatFileWithPrettier;
|
|
|
17
17
|
exports.getGeneratedClientPath = getGeneratedClientPath;
|
|
18
18
|
exports.djb2Hash = djb2Hash;
|
|
19
19
|
exports.getDirectoryForPath = getDirectoryForPath;
|
|
20
|
+
exports.hasOnlyComments = hasOnlyComments;
|
|
20
21
|
const core_1 = require("@orval/core");
|
|
21
22
|
const fs_1 = __importDefault(require("fs"));
|
|
23
|
+
const typescript_1 = require("typescript");
|
|
22
24
|
const path_1 = __importDefault(require("path"));
|
|
23
25
|
const prettier_1 = require("prettier");
|
|
24
26
|
const package_json_1 = __importDefault(require("../package.json"));
|
|
@@ -156,3 +158,33 @@ function getDirectoryForPath(pathString) {
|
|
|
156
158
|
// If the path has an extension, it is a file
|
|
157
159
|
return path_1.default.dirname(pathString);
|
|
158
160
|
}
|
|
161
|
+
/**
|
|
162
|
+
* Checks if a file contains only comments and whitespace, with no actual code.
|
|
163
|
+
*
|
|
164
|
+
* This function uses TypeScript's parser to accurately detect and remove all types
|
|
165
|
+
* of comments (single-line, multi-line, JSDoc) and whitespace from the input text.
|
|
166
|
+
* If nothing remains after removing comments and whitespace, then the file is
|
|
167
|
+
* considered to contain only comments.
|
|
168
|
+
*
|
|
169
|
+
* @param fileContent - The string content of the file to check
|
|
170
|
+
* @returns True if the file contains only comments and whitespace, false if it contains any actual code
|
|
171
|
+
*
|
|
172
|
+
* @example
|
|
173
|
+
* // Returns true
|
|
174
|
+
* hasOnlyComments('// Just a comment');
|
|
175
|
+
*
|
|
176
|
+
* // Returns false
|
|
177
|
+
* hasOnlyComments('// A comment\nconst x = 1;');
|
|
178
|
+
*/
|
|
179
|
+
function hasOnlyComments(fileContent) {
|
|
180
|
+
// Create a source file
|
|
181
|
+
const sourceFile = (0, typescript_1.createSourceFile)('temp.ts', fileContent, typescript_1.ScriptTarget.Latest, true);
|
|
182
|
+
// Remove all comments and whitespace
|
|
183
|
+
const textWithoutComments = sourceFile
|
|
184
|
+
.getFullText()
|
|
185
|
+
.replace(/\/\*[\s\S]*?\*\/|\/\/.*/g, '') // Remove comments
|
|
186
|
+
.replace(/\s+/g, ''); // Remove whitespace
|
|
187
|
+
// If there's any content left after removing comments and whitespace,
|
|
188
|
+
// then there's actual code
|
|
189
|
+
return textWithoutComments.length === 0;
|
|
190
|
+
}
|