@grafana/openapi-to-k6 0.3.0 → 0.3.2

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 (65) hide show
  1. package/dist/generator/index.js +13 -6
  2. package/dist/generator/k6Client.js +3 -1
  3. package/dist/helper.js +16 -0
  4. package/examples/basic_schema/schema.json +2 -1
  5. package/examples/basic_schema/single/simpleAPI.ts +4 -1
  6. package/examples/basic_schema/split/simpleAPI.schemas.ts +2 -1
  7. package/examples/basic_schema/split/simpleAPI.ts +3 -1
  8. package/examples/basic_schema/tags/default.ts +3 -1
  9. package/examples/basic_schema/tags/simpleAPI.schemas.ts +2 -1
  10. package/examples/form_data_schema/single/formDataAPI.ts +15 -13
  11. package/examples/form_data_schema/split/formDataAPI.schemas.ts +12 -12
  12. package/examples/form_data_schema/split/formDataAPI.ts +4 -2
  13. package/examples/form_data_schema/tags/default.ts +4 -2
  14. package/examples/form_data_schema/tags/formDataAPI.schemas.ts +12 -12
  15. package/examples/form_url_encoded_data_schema/single/formURLEncodedAPI.ts +15 -13
  16. package/examples/form_url_encoded_data_schema/split/formURLEncodedAPI.schemas.ts +12 -12
  17. package/examples/form_url_encoded_data_schema/split/formURLEncodedAPI.ts +4 -2
  18. package/examples/form_url_encoded_data_schema/tags/default.ts +4 -2
  19. package/examples/form_url_encoded_data_schema/tags/formURLEncodedAPI.schemas.ts +12 -12
  20. package/examples/form_url_encoded_data_with_query_params_schema/single/formURLEncodedAPIWithQueryParameters.ts +23 -21
  21. package/examples/form_url_encoded_data_with_query_params_schema/split/formURLEncodedAPIWithQueryParameters.schemas.ts +20 -20
  22. package/examples/form_url_encoded_data_with_query_params_schema/split/formURLEncodedAPIWithQueryParameters.ts +4 -2
  23. package/examples/form_url_encoded_data_with_query_params_schema/tags/default.ts +4 -2
  24. package/examples/form_url_encoded_data_with_query_params_schema/tags/formURLEncodedAPIWithQueryParameters.schemas.ts +20 -20
  25. package/examples/get_request_with_path_parameters_schema/single/simpleAPI.ts +4 -2
  26. package/examples/get_request_with_path_parameters_schema/split/simpleAPI.schemas.ts +2 -2
  27. package/examples/get_request_with_path_parameters_schema/split/simpleAPI.ts +3 -1
  28. package/examples/get_request_with_path_parameters_schema/tags/default.ts +3 -1
  29. package/examples/get_request_with_path_parameters_schema/tags/simpleAPI.schemas.ts +2 -2
  30. package/examples/headers_schema/single/headerDemoAPI.ts +16 -14
  31. package/examples/headers_schema/split/headerDemoAPI.schemas.ts +12 -12
  32. package/examples/headers_schema/split/headerDemoAPI.ts +5 -3
  33. package/examples/headers_schema/tags/default.ts +5 -3
  34. package/examples/headers_schema/tags/headerDemoAPI.schemas.ts +12 -12
  35. package/examples/no_title_schema/single/k6Client.ts +3 -1
  36. package/examples/no_title_schema/split/k6Client.schemas.ts +1 -1
  37. package/examples/no_title_schema/split/k6Client.ts +3 -1
  38. package/examples/no_title_schema/tags/default.ts +3 -1
  39. package/examples/no_title_schema/tags/k6Client.schemas.ts +1 -1
  40. package/examples/post_request_with_query_params/single/exampleAPI.ts +20 -18
  41. package/examples/post_request_with_query_params/split/exampleAPI.schemas.ts +17 -17
  42. package/examples/post_request_with_query_params/split/exampleAPI.ts +4 -2
  43. package/examples/post_request_with_query_params/tags/default.ts +4 -2
  44. package/examples/post_request_with_query_params/tags/exampleAPI.schemas.ts +17 -17
  45. package/examples/query_params_schema/single/exampleAPI.ts +11 -9
  46. package/examples/query_params_schema/split/exampleAPI.schemas.ts +9 -9
  47. package/examples/query_params_schema/split/exampleAPI.ts +3 -1
  48. package/examples/query_params_schema/tags/default.ts +3 -1
  49. package/examples/query_params_schema/tags/exampleAPI.schemas.ts +9 -9
  50. package/examples/simple_post_request_schema/single/exampleAPI.ts +25 -23
  51. package/examples/simple_post_request_schema/split/exampleAPI.schemas.ts +22 -22
  52. package/examples/simple_post_request_schema/split/exampleAPI.ts +4 -2
  53. package/examples/simple_post_request_schema/tags/default.ts +4 -2
  54. package/examples/simple_post_request_schema/tags/exampleAPI.schemas.ts +22 -22
  55. package/package.json +3 -2
  56. package/src/generator/index.ts +20 -7
  57. package/src/generator/k6Client.ts +3 -1
  58. package/src/helper.ts +17 -0
  59. package/tests/e2e/single/k6Script.ts +54 -1
  60. package/tests/functional-tests/test-generator/fixtures/basic_parameter_in_ref.json +59 -0
  61. package/tests/functional-tests/test-generator/fixtures/form_data_schema.json +1 -1
  62. package/tests/functional-tests/test-generator/fixtures/form_url_encoded_data_schema.json +1 -1
  63. package/tests/functional-tests/test-generator/fixtures/headers_schema.json +2 -2
  64. package/tests/functional-tests/test-generator/fixtures/simple_post_request_schema.json +1 -1
  65. package/{vite.config.js → vite.config.mjs} +4 -0
