|
| 1 | + |
| 2 | + |
| 3 | +This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. |
| 4 | + |
| 5 | +The version of the OpenAPI document: 1.0.0 |
| 6 | + |
| 7 | +## Building |
| 8 | + |
| 9 | +To install the required dependencies and to build the typescript sources run: |
| 10 | + |
| 11 | +```console |
| 12 | +npm install |
| 13 | +npm run build |
| 14 | +``` |
| 15 | + |
| 16 | +## Publishing |
| 17 | + |
| 18 | +First build the package then run `npm publish dist` (don't forget to specify the `dist` folder!) |
| 19 | + |
| 20 | +## Consuming |
| 21 | + |
| 22 | +Navigate to the folder of your consuming project and run one of next commands. |
| 23 | + |
| 24 | +_published:_ |
| 25 | + |
| 26 | +```console |
| 27 | +npm install [email protected] --save |
| 28 | +``` |
| 29 | + |
| 30 | +_without publishing (not recommended):_ |
| 31 | + |
| 32 | +```console |
| 33 | +npm install PATH_TO_GENERATED_PACKAGE/dist.tgz --save |
| 34 | +``` |
| 35 | + |
| 36 | +_It's important to take the tgz file, otherwise you'll get trouble with links on windows_ |
| 37 | + |
| 38 | +_using `npm link`:_ |
| 39 | + |
| 40 | +In PATH_TO_GENERATED_PACKAGE/dist: |
| 41 | + |
| 42 | +```console |
| 43 | +npm link |
| 44 | +``` |
| 45 | + |
| 46 | +In your project: |
| 47 | + |
| 48 | +```console |
| 49 | +npm link sample-angular-19-0-0-with-angular-dependency-params |
| 50 | +``` |
| 51 | + |
| 52 | +__Note for Windows users:__ The Angular CLI has troubles to use linked npm packages. |
| 53 | +Please refer to this issue <https://github.com/angular/angular-cli/issues/8284> for a solution / workaround. |
| 54 | +Published packages are not effected by this issue. |
| 55 | + |
| 56 | +### General usage |
| 57 | + |
| 58 | +In your Angular project: |
| 59 | + |
| 60 | +```typescript |
| 61 | +// without configuring providers |
| 62 | +import { ApiModule } from 'sample-angular-19-0-0-with-angular-dependency-params'; |
| 63 | +import { HttpClientModule } from '@angular/common/http'; |
| 64 | + |
| 65 | +@NgModule({ |
| 66 | + imports: [ |
| 67 | + ApiModule, |
| 68 | + // make sure to import the HttpClientModule in the AppModule only, |
| 69 | + // see https://github.com/angular/angular/issues/20575 |
| 70 | + HttpClientModule |
| 71 | + ], |
| 72 | + declarations: [ AppComponent ], |
| 73 | + providers: [], |
| 74 | + bootstrap: [ AppComponent ] |
| 75 | +}) |
| 76 | +export class AppModule {} |
| 77 | +``` |
| 78 | + |
| 79 | +```typescript |
| 80 | +// configuring providers |
| 81 | +import { ApiModule, Configuration, ConfigurationParameters } from 'sample-angular-19-0-0-with-angular-dependency-params'; |
| 82 | + |
| 83 | +export function apiConfigFactory (): Configuration { |
| 84 | + const params: ConfigurationParameters = { |
| 85 | + // set configuration parameters here. |
| 86 | + } |
| 87 | + return new Configuration(params); |
| 88 | +} |
| 89 | + |
| 90 | +@NgModule({ |
| 91 | + imports: [ ApiModule.forRoot(apiConfigFactory) ], |
| 92 | + declarations: [ AppComponent ], |
| 93 | + providers: [], |
| 94 | + bootstrap: [ AppComponent ] |
| 95 | +}) |
| 96 | +export class AppModule {} |
| 97 | +``` |
| 98 | + |
| 99 | +```typescript |
| 100 | +// configuring providers with an authentication service that manages your access tokens |
| 101 | +import { ApiModule, Configuration } from 'sample-angular-19-0-0-with-angular-dependency-params'; |
| 102 | + |
| 103 | +@NgModule({ |
| 104 | + imports: [ ApiModule ], |
| 105 | + declarations: [ AppComponent ], |
| 106 | + providers: [ |
| 107 | + { |
| 108 | + provide: Configuration, |
| 109 | + useFactory: (authService: AuthService) => new Configuration( |
| 110 | + { |
| 111 | + basePath: environment.apiUrl, |
| 112 | + accessToken: authService.getAccessToken.bind(authService) |
| 113 | + } |
| 114 | + ), |
| 115 | + deps: [AuthService], |
| 116 | + multi: false |
| 117 | + } |
| 118 | + ], |
| 119 | + bootstrap: [ AppComponent ] |
| 120 | +}) |
| 121 | +export class AppModule {} |
| 122 | +``` |
| 123 | + |
| 124 | +```typescript |
| 125 | +import { DefaultApi } from 'sample-angular-19-0-0-with-angular-dependency-params'; |
| 126 | + |
| 127 | +export class AppComponent { |
| 128 | + constructor(private apiGateway: DefaultApi) { } |
| 129 | +} |
| 130 | +``` |
| 131 | + |
| 132 | +Note: The ApiModule is restricted to being instantiated once app wide. |
| 133 | +This is to ensure that all services are treated as singletons. |
| 134 | + |
| 135 | +### Using multiple OpenAPI files / APIs / ApiModules |
| 136 | + |
| 137 | +In order to use multiple `ApiModules` generated from different OpenAPI files, |
| 138 | +you can create an alias name when importing the modules |
| 139 | +in order to avoid naming conflicts: |
| 140 | + |
| 141 | +```typescript |
| 142 | +import { ApiModule } from 'my-api-path'; |
| 143 | +import { ApiModule as OtherApiModule } from 'my-other-api-path'; |
| 144 | +import { HttpClientModule } from '@angular/common/http'; |
| 145 | + |
| 146 | +@NgModule({ |
| 147 | + imports: [ |
| 148 | + ApiModule, |
| 149 | + OtherApiModule, |
| 150 | + // make sure to import the HttpClientModule in the AppModule only, |
| 151 | + // see https://github.com/angular/angular/issues/20575 |
| 152 | + HttpClientModule |
| 153 | + ] |
| 154 | +}) |
| 155 | +export class AppModule { |
| 156 | + |
| 157 | +} |
| 158 | +``` |
| 159 | + |
| 160 | +### Set service base path |
| 161 | + |
| 162 | +If different than the generated base path, during app bootstrap, you can provide the base path to your service. |
| 163 | + |
| 164 | +```typescript |
| 165 | +import { BASE_PATH } from 'sample-angular-19-0-0-with-angular-dependency-params'; |
| 166 | + |
| 167 | +bootstrap(AppComponent, [ |
| 168 | + { provide: BASE_PATH, useValue: 'https://your-web-service.com' }, |
| 169 | +]); |
| 170 | +``` |
| 171 | + |
| 172 | +or |
| 173 | + |
| 174 | +```typescript |
| 175 | +import { BASE_PATH } from 'sample-angular-19-0-0-with-angular-dependency-params'; |
| 176 | + |
| 177 | +@NgModule({ |
| 178 | + imports: [], |
| 179 | + declarations: [ AppComponent ], |
| 180 | + providers: [ provide: BASE_PATH, useValue: 'https://your-web-service.com' ], |
| 181 | + bootstrap: [ AppComponent ] |
| 182 | +}) |
| 183 | +export class AppModule {} |
| 184 | +``` |
| 185 | + |
| 186 | +### Using @angular/cli |
| 187 | + |
| 188 | +First extend your `src/environments/*.ts` files by adding the corresponding base path: |
| 189 | + |
| 190 | +```typescript |
| 191 | +export const environment = { |
| 192 | + production: false, |
| 193 | + API_BASE_PATH: 'http://127.0.0.1:8080' |
| 194 | +}; |
| 195 | +``` |
| 196 | + |
| 197 | +In the src/app/app.module.ts: |
| 198 | + |
| 199 | +```typescript |
| 200 | +import { BASE_PATH } from 'sample-angular-19-0-0-with-angular-dependency-params'; |
| 201 | +import { environment } from '../environments/environment'; |
| 202 | + |
| 203 | +@NgModule({ |
| 204 | + declarations: [ |
| 205 | + AppComponent |
| 206 | + ], |
| 207 | + imports: [ ], |
| 208 | + providers: [{ provide: BASE_PATH, useValue: environment.API_BASE_PATH }], |
| 209 | + bootstrap: [ AppComponent ] |
| 210 | +}) |
| 211 | +export class AppModule { } |
| 212 | +``` |
| 213 | + |
| 214 | +### Customizing path parameter encoding |
| 215 | + |
| 216 | +Without further customization, only [path-parameters][parameter-locations-url] of [style][style-values-url] 'simple' |
| 217 | +and Dates for format 'date-time' are encoded correctly. |
| 218 | + |
| 219 | +Other styles (e.g. "matrix") are not that easy to encode |
| 220 | +and thus are best delegated to other libraries (e.g.: [@honoluluhenk/http-param-expander]). |
| 221 | + |
| 222 | +To implement your own parameter encoding (or call another library), |
| 223 | +pass an arrow-function or method-reference to the `encodeParam` property of the Configuration-object |
| 224 | +(see [General Usage](#general-usage) above). |
| 225 | + |
| 226 | +Example value for use in your Configuration-Provider: |
| 227 | + |
| 228 | +```typescript |
| 229 | +new Configuration({ |
| 230 | + encodeParam: (param: Param) => myFancyParamEncoder(param), |
| 231 | +}) |
| 232 | +``` |
| 233 | + |
| 234 | +[parameter-locations-url]: https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#parameter-locations |
| 235 | +[style-values-url]: https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#style-values |
| 236 | +[@honoluluhenk/http-param-expander]: https://www.npmjs.com/package/@honoluluhenk/http-param-expander |
0 commit comments