-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
base: master
Are you sure you want to change the base?
Changes from all commits
6328625
9821963
852829d
126d7e6
53c55a6
47df511
4ecba42
58f001e
fc4fd1c
80b3629
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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}} | ||||||
|
@@ -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{ | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
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))); | ||||||
} | ||||||
|
||||||
|
@@ -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)); | ||||||
} | ||||||
|
||||||
|
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"; | ||||||
|
@@ -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{ | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
observableOptions = _options | ||||||
} | ||||||
{{/useInversify}} | ||||||
{{#useInversify}} | ||||||
const observableOptions = _options | ||||||
{{/useInversify}} | ||||||
const result = this.api.{{nickname}}WithHttpInfo({{#allParams}}{{paramName}}, {{/allParams}}observableOptions); | ||||||
return result.toPromise(); | ||||||
} | ||||||
|
||||||
|
@@ -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(); | ||||||
} | ||||||
|
||||||
|
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
so when adding even more params in the future, we would be able to pass the ones we want by name
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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