Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>` 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&lt;string&gt;('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&lt;string&gt;) {
}

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:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -53,14 +53,20 @@ 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<string>) {
if (basePath) {
this.basePath = basePath;
}
if (configuration) {
this.configuration = configuration;
this.basePath = basePath || configuration.basePath || this.basePath;
}
if (basePath$) {
basePath$.subscribe(nextBasePath => this.basePath = nextBasePath);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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}}<string>{{/injectionTokenTyped}}('basePath');
export const BASE_PATH_OBSERVABLE = new {{injectionToken}}{{#injectionTokenTyped}}<Observable<string>>{{/injectionTokenTyped}}('basePathObservable');
export const COLLECTION_FORMATS = {
'csv': ',',
'tsv': ' ',
Expand Down
47 changes: 47 additions & 0 deletions samples/client/petstore/typescript-angular-v2/default/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>` using the `BASE_PATH_OBSERVABLE` token.


```
import { BASE_PATH_OBSERVABLE } from '';

@NgModule({
imports: [],
declarations: [ AppComponent ],
providers: [
DynamicBasePathService,
{
provide: BASE_PATH_OBSERVABLE,
useFactory: () => new BehaviorSubject&lt;string&gt;('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&lt;string&gt;) {
}

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:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';


Expand All @@ -34,14 +34,20 @@ 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<string>) {
if (basePath) {
this.basePath = basePath;
}
if (configuration) {
this.configuration = configuration;
this.basePath = basePath || configuration.basePath || this.basePath;
}
if (basePath$) {
basePath$.subscribe(nextBasePath => this.basePath = nextBasePath);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';


Expand All @@ -33,14 +33,20 @@ 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<string>) {
if (basePath) {
this.basePath = basePath;
}
if (configuration) {
this.configuration = configuration;
this.basePath = basePath || configuration.basePath || this.basePath;
}
if (basePath$) {
basePath$.subscribe(nextBasePath => this.basePath = nextBasePath);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';


Expand All @@ -33,14 +33,20 @@ 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<string>) {
if (basePath) {
this.basePath = basePath;
}
if (configuration) {
this.configuration = configuration;
this.basePath = basePath || configuration.basePath || this.basePath;
}
if (basePath$) {
basePath$.subscribe(nextBasePath => this.basePath = nextBasePath);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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': ' ',
Expand Down
47 changes: 47 additions & 0 deletions samples/client/petstore/typescript-angular-v2/npm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>` 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&lt;string&gt;('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&lt;string&gt;) {
}

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:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';


Expand All @@ -34,14 +34,20 @@ 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<string>) {
if (basePath) {
this.basePath = basePath;
}
if (configuration) {
this.configuration = configuration;
this.basePath = basePath || configuration.basePath || this.basePath;
}
if (basePath$) {
basePath$.subscribe(nextBasePath => this.basePath = nextBasePath);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';


Expand All @@ -33,14 +33,20 @@ 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<string>) {
if (basePath) {
this.basePath = basePath;
}
if (configuration) {
this.configuration = configuration;
this.basePath = basePath || configuration.basePath || this.basePath;
}
if (basePath$) {
basePath$.subscribe(nextBasePath => this.basePath = nextBasePath);
}
}

/**
Expand Down
Loading