@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,106 @@
1
+ /* eslint-disable import/no-unresolved */
2
+ import { check } from 'k6'
3
+ import { DefaultClient } from './default.ts'
4
+ import { ItemsFormClient } from './items-form.ts'
5
+ import { ItemsClient } from './items.ts'
6
+
7
+ /* eslint-enable import/no-unresolved */
8
+
9
+ const baseUrl = 'http://localhost:3000'
10
+ const itemsClient = new ItemsClient({ baseUrl })
11
+ const itemFormClient = new ItemsFormClient({ baseUrl })
12
+ const defaultClient = new DefaultClient({ baseUrl })
13
+
14
+ export const options = {
15
+ thresholds: {
16
+ // the rate of successful checks should be higher than 90%
17
+ checks: ['rate>=1'],
18
+ },
19
+ }
20
+
21
+ function checkResponseStatus(response, expectedStatus) {
22
+ const result = check(response, {
23
+ [`status is ${expectedStatus}`]: (r) => r.status === expectedStatus,
24
+ })
25
+
26
+ if (!result) {
27
+ console.error(
28
+ `Check failed! Expected status ${expectedStatus} but got ${response.status}. Following is the response:`
29
+ )
30
+ console.log(JSON.stringify(response, null, 2))
31
+ }
32
+ }
33
+
34
+ export default function () {
35
+ // Items client calls start
36
+ const getResponseData = itemsClient.getItemsId('1')
37
+ checkResponseStatus(getResponseData.response, 200)
38
+
39
+ const postResponseData = itemsClient.postItemsId('1', {
40
+ name: 'string',
41
+ })
42
+ checkResponseStatus(postResponseData.response, 201)
43
+
44
+ const putResponseData = itemsClient.putItemsId('1', {
45
+ description: 'string',
46
+ })
47
+ checkResponseStatus(putResponseData.response, 200)
48
+
49
+ const deleteResponseData = itemsClient.deleteItemsId('1')
50
+ checkResponseStatus(deleteResponseData.response, 204)
51
+
52
+ const patchResponseData = itemsClient.patchItemsId('1', {
53
+ name: 'string',
54
+ })
55
+ checkResponseStatus(patchResponseData.response, 200)
56
+
57
+ const headResponseData = itemsClient.headItemsId('1')
58
+ checkResponseStatus(headResponseData.response, 200)
59
+
60
+ const postFormUrlEncodedResponseDataFromItem =
61
+ itemsClient.postItemsFormUrlEncoded({
62
+ name: 'string',
63
+ })
64
+ checkResponseStatus(postFormUrlEncodedResponseDataFromItem.response, 201)
65
+ // Should add x-www-form-urlencoded content type header in request
66
+ check(postFormUrlEncodedResponseDataFromItem.response, {
67
+ 'has x-www-form-urlencoded content type header': (r) =>
68
+ r.request.headers['Content-Type'][0] ===
69
+ 'application/x-www-form-urlencoded',
70
+ })
71
+ // Items client calls end
72
+
73
+ // Items form client call start
74
+ const postFormUrlEncodedResponseData = itemFormClient.postItemsFormUrlEncoded(
75
+ {
76
+ name: 'string',
77
+ }
78
+ )
79
+ checkResponseStatus(postFormUrlEncodedResponseData.response, 201)
80
+ // Should add x-www-form-urlencoded content type header in request
81
+ check(postFormUrlEncodedResponseData.response, {
82
+ 'has x-www-form-urlencoded content type header': (r) =>
83
+ r.request.headers['Content-Type'][0] ===
84
+ 'application/x-www-form-urlencoded',
85
+ })
86
+
87
+ const postFormDataResponseData = itemFormClient.postItemsFormData({
88
+ name: 'string',
89
+ })
90
+ checkResponseStatus(postFormDataResponseData.response, 201)
91
+ // Should add multipart/form-data content type header in request with boundary
92
+ check(postFormDataResponseData.response, {
93
+ 'has multipart/form-data content type header with bounday': (r) =>
94
+ r.request.headers['Content-Type'][0].includes(
95
+ 'multipart/form-data; boundary=---'
96
+ ),
97
+ })
98
+ // Items form client call end
99
+
100
+ // Default client call start
101
+ const getItemsHeaderResponseData = defaultClient.getItemsHeader({
102
+ id: 'test',
103
+ })
104
+ checkResponseStatus(getItemsHeaderResponseData.response, 200)
105
+ // Default client call end
106
+ }
@@ -33,9 +33,6 @@
33
33
  },
