Skip to content

Commit afc0a88

Browse files
committed
feat(angular): add util "provideApi" for standalone applications (Angular 17 and above)
1 parent de310f6 commit afc0a88

21 files changed

Lines changed: 129 additions & 10 deletions

File tree

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

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,27 @@ public String getHelp() {
176176
@Override
177177
public void processOpts() {
178178
super.processOpts();
179+
180+
// determine NG version
181+
SemVer ngVersion;
182+
if (additionalProperties.containsKey(NG_VERSION)) {
183+
ngVersion = new SemVer(additionalProperties.get(NG_VERSION).toString());
184+
} else {
185+
ngVersion = new SemVer(this.ngVersion);
186+
LOGGER.info("generating code for Angular {} ...", ngVersion);
187+
LOGGER.info(" (you can select the angular version by setting the additionalProperties (--additional-properties in CLI) ngVersion)");
188+
}
189+
boolean ngVersionAtLeast_17 = ngVersion.atLeast("17.0.0");
190+
179191
supportingFiles.add(
180192
new SupportingFile("models.mustache", modelPackage().replace('.', File.separatorChar), "models.ts"));
181193
supportingFiles
182194
.add(new SupportingFile("apis.mustache", apiPackage().replace('.', File.separatorChar), "api.ts"));
183195
supportingFiles.add(new SupportingFile("index.mustache", getIndexDirectory(), "index.ts"));
184196
supportingFiles.add(new SupportingFile("api.module.mustache", getIndexDirectory(), "api.module.ts"));
197+
if (ngVersionAtLeast_17) {
198+
supportingFiles.add(new SupportingFile("provide-api.mustache", getIndexDirectory(), "provide-api.ts"));
199+
}
185200
supportingFiles.add(new SupportingFile("configuration.mustache", getIndexDirectory(), "configuration.ts"));
186201
supportingFiles.add(new SupportingFile("api.base.service.mustache", getIndexDirectory(), "api.base.service.ts"));
187202
supportingFiles.add(new SupportingFile("variables.mustache", getIndexDirectory(), "variables.ts"));
@@ -191,16 +206,6 @@ public void processOpts() {
191206
supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
192207
supportingFiles.add(new SupportingFile("README.mustache", getIndexDirectory(), "README.md"));
193208

194-
// determine NG version
195-
SemVer ngVersion;
196-
if (additionalProperties.containsKey(NG_VERSION)) {
197-
ngVersion = new SemVer(additionalProperties.get(NG_VERSION).toString());
198-
} else {
199-
ngVersion = new SemVer(this.ngVersion);
200-
LOGGER.info("generating code for Angular {} ...", ngVersion);
201-
LOGGER.info(" (you can select the angular version by setting the additionalProperties (--additional-properties in CLI) ngVersion)");
202-
}
203-
204209
if (!ngVersion.atLeast("9.0.0")) {
205210
throw new IllegalArgumentException("Invalid ngVersion: " + ngVersion + ". Only Angular v9+ is supported.");
206211
}
@@ -250,6 +255,7 @@ public void processOpts() {
250255
}
251256

252257
additionalProperties.put(NG_VERSION, ngVersion);
258+
additionalProperties.put("ngVersionAtLeast_17", ngVersionAtLeast_17);
253259

254260
if (additionalProperties.containsKey(API_MODULE_PREFIX)) {
255261
String apiModulePrefix = additionalProperties.get(API_MODULE_PREFIX).toString();

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@ export * from './model/models';
55
export * from './variables';
66
export * from './configuration';
77
export * from './api.module';
8+
{{#ngVersionAtLeast_17}}
9+
export * from './provide-api';
10+
{{/ngVersionAtLeast_17}}
811
export * from './param';
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { makeEnvironmentProviders } from "@angular/core";
2+
import { {{configurationClassName}}, {{configurationParametersInterfaceName}} } from './configuration';
3+
import { BASE_PATH } from './variables';
4+
5+
export function provideApi(configOrBasePath: string | {{configurationParametersInterfaceName}}) {
6+
return makeEnvironmentProviders([
7+
typeof configOrBasePath === "string"
8+
? { provide: BASE_PATH, useValue: configOrBasePath }
9+
: {
10+
provide: {{configurationClassName}},
11+
useValue: new {{configurationClassName}}({ ...configOrBasePath }),
12+
},
13+
]);
14+
}

samples/client/others/typescript-angular/builds/composed-schemas-tagged-unions/.openapi-generator/FILES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ model/petWithMappedDiscriminator.ts
2121
model/petWithSimpleDiscriminator.ts
2222
model/petWithoutDiscriminator.ts
2323
param.ts
24+
provide-api.ts
2425
variables.ts

samples/client/others/typescript-angular/builds/composed-schemas-tagged-unions/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ export * from './model/models';
33
export * from './variables';
44
export * from './configuration';
55
export * from './api.module';
6+
export * from './provide-api';
67
export * from './param';
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { makeEnvironmentProviders } from "@angular/core";
2+
import { Configuration, ConfigurationParameters } from './configuration';
3+
import { BASE_PATH } from './variables';
4+
5+
export function provideApi(configOrBasePath: string | ConfigurationParameters) {
6+
return makeEnvironmentProviders([
7+
typeof configOrBasePath === "string"
8+
? { provide: BASE_PATH, useValue: configOrBasePath }
9+
: {
10+
provide: Configuration,
11+
useValue: new Configuration({ ...configOrBasePath }),
12+
},
13+
]);
14+
}

samples/client/others/typescript-angular/builds/composed-schemas/.openapi-generator/FILES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ model/petWithMappedDiscriminator.ts
2121
model/petWithSimpleDiscriminator.ts
2222
model/petWithoutDiscriminator.ts
2323
param.ts
24+
provide-api.ts
2425
variables.ts

samples/client/others/typescript-angular/builds/composed-schemas/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ export * from './model/models';
33
export * from './variables';
44
export * from './configuration';
55
export * from './api.module';
6+
export * from './provide-api';
67
export * from './param';
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { makeEnvironmentProviders } from "@angular/core";
2+
import { Configuration, ConfigurationParameters } from './configuration';
3+
import { BASE_PATH } from './variables';
4+
5+
export function provideApi(configOrBasePath: string | ConfigurationParameters) {
6+
return makeEnvironmentProviders([
7+
typeof configOrBasePath === "string"
8+
? { provide: BASE_PATH, useValue: configOrBasePath }
9+
: {
10+
provide: Configuration,
11+
useValue: new Configuration({ ...configOrBasePath }),
12+
},
13+
]);
14+
}

samples/client/petstore/typescript-angular-v17-provided-in-root/builds/default/.openapi-generator/FILES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ model/pet.ts
1818
model/tag.ts
1919
model/user.ts
2020
param.ts
21+
provide-api.ts
2122
variables.ts

0 commit comments

Comments
 (0)