@grafana/openapi-to-k6 0.3.1 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/publish.yaml +39 -13
- package/.github/workflows/tests.yaml +23 -10
- package/.nvmrc +1 -1
- package/CODEOWNERS +2 -0
- package/README.md +34 -39
- package/catalog-info.yaml +13 -0
- package/dist/generator/k6Client.js +7 -4
- package/examples/basic_schema/single/simpleAPI.ts +5 -3
- package/examples/basic_schema/split/simpleAPI.schemas.ts +1 -1
- package/examples/basic_schema/split/simpleAPI.ts +5 -3
- package/examples/basic_schema/tags/default.ts +5 -3
- package/examples/basic_schema/tags/simpleAPI.schemas.ts +1 -1
- package/examples/form_data_schema/single/formDataAPI.ts +15 -13
- package/examples/form_data_schema/split/formDataAPI.schemas.ts +11 -11
- package/examples/form_data_schema/split/formDataAPI.ts +5 -3
- package/examples/form_data_schema/tags/default.ts +5 -3
- package/examples/form_data_schema/tags/formDataAPI.schemas.ts +11 -11
- package/examples/form_url_encoded_data_schema/single/formURLEncodedAPI.ts +15 -13
- package/examples/form_url_encoded_data_schema/split/formURLEncodedAPI.schemas.ts +11 -11
- package/examples/form_url_encoded_data_schema/split/formURLEncodedAPI.ts +5 -3
- package/examples/form_url_encoded_data_schema/tags/default.ts +5 -3
- package/examples/form_url_encoded_data_schema/tags/formURLEncodedAPI.schemas.ts +11 -11
- package/examples/form_url_encoded_data_with_query_params_schema/single/formURLEncodedAPIWithQueryParameters.ts +22 -20
- package/examples/form_url_encoded_data_with_query_params_schema/split/formURLEncodedAPIWithQueryParameters.schemas.ts +18 -18
- package/examples/form_url_encoded_data_with_query_params_schema/split/formURLEncodedAPIWithQueryParameters.ts +5 -3
- package/examples/form_url_encoded_data_with_query_params_schema/tags/default.ts +5 -3
- package/examples/form_url_encoded_data_with_query_params_schema/tags/formURLEncodedAPIWithQueryParameters.schemas.ts +18 -18
- package/examples/get_request_with_path_parameters_schema/single/simpleAPI.ts +5 -3
- package/examples/get_request_with_path_parameters_schema/split/simpleAPI.schemas.ts +1 -1
- package/examples/get_request_with_path_parameters_schema/split/simpleAPI.ts +5 -3
- package/examples/get_request_with_path_parameters_schema/tags/default.ts +5 -3
- package/examples/get_request_with_path_parameters_schema/tags/simpleAPI.schemas.ts +1 -1
- package/examples/headers_schema/single/headerDemoAPI.ts +24 -18
- package/examples/headers_schema/split/headerDemoAPI.schemas.ts +12 -12
- package/examples/headers_schema/split/headerDemoAPI.ts +13 -7
- package/examples/headers_schema/tags/default.ts +13 -7
- package/examples/headers_schema/tags/headerDemoAPI.schemas.ts +12 -12
- package/examples/no_title_schema/single/k6Client.ts +5 -3
- package/examples/no_title_schema/split/k6Client.schemas.ts +1 -1
- package/examples/no_title_schema/split/k6Client.ts +5 -3
- package/examples/no_title_schema/tags/default.ts +5 -3
- package/examples/no_title_schema/tags/k6Client.schemas.ts +1 -1
- package/examples/post_request_with_query_params/single/exampleAPI.ts +21 -19
- package/examples/post_request_with_query_params/split/exampleAPI.schemas.ts +17 -17
- package/examples/post_request_with_query_params/split/exampleAPI.ts +5 -3
- package/examples/post_request_with_query_params/tags/default.ts +5 -3
- package/examples/post_request_with_query_params/tags/exampleAPI.schemas.ts +17 -17
- package/examples/query_params_schema/single/exampleAPI.ts +13 -11
- package/examples/query_params_schema/split/exampleAPI.schemas.ts +9 -9
- package/examples/query_params_schema/split/exampleAPI.ts +5 -3
- package/examples/query_params_schema/tags/default.ts +5 -3
- package/examples/query_params_schema/tags/exampleAPI.schemas.ts +9 -9
- package/examples/simple_post_request_schema/single/exampleAPI.ts +21 -19
- package/examples/simple_post_request_schema/split/exampleAPI.schemas.ts +17 -17
- package/examples/simple_post_request_schema/split/exampleAPI.ts +5 -3
- package/examples/simple_post_request_schema/tags/default.ts +5 -3
- package/examples/simple_post_request_schema/tags/exampleAPI.schemas.ts +17 -17
- package/package.json +20 -20
- package/renovate.json +6 -0
- package/src/generator/k6Client.ts +9 -3
- package/tests/functional-tests/test-generator/fixtures/form_data_schema.json +2 -2
- package/tests/functional-tests/test-generator/fixtures/form_url_encoded_data_schema.json +2 -2
- package/tests/functional-tests/test-generator/fixtures/form_url_encoded_data_with_query_params_schema.json +1 -1
- package/tests/functional-tests/test-generator/fixtures/get_request_with_path_parameters_schema.json +1 -1
- package/tests/functional-tests/test-generator/fixtures/headers_schema.json +6 -6
- package/tests/functional-tests/test-generator/fixtures/simple_post_request_schema.json +1 -1
- package/tests/functional-tests/test-generator/generator.test.ts +9 -1
|
@@ -9,12 +9,20 @@ jobs:
|
|
|
9
9
|
build:
|
|
10
10
|
runs-on: ubuntu-latest
|
|
11
11
|
permissions:
|
|
12
|
-
contents:
|
|
12
|
+
contents: read
|
|
13
|
+
packages: write
|
|
14
|
+
id-token: write
|
|
13
15
|
steps:
|
|
14
|
-
- uses: actions/
|
|
16
|
+
- uses: grafana/shared-workflows/actions/get-vault-secrets@a37de51f3d713a30a9e4b21bcdfbd38170020593 # get-vault-secrets/v1.3.0
|
|
17
|
+
with:
|
|
18
|
+
# Secrets placed in the ci/repo/grafana/<repo>/<path> path in Vault
|
|
19
|
+
# Also stored in 1Password as "k6 Cloud CI/CD Secrets" (Vault is write-only)
|
|
20
|
+
repo_secrets: |
|
|
21
|
+
NPM_TOKEN=npm:token
|
|
22
|
+
- uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6
|
|
15
23
|
with:
|
|
16
24
|
fetch-depth: 0 # Fetch all history for all branches and tags
|
|
17
|
-
- uses: actions/setup-node@
|
|
25
|
+
- uses: actions/setup-node@395ad3262231945c25e8478fd5baf05154b1d79f # v6
|
|
18
26
|
with:
|
|
19
27
|
node-version: '21'
|
|
20
28
|
registry-url: 'https://registry.npmjs.org'
|
|
@@ -22,20 +30,38 @@ jobs:
|
|
|
22
30
|
cache: 'npm'
|
|
23
31
|
cache-dependency-path: 'package-lock.json'
|
|
24
32
|
- run: npm ci
|
|
25
|
-
- name:
|
|
33
|
+
- name: Validate version matches release tag
|
|
34
|
+
env:
|
|
35
|
+
RELEASE_TAG: ${{ github.event.release.tag_name }}
|
|
26
36
|
run: |
|
|
27
|
-
|
|
28
|
-
|
|
37
|
+
# Remove 'v' prefix from tag if present (e.g., v1.2.3 -> 1.2.3)
|
|
38
|
+
EXPECTED_VERSION="${RELEASE_TAG#v}"
|
|
39
|
+
|
|
40
|
+
# Get version from package.json
|
|
41
|
+
PACKAGE_VERSION=$(node -p "require('./package.json').version")
|
|
42
|
+
|
|
43
|
+
echo "Release tag: $RELEASE_TAG"
|
|
44
|
+
echo "Expected version: $EXPECTED_VERSION"
|
|
45
|
+
echo "Package.json version: $PACKAGE_VERSION"
|
|
46
|
+
|
|
47
|
+
if [ "$PACKAGE_VERSION" != "$EXPECTED_VERSION" ]; then
|
|
48
|
+
echo "Error: package.json version ($PACKAGE_VERSION) does not match release tag ($RELEASE_TAG)"
|
|
49
|
+
exit 1
|
|
50
|
+
fi
|
|
51
|
+
|
|
52
|
+
echo "✓ Version validation passed"
|
|
53
|
+
|
|
29
54
|
- name: Update generated examples
|
|
30
55
|
run: npm run update-examples
|
|
31
|
-
- name:
|
|
56
|
+
- name: Verify examples are up to date
|
|
32
57
|
run: |
|
|
33
|
-
git
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
58
|
+
if [ "$(git diff --ignore-space-at-eol --text examples/ | wc -l)" -gt "0" ]; then
|
|
59
|
+
echo "Error: Generated examples are out of date. Please run 'npm run update-examples' and commit the changes before creating a release."
|
|
60
|
+
git diff --ignore-space-at-eol --text examples/
|
|
61
|
+
exit 1
|
|
62
|
+
fi
|
|
63
|
+
echo "✓ Examples are up to date"
|
|
38
64
|
- run: npm run build
|
|
39
65
|
- run: npm publish --access public
|
|
40
66
|
env:
|
|
41
|
-
NODE_AUTH_TOKEN: ${{
|
|
67
|
+
NODE_AUTH_TOKEN: ${{ env.NPM_TOKEN }}
|
|
@@ -3,14 +3,19 @@ name: Run Tests
|
|
|
3
3
|
on:
|
|
4
4
|
push:
|
|
5
5
|
branches:
|
|
6
|
-
-
|
|
6
|
+
- main
|
|
7
|
+
pull_request:
|
|
7
8
|
|
|
8
9
|
jobs:
|
|
9
10
|
test:
|
|
10
11
|
runs-on: ubuntu-latest
|
|
12
|
+
permissions:
|
|
13
|
+
contents: read
|
|
11
14
|
steps:
|
|
12
|
-
- uses: actions/checkout@
|
|
13
|
-
|
|
15
|
+
- uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6
|
|
16
|
+
with:
|
|
17
|
+
persist-credentials: false
|
|
18
|
+
- uses: actions/setup-node@395ad3262231945c25e8478fd5baf05154b1d79f # v6
|
|
14
19
|
with:
|
|
15
20
|
node-version: '21'
|
|
16
21
|
cache: 'npm'
|
|
@@ -20,9 +25,13 @@ jobs:
|
|
|
20
25
|
- run: npm test
|
|
21
26
|
e2e-test:
|
|
22
27
|
runs-on: ubuntu-latest
|
|
28
|
+
permissions:
|
|
29
|
+
contents: read
|
|
23
30
|
steps:
|
|
24
|
-
- uses: actions/checkout@
|
|
25
|
-
|
|
31
|
+
- uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6
|
|
32
|
+
with:
|
|
33
|
+
persist-credentials: false
|
|
34
|
+
- uses: actions/setup-node@395ad3262231945c25e8478fd5baf05154b1d79f # v6
|
|
26
35
|
with:
|
|
27
36
|
node-version: '21'
|
|
28
37
|
cache: 'npm'
|
|
@@ -35,22 +44,26 @@ jobs:
|
|
|
35
44
|
npm run dev -- ./tests/e2e/schema.json ./tests/e2e/tags/sdk.ts --disable-analytics --mode tags
|
|
36
45
|
npm run dev -- ./tests/e2e/schema.json ./tests/e2e/single-tag-filter/sdk.ts --disable-analytics --mode single --only-tags ItemsHeader
|
|
37
46
|
- name: Start Mockoon CLI
|
|
38
|
-
uses: mockoon/cli-action@v2
|
|
47
|
+
uses: mockoon/cli-action@971589e873f22f7c9604e4236d6dfae3164efa6c # v2 - https://github.com/mockoon/cli-action/commit/971589e873f22f7c9604e4236d6dfae3164efa6c
|
|
39
48
|
with:
|
|
40
49
|
# Mockoon local data file or URL
|
|
41
50
|
data-file: './tests/e2e/schema.json'
|
|
42
51
|
port: 3000
|
|
43
|
-
- uses: grafana/setup-k6-action@v1
|
|
44
|
-
- uses: grafana/run-k6-action@v1
|
|
52
|
+
- uses: grafana/setup-k6-action@ffe7d7290dfa715e48c2ccc924d068444c94bde2 # v1
|
|
53
|
+
- uses: grafana/run-k6-action@a15e2072ede004e8d46141e33d7f7dad8ad08d9d # v1
|
|
45
54
|
with:
|
|
46
55
|
path: './tests/e2e/*/k6Script.ts'
|
|
47
56
|
flags: '--compatibility-mode=experimental_enhanced'
|
|
48
57
|
inspect-flags: '--compatibility-mode=experimental_enhanced'
|
|
49
58
|
run-examples:
|
|
50
59
|
runs-on: ubuntu-latest
|
|
60
|
+
permissions:
|
|
61
|
+
contents: read
|
|
51
62
|
steps:
|
|
52
|
-
- uses: actions/checkout@
|
|
53
|
-
|
|
63
|
+
- uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6
|
|
64
|
+
with:
|
|
65
|
+
persist-credentials: false
|
|
66
|
+
- uses: actions/setup-node@395ad3262231945c25e8478fd5baf05154b1d79f # v6
|
|
54
67
|
with:
|
|
55
68
|
node-version: '21'
|
|
56
69
|
cache: 'npm'
|
package/.nvmrc
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
24
|
package/CODEOWNERS
ADDED
package/README.md
CHANGED
|
@@ -5,83 +5,80 @@
|
|
|
5
5
|
width="600"
|
|
6
6
|
style="pointer-events: none;" />
|
|
7
7
|
<br />
|
|
8
|
-
<hr/>
|
|
9
|
-
<p align="center">⚠️</p>
|
|
10
|
-
<b>This tool is currently in the experimental stage. Expect bugs, incomplete features, and breaking changes as development progresses. Use at your own risk, and please report any issues or feedback to help us improve.</b>
|
|
11
|
-
<hr/>
|
|
12
8
|
</div>
|
|
13
9
|
|
|
14
10
|
## Overview
|
|
15
11
|
|
|
16
|
-
The _openapi-to-k6_ repository is a tool designed to
|
|
17
|
-
It generates a TypeScript client from OpenAPI specification
|
|
18
|
-
easily call your endpoints and have auto
|
|
12
|
+
The _openapi-to-k6_ repository is a tool designed to simplify the process of writing k6 scripts.
|
|
13
|
+
It generates a TypeScript client from an OpenAPI specification that you can import in your k6 script to
|
|
14
|
+
easily call your endpoints and have auto-completion in your IDE.
|
|
19
15
|
|
|
20
|
-
This
|
|
16
|
+
This lets developers easily create performance tests for their APIs based on their existing
|
|
21
17
|
OpenAPI documentation.
|
|
22
18
|
|
|
23
19
|
Along with the client, it also generates a sample k6 script as an example of how to use the client.
|
|
24
20
|
|
|
25
21
|
The generated client exports a class with methods for each endpoint in the OpenAPI specification. You can create
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
To take a look at a few examples of how the generated client looks and sample script looks, check out the [examples](./examples) directory.
|
|
22
|
+
an instance of the class and use the methods to call the endpoints.
|
|
29
23
|
|
|
24
|
+
To see examples of the generated client and sample script, check out the [examples](./examples) directory.
|
|
30
25
|
|
|
31
26
|
## Getting started
|
|
32
27
|
|
|
33
|
-
1. Install the tool globally via
|
|
28
|
+
1. Install the tool globally via:
|
|
34
29
|
|
|
35
30
|
```shell
|
|
36
31
|
npm install -g @grafana/openapi-to-k6
|
|
37
32
|
```
|
|
38
33
|
|
|
39
|
-
2. To start using the tool either give path to your OpenAPI schema file or provide a URL to your
|
|
40
|
-
API schema and the output path where you want to generate the client files.
|
|
34
|
+
2. To start using the tool, either give the path to your OpenAPI schema file or provide a URL to your OpenAPI schema and the output path where you want to generate the client files:
|
|
41
35
|
|
|
42
36
|
```shell
|
|
43
37
|
openapi-to-k6 <path-to-openapi-schema | url-to-openapi-schema> <output path>
|
|
44
38
|
```
|
|
45
39
|
|
|
46
|
-
This will
|
|
40
|
+
This will generate a TypeScript client and a sample k6 script in the corresponding directory.
|
|
47
41
|
|
|
48
42
|
You can also supply the optional flag `--include-sample-script` to also generate a sample k6 script
|
|
49
43
|
along with the client.
|
|
50
44
|
|
|
51
|
-
💡 _Note_: The tool supports both JSON and YAML format for OpenAPI
|
|
45
|
+
💡 _Note_: The tool supports both JSON and YAML format for OpenAPI schemas.
|
|
52
46
|
|
|
53
47
|
### Options
|
|
54
48
|
|
|
55
|
-
|
|
49
|
+
The following are some of the configuration options supported by the tool:
|
|
56
50
|
|
|
57
|
-
1. `--mode` or `-m`: Specify the mode to use for generating the client.
|
|
58
|
-
1. `single`: This is the default mode used
|
|
51
|
+
1. `--mode` or `-m`: Specify the mode to use for generating the client. The following options are available:
|
|
52
|
+
1. `single`: This is the default mode used if nothing is specified. It generates the TypeScript client as a single file with all the types and implementation in a single file.
|
|
59
53
|
2. `split`: This mode splits the types and implementation into separate files.
|
|
60
|
-
3. `tags`: This modes splits your OpenAPI schema based on the tags and generates a separate client for each tag. If a route has no tag set, it will be available in `default.ts` file.
|
|
54
|
+
3. `tags`: This modes splits your OpenAPI schema based on the tags and generates a separate client for each tag. If a route has no tag set, it will be available in the `default.ts` file.
|
|
61
55
|
|
|
62
56
|
To check how the output looks for each mode, check out the [examples](./examples) directory.
|
|
57
|
+
|
|
63
58
|
2. `--only-tags`: Filter the generated client to only include routes with specific tags from your OpenAPI schema. Multiple tags can be specified to include routes matching any of those tags. Routes without tags will be excluded. This is useful for generating focused clients that only contain the endpoints you need.
|
|
64
|
-
|
|
65
|
-
|
|
59
|
+
|
|
60
|
+
For example: `openapi-to-k6 <path-to-openapi-schema> <output path> --only-tags ItemsHeader` will generate a client with only the routes that have the `ItemsHeader` tag. You can specify multiple tags by using multiple `--only-tags` flags or by separating them with spaces: `--only-tags tag1 --only-tags tag2`.
|
|
61
|
+
|
|
62
|
+
3. `--disable-analytics`: Disable anonymous usage analytics reporting. You can also set an environment variable `DISABLE_ANALYTICS=true` to disable analytics.
|
|
66
63
|
4. `--include-sample-script`: Generate a sample k6 script. The generated sample script uses the examples defined in the OpenAPI schema requests to make the script usable out of the box. If the examples are not defined, it will use Faker to generate random data.
|
|
67
64
|
5. `--verbose` or `-v` : Enable verbose logging to see more detailed logging output.
|
|
68
65
|
6. `--help` or `-h` : Show help message.
|
|
69
66
|
|
|
70
67
|
## Developing locally
|
|
71
68
|
|
|
72
|
-
1. Clone the repository
|
|
69
|
+
1. Clone the repository:
|
|
73
70
|
|
|
74
71
|
```shell
|
|
75
72
|
git clone https://github.com/grafana/openapi-to-k6
|
|
76
73
|
```
|
|
77
74
|
|
|
78
|
-
2. Install dependencies
|
|
75
|
+
2. Install dependencies:
|
|
79
76
|
|
|
80
77
|
```shell
|
|
81
78
|
npm install
|
|
82
79
|
```
|
|
83
80
|
|
|
84
|
-
3. Run the
|
|
81
|
+
3. Run the SDK generator from source:
|
|
85
82
|
|
|
86
83
|
```shell
|
|
87
84
|
npm run dev <path-to-openapi-schema> <output path>
|
|
@@ -89,51 +86,49 @@ npm run dev <path-to-openapi-schema> <output path>
|
|
|
89
86
|
|
|
90
87
|
This will generate the SDK files in the corresponding directory.
|
|
91
88
|
|
|
92
|
-
4. Import them in
|
|
89
|
+
4. Import them in your k6 script and run the script using the following command:
|
|
93
90
|
|
|
94
91
|
```shell
|
|
95
|
-
k6 run
|
|
92
|
+
k6 run <path-to-k6-script>.ts
|
|
96
93
|
```
|
|
97
94
|
|
|
98
|
-
Note: `--compatibility-mode` is needed to use a typescript file as K6 script. To know more about it, click [here](https://grafana.com/docs/k6/latest/using-k6/javascript-typescript-compatibility-mode/).
|
|
99
|
-
|
|
100
95
|
## Running E2E tests
|
|
101
96
|
|
|
102
|
-
We have some end-to-end tests to ensure the generated SDK works as expected. To run these tests
|
|
97
|
+
We have some end-to-end tests to ensure the generated SDK works as expected. To run these tests:
|
|
103
98
|
|
|
104
|
-
1. Navigate to the test directory
|
|
99
|
+
1. Navigate to the test directory:
|
|
105
100
|
|
|
106
101
|
```shell
|
|
107
102
|
cd tests/e2e/
|
|
108
103
|
```
|
|
109
104
|
|
|
110
105
|
2. Use Mockoon CLI to start the mock server which will create a mock server for the endpoints defined in the OpenAPI specification.
|
|
111
|
-
This will run the mock server in a
|
|
106
|
+
This will run the mock server in a Docker container in background:
|
|
112
107
|
|
|
113
108
|
```shell
|
|
114
109
|
docker run -v ./schema.json:/tmp/schema.json -p 3000:3000 mockoon/cli:latest -d /tmp/schema.json
|
|
115
110
|
```
|
|
116
111
|
|
|
117
|
-
3. Assuming you have already followed previous steps and have the environment set up, you can generate the SDK by using
|
|
112
|
+
3. Assuming you have already followed previous steps and have the environment set up, you can generate the SDK by using:
|
|
118
113
|
|
|
119
114
|
```shell
|
|
120
115
|
npm run dev -- ./schema.json ./sdk.ts
|
|
121
116
|
```
|
|
122
117
|
|
|
123
|
-
4. Run the
|
|
118
|
+
4. Run the k6 script:
|
|
124
119
|
|
|
125
120
|
```shell
|
|
126
|
-
k6 run
|
|
121
|
+
k6 run ./K6Script.ts
|
|
127
122
|
```
|
|
128
123
|
|
|
129
124
|
## Packaging
|
|
130
125
|
|
|
131
|
-
1. Run the command `npm run build` to package the project
|
|
126
|
+
1. Run the command `npm run build` to package the project for distribution.
|
|
132
127
|
2. Install the compiled package locally by using `npm install .` or `npm install -g .`.
|
|
133
|
-
3. Use the CLI `k6-sdkgen <path-to-openapi-schema> <output path
|
|
128
|
+
3. Use the CLI: `k6-sdkgen <path-to-openapi-schema> <output path>`.
|
|
134
129
|
|
|
135
130
|
## Releasing
|
|
136
131
|
|
|
137
|
-
To release a new version of the tool, create a new release on GitHub with the new version number as tag (
|
|
132
|
+
To release a new version of the tool, create a new release on GitHub with the new version number as a tag (for example, `0.1.0`) and the release notes. After the release is created, GitHub Actions will automatically package the tool and publish it to npm.
|
|
138
133
|
|
|
139
|
-
Special mention
|
|
134
|
+
Special mention to the the open-source library [Orval](https://orval.dev/) which is used for the generation of the TypeScript client.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
apiVersion: backstage.io/v1alpha1
|
|
2
|
+
kind: Component
|
|
3
|
+
metadata:
|
|
4
|
+
name: openapi-to-k6
|
|
5
|
+
title: openapi-to-k6
|
|
6
|
+
description: |
|
|
7
|
+
A CLI tool to convert an OpenAPI schema to a TypeScript client to use with k6.
|
|
8
|
+
annotations:
|
|
9
|
+
github.com/project-slug: grafana/openapi-to-k6
|
|
10
|
+
spec:
|
|
11
|
+
type: tool
|
|
12
|
+
owner: group:default/k6-cloud-provisioning
|
|
13
|
+
lifecycle: production
|
|
@@ -29,8 +29,10 @@ function _generateResponseTypeDefinition(response) {
|
|
|
29
29
|
return `{
|
|
30
30
|
response: Response
|
|
31
31
|
data: ${responseDataType}
|
|
32
|
+
operationId: string
|
|
32
33
|
}`;
|
|
33
34
|
}
|
|
35
|
+
const INTERNAL_URL_TOKEN = 'k6url';
|
|
34
36
|
function _getRequestParametersMergerFunctionImplementation() {
|
|
35
37
|
return `/**
|
|
36
38
|
* Merges the provided request parameters with default parameters for the client.
|
|
@@ -108,7 +110,7 @@ const _getK6RequestOptions = (verbOptions) => {
|
|
|
108
110
|
// Sample output
|
|
109
111
|
// 'GET', 'http://test.com/route', <body>, <options>
|
|
110
112
|
return `"${verb.toUpperCase()}",
|
|
111
|
-
|
|
113
|
+
${INTERNAL_URL_TOKEN}.toString(),
|
|
112
114
|
${fetchBodyOption},
|
|
113
115
|
${requestParametersValue}`;
|
|
114
116
|
};
|
|
@@ -157,7 +159,7 @@ const getK6Dependencies = () => [
|
|
|
157
159
|
},
|
|
158
160
|
];
|
|
159
161
|
const generateK6Implementation = (verbOptions, { route }, analyticsData) => {
|
|
160
|
-
const { queryParams, operationName, response, body, props, verb, formData, formUrlEncoded, } = verbOptions;
|
|
162
|
+
const { queryParams, operationName, operationId, response, body, props, verb, formData, formUrlEncoded, } = verbOptions;
|
|
161
163
|
if (analyticsData) {
|
|
162
164
|
analyticsData.generatedRequestsCount[verb] += 1;
|
|
163
165
|
}
|
|
@@ -172,7 +174,7 @@ const generateK6Implementation = (verbOptions, { route }, analyticsData) => {
|
|
|
172
174
|
if (queryParams) {
|
|
173
175
|
url += '+`?${new URLSearchParams(params).toString()}`';
|
|
174
176
|
}
|
|
175
|
-
const urlGeneration = `const
|
|
177
|
+
const urlGeneration = `const ${INTERNAL_URL_TOKEN} = new URL(${url});`;
|
|
176
178
|
const options = _getK6RequestOptions(verbOptions);
|
|
177
179
|
return `${operationName}(\n ${(0, core_1.toObjectString)(props, 'implementation')} requestParameters?: Params): ${_generateResponseTypeDefinition(response)} {\n${bodyForm}
|
|
178
180
|
${urlGeneration}
|
|
@@ -187,7 +189,8 @@ const generateK6Implementation = (verbOptions, { route }, analyticsData) => {
|
|
|
187
189
|
}
|
|
188
190
|
return {
|
|
189
191
|
response,
|
|
190
|
-
data
|
|
192
|
+
data,
|
|
193
|
+
operationId: '${(0, core_1.jsStringEscape)(operationId)}'
|
|
191
194
|
}
|
|
192
195
|
}
|
|
193
196
|
`;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Automatically generated by @grafana/openapi-to-k6: 0.
|
|
2
|
+
* Automatically generated by @grafana/openapi-to-k6: 0.4.1
|
|
3
3
|
* Do not edit manually.
|
|
4
4
|
* Simple API
|
|
5
5
|
* Service version: 1.0.0
|
|
@@ -34,15 +34,16 @@ export class SimpleAPIClient {
|
|
|
34
34
|
getExample(requestParameters?: Params): {
|
|
35
35
|
response: Response
|
|
36
36
|
data: GetExample200
|
|
37
|
+
operationId: string
|
|
37
38
|
} {
|
|
38
|
-
const
|
|
39
|
+
const k6url = new URL(this.cleanBaseUrl + `/example`)
|
|
39
40
|
const mergedRequestParameters = this._mergeRequestParameters(
|
|
40
41
|
requestParameters || {},
|
|
41
42
|
this.commonRequestParameters
|
|
42
43
|
)
|
|
43
44
|
const response = http.request(
|
|
44
45
|
'GET',
|
|
45
|
-
|
|
46
|
+
k6url.toString(),
|
|
46
47
|
undefined,
|
|
47
48
|
mergedRequestParameters
|
|
48
49
|
)
|
|
@@ -56,6 +57,7 @@ export class SimpleAPIClient {
|
|
|
56
57
|
return {
|
|
57
58
|
response,
|
|
58
59
|
data,
|
|
60
|
+
operationId: 'GetExample',
|
|
59
61
|
}
|
|
60
62
|
}
|
|
61
63
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Automatically generated by @grafana/openapi-to-k6: 0.
|
|
2
|
+
* Automatically generated by @grafana/openapi-to-k6: 0.4.1
|
|
3
3
|
* Do not edit manually.
|
|
4
4
|
* Simple API
|
|
5
5
|
* Service version: 1.0.0
|
|
@@ -31,15 +31,16 @@ export class SimpleAPIClient {
|
|
|
31
31
|
getExample(requestParameters?: Params): {
|
|
32
32
|
response: Response
|
|
33
33
|
data: GetExample200
|
|
34
|
+
operationId: string
|
|
34
35
|
} {
|
|
35
|
-
const
|
|
36
|
+
const k6url = new URL(this.cleanBaseUrl + `/example`)
|
|
36
37
|
const mergedRequestParameters = this._mergeRequestParameters(
|
|
37
38
|
requestParameters || {},
|
|
38
39
|
this.commonRequestParameters
|
|
39
40
|
)
|
|
40
41
|
const response = http.request(
|
|
41
42
|
'GET',
|
|
42
|
-
|
|
43
|
+
k6url.toString(),
|
|
43
44
|
undefined,
|
|
44
45
|
mergedRequestParameters
|
|
45
46
|
)
|
|
@@ -53,6 +54,7 @@ export class SimpleAPIClient {
|
|
|
53
54
|
return {
|
|
54
55
|
response,
|
|
55
56
|
data,
|
|
57
|
+
operationId: 'GetExample',
|
|
56
58
|
}
|
|
57
59
|
}
|
|
58
60
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Automatically generated by @grafana/openapi-to-k6: 0.
|
|
2
|
+
* Automatically generated by @grafana/openapi-to-k6: 0.4.1
|
|
3
3
|
* Do not edit manually.
|
|
4
4
|
* Simple API
|
|
5
5
|
* Service version: 1.0.0
|
|
@@ -31,15 +31,16 @@ export class DefaultClient {
|
|
|
31
31
|
getExample(requestParameters?: Params): {
|
|
32
32
|
response: Response
|
|
33
33
|
data: GetExample200
|
|
34
|
+
operationId: string
|
|
34
35
|
} {
|
|
35
|
-
const
|
|
36
|
+
const k6url = new URL(this.cleanBaseUrl + `/example`)
|
|
36
37
|
const mergedRequestParameters = this._mergeRequestParameters(
|
|
37
38
|
requestParameters || {},
|
|
38
39
|
this.commonRequestParameters
|
|
39
40
|
)
|
|
40
41
|
const response = http.request(
|
|
41
42
|
'GET',
|
|
42
|
-
|
|
43
|
+
k6url.toString(),
|
|
43
44
|
undefined,
|
|
44
45
|
mergedRequestParameters
|
|
45
46
|
)
|
|
@@ -53,6 +54,7 @@ export class DefaultClient {
|
|
|
53
54
|
return {
|
|
54
55
|
response,
|
|
55
56
|
data,
|
|
57
|
+
operationId: 'GetExample',
|
|
56
58
|
}
|
|
57
59
|
}
|
|
58
60
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Automatically generated by @grafana/openapi-to-k6: 0.
|
|
2
|
+
* Automatically generated by @grafana/openapi-to-k6: 0.4.1
|
|
3
3
|
* Do not edit manually.
|
|
4
4
|
* Form Data API
|
|
5
5
|
* Service version: 1.0.0
|
|
@@ -8,16 +8,6 @@ import { FormData } from 'https://jslib.k6.io/formdata/0.0.2/index.js'
|
|
|
8
8
|
import { URL } from 'https://jslib.k6.io/url/1.0.0/index.js'
|
|
9
9
|
import http from 'k6/http'
|
|
10
10
|
import type { Params, Response } from 'k6/http'
|
|
11
|
-
export type PostUpload400 = {
|
|
12
|
-
success?: boolean
|
|
13
|
-
error?: string
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export type PostUpload200 = {
|
|
17
|
-
success?: boolean
|
|
18
|
-
message?: string
|
|
19
|
-
}
|
|
20
|
-
|
|
21
11
|
export type PostUploadBody = {
|
|
22
12
|
/** File to upload */
|
|
23
13
|
file: Blob
|
|
@@ -27,6 +17,16 @@ export type PostUploadBody = {
|
|
|
27
17
|
userId: string
|
|
28
18
|
}
|
|
29
19
|
|
|
20
|
+
export type PostUpload200 = {
|
|
21
|
+
success?: boolean
|
|
22
|
+
message?: string
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export type PostUpload400 = {
|
|
26
|
+
success?: boolean
|
|
27
|
+
error?: string
|
|
28
|
+
}
|
|
29
|
+
|
|
30
30
|
/**
|
|
31
31
|
* This is the base client to use for interacting with the API.
|
|
32
32
|
*/
|
|
@@ -53,6 +53,7 @@ export class FormDataAPIClient {
|
|
|
53
53
|
): {
|
|
54
54
|
response: Response
|
|
55
55
|
data: PostUpload200
|
|
56
|
+
operationId: string
|
|
56
57
|
} {
|
|
57
58
|
const formData = new FormData()
|
|
58
59
|
formData.append('file', postUploadBody.file)
|
|
@@ -61,12 +62,12 @@ export class FormDataAPIClient {
|
|
|
61
62
|
}
|
|
62
63
|
formData.append('userId', postUploadBody.userId)
|
|
63
64
|
|
|
64
|
-
const
|
|
65
|
+
const k6url = new URL(this.cleanBaseUrl + `/upload`)
|
|
65
66
|
const mergedRequestParameters = this._mergeRequestParameters(
|
|
66
67
|
requestParameters || {},
|
|
67
68
|
this.commonRequestParameters
|
|
68
69
|
)
|
|
69
|
-
const response = http.request('POST',
|
|
70
|
+
const response = http.request('POST', k6url.toString(), formData.body(), {
|
|
70
71
|
...mergedRequestParameters,
|
|
71
72
|
headers: {
|
|
72
73
|
...mergedRequestParameters?.headers,
|
|
@@ -83,6 +84,7 @@ export class FormDataAPIClient {
|
|
|
83
84
|
return {
|
|
84
85
|
response,
|
|
85
86
|
data,
|
|
87
|
+
operationId: 'PostUpload',
|
|
86
88
|
}
|
|
87
89
|
}
|
|
88
90
|
|
|
@@ -1,19 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Automatically generated by @grafana/openapi-to-k6: 0.
|
|
2
|
+
* Automatically generated by @grafana/openapi-to-k6: 0.4.1
|
|
3
3
|
* Do not edit manually.
|
|
4
4
|
* Form Data API
|
|
5
5
|
* Service version: 1.0.0
|
|
6
6
|
*/
|
|
7
|
-
export type PostUpload400 = {
|
|
8
|
-
success?: boolean
|
|
9
|
-
error?: string
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export type PostUpload200 = {
|
|
13
|
-
success?: boolean
|
|
14
|
-
message?: string
|
|
15
|
-
}
|
|
16
|
-
|
|
17
7
|
export type PostUploadBody = {
|
|
18
8
|
/** File to upload */
|
|
19
9
|
file: Blob
|
|
@@ -22,3 +12,13 @@ export type PostUploadBody = {
|
|
|
22
12
|
/** User ID associated with the upload */
|
|
23
13
|
userId: string
|
|
24
14
|
}
|
|
15
|
+
|
|
16
|
+
export type PostUpload200 = {
|
|
17
|
+
success?: boolean
|
|
18
|
+
message?: string
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export type PostUpload400 = {
|
|
22
|
+
success?: boolean
|
|
23
|
+
error?: string
|
|
24
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Automatically generated by @grafana/openapi-to-k6: 0.
|
|
2
|
+
* Automatically generated by @grafana/openapi-to-k6: 0.4.1
|
|
3
3
|
* Do not edit manually.
|
|
4
4
|
* Form Data API
|
|
5
5
|
* Service version: 1.0.0
|
|
@@ -36,6 +36,7 @@ export class FormDataAPIClient {
|
|
|
36
36
|
): {
|
|
37
37
|
response: Response
|
|
38
38
|
data: PostUpload200
|
|
39
|
+
operationId: string
|
|
39
40
|
} {
|
|
40
41
|
const formData = new FormData()
|
|
41
42
|
formData.append('file', postUploadBody.file)
|
|
@@ -44,12 +45,12 @@ export class FormDataAPIClient {
|
|
|
44
45
|
}
|
|
45
46
|
formData.append('userId', postUploadBody.userId)
|
|
46
47
|
|
|
47
|
-
const
|
|
48
|
+
const k6url = new URL(this.cleanBaseUrl + `/upload`)
|
|
48
49
|
const mergedRequestParameters = this._mergeRequestParameters(
|
|
49
50
|
requestParameters || {},
|
|
50
51
|
this.commonRequestParameters
|
|
51
52
|
)
|
|
52
|
-
const response = http.request('POST',
|
|
53
|
+
const response = http.request('POST', k6url.toString(), formData.body(), {
|
|
53
54
|
...mergedRequestParameters,
|
|
54
55
|
headers: {
|
|
55
56
|
...mergedRequestParameters?.headers,
|
|
@@ -66,6 +67,7 @@ export class FormDataAPIClient {
|
|
|
66
67
|
return {
|
|
67
68
|
response,
|
|
68
69
|
data,
|
|
70
|
+
operationId: 'PostUpload',
|
|
69
71
|
}
|
|
70
72
|
}
|
|
71
73
|
|