@@ -12,16 +12,16 @@ import { BaseServerConfiguration, server1 } from "./servers{{importFileExtension
1212import { configureAuthMethods, AuthMethods, AuthMethodsConfiguration } from "./auth/auth{ {importFileExtension} }";
1313
1414export interface Configuration<M = Middleware > {
15- readonly baseServer: BaseServerConfiguration;
16- readonly httpApi: HttpLibrary;
17- readonly middleware: M[];
18- readonly authMethods: AuthMethods;
15+ readonly baseServer: BaseServerConfiguration;
16+ readonly httpApi: HttpLibrary;
17+ readonly middleware: M[];
18+ readonly authMethods: AuthMethods;
1919}
2020
2121// Additional option specific to middleware merge strategy
2222export interface MiddlewareMergeOptions {
2323 // default is `' replace' ` for backwards compatibility
24- middlewareMergeStrategy?: ' replace' | ' append' | ' prepend' ;
24+ middlewareMergeStrategy?: " replace" | " append" | " prepend" ;
2525}
2626
2727// Unify configuration options using Partial plus extra merge strategy
@@ -36,44 +36,44 @@ export type PromiseConfigurationOptions = ConfigurationOptions<PromiseMiddleware
3636 */
3737export interface ConfigurationParameters {
3838 /**
39- * Default server to use - a list of available servers (according to the
39+ * Default server to use - a list of available servers (according to the
4040 * OpenAPI yaml definition) is included in the `servers` const in `./servers`. You can also
41- * create your own server with the `ServerConfiguration` class from the same
41+ * create your own server with the `ServerConfiguration` class from the same
4242 * file.
4343 */
4444 baseServer?: BaseServerConfiguration;
4545 /**
46- * HTTP library to use e.g. IsomorphicFetch. This can usually be skipped as
46+ * HTTP library to use e.g. IsomorphicFetch. This can usually be skipped as
4747 * all generators come with a default library.
4848 * If available, additional libraries can be imported from `./http/*`
4949 */
5050 httpApi?: HttpLibrary;
5151
5252 /**
53- * The middlewares which will be applied to requests and responses. You can
54- * add any number of middleware components to modify requests before they
53+ * The middlewares which will be applied to requests and responses. You can
54+ * add any number of middleware components to modify requests before they
5555 * are sent or before they are deserialized by implementing the `Middleware`
5656 * interface defined in `./middleware`
5757 */
5858 middleware?: Middleware[];
5959 /**
60- * Configures middleware functions that return promises instead of
61- * Observables (which are used by `middleware`). Otherwise allows for the
62- * same functionality as `middleware`, i.e., modifying requests before they
60+ * Configures middleware functions that return promises instead of
61+ * Observables (which are used by `middleware`). Otherwise allows for the
62+ * same functionality as `middleware`, i.e., modifying requests before they
6363 * are sent and before they are deserialized.
6464 */
6565 promiseMiddleware?: PromiseMiddleware[];
6666 /**
67- * Configuration for the available authentication methods (e.g., api keys)
68- * according to the OpenAPI yaml definition. For the definition, please refer to
67+ * Configuration for the available authentication methods (e.g., api keys)
68+ * according to the OpenAPI yaml definition. For the definition, please refer to
6969 * `./auth/auth`
7070 */
7171 authMethods?: AuthMethodsConfiguration
7272}
7373
7474/**
7575 * Provide your `ConfigurationParameters` to this function to get a `Configuration`
76- * object that can be used to configure your APIs (in the constructor or
76+ * object that can be used to configure your APIs (in the constructor or
7777 * for each request individually).
7878 *
7979 * If a property is not included in conf, a default is used:
@@ -99,3 +99,60 @@ export function createConfiguration(conf: ConfigurationParameters = {}): Configu
9999 }
100100 return configuration;
101101}
102+
103+ /**
104+ * Merge configuration options into a configuration.
105+ */
106+ { {#useInversify} }
107+ export function mergeConfiguration(conf: Configuration, options?: Configuration): Configuration {
108+ return options || conf;
109+ }
110+ { {/useInversify} }
111+ { {^useInversify} }
112+ export function mergeConfiguration(conf: Configuration, options?: ConfigurationOptions): Configuration {
113+ if (! options) {
114+ return conf;
115+ }
116+ let allMiddleware: Middleware[] = [];
117+ return {
118+ baseServer: options.baseServer || conf.baseServer,
119+ httpApi: options.httpApi || conf.httpApi,
120+ authMethods: options.authMethods || conf.authMethods,
121+ middleware: mergeMiddleware(conf.middleware, options?.middleware, options?.middlewareMergeStrategy),
122+ } ;
123+ }
124+
125+ function mergeMiddleware(staticMiddleware: Middleware[], calltimeMiddleware?: Middleware[], strategy?: "append" | "prepend" | "replace") {
126+ if (! calltimeMiddleware) {
127+ return staticMiddleware;
128+ }
129+ // default to replace behavior
130+ switch(strategy ?? "replace") {
131+ case " append" :
132+ return staticMiddleware.concat(calltimeMiddleware);
133+ case " prepend" :
134+ return calltimeMiddleware.concat(staticMiddleware)
135+ case " replace" :
136+ return calltimeMiddleware
137+ }
138+ throw new Error(`Unrecognized middleware merge strategy '${ strategy} '`)
139+ }
140+ { {/useInversify} }
141+
142+ /**
143+ * Convert Promise-based configuration options to Observable-based configuration options.
144+ */
145+ export function wrapOptions(options?: PromiseConfigurationOptions): StandardConfigurationOptions | undefined {
146+ if (! options) {
147+ return;
148+ }
149+ return {
150+ baseServer: options.baseServer,
151+ httpApi: options.httpApi,
152+ middleware: options.middleware?.map(
153+ m => new PromiseMiddlewareWrapper(m)
154+ ),
155+ middlewareMergeStrategy: options.middlewareMergeStrategy,
156+ authMethods: options.authMethods
157+ }
158+ }
0 commit comments