@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.
Files changed (119) hide show
  1. package/.github/workflows/publish.yaml +2 -0
  2. package/.github/workflows/tests.yaml +14 -6
  3. package/README.md +49 -14
  4. package/dist/cli.js +18 -2
  5. package/dist/constants.js +8 -2
  6. package/dist/{generator.js → generator/index.js} +7 -13
  7. package/dist/{k6SdkClient.js → generator/k6Client.js} +148 -161
  8. package/examples/basic_schema/single/k6-script.sample.ts +11 -0
  9. package/examples/basic_schema/single/simpleAPI.ts +87 -0
  10. package/examples/basic_schema/split/k6-script.sample.ts +11 -0
  11. package/examples/basic_schema/split/simpleAPI.schemas.ts +9 -0
  12. package/examples/basic_schema/split/simpleAPI.ts +85 -0
  13. package/examples/basic_schema/tags/default.ts +85 -0
  14. package/examples/basic_schema/tags/k6-script.sample.ts +11 -0
  15. package/examples/basic_schema/tags/simpleAPI.schemas.ts +9 -0
  16. package/examples/form_data_schema/single/formDataAPI.ts +115 -0
  17. package/examples/form_data_schema/{k6-script.sample.ts → single/k6-script.sample.ts} +2 -2
  18. package/examples/form_data_schema/split/formDataAPI.schemas.ts +24 -0
  19. package/examples/form_data_schema/split/formDataAPI.ts +98 -0
  20. package/examples/form_data_schema/split/k6-script.sample.ts +11 -0
  21. package/examples/form_data_schema/tags/default.ts +98 -0
  22. package/examples/form_data_schema/tags/formDataAPI.schemas.ts +24 -0
  23. package/examples/form_data_schema/tags/k6-script.sample.ts +11 -0
  24. package/examples/form_url_encoded_data_schema/single/formURLEncodedAPI.ts +112 -0
  25. package/examples/form_url_encoded_data_schema/{k6-script.sample.ts → single/k6-script.sample.ts} +2 -2
  26. package/examples/form_url_encoded_data_schema/split/formURLEncodedAPI.schemas.ts +24 -0
  27. package/examples/form_url_encoded_data_schema/split/formURLEncodedAPI.ts +98 -0
  28. package/examples/form_url_encoded_data_schema/split/k6-script.sample.ts +11 -0
  29. package/examples/form_url_encoded_data_schema/tags/default.ts +98 -0
  30. package/examples/form_url_encoded_data_schema/tags/formURLEncodedAPI.schemas.ts +24 -0
  31. package/examples/form_url_encoded_data_schema/tags/k6-script.sample.ts +11 -0
  32. package/examples/form_url_encoded_data_with_query_params_schema/single/formURLEncodedAPIWithQueryParameters.ts +128 -0
  33. package/examples/form_url_encoded_data_with_query_params_schema/{k6-script.sample.ts → single/k6-script.sample.ts} +2 -2
  34. package/examples/form_url_encoded_data_with_query_params_schema/split/formURLEncodedAPIWithQueryParameters.schemas.ts +35 -0
  35. package/examples/form_url_encoded_data_with_query_params_schema/split/formURLEncodedAPIWithQueryParameters.ts +104 -0
  36. package/examples/form_url_encoded_data_with_query_params_schema/split/k6-script.sample.ts +14 -0
  37. package/examples/form_url_encoded_data_with_query_params_schema/tags/default.ts +104 -0
  38. package/examples/form_url_encoded_data_with_query_params_schema/tags/formURLEncodedAPIWithQueryParameters.schemas.ts +35 -0
  39. package/examples/form_url_encoded_data_with_query_params_schema/tags/k6-script.sample.ts +14 -0
  40. package/examples/get_request_with_path_parameters_schema/{k6-script.sample.ts → single/k6-script.sample.ts} +2 -2
  41. package/examples/get_request_with_path_parameters_schema/single/simpleAPI.ts +94 -0
  42. package/examples/get_request_with_path_parameters_schema/split/k6-script.sample.ts +11 -0
  43. package/examples/get_request_with_path_parameters_schema/split/simpleAPI.schemas.ts +12 -0
  44. package/examples/get_request_with_path_parameters_schema/split/simpleAPI.ts +90 -0
  45. package/examples/get_request_with_path_parameters_schema/tags/default.ts +90 -0
  46. package/examples/get_request_with_path_parameters_schema/tags/k6-script.sample.ts +11 -0
  47. package/examples/get_request_with_path_parameters_schema/tags/simpleAPI.schemas.ts +12 -0
  48. package/examples/headers_schema/single/headerDemoAPI.ts +202 -0
  49. package/examples/headers_schema/{k6-script.sample.ts → single/k6-script.sample.ts} +2 -2
  50. package/examples/headers_schema/split/headerDemoAPI.schemas.ts +32 -0
  51. package/examples/headers_schema/split/headerDemoAPI.ts +184 -0
  52. package/examples/headers_schema/split/k6-script.sample.ts +25 -0
  53. package/examples/headers_schema/tags/default.ts +182 -0
  54. package/examples/headers_schema/tags/headerDemoAPI.schemas.ts +32 -0
  55. package/examples/headers_schema/tags/k6-script.sample.ts +25 -0
  56. package/examples/no_title_schema/{k6-script.sample.ts → single/k6-script.sample.ts} +2 -2
  57. package/examples/no_title_schema/single/k6Client.ts +87 -0
  58. package/examples/{basic_schema → no_title_schema/split}/k6-script.sample.ts +2 -2
  59. package/examples/no_title_schema/split/k6Client.schemas.ts +9 -0
  60. package/examples/no_title_schema/split/k6Client.ts +85 -0
  61. package/examples/no_title_schema/tags/default.ts +85 -0
  62. package/examples/no_title_schema/tags/k6-script.sample.ts +11 -0
  63. package/examples/no_title_schema/tags/k6Client.schemas.ts +9 -0
  64. package/examples/post_request_with_query_params/single/exampleAPI.ts +126 -0
  65. package/examples/post_request_with_query_params/{k6-script.sample.ts → single/k6-script.sample.ts} +2 -2
  66. package/examples/post_request_with_query_params/split/exampleAPI.schemas.ts +33 -0
  67. package/examples/post_request_with_query_params/split/exampleAPI.ts +105 -0
  68. package/examples/post_request_with_query_params/split/k6-script.sample.ts +14 -0
  69. package/examples/post_request_with_query_params/tags/default.ts +105 -0
  70. package/examples/post_request_with_query_params/tags/exampleAPI.schemas.ts +33 -0
  71. package/examples/post_request_with_query_params/tags/k6-script.sample.ts +14 -0
  72. package/examples/query_params_schema/single/exampleAPI.ts +120 -0
  73. package/examples/query_params_schema/{k6-script.sample.ts → single/k6-script.sample.ts} +2 -2
  74. package/examples/query_params_schema/split/exampleAPI.schemas.ts +37 -0
  75. package/examples/query_params_schema/split/exampleAPI.ts +94 -0
  76. package/examples/query_params_schema/split/k6-script.sample.ts +11 -0
  77. package/examples/query_params_schema/tags/default.ts +94 -0
  78. package/examples/query_params_schema/tags/exampleAPI.schemas.ts +37 -0
  79. package/examples/query_params_schema/tags/k6-script.sample.ts +11 -0
  80. package/examples/simple_post_request_schema/{exampleAPI.ts → single/exampleAPI.ts} +49 -49
  81. package/examples/simple_post_request_schema/{k6-script.sample.ts → single/k6-script.sample.ts} +2 -2
  82. package/examples/simple_post_request_schema/split/exampleAPI.schemas.ts +47 -0
  83. package/examples/simple_post_request_schema/split/exampleAPI.ts +99 -0
  84. package/examples/simple_post_request_schema/split/k6-script.sample.ts +13 -0
  85. package/examples/simple_post_request_schema/tags/default.ts +99 -0
  86. package/examples/simple_post_request_schema/tags/exampleAPI.schemas.ts +47 -0
  87. package/examples/simple_post_request_schema/tags/k6-script.sample.ts +13 -0
  88. package/images/openapi-to-k6.png +0 -0
  89. package/package.json +2 -2
  90. package/src/cli.ts +28 -2
  91. package/src/constants.ts +7 -1
  92. package/src/{generator.ts → generator/index.ts} +8 -21
  93. package/src/{k6SdkClient.ts → generator/k6Client.ts} +174 -222
  94. package/src/type.d.ts +3 -4
  95. package/tests/e2e/schema.json +8 -0
  96. package/tests/e2e/{k6Script.ts → single/k6Script.ts} +7 -2
  97. package/tests/e2e/split/k6Script.ts +82 -0
  98. package/tests/e2e/tags/k6Script.ts +106 -0
  99. package/tests/functional-tests/fixtures/schemas/basic_schema.json +1 -4
  100. package/tests/functional-tests/fixtures/schemas/form_data_schema.json +4 -4
  101. package/tests/functional-tests/fixtures/schemas/form_url_encoded_data_schema.json +3 -3
  102. package/tests/functional-tests/fixtures/schemas/form_url_encoded_data_with_query_params_schema.json +2 -2
  103. package/tests/functional-tests/fixtures/schemas/get_request_with_path_parameters_schema.json +2 -2
  104. package/tests/functional-tests/fixtures/schemas/headers_schema.json +7 -8
  105. package/tests/functional-tests/fixtures/schemas/no_title_schema.json +2 -5
  106. package/tests/functional-tests/fixtures/schemas/post_request_with_query_params.json +3 -4
  107. package/tests/functional-tests/fixtures/schemas/query_params_schema.json +3 -3
  108. package/tests/functional-tests/fixtures/schemas/simple_post_request_schema.json +3 -5
  109. package/tests/functional-tests/generator.test.ts +46 -5
  110. package/examples/basic_schema/simpleAPI.ts +0 -87
  111. package/examples/form_data_schema/formDataAPI.ts +0 -115
  112. package/examples/form_url_encoded_data_schema/formURLEncodedAPI.ts +0 -114
  113. package/examples/form_url_encoded_data_with_query_params_schema/formURLEncodedAPIWithQueryParameters.ts +0 -130
  114. package/examples/get_request_with_path_parameters_schema/simpleAPI.ts +0 -94
  115. package/examples/headers_schema/headerDemoAPI.ts +0 -196
  116. package/examples/no_title_schema/K6Client.ts +0 -86
  117. package/examples/post_request_with_query_params/exampleAPI.ts +0 -124
  118. package/examples/query_params_schema/exampleAPI.ts +0 -118
  119. package/examples/update_examples.sh +0 -21
