Skip to content

feat(nx-plugin): add NX Plugin with OpenAPI client generator and executor #1947

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
530c086
feat(nx-plugin): add nx plugin to generate and update client code
seriouslag Apr 14, 2025
c1d59ec
feat(config): add exports field and support for CommonJS format in Vi…
seriouslag Apr 14, 2025
ec4dd29
fix(turbo.json): enable caching for build task
seriouslag Apr 14, 2025
c5219db
fix(nx-plugin): update build output
seriouslag Apr 14, 2025
6ec8fca
refactor(nx-plugin): update schema prompts and descriptions for clari…
seriouslag Apr 14, 2025
e79a8fe
chore(nx-plugin): remove ts-node dependency and update JSON file copy…
seriouslag Apr 14, 2025
0449a29
feat(vitest): add setup files configuration to Vitest base config
seriouslag Apr 14, 2025
8372977
feat(turbo.json): add inputs configuration for improved build process
seriouslag Apr 14, 2025
c3908ba
feat(client): update example to use actual code from the spec to avoi…
seriouslag Apr 14, 2025
57dda7f
feat(config): update setup file reference in Vitest base config to us…
seriouslag Apr 14, 2025
ad16a4e
feat(turbo.json): add build task configuration for Nuxt project
seriouslag Apr 14, 2025
4cc4da0
fix(nx-plugin): fix plugin build output and enhance caching
seriouslag Apr 17, 2025
43baacf
refactor(nx-plugin): enhance plugin handling and directory management
seriouslag Apr 25, 2025
c8c2236
feat(nx-plugin): enhance OpenAPI spec comparison functionality
seriouslag Apr 27, 2025
2526549
chore(dependencies): downgrade find-my-way to version 9.2.0 and add o…
seriouslag Apr 27, 2025
80ba81d
feat(nx-plugin): add base tsconfig options for improved configuration…
seriouslag Apr 28, 2025
013257e
feat(nx-plugin): enhance OpenAPI and Swagger spec comparison utilities
seriouslag Apr 29, 2025
733ff69
feat(nx-plugin): update to support NX 21 continuous pipelines
seriouslag May 14, 2025
94a79a0
major: add node 24 in CI, remove node 18 as a supported target
seriouslag May 14, 2025
e3e4b7d
refactor(nx-plugin): improve file formatting utilities
seriouslag May 15, 2025
52a1e75
refactor(nx-plugin): handle configs from builtin client packages
seriouslag Jul 24, 2025
92f6777
patch(nx-plugin): removed generated tests
seriouslag Jul 24, 2025
4810908
test(nx-plugin): add experimental parser validation to configuration …
seriouslag Jul 24, 2025
a2fb1da
test(nx-plugin): add parser configuration for tests
seriouslag Jul 24, 2025
1314d5d
fix(operation): ensure output case handling is robust by checking for…
seriouslag Jul 24, 2025
a5eb2fa
fix(openapi-client): update temp folder handling to use absolute path…
seriouslag Jul 24, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
strategy:
matrix:
os: [macos-latest, ubuntu-latest, windows-latest]
node-version: ['18.20.5', '20.11.1', '22.11.0']
node-version: ['20.19.0', '22.12.0']
steps:
- uses: actions/[email protected]
with:
Expand All @@ -38,7 +38,7 @@ jobs:
run: pnpm build --filter="@hey-api/**"

- name: Build examples
if: matrix.node-version == '22.11.0' && matrix.os == 'ubuntu-latest'
if: matrix.node-version == '24.0.1' && matrix.os == 'ubuntu-latest'
run: pnpm build --filter="@examples/**"

- name: Run linter
Expand All @@ -54,7 +54,7 @@ jobs:
run: pnpm test:e2e

