From 8d29ca42d5872cd7d1b62c1d1bf9bf9feaa039f9 Mon Sep 17 00:00:00 2001 From: Tino Fuhrmann Date: Sun, 2 Dec 2018 19:47:09 +0100 Subject: [PATCH] Added server variable configuration to ts-refactor --- .../typescript/configuration.mustache | 8 ++-- .../typescript/http/servers.mustache | 41 ++++++++++++++----- .../typescript/builds/default/apis/PetApi.ts | 2 - .../builds/default/apis/StoreApi.ts | 1 - .../typescript/builds/default/apis/UserApi.ts | 4 -- .../builds/default/configuration.ts | 8 ++-- .../typescript/builds/default/middleware.ts | 4 +- .../typescript/builds/default/servers.ts | 35 ++++++++++++---- 8 files changed, 67 insertions(+), 36 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/typescript/configuration.mustache b/modules/openapi-generator/src/main/resources/typescript/configuration.mustache index c672e9a4d00..41d7a075f51 100644 --- a/modules/openapi-generator/src/main/resources/typescript/configuration.mustache +++ b/modules/openapi-generator/src/main/resources/typescript/configuration.mustache @@ -1,12 +1,12 @@ import {HttpLibrary} from './http/http'; import {Middleware} from './middleware'; import {IsomorphicFetchHttpLibrary} from "./http/isomorphic-fetch"; -import {ServerConfiguration, servers} from './servers'; +import {ServerConfiguration, server1} from './servers'; import {configureAuthMethods, AuthMethods, AuthMethodsConfiguration} from './auth/auth'; export interface ConfigurationParameters { - baseServer?: ServerConfiguration; + baseServer?: ServerConfiguration; httpApi?: HttpLibrary; // override for fetch implementation middleware?: Middleware[]; // middleware to apply before/after fetch requests authMethods?: AuthMethodsConfiguration @@ -14,13 +14,13 @@ export interface ConfigurationParameters { export class Configuration { - baseServer: ServerConfiguration; + baseServer: ServerConfiguration; httpApi: HttpLibrary; middleware: Middleware[]; authMethods: AuthMethods; constructor(conf: ConfigurationParameters = {}) { - this.baseServer = conf.baseServer !== undefined ? conf.baseServer : servers[0]; + this.baseServer = conf.baseServer !== undefined ? conf.baseServer : server1; this.httpApi = conf.httpApi || new IsomorphicFetchHttpLibrary(); // TODO: replace with window.fetch? this.middleware = conf.middleware || []; this.authMethods = configureAuthMethods(conf.authMethods); diff --git a/modules/openapi-generator/src/main/resources/typescript/http/servers.mustache b/modules/openapi-generator/src/main/resources/typescript/http/servers.mustache index 6d738c29096..3b706519322 100644 --- a/modules/openapi-generator/src/main/resources/typescript/http/servers.mustache +++ b/modules/openapi-generator/src/main/resources/typescript/http/servers.mustache @@ -1,19 +1,38 @@ import {RequestContext, HttpMethod} from './http/http'; -export class ServerConfiguration { +export class ServerConfiguration { - public constructor(private url: string) { + public constructor(private url: string, private variableConfiguration: T) { } - + + public setVariables(variableConfiguration: Partial) { + for (const key in variableConfiguration) { + const val = variableConfiguration[key] + // We know that val isn't undefined here - hopefully + if (val !== undefined) { + this.variableConfiguration[key] = val as T[Extract]; + } + } + } + + public getConfiguration(): T { + return this.variableConfiguration + } + + private getUrl() { + let replacedUrl = this.url; + for (const key in this.variableConfiguration) { + var re = new RegExp("{" + key + "}","g"); + replacedUrl = replacedUrl.replace(re, this.variableConfiguration[key].toString()); + } + return replacedUrl + } + public makeRequestContext(endpoint: string, httpMethod: HttpMethod): RequestContext { - return new RequestContext(this.url + endpoint, httpMethod); + return new RequestContext(this.getUrl() + endpoint, httpMethod); } } -{{#openAPI}} -export const servers = [ - {{#servers}} - new ServerConfiguration("{{url}}"){{^last}},{{/last}} - {{/servers}} -] -{{/openAPI}} \ No newline at end of file +{{#servers}} +export const server{{-index}} = new ServerConfiguration<{ {{#variables}} "{{name}}": {{#enumValues}}"{{.}}"{{^-last}} | {{/-last}}{{/enumValues}}{{^enumValues}}string{{/enumValues}}{{^-last}},{{/-last}} {{/variables}} }>("{{url}}", { {{#variables}} "{{name}}": "{{defaultValue}}" {{^-last}},{{/-last}}{{/variables}} }) +{{/servers}} diff --git a/samples/client/petstore/typescript/builds/default/apis/PetApi.ts b/samples/client/petstore/typescript/builds/default/apis/PetApi.ts index 75deb2c89f1..049a3a820dd 100644 --- a/samples/client/petstore/typescript/builds/default/apis/PetApi.ts +++ b/samples/client/petstore/typescript/builds/default/apis/PetApi.ts @@ -30,7 +30,6 @@ export class PetApiRequestFactory extends BaseAPIRequestFactory { // Body Params - // TODO: deal with this? Could be useful for server definition requestContext.setHeaderParam("Content-Type", "application/json"); // TODO: Should this be handled by ObjectSerializer? imo yes => confidential information included in local object should not be sent const needsSerialization = ("Pet" !== "string") || requestContext.getHeaders()['Content-Type'] === 'application/json'; @@ -205,7 +204,6 @@ export class PetApiRequestFactory extends BaseAPIRequestFactory { // Body Params - // TODO: deal with this? Could be useful for server definition requestContext.setHeaderParam("Content-Type", "application/json"); // TODO: Should this be handled by ObjectSerializer? imo yes => confidential information included in local object should not be sent const needsSerialization = ("Pet" !== "string") || requestContext.getHeaders()['Content-Type'] === 'application/json'; diff --git a/samples/client/petstore/typescript/builds/default/apis/StoreApi.ts b/samples/client/petstore/typescript/builds/default/apis/StoreApi.ts index 3445cbf2ebf..41ca20d0b2a 100644 --- a/samples/client/petstore/typescript/builds/default/apis/StoreApi.ts +++ b/samples/client/petstore/typescript/builds/default/apis/StoreApi.ts @@ -113,7 +113,6 @@ export class StoreApiRequestFactory extends BaseAPIRequestFactory { // Body Params requestContext.setHeaderParam("Content-Type", "application/json"); - // TODO: deal with this? Could be useful for server definition // TODO: Should this be handled by ObjectSerializer? imo yes => confidential information included in local object should not be sent const needsSerialization = ("Order" !== "string") || requestContext.getHeaders()['Content-Type'] === 'application/json'; const serializedBody = needsSerialization ? JSON.stringify(order || {}) : (order.toString() || ""); // TODO: `toString` call is unnecessary diff --git a/samples/client/petstore/typescript/builds/default/apis/UserApi.ts b/samples/client/petstore/typescript/builds/default/apis/UserApi.ts index ebb1c357f1d..428bcd845b1 100644 --- a/samples/client/petstore/typescript/builds/default/apis/UserApi.ts +++ b/samples/client/petstore/typescript/builds/default/apis/UserApi.ts @@ -30,7 +30,6 @@ export class UserApiRequestFactory extends BaseAPIRequestFactory { // Body Params requestContext.setHeaderParam("Content-Type", "application/json"); - // TODO: deal with this? Could be useful for server definition // TODO: Should this be handled by ObjectSerializer? imo yes => confidential information included in local object should not be sent const needsSerialization = ("User" !== "string") || requestContext.getHeaders()['Content-Type'] === 'application/json'; const serializedBody = needsSerialization ? JSON.stringify(user || {}) : (user.toString() || ""); // TODO: `toString` call is unnecessary @@ -63,7 +62,6 @@ export class UserApiRequestFactory extends BaseAPIRequestFactory { // Body Params requestContext.setHeaderParam("Content-Type", "application/json"); - // TODO: deal with this? Could be useful for server definition // TODO: Should this be handled by ObjectSerializer? imo yes => confidential information included in local object should not be sent const needsSerialization = ("Array<User>" !== "string") || requestContext.getHeaders()['Content-Type'] === 'application/json'; const serializedBody = needsSerialization ? JSON.stringify(user || {}) : (user.toString() || ""); // TODO: `toString` call is unnecessary @@ -96,7 +94,6 @@ export class UserApiRequestFactory extends BaseAPIRequestFactory { // Body Params requestContext.setHeaderParam("Content-Type", "application/json"); - // TODO: deal with this? Could be useful for server definition // TODO: Should this be handled by ObjectSerializer? imo yes => confidential information included in local object should not be sent const needsSerialization = ("Array<User>" !== "string") || requestContext.getHeaders()['Content-Type'] === 'application/json'; const serializedBody = needsSerialization ? JSON.stringify(user || {}) : (user.toString() || ""); // TODO: `toString` call is unnecessary @@ -251,7 +248,6 @@ export class UserApiRequestFactory extends BaseAPIRequestFactory { // Body Params requestContext.setHeaderParam("Content-Type", "application/json"); - // TODO: deal with this? Could be useful for server definition // TODO: Should this be handled by ObjectSerializer? imo yes => confidential information included in local object should not be sent const needsSerialization = ("User" !== "string") || requestContext.getHeaders()['Content-Type'] === 'application/json'; const serializedBody = needsSerialization ? JSON.stringify(user || {}) : (user.toString() || ""); // TODO: `toString` call is unnecessary diff --git a/samples/client/petstore/typescript/builds/default/configuration.ts b/samples/client/petstore/typescript/builds/default/configuration.ts index c672e9a4d00..41d7a075f51 100644 --- a/samples/client/petstore/typescript/builds/default/configuration.ts +++ b/samples/client/petstore/typescript/builds/default/configuration.ts @@ -1,12 +1,12 @@ import {HttpLibrary} from './http/http'; import {Middleware} from './middleware'; import {IsomorphicFetchHttpLibrary} from "./http/isomorphic-fetch"; -import {ServerConfiguration, servers} from './servers'; +import {ServerConfiguration, server1} from './servers'; import {configureAuthMethods, AuthMethods, AuthMethodsConfiguration} from './auth/auth'; export interface ConfigurationParameters { - baseServer?: ServerConfiguration; + baseServer?: ServerConfiguration; httpApi?: HttpLibrary; // override for fetch implementation middleware?: Middleware[]; // middleware to apply before/after fetch requests authMethods?: AuthMethodsConfiguration @@ -14,13 +14,13 @@ export interface ConfigurationParameters { export class Configuration { - baseServer: ServerConfiguration; + baseServer: ServerConfiguration; httpApi: HttpLibrary; middleware: Middleware[]; authMethods: AuthMethods; constructor(conf: ConfigurationParameters = {}) { - this.baseServer = conf.baseServer !== undefined ? conf.baseServer : servers[0]; + this.baseServer = conf.baseServer !== undefined ? conf.baseServer : server1; this.httpApi = conf.httpApi || new IsomorphicFetchHttpLibrary(); // TODO: replace with window.fetch? this.middleware = conf.middleware || []; this.authMethods = configureAuthMethods(conf.authMethods); diff --git a/samples/client/petstore/typescript/builds/default/middleware.ts b/samples/client/petstore/typescript/builds/default/middleware.ts index 3ac072bab22..bb2037740f2 100644 --- a/samples/client/petstore/typescript/builds/default/middleware.ts +++ b/samples/client/petstore/typescript/builds/default/middleware.ts @@ -3,6 +3,4 @@ import {RequestContext, ResponseContext} from './http/http'; export interface Middleware { pre(context: RequestContext): Promise; post(context: ResponseContext): Promise; -} - -// TODO: package.json set npmName \ No newline at end of file +} \ No newline at end of file diff --git a/samples/client/petstore/typescript/builds/default/servers.ts b/samples/client/petstore/typescript/builds/default/servers.ts index 7cc55b40db0..85aeaa2c8a8 100644 --- a/samples/client/petstore/typescript/builds/default/servers.ts +++ b/samples/client/petstore/typescript/builds/default/servers.ts @@ -1,15 +1,36 @@ import {RequestContext, HttpMethod} from './http/http'; -export class ServerConfiguration { +export class ServerConfiguration { - public constructor(private url: string) { + public constructor(private url: string, private variableConfiguration: T) { } - + + public setVariables(variableConfiguration: Partial) { + for (const key in variableConfiguration) { + const val = variableConfiguration[key] + // We know that val isn't undefined here - hopefully + if (val !== undefined) { + this.variableConfiguration[key] = val as T[Extract]; + } + } + } + + public getConfiguration(): T { + return this.variableConfiguration + } + + private getUrl() { + let replacedUrl = this.url; + for (const key in this.variableConfiguration) { + var re = new RegExp("{" + key + "}","g"); + replacedUrl = replacedUrl.replace(re, this.variableConfiguration[key].toString()); + } + return replacedUrl + } + public makeRequestContext(endpoint: string, httpMethod: HttpMethod): RequestContext { - return new RequestContext(this.url + endpoint, httpMethod); + return new RequestContext(this.getUrl() + endpoint, httpMethod); } } -export const servers = [ - new ServerConfiguration("http://petstore.swagger.io/v2"), -] +export const server1 = new ServerConfiguration<{ }>("http://petstore.swagger.io/v2", { })