34
34
  "expected_sdk": {
35
35
  "fileName": "simpleAPI.ts",
36
- "expectedSubstrings": [
37
- "export const createSimpleAPI =",
38
- "return { getExample };"
39
- ]
36
+ "expectedSubstrings": ["export class SimpleAPIClient"]
40
37
  }
41
38
  }
@@ -85,10 +85,10 @@
85
85
  "expected_sdk": {
86
86
  "fileName": "formDataAPI.ts",
87
87
  "expectedSubstrings": [
88
- "export const createFormDataAPI = (clientOptions: CreateFormDataAPIOptions) => {",
89
- "const formData = new FormData();\n formData.append(\"file\", postUploadBody.file);\n if (postUploadBody.description !== undefined) {\n formData.append(\"description\", postUploadBody.description);\n }\n formData.append(\"userId\", postUploadBody.userId);",
90
- "const url = new URL(cleanBaseUrl + `/upload`);",
91
- "const response = http.request(\"POST\", url.toString(), formData.body(), {\n ...mergedRequestParameters,\n headers: {\n \"Content-Type\": \"multipart/form-data; boundary=\" + formData.boundary,\n ...mergedRequestParameters?.headers,\n },\n });"
88
+ "export class FormDataAPIClient",
89
+ "const formData = new FormData(); formData.append(\"file\", postUploadBody.file); if (postUploadBody.description !== undefined) { formData.append(\"description\", postUploadBody.description); } formData.append(\"userId\", postUploadBody.userId);",
90
+ "const url = new URL(this.cleanBaseUrl + `/upload`);",
91
+ "const response = http.request(\"POST\", url.toString(), formData.body(), { ...mergedRequestParameters, headers: { \"Content-Type\": \"multipart/form-data; boundary=\" + formData.boundary, ...mergedRequestParameters?.headers, }, });"
92
92
  ]
93
93
  }
94
94
  }
@@ -85,9 +85,9 @@
85
85
  "expected_sdk": {
86
86
  "fileName": "formURLEncodedAPI.ts",
87
87
  "expectedSubstrings": [
88
- "export const createFormURLEncodedAPI = (\n clientOptions: CreateFormURLEncodedAPIOptions,\n) => {",
89
- "const url = new URL(cleanBaseUrl + `/submit-form`);",
90
- "const response = http.request(\n \"POST\",\n url.toString(),\n JSON.stringify(postSubmitFormBody),\n {\n ...mergedRequestParameters,\n headers: {\n \"Content-Type\": \"application/x-www-form-urlencoded\",\n ...mergedRequestParameters?.headers,\n },\n },\n );"
88
+ "export class FormURLEncodedAPIClient",
89
+ "const url = new URL(this.cleanBaseUrl + `/submit-form`);",
90
+ "const response = http.request( \"POST\", url.toString(), JSON.stringify(postSubmitFormBody), { ...mergedRequestParameters, headers: { \"Content-Type\": \"application/x-www-form-urlencoded\", ...mergedRequestParameters?.headers, }, }, );"
91
91
  ]
92
92
  }
93
93
  }
@@ -106,8 +106,8 @@
106
106
  "expected_sdk": {
107
107
  "fileName": "formURLEncodedAPIWithQueryParameters.ts",
108
108
  "expectedSubstrings": [
109
- "const postSubmitForm = (\n postSubmitFormBody: PostSubmitFormBody,\n params: PostSubmitFormParams,\n requestParameters?: Params,\n ): PostSubmitFormResponse => {",
110
- "const url = new URL(\n cleanBaseUrl +\n `/submit-form` +\n `?${new URLSearchParams(params).toString()}`,\n );"
109
+ "postSubmitForm( postSubmitFormBody: PostSubmitFormBody, params: PostSubmitFormParams, requestParameters?: Params, ): { response: Response; data: PostSubmitForm200; }",
110
+ "new URL( this.cleanBaseUrl + `/submit-form` + `?${new URLSearchParams(params).toString()}`, );"
111
111
  ]
112
112
  }
113
113
  }
