@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,104 @@
1
+ /**
2
+ * Automatically generated by @grafana/openapi-to-k6: 0.2.0
3
+ * Do not edit manually.
4
+ * Form URL Encoded API with Query Parameters
5
+ * OpenAPI spec version: 1.0.0
6
+ */
7
+ import { URL, URLSearchParams } from 'https://jslib.k6.io/url/1.0.0/index.js'
8
+ import http from 'k6/http'
9
+ import type { Params, Response } from 'k6/http'
10
+ import type {
11
+ PostSubmitForm200,
12
+ PostSubmitFormBody,
13
+ PostSubmitFormParams,
14
+ } from './formURLEncodedAPIWithQueryParameters.schemas'
15
+
16
+ /**
17
+ * This is the base client to use for interacting with the API.
18
+ */
19
+ export class FormURLEncodedAPIWithQueryParametersClient {
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 accepts form URL-encoded data and query parameters.
32
+ * @summary Submit form data with query parameters
33
+ */
34
+ postSubmitForm(
35
+ postSubmitFormBody: PostSubmitFormBody,
36
+ params: PostSubmitFormParams,
37
+ requestParameters?: Params
38
+ ): {
39
+ response: Response
40
+ data: PostSubmitForm200
41
+ } {
42
+ const url = new URL(
43
+ this.cleanBaseUrl +
44
+ `/submit-form` +
45
+ `?${new URLSearchParams(params).toString()}`
46
+ )
47
+ const mergedRequestParameters = this._mergeRequestParameters(
48
+ requestParameters || {},
49
+ this.commonRequestParameters
50
+ )
51
+ const response = http.request(
52
+ 'POST',
53
+ url.toString(),
54
+ JSON.stringify(postSubmitFormBody),
55
+ {
56
+ ...mergedRequestParameters,
57
+ headers: {
58
+ 'Content-Type': 'application/x-www-form-urlencoded',
59
+ ...mergedRequestParameters?.headers,
60
+ },
61
+ }
62
+ )
63
+ let data
64
+
65
+ try {
66
+ data = response.json()
67
+ } catch {
68
+ data = response.body
69
+ }
70
+ return {
71
+ response,
72
+ data,
73
+ }
74
+ }
75
+
76
+ /**
77
+ * Merges the provided request parameters with default parameters for the client.
78
+ *
79
+ * @param {Params} requestParameters - The parameters provided specifically for the request
80
+ * @param {Params} commonRequestParameters - Common parameters for all requests
81
+ * @returns {Params} - The merged parameters
82
+ */
83
+ private _mergeRequestParameters(
84
+ requestParameters?: Params,
85
+ commonRequestParameters?: Params
86
+ ): Params {
87
+ return {
88
+ ...commonRequestParameters, // Default to common parameters
89
+ ...requestParameters, // Override with request-specific parameters
90
+ headers: {
91
+ ...(commonRequestParameters?.headers || {}), // Ensure headers are defined
92
+ ...(requestParameters?.headers || {}),
93
+ },
94
+ cookies: {
95
+ ...(commonRequestParameters?.cookies || {}), // Ensure cookies are defined
96
+ ...(requestParameters?.cookies || {}),
97
+ },
98
+ tags: {
99
+ ...(commonRequestParameters?.tags || {}), // Ensure tags are defined
100
+ ...(requestParameters?.tags || {}),
101
+ },
102
+ }
103
+ }
104
+ }
@@ -0,0 +1,14 @@
1
+ import { FormURLEncodedAPIwithQueryParametersClient } from './formURLEncodedAPIWithQueryParameters.ts'
2
+
3
+ const baseUrl = '<BASE_URL>'
4
+ const client = new FormURLEncodedAPIwithQueryParametersClient({ baseUrl })
5
+
6
+ export default function () {
7
+ /**
8
+ * Submit form data with query parameters
9
+ */
10
+ const postSubmitFormResponseData = client.postSubmitForm(
11
+ postSubmitFormBody,
12
+ params
13
+ )
14
+ }
@@ -0,0 +1,104 @@
1
+ /**
2
+ * Automatically generated by @grafana/openapi-to-k6: 0.2.0
3
+ * Do not edit manually.
4
+ * Form URL Encoded API with Query Parameters
5
+ * OpenAPI spec version: 1.0.0
6
+ */
7
+ import { URL, URLSearchParams } from 'https://jslib.k6.io/url/1.0.0/index.js'
8
+ import http from 'k6/http'
9
+ import type { Params, Response } from 'k6/http'
10
+ import type {
11
+ PostSubmitForm200,
12
+ PostSubmitFormBody,
13
+ PostSubmitFormParams,
14
+ } from './formURLEncodedAPIWithQueryParameters.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 accepts form URL-encoded data and query parameters.
32
+ * @summary Submit form data with query parameters
33
+ */
34
+ postSubmitForm(
35
+ postSubmitFormBody: PostSubmitFormBody,
36
+ params: PostSubmitFormParams,
37
+ requestParameters?: Params
38
+ ): {
39
+ response: Response
40
+ data: PostSubmitForm200
41
+ } {
42
+ const url = new URL(
43
+ this.cleanBaseUrl +
44
+ `/submit-form` +
45
+ `?${new URLSearchParams(params).toString()}`
46
+ )
47
+ const mergedRequestParameters = this._mergeRequestParameters(
48
+ requestParameters || {},
49
+ this.commonRequestParameters
50
+ )
51
+ const response = http.request(
52
+ 'POST',
53
+ url.toString(),
54
+ JSON.stringify(postSubmitFormBody),
55
+ {
56
+ ...mergedRequestParameters,
57
+ headers: {
58
+ 'Content-Type': 'application/x-www-form-urlencoded',
59
+ ...mergedRequestParameters?.headers,
60
+ },
61
+ }
62
+ )
63
+ let data
64
+
65
+ try {
66
+ data = response.json()
67
+ } catch {
68
+ data = response.body
69
+ }
70
+ return {
71
+ response,
72
+ data,
73
+ }
74
+ }
75
+
76
+ /**
77
+ * Merges the provided request parameters with default parameters for the client.
78
+ *
79
+ * @param {Params} requestParameters - The parameters provided specifically for the request
80
+ * @param {Params} commonRequestParameters - Common parameters for all requests
81
+ * @returns {Params} - The merged parameters
82
+ */
83
+ private _mergeRequestParameters(
84
+ requestParameters?: Params,
85
+ commonRequestParameters?: Params
86
+ ): Params {
87
+ return {
88
+ ...commonRequestParameters, // Default to common parameters
89
+ ...requestParameters, // Override with request-specific parameters
90
+ headers: {
91
+ ...(commonRequestParameters?.headers || {}), // Ensure headers are defined
92
+ ...(requestParameters?.headers || {}),
93
+ },
94
+ cookies: {
95
+ ...(commonRequestParameters?.cookies || {}), // Ensure cookies are defined
96
+ ...(requestParameters?.cookies || {}),
97
+ },
98
+ tags: {
99
+ ...(commonRequestParameters?.tags || {}), // Ensure tags are defined
100
+ ...(requestParameters?.tags || {}),
101
+ },
102
+ }
103
+ }
104
+ }
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Automatically generated by @grafana/openapi-to-k6: 0.2.0
3
+ * Do not edit manually.
4
+ * Form URL Encoded API with Query Parameters
5
+ * OpenAPI spec version: 1.0.0
6
+ */
7
+ export type PostSubmitForm400 = {
8
+ error?: string
9
+ success?: boolean
10
+ }
11
+
12
+ export type PostSubmitForm200 = {
13
+ message?: string
14
+ success?: boolean
15
+ }
16
+
17
+ export type PostSubmitFormBody = {
18
+ /** Age of the user */
19
+ age?: number
20
+ /** Email address of the user */
21
+ email: string
22
+ /** Name of the user */
23
+ name: string
24
+ }
25
+
26
+ export type PostSubmitFormParams = {
27
+ /**
28
+ * Authentication token
29
+ */
30
+ token: string
31
+ /**
32
+ * Locale of the user
33
+ */
34
+ locale?: string
35
+ }
@@ -0,0 +1,14 @@
1
+ import { FormURLEncodedAPIwithQueryParametersClient } from './formURLEncodedAPIWithQueryParameters.ts'
2
+
3
+ const baseUrl = '<BASE_URL>'
4
+ const client = new FormURLEncodedAPIwithQueryParametersClient({ baseUrl })
5
+
6
+ export default function () {
7
+ /**
8
+ * Submit form data with query parameters
9
+ */
10
+ const postSubmitFormResponseData = client.postSubmitForm(
11
+ postSubmitFormBody,
12
+ params
13
+ )
14
+ }
@@ -1,7 +1,7 @@
1
- import { createSimpleAPI } from './simpleAPI.ts'
1
+ import { SimpleAPIClient } from './simpleAPI.ts'
2
2
 
