|
1 | 1 | # Angular support
|
2 | 2 |
|
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: |
4 | 6 |
|
5 | 7 | `openapi --input ./spec.json --output ./generated --client angular`
|
6 | 8 |
|
7 |
| -This has been tested with the following versions: |
| 9 | +The Angular client has been tested with the following versions: |
8 | 10 |
|
9 | 11 | ```
|
10 | 12 | "@angular/common": "13.1.3",
|
11 | 13 | "@angular/core": "13.1.3",
|
12 | 14 | "rxjs": "7.5.2",
|
13 | 15 | ```
|
| 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 | +``` |
0 commit comments