forked from loafle/openapi-generator-original
Added promise based middleware
This commit is contained in:
parent
1d27563a41
commit
df970ae8b1
@ -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<RequestContext>;
|
||||
post(context: ResponseContext): Observable<ResponseContext>;
|
||||
}
|
||||
|
||||
export class PromiseMiddlewareWrapper implements Middleware {
|
||||
|
||||
public constructor(private middleware: PromiseMiddleware) {
|
||||
|
||||
}
|
||||
|
||||
pre(context: RequestContext): Observable<RequestContext> {
|
||||
return from(this.middleware.pre(context));
|
||||
}
|
||||
|
||||
post(context: ResponseContext): Observable<ResponseContext> {
|
||||
return from(this.middleware.post(context));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export interface PromiseMiddleware {
|
||||
pre(context: RequestContext): Observable<RequestContext>;
|
||||
post(context: ResponseContext): Observable<ResponseContext>;
|
||||
}
|
@ -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<any>;
|
||||
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)));
|
||||
}
|
||||
}
|
||||
}
|
@ -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,
|
||||
|
@ -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<any>;
|
||||
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)));
|
||||
}
|
||||
}
|
||||
}
|
@ -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,
|
||||
|
@ -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<RequestContext>;
|
||||
post(context: ResponseContext): Observable<ResponseContext>;
|
||||
}
|
||||
|
||||
export class PromiseMiddlewareWrapper implements Middleware {
|
||||
|
||||
public constructor(private middleware: PromiseMiddleware) {
|
||||
|
||||
}
|
||||
|
||||
pre(context: RequestContext): Observable<RequestContext> {
|
||||
return from(this.middleware.pre(context));
|
||||
}
|
||||
|
||||
post(context: ResponseContext): Observable<ResponseContext> {
|
||||
return from(this.middleware.post(context));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export interface PromiseMiddleware {
|
||||
pre(context: RequestContext): Observable<RequestContext>;
|
||||
post(context: ResponseContext): Observable<ResponseContext>;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user