@@ -3,8 +3,15 @@ import { check } from 'k6'
3
3
  import { ComprehensiveAPIClient } from './sdk.ts'
4
4
  /* eslint-enable import/no-unresolved */
5
5
 
6
+ const commonRequestParameters = {
7
+ headers: {
8
+ 'Only-Common-Header': 'test',
9
+ 'Only-Common-Header-2': 'test-2',
10
+ 'X-Correlation-ID': 'only-common-header-value',
11
+ },
12
+ }
6
13
  const baseUrl = 'http://localhost:3000'
7
- const client = new ComprehensiveAPIClient({ baseUrl })
14
+ const client = new ComprehensiveAPIClient({ baseUrl, commonRequestParameters })
8
15
 
9
16
  export const options = {
10
17
  thresholds: {
@@ -79,4 +86,50 @@ export default function () {
79
86
  id: 'test',
80
87
  })
81
88
  checkResponseStatus(getItemsHeaderResponseData.response, 200)
89
+
90
+ const requestParameters = {
91
+ headers: {
92
+ 'Only-Common-Header-2': 'test-common-header-2-parameter',
93
+ 'X-Correlation-ID': 'test-request-parameter',
94
+ },
95
+ }
96
+ const getItemsNewMandatoryHeadersResponseData =
97
+ client.getItemsNewMandatoryHeaders(
98
+ {
99
+ itemId: 'test',
100
+ },
101
+ {
102
+ 'X-Auth-Token': 'test',
103
+ 'X-Correlation-ID': 'test-function-argument',
104
+ },
105
+ requestParameters
106
+ )
107
+ checkResponseStatus(getItemsNewMandatoryHeadersResponseData.response, 200)
108
+
109
+ const requestHeaders =
110
+ getItemsNewMandatoryHeadersResponseData.response.request.headers
111
+ console.log('requestHeaders')
112
+ console.log(requestHeaders)
113
+
114
+ // Validate that the header present only in common request parameters is present in the request
115
+ check(requestHeaders, {
116
+ 'has only-common-header': (r) => r['Only-Common-Header'][0] === 'test',
117
+ })
118
+
119
+ // Validate that the header present only in the request parameters is present in the request
120
+ check(requestHeaders, {
121
+ 'has X-Auth-Token': (r) => r['X-Auth-Token'][0] === 'test',
122
+ })
123
+
124
+ // Validate that the header present in the requestParameters takes precedence over the value in common request parameters
125
+ check(requestHeaders, {
126
+ 'has correct value of Only-Common-Header-2 from request parameters': (r) =>
127
+ r['Only-Common-Header-2'][0] === 'test-common-header-2-parameter',
128
+ })
129
+
130
+ // Validate that the value of the header present in function arguments takes precedence over the value in common request parameters and request parameters
131
+ check(requestHeaders, {
132
+ 'has correct value X-Correlation-ID from function arguments': (r) =>
133
+ r['X-Correlation-Id'][0] === 'test-function-argument',
134
+ })
82
135
  }