- name: Publish preview packages
if: github.event_name == 'pull_request' && matrix.node-version == '22.11.0' && matrix.os == 'ubuntu-latest'
if: github.event_name == 'pull_request' && matrix.node-version == '24.0.1' && matrix.os == 'ubuntu-latest'
run: ./scripts/publish-preview-packages.sh
env:
TURBO_SCM_BASE: ${{ github.event_name == 'pull_request' && github.event.pull_request.base.sha || github.event.before }}
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
22.11.0
22.12.0
3 changes: 3 additions & 0 deletions examples/openapi-ts-fastify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@
"typescript": "5.8.3",
"vite": "6.2.7",
"vitest": "3.1.1"
},
"overrides": {
"find-my-way": "9.2.0"
}
}
11 changes: 11 additions & 0 deletions examples/openapi-ts-nuxt/turbo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this file to cache builds

"$schema": "../../node_modules/turbo/schema.json",
"extends": ["//"],
"tasks": {
"build": {
"cache": true,
"dependsOn": ["^build"],
"outputs": [".nuxt/**"]
}
}
}
2 changes: 0 additions & 2 deletions examples/openapi-ts-sample/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ function App() {
const onClick = async () => {
const response = await getPetById({
path: {
// @ts-expect-error
foo: 3,
petId: 3,
},
});
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"vitepress": "turbo run $1 --filter=\"@docs/openapi-ts\""
},
"engines": {
"node": "^18.18.0 || ^20.9.0 || >=22.10.0"
"node": "^20.19.0 || >=22.12.0"
},
"devDependencies": {
"@arethetypeswrong/cli": "0.17.4",
Expand All @@ -57,6 +57,7 @@
"eslint-plugin-sort-keys-fix": "1.1.2",
"eslint-plugin-typescript-sort-keys": "3.3.0",
"eslint-plugin-vue": "9.32.0",
"find-my-way": "9.2.0",
"globals": "15.14.0",
"husky": "9.1.7",
"lint-staged": "15.3.0",
Expand All @@ -67,6 +68,7 @@
"turbo": "2.5.5",
"typescript": "5.8.3",
"typescript-eslint": "8.29.1",
"vite-tsconfig-paths": "5.1.4",
"vitest": "3.1.1"
},
"packageManager": "[email protected]+sha512.1acb565e6193efbebda772702950469150cf12bcc764262e7587e71d19dc98a423dff9536e57ea44c49bdf790ff694e83c27be5faa23d67e0c033b583be4bfcf"
Expand Down
13 changes: 13 additions & 0 deletions packages/config-vite-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@
"scripts": {
"build": "tsup"
},
"exports": {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was facing issues with our new configs, adding the exports and adding cjs fixed the issues,

".": {
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"require": {
"types": "./dist/index.d.cts",
"default": "./dist/index.cjs"
}
},
"./setup": "./src/setup.ts"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exporting the setup file as a direct source file for other vitests to use as a setup file.

},
"dependencies": {
"vite": "^6.2.7",
"vitest": "^3.1.1"
Expand Down
5 changes: 5 additions & 0 deletions packages/config-vite-base/src/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// FAIL LOUDLY on unhandled promise rejections / errors
Copy link
Contributor Author

@seriouslag seriouslag Apr 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding this file because vitest fails if an error is uncaught, but no trace is given to the logs. This change adds the trace. This will help future debugging.

process.on('unhandledRejection', (reason) => {
console.log(`******** FAILED TO HANDLE PROMISE REJECTION ********`);
throw reason;
});
7 changes: 4 additions & 3 deletions packages/config-vite-base/src/vitest.base.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@ export function createVitestConfig(
provider: 'v8',
},
exclude: [...configDefaults.exclude],
pool: platform() === 'win32' ? 'threads' : 'forks',
pool: 'forks',
poolOptions: {
forks: {
singleFork: false,
singleFork: true,
},
threads: {
singleThread: false,
singleThread: true,
},
},
root,
setupFiles: ['@config/vite-base/setup'],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding our new vitest setup file with the new export path

testTimeout: platform() === 'win32' ? 10000 : 5000,
},
});
Expand Down
2 changes: 1 addition & 1 deletion packages/config-vite-base/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ export default defineConfig({
clean: true,
dts: true,
entry: ['src/index.ts'],
format: ['esm'],
format: ['esm', 'cjs'],
});
21 changes: 21 additions & 0 deletions packages/nx-plugin/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) Hey API

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
69 changes: 69 additions & 0 deletions packages/nx-plugin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# @hey-api/nx-plugin