3
3
  const baseUrl = '<BASE_URL>'
4
- const client = createSimpleAPI({ baseUrl })
4
+ const client = new SimpleAPIClient({ baseUrl })
5
5
 
6
6
  export default function () {
7
7
  /**
@@ -0,0 +1,94 @@
1
+ /**
2
+ * Automatically generated by @grafana/openapi-to-k6: 0.2.0
3
+ * Do not edit manually.
4
+ * Simple API
5
+ * An API with a single GET request that takes a path parameter
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 GetItemById200 = {
12
+ description?: string
13
+ id?: string
14
+ name?: string
15
+ }
16
+
17
+ /**
18
+ * This is the base client to use for interacting with the API.
19
+ */
20
+ export class SimpleAPIClient {
21
+ private cleanBaseUrl: string
22
+ private commonRequestParameters: Params
23
+
24
+ constructor(clientOptions: {
25
+ baseUrl: string
26
+ commonRequestParameters?: Params
27
+ }) {
28
+ this.cleanBaseUrl = clientOptions.baseUrl.replace(/\/+$/, '')
29
+ }
30
+
31
+ /**
32
+ * Returns a single item based on the provided ID
33
+ * @summary Get an item by its ID
34
+ */
35
+ getItemById(
36
+ id: string,
37
+ requestParameters?: Params
38
+ ): {
39
+ response: Response
40
+ data: GetItemById200
41
+ } {
42
+ const url = new URL(this.cleanBaseUrl + `/items/${id}`)
43
+ const mergedRequestParameters = this._mergeRequestParameters(
44
+ requestParameters || {},
45
+ this.commonRequestParameters
46
+ )
47
+ const response = http.request(
48
+ 'GET',
49
+ url.toString(),
50
+ undefined,
51
+ mergedRequestParameters
52
+ )
53
+ let data
54
+
55
+ try {
56
+ data = response.json()
57
+ } catch {
58
+ data = response.body
59
+ }
60
+ return {
61
+ response,
62
+ data,
63
+ }
64
+ }
65
+
66
+ /**
67
+ * Merges the provided request parameters with default parameters for the client.
68
+ *
69
+ * @param {Params} requestParameters - The parameters provided specifically for the request
70
+ * @param {Params} commonRequestParameters - Common parameters for all requests
71
+ * @returns {Params} - The merged parameters
72
+ */
73
+ private _mergeRequestParameters(
74
+ requestParameters?: Params,
75
+ commonRequestParameters?: Params
76
+ ): Params {
77
+ return {
78
+ ...commonRequestParameters, // Default to common parameters
79
+ ...requestParameters, // Override with request-specific parameters
80
+ headers: {
81
+ ...(commonRequestParameters?.headers || {}), // Ensure headers are defined
82
+ ...(requestParameters?.headers || {}),
83
+ },
84
+ cookies: {
85
+ ...(commonRequestParameters?.cookies || {}), // Ensure cookies are defined
86
+ ...(requestParameters?.cookies || {}),
87
+ },
88
+ tags: {
89
+ ...(commonRequestParameters?.tags || {}), // Ensure tags are defined
90
+ ...(requestParameters?.tags || {}),
91
+ },
92
+ }
93
+ }
94
+ }
@@ -0,0 +1,11 @@
1
+ import { SimpleAPIClient } from './simpleAPI.ts'
2
+
3
+ const baseUrl = '<BASE_URL>'
4
+ const client = new SimpleAPIClient({ baseUrl })
5
+
6
+ export default function () {
7
+ /**
8
+ * Get an item by its ID
9
+ */
10
+ const getItemByIdResponseData = client.getItemById(id)
11
+ }
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Automatically generated by @grafana/openapi-to-k6: 0.2.0
3
+ * Do not edit manually.
4
+ * Simple API
5
+ * An API with a single GET request that takes a path parameter
6
+ * OpenAPI spec version: 1.0.0
7
+ */
8
+ export type GetItemById200 = {
9
+ description?: string
10
+ id?: string
11
+ name?: string
12
+ }
@@ -0,0 +1,90 @@
1
+ /**
2
+ * Automatically generated by @grafana/openapi-to-k6: 0.2.0
3
+ * Do not edit manually.
4
+ * Simple API
5
+ * An API with a single GET request that takes a path parameter
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 { GetItemById200 } from './simpleAPI.schemas'
12
+
13
+ /**
14
+ * This is the base client to use for interacting with the API.
15
+ */
16
+ export class SimpleAPIClient {
17
+ private cleanBaseUrl: string
18
+ private commonRequestParameters: Params
19
+
20
+ constructor(clientOptions: {
21
+ baseUrl: string
22
+ commonRequestParameters?: Params
23
+ }) {
24
+ this.cleanBaseUrl = clientOptions.baseUrl.replace(/\/+$/, '')
25
+ }
26
+
27
+ /**
28
+ * Returns a single item based on the provided ID
29
+ * @summary Get an item by its ID
30
+ */
31
+ getItemById(
32
+ id: string,
33
+ requestParameters?: Params
34
+ ): {
35
+ response: Response
36
+ data: GetItemById200
37
+ } {
38
+ const url = new URL(this.cleanBaseUrl + `/items/${id}`)
39
+ const mergedRequestParameters = this._mergeRequestParameters(
40
+ requestParameters || {},
41
+ this.commonRequestParameters
42
+ )
43
+ const response = http.request(
44
+ 'GET',
45
+ url.toString(),
46
+ undefined,
47
+ mergedRequestParameters
48
+ )
49
+ let data
50
+
51
+ try {
52
+ data = response.json()
53
+ } catch {
54
+ data = response.body
55
+ }
56
+ return {
57
+ response,
58
+ data,
59
+ }
60
+ }
61
+
62
+ /**
63
+ * Merges the provided request parameters with default parameters for the client.
64
+ *
65
+ * @param {Params} requestParameters - The parameters provided specifically for the request
66
+ * @param {Params} commonRequestParameters - Common parameters for all requests
67
+ * @returns {Params} - The merged parameters
68
+ */
69
+ private _mergeRequestParameters(
70
+ requestParameters?: Params,
71
+ commonRequestParameters?: Params
72
+ ): Params {
73
+ return {
74
+ ...commonRequestParameters, // Default to common parameters
75
+ ...requestParameters, // Override with request-specific parameters
76
+ headers: {
77
+ ...(commonRequestParameters?.headers || {}), // Ensure headers are defined
78
+ ...(requestParameters?.headers || {}),
79
+ },
80
+ cookies: {
81
+ ...(commonRequestParameters?.cookies || {}), // Ensure cookies are defined
82
+ ...(requestParameters?.cookies || {}),
83
+ },
84
+ tags: {
85
+ ...(commonRequestParameters?.tags || {}), // Ensure tags are defined
86
+ ...(requestParameters?.tags || {}),
87
+ },
88
+ }
89
+ }
90
+ }
@@ -0,0 +1,90 @@
1
+ /**
2
+ * Automatically generated by @grafana/openapi-to-k6: 0.2.0
3
+ * Do not edit manually.
4
+ * Simple API
5
+ * An API with a single GET request that takes a path parameter
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 { GetItemById200 } from './simpleAPI.schemas'
12
+
13
+ /**
14
+ * This is the base client to use for interacting with the API.
15
+ */
16
+ export class DefaultClient {
17
+ private cleanBaseUrl: string
18
+ private commonRequestParameters: Params
19
+
20
+ constructor(clientOptions: {
21
+ baseUrl: string
22
+ commonRequestParameters?: Params
23
+ }) {
24
+ this.cleanBaseUrl = clientOptions.baseUrl.replace(/\/+$/, '')
25
+ }
26
+
27
+ /**
28
+ * Returns a single item based on the provided ID
29
+ * @summary Get an item by its ID
30
+ */
31
+ getItemById(
32
+ id: string,
33
+ requestParameters?: Params
34
+ ): {
35
+ response: Response
36
+ data: GetItemById200
37
+ } {
38
+ const url = new URL(this.cleanBaseUrl + `/items/${id}`)
39
+ const mergedRequestParameters = this._mergeRequestParameters(
40
+ requestParameters || {},
41
+ this.commonRequestParameters
42
+ )
43
+ const response = http.request(
44
+ 'GET',
45
+ url.toString(),
46
+ undefined,
47
+ mergedRequestParameters
48
+ )
49
+ let data
50
+
51
+ try {
52
+ data = response.json()
53
+ } catch {
54
+ data = response.body
55
+ }
56
+ return {
57
+ response,
58
+ data,
59
+ }
60
+ }
61
+
62
+ /**
63
+ * Merges the provided request parameters with default parameters for the client.
64
+ *
65
+ * @param {Params} requestParameters - The parameters provided specifically for the request
66
+ * @param {Params} commonRequestParameters - Common parameters for all requests
67
+ * @returns {Params} - The merged parameters
68
+ */
69
+ private _mergeRequestParameters(
70
+ requestParameters?: Params,
71
+ commonRequestParameters?: Params
72
+ ): Params {
73
+ return {
74
+ ...commonRequestParameters, // Default to common parameters
75
+ ...requestParameters, // Override with request-specific parameters
76
+ headers: {
77
+ ...(commonRequestParameters?.headers || {}), // Ensure headers are defined
78
+ ...(requestParameters?.headers || {}),
79
+ },
80
+ cookies: {
81
+ ...(commonRequestParameters?.cookies || {}), // Ensure cookies are defined
82
+ ...(requestParameters?.cookies || {}),
83
+ },
84
+ tags: {
85
+ ...(commonRequestParameters?.tags || {}), // Ensure tags are defined
86
+ ...(requestParameters?.tags || {}),
87
+ },
88
+ }
89
+ }
90
+ }
@@ -0,0 +1,11 @@
1
+ import { SimpleAPIClient } from './simpleAPI.ts'
2
+
3
+ const baseUrl = '<BASE_URL>'
4
+ const client = new SimpleAPIClient({ baseUrl })
5
+
6
+ export default function () {
7
+ /**
8
+ * Get an item by its ID
9
+ */
10
+ const getItemByIdResponseData = client.getItemById(id)
11
+ }
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Automatically generated by @grafana/openapi-to-k6: 0.2.0
3
+ * Do not edit manually.
4
+ * Simple API
5
+ * An API with a single GET request that takes a path parameter
6
+ * OpenAPI spec version: 1.0.0
7
+ */
8
+ export type GetItemById200 = {
9
+ description?: string
10
+ id?: string
11
+ name?: string
12
+ }