@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
|
@@ -0,0 +1,37 @@
|
|
|
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 query parameters
|
|
6
|
+
* OpenAPI spec version: 1.0.0
|
|
7
|
+
*/
|
|
8
|
+
export type GetExampleData200 = {
|
|
9
|
+
age?: number
|
|
10
|
+
date?: string
|
|
11
|
+
isActive?: boolean
|
|
12
|
+
name?: string
|
|
13
|
+
tags?: string[]
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type GetExampleDataParams = {
|
|
17
|
+
/**
|
|
18
|
+
* A string parameter
|
|
19
|
+
*/
|
|
20
|
+
name: string
|
|
21
|
+
/**
|
|
22
|
+
* An integer parameter
|
|
23
|
+
*/
|
|
24
|
+
age?: number
|
|
25
|
+
/**
|
|
26
|
+
* A boolean parameter
|
|
27
|
+
*/
|
|
28
|
+
isActive?: boolean
|
|
29
|
+
/**
|
|
30
|
+
* An array of strings parameter
|
|
31
|
+
*/
|
|
32
|
+
tags?: string[]
|
|
33
|
+
/**
|
|
34
|
+
* A date parameter in YYYY-MM-DD format
|
|
35
|
+
*/
|
|
36
|
+
date?: string
|
|
37
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
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
|
+
* Get example data
|
|
9
|
+
*/
|
|
10
|
+
const getExampleDataResponseData = client.getExampleData(params)
|
|
11
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Automatically generated by @grafana/openapi-to-k6: 0.
|
|
2
|
+
* Automatically generated by @grafana/openapi-to-k6: 0.2.0
|
|
3
3
|
* Do not edit manually.
|
|
4
4
|
* Example API
|
|
5
5
|
* API with all formats of data in the POST request body
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { URL } from 'https://jslib.k6.io/url/1.0.0/index.js'
|
|
9
9
|
import http from 'k6/http'
|
|
10
|
-
import type { Params, Response
|
|
10
|
+
import type { Params, Response } from 'k6/http'
|
|
11
11
|
export type CreateExampleData201Meta = {
|
|
12
12
|
createdBy?: string
|
|
13
13
|
updatedBy?: string
|
|
@@ -49,28 +49,35 @@ export type CreateExampleDataBody = {
|
|
|
49
49
|
tags?: string[]
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
export type CreateExampleAPIOptions = {
|
|
53
|
-
baseUrl: string
|
|
54
|
-
commonRequestParameters?: Params
|
|
55
|
-
}
|
|
56
|
-
|
|
57
52
|
/**
|
|
58
53
|
* This is the base client to use for interacting with the API.
|
|
59
54
|
*/
|
|
60
|
-
export
|
|
61
|
-
|
|
55
|
+
export class ExampleAPIClient {
|
|
56
|
+
private cleanBaseUrl: string
|
|
57
|
+
private commonRequestParameters: Params
|
|
58
|
+
|
|
59
|
+
constructor(clientOptions: {
|
|
60
|
+
baseUrl: string
|
|
61
|
+
commonRequestParameters?: Params
|
|
62
|
+
}) {
|
|
63
|
+
this.cleanBaseUrl = clientOptions.baseUrl.replace(/\/+$/, '')
|
|
64
|
+
}
|
|
65
|
+
|
|
62
66
|
/**
|
|
63
67
|
* This endpoint demonstrates the use of various data formats in the input body
|
|
64
68
|
* @summary Create example data
|
|
65
69
|
*/
|
|
66
|
-
|
|
70
|
+
createExampleData(
|
|
67
71
|
createExampleDataBody: CreateExampleDataBody,
|
|
68
72
|
requestParameters?: Params
|
|
69
|
-
):
|
|
70
|
-
|
|
71
|
-
|
|
73
|
+
): {
|
|
74
|
+
response: Response
|
|
75
|
+
data: CreateExampleData201
|
|
76
|
+
} {
|
|
77
|
+
const url = new URL(this.cleanBaseUrl + `/example`)
|
|
78
|
+
const mergedRequestParameters = this._mergeRequestParameters(
|
|
72
79
|
requestParameters || {},
|
|
73
|
-
|
|
80
|
+
this.commonRequestParameters
|
|
74
81
|
)
|
|
75
82
|
const response = http.request(
|
|
76
83
|
'POST',
|
|
@@ -88,7 +95,7 @@ export const createExampleAPI = (clientOptions: CreateExampleAPIOptions) => {
|
|
|
88
95
|
|
|
89
96
|
try {
|
|
90
97
|
data = response.json()
|
|
91
|
-
} catch
|
|
98
|
+
} catch {
|
|
92
99
|
data = response.body
|
|
93
100
|
}
|
|
94
101
|
return {
|
|
@@ -97,39 +104,32 @@ export const createExampleAPI = (clientOptions: CreateExampleAPIOptions) => {
|
|
|
97
104
|
}
|
|
98
105
|
}
|
|
99
106
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
...(commonRequestParameters?.cookies || {}), // Ensure cookies are defined
|
|
128
|
-
...(requestParameters?.cookies || {}),
|
|
129
|
-
},
|
|
130
|
-
tags: {
|
|
131
|
-
...(commonRequestParameters?.tags || {}), // Ensure tags are defined
|
|
132
|
-
...(requestParameters?.tags || {}),
|
|
133
|
-
},
|
|
107
|
+
/**
|
|
108
|
+
* Merges the provided request parameters with default parameters for the client.
|
|
109
|
+
*
|
|
110
|
+
* @param {Params} requestParameters - The parameters provided specifically for the request
|
|
111
|
+
* @param {Params} commonRequestParameters - Common parameters for all requests
|
|
112
|
+
* @returns {Params} - The merged parameters
|
|
113
|
+
*/
|
|
114
|
+
private _mergeRequestParameters(
|
|
115
|
+
requestParameters?: Params,
|
|
116
|
+
commonRequestParameters?: Params
|
|
117
|
+
): Params {
|
|
118
|
+
return {
|
|
119
|
+
...commonRequestParameters, // Default to common parameters
|
|
120
|
+
...requestParameters, // Override with request-specific parameters
|
|
121
|
+
headers: {
|
|
122
|
+
...(commonRequestParameters?.headers || {}), // Ensure headers are defined
|
|
123
|
+
...(requestParameters?.headers || {}),
|
|
124
|
+
},
|
|
125
|
+
cookies: {
|
|
126
|
+
...(commonRequestParameters?.cookies || {}), // Ensure cookies are defined
|
|
127
|
+
...(requestParameters?.cookies || {}),
|
|
128
|
+
},
|
|
129
|
+
tags: {
|
|
130
|
+
...(commonRequestParameters?.tags || {}), // Ensure tags are defined
|
|
131
|
+
...(requestParameters?.tags || {}),
|
|
132
|
+
},
|
|
133
|
+
}
|
|
134
134
|
}
|
|
135
135
|
}
|
package/examples/simple_post_request_schema/{k6-script.sample.ts → single/k6-script.sample.ts}
RENAMED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ExampleAPIClient } from './exampleAPI.ts'
|
|
2
2
|
|
|
3
3
|
const baseUrl = '<BASE_URL>'
|
|
4
|
-
const client =
|
|
4
|
+
const client = new ExampleAPIClient({ baseUrl })
|
|
5
5
|
|
|
6
6
|
export default function () {
|
|
7
7
|
/**
|
|
@@ -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,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.1
|
|
3
|
+
"version": "0.2.1",
|
|
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 --include-sample-script && npm run dev -- \"$dir/schema.json\" \"$dir/split/\" -m split --include-sample-script && npm run dev -- \"$dir/schema.json\" \"$dir/tags/\" -m tags --include-sample-script); done",
|
|
17
17
|
"prepare": "husky"
|
|
18
18
|
},
|
|
19
19
|
"author": "",
|
package/src/analytics.ts
CHANGED
|
@@ -26,7 +26,8 @@ function getAnonymousUserId(): string {
|
|
|
26
26
|
* @returns
|
|
27
27
|
*/
|
|
28
28
|
export function generateDefaultAnalyticsData(
|
|
29
|
-
packageDetails: PackageDetails
|
|
29
|
+
packageDetails: PackageDetails,
|
|
30
|
+
isSampleK6ScriptGenerated: boolean
|
|
30
31
|
): AnalyticsData {
|
|
31
32
|
const defaultAnalyticsData: AnalyticsData = {
|
|
32
33
|
generatedRequestsCount: {
|
|
@@ -37,6 +38,7 @@ export function generateDefaultAnalyticsData(
|
|
|
37
38
|
delete: 0,
|
|
38
39
|
head: 0,
|
|
39
40
|
},
|
|
41
|
+
isSampleK6ScriptGenerated,
|
|
40
42
|
openApiSpecVersion: '',
|
|
41
43
|
toolVersion: packageDetails.version,
|
|
42
44
|
anonymousUserId: getAnonymousUserId(),
|
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('--include-sample-script', 'generate a sample k6 script')
|
|
49
73
|
.option('--disable-analytics', 'disable anonymous usage data collection')
|
|
50
74
|
.action(
|
|
51
75
|
async (
|
|
@@ -53,16 +77,14 @@ program
|
|
|
53
77
|
outputDir,
|
|
54
78
|
options: {
|
|
55
79
|
verbose?: boolean
|
|
80
|
+
mode: Mode
|
|
56
81
|
disableAnalytics?: boolean
|
|
57
|
-
|
|
82
|
+
includeSampleScript?: boolean
|
|
58
83
|
}
|
|
59
84
|
) => {
|
|
60
85
|
let analyticsData: AnalyticsData | undefined
|
|
61
86
|
const shouldDisableAnalytics =
|
|
62
87
|
options.disableAnalytics || process.env.DISABLE_ANALYTICS === 'true'
|
|
63
|
-
const shouldDisableSampleScript =
|
|
64
|
-
options.disableSampleScript ||
|
|
65
|
-
process.env.DISABLE_SAMPLE_SCRIPT === 'true'
|
|
66
88
|
|
|
67
89
|
if (options.verbose) {
|
|
68
90
|
logger.setVerbose(true)
|
|
@@ -73,7 +95,10 @@ program
|
|
|
73
95
|
logger.debug('Anonymous usage data collection disabled')
|
|
74
96
|
} else {
|
|
75
97
|
logger.debug('Anonymous usage data collection enabled')
|
|
76
|
-
analyticsData = generateDefaultAnalyticsData(
|
|
98
|
+
analyticsData = generateDefaultAnalyticsData(
|
|
99
|
+
packageDetails,
|
|
100
|
+
!!options.includeSampleScript
|
|
101
|
+
)
|
|
77
102
|
}
|
|
78
103
|
|
|
79
104
|
logger.debug(`
|
|
@@ -84,8 +109,9 @@ program
|
|
|
84
109
|
await generateSDK({
|
|
85
110
|
openApiPath,
|
|
86
111
|
outputDir,
|
|
87
|
-
shouldGenerateSampleK6Script:
|
|
112
|
+
shouldGenerateSampleK6Script: !!options.includeSampleScript,
|
|
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
|
+
}
|