@grafana/openapi-to-k6 0.1.2 → 0.2.0
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 -0
- package/.github/workflows/tests.yaml +14 -6
- package/README.md +49 -14
- package/dist/cli.js +18 -2
- 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/cli.ts +28 -2
- 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 +3 -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
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Automatically generated by @grafana/openapi-to-k6: 0.2.0
|
|
3
|
+
* Do not edit manually.
|
|
4
|
+
* Example API
|
|
5
|
+
* API with all formats of data in the POST request body
|
|
6
|
+
* OpenAPI spec version: 1.0.0
|
|
7
|
+
*/
|
|
8
|
+
import { URL } from 'https://jslib.k6.io/url/1.0.0/index.js'
|
|
9
|
+
import http from 'k6/http'
|
|
10
|
+
import type { Params, Response } from 'k6/http'
|
|
11
|
+
import type {
|
|
12
|
+
CreateExampleData201,
|
|
13
|
+
CreateExampleDataBody,
|
|
14
|
+
} from './exampleAPI.schemas'
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* This is the base client to use for interacting with the API.
|
|
18
|
+
*/
|
|
19
|
+
export class ExampleAPIClient {
|
|
20
|
+
private cleanBaseUrl: string
|
|
21
|
+
private commonRequestParameters: Params
|
|
22
|
+
|
|
23
|
+
constructor(clientOptions: {
|
|
24
|
+
baseUrl: string
|
|
25
|
+
commonRequestParameters?: Params
|
|
26
|
+
}) {
|
|
27
|
+
this.cleanBaseUrl = clientOptions.baseUrl.replace(/\/+$/, '')
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* This endpoint demonstrates the use of various data formats in the input body
|
|
32
|
+
* @summary Create example data
|
|
33
|
+
*/
|
|
34
|
+
createExampleData(
|
|
35
|
+
createExampleDataBody: CreateExampleDataBody,
|
|
36
|
+
requestParameters?: Params
|
|
37
|
+
): {
|
|
38
|
+
response: Response
|
|
39
|
+
data: CreateExampleData201
|
|
40
|
+
} {
|
|
41
|
+
const url = new URL(this.cleanBaseUrl + `/example`)
|
|
42
|
+
const mergedRequestParameters = this._mergeRequestParameters(
|
|
43
|
+
requestParameters || {},
|
|
44
|
+
this.commonRequestParameters
|
|
45
|
+
)
|
|
46
|
+
const response = http.request(
|
|
47
|
+
'POST',
|
|
48
|
+
url.toString(),
|
|
49
|
+
JSON.stringify(createExampleDataBody),
|
|
50
|
+
{
|
|
51
|
+
...mergedRequestParameters,
|
|
52
|
+
headers: {
|
|
53
|
+
'Content-Type': 'application/json',
|
|
54
|
+
...mergedRequestParameters?.headers,
|
|
55
|
+
},
|
|
56
|
+
}
|
|
57
|
+
)
|
|
58
|
+
let data
|
|
59
|
+
|
|
60
|
+
try {
|
|
61
|
+
data = response.json()
|
|
62
|
+
} catch {
|
|
63
|
+
data = response.body
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
response,
|
|
67
|
+
data,
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Merges the provided request parameters with default parameters for the client.
|
|
73
|
+
*
|
|
74
|
+
* @param {Params} requestParameters - The parameters provided specifically for the request
|
|
75
|
+
* @param {Params} commonRequestParameters - Common parameters for all requests
|
|
76
|
+
* @returns {Params} - The merged parameters
|
|
77
|
+
*/
|
|
78
|
+
private _mergeRequestParameters(
|
|
79
|
+
requestParameters?: Params,
|
|
80
|
+
commonRequestParameters?: Params
|
|
81
|
+
): Params {
|
|
82
|
+
return {
|
|
83
|
+
...commonRequestParameters, // Default to common parameters
|
|
84
|
+
...requestParameters, // Override with request-specific parameters
|
|
85
|
+
headers: {
|
|
86
|
+
...(commonRequestParameters?.headers || {}), // Ensure headers are defined
|
|
87
|
+
...(requestParameters?.headers || {}),
|
|
88
|
+
},
|
|
89
|
+
cookies: {
|
|
90
|
+
...(commonRequestParameters?.cookies || {}), // Ensure cookies are defined
|
|
91
|
+
...(requestParameters?.cookies || {}),
|
|
92
|
+
},
|
|
93
|
+
tags: {
|
|
94
|
+
...(commonRequestParameters?.tags || {}), // Ensure tags are defined
|
|
95
|
+
...(requestParameters?.tags || {}),
|
|
96
|
+
},
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { ExampleAPIClient } from './exampleAPI.ts'
|
|
2
|
+
|
|
3
|
+
const baseUrl = '<BASE_URL>'
|
|
4
|
+
const client = new ExampleAPIClient({ baseUrl })
|
|
5
|
+
|
|
6
|
+
export default function () {
|
|
7
|
+
/**
|
|
8
|
+
* Create example data
|
|
9
|
+
*/
|
|
10
|
+
const createExampleDataResponseData = client.createExampleData(
|
|
11
|
+
createExampleDataBody
|
|
12
|
+
)
|
|
13
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Automatically generated by @grafana/openapi-to-k6: 0.2.0
|
|
3
|
+
* Do not edit manually.
|
|
4
|
+
* Example API
|
|
5
|
+
* API with all formats of data in the POST request body
|
|
6
|
+
* OpenAPI spec version: 1.0.0
|
|
7
|
+
*/
|
|
8
|
+
import { URL } from 'https://jslib.k6.io/url/1.0.0/index.js'
|
|
9
|
+
import http from 'k6/http'
|
|
10
|
+
import type { Params, Response } from 'k6/http'
|
|
11
|
+
import type {
|
|
12
|
+
CreateExampleData201,
|
|
13
|
+
CreateExampleDataBody,
|
|
14
|
+
} from './exampleAPI.schemas'
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* This is the base client to use for interacting with the API.
|
|
18
|
+
*/
|
|
19
|
+
export class DefaultClient {
|
|
20
|
+
private cleanBaseUrl: string
|
|
21
|
+
private commonRequestParameters: Params
|
|
22
|
+
|
|
23
|
+
constructor(clientOptions: {
|
|
24
|
+
baseUrl: string
|
|
25
|
+
commonRequestParameters?: Params
|
|
26
|
+
}) {
|
|
27
|
+
this.cleanBaseUrl = clientOptions.baseUrl.replace(/\/+$/, '')
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* This endpoint demonstrates the use of various data formats in the input body
|
|
32
|
+
* @summary Create example data
|
|
33
|
+
*/
|
|
34
|
+
createExampleData(
|
|
35
|
+
createExampleDataBody: CreateExampleDataBody,
|
|
36
|
+
requestParameters?: Params
|
|
37
|
+
): {
|
|
38
|
+
response: Response
|
|
39
|
+
data: CreateExampleData201
|
|
40
|
+
} {
|
|
41
|
+
const url = new URL(this.cleanBaseUrl + `/example`)
|
|
42
|
+
const mergedRequestParameters = this._mergeRequestParameters(
|
|
43
|
+
requestParameters || {},
|
|
44
|
+
this.commonRequestParameters
|
|
45
|
+
)
|
|
46
|
+
const response = http.request(
|
|
47
|
+
'POST',
|
|
48
|
+
url.toString(),
|
|
49
|
+
JSON.stringify(createExampleDataBody),
|
|
50
|
+
{
|
|
51
|
+
...mergedRequestParameters,
|
|
52
|
+
headers: {
|
|
53
|
+
'Content-Type': 'application/json',
|
|
54
|
+
...mergedRequestParameters?.headers,
|
|
55
|
+
},
|
|
56
|
+
}
|
|
57
|
+
)
|
|
58
|
+
let data
|
|
59
|
+
|
|
60
|
+
try {
|
|
61
|
+
data = response.json()
|
|
62
|
+
} catch {
|
|
63
|
+
data = response.body
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
response,
|
|
67
|
+
data,
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Merges the provided request parameters with default parameters for the client.
|
|
73
|
+
*
|
|
74
|
+
* @param {Params} requestParameters - The parameters provided specifically for the request
|
|
75
|
+
* @param {Params} commonRequestParameters - Common parameters for all requests
|
|
76
|
+
* @returns {Params} - The merged parameters
|
|
77
|
+
*/
|
|
78
|
+
private _mergeRequestParameters(
|
|
79
|
+
requestParameters?: Params,
|
|
80
|
+
commonRequestParameters?: Params
|
|
81
|
+
): Params {
|
|
82
|
+
return {
|
|
83
|
+
...commonRequestParameters, // Default to common parameters
|
|
84
|
+
...requestParameters, // Override with request-specific parameters
|
|
85
|
+
headers: {
|
|
86
|
+
...(commonRequestParameters?.headers || {}), // Ensure headers are defined
|
|
87
|
+
...(requestParameters?.headers || {}),
|
|
88
|
+
},
|
|
89
|
+
cookies: {
|
|
90
|
+
...(commonRequestParameters?.cookies || {}), // Ensure cookies are defined
|
|
91
|
+
...(requestParameters?.cookies || {}),
|
|
92
|
+
},
|
|
93
|
+
tags: {
|
|
94
|
+
...(commonRequestParameters?.tags || {}), // Ensure tags are defined
|
|
95
|
+
...(requestParameters?.tags || {}),
|
|
96
|
+
},
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Automatically generated by @grafana/openapi-to-k6: 0.2.0
|
|
3
|
+
* Do not edit manually.
|
|
4
|
+
* Example API
|
|
5
|
+
* API with all formats of data in the POST request body
|
|
6
|
+
* OpenAPI spec version: 1.0.0
|
|
7
|
+
*/
|
|
8
|
+
export type CreateExampleData201Meta = {
|
|
9
|
+
createdBy?: string
|
|
10
|
+
updatedBy?: string
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type CreateExampleData201 = {
|
|
14
|
+
age?: number
|
|
15
|
+
date?: string
|
|
16
|
+
/** The unique ID of the created resource */
|
|
17
|
+
id?: string
|
|
18
|
+
isActive?: boolean
|
|
19
|
+
meta?: CreateExampleData201Meta
|
|
20
|
+
name?: string
|
|
21
|
+
tags?: string[]
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* An object parameter containing metadata
|
|
26
|
+
*/
|
|
27
|
+
export type CreateExampleDataBodyMeta = {
|
|
28
|
+
/** A string parameter for the creator's name */
|
|
29
|
+
createdBy?: string
|
|
30
|
+
/** A string parameter for the updater's name */
|
|
31
|
+
updatedBy?: string
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export type CreateExampleDataBody = {
|
|
35
|
+
/** An integer parameter */
|
|
36
|
+
age: number
|
|
37
|
+
/** A date parameter in YYYY-MM-DD format */
|
|
38
|
+
date?: string
|
|
39
|
+
/** A boolean parameter */
|
|
40
|
+
isActive: boolean
|
|
41
|
+
/** An object parameter containing metadata */
|
|
42
|
+
meta?: CreateExampleDataBodyMeta
|
|
43
|
+
/** A string parameter */
|
|
44
|
+
name: string
|
|
45
|
+
/** An array of strings */
|
|
46
|
+
tags?: string[]
|
|
47
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { ExampleAPIClient } from './exampleAPI.ts'
|
|
2
|
+
|
|
3
|
+
const baseUrl = '<BASE_URL>'
|
|
4
|
+
const client = new ExampleAPIClient({ baseUrl })
|
|
5
|
+
|
|
6
|
+
export default function () {
|
|
7
|
+
/**
|
|
8
|
+
* Create example data
|
|
9
|
+
*/
|
|
10
|
+
const createExampleDataResponseData = client.createExampleData(
|
|
11
|
+
createExampleDataBody
|
|
12
|
+
)
|
|
13
|
+
}
|
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@grafana/openapi-to-k6",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "A CLI tool to generate helper modules for K6 from OpenAPI schema",
|
|
5
5
|
"main": "dist/cli.js",
|
|
6
6
|
"bin": {
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"lint": "eslint --ext .ts,.tsx .",
|
|
14
14
|
"lint:fix": "eslint --ext .ts,.tsx . --fix",
|
|
15
15
|
"format": "prettier --ignore-unknown --write \"./**/*\"",
|
|
16
|
-
"update-examples": "for dir in ./examples/*/; do (npm run dev -- \"$dir/schema.json\" \"$dir\"); done",
|
|
16
|
+
"update-examples": "for dir in ./examples/*/; do (npm run dev -- \"$dir/schema.json\" \"$dir/single/\" -m single && npm run dev -- \"$dir/schema.json\" \"$dir/split/\" -m split && npm run dev -- \"$dir/schema.json\" \"$dir/tags/\" -m tags); done",
|
|
17
17
|
"prepare": "husky"
|
|
18
18
|
},
|
|
19
19
|
"author": "",
|
package/src/cli.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
import chalk from 'chalk'
|
|
4
|
-
import { Command } from 'commander'
|
|
4
|
+
import { Command, InvalidArgumentError } from 'commander'
|
|
5
5
|
import { generateDefaultAnalyticsData, reportUsageAnalytics } from './analytics'
|
|
6
|
+
import { Mode } from './constants'
|
|
6
7
|
import generateK6SDK from './generator'
|
|
7
8
|
import { getPackageDetails } from './helper'
|
|
8
9
|
import { logger } from './logger'
|
|
@@ -11,11 +12,27 @@ import { AnalyticsData, GenerateK6SDKOptions } from './type'
|
|
|
11
12
|
const program = new Command()
|
|
12
13
|
const packageDetails = getPackageDetails()
|
|
13
14
|
|
|
15
|
+
/**
|
|
16
|
+
* Validate that the mode argument is one of the supported modes.
|
|
17
|
+
*
|
|
18
|
+
* @param {string} value - The mode value to validate
|
|
19
|
+
* @return {Mode} - The validated mode value
|
|
20
|
+
*/
|
|
21
|
+
function validateMode(value: string): Mode {
|
|
22
|
+
if (!Object.values(Mode).includes(value as Mode)) {
|
|
23
|
+
throw new InvalidArgumentError(
|
|
24
|
+
`Supported modes: ${Object.values(Mode).join(', ')}`
|
|
25
|
+
)
|
|
26
|
+
}
|
|
27
|
+
return value as Mode
|
|
28
|
+
}
|
|
29
|
+
|
|
14
30
|
async function generateSDK({
|
|
15
31
|
openApiPath,
|
|
16
32
|
outputDir,
|
|
17
33
|
shouldGenerateSampleK6Script,
|
|
18
34
|
analyticsData,
|
|
35
|
+
mode,
|
|
19
36
|
}: GenerateK6SDKOptions) {
|
|
20
37
|
logger.logMessage('Generating TypeScript client for k6...\n')
|
|
21
38
|
logger.logMessage(`OpenAPI schema: ${openApiPath}`)
|
|
@@ -26,6 +43,7 @@ async function generateSDK({
|
|
|
26
43
|
outputDir,
|
|
27
44
|
shouldGenerateSampleK6Script,
|
|
28
45
|
analyticsData,
|
|
46
|
+
mode,
|
|
29
47
|
})
|
|
30
48
|
|
|
31
49
|
if (shouldGenerateSampleK6Script) {
|
|
@@ -44,8 +62,14 @@ program
|
|
|
44
62
|
.version(packageDetails.version)
|
|
45
63
|
.argument('<openApiPath>', 'Path or URL for the OpenAPI schema file')
|
|
46
64
|
.argument('<outputDir>', 'Directory where the SDK should be generated')
|
|
65
|
+
.option(
|
|
66
|
+
'-m, --mode <string>',
|
|
67
|
+
`mode to use for generating the client. Valid values - ${Object.values(Mode).join(', ')}`,
|
|
68
|
+
validateMode,
|
|
69
|
+
Mode.SINGLE
|
|
70
|
+
)
|
|
47
71
|
.option('-v, --verbose', 'enable verbose mode to show debug logs')
|
|
48
|
-
.option('--
|
|
72
|
+
.option('--disable-sample-script', 'disable generating sample k6 script')
|
|
49
73
|
.option('--disable-analytics', 'disable anonymous usage data collection')
|
|
50
74
|
.action(
|
|
51
75
|
async (
|
|
@@ -53,6 +77,7 @@ program
|
|
|
53
77
|
outputDir,
|
|
54
78
|
options: {
|
|
55
79
|
verbose?: boolean
|
|
80
|
+
mode: Mode
|
|
56
81
|
disableAnalytics?: boolean
|
|
57
82
|
disableSampleScript?: boolean
|
|
58
83
|
}
|
|
@@ -86,6 +111,7 @@ program
|
|
|
86
111
|
outputDir,
|
|
87
112
|
shouldGenerateSampleK6Script: !shouldDisableSampleScript,
|
|
88
113
|
analyticsData,
|
|
114
|
+
mode: options.mode,
|
|
89
115
|
})
|
|
90
116
|
} catch (error) {
|
|
91
117
|
logger.error('Failed to generate SDK:')
|
package/src/constants.ts
CHANGED
|
@@ -5,7 +5,7 @@ export const K6_SCRIPT_TEMPLATE = `
|
|
|
5
5
|
import { {{clientFunctionName}} } from '{{clientPath}}'
|
|
6
6
|
|
|
7
7
|
const baseUrl = '<BASE_URL>';
|
|
8
|
-
const client = {{clientFunctionName}}({ baseUrl })
|
|
8
|
+
const client = new {{clientFunctionName}}({ baseUrl })
|
|
9
9
|
|
|
10
10
|
export default function () {
|
|
11
11
|
{{#each clientFunctionsList}}
|
|
@@ -17,3 +17,9 @@ export default function () {
|
|
|
17
17
|
{{/each}}
|
|
18
18
|
}
|
|
19
19
|
`
|
|
20
|
+
|
|
21
|
+
export enum Mode {
|
|
22
|
+
SINGLE = 'single',
|
|
23
|
+
SPLIT = 'split',
|
|
24
|
+
TAGS = 'tags',
|
|
25
|
+
}
|
|
@@ -2,15 +2,15 @@ import fs from 'fs'
|
|
|
2
2
|
import { InfoObject } from 'openapi3-ts/oas30'
|
|
3
3
|
import orval from 'orval'
|
|
4
4
|
import path from 'path'
|
|
5
|
-
import { DEFAULT_SCHEMA_TITLE } from '
|
|
5
|
+
import { DEFAULT_SCHEMA_TITLE } from '../constants'
|
|
6
6
|
import {
|
|
7
7
|
formatFileWithPrettier,
|
|
8
8
|
getPackageDetails,
|
|
9
9
|
OutputOverrider,
|
|
10
|
-
} from '
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
10
|
+
} from '../helper'
|
|
11
|
+
import { logger } from '../logger'
|
|
12
|
+
import { GenerateK6SDKOptions } from '../type'
|
|
13
|
+
import { getK6ClientBuilder } from './k6Client'
|
|
14
14
|
|
|
15
15
|
const outputOverrider = OutputOverrider.getInstance()
|
|
16
16
|
const packageDetails = getPackageDetails()
|
|
@@ -49,11 +49,8 @@ export default async ({
|
|
|
49
49
|
outputDir,
|
|
50
50
|
shouldGenerateSampleK6Script,
|
|
51
51
|
analyticsData,
|
|
52
|
+
mode,
|
|
52
53
|
}: GenerateK6SDKOptions) => {
|
|
53
|
-
const schemaDetails: SchemaDetails = {
|
|
54
|
-
title: '',
|
|
55
|
-
}
|
|
56
|
-
|
|
57
54
|
/**
|
|
58
55
|
* Note!
|
|
59
56
|
* 1. override.requestOptions is not supported for the custom K6 client
|
|
@@ -64,13 +61,9 @@ export default async ({
|
|
|
64
61
|
input: openApiPath,
|
|
65
62
|
output: {
|
|
66
63
|
target: outputDir,
|
|
67
|
-
mode:
|
|
64
|
+
mode: mode,
|
|
68
65
|
client: () =>
|
|
69
|
-
getK6ClientBuilder(
|
|
70
|
-
schemaDetails,
|
|
71
|
-
shouldGenerateSampleK6Script,
|
|
72
|
-
analyticsData
|
|
73
|
-
),
|
|
66
|
+
getK6ClientBuilder(shouldGenerateSampleK6Script, analyticsData),
|
|
74
67
|
override: {
|
|
75
68
|
header: generatedFileHeaderGenerator,
|
|
76
69
|
},
|
|
@@ -81,10 +74,4 @@ export default async ({
|
|
|
81
74
|
},
|
|
82
75
|
})
|
|
83
76
|
})
|
|
84
|
-
|
|
85
|
-
if (!schemaDetails.title) {
|
|
86
|
-
logger.warning(
|
|
87
|
-
'Could not find schema title in the OpenAPI spec. Please provide a `title` in the schema in `info` block to generate proper file names'
|
|
88
|
-
)
|
|
89
|
-
}
|
|
90
77
|
}
|