@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.
Files changed (121) hide show
  1. package/.github/workflows/publish.yaml +2 -2
  2. package/.github/workflows/tests.yaml +14 -6
  3. package/README.md +54 -14
  4. package/dist/analytics.js +2 -1
  5. package/dist/cli.js +20 -6
  6. package/dist/constants.js +8 -2
  7. package/dist/{generator.js → generator/index.js} +7 -13
  8. package/dist/{k6SdkClient.js → generator/k6Client.js} +148 -161
  9. package/examples/basic_schema/single/k6-script.sample.ts +11 -0
  10. package/examples/basic_schema/single/simpleAPI.ts +87 -0
  11. package/examples/basic_schema/split/k6-script.sample.ts +11 -0
  12. package/examples/basic_schema/split/simpleAPI.schemas.ts +9 -0
  13. package/examples/basic_schema/split/simpleAPI.ts +85 -0
  14. package/examples/basic_schema/tags/default.ts +85 -0
  15. package/examples/basic_schema/tags/k6-script.sample.ts +11 -0
  16. package/examples/basic_schema/tags/simpleAPI.schemas.ts +9 -0
  17. package/examples/form_data_schema/single/formDataAPI.ts +115 -0
  18. package/examples/form_data_schema/{k6-script.sample.ts → single/k6-script.sample.ts} +2 -2
  19. package/examples/form_data_schema/split/formDataAPI.schemas.ts +24 -0
  20. package/examples/form_data_schema/split/formDataAPI.ts +98 -0
  21. package/examples/form_data_schema/split/k6-script.sample.ts +11 -0
  22. package/examples/form_data_schema/tags/default.ts +98 -0
  23. package/examples/form_data_schema/tags/formDataAPI.schemas.ts +24 -0
  24. package/examples/form_data_schema/tags/k6-script.sample.ts +11 -0
  25. package/examples/form_url_encoded_data_schema/single/formURLEncodedAPI.ts +112 -0
  26. package/examples/form_url_encoded_data_schema/{k6-script.sample.ts → single/k6-script.sample.ts} +2 -2
  27. package/examples/form_url_encoded_data_schema/split/formURLEncodedAPI.schemas.ts +24 -0
  28. package/examples/form_url_encoded_data_schema/split/formURLEncodedAPI.ts +98 -0
  29. package/examples/form_url_encoded_data_schema/split/k6-script.sample.ts +11 -0
  30. package/examples/form_url_encoded_data_schema/tags/default.ts +98 -0
  31. package/examples/form_url_encoded_data_schema/tags/formURLEncodedAPI.schemas.ts +24 -0
  32. package/examples/form_url_encoded_data_schema/tags/k6-script.sample.ts +11 -0
  33. package/examples/form_url_encoded_data_with_query_params_schema/single/formURLEncodedAPIWithQueryParameters.ts +128 -0
  34. package/examples/form_url_encoded_data_with_query_params_schema/{k6-script.sample.ts → single/k6-script.sample.ts} +2 -2
  35. package/examples/form_url_encoded_data_with_query_params_schema/split/formURLEncodedAPIWithQueryParameters.schemas.ts +35 -0
  36. package/examples/form_url_encoded_data_with_query_params_schema/split/formURLEncodedAPIWithQueryParameters.ts +104 -0
  37. package/examples/form_url_encoded_data_with_query_params_schema/split/k6-script.sample.ts +14 -0
  38. package/examples/form_url_encoded_data_with_query_params_schema/tags/default.ts +104 -0
  39. package/examples/form_url_encoded_data_with_query_params_schema/tags/formURLEncodedAPIWithQueryParameters.schemas.ts +35 -0
  40. package/examples/form_url_encoded_data_with_query_params_schema/tags/k6-script.sample.ts +14 -0
  41. package/examples/get_request_with_path_parameters_schema/{k6-script.sample.ts → single/k6-script.sample.ts} +2 -2
  42. package/examples/get_request_with_path_parameters_schema/single/simpleAPI.ts +94 -0
  43. package/examples/get_request_with_path_parameters_schema/split/k6-script.sample.ts +11 -0
  44. package/examples/get_request_with_path_parameters_schema/split/simpleAPI.schemas.ts +12 -0
  45. package/examples/get_request_with_path_parameters_schema/split/simpleAPI.ts +90 -0
  46. package/examples/get_request_with_path_parameters_schema/tags/default.ts +90 -0
  47. package/examples/get_request_with_path_parameters_schema/tags/k6-script.sample.ts +11 -0
  48. package/examples/get_request_with_path_parameters_schema/tags/simpleAPI.schemas.ts +12 -0
  49. package/examples/headers_schema/single/headerDemoAPI.ts +202 -0
  50. package/examples/headers_schema/{k6-script.sample.ts → single/k6-script.sample.ts} +2 -2
  51. package/examples/headers_schema/split/headerDemoAPI.schemas.ts +32 -0
  52. package/examples/headers_schema/split/headerDemoAPI.ts +184 -0
  53. package/examples/headers_schema/split/k6-script.sample.ts +25 -0
  54. package/examples/headers_schema/tags/default.ts +182 -0
  55. package/examples/headers_schema/tags/headerDemoAPI.schemas.ts +32 -0
  56. package/examples/headers_schema/tags/k6-script.sample.ts +25 -0
  57. package/examples/no_title_schema/{k6-script.sample.ts → single/k6-script.sample.ts} +2 -2
  58. package/examples/no_title_schema/single/k6Client.ts +87 -0
  59. package/examples/{basic_schema → no_title_schema/split}/k6-script.sample.ts +2 -2
  60. package/examples/no_title_schema/split/k6Client.schemas.ts +9 -0
  61. package/examples/no_title_schema/split/k6Client.ts +85 -0
  62. package/examples/no_title_schema/tags/default.ts +85 -0
  63. package/examples/no_title_schema/tags/k6-script.sample.ts +11 -0
  64. package/examples/no_title_schema/tags/k6Client.schemas.ts +9 -0
  65. package/examples/post_request_with_query_params/single/exampleAPI.ts +126 -0
  66. package/examples/post_request_with_query_params/{k6-script.sample.ts → single/k6-script.sample.ts} +2 -2
  67. package/examples/post_request_with_query_params/split/exampleAPI.schemas.ts +33 -0
  68. package/examples/post_request_with_query_params/split/exampleAPI.ts +105 -0
  69. package/examples/post_request_with_query_params/split/k6-script.sample.ts +14 -0
  70. package/examples/post_request_with_query_params/tags/default.ts +105 -0
  71. package/examples/post_request_with_query_params/tags/exampleAPI.schemas.ts +33 -0
  72. package/examples/post_request_with_query_params/tags/k6-script.sample.ts +14 -0
  73. package/examples/query_params_schema/single/exampleAPI.ts +120 -0
  74. package/examples/query_params_schema/{k6-script.sample.ts → single/k6-script.sample.ts} +2 -2
  75. package/examples/query_params_schema/split/exampleAPI.schemas.ts +37 -0
  76. package/examples/query_params_schema/split/exampleAPI.ts +94 -0
  77. package/examples/query_params_schema/split/k6-script.sample.ts +11 -0
  78. package/examples/query_params_schema/tags/default.ts +94 -0
  79. package/examples/query_params_schema/tags/exampleAPI.schemas.ts +37 -0
  80. package/examples/query_params_schema/tags/k6-script.sample.ts +11 -0
  81. package/examples/simple_post_request_schema/{exampleAPI.ts → single/exampleAPI.ts} +49 -49
  82. package/examples/simple_post_request_schema/{k6-script.sample.ts → single/k6-script.sample.ts} +2 -2
  83. package/examples/simple_post_request_schema/split/exampleAPI.schemas.ts +47 -0
  84. package/examples/simple_post_request_schema/split/exampleAPI.ts +99 -0
  85. package/examples/simple_post_request_schema/split/k6-script.sample.ts +13 -0
  86. package/examples/simple_post_request_schema/tags/default.ts +99 -0
  87. package/examples/simple_post_request_schema/tags/exampleAPI.schemas.ts +47 -0
  88. package/examples/simple_post_request_schema/tags/k6-script.sample.ts +13 -0
  89. package/images/openapi-to-k6.png +0 -0
  90. package/package.json +2 -2
  91. package/src/analytics.ts +3 -1
  92. package/src/cli.ts +34 -8
  93. package/src/constants.ts +7 -1
  94. package/src/{generator.ts → generator/index.ts} +8 -21
  95. package/src/{k6SdkClient.ts → generator/k6Client.ts} +174 -222
  96. package/src/type.d.ts +4 -4
  97. package/tests/e2e/schema.json +8 -0
  98. package/tests/e2e/{k6Script.ts → single/k6Script.ts} +7 -2
  99. package/tests/e2e/split/k6Script.ts +82 -0
  100. package/tests/e2e/tags/k6Script.ts +106 -0
  101. package/tests/functional-tests/fixtures/schemas/basic_schema.json +1 -4
  102. package/tests/functional-tests/fixtures/schemas/form_data_schema.json +4 -4
  103. package/tests/functional-tests/fixtures/schemas/form_url_encoded_data_schema.json +3 -3
  104. package/tests/functional-tests/fixtures/schemas/form_url_encoded_data_with_query_params_schema.json +2 -2
  105. package/tests/functional-tests/fixtures/schemas/get_request_with_path_parameters_schema.json +2 -2
  106. package/tests/functional-tests/fixtures/schemas/headers_schema.json +7 -8
  107. package/tests/functional-tests/fixtures/schemas/no_title_schema.json +2 -5
  108. package/tests/functional-tests/fixtures/schemas/post_request_with_query_params.json +3 -4
  109. package/tests/functional-tests/fixtures/schemas/query_params_schema.json +3 -3
  110. package/tests/functional-tests/fixtures/schemas/simple_post_request_schema.json +3 -5
  111. package/tests/functional-tests/generator.test.ts +46 -5
  112. package/examples/basic_schema/simpleAPI.ts +0 -87
  113. package/examples/form_data_schema/formDataAPI.ts +0 -115
  114. package/examples/form_url_encoded_data_schema/formURLEncodedAPI.ts +0 -114
  115. package/examples/form_url_encoded_data_with_query_params_schema/formURLEncodedAPIWithQueryParameters.ts +0 -130
  116. package/examples/get_request_with_path_parameters_schema/simpleAPI.ts +0 -94
  117. package/examples/headers_schema/headerDemoAPI.ts +0 -196
  118. package/examples/no_title_schema/K6Client.ts +0 -86
  119. package/examples/post_request_with_query_params/exampleAPI.ts +0 -124
  120. package/examples/query_params_schema/exampleAPI.ts +0 -118
  121. 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.1.2
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, ResponseBody } from 'k6/http'
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 const createExampleAPI = (clientOptions: CreateExampleAPIOptions) => {
61
- const cleanBaseUrl = clientOptions.baseUrl.replace(/\/+$/, '')
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
- const createExampleData = (
70
+ createExampleData(
67
71
  createExampleDataBody: CreateExampleDataBody,
68
72
  requestParameters?: Params
69
- ): CreateExampleDataResponse => {
70
- const url = new URL(cleanBaseUrl + `/example`)
71
- const mergedRequestParameters = _mergeRequestParameters(
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
- clientOptions.commonRequestParameters
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 (error) {
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
- return { createExampleData }
101
- }
102
-
103
- export type CreateExampleDataResponse = {
104
- response: Response
105
- data: CreateExampleData201 | ResponseBody
106
- }
107
-
108
- /**
109
- * Merges the provided request parameters with default parameters for the client.
110
- *
111
- * @param {Params} requestParameters - The parameters provided specifically for the request
112
- * @param {Params} commonRequestParameters - Common parameters for all requests
113
- * @returns {Params} - The merged parameters
114
- */
115
- const _mergeRequestParameters = (
116
- requestParameters?: Params,
117
- commonRequestParameters?: Params
118
- ): Params => {
119
- return {
120
- ...commonRequestParameters, // Default to common parameters
121
- ...requestParameters, // Override with request-specific parameters
122
- headers: {
123
- ...(commonRequestParameters?.headers || {}), // Ensure headers are defined
124
- ...(requestParameters?.headers || {}),
125
- },
126
- cookies: {
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
  }
@@ -1,7 +1,7 @@
1
- import { createExampleAPI } from './exampleAPI.ts'
1
+ import { ExampleAPIClient } from './exampleAPI.ts'
2
2
 
3
3
  const baseUrl = '<BASE_URL>'
4
- const client = createExampleAPI({ baseUrl })
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.2",
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('--no-sample-script', 'disable generating sample k6 script')
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
- disableSampleScript?: boolean
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(packageDetails)
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: !shouldDisableSampleScript,
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
+ }