|
| 1 | +## @swagger/angular2-typescript-petstore@0.0.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 dist``` (don't forget to specify the `dist` folder!) |
| 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 @swagger/[email protected] --save |
| 23 | +``` |
| 24 | + |
| 25 | +_without publishing (not recommended):_ |
| 26 | + |
| 27 | +``` |
| 28 | +npm install PATH_TO_GENERATED_PACKAGE/dist.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/dist: |
| 36 | +``` |
| 37 | +npm link |
| 38 | +``` |
| 39 | + |
| 40 | +In your project: |
| 41 | +``` |
| 42 | +npm link @swagger/angular2-typescript-petstore |
| 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 '@swagger/angular2-typescript-petstore'; |
| 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 '@swagger/angular2-typescript-petstore'; |
| 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 | +import { DefaultApi } from '@swagger/angular2-typescript-petstore'; |
| 97 | +
|
| 98 | +export class AppComponent { |
| 99 | + constructor(private apiGateway: DefaultApi) { } |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +Note: The ApiModule is restricted to being instantiated once app wide. |
| 104 | +This is to ensure that all services are treated as singletons. |
| 105 | + |
| 106 | +#### Using multiple swagger files / APIs / ApiModules |
| 107 | +In order to use multiple `ApiModules` generated from different swagger files, |
| 108 | +you can create an alias name when importing the modules |
| 109 | +in order to avoid naming conflicts: |
| 110 | +``` |
| 111 | +import { ApiModule } from 'my-api-path'; |
| 112 | +import { ApiModule as OtherApiModule } from 'my-other-api-path'; |
| 113 | +import { HttpClientModule } from '@angular/common/http'; |
| 114 | +
|
| 115 | +
|
| 116 | +@NgModule({ |
| 117 | + imports: [ |
| 118 | + ApiModule, |
| 119 | + OtherApiModule, |
| 120 | + // make sure to import the HttpClientModule in the AppModule only, |
| 121 | + // see https://github.com/angular/angular/issues/20575 |
| 122 | + HttpClientModule |
| 123 | + ] |
| 124 | +}) |
| 125 | +export class AppModule { |
| 126 | +
|
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | + |
| 131 | +### Set service base path |
| 132 | +If different than the generated base path, during app bootstrap, you can provide the base path to your service. |
| 133 | + |
| 134 | +``` |
| 135 | +import { BASE_PATH } from '@swagger/angular2-typescript-petstore'; |
| 136 | +
|
| 137 | +bootstrap(AppComponent, [ |
| 138 | + { provide: BASE_PATH, useValue: 'https://your-web-service.com' }, |
| 139 | +]); |
| 140 | +``` |
| 141 | +or |
| 142 | + |
| 143 | +``` |
| 144 | +import { BASE_PATH } from '@swagger/angular2-typescript-petstore'; |
| 145 | +
|
| 146 | +@NgModule({ |
| 147 | + imports: [], |
| 148 | + declarations: [ AppComponent ], |
| 149 | + providers: [ provide: BASE_PATH, useValue: 'https://your-web-service.com' ], |
| 150 | + bootstrap: [ AppComponent ] |
| 151 | +}) |
| 152 | +export class AppModule {} |
| 153 | +``` |
| 154 | + |
| 155 | + |
| 156 | +#### Using @angular/cli |
| 157 | +First extend your `src/environments/*.ts` files by adding the corresponding base path: |
| 158 | + |
| 159 | +``` |
| 160 | +export const environment = { |
| 161 | + production: false, |
| 162 | + API_BASE_PATH: 'http://127.0.0.1:8080' |
| 163 | +}; |
| 164 | +``` |
| 165 | + |
| 166 | +In the src/app/app.module.ts: |
| 167 | +``` |
| 168 | +import { BASE_PATH } from '@swagger/angular2-typescript-petstore'; |
| 169 | +import { environment } from '../environments/environment'; |
| 170 | +
|
| 171 | +@NgModule({ |
| 172 | + declarations: [ |
| 173 | + AppComponent |
| 174 | + ], |
| 175 | + imports: [ ], |
| 176 | + providers: [{ provide: BASE_PATH, useValue: environment.API_BASE_PATH }], |
| 177 | + bootstrap: [ AppComponent ] |
| 178 | +}) |
| 179 | +export class AppModule { } |
| 180 | +``` |
0 commit comments