From 54fe7a731caec554d582b4b0a79defdc52751e05 Mon Sep 17 00:00:00 2001 From: Damien Pontifex Date: Mon, 22 Aug 2016 21:40:51 +0800 Subject: [PATCH] inject base_path opaque token for typescript angular2 (#3514) * inject base_path opaque token for typescript angular2 * Readme note on providing base path to service --- .../languages/TypeScriptAngular2ClientCodegen.java | 1 + .../resources/typescript-angular2/README.mustache | 11 +++++++++++ .../main/resources/typescript-angular2/api.mustache | 7 ++++--- .../main/resources/typescript-angular2/index.mustache | 3 ++- .../resources/typescript-angular2/variables.mustache | 3 +++ 5 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 modules/swagger-codegen/src/main/resources/typescript-angular2/variables.mustache diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngular2ClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngular2ClientCodegen.java index c9dd10b2a58..b8a9f772ad1 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngular2ClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngular2ClientCodegen.java @@ -68,6 +68,7 @@ public class TypeScriptAngular2ClientCodegen extends AbstractTypeScriptClientCod supportingFiles.add(new SupportingFile("models.mustache", modelPackage().replace('.', File.separatorChar), "models.ts")); supportingFiles.add(new SupportingFile("apis.mustache", apiPackage().replace('.', File.separatorChar), "api.ts")); supportingFiles.add(new SupportingFile("index.mustache", getIndexDirectory(), "index.ts")); + supportingFiles.add(new SupportingFile("variables.mustache", getIndexDirectory(), "variables.ts")); supportingFiles.add(new SupportingFile("gitignore", "", ".gitignore")); supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh")); diff --git a/modules/swagger-codegen/src/main/resources/typescript-angular2/README.mustache b/modules/swagger-codegen/src/main/resources/typescript-angular2/README.mustache index 73736fefb0c..218831c42d1 100644 --- a/modules/swagger-codegen/src/main/resources/typescript-angular2/README.mustache +++ b/modules/swagger-codegen/src/main/resources/typescript-angular2/README.mustache @@ -31,3 +31,14 @@ npm install PATH_TO_GENERATED_PACKAGE --save In your angular2 project: TODO: paste example. + +### Set service base path +If different than the generated base path, during app bootstrap, you can provide the base path to your service. + +``` +import { BASE_PATH } from './path-to-swagger-gen-service/index'; + +bootstrap(AppComponent, [ + { provide: BASE_PATH, useValue: 'https://your-web-service.com' }, +]); +``` \ No newline at end of file diff --git a/modules/swagger-codegen/src/main/resources/typescript-angular2/api.mustache b/modules/swagger-codegen/src/main/resources/typescript-angular2/api.mustache index c0645280549..8565dee4c5a 100644 --- a/modules/swagger-codegen/src/main/resources/typescript-angular2/api.mustache +++ b/modules/swagger-codegen/src/main/resources/typescript-angular2/api.mustache @@ -1,8 +1,9 @@ {{>licenseInfo}} import {Http, Headers, RequestOptionsArgs, Response, URLSearchParams} from '@angular/http'; -import {Injectable, Optional} from '@angular/core'; +import {Inject, Injectable, Optional} from '@angular/core'; import {Observable} from 'rxjs/Observable'; import * as models from '../model/models'; +import {BASE_PATH} from '../variables'; import 'rxjs/Rx'; /* tslint:disable:no-unused-variable member-ordering */ @@ -20,7 +21,7 @@ export class {{classname}} { protected basePath = '{{basePath}}'; public defaultHeaders : Headers = new Headers(); - constructor(protected http: Http, @Optional() basePath: string) { + constructor(protected http: Http, @Optional()@Inject(BASE_PATH) basePath: string) { if (basePath) { this.basePath = basePath; } @@ -32,7 +33,7 @@ export class {{classname}} { * {{notes}} {{#allParams}}* @param {{paramName}} {{description}} {{/allParams}}*/ - public {{nickname}} ({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}extraHttpRequestParams?: any ) : Observable<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}{}{{/returnType}}> { + public {{nickname}} ({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}extraHttpRequestParams?: any): Observable<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}{}{{/returnType}}> { const path = this.basePath + '{{path}}'{{#pathParams}} .replace('{' + '{{baseName}}' + '}', String({{paramName}})){{/pathParams}}; diff --git a/modules/swagger-codegen/src/main/resources/typescript-angular2/index.mustache b/modules/swagger-codegen/src/main/resources/typescript-angular2/index.mustache index 557365516ad..ec7dcae82b2 100644 --- a/modules/swagger-codegen/src/main/resources/typescript-angular2/index.mustache +++ b/modules/swagger-codegen/src/main/resources/typescript-angular2/index.mustache @@ -1,2 +1,3 @@ export * from './api/api'; -export * from './model/models'; \ No newline at end of file +export * from './model/models'; +export * from './variables'; \ No newline at end of file diff --git a/modules/swagger-codegen/src/main/resources/typescript-angular2/variables.mustache b/modules/swagger-codegen/src/main/resources/typescript-angular2/variables.mustache new file mode 100644 index 00000000000..27b987e9b23 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/typescript-angular2/variables.mustache @@ -0,0 +1,3 @@ +import { OpaqueToken } from '@angular/core'; + +export const BASE_PATH = new OpaqueToken('basePath'); \ No newline at end of file