-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
[typescript-rxjs] add support for raw response and progressSubscriber #5465
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
Merged
macjohnny
merged 30 commits into
OpenAPITools:master
from
denyo:feature/rxjs-statuscode-and-progress
Jun 29, 2020
Merged
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
f338fce
feat(typescript-rxjs): add support for returning statusCode and progr…
denyo c1923af
feat(typescript-rxjs): use ?? instead of || to support relative baseP…
denyo 00cd0da
feat(typescript-rxjs): regenerate samples
denyo 60e460e
refactor(typescript-rxjs): change explicit undefined checks to shorth…
denyo 45b4fb7
feat(typescript-rxjs): add missing progressSubscriber key when buildi…
denyo 92eb049
feat(typescript-rxjs): regenerate samples
denyo ce40312
style(typescript-rxjs): remove whitespace, add colons
denyo 7056095
feat(typescript-rxjs): regenerate samples
denyo 673d67c
refactor(typescript-rxjs): destructure configuration in BaseApi
denyo 9502f2f
fix(typescript-rxjs): returning empty string for apiKey and accessToken
denyo a5c5a86
feat(typescript-rxjs): replace withStatusCode option with response = …
denyo dfdb999
feat(typescript-rxjs): regenerate samples
denyo 4d89d41
Merge branch 'master' into feature/rxjs-statuscode-and-progress
denyo c6091fd
Merge branch 'master' into feature/rxjs-statuscode-and-progress
denyo d9b688a
Merge branch 'master' into feature/rxjs-statuscode-and-progress
denyo 59420b4
Merge branch 'master' into feature/rxjs-statuscode-and-progress
denyo 0b71548
Prints out the parameter name in throwIfNullOrUndefined
jvandort 7ddf3ad
Fixed misspelling
jvandort 3279d2a
Merge branch 'master' into feature/rxjs-statuscode-and-progress
denyo e131635
feat(typescript-rxjs): add withProgressSubscriber additional-properti…
denyo 77b56b6
refactor(typescript-rxjs): use backticks instead of String constructo…
denyo b826928
feat(typescript-rxjs): replace Object.keys() with Object.entries() in…
denyo 9cea1f7
style(typescript-rxjs): improve indentation of new withProgressSubscr…
denyo 5ca8f47
feat(typescript-rxjs): use entire es2017 lib in tsconfig.json for bui…
denyo 7d895d6
feat(typescript-rxjs): regenerate samples
denyo 3aff87c
Merge branch 'master' into feature/rxjs-cli-option-for-progressSubscr…
denyo 36fca0d
Merge branch 'tx_rxjs_print_param_name' into feature/rxjs-cli-option-…
denyo 7f22d95
Merge branch 'master' into feature/rxjs-cli-option-for-progressSubscr…
denyo c22297b
feat(typescript-rxjs): adjust sample generation, regenerate samples
denyo 68292d7
docs(typescript-rxjs): regenerate docs
denyo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
4 changes: 2 additions & 2 deletions
4
...figs/typescript-rxjs-with-interfaces.yaml → ...script-rxjs-with-progress-subscriber.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| generatorName: typescript-rxjs | ||
| outputDir: samples/client/petstore/typescript-rxjs/builds/with-interfaces | ||
| outputDir: samples/client/petstore/typescript-rxjs/builds/with-progress-subscriber | ||
| inputSpec: modules/openapi-generator/src/test/resources/2_0/petstore.yaml | ||
| additionalProperties: | ||
| withInterfaces: "true" | ||
| withProgressSubscriber: "true" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| // tslint:disable | ||
| {{>licenseInfo}} | ||
| import { Observable, of } from 'rxjs'; | ||
| import { Observable, of, Subscriber } from 'rxjs'; | ||
| import { ajax, AjaxRequest, AjaxResponse } from 'rxjs/ajax'; | ||
| import { map, concatMap } from 'rxjs/operators'; | ||
|
|
||
|
|
@@ -19,11 +19,11 @@ export class Configuration { | |
| constructor(private configuration: ConfigurationParameters = {}) {} | ||
|
|
||
| get basePath(): string { | ||
| return this.configuration.basePath || BASE_PATH; | ||
| return this.configuration.basePath ?? BASE_PATH; | ||
| } | ||
|
|
||
| get middleware(): Middleware[] { | ||
| return this.configuration.middleware || []; | ||
| return this.configuration.middleware ?? []; | ||
| } | ||
|
|
||
| get username(): string | undefined { | ||
|
|
@@ -36,18 +36,12 @@ export class Configuration { | |
|
|
||
| get apiKey(): ((name: string) => string) | undefined { | ||
| const { apiKey } = this.configuration; | ||
| if (!apiKey) { | ||
| return undefined; | ||
| } | ||
| return typeof apiKey === 'string' ? () => apiKey : apiKey; | ||
| return apiKey ? (typeof apiKey === 'string' ? () => apiKey : apiKey) : undefined; | ||
| } | ||
|
|
||
| get accessToken(): ((name: string, scopes?: string[]) => string) | undefined { | ||
| const { accessToken } = this.configuration; | ||
| if (!accessToken) { | ||
| return undefined; | ||
| } | ||
| return typeof accessToken === 'string' ? () => accessToken : accessToken; | ||
| return accessToken ? (typeof accessToken === 'string' ? () => accessToken : accessToken) : undefined; | ||
|
Contributor
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. Double ternary is problematic, as I described above. |
||
| } | ||
| } | ||
|
|
||
|
|
@@ -73,31 +67,35 @@ export class BaseAPI { | |
| withPostMiddleware = (postMiddlewares: Array<Middleware['post']>) => | ||
| this.withMiddleware(postMiddlewares.map((post) => ({ post }))); | ||
|
|
||
| protected request = <T>(requestOpts: RequestOpts): Observable<T> => | ||
| this.rxjsRequest(this.createRequestArgs(requestOpts)).pipe( | ||
| protected request<T>(requestOpts: RequestOpts): Observable<T> | ||
| protected request<T>(requestOpts: RequestOpts, responseOpts?: ResponseOpts): Observable<RawAjaxResponse<T>> | ||
| protected request<T>(requestOpts: RequestOpts, responseOpts?: ResponseOpts): Observable<T | RawAjaxResponse<T>> { | ||
| return this.rxjsRequest(this.createRequestArgs(requestOpts)).pipe( | ||
| map((res) => { | ||
| if (res.status >= 200 && res.status < 300) { | ||
| return res.response as T; | ||
| const { status, response } = res; | ||
| if (status >= 200 && status < 300) { | ||
| return responseOpts?.respone === 'raw' ? res : response; | ||
| } | ||
| throw res; | ||
| }) | ||
| ); | ||
| } | ||
|
|
||
| private createRequestArgs = (requestOpts: RequestOpts): RequestArgs => { | ||
| let url = this.configuration.basePath + requestOpts.path; | ||
| if (requestOpts.query !== undefined && Object.keys(requestOpts.query).length !== 0) { | ||
| // only add the queryString to the URL if there are query parameters. | ||
| // this is done to avoid urls ending with a '?' character which buggy webservers | ||
| // do not handle correctly sometimes. | ||
| url += '?' + queryString(requestOpts.query); | ||
| } | ||
| private createRequestArgs = ({ url: baseUrl, query, method, headers, body, responseType{{#withProgressSubscriber}}, progressSubscriber{{/withProgressSubscriber}} }: RequestOpts): RequestArgs => { | ||
| // only add the queryString to the URL if there are query parameters. | ||
| // this is done to avoid urls ending with a '?' character which buggy webservers | ||
| // do not handle correctly sometimes. | ||
| const url = `${this.configuration.basePath}${baseUrl}${query && Object.keys(query).length ? `?${queryString(query)}`: ''}`; | ||
|
|
||
| return { | ||
| url, | ||
| method: requestOpts.method, | ||
| headers: requestOpts.headers, | ||
| body: requestOpts.body instanceof FormData ? requestOpts.body : JSON.stringify(requestOpts.body), | ||
| responseType: requestOpts.responseType || 'json', | ||
| method, | ||
| headers, | ||
| body: body instanceof FormData ? body : JSON.stringify(body), | ||
| responseType: responseType ?? 'json', | ||
| {{#withProgressSubscriber}} | ||
| progressSubscriber, | ||
| {{/withProgressSubscriber}} | ||
| }; | ||
| } | ||
|
|
||
|
|
@@ -146,24 +144,41 @@ export type HttpHeaders = { [key: string]: string }; | |
| export type HttpQuery = Partial<{ [key: string]: string | number | null | boolean | Array<string | number | null | boolean> }>; // partial is needed for strict mode | ||
| export type HttpBody = Json | FormData; | ||
|
|
||
| export interface RequestOpts { | ||
| path: string; | ||
| export interface RequestOpts extends AjaxRequest { | ||
| query?: HttpQuery; // additional prop | ||
| // the following props have improved types over AjaxRequest | ||
| method: HttpMethod; | ||
| headers?: HttpHeaders; | ||
| query?: HttpQuery; | ||
| body?: HttpBody; | ||
| responseType?: 'json' | 'blob' | 'arraybuffer' | 'text'; | ||
| {{#withProgressSubscriber}} | ||
| progressSubscriber?: Subscriber<ProgressEvent>; | ||
| {{/withProgressSubscriber}} | ||
| } | ||
|
|
||
| export interface ResponseOpts { | ||
| respone?: 'raw'; | ||
| } | ||
|
|
||
| export interface OperationOpts { | ||
| {{#withProgressSubscriber}} | ||
| progressSubscriber?: Subscriber<ProgressEvent>; | ||
| {{/withProgressSubscriber}} | ||
| responseOpts?: ResponseOpts; | ||
| } | ||
|
|
||
| // AjaxResponse with typed response | ||
| export interface RawAjaxResponse<T> extends AjaxResponse { | ||
| response: T; | ||
| } | ||
|
|
||
| export const encodeURI = (value: any) => encodeURIComponent(String(value)); | ||
| export const encodeURI = (value: any) => encodeURIComponent(`${value}`); | ||
|
|
||
| const queryString = (params: HttpQuery): string => Object.keys(params) | ||
| .map((key) => { | ||
| const value = params[key]; | ||
| return (value instanceof Array) | ||
| ? value.map((val) => `${encodeURI(key)}=${encodeURI(val)}`).join('&') | ||
| : `${encodeURI(key)}=${encodeURI(value)}`; | ||
| }) | ||
| const queryString = (params: HttpQuery): string => Object.entries(params) | ||
| .map(([key, value]) => value instanceof Array | ||
| ? value.map((val) => `${encodeURI(key)}=${encodeURI(val)}`).join('&') | ||
| : `${encodeURI(key)}=${encodeURI(value)}` | ||
| ) | ||
| .join('&'); | ||
|
|
||
| // alias fallback for not being a breaking change | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I don't think double ternary is appropriate there. It's obfuscating the conditional, it makes it less readable. I've addressed the nully coalescing in #5329, and I think webpack can take care of the compressing the code.