Skip to content

Commit

Permalink
feat(domain): relative path instead of localhost
Browse files Browse the repository at this point in the history
* Default to relative path instead of localhost for domain.

* Remove window declare (not needed). Use shorter syntax for setting default address

* Implement default hostname in generator instead of template

* Fixes to domain generation

* Added commnets to generator

* Undo unintended change to template
  • Loading branch information
warmans authored and vmasek committed Feb 8, 2018
1 parent ee54000 commit e19fffe
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
16 changes: 9 additions & 7 deletions src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,15 @@ export class Generator {
};

private static generateDomain({schemes, host, basePath}: Swagger): string {
const protocol =
schemes && schemes.length > 0
? schemes[0]
: 'http';
const domain = host ? host : 'localhost';
const base = ('/' === basePath ? '' : basePath);
return `${protocol}://${domain}${base}`;

// if the host is defined then try and use a protocol from the swagger file. Otherwise just send
// requests using the same protocol as the library was loaded with.
const protocol = host && schemes && schemes.length > 0 ? `${schemes[0]}://` : '//';

// if no host exists in the swagger file default to effectively a relative path.
const domain = host ? host : "${window.location.hostname}${window.location.port ? ':'+window.location.port : ''}";
const base = ('/' === basePath || !basePath ? '' : basePath);
return `${protocol}${domain}${base}`;
}

private static generateDefinitions(definitions: { [definitionsName: string]: Schema } = {}): Definition[] {
Expand Down
3 changes: 2 additions & 1 deletion templates/ngx-service.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ interface HttpOptions {
export class ApiClientService {
readonly options: HttpOptions;
private domain = 'http://localhost:8080';
private domain: string = `{{&domain}}`;

constructor(private http: HttpClient,
@Optional() @Inject('domain') domain: string,
@Optional() @Inject('httpOptions') options: HttpOptions) {
if (domain) {
this.domain = domain;
}
Expand Down

0 comments on commit e19fffe

Please sign in to comment.