Skip to main content

openapi-client

@ensono-stacks/rest-client:openapi-client

Generates boilerplate configuration of types, client, stubs and validation using from your openapi schema using Orval.

Utilising Orval, this plugin generates various files based off your openapi schema to enable quick acceleration from definition to implementation.

Usage

nx g @ensono-stacks/rest-client:openapi-client

Command line arguments

The following command line arguments are available:

OptionDescriptionTypeAccepted ValuesDefaultRequired
--nameLibrary namestringtrue
--schemaThe relative path to your openapi schemastringtrue
--zodValidation for your openapi schemabooleanfalsetrue
--tagsAdd tags to the library (used for linting)string
--directorySubdirectory inside libs/ where the generated library is placedstring

Generator Output

The openapi-client will create a new library within your libs folder for the various files generated:

Generated files

├── openapi-client
│ ├── src
│ │ ├── model
| │ │ ├── ...
│ │ ├── <libraryName>.msw.ts
│ │ ├── <libraryName>.ts
│ │ ├── <libraryName>.zod.ts
│ │ ├── index.ts
│ ├── .eslintrc.json
│ ├── jest.config.ts
│ ├── orval.config.js
│ ├── orval.zod.config.js
│ ├── openapi-schema.(json/yaml)
│ ├── project.json
│ ├── README.md
│ ├── tsconfig.json
│ ├── tsconfig.lib.json
└── └── tsconfig.spec.json

Key things to highlight about the generated files are as follows:

  • The generator first creates the orval config files orval.config.js orval.zod.config.js which are used to set the options for orval to be executed against. These options determine what files to generate and how.
  • Once the config files are generated, we execute the Orval generator. The 3 <libraryName>... files are created along wih the model folder with its contents. This generation consists of the end result following the openapi definition being converted into code implementation; types, client, stubs and validation.

The relevant dependencies (orval, msw, @faker-js/faker, zod) are also installed in order for the generation to take place and to resolve the relevant syntax/prettier errors.