RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.73k stars 1.29k forks source link

clientBaseClass option is not importing/copying the base class to the output file #933

Open fodiimre opened 7 years ago

fodiimre commented 7 years ago

Hey guys,

I tried to wire the clientBaseClass option, because I need to use the transformOptions method to add custom HTTP Headers for authentication purposes. I'm generating a TypeScript client. I think my nswag.json has everything required set up:

...
"useTransformOptionsMethod": true,
"clientBaseClass": "ServiceBase",
"extensionCode": "api-client.extensions.ts",
"output": "au-api-client.ts"
...

I set it up, based on this. My problem is that an import statement is missing for the ServiceBase class in the generated file, so the generated client cannot be used at all:

import { inject } from 'aurelia-framework';
import { HttpClient, RequestInit } from 'aurelia-fetch-client';

import * as moment from 'moment';

@inject(String, HttpClient)
export class ApplicationService extends ServiceBase {
    private http: { fetch(url: RequestInfo, init?: RequestInit): Promise<Response> };
...

Here it seems to add the base class to the generated file, but for me it does not.

Version info: Generated using the NSwag toolchain v11.6.1.0 (NJsonSchema v9.5.0.0)

Any ideas how to fix it? Thank you.

RicoSuter commented 7 years ago

You have to add the import statement to "api-client.extensions.ts"

RicoSuter commented 7 years ago

... or define the base class directly in "api-client.extensions.ts" so that it is inlined... (like in the sample)

fodiimre commented 7 years ago

I have the ServiceBase class defined in the api-client.extensions.ts, what should I import there? I have this in api-client.extensions.ts:

export class ServiceBase {
    protected transformOptions(options: RequestOptionsArgs) {
    console.log('transformOptions');
        return Promise.resolve(options);
    }

    protected transformResult(url: string, response: Response, processor: (response: Response) => any): Observable<any> {
    console.log('transformResult');
        return processor(response);
    }
}
RicoSuter commented 7 years ago

Ok this is strange...

RicoSuter commented 7 years ago

Try manually adding "ServiceBase" to the "ExtensionClasses" settings:

"extensionClasses": [ "ServiceBase" ]

the ClientBaseClass should be automatically added but there seems to be a problem...

fodiimre commented 7 years ago

Adding "extensionClasses": [ "ServiceBase" ] did not help.

RicoSuter commented 7 years ago

Somehow the extension code loader does not find your class and does not load your class:

https://github.com/RSuter/NJsonSchema/blob/master/src/NJsonSchema.CodeGeneration.TypeScript/TypeScriptExtensionCode.cs#L29

Do you see a problem here?

HJoost commented 6 years ago

I encountered the same problem. It seems that it has to do with windows/unix file endings. On my windows machine this doesn't work:

export class Lvsapibaseclient {...}

But this ugly one get's generated in the right way:

export class Lvsapibaseclient /\r /{...}