|
| 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 than 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 --save |
| 29 | +``` |
| 30 | + |
| 31 | +_using `npm link`:_ |
| 32 | + |
| 33 | +In PATH_TO_GENERATED_PACKAGE: |
| 34 | +``` |
| 35 | +npm link |
| 36 | +``` |
| 37 | + |
| 38 | +In your project: |
| 39 | +``` |
| 40 | +npm link |
| 41 | +``` |
| 42 | + |
| 43 | +__Note for Windows users:__ The Angular CLI has troubles to use linked npm packages. |
| 44 | +Please refer to this issue https://github.com/angular/angular-cli/issues/8284 for a solution / workaround. |
| 45 | +Published packages are not effected by this issue. |
| 46 | + |
| 47 | + |
| 48 | +#### General usage |
| 49 | + |
| 50 | +In your Angular project: |
| 51 | + |
| 52 | + |
| 53 | +``` |
| 54 | +// without configuring providers |
| 55 | +import { ApiModule } from ''; |
| 56 | +import { HttpClientModule } from '@angular/common/http'; |
| 57 | +
|
| 58 | +
|
| 59 | +@NgModule({ |
| 60 | + imports: [ |
| 61 | + ApiModule, |
| 62 | + // make sure to import the HttpClientModule in the AppModule only, |
| 63 | + // see https://github.com/angular/angular/issues/20575 |
| 64 | + HttpClientModule |
| 65 | + ], |
| 66 | + declarations: [ AppComponent ], |
| 67 | + providers: [], |
| 68 | + bootstrap: [ AppComponent ] |
| 69 | +}) |
| 70 | +export class AppModule {} |
| 71 | +``` |
| 72 | + |
| 73 | +``` |
| 74 | +// configuring providers |
| 75 | +import { ApiModule, Configuration, ConfigurationParameters } from ''; |
| 76 | +
|
| 77 | +export function apiConfigFactory (): Configuration => { |
| 78 | + const params: ConfigurationParameters = { |
| 79 | + // set configuration parameters here. |
| 80 | + } |
| 81 | + return new Configuration(params); |
| 82 | +} |
| 83 | +
|
| 84 | +@NgModule({ |
| 85 | + imports: [ ApiModule.forRoot(apiConfigFactory) ], |
| 86 | + declarations: [ AppComponent ], |
| 87 | + providers: [], |
| 88 | + bootstrap: [ AppComponent ] |
| 89 | +}) |
| 90 | +export class AppModule {} |
| 91 | +``` |
| 92 | + |
| 93 | +``` |
| 94 | +import { DefaultApi } from ''; |
| 95 | +
|
| 96 | +export class AppComponent { |
| 97 | + constructor(private apiGateway: DefaultApi) { } |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +Note: The ApiModule is restricted to being instantiated once app wide. |
| 102 | +This is to ensure that all services are treated as singletons. |
| 103 | + |
| 104 | +#### Using multiple swagger files / APIs / ApiModules |
| 105 | +In order to use multiple `ApiModules` generated from different swagger files, |
| 106 | +you can create an alias name when importing the modules |
| 107 | +in order to avoid naming conflicts: |
| 108 | +``` |
| 109 | +import { ApiModule } from 'my-api-path'; |
| 110 | +import { ApiModule as OtherApiModule } from 'my-other-api-path'; |
| 111 | +import { HttpClientModule } from '@angular/common/http'; |
| 112 | +
|
| 113 | +
|
| 114 | +@NgModule({ |
| 115 | + imports: [ |
| 116 | + ApiModule, |
| 117 | + OtherApiModule, |
| 118 | + // make sure to import the HttpClientModule in the AppModule only, |
| 119 | + // see https://github.com/angular/angular/issues/20575 |
| 120 | + HttpClientModule |
| 121 | + ] |
| 122 | +}) |
| 123 | +export class AppModule { |
| 124 | +
|
| 125 | +} |
| 126 | +``` |
| 127 | + |
| 128 | + |
| 129 | +### Set service base path |
| 130 | +If different than the generated base path, during app bootstrap, you can provide the base path to your service. |
| 131 | + |
| 132 | +``` |
| 133 | +import { BASE_PATH } from ''; |
| 134 | +
|
| 135 | +bootstrap(AppComponent, [ |
| 136 | + { provide: BASE_PATH, useValue: 'https://your-web-service.com' }, |
| 137 | +]); |
| 138 | +``` |
| 139 | +or |
| 140 | + |
| 141 | +``` |
| 142 | +import { BASE_PATH } from ''; |
| 143 | +
|
| 144 | +@NgModule({ |
| 145 | + imports: [], |
| 146 | + declarations: [ AppComponent ], |
| 147 | + providers: [ provide: BASE_PATH, useValue: 'https://your-web-service.com' ], |
| 148 | + bootstrap: [ AppComponent ] |
| 149 | +}) |
| 150 | +export class AppModule {} |
| 151 | +``` |
| 152 | + |
| 153 | + |
| 154 | +#### Using @angular/cli |
| 155 | +First extend your `src/environments/*.ts` files by adding the corresponding base path: |
| 156 | + |
| 157 | +``` |
| 158 | +export const environment = { |
| 159 | + production: false, |
| 160 | + API_BASE_PATH: 'http://127.0.0.1:8080' |
| 161 | +}; |
| 162 | +``` |
| 163 | + |
| 164 | +In the src/app/app.module.ts: |
| 165 | +``` |
| 166 | +import { BASE_PATH } from ''; |
| 167 | +import { environment } from '../environments/environment'; |
| 168 | +
|
| 169 | +@NgModule({ |
| 170 | + declarations: [ |
| 171 | + AppComponent |
| 172 | + ], |
| 173 | + imports: [ ], |
| 174 | + providers: [{ provide: BASE_PATH, useValue: environment.API_BASE_PATH }], |
| 175 | + bootstrap: [ AppComponent ] |
| 176 | +}) |
| 177 | +export class AppModule { } |
| 178 | +``` |
0 commit comments