diff --git a/modules/openapi-generator/src/main/resources/typescript/api/middleware.mustache b/modules/openapi-generator/src/main/resources/typescript/api/middleware.mustache index 5e6d88eeeb1..89a895a32b4 100644 --- a/modules/openapi-generator/src/main/resources/typescript/api/middleware.mustache +++ b/modules/openapi-generator/src/main/resources/typescript/api/middleware.mustache @@ -1,7 +1,28 @@ import {RequestContext, ResponseContext} from './http/http'; -import { Observable } from 'rxjs'; +import { Observable, from } from 'rxjs'; export interface Middleware { pre(context: RequestContext): Observable; post(context: ResponseContext): Observable; +} + +export class PromiseMiddlewareWrapper implements Middleware { + + public constructor(private middleware: PromiseMiddleware) { + + } + + pre(context: RequestContext): Observable { + return from(this.middleware.pre(context)); + } + + post(context: ResponseContext): Observable { + return from(this.middleware.post(context)); + } + +} + +export interface PromiseMiddleware { + pre(context: RequestContext): Observable; + post(context: ResponseContext): Observable; } \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/typescript/configuration.mustache b/modules/openapi-generator/src/main/resources/typescript/configuration.mustache index 41d7a075f51..c31ae8fb359 100644 --- a/modules/openapi-generator/src/main/resources/typescript/configuration.mustache +++ b/modules/openapi-generator/src/main/resources/typescript/configuration.mustache @@ -1,5 +1,5 @@ import {HttpLibrary} from './http/http'; -import {Middleware} from './middleware'; +import {Middleware, PromiseMiddleware, PromiseMiddlewareWrapper} from './middleware'; import {IsomorphicFetchHttpLibrary} from "./http/isomorphic-fetch"; import {ServerConfiguration, server1} from './servers'; import {configureAuthMethods, AuthMethods, AuthMethodsConfiguration} from './auth/auth'; @@ -9,6 +9,7 @@ export interface ConfigurationParameters { baseServer?: ServerConfiguration; httpApi?: HttpLibrary; // override for fetch implementation middleware?: Middleware[]; // middleware to apply before/after fetch requests + promiseMiddleware?: PromiseMiddleware[]; authMethods?: AuthMethodsConfiguration } @@ -24,5 +25,8 @@ export class Configuration { this.httpApi = conf.httpApi || new IsomorphicFetchHttpLibrary(); // TODO: replace with window.fetch? this.middleware = conf.middleware || []; this.authMethods = configureAuthMethods(conf.authMethods); + if (conf.promiseMiddleware) { + conf.promiseMiddleware.forEach(m => this.middleware.push(new PromiseMiddlewareWrapper(m))); + } } } \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/typescript/generators/fetch.mustache b/modules/openapi-generator/src/main/resources/typescript/generators/fetch.mustache index e17b67f2a6c..ca1c5ff976f 100644 --- a/modules/openapi-generator/src/main/resources/typescript/generators/fetch.mustache +++ b/modules/openapi-generator/src/main/resources/typescript/generators/fetch.mustache @@ -1,6 +1,6 @@ import * as http from './http/http'; import * as auth from './auth/auth'; -import {Middleware} from './middleware'; +import {Middleware, PromiseMiddleware} from './middleware'; import * as models from './models/all'; import { Configuration} from './configuration' import * as apis from './types/PromiseAPI'; @@ -10,7 +10,8 @@ export { http, auth, - Middleware, + Middleware, + PromiseMiddleware, models, Configuration, apis, diff --git a/samples/client/petstore/typescript/builds/default/configuration.ts b/samples/client/petstore/typescript/builds/default/configuration.ts index 41d7a075f51..c31ae8fb359 100644 --- a/samples/client/petstore/typescript/builds/default/configuration.ts +++ b/samples/client/petstore/typescript/builds/default/configuration.ts @@ -1,5 +1,5 @@ import {HttpLibrary} from './http/http'; -import {Middleware} from './middleware'; +import {Middleware, PromiseMiddleware, PromiseMiddlewareWrapper} from './middleware'; import {IsomorphicFetchHttpLibrary} from "./http/isomorphic-fetch"; import {ServerConfiguration, server1} from './servers'; import {configureAuthMethods, AuthMethods, AuthMethodsConfiguration} from './auth/auth'; @@ -9,6 +9,7 @@ export interface ConfigurationParameters { baseServer?: ServerConfiguration; httpApi?: HttpLibrary; // override for fetch implementation middleware?: Middleware[]; // middleware to apply before/after fetch requests + promiseMiddleware?: PromiseMiddleware[]; authMethods?: AuthMethodsConfiguration } @@ -24,5 +25,8 @@ export class Configuration { this.httpApi = conf.httpApi || new IsomorphicFetchHttpLibrary(); // TODO: replace with window.fetch? this.middleware = conf.middleware || []; this.authMethods = configureAuthMethods(conf.authMethods); + if (conf.promiseMiddleware) { + conf.promiseMiddleware.forEach(m => this.middleware.push(new PromiseMiddlewareWrapper(m))); + } } } \ No newline at end of file diff --git a/samples/client/petstore/typescript/builds/default/index.ts b/samples/client/petstore/typescript/builds/default/index.ts index e17b67f2a6c..ca1c5ff976f 100644 --- a/samples/client/petstore/typescript/builds/default/index.ts +++ b/samples/client/petstore/typescript/builds/default/index.ts @@ -1,6 +1,6 @@ import * as http from './http/http'; import * as auth from './auth/auth'; -import {Middleware} from './middleware'; +import {Middleware, PromiseMiddleware} from './middleware'; import * as models from './models/all'; import { Configuration} from './configuration' import * as apis from './types/PromiseAPI'; @@ -10,7 +10,8 @@ export { http, auth, - Middleware, + Middleware, + PromiseMiddleware, models, Configuration, apis, diff --git a/samples/client/petstore/typescript/builds/default/middleware.ts b/samples/client/petstore/typescript/builds/default/middleware.ts index 5e6d88eeeb1..89a895a32b4 100644 --- a/samples/client/petstore/typescript/builds/default/middleware.ts +++ b/samples/client/petstore/typescript/builds/default/middleware.ts @@ -1,7 +1,28 @@ import {RequestContext, ResponseContext} from './http/http'; -import { Observable } from 'rxjs'; +import { Observable, from } from 'rxjs'; export interface Middleware { pre(context: RequestContext): Observable; post(context: ResponseContext): Observable; +} + +export class PromiseMiddlewareWrapper implements Middleware { + + public constructor(private middleware: PromiseMiddleware) { + + } + + pre(context: RequestContext): Observable { + return from(this.middleware.pre(context)); + } + + post(context: ResponseContext): Observable { + return from(this.middleware.post(context)); + } + +} + +export interface PromiseMiddleware { + pre(context: RequestContext): Observable; + post(context: ResponseContext): Observable; } \ No newline at end of file