| [d660d75] | 1 | ## @
|
|---|
| 2 |
|
|---|
| 3 | ### Building
|
|---|
| 4 |
|
|---|
| 5 | To install the required dependencies and to build the typescript sources run:
|
|---|
| 6 | ```
|
|---|
| 7 | npm install
|
|---|
| 8 | npm run build
|
|---|
| 9 | ```
|
|---|
| 10 |
|
|---|
| 11 | ### publishing
|
|---|
| 12 |
|
|---|
| 13 | First build the package then run ```npm publish```
|
|---|
| 14 |
|
|---|
| 15 | ### consuming
|
|---|
| 16 |
|
|---|
| 17 | Navigate to the folder of your consuming project and run one of next commands.
|
|---|
| 18 |
|
|---|
| 19 | _published:_
|
|---|
| 20 |
|
|---|
| 21 | ```
|
|---|
| 22 | npm install @ --save
|
|---|
| 23 | ```
|
|---|
| 24 |
|
|---|
| 25 | _without publishing (not recommended):_
|
|---|
| 26 |
|
|---|
| 27 | ```
|
|---|
| 28 | npm install PATH_TO_GENERATED_PACKAGE/-.tgz --save
|
|---|
| 29 | ```
|
|---|
| 30 |
|
|---|
| 31 | _It's important to take the tgz file, otherwise you'll get trouble with links on windows_
|
|---|
| 32 |
|
|---|
| 33 | _using `npm link`:_
|
|---|
| 34 |
|
|---|
| 35 | In PATH_TO_GENERATED_PACKAGE:
|
|---|
| 36 | ```
|
|---|
| 37 | npm link
|
|---|
| 38 | ```
|
|---|
| 39 |
|
|---|
| 40 | In your project:
|
|---|
| 41 | ```
|
|---|
| 42 | npm link
|
|---|
| 43 | ```
|
|---|
| 44 |
|
|---|
| 45 | __Note for Windows users:__ The Angular CLI has troubles to use linked npm packages.
|
|---|
| 46 | Please refer to this issue https://github.com/angular/angular-cli/issues/8284 for a solution / workaround.
|
|---|
| 47 | Published packages are not effected by this issue.
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 | #### General usage
|
|---|
| 51 |
|
|---|
| 52 | In your Angular project:
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 | ```
|
|---|
| 56 | // without configuring providers
|
|---|
| 57 | import { ApiModule } from '';
|
|---|
| 58 | import { HttpClientModule } from '@angular/common/http';
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 | @NgModule({
|
|---|
| 62 | imports: [
|
|---|
| 63 | ApiModule,
|
|---|
| 64 | // make sure to import the HttpClientModule in the AppModule only,
|
|---|
| 65 | // see https://github.com/angular/angular/issues/20575
|
|---|
| 66 | HttpClientModule
|
|---|
| 67 | ],
|
|---|
| 68 | declarations: [ AppComponent ],
|
|---|
| 69 | providers: [],
|
|---|
| 70 | bootstrap: [ AppComponent ]
|
|---|
| 71 | })
|
|---|
| 72 | export class AppModule {}
|
|---|
| 73 | ```
|
|---|
| 74 |
|
|---|
| 75 | ```
|
|---|
| 76 | // configuring providers
|
|---|
| 77 | import { ApiModule, Configuration, ConfigurationParameters } from '';
|
|---|
| 78 |
|
|---|
| 79 | export function apiConfigFactory (): Configuration => {
|
|---|
| 80 | const params: ConfigurationParameters = {
|
|---|
| 81 | // set configuration parameters here.
|
|---|
| 82 | }
|
|---|
| 83 | return new Configuration(params);
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | @NgModule({
|
|---|
| 87 | imports: [ ApiModule.forRoot(apiConfigFactory) ],
|
|---|
| 88 | declarations: [ AppComponent ],
|
|---|
| 89 | providers: [],
|
|---|
| 90 | bootstrap: [ AppComponent ]
|
|---|
| 91 | })
|
|---|
| 92 | export class AppModule {}
|
|---|
| 93 | ```
|
|---|
| 94 |
|
|---|
| 95 | ```
|
|---|
| 96 | // configuring providers with an authentication service that manages your access tokens
|
|---|
| 97 | import { ApiModule, Configuration } from '';
|
|---|
| 98 |
|
|---|
| 99 | @NgModule({
|
|---|
| 100 | imports: [ ApiModule ],
|
|---|
| 101 | declarations: [ AppComponent ],
|
|---|
| 102 | providers: [
|
|---|
| 103 | {
|
|---|
| 104 | provide: Configuration,
|
|---|
| 105 | useFactory: (authService: AuthService) => new Configuration(
|
|---|
| 106 | {
|
|---|
| 107 | basePath: environment.apiUrl,
|
|---|
| 108 | accessToken: authService.getAccessToken.bind(authService)
|
|---|
| 109 | }
|
|---|
| 110 | ),
|
|---|
| 111 | deps: [AuthService],
|
|---|
| 112 | multi: false
|
|---|
| 113 | }
|
|---|
| 114 | ],
|
|---|
| 115 | bootstrap: [ AppComponent ]
|
|---|
| 116 | })
|
|---|
| 117 | export class AppModule {}
|
|---|
| 118 | ```
|
|---|
| 119 |
|
|---|
| 120 | ```
|
|---|
| 121 | import { DefaultApi } from '';
|
|---|
| 122 |
|
|---|
| 123 | export class AppComponent {
|
|---|
| 124 | constructor(private apiGateway: DefaultApi) { }
|
|---|
| 125 | }
|
|---|
| 126 | ```
|
|---|
| 127 |
|
|---|
| 128 | Note: The ApiModule is restricted to being instantiated once app wide.
|
|---|
| 129 | This is to ensure that all services are treated as singletons.
|
|---|
| 130 |
|
|---|
| 131 | #### Using multiple OpenAPI files / APIs / ApiModules
|
|---|
| 132 | In order to use multiple `ApiModules` generated from different OpenAPI files,
|
|---|
| 133 | you can create an alias name when importing the modules
|
|---|
| 134 | in order to avoid naming conflicts:
|
|---|
| 135 | ```
|
|---|
| 136 | import { ApiModule } from 'my-api-path';
|
|---|
| 137 | import { ApiModule as OtherApiModule } from 'my-other-api-path';
|
|---|
| 138 | import { HttpClientModule } from '@angular/common/http';
|
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 | @NgModule({
|
|---|
| 142 | imports: [
|
|---|
| 143 | ApiModule,
|
|---|
| 144 | OtherApiModule,
|
|---|
| 145 | // make sure to import the HttpClientModule in the AppModule only,
|
|---|
| 146 | // see https://github.com/angular/angular/issues/20575
|
|---|
| 147 | HttpClientModule
|
|---|
| 148 | ]
|
|---|
| 149 | })
|
|---|
| 150 | export class AppModule {
|
|---|
| 151 |
|
|---|
| 152 | }
|
|---|
| 153 | ```
|
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 | ### Set service base path
|
|---|
| 157 | If different than the generated base path, during app bootstrap, you can provide the base path to your service.
|
|---|
| 158 |
|
|---|
| 159 | ```
|
|---|
| 160 | import { BASE_PATH } from '';
|
|---|
| 161 |
|
|---|
| 162 | bootstrap(AppComponent, [
|
|---|
| 163 | { provide: BASE_PATH, useValue: 'https://your-web-service.com' },
|
|---|
| 164 | ]);
|
|---|
| 165 | ```
|
|---|
| 166 | or
|
|---|
| 167 |
|
|---|
| 168 | ```
|
|---|
| 169 | import { BASE_PATH } from '';
|
|---|
| 170 |
|
|---|
| 171 | @NgModule({
|
|---|
| 172 | imports: [],
|
|---|
| 173 | declarations: [ AppComponent ],
|
|---|
| 174 | providers: [ provide: BASE_PATH, useValue: 'https://your-web-service.com' ],
|
|---|
| 175 | bootstrap: [ AppComponent ]
|
|---|
| 176 | })
|
|---|
| 177 | export class AppModule {}
|
|---|
| 178 | ```
|
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 | #### Using @angular/cli
|
|---|
| 182 | First extend your `src/environments/*.ts` files by adding the corresponding base path:
|
|---|
| 183 |
|
|---|
| 184 | ```
|
|---|
| 185 | export const environment = {
|
|---|
| 186 | production: false,
|
|---|
| 187 | API_BASE_PATH: 'http://127.0.0.1:8080'
|
|---|
| 188 | };
|
|---|
| 189 | ```
|
|---|
| 190 |
|
|---|
| 191 | In the src/app/app.module.ts:
|
|---|
| 192 | ```
|
|---|
| 193 | import { BASE_PATH } from '';
|
|---|
| 194 | import { environment } from '../environments/environment';
|
|---|
| 195 |
|
|---|
| 196 | @NgModule({
|
|---|
| 197 | declarations: [
|
|---|
| 198 | AppComponent
|
|---|
| 199 | ],
|
|---|
| 200 | imports: [ ],
|
|---|
| 201 | providers: [{ provide: BASE_PATH, useValue: environment.API_BASE_PATH }],
|
|---|
| 202 | bootstrap: [ AppComponent ]
|
|---|
| 203 | })
|
|---|
| 204 | export class AppModule { }
|
|---|
| 205 | ```
|
|---|