forked from ferdikoomen/openapi-typescript-codegen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.hbs
81 lines (72 loc) · 2.29 KB
/
client.hbs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
{{>header}}
{{#equals @root.httpClient 'angular'}}
import { NgModule} from '@angular/core';
import { HttpClientModule } from '@angular/common/http';
import { AngularHttpRequest } from './core/AngularHttpRequest';
import { BaseHttpRequest } from './core/BaseHttpRequest';
import type { OpenAPIConfig } from './core/OpenAPI';
import { OpenAPI } from './core/OpenAPI';
{{else}}
import type { BaseHttpRequest } from './core/BaseHttpRequest';
import type { OpenAPIConfig } from './core/OpenAPI';
import { {{{httpRequest}}} } from './core/{{{httpRequest}}}';
{{/equals}}
{{#if services}}
{{#each services}}
import { {{{name}}}{{{@root.postfix}}} } from './services/{{{name}}}{{{@root.postfix}}}';
{{/each}}
{{/if}}
{{#equals @root.httpClient 'angular'}}
@NgModule({
imports: [HttpClientModule],
providers: [
{
provide: OpenAPI,
useValue: {
BASE: OpenAPI?.BASE ?? '{{{server}}}',
VERSION: OpenAPI?.VERSION ?? '{{{version}}}',
WITH_CREDENTIALS: OpenAPI?.WITH_CREDENTIALS ?? false,
CREDENTIALS: OpenAPI?.CREDENTIALS ?? 'include',
TOKEN: OpenAPI?.TOKEN,
USERNAME: OpenAPI?.USERNAME,
PASSWORD: OpenAPI?.PASSWORD,
HEADERS: OpenAPI?.HEADERS,
ENCODE_PATH: OpenAPI?.ENCODE_PATH,
} as OpenAPIConfig,
},
{
provide: BaseHttpRequest,
useClass: AngularHttpRequest,
},
{{#each services}}
{{{name}}}{{{@root.postfix}}},
{{/each}}
]
})
export class {{{clientName}}} {}
{{else}}
type HttpRequestConstructor = new (config: OpenAPIConfig) => BaseHttpRequest;
export class {{{clientName}}} {
{{#each services}}
public readonly {{{camelCase name}}}: {{{name}}}{{{@root.postfix}}};
{{/each}}
public readonly request: BaseHttpRequest;
constructor(config?: Partial<OpenAPIConfig>, HttpRequest: HttpRequestConstructor = {{{httpRequest}}}) {
this.request = new HttpRequest({
BASE: config?.BASE ?? '{{{server}}}',
VERSION: config?.VERSION ?? '{{{version}}}',
WITH_CREDENTIALS: config?.WITH_CREDENTIALS ?? false,
CREDENTIALS: config?.CREDENTIALS ?? 'include',
TOKEN: config?.TOKEN,
USERNAME: config?.USERNAME,
PASSWORD: config?.PASSWORD,
HEADERS: config?.HEADERS,
ENCODE_PATH: config?.ENCODE_PATH,
MAX_REDIRECTS: config?.MAX_REDIRECTS,
});
{{#each services}}
this.{{{camelCase name}}} = new {{{name}}}{{{@root.postfix}}}(this.request);
{{/each}}
}
}
{{/equals}}