This plugin provides a generator and executor for generating and updating OpenAPI clients using the `@hey-api/openapi-ts` library. This can be tied in to automation and CI workflows to ensure your API clients are always up to date.

## Installation

```bash
npm install -D @hey-api/plugin-nx
```

## Usage

### Generators

#### openapi-client

[Docs](src/generators/openapi-client/README.md)

This plugin provides a generator for generating OpenAPI clients using the `@hey-api/openapi-ts` library.

Run in interactive mode `nx g @hey-api/nx-plugin:openapi-client`

##### Options

- `name`: The name of the project. [ string ] (required)
- `scope`: The scope of the project. [ string ] (required)
- `spec`: The path to the OpenAPI spec file. [ URI or string ] (required)
- `directory`: The directory to create the project in. [ string ] (optional) (default: `libs`)
- `client`: The type of client to generate. [ string ] (optional) (default: `@hey-api/client-fetch`)
To specify a specific version of the client you can use `@hey-api/[email protected]`.
- `tags`: The tags to add to the project. [ string[] ] (optional) (default: `api,openapi`)
The defaults tags will not be added to the project if you specify this option.
- `plugins`: Additional plugins to provide to the client api. [ string[] ] (optional)
- `test`: The type of tests to setup. [ 'none' | 'vitest' ] (optional) (default: `none`)
- `baseTsConfigName`: The name of the base tsconfig file that contains the compiler paths used to resolve the imports. Use this if the base tsconfig file is in the workspace root. If provided with a baseTsConfigPath then the baseTsConfigName will be added to the path. Do not use this if the baseTsConfigPath is a file. [ string ] (optional)
- `baseTsConfigPath`: The path to the base tsconfig file that contains the compiler paths used to resolve the imports. Use this if the base tsconfig file is not in the workspace root. This can be a file or a directory. If it is a directory and the baseTsConfigName is provided then the baseTsConfigName will be added to the path. If it is a file and the baseTsConfigName is provided then there will be an error. [ string ] (optional)

##### Example

```bash
nx g @hey-api/nx-plugin:openapi-client --name=my-api --client=@hey-api/client-fetch --scope=@my-app --directory=libs --spec=./spec.yaml --tags=api,openapi
```

### Executors

#### update-api

This executor updates the OpenAPI spec file and generates a new client.
The options for the executor will be populated from the generator.

No need to add them yourself, to modify the options manually edit the `project.json` of the generated project.

Run `nx run @my-org/my-generated-package:updateApi`

##### Options

- `spec`: The path to the OpenAPI spec file. [ URI or string ] (required)
- `name`: The name of the project. [ string ] (required)
- `scope`: The scope of the project. [ string ] (required)
- `client`: The type of client to generate. [ string ] (optional) (default: `@hey-api/client-fetch`)
- `directory`: The directory to create the project in. [ string ] (optional) (default: `libs`)
- `plugins`: Additional plugins to provide to the client api. [ string[] ] (optional) (default:[])

###### Spec File Notes

If the spec file is a relative path and is located in the workspace then the containing project will be listed as an implicit dependency.
The assumption is made that that project will generate the API spec file on build.