@@ -59,8 +59,8 @@
59
59
  "expected_sdk": {
60
60
  "fileName": "simpleAPI.ts",
61
61
  "expectedSubstrings": [
62
- "const getItemById = (\n id: string,\n requestParameters?: Params,\n ): GetItemByIdResponse => {",
63
- "const url = new URL(cleanBaseUrl + `/items/${id}`);"
62
+ "getItemById( id: string, requestParameters?: Params, ): ",
63
+ "const url = new URL(this.cleanBaseUrl + `/items/${id}`);"
64
64
  ]
65
65
  }
66
66
  }
@@ -150,14 +150,13 @@
150
150
  "expected_sdk": {
151
151
  "fileName": "headerDemoAPI.ts",
152
152
  "expectedSubstrings": [
153
- "export const createHeaderDemoAPI = (\n clientOptions: CreateHeaderDemoAPIOptions,\n) => {",
154
- "const getExampleGet = (\n headers?: GetExampleGetHeaders,\n requestParameters?: Params,\n ): GetExampleGetResponse => {",
155
- "const response = http.request(\"GET\", url.toString(), undefined, {\n ...mergedRequestParameters,\n headers: {\n ...headers,\n ...mergedRequestParameters?.headers,\n },\n });",
156
- "const postExamplePost = (\n postExamplePostBody: PostExamplePostBody,\n headers: PostExamplePostHeaders,\n requestParameters?: Params,\n ): PostExamplePostResponse => {",
157
- "const response = http.request(\n \"POST\",\n url.toString(),\n JSON.stringify(postExamplePostBody),\n {\n ...mergedRequestParameters,\n headers: {\n \"Content-Type\": \"application/json\",\n ...headers,\n ...mergedRequestParameters?.headers,\n },\n },\n );",
158
- "const getExampleResponseHeaders = (\n requestParameters?: Params,\n ): GetExampleResponseHeadersResponse => {",
159
- "const response = http.request(\n \"GET\",\n url.toString(),\n undefined,\n mergedRequestParameters,\n );",
160
- "return { getExampleGet, postExamplePost, getExampleResponseHeaders };"
153
+ "export class HeaderDemoAPIClient",
154
+ "getExampleGet( headers?: GetExampleGetHeaders, requestParameters?: Params, ): { response: Response; data: GetExampleGet200; }",
155
+ "response = http.request(\"GET\", url.toString(), undefined, { ...mergedRequestParameters, headers: { // In the schema, headers can be of any type like number but k6 accepts only strings as headers, hence converting all headers to string ...Object.fromEntries( Object.entries(headers || {}).map(([key, value]) => [ key, String(value), ]), ), ...mergedRequestParameters?.headers, }, });",
156
+ "postExamplePost( postExamplePostBody: PostExamplePostBody, headers: PostExamplePostHeaders, requestParameters?: Params, ): { response: Response; data: void; }",
157
+ "response = http.request( \"POST\", url.toString(), JSON.stringify(postExamplePostBody), { ...mergedRequestParameters, headers: { \"Content-Type\": \"application/json\", // In the schema, headers can be of any type like number but k6 accepts only strings as headers, hence converting all headers to string ...Object.fromEntries( Object.entries(headers || {}).map(([key, value]) => [ key, String(value), ]), ), ...mergedRequestParameters?.headers, }, }, );",
158
+ "getExampleResponseHeaders(requestParameters?: Params): { response: Response; data: GetExampleResponseHeaders200; }",
159
+ "response = http.request( \"GET\", url.toString(), undefined, mergedRequestParameters, );"
161
160
  ]
162
161
  }
163
162
  }
@@ -31,10 +31,7 @@
31
31
  }
32
32
  },
33
33
  "expected_sdk": {
34
- "fileName": "K6Client.ts",
35
- "expectedSubstrings": [
36
- "export const createK6Client =",
37
- "return { getExample };"
38
- ]
34
+ "fileName": "k6Client.ts",
35
+ "expectedSubstrings": ["export class K6ClientClient"]
39
36
  }
40
37
  }