@@ -0,0 +1,202 @@
1
+ /**
2
+ * Automatically generated by @grafana/openapi-to-k6: 0.2.0
3
+ * Do not edit manually.
4
+ * Header Demo API
5
+ * An API demonstrating the use of headers in different ways
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
+ export type GetExampleResponseHeaders200 = {
12
+ status?: string
13
+ }
14
+
15
+ export type PostExamplePostBody = {
16
+ data?: string
17
+ }
18
+
19
+ export type PostExamplePostHeaders = {
20
+ /**
21
+ * Bearer token for authorization
22
+ */
23
+ Authorization: string
24
+ }
25
+
26
+ export type GetExampleGet200 = {
27
+ message?: string
28
+ }
29
+
30
+ export type GetExampleGetHeaders = {
31
+ /**
32
+ * A custom header for this request
33
+ */
34
+ 'X-Custom-Header'?: string
35
+ }
36
+
37
+ /**
38
+ * This is the base client to use for interacting with the API.
39
+ */
40
+ export class HeaderDemoAPIClient {
41
+ private cleanBaseUrl: string
42
+ private commonRequestParameters: Params
43
+
44
+ constructor(clientOptions: {
45
+ baseUrl: string
46
+ commonRequestParameters?: Params
47
+ }) {
48
+ this.cleanBaseUrl = clientOptions.baseUrl.replace(/\/+$/, '')
49
+ }
50
+
51
+ /**
52
+ * This GET request demonstrates the use of custom request headers
53
+ * @summary GET request with headers
54
+ */
55
+ getExampleGet(
56
+ headers?: GetExampleGetHeaders,
57
+ requestParameters?: Params
58
+ ): {
59
+ response: Response
60
+ data: GetExampleGet200
61
+ } {
62
+ const url = new URL(this.cleanBaseUrl + `/example-get`)
63
+ const mergedRequestParameters = this._mergeRequestParameters(
64
+ requestParameters || {},
65
+ this.commonRequestParameters
66
+ )
67
+ const response = http.request('GET', url.toString(), undefined, {
68
+ ...mergedRequestParameters,
69
+ headers: {
70
+ // In the schema, headers can be of any type like number but k6 accepts only strings as headers, hence converting all headers to string
71
+ ...Object.fromEntries(
72
+ Object.entries(headers || {}).map(([key, value]) => [
73
+ key,
74
+ String(value),
75
+ ])
76
+ ),
77
+ ...mergedRequestParameters?.headers,
78
+ },
79
+ })
80
+ let data
81
+
82
+ try {
83
+ data = response.json()
84
+ } catch {
85
+ data = response.body
86
+ }
87
+ return {
88
+ response,
89
+ data,
90
+ }
91
+ }
92
+
93
+ /**
94
+ * This POST request uses a security header for authentication
95
+ * @summary POST request with security headers
96
+ */
97
+ postExamplePost(
98
+ postExamplePostBody: PostExamplePostBody,
99
+ headers: PostExamplePostHeaders,
100
+ requestParameters?: Params
101
+ ): {
102
+ response: Response
103
+ data: void
104
+ } {
105
+ const url = new URL(this.cleanBaseUrl + `/example-post`)
106
+ const mergedRequestParameters = this._mergeRequestParameters(
107
+ requestParameters || {},
108
+ this.commonRequestParameters
109
+ )
110
+ const response = http.request(
111
+ 'POST',
112
+ url.toString(),
113
+ JSON.stringify(postExamplePostBody),
114
+ {
115
+ ...mergedRequestParameters,
116
+ headers: {
117
+ 'Content-Type': 'application/json',
118
+ // In the schema, headers can be of any type like number but k6 accepts only strings as headers, hence converting all headers to string
119
+ ...Object.fromEntries(
120
+ Object.entries(headers || {}).map(([key, value]) => [
121
+ key,
122
+ String(value),
123
+ ])
124
+ ),
125
+ ...mergedRequestParameters?.headers,
126
+ },
127
+ }
128
+ )
129
+ let data
130
+
131
+ try {
132
+ data = response.json()
133
+ } catch {
134
+ data = response.body
135
+ }
136
+ return {
137
+ response,
138
+ data,
139
+ }
140
+ }
141
+
142
+ /**
143
+ * This GET request returns custom response headers
144
+ * @summary GET request with response headers only
145
+ */
146
+ getExampleResponseHeaders(requestParameters?: Params): {
147
+ response: Response
148
+ data: GetExampleResponseHeaders200
149
+ } {
150
+ const url = new URL(this.cleanBaseUrl + `/example-response-headers`)
151
+ const mergedRequestParameters = this._mergeRequestParameters(
152
+ requestParameters || {},
153
+ this.commonRequestParameters
154
+ )
155
+ const response = http.request(
156
+ 'GET',
157
+ url.toString(),
158
+ undefined,
159
+ mergedRequestParameters
160
+ )
161
+ let data
162
+
163
+ try {
164
+ data = response.json()
165
+ } catch {
166
+ data = response.body
167
+ }
168
+ return {
169
+ response,
170
+ data,
171
+ }
172
+ }
173
+
174
+ /**
175
+ * Merges the provided request parameters with default parameters for the client.
176
+ *
177
+ * @param {Params} requestParameters - The parameters provided specifically for the request
178
+ * @param {Params} commonRequestParameters - Common parameters for all requests
179
+ * @returns {Params} - The merged parameters
180
+ */
181
+ private _mergeRequestParameters(
182
+ requestParameters?: Params,
183
+ commonRequestParameters?: Params
184
+ ): Params {
185
+ return {
186
+ ...commonRequestParameters, // Default to common parameters
187
+ ...requestParameters, // Override with request-specific parameters
188
+ headers: {
189
+ ...(commonRequestParameters?.headers || {}), // Ensure headers are defined
190
+ ...(requestParameters?.headers || {}),
191
+ },
192
+ cookies: {
193
+ ...(commonRequestParameters?.cookies || {}), // Ensure cookies are defined
194
+ ...(requestParameters?.cookies || {}),
195
+ },
196
+ tags: {
197
+ ...(commonRequestParameters?.tags || {}), // Ensure tags are defined
198
+ ...(requestParameters?.tags || {}),
199
+ },
200
+ }
201
+ }
202
+ }
@@ -1,7 +1,7 @@
1
- import { createHeaderDemoAPI } from './headerDemoAPI.ts'
1
+ import { HeaderDemoAPIClient } from './headerDemoAPI.ts'
2
2
 
