Skip to content

Templates

TIP

You need version 2.x.x or higher to use templates.

What are templates?

With templates, you can easily override/change the generated output of a specific plugin. Let's say you want to use fetch instead of axios, instead of forking the @kubb/swagger-client you can use templates.

In the background, templates will use React/JSX as the template engine. See @kubb/react to find out which intern components and hooks we provide.

How to use templates?

How does it work?

For example, the following code will create a file helloWorld.ts inside the ./src folder(the FileManager will handle the creation of this file).

Next to that, we have an import(see File.Import) that will add an import statement to the generated file.

At this moment the file will only contain the import statement. To add some code we will use File.Source to export a const called helloWorld.


All of this is handled by @kubb/react, see root.render, where we render the template and also provide some extra meta(that will be used by the hooks we provide). As a user of templates you don't need to do anything, this is all handled by theOperationGenerator of the specific plugin you use.

typescript
<File
  baseName="helloWorld.ts"
  path="./src/helloWorld.ts"
  meta={{}}
>
  <File.Import name="axios" path="axios" />
  <File.Source>
    export const helloWorld = true;
  </File.Source>
</File>
typescript
import axios from 'axios'

export const helloWorld = true

Released under the MIT License.