@@ -0,0 +1,59 @@
1
+ {
2
+ "openapi_schema": {
3
+ "openapi": "3.0.0",
4
+ "info": {
5
+ "title": "Simple API",
6
+ "version": "1.0.0"
7
+ },
8
+ "paths": {
9
+ "/example": {
10
+ "get": {
11
+ "summary": "Retrieve example data",
12
+ "parameters": [
13
+ {
14
+ "$ref": "#/components/parameters/stackId"
15
+ }
16
+ ],
17
+ "responses": {
18
+ "200": {
19
+ "description": "Successful response",
20
+ "content": {
21
+ "application/json": {
22
+ "schema": {
23
+ "type": "object",
24
+ "properties": {
25
+ "message": {
26
+ "type": "string",
27
+ "example": "Hello, World!"
28
+ }
29
+ }
30
+ }
31
+ }
32
+ }
33
+ }
34
+ }
35
+ }
36
+ }
37
+ },
38
+ "components": {
39
+ "parameters": {
40
+ "stackId": {
41
+ "in": "header",
42
+ "name": "X-Stack-Id",
43
+ "schema": {
44
+ "type": "integer"
45
+ },
46
+ "required": true
47
+ }
48
+ }
49
+ }
50
+ },
51
+ "expected_sdk": {
52
+ "fileName": "simpleAPI.ts",
53
+ "expectedSubstrings": [
54
+ "export class SimpleAPIClient",
55
+ "export type StackIdParameter = number;",
56
+ "export type GetExampleHeaders = { \"X-Stack-Id\": StackIdParameter; };"
57
+ ]
58
+ }
59
+ }
@@ -88,7 +88,7 @@
88
88
  "export class FormDataAPIClient",
89
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
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, }, });"
91
+ "const response = http.request(\"POST\", url.toString(), formData.body(), { ...mergedRequestParameters, headers: { ...mergedRequestParameters?.headers, \"Content-Type\": \"multipart/form-data; boundary=\" + formData.boundary, }, });"
92
92
  ]
93
93
  }
94
94
  }
@@ -87,7 +87,7 @@
87
87
  "expectedSubstrings": [
88
88
  "export class FormURLEncodedAPIClient",
89
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, }, }, );"
90
+ "const response = http.request( \"POST\", url.toString(), JSON.stringify(postSubmitFormBody), { ...mergedRequestParameters, headers: { ...mergedRequestParameters?.headers, \"Content-Type\": \"application/x-www-form-urlencoded\", }, }, );"
91
91
  ]
92
92
  }
93
93
  }
@@ -152,9 +152,9 @@
152
152
  "expectedSubstrings": [
153
153
  "export class HeaderDemoAPIClient",
154
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, }, });",
155
+ "response = http.request(\"GET\", url.toString(), undefined, { ...mergedRequestParameters, headers: { ...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), ]), ), }, });",
156
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, }, }, );",
157
+ "response = http.request( \"POST\", url.toString(), JSON.stringify(postExamplePostBody), { ...mergedRequestParameters, headers: { ...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), ]), ), }, }, );",
158
158
  "getExampleResponseHeaders(requestParameters?: Params): { response: Response; data: GetExampleResponseHeaders200; }",
159
159
  "response = http.request( \"GET\", url.toString(), undefined, mergedRequestParameters, );"
160
160
  ]
@@ -124,7 +124,7 @@
124
124
  "const url = new URL(this.cleanBaseUrl + `/example`);",
125
125
  "JSON.stringify(createExampleDataBody),",
126
126
  "POST",
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[];"
127
+ "export type CreateExampleData201 = { /** The unique ID of the created resource */ id?: string; name?: string; age?: number; isActive?: boolean; tags?: string[]; date?: string; meta?: CreateExampleData201Meta; };"
128
128
  ]
129
129
  }
130
130
  }
@@ -1,8 +1,12 @@
1
1
  import { defineConfig } from 'vite'
2
+ import tsconfigPaths from 'vite-tsconfig-paths'
2
3
 
3
4
  export default defineConfig({
5
+ plugins: [tsconfigPaths()],
4
6
  test: {
5
7
  environment: 'node',
8
+ globals: true,
9
+ include: ['tests/**/*.test.ts'],
6
10
  coverage: {
7
11
  reporter: ['json', 'text', 'lcov'],
8
12
  reportsDirectory: 'coverage',