@@ -86,10 +86,9 @@
86
86
  "expected_sdk": {
87
87
  "fileName": "exampleAPI.ts",
88
88
  "expectedSubstrings": [
89
- "export const createExampleAPI = (clientOptions: CreateExampleAPIOptions) => {",
90
- "const createExampleData = (\n createExampleDataBody: CreateExampleDataBody,\n params: CreateExampleDataParams,\n requestParameters?: Params,\n ): CreateExampleDataResponse => {",
91
- "cleanBaseUrl + `/example` + `?${new URLSearchParams(params).toString()}`,",
92
- "return { createExampleData };"
89
+ "export class ExampleAPIClient {",
90
+ "createExampleData( createExampleDataBody: CreateExampleDataBody, params: CreateExampleDataParams, requestParameters?: Params, ):",
91
+ "cleanBaseUrl + `/example` + `?${new URLSearchParams(params).toString()}`,"
93
92
  ]
94
93
  }
95
94
  }
@@ -107,9 +107,9 @@
107
107
  "expected_sdk": {
108
108
  "fileName": "exampleAPI.ts",
109
109
  "expectedSubstrings": [
110
- "export const createExampleAPI =",
111
- "cleanBaseUrl + `/example` + `?${new URLSearchParams(params).toString()}`",
112
- "return { getExampleData };"
110
+ "export class ExampleAPIClient {",
111
+ "this.cleanBaseUrl + `/example` + `?${new URLSearchParams(params).toString()}`",
112
+ "getExampleData("
113
113
  ]
114
114
  }
115
115
  }
@@ -120,13 +120,11 @@
120
120
  "expected_sdk": {
121
121
  "fileName": "exampleAPI.ts",
122
122
  "expectedSubstrings": [
123
- "export const createExampleAPI =",
124
- "const url = new URL(cleanBaseUrl + `/example`);",
123
+ "export class ExampleAPIClient {",
124
+ "const url = new URL(this.cleanBaseUrl + `/example`);",
125
125
  "JSON.stringify(createExampleDataBody),",
126
126
  "POST",
127
- "export type CreateExampleData201 = {\n age?: number;\n date?: string;\n /** The unique ID of the created resource */\n id?: string;\n isActive?: boolean;\n meta?: CreateExampleData201Meta;\n name?: string;\n tags?: string[];",
128
- "): CreateExampleDataResponse => {",
129
- "return { createExampleData };"
127
+ "export type CreateExampleData201 = { age?: number; date?: string; /** The unique ID of the created resource */ id?: string; isActive?: boolean; meta?: CreateExampleData201Meta; name?: string; tags?: string[];"
130
128
  ]
131
129
  }
132
130
  }
@@ -2,6 +2,7 @@ import fs from 'fs'
2
2
  import os from 'os'
3
3
  import path from 'path'
4
4
  import { promisify } from 'util'
5
+ import { Mode } from '../../src/constants'
5
6
  import generator from '../../src/generator'
6
7
 
7
8
  const writeFile = promisify(fs.writeFile)
@@ -21,13 +22,17 @@ const commonSubstringsForAllSDK = [
21
22
  'Automatically generated by',
22
23
  'Do not edit manually',
23
24
  'OpenAPI spec version',
24
- 'const mergedRequestParameters = _mergeRequestParameters(\n requestParameters || {},\n clientOptions.commonRequestParameters,\n );',
25
- 'try {\n data = response.json();\n } catch (error) {\n data = response.body;\n }',
26
- 'return {\n response,\n data,\n };',
25
+ 'const mergedRequestParameters = this._mergeRequestParameters( requestParameters || {}, this.commonRequestParameters, );',
26
+ 'try { data = response.json(); } catch { data = response.body; }',
27
+ 'return { response, data, };',
27
28
  ]
28
29
 
29
30
  const commonSubstringsForK6SampleScript = [`const baseUrl = "<BASE_URL>";`]
30
31
 
