Skip to content

seriouslag/openapi-ts-nx-plugin

Repository files navigation

@seriouslag/openapi-ts-nx-plugin

NPM Version

This repo is to develop and test the @seriouslag/nx-openapi-ts-plugin

The package is a NX plugin that assits with the automation of generating NX projects to a workspace based off an OpenAPI spec file. Under the hood it uses @hey-api/openapi-ts to generate client code from the OpenAPI spec file.

This plugin helps generate packages to be linked correctly in the NX environment and to provide executors to update the client code when an change in the spec file is detected.

install dependencies

pnpm install

To run the plugin

# run the generator to generate a package
npx nx run @seriouslag/nx-openapi-ts-plugin:build && nx g @seriouslag/nx-openapi-ts-plugin:openapi-client pokemon-api --directory ./packages --scope @test-api --client @hey-api/client-fetch --spec https://raw.githubusercontent.com/seriouslag/pokemon-api-spec/refs/heads/main/spec.yaml --plugins @tanstack/react-query --private false --verbose

# Install will happen automatically after new package is generated

# runs the executor to update the generated package
npx nx run @test-api/pokemon-api:updateApi
# There will be no update since the spec is the same
# This result will be cached by nx, for a new result the API spec file must change or the `--skip-nx-cache --force` flags must be used

# to test an update: Ether change the source spec file or make a change to one of the routes paths in the cached spec file located at ./packages/pokemon-api/src/spec.yaml
# then run the executor again and you will see it recreate the client code
npx nx run @test-api/pokemon-api:updateApi --skip-nx-cache --force

After running the generator you will see a new package in the packages folder named pokemon-api.

Run tasks

To run the dev server for your app, use:

npx nx serve test-plugin

To create a production bundle:

npx nx build test-plugin

To see all available targets to run for a project, run:

npx nx show project test-plugin

These targets are either inferred automatically or defined in the project.json or package.json files.

More about running tasks in the docs »

Add new projects

While you could add new projects to your workspace manually, you might want to leverage Nx plugins and their code generation feature.

Use the plugin's generator to create new projects.

To generate a new application, use:

npx nx g @nx/react:app demo

To generate a new library, use:

npx nx g @nx/react:lib mylib

You can use npx nx list to get a list of installed plugins. Then, run npx nx list <plugin-name> to learn about more specific capabilities of a particular plugin. Alternatively, install Nx Console to browse plugins and generators in your IDE.

Learn more about Nx plugins » | Browse the plugin registry »

Set up CI!

Step 1

To connect to Nx Cloud, run the following command:

npx nx connect

Connecting to Nx Cloud ensures a fast and scalable CI pipeline. It includes features such as:

Step 2

Use the following command to configure a CI workflow for your workspace:

npx nx g ci-workflow

Learn more about Nx on CI

Install Nx Console

Nx Console is an editor extension that enriches your developer experience. It lets you run tasks, generate code, and improves code autocompletion in your IDE. It is available for VSCode and IntelliJ.

Install Nx Console »

Useful links

Learn more:

And join the Nx community:

About

Test repo showing a plugin for NX using openapi-ts

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors