diff --git a/package-lock.json b/package-lock.json index 5f50193a0..54cafff22 100644 --- a/package-lock.json +++ b/package-lock.json @@ -728,9 +728,9 @@ "license": "MIT" }, "node_modules/cross-spawn": { - "version": "6.0.5", - "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-6.0.5.tgz", - "integrity": "sha512-eTVLrBSt7fjbDygz805pMnstIs2VTBNkRm0qxZd+M7A5XDdxVRWO5MxGBXZhjY4cqLYLdtrGqRf8mBPmzwSpWQ==", + "version": "6.0.6", + "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-6.0.6.tgz", + "integrity": "sha512-VqCUuhcd1iB+dsv8gxPttb5iZh/D0iubSP21g36KXdEuf6I5JiioesUVjpCdHV9MZRUfVFlvwtIUyPfxo5trtw==", "dev": true, "license": "MIT", "dependencies": { diff --git a/src/configuration/ApiClientConfigurator.ts b/src/configuration/ApiClientConfigurator.ts index 1461c951c..ecfa77dff 100644 --- a/src/configuration/ApiClientConfigurator.ts +++ b/src/configuration/ApiClientConfigurator.ts @@ -112,9 +112,50 @@ export class ApiClientConfigurator { ) } + if (config.middleware) { + middleware.push( + ...this.getCustomMiddleware< + RequestContextType, + ResponseContextType, + ObservableRequestContextType, + ObservableResponseContextType + >(config, observableRequestContextParam, observableResponseContextParam), + ) + } + return middleware } + protected static getCustomMiddleware< + RequestContextType extends IRequestContext, + ResponseContextType, + ObservableRequestContextType, + ObservableResponseContextType, + >( + config: IConfiguration, + observableRequestContextParam: new (promise: Promise) => ObservableRequestContextType, + observableResponseContextParam: new (promise: Promise) => ObservableResponseContextType, + ) { + return ( + config.middleware + ?.filter((m) => m.pre || m.post) + .map((m) => ({ + pre: (context: RequestContextType): ObservableRequestContextType => { + if (m.pre && typeof m.pre === 'function') { + return new observableRequestContextParam(Promise.resolve(m.pre(context) as RequestContextType)) + } + return new observableRequestContextParam(Promise.resolve(context)) + }, + post: (context: ResponseContextType): ObservableResponseContextType => { + if (m.post && typeof m.post === 'function') { + return new observableResponseContextParam(Promise.resolve(m.post(context) as ResponseContextType)) + } + return new observableResponseContextParam(Promise.resolve(context)) + }, + })) ?? [] + ) + } + protected static getHeaderMiddleware< RequestContextType extends IRequestContext, ResponseContextType, diff --git a/src/configuration/IConfiguration.ts b/src/configuration/IConfiguration.ts index b9da99c30..bdea190b5 100644 --- a/src/configuration/IConfiguration.ts +++ b/src/configuration/IConfiguration.ts @@ -12,4 +12,8 @@ export default interface IConfiguration { limiterOptions?: Bottleneck.ConstructorOptions limiterJobOptions?: Bottleneck.JobOptions httpAgent?: http.Agent | https.Agent + middleware?: Array<{ + pre(ctx: unknown): unknown + post(ctx: unknown): unknown + }> }