32
+ function replaceSpacesAndNewLineToSingleSpace(input: string): string {
33
+ return input.replace(/\s+/g, ' ')
34
+ }
35
+
31
36
  describe('generator', () => {
32
37
  let tempDir: string, schemaDirectory: string
33
38
  const allFixtures = fs.readdirSync(fixturesDir)
@@ -65,7 +70,11 @@ describe('generator', () => {
65
70
  it(`should generate SDK client from the OpenAPI schema`, async () => {
66
71
  const expectedGeneratedCode = fixture['expected_sdk']
67
72
 
68
- await generator({ openApiPath, outputDir: generatedSchemaPath })
73
+ await generator({
74
+ openApiPath,
75
+ outputDir: generatedSchemaPath,
76
+ mode: Mode.SINGLE,
77
+ })
69
78
 
70
79
  const generatedFiles = fs.readdirSync(generatedSchemaPath)
71
80
  expect(generatedFiles.length).toBe(1)
@@ -82,7 +91,9 @@ describe('generator', () => {
82
91
  ...expectedGeneratedCode['expectedSubstrings'],
83
92
  ...commonSubstringsForAllSDK,
84
93
  ]) {
85
- expect(generatedContent).toContain(expectedString)
94
+ expect(
95
+ replaceSpacesAndNewLineToSingleSpace(generatedContent)
96
+ ).toContain(expectedString)
86
97
  }
87
98
  })
88
99
 
@@ -91,6 +102,7 @@ describe('generator', () => {
91
102
  openApiPath,
92
103
  outputDir: generatedSchemaPath,
93
104
  shouldGenerateSampleK6Script: true,
105
+ mode: Mode.SINGLE,
94
106
  })
95
107
 
96
108
  const generatedFiles = fs.readdirSync(generatedSchemaPath)
@@ -108,6 +120,35 @@ describe('generator', () => {
108
120
  expect(generatedContent).toContain(expectedString)
109
121
  }
110
122
  })
123
+
124
+ it('should not contain types in main file when using split mode', async () => {
125
+ await generator({
126
+ openApiPath,
127
+ outputDir: generatedSchemaPath,
128
+ shouldGenerateSampleK6Script: true,
129
+ mode: Mode.SPLIT,
130
+ })
131
+ const expectedGeneratedCode = fixture['expected_sdk']
132
+ const generatedFiles = fs.readdirSync(generatedSchemaPath)
133
+
134
+ expect(generatedFiles.length).toBe(3)
135
+
136
+ const clientFile = generatedFiles.find((file) =>
137
+ file.includes(expectedGeneratedCode.fileName)
138
+ )
139
+ const schemaFile = generatedFiles.find((file) =>
140
+ file.includes('.schemas.ts')
141
+ )
142
+
143
+ expect(clientFile).toBeDefined()
144
+ expect(schemaFile).toBeDefined()
145
+
146
+ const generatedFilePath = path.join(generatedSchemaPath, clientFile!)
147
+ const generatedContent = await readFile(generatedFilePath, 'utf-8')
148
+
149
+ expect(generatedContent).not.toContain('export type')
150
+ expect(generatedContent).not.toContain('export interface')
151
+ })
111
152
  })
112
153
  }
113
154
  })