3
3
  const baseUrl = '<BASE_URL>'
4
- const client = createHeaderDemoAPI({ baseUrl })
4
+ const client = new HeaderDemoAPIClient({ baseUrl })
5
5
 
6
6
  export default function () {
7
7
  /**
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Automatically generated by @grafana/openapi-to-k6: 0.2.0
3
+ * Do not edit manually.
4
+ * Header Demo API
5
+ * An API demonstrating the use of headers in different ways
6
+ * OpenAPI spec version: 1.0.0
7
+ */
8
+ export type GetExampleResponseHeaders200 = {
9
+ status?: string
10
+ }
11
+
12
+ export type PostExamplePostBody = {
13
+ data?: string
14
+ }
15
+
16
+ export type PostExamplePostHeaders = {
17
+ /**
18
+ * Bearer token for authorization
19
+ */
20
+ Authorization: string
21
+ }
22
+
23
+ export type GetExampleGet200 = {
24
+ message?: string
25
+ }
26
+
27
+ export type GetExampleGetHeaders = {
28
+ /**
29
+ * A custom header for this request
30
+ */
31
+ 'X-Custom-Header'?: string
32
+ }
@@ -0,0 +1,184 @@
1
+ /**
2
+ * Automatically generated by @grafana/openapi-to-k6: 0.2.0
3
+ * Do not edit manually.
4
+ * Header Demo API
5
+ * An API demonstrating the use of headers in different ways
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
+ GetExampleGet200,
13
+ GetExampleGetHeaders,
14
+ GetExampleResponseHeaders200,
15
+ PostExamplePostBody,
16
+ PostExamplePostHeaders,
17
+ } from './headerDemoAPI.schemas'
18
+
19
+ /**
20
+ * This is the base client to use for interacting with the API.
21
+ */
22
+ export class HeaderDemoAPIClient {
23
+ private cleanBaseUrl: string
24
+ private commonRequestParameters: Params
25
+
26
+ constructor(clientOptions: {
27
+ baseUrl: string
28
+ commonRequestParameters?: Params
29
+ }) {
30
+ this.cleanBaseUrl = clientOptions.baseUrl.replace(/\/+$/, '')
31
+ }
32
+
33
+ /**
34
+ * This GET request demonstrates the use of custom request headers
35
+ * @summary GET request with headers
36
+ */
37
+ getExampleGet(
38
+ headers?: GetExampleGetHeaders,
39
+ requestParameters?: Params
40
+ ): {
41
+ response: Response
42
+ data: GetExampleGet200
43
+ } {
44
+ const url = new URL(this.cleanBaseUrl + `/example-get`)
45
+ const mergedRequestParameters = this._mergeRequestParameters(
46
+ requestParameters || {},
47
+ this.commonRequestParameters
48
+ )
49
+ const response = http.request('GET', url.toString(), undefined, {
50
+ ...mergedRequestParameters,
51
+ headers: {
52
+ // In the schema, headers can be of any type like number but k6 accepts only strings as headers, hence converting all headers to string
53
+ ...Object.fromEntries(
54
+ Object.entries(headers || {}).map(([key, value]) => [
55
+ key,
56
+ String(value),
57
+ ])
58
+ ),
59
+ ...mergedRequestParameters?.headers,
60
+ },
61
+ })
62
+ let data
63
+
64
+ try {
65
+ data = response.json()
66
+ } catch {
67
+ data = response.body
68
+ }
69
+ return {
70
+ response,
71
+ data,
72
+ }
73
+ }
74
+
75
+ /**
76
+ * This POST request uses a security header for authentication
77
+ * @summary POST request with security headers
78
+ */
79
+ postExamplePost(
80
+ postExamplePostBody: PostExamplePostBody,
81
+ headers: PostExamplePostHeaders,
82
+ requestParameters?: Params
83
+ ): {
84
+ response: Response
85
+ data: void
86
+ } {
87
+ const url = new URL(this.cleanBaseUrl + `/example-post`)
88
+ const mergedRequestParameters = this._mergeRequestParameters(
89
+ requestParameters || {},
90
+ this.commonRequestParameters
91
+ )
92
+ const response = http.request(
93
+ 'POST',
94
+ url.toString(),
95
+ JSON.stringify(postExamplePostBody),
96
+ {
97
+ ...mergedRequestParameters,
98
+ headers: {
99
+ 'Content-Type': 'application/json',
100
+ // In the schema, headers can be of any type like number but k6 accepts only strings as headers, hence converting all headers to string
101
+ ...Object.fromEntries(
102
+ Object.entries(headers || {}).map(([key, value]) => [
103
+ key,
104
+ String(value),
105
+ ])
106
+ ),
107
+ ...mergedRequestParameters?.headers,
108
+ },
109
+ }
110
+ )
111
+ let data
112
+
113
+ try {
114
+ data = response.json()
115
+ } catch {
116
+ data = response.body
117
+ }
118
+ return {
119
+ response,
120
+ data,
121
+ }
122
+ }
123
+
124
+ /**
125
+ * This GET request returns custom response headers
126
+ * @summary GET request with response headers only
127
+ */
128
+ getExampleResponseHeaders(requestParameters?: Params): {
129
+ response: Response
130
+ data: GetExampleResponseHeaders200
131
+ } {
132
+ const url = new URL(this.cleanBaseUrl + `/example-response-headers`)
133
+ const mergedRequestParameters = this._mergeRequestParameters(
134
+ requestParameters || {},
135
+ this.commonRequestParameters
136
+ )
137
+ const response = http.request(
138
+ 'GET',
139
+ url.toString(),
140
+ undefined,
141
+ mergedRequestParameters
142
+ )
143
+ let data
144
+
145
+ try {
146
+ data = response.json()
147
+ } catch {
148
+ data = response.body
149
+ }
150
+ return {
151
+ response,
152
+ data,
153
+ }
154
+ }
155
+
156
+ /**
157
+ * Merges the provided request parameters with default parameters for the client.
158
+ *
159
+ * @param {Params} requestParameters - The parameters provided specifically for the request
160
+ * @param {Params} commonRequestParameters - Common parameters for all requests
161
+ * @returns {Params} - The merged parameters
162
+ */
163
+ private _mergeRequestParameters(
164
+ requestParameters?: Params,
165
+ commonRequestParameters?: Params
166
+ ): Params {
167
+ return {
168
+ ...commonRequestParameters, // Default to common parameters
169
+ ...requestParameters, // Override with request-specific parameters
170
+ headers: {
171
+ ...(commonRequestParameters?.headers || {}), // Ensure headers are defined
172
+ ...(requestParameters?.headers || {}),
173
+ },
174
+ cookies: {
175
+ ...(commonRequestParameters?.cookies || {}), // Ensure cookies are defined
176
+ ...(requestParameters?.cookies || {}),
177
+ },
178
+ tags: {
179
+ ...(commonRequestParameters?.tags || {}), // Ensure tags are defined
180
+ ...(requestParameters?.tags || {}),
181
+ },
182
+ }
183
+ }
184
+ }
@@ -0,0 +1,25 @@
1
+ import { HeaderDemoAPIClient } from './headerDemoAPI.ts'
2
+
3
+ const baseUrl = '<BASE_URL>'
4
+ const client = new HeaderDemoAPIClient({ baseUrl })
5
+
6
+ export default function () {
7
+ /**
8
+ * GET request with headers
9
+ */
10
+ const getExampleGetResponseData = client.getExampleGet()
11
+
12
+ /**
13
+ * POST request with security headers
14
+ */
15
+ const postExamplePostResponseData = client.postExamplePost(
16
+ postExamplePostBody,
17
+ headers
18
+ )
19
+
20
+ /**
21
+ * GET request with response headers only
22
+ */
23
+ const getExampleResponseHeadersResponseData =
24
+ client.getExampleResponseHeaders()
25
+ }
@@ -0,0 +1,182 @@
1
+ /**
2
+ * Automatically generated by @grafana/openapi-to-k6: 0.2.0
3
+ * Do not edit manually.
4
+ * Header Demo API
5
+ * An API demonstrating the use of headers in different ways
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
+ GetExampleGet200,
13
+ GetExampleGetHeaders,
14
+ GetExampleResponseHeaders200,
15
+ PostExamplePostBody,
16
+ PostExamplePostHeaders,
17
+ } from './headerDemoAPI.schemas'
18
+
19
+ /**
20
+ * This is the base client to use for interacting with the API.
21
+ */
22
+ export class DefaultClient {
23
+ private cleanBaseUrl: string
24
+ private commonRequestParameters: Params
25
+
26
+ constructor(clientOptions: {
27
+ baseUrl: string
28
+ commonRequestParameters?: Params
29
+ }) {
30
+ this.cleanBaseUrl = clientOptions.baseUrl.replace(/\/+$/, '')
31
+ }
32
+
33
+ /**
34
+ * This GET request demonstrates the use of custom request headers
35
+ * @summary GET request with headers
36
+ */
37
+ getExampleGet(
38
+ headers?: GetExampleGetHeaders,
39
+ requestParameters?: Params
40
+ ): {
41
+ response: Response
42
+ data: GetExampleGet200
43
+ } {
44
+ const url = new URL(this.cleanBaseUrl + `/example-get`)
45
+ const mergedRequestParameters = this._mergeRequestParameters(
46
+ requestParameters || {},
47
+ this.commonRequestParameters
48
+ )
49
+ const response = http.request('GET', url.toString(), undefined, {
50
+ ...mergedRequestParameters,
51
+ headers: {
52
+ // In the schema, headers can be of any type like number but k6 accepts only strings as headers, hence converting all headers to string
53
+ ...Object.fromEntries(
54
+ Object.entries(headers || {}).map(([key, value]) => [
55
+ key,
56
+ String(value),
57
+ ])
58
+ ),
59
+ ...mergedRequestParameters?.headers,
60
+ },
61
+ })
62
+ let data
63
+
64
+ try {
65
+ data = response.json()
66
+ } catch {
67
+ data = response.body
68
+ }
69
+ return {
70
+ response,
71
+ data,
72
+ }
73
+ }
74
+ /**
75
+ * This POST request uses a security header for authentication
76
+ * @summary POST request with security headers
77
+ */
78
+ postExamplePost(
79
+ postExamplePostBody: PostExamplePostBody,
80
+ headers: PostExamplePostHeaders,
81
+ requestParameters?: Params
82
+ ): {
83
+ response: Response
84
+ data: void
85
+ } {
86
+ const url = new URL(this.cleanBaseUrl + `/example-post`)
87
+ const mergedRequestParameters = this._mergeRequestParameters(
88
+ requestParameters || {},
89
+ this.commonRequestParameters
90
+ )
91
+ const response = http.request(
92
+ 'POST',
93
+ url.toString(),
94
+ JSON.stringify(postExamplePostBody),
95
+ {
96
+ ...mergedRequestParameters,
97
+ headers: {
98
+ 'Content-Type': 'application/json',
99
+ // In the schema, headers can be of any type like number but k6 accepts only strings as headers, hence converting all headers to string
100
+ ...Object.fromEntries(
101
+ Object.entries(headers || {}).map(([key, value]) => [
102
+ key,
103
+ String(value),
104
+ ])
105
+ ),
106
+ ...mergedRequestParameters?.headers,
107
+ },
108
+ }
109
+ )
110
+ let data
111
+
112
+ try {
113
+ data = response.json()
114
+ } catch {
115
+ data = response.body
116
+ }
117
+ return {
118
+ response,
119
+ data,
120
+ }
121
+ }
122
+ /**
123
+ * This GET request returns custom response headers
124
+ * @summary GET request with response headers only
125
+ */
126
+ getExampleResponseHeaders(requestParameters?: Params): {
127
+ response: Response
128
+ data: GetExampleResponseHeaders200
129
+ } {
130
+ const url = new URL(this.cleanBaseUrl + `/example-response-headers`)
131
+ const mergedRequestParameters = this._mergeRequestParameters(
132
+ requestParameters || {},
133
+ this.commonRequestParameters
134
+ )
135
+ const response = http.request(
136
+ 'GET',
137
+ url.toString(),
138
+ undefined,
139
+ mergedRequestParameters
140
+ )
141
+ let data
142
+
143
+ try {
144
+ data = response.json()
145
+ } catch {
146
+ data = response.body
147
+ }
148
+ return {
149
+ response,
150
+ data,
151
+ }
152
+ }
153
+
154
+ /**
155
+ * Merges the provided request parameters with default parameters for the client.
156
+ *
157
+ * @param {Params} requestParameters - The parameters provided specifically for the request
158
+ * @param {Params} commonRequestParameters - Common parameters for all requests
159
+ * @returns {Params} - The merged parameters
160
+ */
161
+ private _mergeRequestParameters(
162
+ requestParameters?: Params,
163
+ commonRequestParameters?: Params
164
+ ): Params {
165
+ return {
166
+ ...commonRequestParameters, // Default to common parameters
167
+ ...requestParameters, // Override with request-specific parameters
168
+ headers: {
169
+ ...(commonRequestParameters?.headers || {}), // Ensure headers are defined
170
+ ...(requestParameters?.headers || {}),
171
+ },
172
+ cookies: {
173
+ ...(commonRequestParameters?.cookies || {}), // Ensure cookies are defined
174
+ ...(requestParameters?.cookies || {}),
175
+ },
176
+ tags: {
177
+ ...(commonRequestParameters?.tags || {}), // Ensure tags are defined
178
+ ...(requestParameters?.tags || {}),
179
+ },
180
+ }
181
+ }
182
+ }
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Automatically generated by @grafana/openapi-to-k6: 0.2.0
3
+ * Do not edit manually.
4
+ * Header Demo API
5
+ * An API demonstrating the use of headers in different ways
6
+ * OpenAPI spec version: 1.0.0
7
+ */
8
+ export type GetExampleResponseHeaders200 = {
9
+ status?: string
10
+ }
11
+
12
+ export type PostExamplePostBody = {
13
+ data?: string
14
+ }
15
+
16
+ export type PostExamplePostHeaders = {
17
+ /**
18
+ * Bearer token for authorization
19
+ */
20
+ Authorization: string
21
+ }
22
+
23
+ export type GetExampleGet200 = {
24
+ message?: string
25
+ }
26
+
27
+ export type GetExampleGetHeaders = {
28
+ /**
29
+ * A custom header for this request
30
+ */
31
+ 'X-Custom-Header'?: string
32
+ }
@@ -0,0 +1,25 @@
1
+ import { HeaderDemoAPIClient } from './headerDemoAPI.ts'
2
+
3
+ const baseUrl = '<BASE_URL>'
4
+ const client = new HeaderDemoAPIClient({ baseUrl })
5
+
6
+ export default function () {
7
+ /**
8
+ * GET request with headers
9
+ */
10
+ const getExampleGetResponseData = client.getExampleGet()
11
+
12
+ /**
13
+ * POST request with security headers
14
+ */
15
+ const postExamplePostResponseData = client.postExamplePost(
16
+ postExamplePostBody,
17
+ headers
18
+ )
19
+
20
+ /**
21
+ * GET request with response headers only
22
+ */
23
+ const getExampleResponseHeadersResponseData =
24
+ client.getExampleResponseHeaders()
25
+ }
@@ -1,7 +1,7 @@
1
- import { createK6Client } from './k6Client.ts'
1
+ import { K6ClientClient } from './k6Client.ts'
2
2
 
3
3
  const baseUrl = '<BASE_URL>'
4
- const client = createK6Client({ baseUrl })
4
+ const client = new K6ClientClient({ baseUrl })
5
5
 
6
6
  export default function () {
7
7
  /**