Skip to main content

Types and Interfaces

Install

npm install @basketry/typescript

Basic Usage

basketry.config.json
{
"source": "petstore.json",
"parser": "@basketry/openapi-3",
"generators": ["@basketry/typescript"],
"output": "src"
}

File Structure

This generator will create a types.ts file that contains all the types and interfaces for the API. By default, the file will be nested within a directory named after the API major version. This behavior can be disabled by using the includeVersion option described below.

my-project/
├── node_modules/
├── src/
│ ├── v1/ <-- generated
│ │ └── types.ts <-- generated
│ ├── index.ts
├── .gitignore
├── basketry.config.json
├── package.json
├── petstore.json
└── README.md

Options

The @basketry/typescript generator accepts the following options to customize the generated code:

eslintDisable

  • Type: string[] - An array of ESLint rules to disable in the generated code.

Use this option if a generator produces code that violates a rule you have configured in your ESLint configuration.

basketry.config.json
{
"source": "petstore.json",
"parser": "@basketry/openapi-3",
"generators": ["@basketry/typescript"],
"output": "src",
"options": {
"typescript": {
"eslintDisable": [
"@typescript-eslint/no-explicit-any",
"@typescript-eslint/no-unused-vars"
]
}
}
}

This option as configured will emit the following code at the top of the generated file:

src/v1/types.ts
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-unused-vars */

includeVersion

  • Type: boolean - Determines whether or not to nest the generated files in a directory named after the API major version.
basketry.config.json
{
"source": "petstore.json",
"parser": "@basketry/openapi-3",
"generators": ["@basketry/typescript"],
"output": "src",
"options": {
"typescript": {
"includeVersion": false
}
}
}

This option as configured will emit the following file structure:

my-project/
├── node_modules/
├── src/
│ ├── types.ts <-- generated
│ ├── index.ts
├── .gitignore
├── basketry.config.json
├── package.json
├── petstore.json
└── README.md

prettierConfig

  • Type: string - Path to a Prettier configuration file.

This option provides a path to a Prettier configuration file. This generator formats all generated code with prettier. If a prettierConfig option is provided, the generator will use that configuration file. If no prettierConfig option is provided, the generator will locate a Prettier configuration file per the Prettier configuration file resolution rules.

basketry.config.json
{
"source": "petstore.json",
"parser": "@basketry/openapi-3",
"generators": ["@basketry/typescript"],
"output": "src",
"options": {
"typescript": {
"prettierConfig": "alternate.prettier.config.js"
}
}
}

interfaceNomenclature

  • Type: string - The nomenclature to use for interfaces.

By default, the interfaces emitted by this generator will have names like PetsService or OrdersService. This behavior can be controlled with this option to specify the nomenclature to use for interfaces. The default is service, but if you supply a value like client, this generator will emit interface names like PetClient or OrdersClient.

basketry.config.json
{
"source": "petstore.json",
"parser": "@basketry/openapi-3",
"generators": ["@basketry/typescript"],
"output": "src",
"options": {
"typescript": {
"interfaceNomenclature": "client"
}
}
}