@@ -1,87 +0,0 @@
1
- /**
2
- * Automatically generated by @grafana/openapi-to-k6: 0.1.2
3
- * Do not edit manually.
4
- * Simple API
5
- * OpenAPI spec version: 1.0.0
6
- */
7
- import { URL } from 'https://jslib.k6.io/url/1.0.0/index.js'
8
- import http from 'k6/http'
9
- import type { Params, Response, ResponseBody } from 'k6/http'
10
- export type GetExample200 = {
11
- message?: string
12
- }
13
-
14
- export type CreateSimpleAPIOptions = {
15
- baseUrl: string
16
- commonRequestParameters?: Params
17
- }
18
-
19
- /**
20
- * This is the base client to use for interacting with the API.
21
- */
22
- export const createSimpleAPI = (clientOptions: CreateSimpleAPIOptions) => {
23
- const cleanBaseUrl = clientOptions.baseUrl.replace(/\/+$/, '')
24
- /**
25
- * @summary Retrieve example data
26
- */
27
- const getExample = (requestParameters?: Params): GetExampleResponse => {
28
- const url = new URL(cleanBaseUrl + `/example`)
29
- const mergedRequestParameters = _mergeRequestParameters(
30
- requestParameters || {},
31
- clientOptions.commonRequestParameters
32
- )
33
- const response = http.request(
34
- 'GET',
35
- url.toString(),
36
- undefined,
37
- mergedRequestParameters
38
- )
39
- let data
40
-
41
- try {
42
- data = response.json()
43
- } catch (error) {
44
- data = response.body
45
- }
46
- return {
47
- response,
48
- data,
49
- }
50
- }
51
-
52
- return { getExample }
53
- }
54
-
55
- export type GetExampleResponse = {
56
- response: Response
57
- data: GetExample200 | ResponseBody
58
- }
59
-
60
- /**
61
- * Merges the provided request parameters with default parameters for the client.
62
- *
63
- * @param {Params} requestParameters - The parameters provided specifically for the request
64
- * @param {Params} commonRequestParameters - Common parameters for all requests
65
- * @returns {Params} - The merged parameters
66
- */
67
- const _mergeRequestParameters = (
68
- requestParameters?: Params,
69
- commonRequestParameters?: Params
70
- ): Params => {
71
- return {
72
- ...commonRequestParameters, // Default to common parameters
73
- ...requestParameters, // Override with request-specific parameters
74
- headers: {
75
- ...(commonRequestParameters?.headers || {}), // Ensure headers are defined
76
- ...(requestParameters?.headers || {}),
77
- },
78
- cookies: {
79
- ...(commonRequestParameters?.cookies || {}), // Ensure cookies are defined
80
- ...(requestParameters?.cookies || {}),
81
- },
82
- tags: {
83
- ...(commonRequestParameters?.tags || {}), // Ensure tags are defined
84
- ...(requestParameters?.tags || {}),
85
- },
86
- }
87
- }
@@ -1,115 +0,0 @@
1
- /**
2
- * Automatically generated by @grafana/openapi-to-k6: 0.1.2
3
- * Do not edit manually.
4
- * Form Data API
5
- * OpenAPI spec version: 1.0.0
6
- */
7
- import { FormData } from 'https://jslib.k6.io/formdata/0.0.2/index.js'
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, ResponseBody } from 'k6/http'
11
- export type PostUpload400 = {
12
- error?: string
13
- success?: boolean
14
- }
15
-
16
- export type PostUpload200 = {
17
- message?: string
18
- success?: boolean
19
- }
20
-
21
- export type PostUploadBody = {
22
- /** Description of the file */
23
- description?: string
24
- /** File to upload */
25
- file: Blob
26
- /** User ID associated with the upload */
27
- userId: string
28
- }
29
-
30
- export type CreateFormDataAPIOptions = {
31
- baseUrl: string
32
- commonRequestParameters?: Params
33
- }
34
-
35
- /**
36
- * This is the base client to use for interacting with the API.
37
- */
38
- export const createFormDataAPI = (clientOptions: CreateFormDataAPIOptions) => {
39
- const cleanBaseUrl = clientOptions.baseUrl.replace(/\/+$/, '')
40
- /**
41
- * This endpoint accepts form data and file uploads.
42
- * @summary Upload files and data
43
- */
44
- const postUpload = (
45
- postUploadBody: PostUploadBody,
46
- requestParameters?: Params
47
- ): PostUploadResponse => {
48
- const formData = new FormData()
49
- formData.append('file', postUploadBody.file)
50
- if (postUploadBody.description !== undefined) {
51
- formData.append('description', postUploadBody.description)
52
- }
53
- formData.append('userId', postUploadBody.userId)
54
-
55
- const url = new URL(cleanBaseUrl + `/upload`)
56
- const mergedRequestParameters = _mergeRequestParameters(
57
- requestParameters || {},
58
- clientOptions.commonRequestParameters
59
- )
60
- const response = http.request('POST', url.toString(), formData.body(), {
61
- ...mergedRequestParameters,
62
- headers: {
63
- 'Content-Type': 'multipart/form-data; boundary=' + formData.boundary,
64
- ...mergedRequestParameters?.headers,
65
- },
66
- })
67
- let data
68
-
69
- try {
70
- data = response.json()
71
- } catch (error) {
72
- data = response.body
73
- }
74
- return {
75
- response,
76
- data,
77
- }
78
- }
79
-
80
- return { postUpload }
81
- }
82
-
83
- export type PostUploadResponse = {
84
- response: Response
85
- data: PostUpload200 | ResponseBody
86
- }
87
-
88
- /**
89
- * Merges the provided request parameters with default parameters for the client.
90
- *
91
- * @param {Params} requestParameters - The parameters provided specifically for the request
92
- * @param {Params} commonRequestParameters - Common parameters for all requests
93
- * @returns {Params} - The merged parameters
94
- */
95
- const _mergeRequestParameters = (
96
- requestParameters?: Params,
97
- commonRequestParameters?: Params
98
- ): Params => {
99
- return {
100
- ...commonRequestParameters, // Default to common parameters
101
- ...requestParameters, // Override with request-specific parameters
102
- headers: {
103
- ...(commonRequestParameters?.headers || {}), // Ensure headers are defined
104
- ...(requestParameters?.headers || {}),
105
- },
106
- cookies: {
107
- ...(commonRequestParameters?.cookies || {}), // Ensure cookies are defined
108
- ...(requestParameters?.cookies || {}),
109
- },
110
- tags: {
111
- ...(commonRequestParameters?.tags || {}), // Ensure tags are defined
112
- ...(requestParameters?.tags || {}),
113
- },
114
- }
115
- }
@@ -1,114 +0,0 @@
1
- /**
2
- * Automatically generated by @grafana/openapi-to-k6: 0.1.2
3
- * Do not edit manually.
4
- * Form URL Encoded API
5
- * OpenAPI spec version: 1.0.0
6
- */
7
- import { URL } from 'https://jslib.k6.io/url/1.0.0/index.js'
8
- import http from 'k6/http'
9
- import type { Params, Response, ResponseBody } from 'k6/http'
10
- export type PostSubmitForm400 = {
11
- error?: string
12
- success?: boolean
13
- }
14
-
15
- export type PostSubmitForm200 = {
16
- message?: string
17
- success?: boolean
18
- }
19
-
20
- export type PostSubmitFormBody = {
21
- /** Age of the user */
22
- age?: number
23
- /** Email address of the user */
24
- email: string
25
- /** Name of the user */
26
- name: string
27
- }
28
-
29
- export type CreateFormURLEncodedAPIOptions = {
30
- baseUrl: string
31
- commonRequestParameters?: Params
32
- }
33
-
34
- /**
35
- * This is the base client to use for interacting with the API.
36
- */
37
- export const createFormURLEncodedAPI = (
38
- clientOptions: CreateFormURLEncodedAPIOptions
39
- ) => {
40
- const cleanBaseUrl = clientOptions.baseUrl.replace(/\/+$/, '')
41
- /**
42
- * This endpoint accepts form URL-encoded data.
43
- * @summary Submit form data
44
- */
45
- const postSubmitForm = (
46
- postSubmitFormBody: PostSubmitFormBody,
47
- requestParameters?: Params
48
- ): PostSubmitFormResponse => {
49
- const url = new URL(cleanBaseUrl + `/submit-form`)
50
- const mergedRequestParameters = _mergeRequestParameters(
51
- requestParameters || {},
52
- clientOptions.commonRequestParameters
53
- )
54
- const response = http.request(
55
- 'POST',
56
- url.toString(),
57
- JSON.stringify(postSubmitFormBody),
58
- {
59
- ...mergedRequestParameters,
60
- headers: {
61
- 'Content-Type': 'application/x-www-form-urlencoded',
62
- ...mergedRequestParameters?.headers,
63
- },
64
- }
65
- )
66
- let data
67
-
68
- try {
69
- data = response.json()
70
- } catch (error) {
71
- data = response.body
72
- }
73
- return {
74
- response,
75
- data,
76
- }
77
- }
78
-
79
- return { postSubmitForm }
80
- }
81
-
82
- export type PostSubmitFormResponse = {
83
- response: Response
84
- data: PostSubmitForm200 | ResponseBody
85
- }
86
-
87
- /**
88
- * Merges the provided request parameters with default parameters for the client.
89
- *
90
- * @param {Params} requestParameters - The parameters provided specifically for the request
91
- * @param {Params} commonRequestParameters - Common parameters for all requests
92
- * @returns {Params} - The merged parameters
93
- */
94
- const _mergeRequestParameters = (
95
- requestParameters?: Params,
96
- commonRequestParameters?: Params
97
- ): Params => {
98
- return {
99
- ...commonRequestParameters, // Default to common parameters
100
- ...requestParameters, // Override with request-specific parameters
101
- headers: {
102
- ...(commonRequestParameters?.headers || {}), // Ensure headers are defined
103
- ...(requestParameters?.headers || {}),
104
- },
105
- cookies: {
106
- ...(commonRequestParameters?.cookies || {}), // Ensure cookies are defined
107
- ...(requestParameters?.cookies || {}),
108
- },
109
- tags: {
110
- ...(commonRequestParameters?.tags || {}), // Ensure tags are defined
111
- ...(requestParameters?.tags || {}),
112
- },
113
- }
114
- }