If the spec file is a URL then we fetch the spec during cache checks to determine if we should rebuild the client code.
9 changes: 9 additions & 0 deletions packages/nx-plugin/executors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"executors": {
"update-api": {
"implementation": "./dist/updateApi.cjs",
"schema": "./dist/executors/update-api/updateApi.schema.json",
"description": "Updates the OpenAPI spec file and generates new client code if needed."
}
}
}
11 changes: 11 additions & 0 deletions packages/nx-plugin/generators.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "openapi-generator",
"version": "0.0.1",
"generators": {
"openapi-client": {
"factory": "./dist/openapiClient.cjs",
"schema": "./dist/generators/openapi-client/openapiClient.schema.json",
"description": "Generate an OpenAPI client library from an OpenAPI spec file."
}
}
}
75 changes: 75 additions & 0 deletions packages/nx-plugin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{
"name": "@hey-api/nx-plugin",
"version": "0.0.1",
"description": "🚀 Nx plugin for `@hey-api/openapi-ts` codegen.",
"homepage": "https://heyapi.dev/",
"repository": {
"type": "git",
"url": "git+https://github.com/hey-api/openapi-ts.git",
"directory": "packages/nx-plugin"
},
"bugs": {
"url": "https://github.com/hey-api/openapi-ts/issues"
},
"license": "MIT",
"author": {
"email": "[email protected]",
"name": "Hey API",
"url": "https://heyapi.dev"
},
"funding": "https://github.com/sponsors/hey-api",
"keywords": [
"codegen",
"openapi",
"plugin",
"nx",
"swagger"
],
"type": "commonjs",
"main": "./dist/index.cjs",
"module": "./dist/index.cjs",
"types": "./dist/index.d.ts",
"scripts": {
"build": "tsup",
"test:watch": "vitest watch --config vitest.config.ts",
"test": "vitest run --config vitest.config.ts",
"typecheck": "tsc --noEmit",
"prepublishOnly": "pnpm build"
},
"exports": {
".": {
"require": {
"types": "./dist/index.d.cts",
"default": "./dist/index.cjs"
}
},
"./package.json": "./package.json"
},
"executors": "./executors.json",
"generators": "./generators.json",
"dependencies": {
"@hey-api/json-schema-ref-parser": "^1.0.6",
"@hey-api/openapi-ts": "workspace:*",
"@nx/devkit": "^21.3.5",
"api-smart-diff": "^1.0.6",
"latest-version": "^9.0.0",
"nx": "^21.3.5",
"prettier": "^3.6.2",
"swagger2openapi": "^7.0.8",
"tslib": "^2.8.1",
"xcurl": "^2.1.2"
},
"devDependencies": {
"@config/vite-base": "workspace:*",
"@types/swagger2openapi": "^7.0.4",
"typescript": "^5.8.3",
"vitest": "^3.2.4"
},
"files": [
"executors.json",
"generators.json",
"package.json",
"dist",
"LICENSE.md"
]
}
57 changes: 57 additions & 0 deletions packages/nx-plugin/scripts/copy-json-files.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import {
copyFileSync,
existsSync,
mkdirSync,
readdirSync,
statSync,
} from 'node:fs';
import { dirname, extname, join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

/**
* Recursively creates a directory if it does not exist.
* @param {string} dirPath
*/
function ensureDirectoryExists(dirPath) {
if (!existsSync(dirPath)) {
mkdirSync(dirPath, { recursive: true });
}
}

/**
* Recursively copies JSON files from a source directory to a target directory.
* @param {string} sourceDir
* @param {string} targetDir
*/
function copyJsonFiles(sourceDir, targetDir) {
// Read all files and directories in the source directory
const items = readdirSync(sourceDir);

for (const item of items) {
const sourcePath = join(sourceDir, item);
const targetPath = join(targetDir, item);

const stats = statSync(sourcePath);

if (stats.isDirectory()) {
// Recursively copy JSON files from subdirectories
copyJsonFiles(sourcePath, targetPath);
} else if (stats.isFile() && extname(item).toLowerCase() === '.json') {
// Copy JSON files
ensureDirectoryExists(dirname(targetPath));
copyFileSync(sourcePath, targetPath);
console.log(`Copied: ${sourcePath} -> ${targetPath}`);
}
}
}

// Copy JSON files from src to dist
const sourceDir = resolve(__dirname, '../src');
const targetDir = resolve(__dirname, '../dist');

console.log('Copying JSON files from src to dist...');
copyJsonFiles(sourceDir, targetDir);
console.log('JSON files copying completed.');
Loading
Loading