Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 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
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ import { BaseServerConfiguration, server1 } from "./servers{{importFileExtension
import { configureAuthMethods, AuthMethods, AuthMethodsConfiguration } from "./auth/auth{{importFileExtension}}";

export interface Configuration<M = Middleware> {
readonly baseServer: BaseServerConfiguration;
readonly httpApi: HttpLibrary;
readonly middleware: M[];
readonly authMethods: AuthMethods;
readonly baseServer: BaseServerConfiguration;
readonly httpApi: HttpLibrary;
readonly middleware: M[];
readonly authMethods: AuthMethods;
}

// Additional option specific to middleware merge strategy
export interface MiddlewareMergeOptions {
// default is `'replace'` for backwards compatibility
middlewareMergeStrategy?: 'replace' | 'append' | 'prepend';
// default is `"replace"` for backwards compatibility
middlewareMergeStrategy?: "replace" | "append" | "prepend";
}

// Unify configuration options using Partial plus extra merge strategy
Expand Down Expand Up @@ -105,43 +105,35 @@ export function createConfiguration(conf: ConfigurationParameters = {}): Configu
*/
{{#useInversify}}
export function mergeConfiguration(conf: Configuration, options?: Configuration): Configuration {
if (options) {
conf = options;
}
return conf;
return options || conf;
}
{{/useInversify}}
{{^useInversify}}
export function mergeConfiguration(conf: Configuration, options?: ConfigurationOptions): Configuration {
let allMiddleware: Middleware[] = [];
if (options && options.middleware) {
const middlewareMergeStrategy = options.middlewareMergeStrategy || "replace" // default to replace behavior
// call-time middleware provided
const calltimeMiddleware: Middleware[] = options.middleware;
if (!options) {
return conf;
}
return {
baseServer: options.baseServer || conf.baseServer,
httpApi: options.httpApi || conf.httpApi,
authMethods: options.authMethods || conf.authMethods,
middleware: mergeMiddleware(conf.middleware, options?.middleware, options?.middlewareMergeStrategy),
};
}

switch(middlewareMergeStrategy) {
function mergeMiddleware(staticMiddleware: Middleware[], calltimeMiddleware?: Middleware[], strategy: "append" | "prepend" | "replace" = "replace") {
if (!calltimeMiddleware) {
return staticMiddleware;
}
switch(strategy) {
case "append":
allMiddleware = conf.middleware.concat(calltimeMiddleware);
break;
return staticMiddleware.concat(calltimeMiddleware);
case "prepend":
allMiddleware = calltimeMiddleware.concat(conf.middleware)
break;
return calltimeMiddleware.concat(staticMiddleware)
case "replace":
allMiddleware = calltimeMiddleware
break;
default:
throw new Error(`unrecognized middleware merge strategy '${middlewareMergeStrategy}'`);
}
}
if (options) {
conf = {
baseServer: options.baseServer || conf.baseServer,
httpApi: options.httpApi || conf.httpApi,
authMethods: options.authMethods || conf.authMethods,
middleware: allMiddleware || conf.middleware
};
return calltimeMiddleware
}
return conf;
throw new Error(`Unrecognized middleware merge strategy '${strategy}'`)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you move this out because of the explicit return rule? Otherwise I think it was semantically easier to understand inside the switch.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It felt cleaner to me, because it made it obvious, that it is a last-resort for the function (actually in typescript it is unreachable anyway, because the switch completely covers all possible values of strategy)
Putting it into default makes sense as well because it is concerned with invalid values for strategy.
I can put it back into default: if you prefer.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer it for future readers/changes, to make sure noone puts code in between. It should immediately exit on an unknown value. If you're happy to move it back in, that would be appreciated.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. I moved it back inside.

}
{{/useInversify}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ export class Observable{{classname}} {
public {{nickname}}WithHttpInfo({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}_options?: Configuration{{^useInversify}}Options{{/useInversify}}): Observable<HttpInfo<{{{returnType}}}{{^returnType}}void{{/returnType}}>> {
const _config = mergeConfiguration(this.configuration, _options);

const requestContextPromise = this.requestFactory.{{nickname}}({{#allParams}}{{paramName}}, {{/allParams}}{{#useInversify}}_options{{/useInversify}}{{^useInversify}}_config{{/useInversify}});
const requestContextPromise = this.requestFactory.{{nickname}}({{#allParams}}{{paramName}}, {{/allParams}}_config);
// build promise chain
let middlewarePreObservable = from<RequestContext>(requestContextPromise);
for (const middleware of _config.middleware) {
middlewarePreObservable = middlewarePreObservable.pipe(mergeMap((ctx: RequestContext) => middleware.pre(ctx)));
}

return middlewarePreObservable.pipe(mergeMap((ctx: RequestContext) => this.configuration.httpApi.send(ctx))).
return middlewarePreObservable.pipe(mergeMap((ctx: RequestContext) => _config.httpApi.send(ctx))).
pipe(mergeMap((response: ResponseContext) => {
let middlewarePostObservable = of(response);
for (const middleware of _config.middleware.reverse()) {
Expand Down
55 changes: 25 additions & 30 deletions samples/client/echo_api/typescript/build/configuration.ts

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

Loading