NSwagStudio add custom typescript imports/lines

1.1k Views Asked by At

In my project I'm generating typescript clients via NSwagStudio.

To add credentails I have a BaseClient class that implements transformOptions.

export class MyClient extends BaseClient{...

Now I need to add following lines at the beginning of each generated script:

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck
import { BaseClient } from 'my-components-lib';

Can this be done via NSwagStudio-Configuration and if, how?

1

There are 1 best solutions below

0
On

For the openApiToTypeScriptClient code generator you can configure the setting extensionCode setting to use an external template.

Here's how I use it

"extensionCode": "nswag.imports.ts"

And this is what my nswag.imports.ts file looks like

import BaseClient from './MyComponent.base';

The final NSwag generated code looks something like this

/* tslint:disable */
/* eslint-disable */
//----------------------
// <auto-generated>
//     Generated using the NSwag toolchain v13.10.8.0 (NJsonSchema v10.3.11.0 (Newtonsoft.Json v12.0.0.0)) (http://NSwag.org)
// </auto-generated>
//----------------------
// ReSharper disable InconsistentNaming
import BaseClient from './MyComponent.base';
import axios, { AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse, CancelToken } from 'axios';