diff --git a/modules/swagger-codegen/src/main/resources/typescript-angular/README.mustache b/modules/swagger-codegen/src/main/resources/typescript-angular/README.mustache index 7f7acecf7fe..e3a822652fb 100644 --- a/modules/swagger-codegen/src/main/resources/typescript-angular/README.mustache +++ b/modules/swagger-codegen/src/main/resources/typescript-angular/README.mustache @@ -166,6 +166,53 @@ export class AppModule {} ``` +### Set dynamic service base path +If required the base path can be modified throughout the lifecycle of the +service by injecting an `Observable` using the `BASE_PATH_OBSERVABLE` token. + + +``` +import { BASE_PATH_OBSERVABLE } from '{{npmName}}'; + +@NgModule({ + imports: [], + declarations: [ AppComponent ], + providers: [ + DynamicBasePathService, + { + provide: BASE_PATH_OBSERVABLE, + useFactory: () => new BehaviorSubject<string>('http://localhost:5000') + } + ], + bootstrap: [ AppComponent ] +}) +export class AppModule {} + +``` + +An example service defined below allows publishing new basePaths to the api services + +``` + +import {Inject, Injectable} from '@angular/core'; +import {Subject} from 'rxjs/Subject'; +import {BASE_PATH_OBSERVABLE} from '{{npmName}}'; + +@Injectable() +export class DynamicBasePathService { + + constructor(@Inject(BASE_PATH_OBSERVABLE) private basePathSubject$: Subject<string>) { + } + + public setBasePath(basePath: string): void { + this.basePathSubject$.next(basePath); + } + +} + +``` + + #### Using @angular/cli First extend your `src/environments/*.ts` files by adding the corresponding base path: diff --git a/modules/swagger-codegen/src/main/resources/typescript-angular/api.service.mustache b/modules/swagger-codegen/src/main/resources/typescript-angular/api.service.mustache index 8d665c768f7..13964b7ba50 100644 --- a/modules/swagger-codegen/src/main/resources/typescript-angular/api.service.mustache +++ b/modules/swagger-codegen/src/main/resources/typescript-angular/api.service.mustache @@ -28,7 +28,7 @@ import '../rxjs-operators'; import { {{classname}} } from '../{{filename}}'; {{/imports}} -import { BASE_PATH, COLLECTION_FORMATS } from '../variables'; +import { BASE_PATH, COLLECTION_FORMATS, BASE_PATH_OBSERVABLE } from '../variables'; import { Configuration } from '../configuration'; {{#withInterfaces}} import { {{classname}}Interface } from './{{classFilename}}Interface'; @@ -53,7 +53,10 @@ export class {{classname}} { public defaultHeaders = new {{#useHttpClient}}Http{{/useHttpClient}}Headers(); public configuration = new Configuration(); - constructor(protected {{#useHttpClient}}httpClient: HttpClient{{/useHttpClient}}{{^useHttpClient}}http: Http{{/useHttpClient}}, @Optional()@Inject(BASE_PATH) basePath: string, @Optional() configuration: Configuration) { + constructor(protected {{#useHttpClient}}httpClient: HttpClient{{/useHttpClient}}{{^useHttpClient}}http: Http{{/useHttpClient}}, + @Optional() @Inject(BASE_PATH) basePath: string, + @Optional() configuration: Configuration, + @Optional() @Inject(BASE_PATH_OBSERVABLE) basePath$: Observable) { if (basePath) { this.basePath = basePath; } @@ -61,6 +64,9 @@ export class {{classname}} { this.configuration = configuration; this.basePath = basePath || configuration.basePath || this.basePath; } + if (basePath$) { + basePath$.subscribe(nextBasePath => this.basePath = nextBasePath); + } } /** diff --git a/modules/swagger-codegen/src/main/resources/typescript-angular/variables.mustache b/modules/swagger-codegen/src/main/resources/typescript-angular/variables.mustache index b3241fcebcd..cd009fc82d2 100644 --- a/modules/swagger-codegen/src/main/resources/typescript-angular/variables.mustache +++ b/modules/swagger-codegen/src/main/resources/typescript-angular/variables.mustache @@ -1,6 +1,14 @@ import { {{injectionToken}} } from '@angular/core'; +{{^useRxJS6}} +import { Observable } from 'rxjs/Observable'; +{{/useRxJS6}} +{{#useRxJS6}} +import { Observable } from 'rxjs'; +{{/useRxJS6}} + export const BASE_PATH = new {{injectionToken}}{{#injectionTokenTyped}}{{/injectionTokenTyped}}('basePath'); +export const BASE_PATH_OBSERVABLE = new {{injectionToken}}{{#injectionTokenTyped}}>{{/injectionTokenTyped}}('basePathObservable'); export const COLLECTION_FORMATS = { 'csv': ',', 'tsv': ' ', diff --git a/samples/client/petstore/typescript-angular-v2/default/README.md b/samples/client/petstore/typescript-angular-v2/default/README.md index 851ec448b23..a269164bf1b 100644 --- a/samples/client/petstore/typescript-angular-v2/default/README.md +++ b/samples/client/petstore/typescript-angular-v2/default/README.md @@ -147,6 +147,53 @@ export class AppModule {} ``` +### Set dynamic service base path +If required the base path can be modified throughout the lifecycle of the +service by injecting an `Observable` using the `BASE_PATH_OBSERVABLE` token. + + +``` +import { BASE_PATH_OBSERVABLE } from ''; + +@NgModule({ + imports: [], + declarations: [ AppComponent ], + providers: [ + DynamicBasePathService, + { + provide: BASE_PATH_OBSERVABLE, + useFactory: () => new BehaviorSubject<string>('http://localhost:5000') + } + ], + bootstrap: [ AppComponent ] +}) +export class AppModule {} + +``` + +An example service defined below allows publishing new basePaths to the api services + +``` + +import {Inject, Injectable} from '@angular/core'; +import {Subject} from 'rxjs/Subject'; +import {BASE_PATH_OBSERVABLE} from ''; + +@Injectable() +export class DynamicBasePathService { + + constructor(@Inject(BASE_PATH_OBSERVABLE) private basePathSubject$: Subject<string>) { + } + + public setBasePath(basePath: string): void { + this.basePathSubject$.next(basePath); + } + +} + +``` + + #### Using @angular/cli First extend your `src/environments/*.ts` files by adding the corresponding base path: diff --git a/samples/client/petstore/typescript-angular-v2/default/api/pet.service.ts b/samples/client/petstore/typescript-angular-v2/default/api/pet.service.ts index 0f487e7b1a0..4f8d19784e8 100644 --- a/samples/client/petstore/typescript-angular-v2/default/api/pet.service.ts +++ b/samples/client/petstore/typescript-angular-v2/default/api/pet.service.ts @@ -23,7 +23,7 @@ import '../rxjs-operators'; import { ApiResponse } from '../model/apiResponse'; import { Pet } from '../model/pet'; -import { BASE_PATH, COLLECTION_FORMATS } from '../variables'; +import { BASE_PATH, COLLECTION_FORMATS, BASE_PATH_OBSERVABLE } from '../variables'; import { Configuration } from '../configuration'; @@ -34,7 +34,10 @@ export class PetService { public defaultHeaders = new Headers(); public configuration = new Configuration(); - constructor(protected http: Http, @Optional()@Inject(BASE_PATH) basePath: string, @Optional() configuration: Configuration) { + constructor(protected http: Http, + @Optional() @Inject(BASE_PATH) basePath: string, + @Optional() configuration: Configuration, + @Optional() @Inject(BASE_PATH_OBSERVABLE) basePath$: Observable) { if (basePath) { this.basePath = basePath; } @@ -42,6 +45,9 @@ export class PetService { this.configuration = configuration; this.basePath = basePath || configuration.basePath || this.basePath; } + if (basePath$) { + basePath$.subscribe(nextBasePath => this.basePath = nextBasePath); + } } /** diff --git a/samples/client/petstore/typescript-angular-v2/default/api/store.service.ts b/samples/client/petstore/typescript-angular-v2/default/api/store.service.ts index 443d2ba8480..13a18b7aeec 100644 --- a/samples/client/petstore/typescript-angular-v2/default/api/store.service.ts +++ b/samples/client/petstore/typescript-angular-v2/default/api/store.service.ts @@ -22,7 +22,7 @@ import '../rxjs-operators'; import { Order } from '../model/order'; -import { BASE_PATH, COLLECTION_FORMATS } from '../variables'; +import { BASE_PATH, COLLECTION_FORMATS, BASE_PATH_OBSERVABLE } from '../variables'; import { Configuration } from '../configuration'; @@ -33,7 +33,10 @@ export class StoreService { public defaultHeaders = new Headers(); public configuration = new Configuration(); - constructor(protected http: Http, @Optional()@Inject(BASE_PATH) basePath: string, @Optional() configuration: Configuration) { + constructor(protected http: Http, + @Optional() @Inject(BASE_PATH) basePath: string, + @Optional() configuration: Configuration, + @Optional() @Inject(BASE_PATH_OBSERVABLE) basePath$: Observable) { if (basePath) { this.basePath = basePath; } @@ -41,6 +44,9 @@ export class StoreService { this.configuration = configuration; this.basePath = basePath || configuration.basePath || this.basePath; } + if (basePath$) { + basePath$.subscribe(nextBasePath => this.basePath = nextBasePath); + } } /** diff --git a/samples/client/petstore/typescript-angular-v2/default/api/user.service.ts b/samples/client/petstore/typescript-angular-v2/default/api/user.service.ts index 33bd9f1362a..19ac5ff4069 100644 --- a/samples/client/petstore/typescript-angular-v2/default/api/user.service.ts +++ b/samples/client/petstore/typescript-angular-v2/default/api/user.service.ts @@ -22,7 +22,7 @@ import '../rxjs-operators'; import { User } from '../model/user'; -import { BASE_PATH, COLLECTION_FORMATS } from '../variables'; +import { BASE_PATH, COLLECTION_FORMATS, BASE_PATH_OBSERVABLE } from '../variables'; import { Configuration } from '../configuration'; @@ -33,7 +33,10 @@ export class UserService { public defaultHeaders = new Headers(); public configuration = new Configuration(); - constructor(protected http: Http, @Optional()@Inject(BASE_PATH) basePath: string, @Optional() configuration: Configuration) { + constructor(protected http: Http, + @Optional() @Inject(BASE_PATH) basePath: string, + @Optional() configuration: Configuration, + @Optional() @Inject(BASE_PATH_OBSERVABLE) basePath$: Observable) { if (basePath) { this.basePath = basePath; } @@ -41,6 +44,9 @@ export class UserService { this.configuration = configuration; this.basePath = basePath || configuration.basePath || this.basePath; } + if (basePath$) { + basePath$.subscribe(nextBasePath => this.basePath = nextBasePath); + } } /** diff --git a/samples/client/petstore/typescript-angular-v2/default/variables.ts b/samples/client/petstore/typescript-angular-v2/default/variables.ts index 29b7e5b1d71..bd349d22a04 100644 --- a/samples/client/petstore/typescript-angular-v2/default/variables.ts +++ b/samples/client/petstore/typescript-angular-v2/default/variables.ts @@ -1,6 +1,9 @@ import { OpaqueToken } from '@angular/core'; +import { Observable } from 'rxjs/Observable'; + export const BASE_PATH = new OpaqueToken('basePath'); +export const BASE_PATH_OBSERVABLE = new OpaqueToken('basePathObservable'); export const COLLECTION_FORMATS = { 'csv': ',', 'tsv': ' ', diff --git a/samples/client/petstore/typescript-angular-v2/npm/README.md b/samples/client/petstore/typescript-angular-v2/npm/README.md index 1d02eea3ae8..57db079fc98 100644 --- a/samples/client/petstore/typescript-angular-v2/npm/README.md +++ b/samples/client/petstore/typescript-angular-v2/npm/README.md @@ -147,6 +147,53 @@ export class AppModule {} ``` +### Set dynamic service base path +If required the base path can be modified throughout the lifecycle of the +service by injecting an `Observable` using the `BASE_PATH_OBSERVABLE` token. + + +``` +import { BASE_PATH_OBSERVABLE } from '@swagger/angular2-typescript-petstore'; + +@NgModule({ + imports: [], + declarations: [ AppComponent ], + providers: [ + DynamicBasePathService, + { + provide: BASE_PATH_OBSERVABLE, + useFactory: () => new BehaviorSubject<string>('http://localhost:5000') + } + ], + bootstrap: [ AppComponent ] +}) +export class AppModule {} + +``` + +An example service defined below allows publishing new basePaths to the api services + +``` + +import {Inject, Injectable} from '@angular/core'; +import {Subject} from 'rxjs/Subject'; +import {BASE_PATH_OBSERVABLE} from '@swagger/angular2-typescript-petstore'; + +@Injectable() +export class DynamicBasePathService { + + constructor(@Inject(BASE_PATH_OBSERVABLE) private basePathSubject$: Subject<string>) { + } + + public setBasePath(basePath: string): void { + this.basePathSubject$.next(basePath); + } + +} + +``` + + #### Using @angular/cli First extend your `src/environments/*.ts` files by adding the corresponding base path: diff --git a/samples/client/petstore/typescript-angular-v2/npm/api/pet.service.ts b/samples/client/petstore/typescript-angular-v2/npm/api/pet.service.ts index 0f487e7b1a0..4f8d19784e8 100644 --- a/samples/client/petstore/typescript-angular-v2/npm/api/pet.service.ts +++ b/samples/client/petstore/typescript-angular-v2/npm/api/pet.service.ts @@ -23,7 +23,7 @@ import '../rxjs-operators'; import { ApiResponse } from '../model/apiResponse'; import { Pet } from '../model/pet'; -import { BASE_PATH, COLLECTION_FORMATS } from '../variables'; +import { BASE_PATH, COLLECTION_FORMATS, BASE_PATH_OBSERVABLE } from '../variables'; import { Configuration } from '../configuration'; @@ -34,7 +34,10 @@ export class PetService { public defaultHeaders = new Headers(); public configuration = new Configuration(); - constructor(protected http: Http, @Optional()@Inject(BASE_PATH) basePath: string, @Optional() configuration: Configuration) { + constructor(protected http: Http, + @Optional() @Inject(BASE_PATH) basePath: string, + @Optional() configuration: Configuration, + @Optional() @Inject(BASE_PATH_OBSERVABLE) basePath$: Observable) { if (basePath) { this.basePath = basePath; } @@ -42,6 +45,9 @@ export class PetService { this.configuration = configuration; this.basePath = basePath || configuration.basePath || this.basePath; } + if (basePath$) { + basePath$.subscribe(nextBasePath => this.basePath = nextBasePath); + } } /** diff --git a/samples/client/petstore/typescript-angular-v2/npm/api/store.service.ts b/samples/client/petstore/typescript-angular-v2/npm/api/store.service.ts index 443d2ba8480..13a18b7aeec 100644 --- a/samples/client/petstore/typescript-angular-v2/npm/api/store.service.ts +++ b/samples/client/petstore/typescript-angular-v2/npm/api/store.service.ts @@ -22,7 +22,7 @@ import '../rxjs-operators'; import { Order } from '../model/order'; -import { BASE_PATH, COLLECTION_FORMATS } from '../variables'; +import { BASE_PATH, COLLECTION_FORMATS, BASE_PATH_OBSERVABLE } from '../variables'; import { Configuration } from '../configuration'; @@ -33,7 +33,10 @@ export class StoreService { public defaultHeaders = new Headers(); public configuration = new Configuration(); - constructor(protected http: Http, @Optional()@Inject(BASE_PATH) basePath: string, @Optional() configuration: Configuration) { + constructor(protected http: Http, + @Optional() @Inject(BASE_PATH) basePath: string, + @Optional() configuration: Configuration, + @Optional() @Inject(BASE_PATH_OBSERVABLE) basePath$: Observable) { if (basePath) { this.basePath = basePath; } @@ -41,6 +44,9 @@ export class StoreService { this.configuration = configuration; this.basePath = basePath || configuration.basePath || this.basePath; } + if (basePath$) { + basePath$.subscribe(nextBasePath => this.basePath = nextBasePath); + } } /** diff --git a/samples/client/petstore/typescript-angular-v2/npm/api/user.service.ts b/samples/client/petstore/typescript-angular-v2/npm/api/user.service.ts index 33bd9f1362a..19ac5ff4069 100644 --- a/samples/client/petstore/typescript-angular-v2/npm/api/user.service.ts +++ b/samples/client/petstore/typescript-angular-v2/npm/api/user.service.ts @@ -22,7 +22,7 @@ import '../rxjs-operators'; import { User } from '../model/user'; -import { BASE_PATH, COLLECTION_FORMATS } from '../variables'; +import { BASE_PATH, COLLECTION_FORMATS, BASE_PATH_OBSERVABLE } from '../variables'; import { Configuration } from '../configuration'; @@ -33,7 +33,10 @@ export class UserService { public defaultHeaders = new Headers(); public configuration = new Configuration(); - constructor(protected http: Http, @Optional()@Inject(BASE_PATH) basePath: string, @Optional() configuration: Configuration) { + constructor(protected http: Http, + @Optional() @Inject(BASE_PATH) basePath: string, + @Optional() configuration: Configuration, + @Optional() @Inject(BASE_PATH_OBSERVABLE) basePath$: Observable) { if (basePath) { this.basePath = basePath; } @@ -41,6 +44,9 @@ export class UserService { this.configuration = configuration; this.basePath = basePath || configuration.basePath || this.basePath; } + if (basePath$) { + basePath$.subscribe(nextBasePath => this.basePath = nextBasePath); + } } /** diff --git a/samples/client/petstore/typescript-angular-v2/npm/variables.ts b/samples/client/petstore/typescript-angular-v2/npm/variables.ts index 29b7e5b1d71..bd349d22a04 100644 --- a/samples/client/petstore/typescript-angular-v2/npm/variables.ts +++ b/samples/client/petstore/typescript-angular-v2/npm/variables.ts @@ -1,6 +1,9 @@ import { OpaqueToken } from '@angular/core'; +import { Observable } from 'rxjs/Observable'; + export const BASE_PATH = new OpaqueToken('basePath'); +export const BASE_PATH_OBSERVABLE = new OpaqueToken('basePathObservable'); export const COLLECTION_FORMATS = { 'csv': ',', 'tsv': ' ', diff --git a/samples/client/petstore/typescript-angular-v2/with-interfaces/README.md b/samples/client/petstore/typescript-angular-v2/with-interfaces/README.md index 851ec448b23..57db079fc98 100644 --- a/samples/client/petstore/typescript-angular-v2/with-interfaces/README.md +++ b/samples/client/petstore/typescript-angular-v2/with-interfaces/README.md @@ -1,4 +1,4 @@ -## @ +## @swagger/angular2-typescript-petstore@0.0.1 ### Building @@ -19,7 +19,7 @@ Navigate to the folder of your consuming project and run one of next commands. _published:_ ``` -npm install @ --save +npm install @swagger/angular2-typescript-petstore@0.0.1 --save ``` _without publishing (not recommended):_ @@ -37,7 +37,7 @@ npm link In your project: ``` -npm link +npm link @swagger/angular2-typescript-petstore ``` __Note for Windows users:__ The Angular CLI has troubles to use linked npm packages. @@ -52,7 +52,7 @@ In your Angular project: ``` // without configuring providers -import { ApiModule } from ''; +import { ApiModule } from '@swagger/angular2-typescript-petstore'; import { HttpModule } from '@angular/http'; @@ -70,7 +70,7 @@ export class AppModule {} ``` // configuring providers -import { ApiModule, Configuration, ConfigurationParameters } from ''; +import { ApiModule, Configuration, ConfigurationParameters } from '@swagger/angular2-typescript-petstore'; export function apiConfigFactory (): Configuration => { const params: ConfigurationParameters = { @@ -89,7 +89,7 @@ export class AppModule {} ``` ``` -import { DefaultApi } from ''; +import { DefaultApi } from '@swagger/angular2-typescript-petstore'; export class AppComponent { constructor(private apiGateway: DefaultApi) { } @@ -126,7 +126,7 @@ export class AppModule { If different than the generated base path, during app bootstrap, you can provide the base path to your service. ``` -import { BASE_PATH } from ''; +import { BASE_PATH } from '@swagger/angular2-typescript-petstore'; bootstrap(AppComponent, [ { provide: BASE_PATH, useValue: 'https://your-web-service.com' }, @@ -135,7 +135,7 @@ bootstrap(AppComponent, [ or ``` -import { BASE_PATH } from ''; +import { BASE_PATH } from '@swagger/angular2-typescript-petstore'; @NgModule({ imports: [], @@ -147,6 +147,53 @@ export class AppModule {} ``` +### Set dynamic service base path +If required the base path can be modified throughout the lifecycle of the +service by injecting an `Observable` using the `BASE_PATH_OBSERVABLE` token. + + +``` +import { BASE_PATH_OBSERVABLE } from '@swagger/angular2-typescript-petstore'; + +@NgModule({ + imports: [], + declarations: [ AppComponent ], + providers: [ + DynamicBasePathService, + { + provide: BASE_PATH_OBSERVABLE, + useFactory: () => new BehaviorSubject<string>('http://localhost:5000') + } + ], + bootstrap: [ AppComponent ] +}) +export class AppModule {} + +``` + +An example service defined below allows publishing new basePaths to the api services + +``` + +import {Inject, Injectable} from '@angular/core'; +import {Subject} from 'rxjs/Subject'; +import {BASE_PATH_OBSERVABLE} from '@swagger/angular2-typescript-petstore'; + +@Injectable() +export class DynamicBasePathService { + + constructor(@Inject(BASE_PATH_OBSERVABLE) private basePathSubject$: Subject<string>) { + } + + public setBasePath(basePath: string): void { + this.basePathSubject$.next(basePath); + } + +} + +``` + + #### Using @angular/cli First extend your `src/environments/*.ts` files by adding the corresponding base path: @@ -159,7 +206,7 @@ export const environment = { In the src/app/app.module.ts: ``` -import { BASE_PATH } from ''; +import { BASE_PATH } from '@swagger/angular2-typescript-petstore'; import { environment } from '../environments/environment'; @NgModule({ diff --git a/samples/client/petstore/typescript-angular-v2/with-interfaces/api/pet.service.ts b/samples/client/petstore/typescript-angular-v2/with-interfaces/api/pet.service.ts index 4e93857c426..a691c381577 100644 --- a/samples/client/petstore/typescript-angular-v2/with-interfaces/api/pet.service.ts +++ b/samples/client/petstore/typescript-angular-v2/with-interfaces/api/pet.service.ts @@ -23,7 +23,7 @@ import '../rxjs-operators'; import { ApiResponse } from '../model/apiResponse'; import { Pet } from '../model/pet'; -import { BASE_PATH, COLLECTION_FORMATS } from '../variables'; +import { BASE_PATH, COLLECTION_FORMATS, BASE_PATH_OBSERVABLE } from '../variables'; import { Configuration } from '../configuration'; import { PetServiceInterface } from './pet.serviceInterface'; @@ -35,7 +35,10 @@ export class PetService implements PetServiceInterface { public defaultHeaders = new Headers(); public configuration = new Configuration(); - constructor(protected http: Http, @Optional()@Inject(BASE_PATH) basePath: string, @Optional() configuration: Configuration) { + constructor(protected http: Http, + @Optional() @Inject(BASE_PATH) basePath: string, + @Optional() configuration: Configuration, + @Optional() @Inject(BASE_PATH_OBSERVABLE) basePath$: Observable) { if (basePath) { this.basePath = basePath; } @@ -43,6 +46,9 @@ export class PetService implements PetServiceInterface { this.configuration = configuration; this.basePath = basePath || configuration.basePath || this.basePath; } + if (basePath$) { + basePath$.subscribe(nextBasePath => this.basePath = nextBasePath); + } } /** diff --git a/samples/client/petstore/typescript-angular-v2/with-interfaces/api/store.service.ts b/samples/client/petstore/typescript-angular-v2/with-interfaces/api/store.service.ts index 47e4cfd5e93..176f1fd1045 100644 --- a/samples/client/petstore/typescript-angular-v2/with-interfaces/api/store.service.ts +++ b/samples/client/petstore/typescript-angular-v2/with-interfaces/api/store.service.ts @@ -22,7 +22,7 @@ import '../rxjs-operators'; import { Order } from '../model/order'; -import { BASE_PATH, COLLECTION_FORMATS } from '../variables'; +import { BASE_PATH, COLLECTION_FORMATS, BASE_PATH_OBSERVABLE } from '../variables'; import { Configuration } from '../configuration'; import { StoreServiceInterface } from './store.serviceInterface'; @@ -34,7 +34,10 @@ export class StoreService implements StoreServiceInterface { public defaultHeaders = new Headers(); public configuration = new Configuration(); - constructor(protected http: Http, @Optional()@Inject(BASE_PATH) basePath: string, @Optional() configuration: Configuration) { + constructor(protected http: Http, + @Optional() @Inject(BASE_PATH) basePath: string, + @Optional() configuration: Configuration, + @Optional() @Inject(BASE_PATH_OBSERVABLE) basePath$: Observable) { if (basePath) { this.basePath = basePath; } @@ -42,6 +45,9 @@ export class StoreService implements StoreServiceInterface { this.configuration = configuration; this.basePath = basePath || configuration.basePath || this.basePath; } + if (basePath$) { + basePath$.subscribe(nextBasePath => this.basePath = nextBasePath); + } } /** diff --git a/samples/client/petstore/typescript-angular-v2/with-interfaces/api/user.service.ts b/samples/client/petstore/typescript-angular-v2/with-interfaces/api/user.service.ts index f664442825d..e35c4f718bc 100644 --- a/samples/client/petstore/typescript-angular-v2/with-interfaces/api/user.service.ts +++ b/samples/client/petstore/typescript-angular-v2/with-interfaces/api/user.service.ts @@ -22,7 +22,7 @@ import '../rxjs-operators'; import { User } from '../model/user'; -import { BASE_PATH, COLLECTION_FORMATS } from '../variables'; +import { BASE_PATH, COLLECTION_FORMATS, BASE_PATH_OBSERVABLE } from '../variables'; import { Configuration } from '../configuration'; import { UserServiceInterface } from './user.serviceInterface'; @@ -34,7 +34,10 @@ export class UserService implements UserServiceInterface { public defaultHeaders = new Headers(); public configuration = new Configuration(); - constructor(protected http: Http, @Optional()@Inject(BASE_PATH) basePath: string, @Optional() configuration: Configuration) { + constructor(protected http: Http, + @Optional() @Inject(BASE_PATH) basePath: string, + @Optional() configuration: Configuration, + @Optional() @Inject(BASE_PATH_OBSERVABLE) basePath$: Observable) { if (basePath) { this.basePath = basePath; } @@ -42,6 +45,9 @@ export class UserService implements UserServiceInterface { this.configuration = configuration; this.basePath = basePath || configuration.basePath || this.basePath; } + if (basePath$) { + basePath$.subscribe(nextBasePath => this.basePath = nextBasePath); + } } /** diff --git a/samples/client/petstore/typescript-angular-v2/with-interfaces/package.json b/samples/client/petstore/typescript-angular-v2/with-interfaces/package.json index 704da6b4cae..d73ea17840b 100644 --- a/samples/client/petstore/typescript-angular-v2/with-interfaces/package.json +++ b/samples/client/petstore/typescript-angular-v2/with-interfaces/package.json @@ -34,7 +34,7 @@ "reflect-metadata": "^0.1.3", "rxjs": "^5.4.0", "zone.js": "^0.7.6", - "typescript": "~2.1.5" + "typescript": ">=2.1.5 <2.8" }, "publishConfig": { "registry": "https://skimdb.npmjs.com/registry" diff --git a/samples/client/petstore/typescript-angular-v2/with-interfaces/variables.ts b/samples/client/petstore/typescript-angular-v2/with-interfaces/variables.ts index 29b7e5b1d71..bd349d22a04 100644 --- a/samples/client/petstore/typescript-angular-v2/with-interfaces/variables.ts +++ b/samples/client/petstore/typescript-angular-v2/with-interfaces/variables.ts @@ -1,6 +1,9 @@ import { OpaqueToken } from '@angular/core'; +import { Observable } from 'rxjs/Observable'; + export const BASE_PATH = new OpaqueToken('basePath'); +export const BASE_PATH_OBSERVABLE = new OpaqueToken('basePathObservable'); export const COLLECTION_FORMATS = { 'csv': ',', 'tsv': ' ', diff --git a/samples/client/petstore/typescript-angular-v4.3/npm/README.md b/samples/client/petstore/typescript-angular-v4.3/npm/README.md index b5a6ac2a8b8..1c867e21151 100644 --- a/samples/client/petstore/typescript-angular-v4.3/npm/README.md +++ b/samples/client/petstore/typescript-angular-v4.3/npm/README.md @@ -151,6 +151,53 @@ export class AppModule {} ``` +### Set dynamic service base path +If required the base path can be modified throughout the lifecycle of the +service by injecting an `Observable` using the `BASE_PATH_OBSERVABLE` token. + + +``` +import { BASE_PATH_OBSERVABLE } from '@swagger/angular2-typescript-petstore'; + +@NgModule({ + imports: [], + declarations: [ AppComponent ], + providers: [ + DynamicBasePathService, + { + provide: BASE_PATH_OBSERVABLE, + useFactory: () => new BehaviorSubject<string>('http://localhost:5000') + } + ], + bootstrap: [ AppComponent ] +}) +export class AppModule {} + +``` + +An example service defined below allows publishing new basePaths to the api services + +``` + +import {Inject, Injectable} from '@angular/core'; +import {Subject} from 'rxjs/Subject'; +import {BASE_PATH_OBSERVABLE} from '@swagger/angular2-typescript-petstore'; + +@Injectable() +export class DynamicBasePathService { + + constructor(@Inject(BASE_PATH_OBSERVABLE) private basePathSubject$: Subject<string>) { + } + + public setBasePath(basePath: string): void { + this.basePathSubject$.next(basePath); + } + +} + +``` + + #### Using @angular/cli First extend your `src/environments/*.ts` files by adding the corresponding base path: diff --git a/samples/client/petstore/typescript-angular-v4.3/npm/api/pet.service.ts b/samples/client/petstore/typescript-angular-v4.3/npm/api/pet.service.ts index 18563adb1b3..99017ffe27f 100644 --- a/samples/client/petstore/typescript-angular-v4.3/npm/api/pet.service.ts +++ b/samples/client/petstore/typescript-angular-v4.3/npm/api/pet.service.ts @@ -21,7 +21,7 @@ import { Observable } from 'rxjs/Observab import { ApiResponse } from '../model/apiResponse'; import { Pet } from '../model/pet'; -import { BASE_PATH, COLLECTION_FORMATS } from '../variables'; +import { BASE_PATH, COLLECTION_FORMATS, BASE_PATH_OBSERVABLE } from '../variables'; import { Configuration } from '../configuration'; @@ -32,7 +32,10 @@ export class PetService { public defaultHeaders = new HttpHeaders(); public configuration = new Configuration(); - constructor(protected httpClient: HttpClient, @Optional()@Inject(BASE_PATH) basePath: string, @Optional() configuration: Configuration) { + constructor(protected httpClient: HttpClient, + @Optional() @Inject(BASE_PATH) basePath: string, + @Optional() configuration: Configuration, + @Optional() @Inject(BASE_PATH_OBSERVABLE) basePath$: Observable) { if (basePath) { this.basePath = basePath; } @@ -40,6 +43,9 @@ export class PetService { this.configuration = configuration; this.basePath = basePath || configuration.basePath || this.basePath; } + if (basePath$) { + basePath$.subscribe(nextBasePath => this.basePath = nextBasePath); + } } /** diff --git a/samples/client/petstore/typescript-angular-v4.3/npm/api/store.service.ts b/samples/client/petstore/typescript-angular-v4.3/npm/api/store.service.ts index 03016b73c9d..3372180d555 100644 --- a/samples/client/petstore/typescript-angular-v4.3/npm/api/store.service.ts +++ b/samples/client/petstore/typescript-angular-v4.3/npm/api/store.service.ts @@ -20,7 +20,7 @@ import { Observable } from 'rxjs/Observab import { Order } from '../model/order'; -import { BASE_PATH, COLLECTION_FORMATS } from '../variables'; +import { BASE_PATH, COLLECTION_FORMATS, BASE_PATH_OBSERVABLE } from '../variables'; import { Configuration } from '../configuration'; @@ -31,7 +31,10 @@ export class StoreService { public defaultHeaders = new HttpHeaders(); public configuration = new Configuration(); - constructor(protected httpClient: HttpClient, @Optional()@Inject(BASE_PATH) basePath: string, @Optional() configuration: Configuration) { + constructor(protected httpClient: HttpClient, + @Optional() @Inject(BASE_PATH) basePath: string, + @Optional() configuration: Configuration, + @Optional() @Inject(BASE_PATH_OBSERVABLE) basePath$: Observable) { if (basePath) { this.basePath = basePath; } @@ -39,6 +42,9 @@ export class StoreService { this.configuration = configuration; this.basePath = basePath || configuration.basePath || this.basePath; } + if (basePath$) { + basePath$.subscribe(nextBasePath => this.basePath = nextBasePath); + } } /** diff --git a/samples/client/petstore/typescript-angular-v4.3/npm/api/user.service.ts b/samples/client/petstore/typescript-angular-v4.3/npm/api/user.service.ts index 7c7ccda2a79..9a63ef74fa2 100644 --- a/samples/client/petstore/typescript-angular-v4.3/npm/api/user.service.ts +++ b/samples/client/petstore/typescript-angular-v4.3/npm/api/user.service.ts @@ -20,7 +20,7 @@ import { Observable } from 'rxjs/Observab import { User } from '../model/user'; -import { BASE_PATH, COLLECTION_FORMATS } from '../variables'; +import { BASE_PATH, COLLECTION_FORMATS, BASE_PATH_OBSERVABLE } from '../variables'; import { Configuration } from '../configuration'; @@ -31,7 +31,10 @@ export class UserService { public defaultHeaders = new HttpHeaders(); public configuration = new Configuration(); - constructor(protected httpClient: HttpClient, @Optional()@Inject(BASE_PATH) basePath: string, @Optional() configuration: Configuration) { + constructor(protected httpClient: HttpClient, + @Optional() @Inject(BASE_PATH) basePath: string, + @Optional() configuration: Configuration, + @Optional() @Inject(BASE_PATH_OBSERVABLE) basePath$: Observable) { if (basePath) { this.basePath = basePath; } @@ -39,6 +42,9 @@ export class UserService { this.configuration = configuration; this.basePath = basePath || configuration.basePath || this.basePath; } + if (basePath$) { + basePath$.subscribe(nextBasePath => this.basePath = nextBasePath); + } } /** diff --git a/samples/client/petstore/typescript-angular-v4.3/npm/variables.ts b/samples/client/petstore/typescript-angular-v4.3/npm/variables.ts index 6fe58549f39..6adbee1db22 100644 --- a/samples/client/petstore/typescript-angular-v4.3/npm/variables.ts +++ b/samples/client/petstore/typescript-angular-v4.3/npm/variables.ts @@ -1,6 +1,9 @@ import { InjectionToken } from '@angular/core'; +import { Observable } from 'rxjs/Observable'; + export const BASE_PATH = new InjectionToken('basePath'); +export const BASE_PATH_OBSERVABLE = new InjectionToken>('basePathObservable'); export const COLLECTION_FORMATS = { 'csv': ',', 'tsv': ' ', diff --git a/samples/client/petstore/typescript-angular-v4.3/with-interfaces/README.md b/samples/client/petstore/typescript-angular-v4.3/with-interfaces/README.md index 9c7753a47b7..1c867e21151 100644 --- a/samples/client/petstore/typescript-angular-v4.3/with-interfaces/README.md +++ b/samples/client/petstore/typescript-angular-v4.3/with-interfaces/README.md @@ -1,4 +1,4 @@ -## @ +## @swagger/angular2-typescript-petstore@0.0.1 ### Building @@ -10,7 +10,7 @@ npm run build ### publishing -First build the package than run ```npm publish``` +First build the package than run ```npm publish dist``` (don't forget to specify the `dist` folder!) ### consuming @@ -19,25 +19,25 @@ Navigate to the folder of your consuming project and run one of next commands. _published:_ ``` -npm install @ --save +npm install @swagger/angular2-typescript-petstore@0.0.1 --save ``` _without publishing (not recommended):_ ``` -npm install PATH_TO_GENERATED_PACKAGE --save +npm install PATH_TO_GENERATED_PACKAGE/dist --save ``` _using `npm link`:_ -In PATH_TO_GENERATED_PACKAGE: +In PATH_TO_GENERATED_PACKAGE/dist: ``` npm link ``` In your project: ``` -npm link +npm link @swagger/angular2-typescript-petstore ``` __Note for Windows users:__ The Angular CLI has troubles to use linked npm packages. @@ -52,7 +52,7 @@ In your Angular project: ``` // without configuring providers -import { ApiModule } from ''; +import { ApiModule } from '@swagger/angular2-typescript-petstore'; import { HttpClientModule } from '@angular/common/http'; @@ -72,7 +72,7 @@ export class AppModule {} ``` // configuring providers -import { ApiModule, Configuration, ConfigurationParameters } from ''; +import { ApiModule, Configuration, ConfigurationParameters } from '@swagger/angular2-typescript-petstore'; export function apiConfigFactory (): Configuration => { const params: ConfigurationParameters = { @@ -91,7 +91,7 @@ export class AppModule {} ``` ``` -import { DefaultApi } from ''; +import { DefaultApi } from '@swagger/angular2-typescript-petstore'; export class AppComponent { constructor(private apiGateway: DefaultApi) { } @@ -130,7 +130,7 @@ export class AppModule { If different than the generated base path, during app bootstrap, you can provide the base path to your service. ``` -import { BASE_PATH } from ''; +import { BASE_PATH } from '@swagger/angular2-typescript-petstore'; bootstrap(AppComponent, [ { provide: BASE_PATH, useValue: 'https://your-web-service.com' }, @@ -139,7 +139,7 @@ bootstrap(AppComponent, [ or ``` -import { BASE_PATH } from ''; +import { BASE_PATH } from '@swagger/angular2-typescript-petstore'; @NgModule({ imports: [], @@ -151,6 +151,53 @@ export class AppModule {} ``` +### Set dynamic service base path +If required the base path can be modified throughout the lifecycle of the +service by injecting an `Observable` using the `BASE_PATH_OBSERVABLE` token. + + +``` +import { BASE_PATH_OBSERVABLE } from '@swagger/angular2-typescript-petstore'; + +@NgModule({ + imports: [], + declarations: [ AppComponent ], + providers: [ + DynamicBasePathService, + { + provide: BASE_PATH_OBSERVABLE, + useFactory: () => new BehaviorSubject<string>('http://localhost:5000') + } + ], + bootstrap: [ AppComponent ] +}) +export class AppModule {} + +``` + +An example service defined below allows publishing new basePaths to the api services + +``` + +import {Inject, Injectable} from '@angular/core'; +import {Subject} from 'rxjs/Subject'; +import {BASE_PATH_OBSERVABLE} from '@swagger/angular2-typescript-petstore'; + +@Injectable() +export class DynamicBasePathService { + + constructor(@Inject(BASE_PATH_OBSERVABLE) private basePathSubject$: Subject<string>) { + } + + public setBasePath(basePath: string): void { + this.basePathSubject$.next(basePath); + } + +} + +``` + + #### Using @angular/cli First extend your `src/environments/*.ts` files by adding the corresponding base path: @@ -163,7 +210,7 @@ export const environment = { In the src/app/app.module.ts: ``` -import { BASE_PATH } from ''; +import { BASE_PATH } from '@swagger/angular2-typescript-petstore'; import { environment } from '../environments/environment'; @NgModule({ diff --git a/samples/client/petstore/typescript-angular-v4.3/with-interfaces/api/pet.service.ts b/samples/client/petstore/typescript-angular-v4.3/with-interfaces/api/pet.service.ts index f7a5a17785d..4cee12634ed 100644 --- a/samples/client/petstore/typescript-angular-v4.3/with-interfaces/api/pet.service.ts +++ b/samples/client/petstore/typescript-angular-v4.3/with-interfaces/api/pet.service.ts @@ -21,7 +21,7 @@ import { Observable } from 'rxjs/Observab import { ApiResponse } from '../model/apiResponse'; import { Pet } from '../model/pet'; -import { BASE_PATH, COLLECTION_FORMATS } from '../variables'; +import { BASE_PATH, COLLECTION_FORMATS, BASE_PATH_OBSERVABLE } from '../variables'; import { Configuration } from '../configuration'; import { PetServiceInterface } from './pet.serviceInterface'; @@ -33,7 +33,10 @@ export class PetService implements PetServiceInterface { public defaultHeaders = new HttpHeaders(); public configuration = new Configuration(); - constructor(protected httpClient: HttpClient, @Optional()@Inject(BASE_PATH) basePath: string, @Optional() configuration: Configuration) { + constructor(protected httpClient: HttpClient, + @Optional() @Inject(BASE_PATH) basePath: string, + @Optional() configuration: Configuration, + @Optional() @Inject(BASE_PATH_OBSERVABLE) basePath$: Observable) { if (basePath) { this.basePath = basePath; } @@ -41,6 +44,9 @@ export class PetService implements PetServiceInterface { this.configuration = configuration; this.basePath = basePath || configuration.basePath || this.basePath; } + if (basePath$) { + basePath$.subscribe(nextBasePath => this.basePath = nextBasePath); + } } /** diff --git a/samples/client/petstore/typescript-angular-v4.3/with-interfaces/api/pet.serviceInterface.ts b/samples/client/petstore/typescript-angular-v4.3/with-interfaces/api/pet.serviceInterface.ts index 0278db6664e..d7dc9e5be5e 100644 --- a/samples/client/petstore/typescript-angular-v4.3/with-interfaces/api/pet.serviceInterface.ts +++ b/samples/client/petstore/typescript-angular-v4.3/with-interfaces/api/pet.serviceInterface.ts @@ -13,7 +13,6 @@ import { HttpHeaders } from '@angular/comm import { Observable } from 'rxjs/Observable'; - import { ApiResponse } from '../model/apiResponse'; import { Pet } from '../model/pet'; diff --git a/samples/client/petstore/typescript-angular-v4.3/with-interfaces/api/store.service.ts b/samples/client/petstore/typescript-angular-v4.3/with-interfaces/api/store.service.ts index 233e257c2fa..e4a0f55f9c6 100644 --- a/samples/client/petstore/typescript-angular-v4.3/with-interfaces/api/store.service.ts +++ b/samples/client/petstore/typescript-angular-v4.3/with-interfaces/api/store.service.ts @@ -20,7 +20,7 @@ import { Observable } from 'rxjs/Observab import { Order } from '../model/order'; -import { BASE_PATH, COLLECTION_FORMATS } from '../variables'; +import { BASE_PATH, COLLECTION_FORMATS, BASE_PATH_OBSERVABLE } from '../variables'; import { Configuration } from '../configuration'; import { StoreServiceInterface } from './store.serviceInterface'; @@ -32,7 +32,10 @@ export class StoreService implements StoreServiceInterface { public defaultHeaders = new HttpHeaders(); public configuration = new Configuration(); - constructor(protected httpClient: HttpClient, @Optional()@Inject(BASE_PATH) basePath: string, @Optional() configuration: Configuration) { + constructor(protected httpClient: HttpClient, + @Optional() @Inject(BASE_PATH) basePath: string, + @Optional() configuration: Configuration, + @Optional() @Inject(BASE_PATH_OBSERVABLE) basePath$: Observable) { if (basePath) { this.basePath = basePath; } @@ -40,6 +43,9 @@ export class StoreService implements StoreServiceInterface { this.configuration = configuration; this.basePath = basePath || configuration.basePath || this.basePath; } + if (basePath$) { + basePath$.subscribe(nextBasePath => this.basePath = nextBasePath); + } } /** diff --git a/samples/client/petstore/typescript-angular-v4.3/with-interfaces/api/store.serviceInterface.ts b/samples/client/petstore/typescript-angular-v4.3/with-interfaces/api/store.serviceInterface.ts index 5d3227ea075..38614ae7be2 100644 --- a/samples/client/petstore/typescript-angular-v4.3/with-interfaces/api/store.serviceInterface.ts +++ b/samples/client/petstore/typescript-angular-v4.3/with-interfaces/api/store.serviceInterface.ts @@ -13,7 +13,6 @@ import { HttpHeaders } from '@angular/comm import { Observable } from 'rxjs/Observable'; - import { Order } from '../model/order'; diff --git a/samples/client/petstore/typescript-angular-v4.3/with-interfaces/api/user.service.ts b/samples/client/petstore/typescript-angular-v4.3/with-interfaces/api/user.service.ts index b6e09b12657..9d58e316eaa 100644 --- a/samples/client/petstore/typescript-angular-v4.3/with-interfaces/api/user.service.ts +++ b/samples/client/petstore/typescript-angular-v4.3/with-interfaces/api/user.service.ts @@ -20,7 +20,7 @@ import { Observable } from 'rxjs/Observab import { User } from '../model/user'; -import { BASE_PATH, COLLECTION_FORMATS } from '../variables'; +import { BASE_PATH, COLLECTION_FORMATS, BASE_PATH_OBSERVABLE } from '../variables'; import { Configuration } from '../configuration'; import { UserServiceInterface } from './user.serviceInterface'; @@ -32,7 +32,10 @@ export class UserService implements UserServiceInterface { public defaultHeaders = new HttpHeaders(); public configuration = new Configuration(); - constructor(protected httpClient: HttpClient, @Optional()@Inject(BASE_PATH) basePath: string, @Optional() configuration: Configuration) { + constructor(protected httpClient: HttpClient, + @Optional() @Inject(BASE_PATH) basePath: string, + @Optional() configuration: Configuration, + @Optional() @Inject(BASE_PATH_OBSERVABLE) basePath$: Observable) { if (basePath) { this.basePath = basePath; } @@ -40,6 +43,9 @@ export class UserService implements UserServiceInterface { this.configuration = configuration; this.basePath = basePath || configuration.basePath || this.basePath; } + if (basePath$) { + basePath$.subscribe(nextBasePath => this.basePath = nextBasePath); + } } /** diff --git a/samples/client/petstore/typescript-angular-v4.3/with-interfaces/api/user.serviceInterface.ts b/samples/client/petstore/typescript-angular-v4.3/with-interfaces/api/user.serviceInterface.ts index 7dc6496c2fa..b298d40ca4f 100644 --- a/samples/client/petstore/typescript-angular-v4.3/with-interfaces/api/user.serviceInterface.ts +++ b/samples/client/petstore/typescript-angular-v4.3/with-interfaces/api/user.serviceInterface.ts @@ -13,7 +13,6 @@ import { HttpHeaders } from '@angular/comm import { Observable } from 'rxjs/Observable'; - import { User } from '../model/user'; diff --git a/samples/client/petstore/typescript-angular-v4.3/with-interfaces/ng-package.json b/samples/client/petstore/typescript-angular-v4.3/with-interfaces/ng-package.json new file mode 100644 index 00000000000..3b17900dc9c --- /dev/null +++ b/samples/client/petstore/typescript-angular-v4.3/with-interfaces/ng-package.json @@ -0,0 +1,6 @@ +{ + "$schema": "./node_modules/ng-packagr/ng-package.schema.json", + "lib": { + "entryFile": "index.ts" + } +} diff --git a/samples/client/petstore/typescript-angular-v4.3/with-interfaces/package.json b/samples/client/petstore/typescript-angular-v4.3/with-interfaces/package.json new file mode 100644 index 00000000000..3b689b9df96 --- /dev/null +++ b/samples/client/petstore/typescript-angular-v4.3/with-interfaces/package.json @@ -0,0 +1,39 @@ +{ + "name": "@swagger/angular2-typescript-petstore", + "version": "0.0.1", + "description": "swagger client for @swagger/angular2-typescript-petstore", + "author": "Swagger Codegen Contributors", + "keywords": [ + "swagger-client" + ], + "license": "Unlicense", + "scripts": { + "build": "ng-packagr -p ng-package.json" + }, + "peerDependencies": { + "@angular/core": "^4.3.0", + "@angular/http": "^4.3.0", + "@angular/common": "^4.3.0", + "@angular/compiler": "^4.3.0", + "core-js": "^2.4.0", + "reflect-metadata": "^0.1.3", + "rxjs": "^5.4.0", + "zone.js": "^0.7.6" + }, + "devDependencies": { + "@angular/compiler-cli": "^4.3.0", + "@angular/core": "^4.3.0", + "@angular/http": "^4.3.0", + "@angular/common": "^4.3.0", + "@angular/compiler": "^4.3.0", + "@angular/platform-browser": "^4.3.0", + "ng-packagr": "^1.6.0", + "reflect-metadata": "^0.1.3", + "rxjs": "^5.4.0", + "zone.js": "^0.7.6", + "typescript": ">=2.1.5 <2.8" + }, + "publishConfig": { + "registry": "https://skimdb.npmjs.com/registry" + } +} diff --git a/samples/client/petstore/typescript-angular-v4.3/with-interfaces/tsconfig.json b/samples/client/petstore/typescript-angular-v4.3/with-interfaces/tsconfig.json new file mode 100644 index 00000000000..498ee01e9dd --- /dev/null +++ b/samples/client/petstore/typescript-angular-v4.3/with-interfaces/tsconfig.json @@ -0,0 +1,25 @@ +{ + "compilerOptions": { + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + "noImplicitAny": false, + "suppressImplicitAnyIndexErrors": true, + "target": "es5", + "module": "commonjs", + "moduleResolution": "node", + "removeComments": true, + "sourceMap": true, + "outDir": "./dist", + "noLib": false, + "declaration": true, + "lib": [ "es6", "dom" ] + }, + "exclude": [ + "node_modules", + "dist" + ], + "filesGlob": [ + "./model/*.ts", + "./api/*.ts" + ] +} diff --git a/samples/client/petstore/typescript-angular-v4.3/with-interfaces/typings.json b/samples/client/petstore/typescript-angular-v4.3/with-interfaces/typings.json new file mode 100644 index 00000000000..507c40e5cbe --- /dev/null +++ b/samples/client/petstore/typescript-angular-v4.3/with-interfaces/typings.json @@ -0,0 +1,5 @@ +{ + "globalDependencies": { + "core-js": "registry:dt/core-js#0.0.0+20160725163759" + } +} diff --git a/samples/client/petstore/typescript-angular-v4.3/with-interfaces/variables.ts b/samples/client/petstore/typescript-angular-v4.3/with-interfaces/variables.ts index 6fe58549f39..6adbee1db22 100644 --- a/samples/client/petstore/typescript-angular-v4.3/with-interfaces/variables.ts +++ b/samples/client/petstore/typescript-angular-v4.3/with-interfaces/variables.ts @@ -1,6 +1,9 @@ import { InjectionToken } from '@angular/core'; +import { Observable } from 'rxjs/Observable'; + export const BASE_PATH = new InjectionToken('basePath'); +export const BASE_PATH_OBSERVABLE = new InjectionToken>('basePathObservable'); export const COLLECTION_FORMATS = { 'csv': ',', 'tsv': ' ', diff --git a/samples/client/petstore/typescript-angular-v4/npm/README.md b/samples/client/petstore/typescript-angular-v4/npm/README.md index 73c64dbafca..2d5b18615fc 100644 --- a/samples/client/petstore/typescript-angular-v4/npm/README.md +++ b/samples/client/petstore/typescript-angular-v4/npm/README.md @@ -147,6 +147,53 @@ export class AppModule {} ``` +### Set dynamic service base path +If required the base path can be modified throughout the lifecycle of the +service by injecting an `Observable` using the `BASE_PATH_OBSERVABLE` token. + + +``` +import { BASE_PATH_OBSERVABLE } from '@swagger/angular2-typescript-petstore'; + +@NgModule({ + imports: [], + declarations: [ AppComponent ], + providers: [ + DynamicBasePathService, + { + provide: BASE_PATH_OBSERVABLE, + useFactory: () => new BehaviorSubject<string>('http://localhost:5000') + } + ], + bootstrap: [ AppComponent ] +}) +export class AppModule {} + +``` + +An example service defined below allows publishing new basePaths to the api services + +``` + +import {Inject, Injectable} from '@angular/core'; +import {Subject} from 'rxjs/Subject'; +import {BASE_PATH_OBSERVABLE} from '@swagger/angular2-typescript-petstore'; + +@Injectable() +export class DynamicBasePathService { + + constructor(@Inject(BASE_PATH_OBSERVABLE) private basePathSubject$: Subject<string>) { + } + + public setBasePath(basePath: string): void { + this.basePathSubject$.next(basePath); + } + +} + +``` + + #### Using @angular/cli First extend your `src/environments/*.ts` files by adding the corresponding base path: diff --git a/samples/client/petstore/typescript-angular-v4/npm/api/pet.service.ts b/samples/client/petstore/typescript-angular-v4/npm/api/pet.service.ts index 0f487e7b1a0..4f8d19784e8 100644 --- a/samples/client/petstore/typescript-angular-v4/npm/api/pet.service.ts +++ b/samples/client/petstore/typescript-angular-v4/npm/api/pet.service.ts @@ -23,7 +23,7 @@ import '../rxjs-operators'; import { ApiResponse } from '../model/apiResponse'; import { Pet } from '../model/pet'; -import { BASE_PATH, COLLECTION_FORMATS } from '../variables'; +import { BASE_PATH, COLLECTION_FORMATS, BASE_PATH_OBSERVABLE } from '../variables'; import { Configuration } from '../configuration'; @@ -34,7 +34,10 @@ export class PetService { public defaultHeaders = new Headers(); public configuration = new Configuration(); - constructor(protected http: Http, @Optional()@Inject(BASE_PATH) basePath: string, @Optional() configuration: Configuration) { + constructor(protected http: Http, + @Optional() @Inject(BASE_PATH) basePath: string, + @Optional() configuration: Configuration, + @Optional() @Inject(BASE_PATH_OBSERVABLE) basePath$: Observable) { if (basePath) { this.basePath = basePath; } @@ -42,6 +45,9 @@ export class PetService { this.configuration = configuration; this.basePath = basePath || configuration.basePath || this.basePath; } + if (basePath$) { + basePath$.subscribe(nextBasePath => this.basePath = nextBasePath); + } } /** diff --git a/samples/client/petstore/typescript-angular-v4/npm/api/store.service.ts b/samples/client/petstore/typescript-angular-v4/npm/api/store.service.ts index 443d2ba8480..13a18b7aeec 100644 --- a/samples/client/petstore/typescript-angular-v4/npm/api/store.service.ts +++ b/samples/client/petstore/typescript-angular-v4/npm/api/store.service.ts @@ -22,7 +22,7 @@ import '../rxjs-operators'; import { Order } from '../model/order'; -import { BASE_PATH, COLLECTION_FORMATS } from '../variables'; +import { BASE_PATH, COLLECTION_FORMATS, BASE_PATH_OBSERVABLE } from '../variables'; import { Configuration } from '../configuration'; @@ -33,7 +33,10 @@ export class StoreService { public defaultHeaders = new Headers(); public configuration = new Configuration(); - constructor(protected http: Http, @Optional()@Inject(BASE_PATH) basePath: string, @Optional() configuration: Configuration) { + constructor(protected http: Http, + @Optional() @Inject(BASE_PATH) basePath: string, + @Optional() configuration: Configuration, + @Optional() @Inject(BASE_PATH_OBSERVABLE) basePath$: Observable) { if (basePath) { this.basePath = basePath; } @@ -41,6 +44,9 @@ export class StoreService { this.configuration = configuration; this.basePath = basePath || configuration.basePath || this.basePath; } + if (basePath$) { + basePath$.subscribe(nextBasePath => this.basePath = nextBasePath); + } } /** diff --git a/samples/client/petstore/typescript-angular-v4/npm/api/user.service.ts b/samples/client/petstore/typescript-angular-v4/npm/api/user.service.ts index 33bd9f1362a..19ac5ff4069 100644 --- a/samples/client/petstore/typescript-angular-v4/npm/api/user.service.ts +++ b/samples/client/petstore/typescript-angular-v4/npm/api/user.service.ts @@ -22,7 +22,7 @@ import '../rxjs-operators'; import { User } from '../model/user'; -import { BASE_PATH, COLLECTION_FORMATS } from '../variables'; +import { BASE_PATH, COLLECTION_FORMATS, BASE_PATH_OBSERVABLE } from '../variables'; import { Configuration } from '../configuration'; @@ -33,7 +33,10 @@ export class UserService { public defaultHeaders = new Headers(); public configuration = new Configuration(); - constructor(protected http: Http, @Optional()@Inject(BASE_PATH) basePath: string, @Optional() configuration: Configuration) { + constructor(protected http: Http, + @Optional() @Inject(BASE_PATH) basePath: string, + @Optional() configuration: Configuration, + @Optional() @Inject(BASE_PATH_OBSERVABLE) basePath$: Observable) { if (basePath) { this.basePath = basePath; } @@ -41,6 +44,9 @@ export class UserService { this.configuration = configuration; this.basePath = basePath || configuration.basePath || this.basePath; } + if (basePath$) { + basePath$.subscribe(nextBasePath => this.basePath = nextBasePath); + } } /** diff --git a/samples/client/petstore/typescript-angular-v4/npm/variables.ts b/samples/client/petstore/typescript-angular-v4/npm/variables.ts index 6fe58549f39..6adbee1db22 100644 --- a/samples/client/petstore/typescript-angular-v4/npm/variables.ts +++ b/samples/client/petstore/typescript-angular-v4/npm/variables.ts @@ -1,6 +1,9 @@ import { InjectionToken } from '@angular/core'; +import { Observable } from 'rxjs/Observable'; + export const BASE_PATH = new InjectionToken('basePath'); +export const BASE_PATH_OBSERVABLE = new InjectionToken>('basePathObservable'); export const COLLECTION_FORMATS = { 'csv': ',', 'tsv': ' ', diff --git a/samples/client/petstore/typescript-angular-v5/npm/README.md b/samples/client/petstore/typescript-angular-v5/npm/README.md index b5a6ac2a8b8..1c867e21151 100644 --- a/samples/client/petstore/typescript-angular-v5/npm/README.md +++ b/samples/client/petstore/typescript-angular-v5/npm/README.md @@ -151,6 +151,53 @@ export class AppModule {} ``` +### Set dynamic service base path +If required the base path can be modified throughout the lifecycle of the +service by injecting an `Observable` using the `BASE_PATH_OBSERVABLE` token. + + +``` +import { BASE_PATH_OBSERVABLE } from '@swagger/angular2-typescript-petstore'; + +@NgModule({ + imports: [], + declarations: [ AppComponent ], + providers: [ + DynamicBasePathService, + { + provide: BASE_PATH_OBSERVABLE, + useFactory: () => new BehaviorSubject<string>('http://localhost:5000') + } + ], + bootstrap: [ AppComponent ] +}) +export class AppModule {} + +``` + +An example service defined below allows publishing new basePaths to the api services + +``` + +import {Inject, Injectable} from '@angular/core'; +import {Subject} from 'rxjs/Subject'; +import {BASE_PATH_OBSERVABLE} from '@swagger/angular2-typescript-petstore'; + +@Injectable() +export class DynamicBasePathService { + + constructor(@Inject(BASE_PATH_OBSERVABLE) private basePathSubject$: Subject<string>) { + } + + public setBasePath(basePath: string): void { + this.basePathSubject$.next(basePath); + } + +} + +``` + + #### Using @angular/cli First extend your `src/environments/*.ts` files by adding the corresponding base path: diff --git a/samples/client/petstore/typescript-angular-v5/npm/api/pet.service.ts b/samples/client/petstore/typescript-angular-v5/npm/api/pet.service.ts index 18563adb1b3..99017ffe27f 100644 --- a/samples/client/petstore/typescript-angular-v5/npm/api/pet.service.ts +++ b/samples/client/petstore/typescript-angular-v5/npm/api/pet.service.ts @@ -21,7 +21,7 @@ import { Observable } from 'rxjs/Observab import { ApiResponse } from '../model/apiResponse'; import { Pet } from '../model/pet'; -import { BASE_PATH, COLLECTION_FORMATS } from '../variables'; +import { BASE_PATH, COLLECTION_FORMATS, BASE_PATH_OBSERVABLE } from '../variables'; import { Configuration } from '../configuration'; @@ -32,7 +32,10 @@ export class PetService { public defaultHeaders = new HttpHeaders(); public configuration = new Configuration(); - constructor(protected httpClient: HttpClient, @Optional()@Inject(BASE_PATH) basePath: string, @Optional() configuration: Configuration) { + constructor(protected httpClient: HttpClient, + @Optional() @Inject(BASE_PATH) basePath: string, + @Optional() configuration: Configuration, + @Optional() @Inject(BASE_PATH_OBSERVABLE) basePath$: Observable) { if (basePath) { this.basePath = basePath; } @@ -40,6 +43,9 @@ export class PetService { this.configuration = configuration; this.basePath = basePath || configuration.basePath || this.basePath; } + if (basePath$) { + basePath$.subscribe(nextBasePath => this.basePath = nextBasePath); + } } /** diff --git a/samples/client/petstore/typescript-angular-v5/npm/api/store.service.ts b/samples/client/petstore/typescript-angular-v5/npm/api/store.service.ts index 03016b73c9d..3372180d555 100644 --- a/samples/client/petstore/typescript-angular-v5/npm/api/store.service.ts +++ b/samples/client/petstore/typescript-angular-v5/npm/api/store.service.ts @@ -20,7 +20,7 @@ import { Observable } from 'rxjs/Observab import { Order } from '../model/order'; -import { BASE_PATH, COLLECTION_FORMATS } from '../variables'; +import { BASE_PATH, COLLECTION_FORMATS, BASE_PATH_OBSERVABLE } from '../variables'; import { Configuration } from '../configuration'; @@ -31,7 +31,10 @@ export class StoreService { public defaultHeaders = new HttpHeaders(); public configuration = new Configuration(); - constructor(protected httpClient: HttpClient, @Optional()@Inject(BASE_PATH) basePath: string, @Optional() configuration: Configuration) { + constructor(protected httpClient: HttpClient, + @Optional() @Inject(BASE_PATH) basePath: string, + @Optional() configuration: Configuration, + @Optional() @Inject(BASE_PATH_OBSERVABLE) basePath$: Observable) { if (basePath) { this.basePath = basePath; } @@ -39,6 +42,9 @@ export class StoreService { this.configuration = configuration; this.basePath = basePath || configuration.basePath || this.basePath; } + if (basePath$) { + basePath$.subscribe(nextBasePath => this.basePath = nextBasePath); + } } /** diff --git a/samples/client/petstore/typescript-angular-v5/npm/api/user.service.ts b/samples/client/petstore/typescript-angular-v5/npm/api/user.service.ts index 7c7ccda2a79..9a63ef74fa2 100644 --- a/samples/client/petstore/typescript-angular-v5/npm/api/user.service.ts +++ b/samples/client/petstore/typescript-angular-v5/npm/api/user.service.ts @@ -20,7 +20,7 @@ import { Observable } from 'rxjs/Observab import { User } from '../model/user'; -import { BASE_PATH, COLLECTION_FORMATS } from '../variables'; +import { BASE_PATH, COLLECTION_FORMATS, BASE_PATH_OBSERVABLE } from '../variables'; import { Configuration } from '../configuration'; @@ -31,7 +31,10 @@ export class UserService { public defaultHeaders = new HttpHeaders(); public configuration = new Configuration(); - constructor(protected httpClient: HttpClient, @Optional()@Inject(BASE_PATH) basePath: string, @Optional() configuration: Configuration) { + constructor(protected httpClient: HttpClient, + @Optional() @Inject(BASE_PATH) basePath: string, + @Optional() configuration: Configuration, + @Optional() @Inject(BASE_PATH_OBSERVABLE) basePath$: Observable) { if (basePath) { this.basePath = basePath; } @@ -39,6 +42,9 @@ export class UserService { this.configuration = configuration; this.basePath = basePath || configuration.basePath || this.basePath; } + if (basePath$) { + basePath$.subscribe(nextBasePath => this.basePath = nextBasePath); + } } /** diff --git a/samples/client/petstore/typescript-angular-v5/npm/variables.ts b/samples/client/petstore/typescript-angular-v5/npm/variables.ts index 6fe58549f39..6adbee1db22 100644 --- a/samples/client/petstore/typescript-angular-v5/npm/variables.ts +++ b/samples/client/petstore/typescript-angular-v5/npm/variables.ts @@ -1,6 +1,9 @@ import { InjectionToken } from '@angular/core'; +import { Observable } from 'rxjs/Observable'; + export const BASE_PATH = new InjectionToken('basePath'); +export const BASE_PATH_OBSERVABLE = new InjectionToken>('basePathObservable'); export const COLLECTION_FORMATS = { 'csv': ',', 'tsv': ' ', diff --git a/samples/client/petstore/typescript-angular-v5/with-interfaces/README.md b/samples/client/petstore/typescript-angular-v5/with-interfaces/README.md index 9c7753a47b7..1c867e21151 100644 --- a/samples/client/petstore/typescript-angular-v5/with-interfaces/README.md +++ b/samples/client/petstore/typescript-angular-v5/with-interfaces/README.md @@ -1,4 +1,4 @@ -## @ +## @swagger/angular2-typescript-petstore@0.0.1 ### Building @@ -10,7 +10,7 @@ npm run build ### publishing -First build the package than run ```npm publish``` +First build the package than run ```npm publish dist``` (don't forget to specify the `dist` folder!) ### consuming @@ -19,25 +19,25 @@ Navigate to the folder of your consuming project and run one of next commands. _published:_ ``` -npm install @ --save +npm install @swagger/angular2-typescript-petstore@0.0.1 --save ``` _without publishing (not recommended):_ ``` -npm install PATH_TO_GENERATED_PACKAGE --save +npm install PATH_TO_GENERATED_PACKAGE/dist --save ``` _using `npm link`:_ -In PATH_TO_GENERATED_PACKAGE: +In PATH_TO_GENERATED_PACKAGE/dist: ``` npm link ``` In your project: ``` -npm link +npm link @swagger/angular2-typescript-petstore ``` __Note for Windows users:__ The Angular CLI has troubles to use linked npm packages. @@ -52,7 +52,7 @@ In your Angular project: ``` // without configuring providers -import { ApiModule } from ''; +import { ApiModule } from '@swagger/angular2-typescript-petstore'; import { HttpClientModule } from '@angular/common/http'; @@ -72,7 +72,7 @@ export class AppModule {} ``` // configuring providers -import { ApiModule, Configuration, ConfigurationParameters } from ''; +import { ApiModule, Configuration, ConfigurationParameters } from '@swagger/angular2-typescript-petstore'; export function apiConfigFactory (): Configuration => { const params: ConfigurationParameters = { @@ -91,7 +91,7 @@ export class AppModule {} ``` ``` -import { DefaultApi } from ''; +import { DefaultApi } from '@swagger/angular2-typescript-petstore'; export class AppComponent { constructor(private apiGateway: DefaultApi) { } @@ -130,7 +130,7 @@ export class AppModule { If different than the generated base path, during app bootstrap, you can provide the base path to your service. ``` -import { BASE_PATH } from ''; +import { BASE_PATH } from '@swagger/angular2-typescript-petstore'; bootstrap(AppComponent, [ { provide: BASE_PATH, useValue: 'https://your-web-service.com' }, @@ -139,7 +139,7 @@ bootstrap(AppComponent, [ or ``` -import { BASE_PATH } from ''; +import { BASE_PATH } from '@swagger/angular2-typescript-petstore'; @NgModule({ imports: [], @@ -151,6 +151,53 @@ export class AppModule {} ``` +### Set dynamic service base path +If required the base path can be modified throughout the lifecycle of the +service by injecting an `Observable` using the `BASE_PATH_OBSERVABLE` token. + + +``` +import { BASE_PATH_OBSERVABLE } from '@swagger/angular2-typescript-petstore'; + +@NgModule({ + imports: [], + declarations: [ AppComponent ], + providers: [ + DynamicBasePathService, + { + provide: BASE_PATH_OBSERVABLE, + useFactory: () => new BehaviorSubject<string>('http://localhost:5000') + } + ], + bootstrap: [ AppComponent ] +}) +export class AppModule {} + +``` + +An example service defined below allows publishing new basePaths to the api services + +``` + +import {Inject, Injectable} from '@angular/core'; +import {Subject} from 'rxjs/Subject'; +import {BASE_PATH_OBSERVABLE} from '@swagger/angular2-typescript-petstore'; + +@Injectable() +export class DynamicBasePathService { + + constructor(@Inject(BASE_PATH_OBSERVABLE) private basePathSubject$: Subject<string>) { + } + + public setBasePath(basePath: string): void { + this.basePathSubject$.next(basePath); + } + +} + +``` + + #### Using @angular/cli First extend your `src/environments/*.ts` files by adding the corresponding base path: @@ -163,7 +210,7 @@ export const environment = { In the src/app/app.module.ts: ``` -import { BASE_PATH } from ''; +import { BASE_PATH } from '@swagger/angular2-typescript-petstore'; import { environment } from '../environments/environment'; @NgModule({ diff --git a/samples/client/petstore/typescript-angular-v5/with-interfaces/api/pet.service.ts b/samples/client/petstore/typescript-angular-v5/with-interfaces/api/pet.service.ts index f7a5a17785d..4cee12634ed 100644 --- a/samples/client/petstore/typescript-angular-v5/with-interfaces/api/pet.service.ts +++ b/samples/client/petstore/typescript-angular-v5/with-interfaces/api/pet.service.ts @@ -21,7 +21,7 @@ import { Observable } from 'rxjs/Observab import { ApiResponse } from '../model/apiResponse'; import { Pet } from '../model/pet'; -import { BASE_PATH, COLLECTION_FORMATS } from '../variables'; +import { BASE_PATH, COLLECTION_FORMATS, BASE_PATH_OBSERVABLE } from '../variables'; import { Configuration } from '../configuration'; import { PetServiceInterface } from './pet.serviceInterface'; @@ -33,7 +33,10 @@ export class PetService implements PetServiceInterface { public defaultHeaders = new HttpHeaders(); public configuration = new Configuration(); - constructor(protected httpClient: HttpClient, @Optional()@Inject(BASE_PATH) basePath: string, @Optional() configuration: Configuration) { + constructor(protected httpClient: HttpClient, + @Optional() @Inject(BASE_PATH) basePath: string, + @Optional() configuration: Configuration, + @Optional() @Inject(BASE_PATH_OBSERVABLE) basePath$: Observable) { if (basePath) { this.basePath = basePath; } @@ -41,6 +44,9 @@ export class PetService implements PetServiceInterface { this.configuration = configuration; this.basePath = basePath || configuration.basePath || this.basePath; } + if (basePath$) { + basePath$.subscribe(nextBasePath => this.basePath = nextBasePath); + } } /** diff --git a/samples/client/petstore/typescript-angular-v5/with-interfaces/api/pet.serviceInterface.ts b/samples/client/petstore/typescript-angular-v5/with-interfaces/api/pet.serviceInterface.ts index 0278db6664e..d7dc9e5be5e 100644 --- a/samples/client/petstore/typescript-angular-v5/with-interfaces/api/pet.serviceInterface.ts +++ b/samples/client/petstore/typescript-angular-v5/with-interfaces/api/pet.serviceInterface.ts @@ -13,7 +13,6 @@ import { HttpHeaders } from '@angular/comm import { Observable } from 'rxjs/Observable'; - import { ApiResponse } from '../model/apiResponse'; import { Pet } from '../model/pet'; diff --git a/samples/client/petstore/typescript-angular-v5/with-interfaces/api/store.service.ts b/samples/client/petstore/typescript-angular-v5/with-interfaces/api/store.service.ts index 233e257c2fa..e4a0f55f9c6 100644 --- a/samples/client/petstore/typescript-angular-v5/with-interfaces/api/store.service.ts +++ b/samples/client/petstore/typescript-angular-v5/with-interfaces/api/store.service.ts @@ -20,7 +20,7 @@ import { Observable } from 'rxjs/Observab import { Order } from '../model/order'; -import { BASE_PATH, COLLECTION_FORMATS } from '../variables'; +import { BASE_PATH, COLLECTION_FORMATS, BASE_PATH_OBSERVABLE } from '../variables'; import { Configuration } from '../configuration'; import { StoreServiceInterface } from './store.serviceInterface'; @@ -32,7 +32,10 @@ export class StoreService implements StoreServiceInterface { public defaultHeaders = new HttpHeaders(); public configuration = new Configuration(); - constructor(protected httpClient: HttpClient, @Optional()@Inject(BASE_PATH) basePath: string, @Optional() configuration: Configuration) { + constructor(protected httpClient: HttpClient, + @Optional() @Inject(BASE_PATH) basePath: string, + @Optional() configuration: Configuration, + @Optional() @Inject(BASE_PATH_OBSERVABLE) basePath$: Observable) { if (basePath) { this.basePath = basePath; } @@ -40,6 +43,9 @@ export class StoreService implements StoreServiceInterface { this.configuration = configuration; this.basePath = basePath || configuration.basePath || this.basePath; } + if (basePath$) { + basePath$.subscribe(nextBasePath => this.basePath = nextBasePath); + } } /** diff --git a/samples/client/petstore/typescript-angular-v5/with-interfaces/api/store.serviceInterface.ts b/samples/client/petstore/typescript-angular-v5/with-interfaces/api/store.serviceInterface.ts index 5d3227ea075..38614ae7be2 100644 --- a/samples/client/petstore/typescript-angular-v5/with-interfaces/api/store.serviceInterface.ts +++ b/samples/client/petstore/typescript-angular-v5/with-interfaces/api/store.serviceInterface.ts @@ -13,7 +13,6 @@ import { HttpHeaders } from '@angular/comm import { Observable } from 'rxjs/Observable'; - import { Order } from '../model/order'; diff --git a/samples/client/petstore/typescript-angular-v5/with-interfaces/api/user.service.ts b/samples/client/petstore/typescript-angular-v5/with-interfaces/api/user.service.ts index b6e09b12657..9d58e316eaa 100644 --- a/samples/client/petstore/typescript-angular-v5/with-interfaces/api/user.service.ts +++ b/samples/client/petstore/typescript-angular-v5/with-interfaces/api/user.service.ts @@ -20,7 +20,7 @@ import { Observable } from 'rxjs/Observab import { User } from '../model/user'; -import { BASE_PATH, COLLECTION_FORMATS } from '../variables'; +import { BASE_PATH, COLLECTION_FORMATS, BASE_PATH_OBSERVABLE } from '../variables'; import { Configuration } from '../configuration'; import { UserServiceInterface } from './user.serviceInterface'; @@ -32,7 +32,10 @@ export class UserService implements UserServiceInterface { public defaultHeaders = new HttpHeaders(); public configuration = new Configuration(); - constructor(protected httpClient: HttpClient, @Optional()@Inject(BASE_PATH) basePath: string, @Optional() configuration: Configuration) { + constructor(protected httpClient: HttpClient, + @Optional() @Inject(BASE_PATH) basePath: string, + @Optional() configuration: Configuration, + @Optional() @Inject(BASE_PATH_OBSERVABLE) basePath$: Observable) { if (basePath) { this.basePath = basePath; } @@ -40,6 +43,9 @@ export class UserService implements UserServiceInterface { this.configuration = configuration; this.basePath = basePath || configuration.basePath || this.basePath; } + if (basePath$) { + basePath$.subscribe(nextBasePath => this.basePath = nextBasePath); + } } /** diff --git a/samples/client/petstore/typescript-angular-v5/with-interfaces/api/user.serviceInterface.ts b/samples/client/petstore/typescript-angular-v5/with-interfaces/api/user.serviceInterface.ts index 7dc6496c2fa..b298d40ca4f 100644 --- a/samples/client/petstore/typescript-angular-v5/with-interfaces/api/user.serviceInterface.ts +++ b/samples/client/petstore/typescript-angular-v5/with-interfaces/api/user.serviceInterface.ts @@ -13,7 +13,6 @@ import { HttpHeaders } from '@angular/comm import { Observable } from 'rxjs/Observable'; - import { User } from '../model/user'; diff --git a/samples/client/petstore/typescript-angular-v5/with-interfaces/ng-package.json b/samples/client/petstore/typescript-angular-v5/with-interfaces/ng-package.json new file mode 100644 index 00000000000..3b17900dc9c --- /dev/null +++ b/samples/client/petstore/typescript-angular-v5/with-interfaces/ng-package.json @@ -0,0 +1,6 @@ +{ + "$schema": "./node_modules/ng-packagr/ng-package.schema.json", + "lib": { + "entryFile": "index.ts" + } +} diff --git a/samples/client/petstore/typescript-angular-v5/with-interfaces/package.json b/samples/client/petstore/typescript-angular-v5/with-interfaces/package.json new file mode 100644 index 00000000000..3de3a319525 --- /dev/null +++ b/samples/client/petstore/typescript-angular-v5/with-interfaces/package.json @@ -0,0 +1,39 @@ +{ + "name": "@swagger/angular2-typescript-petstore", + "version": "0.0.1", + "description": "swagger client for @swagger/angular2-typescript-petstore", + "author": "Swagger Codegen Contributors", + "keywords": [ + "swagger-client" + ], + "license": "Unlicense", + "scripts": { + "build": "ng-packagr -p ng-package.json" + }, + "peerDependencies": { + "@angular/core": "^5.0.0", + "@angular/http": "^5.0.0", + "@angular/common": "^5.0.0", + "@angular/compiler": "^5.0.0", + "core-js": "^2.4.0", + "reflect-metadata": "^0.1.3", + "rxjs": "^5.4.0", + "zone.js": "^0.7.6" + }, + "devDependencies": { + "@angular/compiler-cli": "^5.0.0", + "@angular/core": "^5.0.0", + "@angular/http": "^5.0.0", + "@angular/common": "^5.0.0", + "@angular/compiler": "^5.0.0", + "@angular/platform-browser": "^5.0.0", + "ng-packagr": "^2.4.1", + "reflect-metadata": "^0.1.3", + "rxjs": "^5.4.0", + "zone.js": "^0.7.6", + "typescript": ">=2.1.5 <2.8" + }, + "publishConfig": { + "registry": "https://skimdb.npmjs.com/registry" + } +} diff --git a/samples/client/petstore/typescript-angular-v5/with-interfaces/tsconfig.json b/samples/client/petstore/typescript-angular-v5/with-interfaces/tsconfig.json new file mode 100644 index 00000000000..498ee01e9dd --- /dev/null +++ b/samples/client/petstore/typescript-angular-v5/with-interfaces/tsconfig.json @@ -0,0 +1,25 @@ +{ + "compilerOptions": { + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + "noImplicitAny": false, + "suppressImplicitAnyIndexErrors": true, + "target": "es5", + "module": "commonjs", + "moduleResolution": "node", + "removeComments": true, + "sourceMap": true, + "outDir": "./dist", + "noLib": false, + "declaration": true, + "lib": [ "es6", "dom" ] + }, + "exclude": [ + "node_modules", + "dist" + ], + "filesGlob": [ + "./model/*.ts", + "./api/*.ts" + ] +} diff --git a/samples/client/petstore/typescript-angular-v5/with-interfaces/typings.json b/samples/client/petstore/typescript-angular-v5/with-interfaces/typings.json new file mode 100644 index 00000000000..507c40e5cbe --- /dev/null +++ b/samples/client/petstore/typescript-angular-v5/with-interfaces/typings.json @@ -0,0 +1,5 @@ +{ + "globalDependencies": { + "core-js": "registry:dt/core-js#0.0.0+20160725163759" + } +} diff --git a/samples/client/petstore/typescript-angular-v5/with-interfaces/variables.ts b/samples/client/petstore/typescript-angular-v5/with-interfaces/variables.ts index 6fe58549f39..6adbee1db22 100644 --- a/samples/client/petstore/typescript-angular-v5/with-interfaces/variables.ts +++ b/samples/client/petstore/typescript-angular-v5/with-interfaces/variables.ts @@ -1,6 +1,9 @@ import { InjectionToken } from '@angular/core'; +import { Observable } from 'rxjs/Observable'; + export const BASE_PATH = new InjectionToken('basePath'); +export const BASE_PATH_OBSERVABLE = new InjectionToken>('basePathObservable'); export const COLLECTION_FORMATS = { 'csv': ',', 'tsv': ' ', diff --git a/samples/client/petstore/typescript-angular-v6/npm/README.md b/samples/client/petstore/typescript-angular-v6/npm/README.md index b5a6ac2a8b8..1c867e21151 100644 --- a/samples/client/petstore/typescript-angular-v6/npm/README.md +++ b/samples/client/petstore/typescript-angular-v6/npm/README.md @@ -151,6 +151,53 @@ export class AppModule {} ``` +### Set dynamic service base path +If required the base path can be modified throughout the lifecycle of the +service by injecting an `Observable` using the `BASE_PATH_OBSERVABLE` token. + + +``` +import { BASE_PATH_OBSERVABLE } from '@swagger/angular2-typescript-petstore'; + +@NgModule({ + imports: [], + declarations: [ AppComponent ], + providers: [ + DynamicBasePathService, + { + provide: BASE_PATH_OBSERVABLE, + useFactory: () => new BehaviorSubject<string>('http://localhost:5000') + } + ], + bootstrap: [ AppComponent ] +}) +export class AppModule {} + +``` + +An example service defined below allows publishing new basePaths to the api services + +``` + +import {Inject, Injectable} from '@angular/core'; +import {Subject} from 'rxjs/Subject'; +import {BASE_PATH_OBSERVABLE} from '@swagger/angular2-typescript-petstore'; + +@Injectable() +export class DynamicBasePathService { + + constructor(@Inject(BASE_PATH_OBSERVABLE) private basePathSubject$: Subject<string>) { + } + + public setBasePath(basePath: string): void { + this.basePathSubject$.next(basePath); + } + +} + +``` + + #### Using @angular/cli First extend your `src/environments/*.ts` files by adding the corresponding base path: diff --git a/samples/client/petstore/typescript-angular-v6/npm/api/pet.service.ts b/samples/client/petstore/typescript-angular-v6/npm/api/pet.service.ts index d62314aefe5..e658e0a3485 100644 --- a/samples/client/petstore/typescript-angular-v6/npm/api/pet.service.ts +++ b/samples/client/petstore/typescript-angular-v6/npm/api/pet.service.ts @@ -21,7 +21,7 @@ import { Observable } from 'rxjs'; import { ApiResponse } from '../model/apiResponse'; import { Pet } from '../model/pet'; -import { BASE_PATH, COLLECTION_FORMATS } from '../variables'; +import { BASE_PATH, COLLECTION_FORMATS, BASE_PATH_OBSERVABLE } from '../variables'; import { Configuration } from '../configuration'; @@ -32,7 +32,10 @@ export class PetService { public defaultHeaders = new HttpHeaders(); public configuration = new Configuration(); - constructor(protected httpClient: HttpClient, @Optional()@Inject(BASE_PATH) basePath: string, @Optional() configuration: Configuration) { + constructor(protected httpClient: HttpClient, + @Optional() @Inject(BASE_PATH) basePath: string, + @Optional() configuration: Configuration, + @Optional() @Inject(BASE_PATH_OBSERVABLE) basePath$: Observable) { if (basePath) { this.basePath = basePath; } @@ -40,6 +43,9 @@ export class PetService { this.configuration = configuration; this.basePath = basePath || configuration.basePath || this.basePath; } + if (basePath$) { + basePath$.subscribe(nextBasePath => this.basePath = nextBasePath); + } } /** diff --git a/samples/client/petstore/typescript-angular-v6/npm/api/store.service.ts b/samples/client/petstore/typescript-angular-v6/npm/api/store.service.ts index c8e83fb6416..8b342ece073 100644 --- a/samples/client/petstore/typescript-angular-v6/npm/api/store.service.ts +++ b/samples/client/petstore/typescript-angular-v6/npm/api/store.service.ts @@ -20,7 +20,7 @@ import { Observable } from 'rxjs'; import { Order } from '../model/order'; -import { BASE_PATH, COLLECTION_FORMATS } from '../variables'; +import { BASE_PATH, COLLECTION_FORMATS, BASE_PATH_OBSERVABLE } from '../variables'; import { Configuration } from '../configuration'; @@ -31,7 +31,10 @@ export class StoreService { public defaultHeaders = new HttpHeaders(); public configuration = new Configuration(); - constructor(protected httpClient: HttpClient, @Optional()@Inject(BASE_PATH) basePath: string, @Optional() configuration: Configuration) { + constructor(protected httpClient: HttpClient, + @Optional() @Inject(BASE_PATH) basePath: string, + @Optional() configuration: Configuration, + @Optional() @Inject(BASE_PATH_OBSERVABLE) basePath$: Observable) { if (basePath) { this.basePath = basePath; } @@ -39,6 +42,9 @@ export class StoreService { this.configuration = configuration; this.basePath = basePath || configuration.basePath || this.basePath; } + if (basePath$) { + basePath$.subscribe(nextBasePath => this.basePath = nextBasePath); + } } /** diff --git a/samples/client/petstore/typescript-angular-v6/npm/api/user.service.ts b/samples/client/petstore/typescript-angular-v6/npm/api/user.service.ts index c6ea28aa921..761914ed4d9 100644 --- a/samples/client/petstore/typescript-angular-v6/npm/api/user.service.ts +++ b/samples/client/petstore/typescript-angular-v6/npm/api/user.service.ts @@ -20,7 +20,7 @@ import { Observable } from 'rxjs'; import { User } from '../model/user'; -import { BASE_PATH, COLLECTION_FORMATS } from '../variables'; +import { BASE_PATH, COLLECTION_FORMATS, BASE_PATH_OBSERVABLE } from '../variables'; import { Configuration } from '../configuration'; @@ -31,7 +31,10 @@ export class UserService { public defaultHeaders = new HttpHeaders(); public configuration = new Configuration(); - constructor(protected httpClient: HttpClient, @Optional()@Inject(BASE_PATH) basePath: string, @Optional() configuration: Configuration) { + constructor(protected httpClient: HttpClient, + @Optional() @Inject(BASE_PATH) basePath: string, + @Optional() configuration: Configuration, + @Optional() @Inject(BASE_PATH_OBSERVABLE) basePath$: Observable) { if (basePath) { this.basePath = basePath; } @@ -39,6 +42,9 @@ export class UserService { this.configuration = configuration; this.basePath = basePath || configuration.basePath || this.basePath; } + if (basePath$) { + basePath$.subscribe(nextBasePath => this.basePath = nextBasePath); + } } /** diff --git a/samples/client/petstore/typescript-angular-v6/npm/variables.ts b/samples/client/petstore/typescript-angular-v6/npm/variables.ts index 6fe58549f39..fc3e7860a0a 100644 --- a/samples/client/petstore/typescript-angular-v6/npm/variables.ts +++ b/samples/client/petstore/typescript-angular-v6/npm/variables.ts @@ -1,6 +1,9 @@ import { InjectionToken } from '@angular/core'; +import { Observable } from 'rxjs'; + export const BASE_PATH = new InjectionToken('basePath'); +export const BASE_PATH_OBSERVABLE = new InjectionToken>('basePathObservable'); export const COLLECTION_FORMATS = { 'csv': ',', 'tsv': ' ',