@grafana/openapi-to-k6 0.1.2 → 0.2.1
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/publish.yaml +2 -2
- package/.github/workflows/tests.yaml +14 -6
- package/README.md +54 -14
- package/dist/analytics.js +2 -1
- package/dist/cli.js +20 -6
- package/dist/constants.js +8 -2
- package/dist/{generator.js → generator/index.js} +7 -13
- package/dist/{k6SdkClient.js → generator/k6Client.js} +148 -161
- package/examples/basic_schema/single/k6-script.sample.ts +11 -0
- package/examples/basic_schema/single/simpleAPI.ts +87 -0
- package/examples/basic_schema/split/k6-script.sample.ts +11 -0
- package/examples/basic_schema/split/simpleAPI.schemas.ts +9 -0
- package/examples/basic_schema/split/simpleAPI.ts +85 -0
- package/examples/basic_schema/tags/default.ts +85 -0
- package/examples/basic_schema/tags/k6-script.sample.ts +11 -0
- package/examples/basic_schema/tags/simpleAPI.schemas.ts +9 -0
- package/examples/form_data_schema/single/formDataAPI.ts +115 -0
- package/examples/form_data_schema/{k6-script.sample.ts → single/k6-script.sample.ts} +2 -2
- package/examples/form_data_schema/split/formDataAPI.schemas.ts +24 -0
- package/examples/form_data_schema/split/formDataAPI.ts +98 -0
- package/examples/form_data_schema/split/k6-script.sample.ts +11 -0
- package/examples/form_data_schema/tags/default.ts +98 -0
- package/examples/form_data_schema/tags/formDataAPI.schemas.ts +24 -0
- package/examples/form_data_schema/tags/k6-script.sample.ts +11 -0
- package/examples/form_url_encoded_data_schema/single/formURLEncodedAPI.ts +112 -0
- package/examples/form_url_encoded_data_schema/{k6-script.sample.ts → single/k6-script.sample.ts} +2 -2
- package/examples/form_url_encoded_data_schema/split/formURLEncodedAPI.schemas.ts +24 -0
- package/examples/form_url_encoded_data_schema/split/formURLEncodedAPI.ts +98 -0
- package/examples/form_url_encoded_data_schema/split/k6-script.sample.ts +11 -0
- package/examples/form_url_encoded_data_schema/tags/default.ts +98 -0
- package/examples/form_url_encoded_data_schema/tags/formURLEncodedAPI.schemas.ts +24 -0
- package/examples/form_url_encoded_data_schema/tags/k6-script.sample.ts +11 -0
- package/examples/form_url_encoded_data_with_query_params_schema/single/formURLEncodedAPIWithQueryParameters.ts +128 -0
- package/examples/form_url_encoded_data_with_query_params_schema/{k6-script.sample.ts → single/k6-script.sample.ts} +2 -2
- package/examples/form_url_encoded_data_with_query_params_schema/split/formURLEncodedAPIWithQueryParameters.schemas.ts +35 -0
- package/examples/form_url_encoded_data_with_query_params_schema/split/formURLEncodedAPIWithQueryParameters.ts +104 -0
- package/examples/form_url_encoded_data_with_query_params_schema/split/k6-script.sample.ts +14 -0
- package/examples/form_url_encoded_data_with_query_params_schema/tags/default.ts +104 -0
- package/examples/form_url_encoded_data_with_query_params_schema/tags/formURLEncodedAPIWithQueryParameters.schemas.ts +35 -0
- package/examples/form_url_encoded_data_with_query_params_schema/tags/k6-script.sample.ts +14 -0
- package/examples/get_request_with_path_parameters_schema/{k6-script.sample.ts → single/k6-script.sample.ts} +2 -2
- package/examples/get_request_with_path_parameters_schema/single/simpleAPI.ts +94 -0
- package/examples/get_request_with_path_parameters_schema/split/k6-script.sample.ts +11 -0
- package/examples/get_request_with_path_parameters_schema/split/simpleAPI.schemas.ts +12 -0
- package/examples/get_request_with_path_parameters_schema/split/simpleAPI.ts +90 -0
- package/examples/get_request_with_path_parameters_schema/tags/default.ts +90 -0
- package/examples/get_request_with_path_parameters_schema/tags/k6-script.sample.ts +11 -0
- package/examples/get_request_with_path_parameters_schema/tags/simpleAPI.schemas.ts +12 -0
- package/examples/headers_schema/single/headerDemoAPI.ts +202 -0
- package/examples/headers_schema/{k6-script.sample.ts → single/k6-script.sample.ts} +2 -2
- package/examples/headers_schema/split/headerDemoAPI.schemas.ts +32 -0
- package/examples/headers_schema/split/headerDemoAPI.ts +184 -0
- package/examples/headers_schema/split/k6-script.sample.ts +25 -0
- package/examples/headers_schema/tags/default.ts +182 -0
- package/examples/headers_schema/tags/headerDemoAPI.schemas.ts +32 -0
- package/examples/headers_schema/tags/k6-script.sample.ts +25 -0
- package/examples/no_title_schema/{k6-script.sample.ts → single/k6-script.sample.ts} +2 -2
- package/examples/no_title_schema/single/k6Client.ts +87 -0
- package/examples/{basic_schema → no_title_schema/split}/k6-script.sample.ts +2 -2
- package/examples/no_title_schema/split/k6Client.schemas.ts +9 -0
- package/examples/no_title_schema/split/k6Client.ts +85 -0
- package/examples/no_title_schema/tags/default.ts +85 -0
- package/examples/no_title_schema/tags/k6-script.sample.ts +11 -0
- package/examples/no_title_schema/tags/k6Client.schemas.ts +9 -0
- package/examples/post_request_with_query_params/single/exampleAPI.ts +126 -0
- package/examples/post_request_with_query_params/{k6-script.sample.ts → single/k6-script.sample.ts} +2 -2
- package/examples/post_request_with_query_params/split/exampleAPI.schemas.ts +33 -0
- package/examples/post_request_with_query_params/split/exampleAPI.ts +105 -0
- package/examples/post_request_with_query_params/split/k6-script.sample.ts +14 -0
- package/examples/post_request_with_query_params/tags/default.ts +105 -0
- package/examples/post_request_with_query_params/tags/exampleAPI.schemas.ts +33 -0
- package/examples/post_request_with_query_params/tags/k6-script.sample.ts +14 -0
- package/examples/query_params_schema/single/exampleAPI.ts +120 -0
- package/examples/query_params_schema/{k6-script.sample.ts → single/k6-script.sample.ts} +2 -2
- package/examples/query_params_schema/split/exampleAPI.schemas.ts +37 -0
- package/examples/query_params_schema/split/exampleAPI.ts +94 -0
- package/examples/query_params_schema/split/k6-script.sample.ts +11 -0
- package/examples/query_params_schema/tags/default.ts +94 -0
- package/examples/query_params_schema/tags/exampleAPI.schemas.ts +37 -0
- package/examples/query_params_schema/tags/k6-script.sample.ts +11 -0
- package/examples/simple_post_request_schema/{exampleAPI.ts → single/exampleAPI.ts} +49 -49
- package/examples/simple_post_request_schema/{k6-script.sample.ts → single/k6-script.sample.ts} +2 -2
- package/examples/simple_post_request_schema/split/exampleAPI.schemas.ts +47 -0
- package/examples/simple_post_request_schema/split/exampleAPI.ts +99 -0
- package/examples/simple_post_request_schema/split/k6-script.sample.ts +13 -0
- package/examples/simple_post_request_schema/tags/default.ts +99 -0
- package/examples/simple_post_request_schema/tags/exampleAPI.schemas.ts +47 -0
- package/examples/simple_post_request_schema/tags/k6-script.sample.ts +13 -0
- package/images/openapi-to-k6.png +0 -0
- package/package.json +2 -2
- package/src/analytics.ts +3 -1
- package/src/cli.ts +34 -8
- package/src/constants.ts +7 -1
- package/src/{generator.ts → generator/index.ts} +8 -21
- package/src/{k6SdkClient.ts → generator/k6Client.ts} +174 -222
- package/src/type.d.ts +4 -4
- package/tests/e2e/schema.json +8 -0
- package/tests/e2e/{k6Script.ts → single/k6Script.ts} +7 -2
- package/tests/e2e/split/k6Script.ts +82 -0
- package/tests/e2e/tags/k6Script.ts +106 -0
- package/tests/functional-tests/fixtures/schemas/basic_schema.json +1 -4
- package/tests/functional-tests/fixtures/schemas/form_data_schema.json +4 -4
- package/tests/functional-tests/fixtures/schemas/form_url_encoded_data_schema.json +3 -3
- package/tests/functional-tests/fixtures/schemas/form_url_encoded_data_with_query_params_schema.json +2 -2
- package/tests/functional-tests/fixtures/schemas/get_request_with_path_parameters_schema.json +2 -2
- package/tests/functional-tests/fixtures/schemas/headers_schema.json +7 -8
- package/tests/functional-tests/fixtures/schemas/no_title_schema.json +2 -5
- package/tests/functional-tests/fixtures/schemas/post_request_with_query_params.json +3 -4
- package/tests/functional-tests/fixtures/schemas/query_params_schema.json +3 -3
- package/tests/functional-tests/fixtures/schemas/simple_post_request_schema.json +3 -5
- package/tests/functional-tests/generator.test.ts +46 -5
- package/examples/basic_schema/simpleAPI.ts +0 -87
- package/examples/form_data_schema/formDataAPI.ts +0 -115
- package/examples/form_url_encoded_data_schema/formURLEncodedAPI.ts +0 -114
- package/examples/form_url_encoded_data_with_query_params_schema/formURLEncodedAPIWithQueryParameters.ts +0 -130
- package/examples/get_request_with_path_parameters_schema/simpleAPI.ts +0 -94
- package/examples/headers_schema/headerDemoAPI.ts +0 -196
- package/examples/no_title_schema/K6Client.ts +0 -86
- package/examples/post_request_with_query_params/exampleAPI.ts +0 -124
- package/examples/query_params_schema/exampleAPI.ts +0 -118
- package/examples/update_examples.sh +0 -21
|
@@ -12,17 +12,119 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
12
12
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
13
|
};
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.generateFooter = exports.generateK6Header = exports.generateTitle = exports.getK6Dependencies = void 0;
|
|
16
15
|
exports.getK6ClientBuilder = getK6ClientBuilder;
|
|
17
16
|
const core_1 = require("@orval/core");
|
|
18
17
|
const handlebars_1 = __importDefault(require("handlebars"));
|
|
19
18
|
const path_1 = __importDefault(require("path"));
|
|
20
|
-
const constants_1 = require("
|
|
21
|
-
const helper_1 = require("
|
|
22
|
-
const logger_1 = require("
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
19
|
+
const constants_1 = require("../constants");
|
|
20
|
+
const helper_1 = require("../helper");
|
|
21
|
+
const logger_1 = require("../logger");
|
|
22
|
+
/**
|
|
23
|
+
* In case the supplied schema does not have a title set, it will set the default title to ensure
|
|
24
|
+
* proper client generation
|
|
25
|
+
*
|
|
26
|
+
* @param context - The context object containing the schema details
|
|
27
|
+
*/
|
|
28
|
+
function _setDefaultSchemaTitle(context) {
|
|
29
|
+
const schemaDetails = context.specs[context.specKey];
|
|
30
|
+
if (schemaDetails && !schemaDetails.info.title) {
|
|
31
|
+
schemaDetails.info.title = constants_1.DEFAULT_SCHEMA_TITLE;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function _generateResponseTypeDefinition(response) {
|
|
35
|
+
let responseDataType = '';
|
|
36
|
+
if (response.definition.success &&
|
|
37
|
+
!['any', 'unknown'].includes(response.definition.success)) {
|
|
38
|
+
responseDataType += response.definition.success;
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
responseDataType += 'ResponseBody';
|
|
42
|
+
}
|
|
43
|
+
return `{
|
|
44
|
+
response: Response
|
|
45
|
+
data: ${responseDataType}
|
|
46
|
+
}`;
|
|
47
|
+
}
|
|
48
|
+
function _getRequestParametersMergerFunctionImplementation() {
|
|
49
|
+
return `/**
|
|
50
|
+
* Merges the provided request parameters with default parameters for the client.
|
|
51
|
+
*
|
|
52
|
+
* @param {Params} requestParameters - The parameters provided specifically for the request
|
|
53
|
+
* @param {Params} commonRequestParameters - Common parameters for all requests
|
|
54
|
+
* @returns {Params} - The merged parameters
|
|
55
|
+
*/
|
|
56
|
+
private _mergeRequestParameters (requestParameters?: Params, commonRequestParameters?: Params): Params {
|
|
57
|
+
return {
|
|
58
|
+
...commonRequestParameters, // Default to common parameters
|
|
59
|
+
...requestParameters, // Override with request-specific parameters
|
|
60
|
+
headers: {
|
|
61
|
+
...commonRequestParameters?.headers || {}, // Ensure headers are defined
|
|
62
|
+
...requestParameters?.headers || {},
|
|
63
|
+
},
|
|
64
|
+
cookies: {
|
|
65
|
+
...commonRequestParameters?.cookies || {}, // Ensure cookies are defined
|
|
66
|
+
...requestParameters?.cookies || {},
|
|
67
|
+
},
|
|
68
|
+
tags: {
|
|
69
|
+
...commonRequestParameters?.tags || {}, // Ensure tags are defined
|
|
70
|
+
...requestParameters?.tags || {},
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
};`;
|
|
74
|
+
}
|
|
75
|
+
const _getRequestParamsValue = ({ response, queryParams, headers, body, }) => {
|
|
76
|
+
if (!queryParams && !headers && !response.isBlob && !body.contentType) {
|
|
77
|
+
// No parameters to merge, return the request parameters directly
|
|
78
|
+
return 'mergedRequestParameters';
|
|
79
|
+
}
|
|
80
|
+
let value = '\n ...mergedRequestParameters,';
|
|
81
|
+
if (response.isBlob) {
|
|
82
|
+
value += `\n responseType: 'binary',`;
|
|
83
|
+
}
|
|
84
|
+
// Expand the headers
|
|
85
|
+
if (body.contentType || headers) {
|
|
86
|
+
let headersValue = `\n headers: {`;
|
|
87
|
+
if (body.contentType) {
|
|
88
|
+
if (body.formData) {
|
|
89
|
+
headersValue += `\n'Content-Type': '${body.contentType}; boundary=' + formData.boundary,`;
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
headersValue += `\n'Content-Type': '${body.contentType}',`;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (headers) {
|
|
96
|
+
headersValue += `\n// In the schema, headers can be of any type like number but k6 accepts only strings as headers, hence converting all headers to string`;
|
|
97
|
+
headersValue += `\n...Object.fromEntries(Object.entries(headers || {}).map(([key, value]) => [key, String(value)])),`;
|
|
98
|
+
}
|
|
99
|
+
headersValue += `\n...mergedRequestParameters?.headers},`;
|
|
100
|
+
value += headersValue;
|
|
101
|
+
}
|
|
102
|
+
return `{${value}}`;
|
|
103
|
+
};
|
|
104
|
+
const _getK6RequestOptions = (verbOptions) => {
|
|
105
|
+
const { body, headers, queryParams, response, verb } = verbOptions;
|
|
106
|
+
let fetchBodyOption = 'undefined';
|
|
107
|
+
if (body.formData) {
|
|
108
|
+
// Use the FormData.body() method to get the body of the request
|
|
109
|
+
fetchBodyOption = 'formData.body()';
|
|
110
|
+
}
|
|
111
|
+
else if (body.formUrlEncoded || body.implementation) {
|
|
112
|
+
fetchBodyOption = `JSON.stringify(${body.implementation})`;
|
|
113
|
+
}
|
|
114
|
+
// Generate the params input for the call
|
|
115
|
+
const requestParametersValue = _getRequestParamsValue({
|
|
116
|
+
response,
|
|
117
|
+
body,
|
|
118
|
+
headers: headers === null || headers === void 0 ? void 0 : headers.schema,
|
|
119
|
+
queryParams: queryParams === null || queryParams === void 0 ? void 0 : queryParams.schema,
|
|
120
|
+
});
|
|
121
|
+
// Sample output
|
|
122
|
+
// 'GET', 'http://test.com/route', <body>, <options>
|
|
123
|
+
return `"${verb.toUpperCase()}",
|
|
124
|
+
url.toString(),
|
|
125
|
+
${fetchBodyOption},
|
|
126
|
+
${requestParametersValue}`;
|
|
127
|
+
};
|
|
26
128
|
const getK6Dependencies = () => [
|
|
27
129
|
{
|
|
28
130
|
exports: [
|
|
@@ -67,32 +169,8 @@ const getK6Dependencies = () => [
|
|
|
67
169
|
dependency: 'https://jslib.k6.io/formdata/0.0.2/index.js',
|
|
68
170
|
},
|
|
69
171
|
];
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
const specData = Object.values(context.specs);
|
|
73
|
-
let schemaTitle;
|
|
74
|
-
if (specData[0]) {
|
|
75
|
-
schemaTitle = specData[0].info.title;
|
|
76
|
-
}
|
|
77
|
-
schemaTitle !== null && schemaTitle !== void 0 ? schemaTitle : (schemaTitle = constants_1.DEFAULT_SCHEMA_TITLE);
|
|
78
|
-
return schemaTitle;
|
|
79
|
-
}
|
|
80
|
-
function _generateResponseTypeName(operationName) {
|
|
81
|
-
return `${(0, core_1.pascal)(operationName)}Response`;
|
|
82
|
-
}
|
|
83
|
-
function _generateResponseTypeDefinition(operationName, response) {
|
|
84
|
-
const typeName = _generateResponseTypeName(operationName);
|
|
85
|
-
let responseDataType = '';
|
|
86
|
-
if (response.definition.success) {
|
|
87
|
-
responseDataType += response.definition.success + ' | ';
|
|
88
|
-
}
|
|
89
|
-
responseDataType += 'ResponseBody';
|
|
90
|
-
return `export type ${typeName} = {
|
|
91
|
-
response: Response
|
|
92
|
-
data: ${responseDataType}
|
|
93
|
-
};`;
|
|
94
|
-
}
|
|
95
|
-
const generateK6Implementation = ({ headers, queryParams, operationName, response, body, props, verb, override, formData, formUrlEncoded, paramsSerializer, }, { route }, analyticsData) => {
|
|
172
|
+
const generateK6Implementation = (verbOptions, { route }, analyticsData) => {
|
|
173
|
+
const { queryParams, operationName, response, body, props, verb, formData, formUrlEncoded, } = verbOptions;
|
|
96
174
|
if (analyticsData) {
|
|
97
175
|
analyticsData.generatedRequestsCount[verb] += 1;
|
|
98
176
|
}
|
|
@@ -103,33 +181,21 @@ const generateK6Implementation = ({ headers, queryParams, operationName, respons
|
|
|
103
181
|
isFormData: true,
|
|
104
182
|
isFormUrlEncoded: false,
|
|
105
183
|
});
|
|
106
|
-
|
|
107
|
-
returnTypesToWrite.set(operationName, _generateResponseTypeDefinition(operationName, response));
|
|
108
|
-
let url = `cleanBaseUrl + \`${route}\``;
|
|
184
|
+
let url = `this.cleanBaseUrl + \`${route}\``;
|
|
109
185
|
if (queryParams) {
|
|
110
186
|
url += '+`?${new URLSearchParams(params).toString()}`';
|
|
111
187
|
}
|
|
112
188
|
const urlGeneration = `const url = new URL(${url});`;
|
|
113
|
-
const options =
|
|
114
|
-
|
|
115
|
-
body,
|
|
116
|
-
headers,
|
|
117
|
-
queryParams,
|
|
118
|
-
response,
|
|
119
|
-
verb,
|
|
120
|
-
requestOptions: override === null || override === void 0 ? void 0 : override.requestOptions,
|
|
121
|
-
paramsSerializer,
|
|
122
|
-
paramsSerializerOptions: override === null || override === void 0 ? void 0 : override.paramsSerializerOptions,
|
|
123
|
-
});
|
|
124
|
-
return `const ${operationName} = (\n ${(0, core_1.toObjectString)(props, 'implementation')} requestParameters?: Params): ${_generateResponseTypeName(operationName)} => {${bodyForm}
|
|
189
|
+
const options = _getK6RequestOptions(verbOptions);
|
|
190
|
+
return `${operationName}(\n ${(0, core_1.toObjectString)(props, 'implementation')} requestParameters?: Params): ${_generateResponseTypeDefinition(response)} {\n${bodyForm}
|
|
125
191
|
${urlGeneration}
|
|
126
|
-
const mergedRequestParameters = _mergeRequestParameters(requestParameters || {},
|
|
192
|
+
const mergedRequestParameters = this._mergeRequestParameters(requestParameters || {}, this.commonRequestParameters);
|
|
127
193
|
const response = http.request(${options});
|
|
128
194
|
let data;
|
|
129
195
|
|
|
130
196
|
try {
|
|
131
197
|
data = response.json();
|
|
132
|
-
} catch
|
|
198
|
+
} catch {
|
|
133
199
|
data = response.body;
|
|
134
200
|
}
|
|
135
201
|
return {
|
|
@@ -139,120 +205,41 @@ const generateK6Implementation = ({ headers, queryParams, operationName, respons
|
|
|
139
205
|
}
|
|
140
206
|
`;
|
|
141
207
|
};
|
|
142
|
-
const getParamsInputValue = ({ response, queryParams, headers, body, }) => {
|
|
143
|
-
if (!queryParams && !headers && !response.isBlob && !body.contentType) {
|
|
144
|
-
// No parameters to merge, return the request parameters directly
|
|
145
|
-
return 'mergedRequestParameters';
|
|
146
|
-
}
|
|
147
|
-
let value = '\n ...mergedRequestParameters,';
|
|
148
|
-
if (response.isBlob) {
|
|
149
|
-
value += `\n responseType: 'binary',`;
|
|
150
|
-
}
|
|
151
|
-
// Expand the headers
|
|
152
|
-
if (body.contentType || headers) {
|
|
153
|
-
let headersValue = `\n headers: {`;
|
|
154
|
-
if (body.contentType) {
|
|
155
|
-
if (body.formData) {
|
|
156
|
-
headersValue += `\n'Content-Type': '${body.contentType}; boundary=' + formData.boundary,`;
|
|
157
|
-
}
|
|
158
|
-
else {
|
|
159
|
-
headersValue += `\n'Content-Type': '${body.contentType}',`;
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
if (headers) {
|
|
163
|
-
headersValue += `\n...headers,`;
|
|
164
|
-
}
|
|
165
|
-
headersValue += `\n...mergedRequestParameters?.headers},`;
|
|
166
|
-
value += headersValue;
|
|
167
|
-
}
|
|
168
|
-
return `{${value}}`;
|
|
169
|
-
};
|
|
170
|
-
const getK6RequestOptions = (options) => {
|
|
171
|
-
const { body, headers, queryParams, response, verb } = options;
|
|
172
|
-
let fetchBodyOption = 'undefined';
|
|
173
|
-
if (body.formData) {
|
|
174
|
-
// Use the FormData.body() method to get the body of the request
|
|
175
|
-
fetchBodyOption = 'formData.body()';
|
|
176
|
-
}
|
|
177
|
-
else if (body.formUrlEncoded || body.implementation) {
|
|
178
|
-
fetchBodyOption = `JSON.stringify(${body.implementation})`;
|
|
179
|
-
}
|
|
180
|
-
// Generate the params input for the call
|
|
181
|
-
const paramsValue = getParamsInputValue({
|
|
182
|
-
response,
|
|
183
|
-
body,
|
|
184
|
-
headers: headers === null || headers === void 0 ? void 0 : headers.schema,
|
|
185
|
-
queryParams: queryParams === null || queryParams === void 0 ? void 0 : queryParams.schema,
|
|
186
|
-
});
|
|
187
|
-
// Sample output
|
|
188
|
-
// 'GET', 'http://test.com/route', <body>, <options>
|
|
189
|
-
return `"${verb.toUpperCase()}",
|
|
190
|
-
url.toString(),
|
|
191
|
-
${fetchBodyOption},
|
|
192
|
-
${paramsValue}`;
|
|
193
|
-
};
|
|
194
|
-
function _getRequestParametersMergerFunctionImplementation() {
|
|
195
|
-
return `/**
|
|
196
|
-
* Merges the provided request parameters with default parameters for the client.
|
|
197
|
-
*
|
|
198
|
-
* @param {Params} requestParameters - The parameters provided specifically for the request
|
|
199
|
-
* @param {Params} commonRequestParameters - Common parameters for all requests
|
|
200
|
-
* @returns {Params} - The merged parameters
|
|
201
|
-
*/
|
|
202
|
-
const _mergeRequestParameters = (requestParameters?: Params, commonRequestParameters?: Params): Params => {
|
|
203
|
-
return {
|
|
204
|
-
...commonRequestParameters, // Default to common parameters
|
|
205
|
-
...requestParameters, // Override with request-specific parameters
|
|
206
|
-
headers: {
|
|
207
|
-
...commonRequestParameters?.headers || {}, // Ensure headers are defined
|
|
208
|
-
...requestParameters?.headers || {},
|
|
209
|
-
},
|
|
210
|
-
cookies: {
|
|
211
|
-
...commonRequestParameters?.cookies || {}, // Ensure cookies are defined
|
|
212
|
-
...requestParameters?.cookies || {},
|
|
213
|
-
},
|
|
214
|
-
tags: {
|
|
215
|
-
...commonRequestParameters?.tags || {}, // Ensure tags are defined
|
|
216
|
-
...requestParameters?.tags || {},
|
|
217
|
-
},
|
|
218
|
-
};
|
|
219
|
-
};`;
|
|
220
|
-
}
|
|
221
208
|
const generateTitle = (title) => {
|
|
222
209
|
const sanTitle = (0, core_1.sanitize)(title || constants_1.DEFAULT_SCHEMA_TITLE);
|
|
223
|
-
return
|
|
210
|
+
return `${(0, core_1.pascal)(sanTitle)}Client`;
|
|
224
211
|
};
|
|
225
|
-
exports.generateTitle = generateTitle;
|
|
226
212
|
const generateK6Header = ({ title }) => {
|
|
227
|
-
const clientOptionsTypeName = `${(0, core_1.pascal)(title)}Options`;
|
|
228
213
|
return `
|
|
229
|
-
|
|
214
|
+
/**
|
|
215
|
+
* This is the base client to use for interacting with the API.
|
|
216
|
+
*/
|
|
217
|
+
export class ${title} {
|
|
218
|
+
private cleanBaseUrl: string;
|
|
219
|
+
private commonRequestParameters: Params;
|
|
220
|
+
|
|
221
|
+
constructor (clientOptions: {
|
|
230
222
|
baseUrl: string,
|
|
231
223
|
commonRequestParameters?: Params
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
*/
|
|
237
|
-
export const ${title} = (clientOptions: ${clientOptionsTypeName}) => {\n
|
|
238
|
-
const cleanBaseUrl = clientOptions.baseUrl.replace(/\\/+$/, '');\n`;
|
|
224
|
+
}) {
|
|
225
|
+
this.cleanBaseUrl = clientOptions.baseUrl.replace(/\\/+$/, '');\n
|
|
226
|
+
}\n
|
|
227
|
+
`;
|
|
239
228
|
};
|
|
240
|
-
|
|
241
|
-
const generateFooter = ({ operationNames }) => {
|
|
242
|
-
let footer = '';
|
|
243
|
-
footer += `return {${operationNames.join(',')}}};\n\n`;
|
|
244
|
-
operationNames.forEach((operationName) => {
|
|
245
|
-
if (returnTypesToWrite.has(operationName)) {
|
|
246
|
-
footer += returnTypesToWrite.get(operationName) + '\n';
|
|
247
|
-
}
|
|
248
|
-
});
|
|
229
|
+
const generateFooter = () => {
|
|
249
230
|
// Add function definition for merging request parameters
|
|
250
|
-
footer
|
|
231
|
+
const footer = `
|
|
232
|
+
|
|
233
|
+
${_getRequestParametersMergerFunctionImplementation()}
|
|
234
|
+
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
`;
|
|
251
238
|
return footer;
|
|
252
239
|
};
|
|
253
|
-
exports.generateFooter = generateFooter;
|
|
254
240
|
const k6ScriptBuilder = (verbOptions, output, context) => __awaiter(void 0, void 0, void 0, function* () {
|
|
255
|
-
|
|
241
|
+
var _a;
|
|
242
|
+
const schemaTitle = ((_a = context.specs[context.specKey]) === null || _a === void 0 ? void 0 : _a.info.title) || constants_1.DEFAULT_SCHEMA_TITLE;
|
|
256
243
|
const { path: pathOfGeneratedClient, filename, extension, } = yield (0, helper_1.getGeneratedClientPath)(output.target, schemaTitle);
|
|
257
244
|
const directoryPath = (0, helper_1.getDirectoryForPath)(pathOfGeneratedClient);
|
|
258
245
|
const generateScriptPath = path_1.default.join(directoryPath, constants_1.SAMPLE_K6_SCRIPT_FILE_NAME);
|
|
@@ -275,7 +262,7 @@ const k6ScriptBuilder = (verbOptions, output, context) => __awaiter(void 0, void
|
|
|
275
262
|
});
|
|
276
263
|
}
|
|
277
264
|
const scriptContentData = {
|
|
278
|
-
clientFunctionName:
|
|
265
|
+
clientFunctionName: generateTitle(schemaTitle),
|
|
279
266
|
clientPath: `./${filename}${extension}`,
|
|
280
267
|
clientFunctionsList,
|
|
281
268
|
};
|
|
@@ -287,11 +274,11 @@ const k6ScriptBuilder = (verbOptions, output, context) => __awaiter(void 0, void
|
|
|
287
274
|
},
|
|
288
275
|
];
|
|
289
276
|
});
|
|
290
|
-
function getK6Client(
|
|
277
|
+
function getK6Client(analyticsData) {
|
|
291
278
|
return function (verbOptions, options) {
|
|
279
|
+
_setDefaultSchemaTitle(options.context);
|
|
292
280
|
const imports = (0, core_1.generateVerbImports)(verbOptions);
|
|
293
281
|
const implementation = generateK6Implementation(verbOptions, options, analyticsData);
|
|
294
|
-
schemaDetails.title = getSchemaTitleFromContext(options.context);
|
|
295
282
|
const specData = Object.values(options.context.specs);
|
|
296
283
|
if (specData[0]) {
|
|
297
284
|
if (analyticsData) {
|
|
@@ -301,13 +288,13 @@ function getK6Client(schemaDetails, analyticsData) {
|
|
|
301
288
|
return { implementation, imports };
|
|
302
289
|
};
|
|
303
290
|
}
|
|
304
|
-
function getK6ClientBuilder(
|
|
291
|
+
function getK6ClientBuilder(shouldGenerateSampleK6Script, analyticsData) {
|
|
305
292
|
return {
|
|
306
|
-
client: getK6Client(
|
|
307
|
-
header:
|
|
308
|
-
dependencies:
|
|
309
|
-
footer:
|
|
310
|
-
title:
|
|
293
|
+
client: getK6Client(analyticsData),
|
|
294
|
+
header: generateK6Header,
|
|
295
|
+
dependencies: getK6Dependencies,
|
|
296
|
+
footer: generateFooter,
|
|
297
|
+
title: generateTitle,
|
|
311
298
|
extraFiles: shouldGenerateSampleK6Script ? k6ScriptBuilder : undefined,
|
|
312
299
|
};
|
|
313
300
|
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { SimpleAPIClient } from './simpleAPI.ts'
|
|
2
|
+
|
|
3
|
+
const baseUrl = '<BASE_URL>'
|
|
4
|
+
const client = new SimpleAPIClient({ baseUrl })
|
|
5
|
+
|
|
6
|
+
export default function () {
|
|
7
|
+
/**
|
|
8
|
+
* Retrieve example data
|
|
9
|
+
*/
|
|
10
|
+
const getExampleResponseData = client.getExample()
|
|
11
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Automatically generated by @grafana/openapi-to-k6: 0.2.0
|
|
3
|
+
* Do not edit manually.
|
|
4
|
+
* Simple API
|
|
5
|
+
* OpenAPI spec version: 1.0.0
|
|
6
|
+
*/
|
|
7
|
+
import { URL } from 'https://jslib.k6.io/url/1.0.0/index.js'
|
|
8
|
+
import http from 'k6/http'
|
|
9
|
+
import type { Params, Response } from 'k6/http'
|
|
10
|
+
export type GetExample200 = {
|
|
11
|
+
message?: string
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* This is the base client to use for interacting with the API.
|
|
16
|
+
*/
|
|
17
|
+
export class SimpleAPIClient {
|
|
18
|
+
private cleanBaseUrl: string
|
|
19
|
+
private commonRequestParameters: Params
|
|
20
|
+
|
|
21
|
+
constructor(clientOptions: {
|
|
22
|
+
baseUrl: string
|
|
23
|
+
commonRequestParameters?: Params
|
|
24
|
+
}) {
|
|
25
|
+
this.cleanBaseUrl = clientOptions.baseUrl.replace(/\/+$/, '')
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @summary Retrieve example data
|
|
30
|
+
*/
|
|
31
|
+
getExample(requestParameters?: Params): {
|
|
32
|
+
response: Response
|
|
33
|
+
data: GetExample200
|
|
34
|
+
} {
|
|
35
|
+
const url = new URL(this.cleanBaseUrl + `/example`)
|
|
36
|
+
const mergedRequestParameters = this._mergeRequestParameters(
|
|
37
|
+
requestParameters || {},
|
|
38
|
+
this.commonRequestParameters
|
|
39
|
+
)
|
|
40
|
+
const response = http.request(
|
|
41
|
+
'GET',
|
|
42
|
+
url.toString(),
|
|
43
|
+
undefined,
|
|
44
|
+
mergedRequestParameters
|
|
45
|
+
)
|
|
46
|
+
let data
|
|
47
|
+
|
|
48
|
+
try {
|
|
49
|
+
data = response.json()
|
|
50
|
+
} catch {
|
|
51
|
+
data = response.body
|
|
52
|
+
}
|
|
53
|
+
return {
|
|
54
|
+
response,
|
|
55
|
+
data,
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Merges the provided request parameters with default parameters for the client.
|
|
61
|
+
*
|
|
62
|
+
* @param {Params} requestParameters - The parameters provided specifically for the request
|
|
63
|
+
* @param {Params} commonRequestParameters - Common parameters for all requests
|
|
64
|
+
* @returns {Params} - The merged parameters
|
|
65
|
+
*/
|
|
66
|
+
private _mergeRequestParameters(
|
|
67
|
+
requestParameters?: Params,
|
|
68
|
+
commonRequestParameters?: Params
|
|
69
|
+
): Params {
|
|
70
|
+
return {
|
|
71
|
+
...commonRequestParameters, // Default to common parameters
|
|
72
|
+
...requestParameters, // Override with request-specific parameters
|
|
73
|
+
headers: {
|
|
74
|
+
...(commonRequestParameters?.headers || {}), // Ensure headers are defined
|
|
75
|
+
...(requestParameters?.headers || {}),
|
|
76
|
+
},
|
|
77
|
+
cookies: {
|
|
78
|
+
...(commonRequestParameters?.cookies || {}), // Ensure cookies are defined
|
|
79
|
+
...(requestParameters?.cookies || {}),
|
|
80
|
+
},
|
|
81
|
+
tags: {
|
|
82
|
+
...(commonRequestParameters?.tags || {}), // Ensure tags are defined
|
|
83
|
+
...(requestParameters?.tags || {}),
|
|
84
|
+
},
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { SimpleAPIClient } from './simpleAPI.ts'
|
|
2
|
+
|
|
3
|
+
const baseUrl = '<BASE_URL>'
|
|
4
|
+
const client = new SimpleAPIClient({ baseUrl })
|
|
5
|
+
|
|
6
|
+
export default function () {
|
|
7
|
+
/**
|
|
8
|
+
* Retrieve example data
|
|
9
|
+
*/
|
|
10
|
+
const getExampleResponseData = client.getExample()
|
|
11
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Automatically generated by @grafana/openapi-to-k6: 0.2.0
|
|
3
|
+
* Do not edit manually.
|
|
4
|
+
* Simple API
|
|
5
|
+
* OpenAPI spec version: 1.0.0
|
|
6
|
+
*/
|
|
7
|
+
import { URL } from 'https://jslib.k6.io/url/1.0.0/index.js'
|
|
8
|
+
import http from 'k6/http'
|
|
9
|
+
import type { Params, Response } from 'k6/http'
|
|
10
|
+
import type { GetExample200 } from './simpleAPI.schemas'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* This is the base client to use for interacting with the API.
|
|
14
|
+
*/
|
|
15
|
+
export class SimpleAPIClient {
|
|
16
|
+
private cleanBaseUrl: string
|
|
17
|
+
private commonRequestParameters: Params
|
|
18
|
+
|
|
19
|
+
constructor(clientOptions: {
|
|
20
|
+
baseUrl: string
|
|
21
|
+
commonRequestParameters?: Params
|
|
22
|
+
}) {
|
|
23
|
+
this.cleanBaseUrl = clientOptions.baseUrl.replace(/\/+$/, '')
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @summary Retrieve example data
|
|
28
|
+
*/
|
|
29
|
+
getExample(requestParameters?: Params): {
|
|
30
|
+
response: Response
|
|
31
|
+
data: GetExample200
|
|
32
|
+
} {
|
|
33
|
+
const url = new URL(this.cleanBaseUrl + `/example`)
|
|
34
|
+
const mergedRequestParameters = this._mergeRequestParameters(
|
|
35
|
+
requestParameters || {},
|
|
36
|
+
this.commonRequestParameters
|
|
37
|
+
)
|
|
38
|
+
const response = http.request(
|
|
39
|
+
'GET',
|
|
40
|
+
url.toString(),
|
|
41
|
+
undefined,
|
|
42
|
+
mergedRequestParameters
|
|
43
|
+
)
|
|
44
|
+
let data
|
|
45
|
+
|
|
46
|
+
try {
|
|
47
|
+
data = response.json()
|
|
48
|
+
} catch {
|
|
49
|
+
data = response.body
|
|
50
|
+
}
|
|
51
|
+
return {
|
|
52
|
+
response,
|
|
53
|
+
data,
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Merges the provided request parameters with default parameters for the client.
|
|
59
|
+
*
|
|
60
|
+
* @param {Params} requestParameters - The parameters provided specifically for the request
|
|
61
|
+
* @param {Params} commonRequestParameters - Common parameters for all requests
|
|
62
|
+
* @returns {Params} - The merged parameters
|
|
63
|
+
*/
|
|
64
|
+
private _mergeRequestParameters(
|
|
65
|
+
requestParameters?: Params,
|
|
66
|
+
commonRequestParameters?: Params
|
|
67
|
+
): Params {
|
|
68
|
+
return {
|
|
69
|
+
...commonRequestParameters, // Default to common parameters
|
|
70
|
+
...requestParameters, // Override with request-specific parameters
|
|
71
|
+
headers: {
|
|
72
|
+
...(commonRequestParameters?.headers || {}), // Ensure headers are defined
|
|
73
|
+
...(requestParameters?.headers || {}),
|
|
74
|
+
},
|
|
75
|
+
cookies: {
|
|
76
|
+
...(commonRequestParameters?.cookies || {}), // Ensure cookies are defined
|
|
77
|
+
...(requestParameters?.cookies || {}),
|
|
78
|
+
},
|
|
79
|
+
tags: {
|
|
80
|
+
...(commonRequestParameters?.tags || {}), // Ensure tags are defined
|
|
81
|
+
...(requestParameters?.tags || {}),
|
|
82
|
+
},
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Automatically generated by @grafana/openapi-to-k6: 0.2.0
|
|
3
|
+
* Do not edit manually.
|
|
4
|
+
* Simple API
|
|
5
|
+
* OpenAPI spec version: 1.0.0
|
|
6
|
+
*/
|
|
7
|
+
import { URL } from 'https://jslib.k6.io/url/1.0.0/index.js'
|
|
8
|
+
import http from 'k6/http'
|
|
9
|
+
import type { Params, Response } from 'k6/http'
|
|
10
|
+
import type { GetExample200 } from './simpleAPI.schemas'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* This is the base client to use for interacting with the API.
|
|
14
|
+
*/
|
|
15
|
+
export class DefaultClient {
|
|
16
|
+
private cleanBaseUrl: string
|
|
17
|
+
private commonRequestParameters: Params
|
|
18
|
+
|
|
19
|
+
constructor(clientOptions: {
|
|
20
|
+
baseUrl: string
|
|
21
|
+
commonRequestParameters?: Params
|
|
22
|
+
}) {
|
|
23
|
+
this.cleanBaseUrl = clientOptions.baseUrl.replace(/\/+$/, '')
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @summary Retrieve example data
|
|
28
|
+
*/
|
|
29
|
+
getExample(requestParameters?: Params): {
|
|
30
|
+
response: Response
|
|
31
|
+
data: GetExample200
|
|
32
|
+
} {
|
|
33
|
+
const url = new URL(this.cleanBaseUrl + `/example`)
|
|
34
|
+
const mergedRequestParameters = this._mergeRequestParameters(
|
|
35
|
+
requestParameters || {},
|
|
36
|
+
this.commonRequestParameters
|
|
37
|
+
)
|
|
38
|
+
const response = http.request(
|
|
39
|
+
'GET',
|
|
40
|
+
url.toString(),
|
|
41
|
+
undefined,
|
|
42
|
+
mergedRequestParameters
|
|
43
|
+
)
|
|
44
|
+
let data
|
|
45
|
+
|
|
46
|
+
try {
|
|
47
|
+
data = response.json()
|
|
48
|
+
} catch {
|
|
49
|
+
data = response.body
|
|
50
|
+
}
|
|
51
|
+
return {
|
|
52
|
+
response,
|
|
53
|
+
data,
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Merges the provided request parameters with default parameters for the client.
|
|
59
|
+
*
|
|
60
|
+
* @param {Params} requestParameters - The parameters provided specifically for the request
|
|
61
|
+
* @param {Params} commonRequestParameters - Common parameters for all requests
|
|
62
|
+
* @returns {Params} - The merged parameters
|
|
63
|
+
*/
|
|
64
|
+
private _mergeRequestParameters(
|
|
65
|
+
requestParameters?: Params,
|
|
66
|
+
commonRequestParameters?: Params
|
|
67
|
+
): Params {
|
|
68
|
+
return {
|
|
69
|
+
...commonRequestParameters, // Default to common parameters
|
|
70
|
+
...requestParameters, // Override with request-specific parameters
|
|
71
|
+
headers: {
|
|
72
|
+
...(commonRequestParameters?.headers || {}), // Ensure headers are defined
|
|
73
|
+
...(requestParameters?.headers || {}),
|
|
74
|
+
},
|
|
75
|
+
cookies: {
|
|
76
|
+
...(commonRequestParameters?.cookies || {}), // Ensure cookies are defined
|
|
77
|
+
...(requestParameters?.cookies || {}),
|
|
78
|
+
},
|
|
79
|
+
tags: {
|
|
80
|
+
...(commonRequestParameters?.tags || {}), // Ensure tags are defined
|
|
81
|
+
...(requestParameters?.tags || {}),
|
|
82
|
+
},
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { SimpleAPIClient } from './simpleAPI.ts'
|
|
2
|
+
|
|
3
|
+
const baseUrl = '<BASE_URL>'
|
|
4
|
+
const client = new SimpleAPIClient({ baseUrl })
|
|
5
|
+
|
|
6
|
+
export default function () {
|
|
7
|
+
/**
|
|
8
|
+
* Retrieve example data
|
|
9
|
+
*/
|
|
10
|
+
const getExampleResponseData = client.getExample()
|
|
11
|
+
}
|