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"
}
}
}