Added server variable configuration to ts-refactor

This commit is contained in:
Tino Fuhrmann 2018-12-02 19:47:09 +01:00
parent 49219fa48a
commit 8d29ca42d5
8 changed files with 67 additions and 36 deletions

View File

@ -1,12 +1,12 @@
import {HttpLibrary} from './http/http'; import {HttpLibrary} from './http/http';
import {Middleware} from './middleware'; import {Middleware} from './middleware';
import {IsomorphicFetchHttpLibrary} from "./http/isomorphic-fetch"; import {IsomorphicFetchHttpLibrary} from "./http/isomorphic-fetch";
import {ServerConfiguration, servers} from './servers'; import {ServerConfiguration, server1} from './servers';
import {configureAuthMethods, AuthMethods, AuthMethodsConfiguration} from './auth/auth'; import {configureAuthMethods, AuthMethods, AuthMethodsConfiguration} from './auth/auth';
export interface ConfigurationParameters { export interface ConfigurationParameters {
baseServer?: ServerConfiguration; baseServer?: ServerConfiguration<any>;
httpApi?: HttpLibrary; // override for fetch implementation httpApi?: HttpLibrary; // override for fetch implementation
middleware?: Middleware[]; // middleware to apply before/after fetch requests middleware?: Middleware[]; // middleware to apply before/after fetch requests
authMethods?: AuthMethodsConfiguration authMethods?: AuthMethodsConfiguration
@ -14,13 +14,13 @@ export interface ConfigurationParameters {
export class Configuration { export class Configuration {
baseServer: ServerConfiguration; baseServer: ServerConfiguration<any>;
httpApi: HttpLibrary; httpApi: HttpLibrary;
middleware: Middleware[]; middleware: Middleware[];
authMethods: AuthMethods; authMethods: AuthMethods;
constructor(conf: ConfigurationParameters = {}) { 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.httpApi = conf.httpApi || new IsomorphicFetchHttpLibrary(); // TODO: replace with window.fetch?
this.middleware = conf.middleware || []; this.middleware = conf.middleware || [];
this.authMethods = configureAuthMethods(conf.authMethods); this.authMethods = configureAuthMethods(conf.authMethods);

View File

@ -1,19 +1,38 @@
import {RequestContext, HttpMethod} from './http/http'; import {RequestContext, HttpMethod} from './http/http';
export class ServerConfiguration { export class ServerConfiguration<T> {
public constructor(private url: string) { public constructor(private url: string, private variableConfiguration: T) {
}
public setVariables(variableConfiguration: Partial<T>) {
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<keyof T, string>];
}
}
}
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 { 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}} {{#servers}}
new ServerConfiguration("{{url}}"){{^last}},{{/last}} 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}} {{/servers}}
]
{{/openAPI}}

View File

@ -30,7 +30,6 @@ export class PetApiRequestFactory extends BaseAPIRequestFactory {
// Body Params // Body Params
// TODO: deal with this? Could be useful for server definition
requestContext.setHeaderParam("Content-Type", "application/json"); 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 // TODO: Should this be handled by ObjectSerializer? imo yes => confidential information included in local object should not be sent
const needsSerialization = (<any>"Pet" !== "string") || requestContext.getHeaders()['Content-Type'] === 'application/json'; const needsSerialization = (<any>"Pet" !== "string") || requestContext.getHeaders()['Content-Type'] === 'application/json';
@ -205,7 +204,6 @@ export class PetApiRequestFactory extends BaseAPIRequestFactory {
// Body Params // Body Params
// TODO: deal with this? Could be useful for server definition
requestContext.setHeaderParam("Content-Type", "application/json"); 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 // TODO: Should this be handled by ObjectSerializer? imo yes => confidential information included in local object should not be sent
const needsSerialization = (<any>"Pet" !== "string") || requestContext.getHeaders()['Content-Type'] === 'application/json'; const needsSerialization = (<any>"Pet" !== "string") || requestContext.getHeaders()['Content-Type'] === 'application/json';

View File

@ -113,7 +113,6 @@ export class StoreApiRequestFactory extends BaseAPIRequestFactory {
// Body Params // Body Params
requestContext.setHeaderParam("Content-Type", "application/json"); 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 // TODO: Should this be handled by ObjectSerializer? imo yes => confidential information included in local object should not be sent
const needsSerialization = (<any>"Order" !== "string") || requestContext.getHeaders()['Content-Type'] === 'application/json'; const needsSerialization = (<any>"Order" !== "string") || requestContext.getHeaders()['Content-Type'] === 'application/json';
const serializedBody = needsSerialization ? JSON.stringify(order || {}) : (order.toString() || ""); // TODO: `toString` call is unnecessary const serializedBody = needsSerialization ? JSON.stringify(order || {}) : (order.toString() || ""); // TODO: `toString` call is unnecessary

View File

@ -30,7 +30,6 @@ export class UserApiRequestFactory extends BaseAPIRequestFactory {
// Body Params // Body Params
requestContext.setHeaderParam("Content-Type", "application/json"); 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 // TODO: Should this be handled by ObjectSerializer? imo yes => confidential information included in local object should not be sent
const needsSerialization = (<any>"User" !== "string") || requestContext.getHeaders()['Content-Type'] === 'application/json'; const needsSerialization = (<any>"User" !== "string") || requestContext.getHeaders()['Content-Type'] === 'application/json';
const serializedBody = needsSerialization ? JSON.stringify(user || {}) : (user.toString() || ""); // TODO: `toString` call is unnecessary const serializedBody = needsSerialization ? JSON.stringify(user || {}) : (user.toString() || ""); // TODO: `toString` call is unnecessary
@ -63,7 +62,6 @@ export class UserApiRequestFactory extends BaseAPIRequestFactory {
// Body Params // Body Params
requestContext.setHeaderParam("Content-Type", "application/json"); 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 // TODO: Should this be handled by ObjectSerializer? imo yes => confidential information included in local object should not be sent
const needsSerialization = (<any>"Array&lt;User&gt;" !== "string") || requestContext.getHeaders()['Content-Type'] === 'application/json'; const needsSerialization = (<any>"Array&lt;User&gt;" !== "string") || requestContext.getHeaders()['Content-Type'] === 'application/json';
const serializedBody = needsSerialization ? JSON.stringify(user || {}) : (user.toString() || ""); // TODO: `toString` call is unnecessary const serializedBody = needsSerialization ? JSON.stringify(user || {}) : (user.toString() || ""); // TODO: `toString` call is unnecessary
@ -96,7 +94,6 @@ export class UserApiRequestFactory extends BaseAPIRequestFactory {
// Body Params // Body Params
requestContext.setHeaderParam("Content-Type", "application/json"); 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 // TODO: Should this be handled by ObjectSerializer? imo yes => confidential information included in local object should not be sent
const needsSerialization = (<any>"Array&lt;User&gt;" !== "string") || requestContext.getHeaders()['Content-Type'] === 'application/json'; const needsSerialization = (<any>"Array&lt;User&gt;" !== "string") || requestContext.getHeaders()['Content-Type'] === 'application/json';
const serializedBody = needsSerialization ? JSON.stringify(user || {}) : (user.toString() || ""); // TODO: `toString` call is unnecessary const serializedBody = needsSerialization ? JSON.stringify(user || {}) : (user.toString() || ""); // TODO: `toString` call is unnecessary
@ -251,7 +248,6 @@ export class UserApiRequestFactory extends BaseAPIRequestFactory {
// Body Params // Body Params
requestContext.setHeaderParam("Content-Type", "application/json"); 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 // TODO: Should this be handled by ObjectSerializer? imo yes => confidential information included in local object should not be sent
const needsSerialization = (<any>"User" !== "string") || requestContext.getHeaders()['Content-Type'] === 'application/json'; const needsSerialization = (<any>"User" !== "string") || requestContext.getHeaders()['Content-Type'] === 'application/json';
const serializedBody = needsSerialization ? JSON.stringify(user || {}) : (user.toString() || ""); // TODO: `toString` call is unnecessary const serializedBody = needsSerialization ? JSON.stringify(user || {}) : (user.toString() || ""); // TODO: `toString` call is unnecessary

View File

@ -1,12 +1,12 @@
import {HttpLibrary} from './http/http'; import {HttpLibrary} from './http/http';
import {Middleware} from './middleware'; import {Middleware} from './middleware';
import {IsomorphicFetchHttpLibrary} from "./http/isomorphic-fetch"; import {IsomorphicFetchHttpLibrary} from "./http/isomorphic-fetch";
import {ServerConfiguration, servers} from './servers'; import {ServerConfiguration, server1} from './servers';
import {configureAuthMethods, AuthMethods, AuthMethodsConfiguration} from './auth/auth'; import {configureAuthMethods, AuthMethods, AuthMethodsConfiguration} from './auth/auth';
export interface ConfigurationParameters { export interface ConfigurationParameters {
baseServer?: ServerConfiguration; baseServer?: ServerConfiguration<any>;
httpApi?: HttpLibrary; // override for fetch implementation httpApi?: HttpLibrary; // override for fetch implementation
middleware?: Middleware[]; // middleware to apply before/after fetch requests middleware?: Middleware[]; // middleware to apply before/after fetch requests
authMethods?: AuthMethodsConfiguration authMethods?: AuthMethodsConfiguration
@ -14,13 +14,13 @@ export interface ConfigurationParameters {
export class Configuration { export class Configuration {
baseServer: ServerConfiguration; baseServer: ServerConfiguration<any>;
httpApi: HttpLibrary; httpApi: HttpLibrary;
middleware: Middleware[]; middleware: Middleware[];
authMethods: AuthMethods; authMethods: AuthMethods;
constructor(conf: ConfigurationParameters = {}) { 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.httpApi = conf.httpApi || new IsomorphicFetchHttpLibrary(); // TODO: replace with window.fetch?
this.middleware = conf.middleware || []; this.middleware = conf.middleware || [];
this.authMethods = configureAuthMethods(conf.authMethods); this.authMethods = configureAuthMethods(conf.authMethods);

View File

@ -4,5 +4,3 @@ export interface Middleware {
pre(context: RequestContext): Promise<RequestContext>; pre(context: RequestContext): Promise<RequestContext>;
post(context: ResponseContext): Promise<ResponseContext>; post(context: ResponseContext): Promise<ResponseContext>;
} }
// TODO: package.json set npmName

View File

@ -1,15 +1,36 @@
import {RequestContext, HttpMethod} from './http/http'; import {RequestContext, HttpMethod} from './http/http';
export class ServerConfiguration { export class ServerConfiguration<T> {
public constructor(private url: string) { public constructor(private url: string, private variableConfiguration: T) {
}
public setVariables(variableConfiguration: Partial<T>) {
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<keyof T, string>];
}
}
}
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 { public makeRequestContext(endpoint: string, httpMethod: HttpMethod): RequestContext {
return new RequestContext(this.url + endpoint, httpMethod); return new RequestContext(this.getUrl() + endpoint, httpMethod);
} }
} }
export const servers = [ export const server1 = new ServerConfiguration<{ }>("http://petstore.swagger.io/v2", { })
new ServerConfiguration("http://petstore.swagger.io/v2"),
]