Skip to content

Commit ff1cb27

Browse files
committed
- Added angular documentation
1 parent 71fd3ed commit ff1cb27

File tree

2 files changed

+76
-5
lines changed

2 files changed

+76
-5
lines changed

docs/angular-support.md

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,84 @@
11
# Angular support
22

3-
Lorem
3+
This tool allows you to generate a client based on the [`Angular HttpClient`](https://angular.io/guide/http).
4+
The generated services are fully injectable and make use of the [RxJS](https://rxjs.dev/) Observer pattern.
5+
If you want to generate the Angular based client then you can specify `--client angular` in the openapi call:
46

57
`openapi --input ./spec.json --output ./generated --client angular`
68

7-
This has been tested with the following versions:
9+
The Angular client has been tested with the following versions:
810

911
```
1012
"@angular/common": "13.1.3",
1113
"@angular/core": "13.1.3",
1214
"rxjs": "7.5.2",
1315
```
16+
17+
## Example
18+
19+
In the AppModule you can import the services and add them to the list of injectable services:
20+
21+
```typescript
22+
import { HttpClientModule } from '@angular/common/http';
23+
import { NgModule } from '@angular/core';
24+
import { BrowserModule } from '@angular/platform-browser';
25+
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
26+
27+
import { OrganizationService } from './generated/services/OrganizationService';
28+
29+
@NgModule({
30+
imports: [
31+
BrowserModule,
32+
HttpClientModule,
33+
],
34+
providers: [
35+
OrganizationService,
36+
],
37+
bootstrap: [
38+
AppComponent,
39+
],
40+
})
41+
export class AppModule {}
42+
43+
platformBrowserDynamic()
44+
.bootstrapModule(AppModule)
45+
.catch(err => console.error(err));
46+
```
47+
48+
Inside the component you can inject the service and just use it as you would with any observable:
49+
50+
```typescript
51+
import { Component } from '@angular/core';
52+
53+
import type { OrganizationService } from './generated/services/OrganizationService';
54+
55+
@Component({
56+
selector: 'app-root',
57+
template: `<div>Angular is ready</div>`,
58+
})
59+
export class AppComponent {
60+
constructor(private readonly organizationService: OrganizationService) {
61+
62+
// Make a call
63+
this.organizationService
64+
.createOrganization({
65+
name: 'OrgName',
66+
description: 'OrgDescription',
67+
})
68+
.subscribe(organization => {
69+
console.log(organization);
70+
});
71+
72+
// Or even map result and retry on error
73+
this.organizationService
74+
.getOrganizations()
75+
.pipe(
76+
map(organizations => organizations[0]),
77+
retryWhen(error => error)
78+
)
79+
.subscribe(organization => {
80+
console.log(organization);
81+
});
82+
}
83+
}
84+
```

test/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const generate = async (input, output) => {
77
await OpenAPI.generate({
88
input,
99
output,
10-
httpClient: OpenAPI.HttpClient.ANGULAR,
10+
httpClient: OpenAPI.HttpClient.FETCH,
1111
useOptions: false,
1212
useUnionTypes: false,
1313
exportCore: true,
@@ -57,8 +57,8 @@ const generateRealWorldSpecs = async () => {
5757
};
5858

5959
const main = async () => {
60-
await generate('./test/spec/aap.json', './test/generated/aap/');
61-
// await generate('./test/spec/v3.json', './test/generated/v3/');
60+
await generate('./test/spec/v2.json', './test/generated/v2/');
61+
await generate('./test/spec/v3.json', './test/generated/v3/');
6262
// await generateRealWorldSpecs();
6363
};
6464

0 commit comments

Comments
 (0)