Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ApiClientConfigurator: getCustomMiddleware #561

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 60 additions & 7 deletions src/configuration/ApiClientConfigurator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ import { IRequestContext } from '../services/IRequestContext'
import IConfiguration from './IConfiguration'
import { VERSION } from './version'

type MiddlewarePreBase<RequestContextType extends IRequestContext> = (context: RequestContextType) => RequestContextType
type MiddlewarePostBase<ResponseContextType> = (context: ResponseContextType) => ResponseContextType
export default interface IMiddlewareConfiguration<RequestContextType extends IRequestContext, ResponseContextType>
extends IConfiguration {
middleware?: Array<{
pre: MiddlewarePreBase<RequestContextType> | undefined
post: MiddlewarePostBase<ResponseContextType> | undefined
}>
}

export class ApiClientConfigurator {
public static getParams<
RequestContextType extends IRequestContext,
Expand All @@ -11,7 +21,7 @@ export class ApiClientConfigurator {
ObservableResponseContextType,
ServerConfiguration,
>(
config: IConfiguration,
config: IMiddlewareConfiguration<RequestContextType, ResponseContextType>,
serverConfigurationClass: new (
url: string,
variableConfiguration: { [key: string]: string },
Expand All @@ -38,7 +48,9 @@ export class ApiClientConfigurator {
return `hubspot-api-client-nodejs; ${VERSION}`
}

protected static getAuthMethods(config: IConfiguration) {
protected static getAuthMethods<RequestContextType extends IRequestContext, ResponseContextType>(
config: IMiddlewareConfiguration<RequestContextType, ResponseContextType>,
) {
let authMethods = {}

if (config.accessToken) {
Expand Down Expand Up @@ -69,8 +81,8 @@ export class ApiClientConfigurator {
return authMethods
}

protected static getBaseServer<ServerConfiguration>(
config: IConfiguration,
protected static getBaseServer<ServerConfiguration, RequestContextType extends IRequestContext, ResponseContextType>(
config: IMiddlewareConfiguration<RequestContextType, ResponseContextType>,
serverConfigurationClass: new (
url: string,
variableConfiguration: { [key: string]: string },
Expand All @@ -88,7 +100,7 @@ export class ApiClientConfigurator {
ObservableRequestContextType,
ObservableResponseContextType,
>(
config: IConfiguration,
config: IMiddlewareConfiguration<RequestContextType, ResponseContextType>,
observableRequestContextParam: new (promise: Promise<RequestContextType>) => ObservableRequestContextType,
observableResponseContextParam: new (promise: Promise<ResponseContextType>) => ObservableResponseContextType,
) {
Expand All @@ -112,16 +124,57 @@ 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: IMiddlewareConfiguration<RequestContextType, ResponseContextType>,
observableRequestContextParam: new (promise: Promise<RequestContextType>) => ObservableRequestContextType,
observableResponseContextParam: new (promise: Promise<ResponseContextType>) => 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)))
}
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)))
}
return new observableResponseContextParam(Promise.resolve(context))
},
})) ?? []
)
}

protected static getHeaderMiddleware<
RequestContextType extends IRequestContext,
ResponseContextType,
ObservableRequestContextType,
ObservableResponseContextType,
>(
config: IConfiguration,
config: IMiddlewareConfiguration<RequestContextType, ResponseContextType>,
observableRequestContextParam: new (promise: Promise<RequestContextType>) => ObservableRequestContextType,
observableResponseContextParam: new (promise: Promise<ResponseContextType>) => ObservableResponseContextType,
) {
Expand All @@ -148,7 +201,7 @@ export class ApiClientConfigurator {
ObservableRequestContextType,
ObservableResponseContextType,
>(
config: IConfiguration,
config: IMiddlewareConfiguration<RequestContextType, ResponseContextType>,
observableRequestContextParam: new (promise: Promise<RequestContextType>) => ObservableRequestContextType,
observableResponseContextParam: new (promise: Promise<ResponseContextType>) => ObservableResponseContextType,
) {
Expand Down