forked from loafle/openapi-generator-original
Added server variable configuration to ts-refactor
This commit is contained in:
parent
49219fa48a
commit
8d29ca42d5
@ -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<any>;
|
||||
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<any>;
|
||||
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);
|
||||
|
@ -1,19 +1,38 @@
|
||||
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 {
|
||||
return new RequestContext(this.url + endpoint, httpMethod);
|
||||
return new RequestContext(this.getUrl() + endpoint, httpMethod);
|
||||
}
|
||||
}
|
||||
|
||||
{{#openAPI}}
|
||||
export const 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}}
|
||||
]
|
||||
{{/openAPI}}
|
@ -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 = (<any>"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 = (<any>"Pet" !== "string") || requestContext.getHeaders()['Content-Type'] === 'application/json';
|
||||
|
@ -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 = (<any>"Order" !== "string") || requestContext.getHeaders()['Content-Type'] === 'application/json';
|
||||
const serializedBody = needsSerialization ? JSON.stringify(order || {}) : (order.toString() || ""); // TODO: `toString` call is unnecessary
|
||||
|
@ -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 = (<any>"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 = (<any>"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 = (<any>"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 = (<any>"User" !== "string") || requestContext.getHeaders()['Content-Type'] === 'application/json';
|
||||
const serializedBody = needsSerialization ? JSON.stringify(user || {}) : (user.toString() || ""); // TODO: `toString` call is unnecessary
|
||||
|
@ -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<any>;
|
||||
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<any>;
|
||||
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);
|
||||
|
@ -4,5 +4,3 @@ export interface Middleware {
|
||||
pre(context: RequestContext): Promise<RequestContext>;
|
||||
post(context: ResponseContext): Promise<ResponseContext>;
|
||||
}
|
||||
|
||||
// TODO: package.json set npmName
|
@ -1,15 +1,36 @@
|
||||
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 {
|
||||
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", { })
|
||||
|
Loading…
x
Reference in New Issue
Block a user