Skip to content

Commit 9ea90fc

Browse files
committed
[typescript-angular] refactor: convert api service base class to static utils
1 parent 0e34d39 commit 9ea90fc

52 files changed

Lines changed: 628 additions & 318 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptAngularClientCodegen.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public void processOpts() {
183183
supportingFiles.add(new SupportingFile("index.mustache", getIndexDirectory(), "index.ts"));
184184
supportingFiles.add(new SupportingFile("api.module.mustache", getIndexDirectory(), "api.module.ts"));
185185
supportingFiles.add(new SupportingFile("configuration.mustache", getIndexDirectory(), "configuration.ts"));
186-
supportingFiles.add(new SupportingFile("api.base.service.mustache", getIndexDirectory(), "api.base.service.ts"));
186+
supportingFiles.add(new SupportingFile("api.utils.mustache", getIndexDirectory(), "api.utils.ts"));
187187
supportingFiles.add(new SupportingFile("variables.mustache", getIndexDirectory(), "variables.ts"));
188188
supportingFiles.add(new SupportingFile("encoder.mustache", getIndexDirectory(), "encoder.ts"));
189189
supportingFiles.add(new SupportingFile("param.mustache", getIndexDirectory(), "param.ts"));

modules/openapi-generator/src/main/resources/typescript-angular/api.base.service.mustache

Lines changed: 0 additions & 69 deletions
This file was deleted.

modules/openapi-generator/src/main/resources/typescript-angular/api.service.mustache

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { {{ classname }} } from '{{ filename }}';
1616
// @ts-ignore
1717
import { BASE_PATH, COLLECTION_FORMATS } from '../variables';
1818
import { {{configurationClassName}} } from '../configuration';
19-
import { BaseService } from '../api.base.service';
19+
import { addToHttpParams, canConsumeForm } from '../api.utils';
2020
{{#withInterfaces}}
2121
import {
2222
{{classname}}Interface{{#useSingleRequestParameter}}{{#operations}}{{#operation}}{{#allParams.0}},
@@ -56,14 +56,21 @@ export interface {{#prefixParameterInterfaces}}{{classname}}{{/prefixParameterIn
5656
})
5757
{{/isProvidedInNone}}
5858
{{#withInterfaces}}
59-
export class {{classname}} extends BaseService implements {{classname}}Interface {
59+
export class {{classname}} implements {{classname}}Interface {
6060
{{/withInterfaces}}
6161
{{^withInterfaces}}
62-
export class {{classname}} extends BaseService {
62+
export class {{classname}} {
6363
{{/withInterfaces}}
6464

65+
protected basePath = '{{{basePath}}}';
66+
public defaultHeaders = new HttpHeaders();
67+
public configuration: {{configurationClassName}};
68+
public encoder: HttpParameterCodec;
69+
6570
constructor(protected httpClient: HttpClient, @Optional() @Inject(BASE_PATH) basePath: string|string[], @Optional() configuration?: {{configurationClassName}}) {
66-
super(basePath, configuration);
71+
this.configuration = configuration || new {{configurationClassName}}();
72+
this.configuration.basePath = this.configuration.basePath ?? (Array.isArray(basePath) ? basePath[0] : basePath)
73+
this.encoder = this.configuration.encoder || new CustomHttpParameterCodec();
6774
}
6875

6976
{{#operation}}
@@ -111,25 +118,25 @@ export class {{classname}} extends BaseService {
111118
{{#isArray}}
112119
if ({{paramName}}) {
113120
{{#isQueryParamObjectFormatJson}}
114-
localVarQueryParameters = this.addToHttpParams(localVarQueryParameters,
121+
localVarQueryParameters = addToHttpParams(localVarQueryParameters,
115122
<any>{{paramName}}, '{{baseName}}');
116123
{{/isQueryParamObjectFormatJson}}
117124
{{^isQueryParamObjectFormatJson}}
118125
{{#isCollectionFormatMulti}}
119126
{{paramName}}.forEach((element) => {
120-
localVarQueryParameters = this.addToHttpParams(localVarQueryParameters,
127+
localVarQueryParameters = addToHttpParams(localVarQueryParameters,
121128
<any>element, '{{baseName}}');
122129
})
123130
{{/isCollectionFormatMulti}}
124131
{{^isCollectionFormatMulti}}
125-
localVarQueryParameters = this.addToHttpParams(localVarQueryParameters,
132+
localVarQueryParameters = addToHttpParams(localVarQueryParameters,
126133
[...{{paramName}}].join(COLLECTION_FORMATS['{{collectionFormat}}']), '{{baseName}}');
127134
{{/isCollectionFormatMulti}}
128135
{{/isQueryParamObjectFormatJson}}
129136
}
130137
{{/isArray}}
131138
{{^isArray}}
132-
localVarQueryParameters = this.addToHttpParams(localVarQueryParameters,
139+
localVarQueryParameters = addToHttpParams(localVarQueryParameters,
133140
<any>{{paramName}}, '{{baseName}}');
134141
{{/isArray}}
135142
{{/queryParams}}
@@ -216,7 +223,7 @@ export class {{classname}} extends BaseService {
216223
{{/bodyParam}}
217224

218225
{{#hasFormParams}}
219-
const canConsumeForm = this.canConsumeForm(consumes);
226+
const canConsumeFormResult = canConsumeForm(consumes);
220227

221228
let localVarFormParams: { append(param: string, value: any): any; };
222229
let localVarUseForm = false;
@@ -225,7 +232,7 @@ export class {{classname}} extends BaseService {
225232
{{#isFile}}
226233
// use FormData to transmit files using content-type "multipart/form-data"
227234
// see https://stackoverflow.com/questions/4007969/application-x-www-form-urlencoded-or-multipart-form-data
228-
localVarUseForm = canConsumeForm;
235+
localVarUseForm = canConsumeFormResult;
229236
{{/isFile}}
230237
{{/formParams}}
231238
if (localVarUseForm) {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { HttpParams } from '@angular/common/http';
2+
3+
export function canConsumeForm(consumes: string[]): boolean {
4+
return consumes.indexOf('multipart/form-data') !== -1;
5+
}
6+
7+
export function addToHttpParams(httpParams: HttpParams, value: any, key?: string): HttpParams {
8+
// If the value is an object (but not a Date), recursively add its keys.
9+
if (typeof value === 'object' && !(value instanceof Date)) {
10+
return addToHttpParamsRecursive(httpParams, value, key);
11+
}
12+
return addToHttpParamsRecursive(httpParams, value, key);
13+
}
14+
15+
function addToHttpParamsRecursive(httpParams: HttpParams, value?: any, key?: string): HttpParams {
16+
if (value === null || value === undefined) {
17+
return httpParams;
18+
}
19+
if (typeof value === 'object') {
20+
// If JSON format is preferred, key must be provided.
21+
if (key != null) {
22+
return httpParams.append(key, JSON.stringify(value));
23+
}
24+
// Otherwise, if it's an array, add each element.
25+
if (Array.isArray(value)) {
26+
value.forEach(elem => httpParams = addToHttpParamsRecursive(httpParams, elem, key));
27+
} else if (value instanceof Date) {
28+
if (key != null) {
29+
httpParams = httpParams.append(key, value.toISOString());
30+
} else {
31+
throw Error("key may not be null if value is Date");
32+
}
33+
} else {
34+
Object.keys(value).forEach(k => {
35+
const paramKey = key ? `${key}.${k}` : k;
36+
httpParams = addToHttpParamsRecursive(httpParams, value[k], paramKey);
37+
});
38+
}
39+
return httpParams;
40+
} else if (key != null) {
41+
return httpParams.append(key, value);
42+
}
43+
throw Error("key may not be null if value is not object or array");
44+
}

modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptangular/TypeScriptAngularClientCodegenTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,8 @@ public void testBasePath() throws IOException {
436436
generator.opts(clientOptInput).generate();
437437

438438
// THEN
439-
final String fileContents = Files.readString(Paths.get(output + "/api.base.service.ts"));
439+
// list all files in output:
440+
final String fileContents = Files.readString(Paths.get(output + "/api/default.service.ts"));
440441
assertThat(fileContents).containsOnlyOnce("basePath = '/relative/url'");
441442
}
442443
}

modules/openapi-generator/src/test/resources/3_0/typescript-angular/issue_20760.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ info:
99
servers:
1010
- url: /relative/url
1111
tags: []
12-
paths: {}
12+
paths:
13+
/some/endpoint:
14+
get:
15+
responses:
16+
"200":
17+
description: OK
1318
components:
1419
schemas: {}
1520
securitySchemes: {}

samples/client/others/typescript-angular/builds/composed-schemas-tagged-unions/api/pet.service.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,24 @@ import { PetWithMappedDiscriminatorModel } from '../model/petWithMappedDiscrimin
2222
// @ts-ignore
2323
import { BASE_PATH, COLLECTION_FORMATS } from '../variables';
2424
import { Configuration } from '../configuration';
25-
import { BaseService } from '../api.base.service';
25+
import { addToHttpParams, canConsumeForm } from '../api.utils';
2626

2727

2828

2929
@Injectable({
3030
providedIn: 'root'
3131
})
32-
export class PetService extends BaseService {
32+
export class PetService {
33+
34+
protected basePath = 'http://api.example.xyz/v1';
35+
public defaultHeaders = new HttpHeaders();
36+
public configuration: Configuration;
37+
public encoder: HttpParameterCodec;
3338

3439
constructor(protected httpClient: HttpClient, @Optional() @Inject(BASE_PATH) basePath: string|string[], @Optional() configuration?: Configuration) {
35-
super(basePath, configuration);
40+
this.configuration = configuration || new Configuration();
41+
this.configuration.basePath = this.configuration.basePath ?? (Array.isArray(basePath) ? basePath[0] : basePath)
42+
this.encoder = this.configuration.encoder || new CustomHttpParameterCodec();
3643
}
3744

3845
/**

samples/client/others/typescript-angular/builds/composed-schemas/api/pet.service.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,24 @@ import { PetWithMappedDiscriminatorModel } from '../model/petWithMappedDiscrimin
2222
// @ts-ignore
2323
import { BASE_PATH, COLLECTION_FORMATS } from '../variables';
2424
import { Configuration } from '../configuration';
25-
import { BaseService } from '../api.base.service';
25+
import { addToHttpParams, canConsumeForm } from '../api.utils';
2626

2727

2828

2929
@Injectable({
3030
providedIn: 'root'
3131
})
32-
export class PetService extends BaseService {
32+
export class PetService {
33+
34+
protected basePath = 'http://api.example.xyz/v1';
35+
public defaultHeaders = new HttpHeaders();
36+
public configuration: Configuration;
37+
public encoder: HttpParameterCodec;
3338

3439
constructor(protected httpClient: HttpClient, @Optional() @Inject(BASE_PATH) basePath: string|string[], @Optional() configuration?: Configuration) {
35-
super(basePath, configuration);
40+
this.configuration = configuration || new Configuration();
41+
this.configuration.basePath = this.configuration.basePath ?? (Array.isArray(basePath) ? basePath[0] : basePath)
42+
this.encoder = this.configuration.encoder || new CustomHttpParameterCodec();
3643
}
3744

3845
/**

samples/client/petstore/typescript-angular-v12-oneOf/builds/default/api/default.service.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,24 @@ import { Fruit } from '../model/fruit';
2222
// @ts-ignore
2323
import { BASE_PATH, COLLECTION_FORMATS } from '../variables';
2424
import { Configuration } from '../configuration';
25-
import { BaseService } from '../api.base.service';
25+
import { addToHttpParams, canConsumeForm } from '../api.utils';
2626

2727

2828

2929
@Injectable({
3030
providedIn: 'root'
3131
})
32-
export class DefaultService extends BaseService {
32+
export class DefaultService {
33+
34+
protected basePath = 'http://localhost';
35+
public defaultHeaders = new HttpHeaders();
36+
public configuration: Configuration;
37+
public encoder: HttpParameterCodec;
3338

3439
constructor(protected httpClient: HttpClient, @Optional() @Inject(BASE_PATH) basePath: string|string[], @Optional() configuration?: Configuration) {
35-
super(basePath, configuration);
40+
this.configuration = configuration || new Configuration();
41+
this.configuration.basePath = this.configuration.basePath ?? (Array.isArray(basePath) ? basePath[0] : basePath)
42+
this.encoder = this.configuration.encoder || new CustomHttpParameterCodec();
3643
}
3744

3845
/**

samples/client/petstore/typescript-angular-v12-provided-in-any/builds/default/api/pet.service.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,24 @@ import { Pet } from '../model/pet';
2424
// @ts-ignore
2525
import { BASE_PATH, COLLECTION_FORMATS } from '../variables';
2626
import { Configuration } from '../configuration';
27-
import { BaseService } from '../api.base.service';
27+
import { addToHttpParams, canConsumeForm } from '../api.utils';
2828

2929

3030

3131
@Injectable({
3232
providedIn: 'any'
3333
})
34-
export class PetService extends BaseService {
34+
export class PetService {
35+
36+
protected basePath = 'http://petstore.swagger.io/v2';
37+
public defaultHeaders = new HttpHeaders();
38+
public configuration: Configuration;
39+
public encoder: HttpParameterCodec;
3540

3641
constructor(protected httpClient: HttpClient, @Optional() @Inject(BASE_PATH) basePath: string|string[], @Optional() configuration?: Configuration) {
37-
super(basePath, configuration);
42+
this.configuration = configuration || new Configuration();
43+
this.configuration.basePath = this.configuration.basePath ?? (Array.isArray(basePath) ? basePath[0] : basePath)
44+
this.encoder = this.configuration.encoder || new CustomHttpParameterCodec();
3845
}
3946

4047
/**
@@ -173,7 +180,7 @@ export class PetService extends BaseService {
173180

174181
let localVarQueryParameters = new HttpParams({encoder: this.encoder});
175182
if (status) {
176-
localVarQueryParameters = this.addToHttpParams(localVarQueryParameters,
183+
localVarQueryParameters = addToHttpParams(localVarQueryParameters,
177184
[...status].join(COLLECTION_FORMATS['csv']), 'status');
178185
}
179186

@@ -236,7 +243,7 @@ export class PetService extends BaseService {
236243

237244
let localVarQueryParameters = new HttpParams({encoder: this.encoder});
238245
if (tags) {
239-
localVarQueryParameters = this.addToHttpParams(localVarQueryParameters,
246+
localVarQueryParameters = addToHttpParams(localVarQueryParameters,
240247
[...tags].join(COLLECTION_FORMATS['csv']), 'tags');
241248
}
242249

@@ -433,7 +440,7 @@ export class PetService extends BaseService {
433440
'application/x-www-form-urlencoded'
434441
];
435442

436-
const canConsumeForm = this.canConsumeForm(consumes);
443+
const canConsumeFormResult = canConsumeForm(consumes);
437444

438445
let localVarFormParams: { append(param: string, value: any): any; };
439446
let localVarUseForm = false;
@@ -511,14 +518,14 @@ export class PetService extends BaseService {
511518
'multipart/form-data'
512519
];
513520

514-
const canConsumeForm = this.canConsumeForm(consumes);
521+
const canConsumeFormResult = canConsumeForm(consumes);
515522

516523
let localVarFormParams: { append(param: string, value: any): any; };
517524
let localVarUseForm = false;
518525
let localVarConvertFormParamsToString = false;
519526
// use FormData to transmit files using content-type "multipart/form-data"
520527
// see https://stackoverflow.com/questions/4007969/application-x-www-form-urlencoded-or-multipart-form-data
521-
localVarUseForm = canConsumeForm;
528+
localVarUseForm = canConsumeFormResult;
522529
if (localVarUseForm) {
523530
localVarFormParams = new FormData();
524531
} else {

0 commit comments

Comments
 (0)