Nswag: Setting baseclass on TypeScript clients

Created on 24 Oct 2017  路  14Comments  路  Source: RicoSuter/NSwag

I would like to provide the generated clients with a baseclass so they can call this.getBaseUrl() as the url they need to call is only known at deployment time. I will use aurelia-configuration to provide the url at that time.
I can make the base client but the code does not compile since a

import { ClientBase } from './client-base';

is missing from the head of the generated file.
I am using msbuild like this
<Exec Command="$(NSwagExe) swagger2tsclient /input:swagger.json /output:src/server/backendservice.ts /UseGetBaseUrlMethod:true /ClientBaseClass:ClientBase" />

Am I doing this wrong?
If this is currently not supported, would it make sense for the generator to infer the filename from the base class name?

question

Most helpful comment

settings.TypeScriptGeneratorSettings.ExtensionCode = "import {BaseClient} from "./baseClient";";

All 14 comments

Create a new file "client.extensions.ts" add the import to the file and reference the file as ExtensionCode

https://github.com/RSuter/NSwag/wiki/SwaggerToTypeScriptClientGenerator#extended-classes-and-extension-code

Thank you this is great, I should have found it in the documentation.
Only remaining problem is that in my base class I inject AureliaConfiguration and the extending class calls super() without any parameters. Is there also an extension point for providing parameters to the constructor?

ConfigurationClass setting

Thank you for extremely fast responses, are there any examples of using the ConfigurationClass setting?

Found it in the source, thank you for a very nice and extensible product.

@cruising , did you succeed? I m interested to do the same, but even after read the link given by @RSuter I dont understand the way to do it. Could you share your solution?

@ranouf and @cruising I'm interested as well.
@RicoSuter I'm a bit confused here. The documentation of the TypeScriptClientGenerator (https://github.com/RicoSuter/NSwag/wiki/TypeScriptClientGenerator) describes an TypeScriptGeneratorSettings object. But I think that should be the TypeScriptClientGeneratorSettings. And the latter doesn't have a ExtensionCode property. Am I missing something?

The TypeScriptClientGeneratorSettings contains swagger client settings, the TypeScriptGeneratorSettings contains DTO schema generator settings (e.g. extension code

Thank you for the fast response. I see it now. TypeScriptGeneratorSettings is also a property of TypeScriptClientGeneratorSettings.

Hi. Could you be more detailed here a bit? I generate my SomeClient extends BaseClient and now all the file is surrounded with red error strokes because I have to manually add imports to the generated file.

Is there anyway to prepend file with:

import {BaseClient} from "./baseClient";
import {ClientConfiguration} from "./clientConfiguration";

It's quite annoying to do it manually everey time file gets regenerated.

My generation code:

var path = Path.GetDirectoryName (Util.CurrentQueryPath);
var document = await OpenApiDocument.FromFileAsync(Path.Combine(path, "my.swagger.json"));

var settings = new TypeScriptClientGeneratorSettings
{
    ClassName = $"{"My"}Client",
    OperationNameGenerator = new SingleClientFromOperationIdOperationNameGenerator(),
    UseTransformOptionsMethod = true,
    ClientBaseClass = "BaseClient",
    ConfigurationClass = "ClientConfiguration"
};

var generator = new TypeScriptClientGenerator(document, settings);
var code = generator.GenerateFile();
File.WriteAllText(Path.Combine(path, "src/generated/myClient.ts"), code);

settings.TypeScriptGeneratorSettings.ExtensionCode = "import {BaseClient} from "./baseClient";";

Or if you want it in a file (for more than one line) add a file
client.extensions.ts

within that file add
import {BaseClient} from "./baseClient";
import {ClientConfiguration} from "./clientConfiguration";

and call the generator with this flag:
/ExtensionCode:client.extensions.ts

Ah, this is why I didn't find it. The property is readonly so it's not listed in the initalizer list.

Was this page helpful?
0 / 5 - 0 ratings