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

[typescript] add call-time middleware support #20430

Open
wants to merge 10 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
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// TODO: better import syntax?
import {BaseAPIRequestFactory, RequiredError, COLLECTION_FORMATS} from './baseapi{{importFileExtension}}';
import {Configuration} from '../configuration{{importFileExtension}}';
{{^useInversify}}
import {Middleware} from '../middleware';
{{/useInversify}}
import {RequestContext, HttpMethod, ResponseContext, HttpFile, HttpInfo} from '../http/http{{importFileExtension}}';
{{#platforms}}
{{#node}}
Expand Down Expand Up @@ -42,8 +45,11 @@ export class {{classname}}RequestFactory extends BaseAPIRequestFactory {
* @param {{paramName}} {{description}}
{{/allParams}}
*/
public async {{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}_options?: Configuration): Promise<RequestContext> {
let _config = _options || this.configuration;
public async {{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}_options?: Configuration{{^useInversify}} | Middleware[]{{/useInversify}}): Promise<RequestContext> {
Copy link
Member

Choose a reason for hiding this comment

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

could it also be a separate parameter instead of turning _options into a type union? same for all similar changes in the other files

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, we're okay with users having to pass null/undefined to skip args? I can just put it after _options arg

Copy link
Member

Choose a reason for hiding this comment

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

we could also just destructure

args?: {middleware?: Middleware[]}

so when adding even more params in the future, we would be able to pass the ones we want by name

Copy link
Contributor Author

@davidgamero davidgamero Jan 21, 2025

Choose a reason for hiding this comment

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

That looks cleaner imo, and easier to consume.
Should the current configuration go in there too?

args?: {
  middleware?: Middleware[],
  configuration?: Configuration
}

Copy link
Member

Choose a reason for hiding this comment

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

that wouldn't be backwards compatible, so i would avoid it

let _config = this.configuration;
if (_options && !Array.isArray(_options)){
_config = _options
}
{{#allParams}}

{{#required}}
Expand Down Expand Up @@ -183,7 +189,7 @@ export class {{classname}}RequestFactory extends BaseAPIRequestFactory {
{{/authMethods}}

{{^useInversify}}
const defaultAuth: SecurityAuthentication | undefined = _options?.authMethods?.default || this.configuration?.authMethods?.default
const defaultAuth: SecurityAuthentication | undefined = _config?.authMethods?.default
if (defaultAuth?.applySecurityAuthentication) {
await defaultAuth?.applySecurityAuthentication(requestContext);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export { Middleware } from './middleware{{importFileExtension}}';
{{/useRxJS}}
{{^useRxJS}}
export type { PromiseMiddleware as Middleware } from './middleware{{importFileExtension}}';
export type { Middleware as ObservableMiddleware } from './middleware{{importFileExtension}}';
{{/useRxJS}}
{{#useObjectParameters}}
export { {{#apiInfo}}{{#apis}}{{#operations}}{{#operation}}type {{classname}}{{operationIdCamelCase}}Request, {{/operation}}Object{{classname}} as {{classname}}{{^-last}}, {{/-last}} {{/operations}}{{/apis}}{{/apiInfo}}} from './types/ObjectParamAPI{{importFileExtension}}';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import type { HttpFile } from "../http/http";
import type { Observable } from {{#useRxJS}}"rxjs"{{/useRxJS}}{{^useRxJS}}"../rxjsStub"{{/useRxJS}};
import type { Configuration } from "../configuration";
{{^useInversify}}
import type { Middleware } from "../middleware";
{{/useInversify}}

{{#models}}
{{#model}}
Expand All @@ -14,10 +17,10 @@ import { {{{ classname }}} } from "{{{ importPath }}}";

export abstract class AbstractObservable{{classname}} {
{{#operation}}
public abstract {{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}options?: Configuration): Observable<{{{returnType}}}{{^returnType}}void{{/returnType}}>;
public abstract {{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}options?: Configuration{{^useInversify}} | Middleware[]{{/useInversify}}): Observable<{{{returnType}}}{{^returnType}}void{{/returnType}}>;

{{/operation}}
}
{{/operations}}
{{/apis}}
{{/apiInfo}}
{{/apiInfo}}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { ResponseContext, RequestContext, HttpFile, HttpInfo } from '../http/http{{importFileExtension}}';
import { Configuration} from '../configuration{{importFileExtension}}'
{{^useInversify}}
import type { Middleware } from "../middleware";
{{/useInversify}}
{{#useRxJS}}
import { Observable } from 'rxjs';
{{/useRxJS}}
Expand Down Expand Up @@ -55,7 +58,7 @@ export class Object{{classname}} {
{{/summary}}
* @param param the request object
*/
public {{nickname}}WithHttpInfo(param: {{classname}}{{operationIdCamelCase}}Request{{^hasRequiredParams}} = {}{{/hasRequiredParams}}, options?: Configuration): {{#useRxJS}}Observable{{/useRxJS}}{{^useRxJS}}Promise{{/useRxJS}}<HttpInfo<{{{returnType}}}{{^returnType}}void{{/returnType}}>> {
public {{nickname}}WithHttpInfo(param: {{classname}}{{operationIdCamelCase}}Request{{^hasRequiredParams}} = {}{{/hasRequiredParams}}, options?: Configuration{{^useInversify}} | Middleware[]{{/useInversify}}): {{#useRxJS}}Observable{{/useRxJS}}{{^useRxJS}}Promise{{/useRxJS}}<HttpInfo<{{{returnType}}}{{^returnType}}void{{/returnType}}>> {
return this.api.{{nickname}}WithHttpInfo({{#allParams}}param.{{paramName}}, {{/allParams}} options){{^useRxJS}}.toPromise(){{/useRxJS}};
}

Expand All @@ -68,7 +71,7 @@ export class Object{{classname}} {
{{/summary}}
* @param param the request object
*/
public {{nickname}}(param: {{classname}}{{operationIdCamelCase}}Request{{^hasRequiredParams}} = {}{{/hasRequiredParams}}, options?: Configuration): {{#useRxJS}}Observable{{/useRxJS}}{{^useRxJS}}Promise{{/useRxJS}}<{{{returnType}}}{{^returnType}}void{{/returnType}}> {
public {{nickname}}(param: {{classname}}{{operationIdCamelCase}}Request{{^hasRequiredParams}} = {}{{/hasRequiredParams}}, options?: Configuration{{^useInversify}}| Middleware[]{{/useInversify}}): {{#useRxJS}}Observable{{/useRxJS}}{{^useRxJS}}Promise{{/useRxJS}}<{{{returnType}}}{{^returnType}}void{{/returnType}}> {
return this.api.{{nickname}}({{#allParams}}param.{{paramName}}, {{/allParams}} options){{^useRxJS}}.toPromise(){{/useRxJS}};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ResponseContext, RequestContext, HttpFile, HttpInfo } from '../http/http{{importFileExtension}}';
import { Configuration} from '../configuration{{importFileExtension}}'
import type { Middleware } from "../middleware";
import { Observable, of, from } from {{#useRxJS}}'rxjs'{{/useRxJS}}{{^useRxJS}}'../rxjsStub{{importFileExtension}}'{{/useRxJS}};
import {mergeMap, map} from {{#useRxJS}}'rxjs/operators'{{/useRxJS}}{{^useRxJS}}'../rxjsStub{{importFileExtension}}'{{/useRxJS}};
{{#useInversify}}
Expand Down Expand Up @@ -61,12 +62,21 @@ export class Observable{{classname}} {
* @param {{#required}}{{paramName}}{{/required}}{{^required}}[{{paramName}}]{{/required}}{{#description}} {{description}}{{/description}}
{{/allParams}}
*/
public {{nickname}}WithHttpInfo({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}_options?: Configuration): Observable<HttpInfo<{{{returnType}}}{{^returnType}}void{{/returnType}}>> {
public {{nickname}}WithHttpInfo({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}_options?: Configuration{{^useInversify}} | Middleware[]{{/useInversify}}): Observable<HttpInfo<{{{returnType}}}{{^returnType}}void{{/returnType}}>> {
let configuration = undefined
let calltimeMiddleware: Middleware[] = []
if (Array.isArray(_options)){
// call-time middleware provided
calltimeMiddleware = _options
}else{
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
}else{
} else {

configuration = _options
}
const requestContextPromise = this.requestFactory.{{nickname}}({{#allParams}}{{paramName}}, {{/allParams}}_options);

// build promise chain
let allMiddleware = this.configuration.middleware.concat(calltimeMiddleware)
let middlewarePreObservable = from<RequestContext>(requestContextPromise);
for (const middleware of this.configuration.middleware) {
for (const middleware of allMiddleware) {
middlewarePreObservable = middlewarePreObservable.pipe(mergeMap((ctx: RequestContext) => middleware.pre(ctx)));
}

Expand All @@ -91,7 +101,7 @@ export class Observable{{classname}} {
* @param {{#required}}{{paramName}}{{/required}}{{^required}}[{{paramName}}]{{/required}}{{#description}} {{description}}{{/description}}
{{/allParams}}
*/
public {{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}_options?: Configuration): Observable<{{{returnType}}}{{^returnType}}void{{/returnType}}> {
public {{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}_options?: Configuration{{^useInversify}} | Middleware[]{{/useInversify}}): Observable<{{{returnType}}}{{^returnType}}void{{/returnType}}> {
return this.{{nickname}}WithHttpInfo({{#allParams}}{{paramName}}, {{/allParams}}_options).pipe(map((apiResponse: HttpInfo<{{{returnType}}}{{^returnType}}void{{/returnType}}>) => apiResponse.data));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { ResponseContext, RequestContext, HttpFile, HttpInfo } from '../http/http{{importFileExtension}}';
import { Configuration} from '../configuration{{importFileExtension}}'
{{^useInversify}}
import { PromiseMiddleware, Middleware, PromiseMiddlewareWrapper } from "../middleware";
{{/useInversify}}
{{#useInversify}}
import { injectable, inject, optional } from "inversify";
import { AbstractConfiguration } from "../services/configuration";
Expand Down Expand Up @@ -51,8 +54,19 @@ export class Promise{{classname}} {
* @param {{#required}}{{paramName}}{{/required}}{{^required}}[{{paramName}}]{{/required}}{{#description}} {{description}}{{/description}}
{{/allParams}}
*/
public {{nickname}}WithHttpInfo({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}_options?: Configuration): Promise<HttpInfo<{{{returnType}}}{{^returnType}}void{{/returnType}}>> {
const result = this.api.{{nickname}}WithHttpInfo({{#allParams}}{{paramName}}, {{/allParams}}_options);
public {{nickname}}WithHttpInfo({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}_options?: Configuration{{^useInversify}} | PromiseMiddleware[]{{/useInversify}}): Promise<HttpInfo<{{{returnType}}}{{^returnType}}void{{/returnType}}>> {
{{^useInversify}}
let observableOptions: Configuration | undefined | Middleware[]
if (Array.isArray(_options)){
observableOptions = _options.map(m => new PromiseMiddlewareWrapper(m))
}else{
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
}else{
} else {

observableOptions = _options
}
{{/useInversify}}
{{#useInversify}}
const observableOptions = _options
{{/useInversify}}
const result = this.api.{{nickname}}WithHttpInfo({{#allParams}}{{paramName}}, {{/allParams}}observableOptions);
return result.toPromise();
}

Expand All @@ -67,8 +81,19 @@ export class Promise{{classname}} {
* @param {{#required}}{{paramName}}{{/required}}{{^required}}[{{paramName}}]{{/required}}{{#description}} {{description}}{{/description}}
{{/allParams}}
*/
public {{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}_options?: Configuration): Promise<{{{returnType}}}{{^returnType}}void{{/returnType}}> {
const result = this.api.{{nickname}}({{#allParams}}{{paramName}}, {{/allParams}}_options);
public {{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}_options?: Configuration{{^useInversify}} | PromiseMiddleware[]{{/useInversify}}): Promise<{{{returnType}}}{{^returnType}}void{{/returnType}}> {
{{^useInversify}}
let observableOptions: Configuration | undefined | Middleware[]
if (Array.isArray(_options)){
observableOptions = _options.map(m => new PromiseMiddlewareWrapper(m))
}else{
observableOptions = _options
}
{{/useInversify}}
{{#useInversify}}
const observableOptions = _options
{{/useInversify}}
const result = this.api.{{nickname}}({{#allParams}}{{paramName}}, {{/allParams}}observableOptions);
return result.toPromise();
}

Expand Down

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

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

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

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

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

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

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

Loading
Loading