diff --git a/bin/configs/typescript-rxjs-with-interfaces.yaml b/bin/configs/typescript-rxjs-with-progress-subscriber.yaml similarity index 52% rename from bin/configs/typescript-rxjs-with-interfaces.yaml rename to bin/configs/typescript-rxjs-with-progress-subscriber.yaml index a5ef676abc7d..f20df704bec7 100644 --- a/bin/configs/typescript-rxjs-with-interfaces.yaml +++ b/bin/configs/typescript-rxjs-with-progress-subscriber.yaml @@ -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" diff --git a/docs/generators/typescript-rxjs.md b/docs/generators/typescript-rxjs.md index e48d7e4827ca..270ce2ad7e4e 100644 --- a/docs/generators/typescript-rxjs.md +++ b/docs/generators/typescript-rxjs.md @@ -21,7 +21,7 @@ sidebar_label: typescript-rxjs |sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true| |sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true| |supportsES6|Generate code that conforms to ES6.| |false| -|withInterfaces|Setting this property to true will generate interfaces next to the default class implementations.| |false| +|withProgressSubscriber|Setting this property to true will generate API controller methods with support for subscribing to request progress.| |false| ## IMPORT MAPPING diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptRxjsClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptRxjsClientCodegen.java index 7a740ab76e00..673d4d41e45b 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptRxjsClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptRxjsClientCodegen.java @@ -37,7 +37,7 @@ public class TypeScriptRxjsClientCodegen extends AbstractTypeScriptClientCodegen private static final Logger LOGGER = LoggerFactory.getLogger(AbstractTypeScriptClientCodegen.class); public static final String NPM_REPOSITORY = "npmRepository"; - public static final String WITH_INTERFACES = "withInterfaces"; + public static final String WITH_PROGRESS_SUBSCRIBER = "withProgressSubscriber"; protected String npmRepository = null; protected Set reservedParamNames = new HashSet<>(); @@ -60,7 +60,7 @@ public TypeScriptRxjsClientCodegen() { typeMapping.put("file", "Blob"); this.cliOptions.add(new CliOption(NPM_REPOSITORY, "Use this property to set an url your private npmRepo in the package.json")); - this.cliOptions.add(new CliOption(WITH_INTERFACES, "Setting this property to true will generate interfaces next to the default class implementations.", SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString())); + this.cliOptions.add(new CliOption(WITH_PROGRESS_SUBSCRIBER, "Setting this property to true will generate API controller methods with support for subscribing to request progress.", SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString())); // these are used in the api template for more efficient destructuring this.reservedParamNames.add("headers"); diff --git a/modules/openapi-generator/src/main/resources/typescript-rxjs/apis.mustache b/modules/openapi-generator/src/main/resources/typescript-rxjs/apis.mustache index 4aae18a02f0e..ab1847d6703a 100644 --- a/modules/openapi-generator/src/main/resources/typescript-rxjs/apis.mustache +++ b/modules/openapi-generator/src/main/resources/typescript-rxjs/apis.mustache @@ -1,7 +1,7 @@ // tslint:disable {{>licenseInfo}} import { Observable } from 'rxjs'; -import { BaseAPI{{#hasHttpHeaders}}, HttpHeaders{{/hasHttpHeaders}}{{#hasQueryParams}}, HttpQuery{{/hasQueryParams}}{{#hasRequiredParams}}, throwIfNullOrUndefined{{/hasRequiredParams}}{{#hasPathParams}}, encodeURI{{/hasPathParams}}{{#hasListContainers}}, COLLECTION_FORMATS{{/hasListContainers}} } from '../runtime'; +import { BaseAPI{{#hasHttpHeaders}}, HttpHeaders{{/hasHttpHeaders}}{{#hasQueryParams}}, HttpQuery{{/hasQueryParams}}{{#hasRequiredParams}}, throwIfNullOrUndefined{{/hasRequiredParams}}{{#hasPathParams}}, encodeURI{{/hasPathParams}}{{#hasListContainers}}, COLLECTION_FORMATS{{/hasListContainers}}, OperationOpts, RawAjaxResponse } from '../runtime'; {{#imports.0}} import { {{#imports}} @@ -37,7 +37,12 @@ export class {{classname}} extends BaseAPI { * {{&summary}} {{/summary}} */ - {{nickname}} = ({{#allParams.0}}{ {{#allParams}}{{paramName}}{{#vendorExtensions.x-param-name-alternative}}: {{vendorExtensions.x-param-name-alternative}}{{/vendorExtensions.x-param-name-alternative}}{{^-last}}, {{/-last}}{{/allParams}} }: {{operationIdCamelCase}}Request{{/allParams.0}}): Observable<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}> => { + {{nickname}}({{#allParams.0}}{ {{#allParams}}{{paramName}}{{#vendorExtensions.x-param-name-alternative}}: {{vendorExtensions.x-param-name-alternative}}{{/vendorExtensions.x-param-name-alternative}}{{^-last}}, {{/-last}}{{/allParams}} }: {{operationIdCamelCase}}Request{{/allParams.0}}): Observable<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}> +{{#withProgressSubscriber}} + {{nickname}}({{#allParams.0}}{ {{#allParams}}{{paramName}}{{#vendorExtensions.x-param-name-alternative}}: {{vendorExtensions.x-param-name-alternative}}{{/vendorExtensions.x-param-name-alternative}}{{^-last}}, {{/-last}}{{/allParams}} }: {{operationIdCamelCase}}Request, {{/allParams.0}}opts?: Pick): Observable<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}> +{{/withProgressSubscriber}} + {{nickname}}({{#allParams.0}}{ {{#allParams}}{{paramName}}{{#vendorExtensions.x-param-name-alternative}}: {{vendorExtensions.x-param-name-alternative}}{{/vendorExtensions.x-param-name-alternative}}{{^-last}}, {{/-last}}{{/allParams}} }: {{operationIdCamelCase}}Request, {{/allParams.0}}opts?: OperationOpts): Observable<{{#returnType}}RawAjaxResponse<{{{returnType}}}>{{/returnType}}{{^returnType}}void | RawAjaxResponse{{/returnType}}> + {{nickname}}({{#allParams.0}}{ {{#allParams}}{{paramName}}{{#vendorExtensions.x-param-name-alternative}}: {{vendorExtensions.x-param-name-alternative}}{{/vendorExtensions.x-param-name-alternative}}{{^-last}}, {{/-last}}{{/allParams}} }: {{operationIdCamelCase}}Request, {{/allParams.0}}opts?: OperationOpts): Observable<{{#returnType}}{{{returnType}}} | RawAjaxResponse<{{{returnType}}}>{{/returnType}}{{^returnType}}void | RawAjaxResponse{{/returnType}}> { {{#hasParams}} {{#allParams}} {{#required}} @@ -177,7 +182,7 @@ export class {{classname}} extends BaseAPI { {{/hasFormParams}} return this.request<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}>({ - path: '{{{path}}}'{{#pathParams}}.replace({{=<% %>=}}'{<%baseName%>}'<%={{ }}=%>, encodeURI({{> paramNamePartial}})){{/pathParams}}, + url: '{{{path}}}'{{#pathParams}}.replace({{=<% %>=}}'{<%baseName%>}'<%={{ }}=%>, encodeURI({{> paramNamePartial}})){{/pathParams}}, method: '{{httpMethod}}', {{#hasHttpHeaders}} headers, @@ -204,9 +209,12 @@ export class {{classname}} extends BaseAPI { body: formData, {{/hasFormParams}} {{#isResponseFile}} - responseType: 'blob' + responseType: 'blob', {{/isResponseFile}} - }); +{{#withProgressSubscriber}} + progressSubscriber: opts?.progressSubscriber, +{{/withProgressSubscriber}} + }, opts?.responseOpts); }; {{/operation}} diff --git a/modules/openapi-generator/src/main/resources/typescript-rxjs/package.mustache b/modules/openapi-generator/src/main/resources/typescript-rxjs/package.mustache index 89f0f03561c8..26c1529a3a2d 100644 --- a/modules/openapi-generator/src/main/resources/typescript-rxjs/package.mustache +++ b/modules/openapi-generator/src/main/resources/typescript-rxjs/package.mustache @@ -6,14 +6,14 @@ "main": "./dist/index.js", "typings": "./dist/index.d.ts", "scripts" : { - "build": "tsc --outDir dist/", + "build": "node_modules/.bin/tsc --outDir dist/", "prepare": "npm run build" }, "dependencies": { "rxjs": "^6.3.3" }, "devDependencies": { - "typescript": "^3.0.1" + "typescript": "^3.7" }{{#npmRepository}},{{/npmRepository}} {{#npmRepository}} "publishConfig": { diff --git a/modules/openapi-generator/src/main/resources/typescript-rxjs/runtime.mustache b/modules/openapi-generator/src/main/resources/typescript-rxjs/runtime.mustache index db845a859459..400f7df8eb9a 100644 --- a/modules/openapi-generator/src/main/resources/typescript-rxjs/runtime.mustache +++ b/modules/openapi-generator/src/main/resources/typescript-rxjs/runtime.mustache @@ -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; } } @@ -73,31 +67,35 @@ export class BaseAPI { withPostMiddleware = (postMiddlewares: Array) => this.withMiddleware(postMiddlewares.map((post) => ({ post }))); - protected request = (requestOpts: RequestOpts): Observable => - this.rxjsRequest(this.createRequestArgs(requestOpts)).pipe( + protected request(requestOpts: RequestOpts): Observable + protected request(requestOpts: RequestOpts, responseOpts?: ResponseOpts): Observable> + protected request(requestOpts: RequestOpts, responseOpts?: ResponseOpts): Observable> { + 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 }>; // 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; + {{/withProgressSubscriber}} +} + +export interface ResponseOpts { + respone?: 'raw'; +} + +export interface OperationOpts { + {{#withProgressSubscriber}} + progressSubscriber?: Subscriber; + {{/withProgressSubscriber}} + responseOpts?: ResponseOpts; +} + +// AjaxResponse with typed response +export interface RawAjaxResponse 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 diff --git a/modules/openapi-generator/src/main/resources/typescript-rxjs/tsconfig.mustache b/modules/openapi-generator/src/main/resources/typescript-rxjs/tsconfig.mustache index 328c7d4e141a..0779a8b0037f 100644 --- a/modules/openapi-generator/src/main/resources/typescript-rxjs/tsconfig.mustache +++ b/modules/openapi-generator/src/main/resources/typescript-rxjs/tsconfig.mustache @@ -6,12 +6,13 @@ "moduleResolution": "node", "outDir": "dist", "rootDir": ".", - {{^supportsES6}} "lib": [ + {{^supportsES6}} "es6", - "dom" + {{/supportsES6}} + "dom", + "es2017" ], - {{/supportsES6}} "typeRoots": [ "node_modules/@types" ] diff --git a/samples/client/petstore/typescript-rxjs/builds/default/apis/PetApi.ts b/samples/client/petstore/typescript-rxjs/builds/default/apis/PetApi.ts index 021781fd7abb..026381b12b2b 100644 --- a/samples/client/petstore/typescript-rxjs/builds/default/apis/PetApi.ts +++ b/samples/client/petstore/typescript-rxjs/builds/default/apis/PetApi.ts @@ -12,7 +12,7 @@ */ import { Observable } from 'rxjs'; -import { BaseAPI, HttpHeaders, HttpQuery, throwIfNullOrUndefined, encodeURI, COLLECTION_FORMATS } from '../runtime'; +import { BaseAPI, HttpHeaders, HttpQuery, throwIfNullOrUndefined, encodeURI, COLLECTION_FORMATS, OperationOpts, RawAjaxResponse } from '../runtime'; import { ApiResponse, Pet, @@ -63,7 +63,9 @@ export class PetApi extends BaseAPI { /** * Add a new pet to the store */ - addPet = ({ body }: AddPetRequest): Observable => { + addPet({ body }: AddPetRequest): Observable + addPet({ body }: AddPetRequest, opts?: OperationOpts): Observable> + addPet({ body }: AddPetRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(body, 'body', 'addPet'); const headers: HttpHeaders = { @@ -78,17 +80,19 @@ export class PetApi extends BaseAPI { }; return this.request({ - path: '/pet', + url: '/pet', method: 'POST', headers, body: body, - }); + }, opts?.responseOpts); }; /** * Deletes a pet */ - deletePet = ({ petId, apiKey }: DeletePetRequest): Observable => { + deletePet({ petId, apiKey }: DeletePetRequest): Observable + deletePet({ petId, apiKey }: DeletePetRequest, opts?: OperationOpts): Observable> + deletePet({ petId, apiKey }: DeletePetRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(petId, 'petId', 'deletePet'); const headers: HttpHeaders = { @@ -103,17 +107,19 @@ export class PetApi extends BaseAPI { }; return this.request({ - path: '/pet/{petId}'.replace('{petId}', encodeURI(petId)), + url: '/pet/{petId}'.replace('{petId}', encodeURI(petId)), method: 'DELETE', headers, - }); + }, opts?.responseOpts); }; /** * Multiple status values can be provided with comma separated strings * Finds Pets by status */ - findPetsByStatus = ({ status }: FindPetsByStatusRequest): Observable> => { + findPetsByStatus({ status }: FindPetsByStatusRequest): Observable> + findPetsByStatus({ status }: FindPetsByStatusRequest, opts?: OperationOpts): Observable>> + findPetsByStatus({ status }: FindPetsByStatusRequest, opts?: OperationOpts): Observable | RawAjaxResponse>> { throwIfNullOrUndefined(status, 'status', 'findPetsByStatus'); const headers: HttpHeaders = { @@ -131,18 +137,20 @@ export class PetApi extends BaseAPI { }; return this.request>({ - path: '/pet/findByStatus', + url: '/pet/findByStatus', method: 'GET', headers, query, - }); + }, opts?.responseOpts); }; /** * Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing. * Finds Pets by tags */ - findPetsByTags = ({ tags }: FindPetsByTagsRequest): Observable> => { + findPetsByTags({ tags }: FindPetsByTagsRequest): Observable> + findPetsByTags({ tags }: FindPetsByTagsRequest, opts?: OperationOpts): Observable>> + findPetsByTags({ tags }: FindPetsByTagsRequest, opts?: OperationOpts): Observable | RawAjaxResponse>> { throwIfNullOrUndefined(tags, 'tags', 'findPetsByTags'); const headers: HttpHeaders = { @@ -160,18 +168,20 @@ export class PetApi extends BaseAPI { }; return this.request>({ - path: '/pet/findByTags', + url: '/pet/findByTags', method: 'GET', headers, query, - }); + }, opts?.responseOpts); }; /** * Returns a single pet * Find pet by ID */ - getPetById = ({ petId }: GetPetByIdRequest): Observable => { + getPetById({ petId }: GetPetByIdRequest): Observable + getPetById({ petId }: GetPetByIdRequest, opts?: OperationOpts): Observable> + getPetById({ petId }: GetPetByIdRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(petId, 'petId', 'getPetById'); const headers: HttpHeaders = { @@ -179,16 +189,18 @@ export class PetApi extends BaseAPI { }; return this.request({ - path: '/pet/{petId}'.replace('{petId}', encodeURI(petId)), + url: '/pet/{petId}'.replace('{petId}', encodeURI(petId)), method: 'GET', headers, - }); + }, opts?.responseOpts); }; /** * Update an existing pet */ - updatePet = ({ body }: UpdatePetRequest): Observable => { + updatePet({ body }: UpdatePetRequest): Observable + updatePet({ body }: UpdatePetRequest, opts?: OperationOpts): Observable> + updatePet({ body }: UpdatePetRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(body, 'body', 'updatePet'); const headers: HttpHeaders = { @@ -203,17 +215,19 @@ export class PetApi extends BaseAPI { }; return this.request({ - path: '/pet', + url: '/pet', method: 'PUT', headers, body: body, - }); + }, opts?.responseOpts); }; /** * Updates a pet in the store with form data */ - updatePetWithForm = ({ petId, name, status }: UpdatePetWithFormRequest): Observable => { + updatePetWithForm({ petId, name, status }: UpdatePetWithFormRequest): Observable + updatePetWithForm({ petId, name, status }: UpdatePetWithFormRequest, opts?: OperationOpts): Observable> + updatePetWithForm({ petId, name, status }: UpdatePetWithFormRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(petId, 'petId', 'updatePetWithForm'); const headers: HttpHeaders = { @@ -231,17 +245,19 @@ export class PetApi extends BaseAPI { if (status !== undefined) { formData.append('status', status as any); } return this.request({ - path: '/pet/{petId}'.replace('{petId}', encodeURI(petId)), + url: '/pet/{petId}'.replace('{petId}', encodeURI(petId)), method: 'POST', headers, body: formData, - }); + }, opts?.responseOpts); }; /** * uploads an image */ - uploadFile = ({ petId, additionalMetadata, file }: UploadFileRequest): Observable => { + uploadFile({ petId, additionalMetadata, file }: UploadFileRequest): Observable + uploadFile({ petId, additionalMetadata, file }: UploadFileRequest, opts?: OperationOpts): Observable> + uploadFile({ petId, additionalMetadata, file }: UploadFileRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(petId, 'petId', 'uploadFile'); const headers: HttpHeaders = { @@ -259,11 +275,11 @@ export class PetApi extends BaseAPI { if (file !== undefined) { formData.append('file', file as any); } return this.request({ - path: '/pet/{petId}/uploadImage'.replace('{petId}', encodeURI(petId)), + url: '/pet/{petId}/uploadImage'.replace('{petId}', encodeURI(petId)), method: 'POST', headers, body: formData, - }); + }, opts?.responseOpts); }; } diff --git a/samples/client/petstore/typescript-rxjs/builds/default/apis/StoreApi.ts b/samples/client/petstore/typescript-rxjs/builds/default/apis/StoreApi.ts index 8ce04d16aa04..5575fe6c3d64 100644 --- a/samples/client/petstore/typescript-rxjs/builds/default/apis/StoreApi.ts +++ b/samples/client/petstore/typescript-rxjs/builds/default/apis/StoreApi.ts @@ -12,7 +12,7 @@ */ import { Observable } from 'rxjs'; -import { BaseAPI, HttpHeaders, throwIfNullOrUndefined, encodeURI } from '../runtime'; +import { BaseAPI, HttpHeaders, throwIfNullOrUndefined, encodeURI, OperationOpts, RawAjaxResponse } from '../runtime'; import { Order, } from '../models'; @@ -38,48 +38,56 @@ export class StoreApi extends BaseAPI { * For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors * Delete purchase order by ID */ - deleteOrder = ({ orderId }: DeleteOrderRequest): Observable => { + deleteOrder({ orderId }: DeleteOrderRequest): Observable + deleteOrder({ orderId }: DeleteOrderRequest, opts?: OperationOpts): Observable> + deleteOrder({ orderId }: DeleteOrderRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(orderId, 'orderId', 'deleteOrder'); return this.request({ - path: '/store/order/{orderId}'.replace('{orderId}', encodeURI(orderId)), + url: '/store/order/{orderId}'.replace('{orderId}', encodeURI(orderId)), method: 'DELETE', - }); + }, opts?.responseOpts); }; /** * Returns a map of status codes to quantities * Returns pet inventories by status */ - getInventory = (): Observable<{ [key: string]: number; }> => { + getInventory(): Observable<{ [key: string]: number; }> + getInventory(opts?: OperationOpts): Observable> + getInventory(opts?: OperationOpts): Observable<{ [key: string]: number; } | RawAjaxResponse<{ [key: string]: number; }>> { const headers: HttpHeaders = { ...(this.configuration.apiKey && { 'api_key': this.configuration.apiKey('api_key') }), // api_key authentication }; return this.request<{ [key: string]: number; }>({ - path: '/store/inventory', + url: '/store/inventory', method: 'GET', headers, - }); + }, opts?.responseOpts); }; /** * For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions * Find purchase order by ID */ - getOrderById = ({ orderId }: GetOrderByIdRequest): Observable => { + getOrderById({ orderId }: GetOrderByIdRequest): Observable + getOrderById({ orderId }: GetOrderByIdRequest, opts?: OperationOpts): Observable> + getOrderById({ orderId }: GetOrderByIdRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(orderId, 'orderId', 'getOrderById'); return this.request({ - path: '/store/order/{orderId}'.replace('{orderId}', encodeURI(orderId)), + url: '/store/order/{orderId}'.replace('{orderId}', encodeURI(orderId)), method: 'GET', - }); + }, opts?.responseOpts); }; /** * Place an order for a pet */ - placeOrder = ({ body }: PlaceOrderRequest): Observable => { + placeOrder({ body }: PlaceOrderRequest): Observable + placeOrder({ body }: PlaceOrderRequest, opts?: OperationOpts): Observable> + placeOrder({ body }: PlaceOrderRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(body, 'body', 'placeOrder'); const headers: HttpHeaders = { @@ -87,11 +95,11 @@ export class StoreApi extends BaseAPI { }; return this.request({ - path: '/store/order', + url: '/store/order', method: 'POST', headers, body: body, - }); + }, opts?.responseOpts); }; } diff --git a/samples/client/petstore/typescript-rxjs/builds/default/apis/UserApi.ts b/samples/client/petstore/typescript-rxjs/builds/default/apis/UserApi.ts index a223874786eb..5f08fdfd6580 100644 --- a/samples/client/petstore/typescript-rxjs/builds/default/apis/UserApi.ts +++ b/samples/client/petstore/typescript-rxjs/builds/default/apis/UserApi.ts @@ -12,7 +12,7 @@ */ import { Observable } from 'rxjs'; -import { BaseAPI, HttpHeaders, HttpQuery, throwIfNullOrUndefined, encodeURI } from '../runtime'; +import { BaseAPI, HttpHeaders, HttpQuery, throwIfNullOrUndefined, encodeURI, OperationOpts, RawAjaxResponse } from '../runtime'; import { User, } from '../models'; @@ -56,7 +56,9 @@ export class UserApi extends BaseAPI { * This can only be done by the logged in user. * Create user */ - createUser = ({ body }: CreateUserRequest): Observable => { + createUser({ body }: CreateUserRequest): Observable + createUser({ body }: CreateUserRequest, opts?: OperationOpts): Observable> + createUser({ body }: CreateUserRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(body, 'body', 'createUser'); const headers: HttpHeaders = { @@ -64,17 +66,19 @@ export class UserApi extends BaseAPI { }; return this.request({ - path: '/user', + url: '/user', method: 'POST', headers, body: body, - }); + }, opts?.responseOpts); }; /** * Creates list of users with given input array */ - createUsersWithArrayInput = ({ body }: CreateUsersWithArrayInputRequest): Observable => { + createUsersWithArrayInput({ body }: CreateUsersWithArrayInputRequest): Observable + createUsersWithArrayInput({ body }: CreateUsersWithArrayInputRequest, opts?: OperationOpts): Observable> + createUsersWithArrayInput({ body }: CreateUsersWithArrayInputRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(body, 'body', 'createUsersWithArrayInput'); const headers: HttpHeaders = { @@ -82,17 +86,19 @@ export class UserApi extends BaseAPI { }; return this.request({ - path: '/user/createWithArray', + url: '/user/createWithArray', method: 'POST', headers, body: body, - }); + }, opts?.responseOpts); }; /** * Creates list of users with given input array */ - createUsersWithListInput = ({ body }: CreateUsersWithListInputRequest): Observable => { + createUsersWithListInput({ body }: CreateUsersWithListInputRequest): Observable + createUsersWithListInput({ body }: CreateUsersWithListInputRequest, opts?: OperationOpts): Observable> + createUsersWithListInput({ body }: CreateUsersWithListInputRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(body, 'body', 'createUsersWithListInput'); const headers: HttpHeaders = { @@ -100,42 +106,48 @@ export class UserApi extends BaseAPI { }; return this.request({ - path: '/user/createWithList', + url: '/user/createWithList', method: 'POST', headers, body: body, - }); + }, opts?.responseOpts); }; /** * This can only be done by the logged in user. * Delete user */ - deleteUser = ({ username }: DeleteUserRequest): Observable => { + deleteUser({ username }: DeleteUserRequest): Observable + deleteUser({ username }: DeleteUserRequest, opts?: OperationOpts): Observable> + deleteUser({ username }: DeleteUserRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(username, 'username', 'deleteUser'); return this.request({ - path: '/user/{username}'.replace('{username}', encodeURI(username)), + url: '/user/{username}'.replace('{username}', encodeURI(username)), method: 'DELETE', - }); + }, opts?.responseOpts); }; /** * Get user by user name */ - getUserByName = ({ username }: GetUserByNameRequest): Observable => { + getUserByName({ username }: GetUserByNameRequest): Observable + getUserByName({ username }: GetUserByNameRequest, opts?: OperationOpts): Observable> + getUserByName({ username }: GetUserByNameRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(username, 'username', 'getUserByName'); return this.request({ - path: '/user/{username}'.replace('{username}', encodeURI(username)), + url: '/user/{username}'.replace('{username}', encodeURI(username)), method: 'GET', - }); + }, opts?.responseOpts); }; /** * Logs user into the system */ - loginUser = ({ username, password }: LoginUserRequest): Observable => { + loginUser({ username, password }: LoginUserRequest): Observable + loginUser({ username, password }: LoginUserRequest, opts?: OperationOpts): Observable> + loginUser({ username, password }: LoginUserRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(username, 'username', 'loginUser'); throwIfNullOrUndefined(password, 'password', 'loginUser'); @@ -145,27 +157,31 @@ export class UserApi extends BaseAPI { }; return this.request({ - path: '/user/login', + url: '/user/login', method: 'GET', query, - }); + }, opts?.responseOpts); }; /** * Logs out current logged in user session */ - logoutUser = (): Observable => { + logoutUser(): Observable + logoutUser(opts?: OperationOpts): Observable> + logoutUser(opts?: OperationOpts): Observable> { return this.request({ - path: '/user/logout', + url: '/user/logout', method: 'GET', - }); + }, opts?.responseOpts); }; /** * This can only be done by the logged in user. * Updated user */ - updateUser = ({ username, body }: UpdateUserRequest): Observable => { + updateUser({ username, body }: UpdateUserRequest): Observable + updateUser({ username, body }: UpdateUserRequest, opts?: OperationOpts): Observable> + updateUser({ username, body }: UpdateUserRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(username, 'username', 'updateUser'); throwIfNullOrUndefined(body, 'body', 'updateUser'); @@ -174,11 +190,11 @@ export class UserApi extends BaseAPI { }; return this.request({ - path: '/user/{username}'.replace('{username}', encodeURI(username)), + url: '/user/{username}'.replace('{username}', encodeURI(username)), method: 'PUT', headers, body: body, - }); + }, opts?.responseOpts); }; } diff --git a/samples/client/petstore/typescript-rxjs/builds/default/runtime.ts b/samples/client/petstore/typescript-rxjs/builds/default/runtime.ts index 42ddef55ae1c..b93291601c1c 100644 --- a/samples/client/petstore/typescript-rxjs/builds/default/runtime.ts +++ b/samples/client/petstore/typescript-rxjs/builds/default/runtime.ts @@ -11,7 +11,7 @@ * Do not edit the class manually. */ -import { Observable, of } from 'rxjs'; +import { Observable, of, Subscriber } from 'rxjs'; import { ajax, AjaxRequest, AjaxResponse } from 'rxjs/ajax'; import { map, concatMap } from 'rxjs/operators'; @@ -30,11 +30,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 { @@ -47,18 +47,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; } } @@ -84,31 +78,32 @@ export class BaseAPI { withPostMiddleware = (postMiddlewares: Array) => this.withMiddleware(postMiddlewares.map((post) => ({ post }))); - protected request = (requestOpts: RequestOpts): Observable => - this.rxjsRequest(this.createRequestArgs(requestOpts)).pipe( + protected request(requestOpts: RequestOpts): Observable + protected request(requestOpts: RequestOpts, responseOpts?: ResponseOpts): Observable> + protected request(requestOpts: RequestOpts, responseOpts?: ResponseOpts): Observable> { + 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 }: 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', }; } @@ -157,24 +152,35 @@ export type HttpHeaders = { [key: string]: string }; export type HttpQuery = Partial<{ [key: string]: string | number | null | boolean | Array }>; // 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'; } -export const encodeURI = (value: any) => encodeURIComponent(String(value)); +export interface ResponseOpts { + respone?: 'raw'; +} + +export interface OperationOpts { + responseOpts?: ResponseOpts; +} + +// AjaxResponse with typed response +export interface RawAjaxResponse extends AjaxResponse { + response: T; +} + +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 diff --git a/samples/client/petstore/typescript-rxjs/builds/default/tsconfig.json b/samples/client/petstore/typescript-rxjs/builds/default/tsconfig.json index d65f2a95afc1..59a60838a05c 100644 --- a/samples/client/petstore/typescript-rxjs/builds/default/tsconfig.json +++ b/samples/client/petstore/typescript-rxjs/builds/default/tsconfig.json @@ -8,7 +8,8 @@ "rootDir": ".", "lib": [ "es6", - "dom" + "dom", + "es2017" ], "typeRoots": [ "node_modules/@types" diff --git a/samples/client/petstore/typescript-rxjs/builds/es6-target/apis/PetApi.ts b/samples/client/petstore/typescript-rxjs/builds/es6-target/apis/PetApi.ts index 021781fd7abb..026381b12b2b 100644 --- a/samples/client/petstore/typescript-rxjs/builds/es6-target/apis/PetApi.ts +++ b/samples/client/petstore/typescript-rxjs/builds/es6-target/apis/PetApi.ts @@ -12,7 +12,7 @@ */ import { Observable } from 'rxjs'; -import { BaseAPI, HttpHeaders, HttpQuery, throwIfNullOrUndefined, encodeURI, COLLECTION_FORMATS } from '../runtime'; +import { BaseAPI, HttpHeaders, HttpQuery, throwIfNullOrUndefined, encodeURI, COLLECTION_FORMATS, OperationOpts, RawAjaxResponse } from '../runtime'; import { ApiResponse, Pet, @@ -63,7 +63,9 @@ export class PetApi extends BaseAPI { /** * Add a new pet to the store */ - addPet = ({ body }: AddPetRequest): Observable => { + addPet({ body }: AddPetRequest): Observable + addPet({ body }: AddPetRequest, opts?: OperationOpts): Observable> + addPet({ body }: AddPetRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(body, 'body', 'addPet'); const headers: HttpHeaders = { @@ -78,17 +80,19 @@ export class PetApi extends BaseAPI { }; return this.request({ - path: '/pet', + url: '/pet', method: 'POST', headers, body: body, - }); + }, opts?.responseOpts); }; /** * Deletes a pet */ - deletePet = ({ petId, apiKey }: DeletePetRequest): Observable => { + deletePet({ petId, apiKey }: DeletePetRequest): Observable + deletePet({ petId, apiKey }: DeletePetRequest, opts?: OperationOpts): Observable> + deletePet({ petId, apiKey }: DeletePetRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(petId, 'petId', 'deletePet'); const headers: HttpHeaders = { @@ -103,17 +107,19 @@ export class PetApi extends BaseAPI { }; return this.request({ - path: '/pet/{petId}'.replace('{petId}', encodeURI(petId)), + url: '/pet/{petId}'.replace('{petId}', encodeURI(petId)), method: 'DELETE', headers, - }); + }, opts?.responseOpts); }; /** * Multiple status values can be provided with comma separated strings * Finds Pets by status */ - findPetsByStatus = ({ status }: FindPetsByStatusRequest): Observable> => { + findPetsByStatus({ status }: FindPetsByStatusRequest): Observable> + findPetsByStatus({ status }: FindPetsByStatusRequest, opts?: OperationOpts): Observable>> + findPetsByStatus({ status }: FindPetsByStatusRequest, opts?: OperationOpts): Observable | RawAjaxResponse>> { throwIfNullOrUndefined(status, 'status', 'findPetsByStatus'); const headers: HttpHeaders = { @@ -131,18 +137,20 @@ export class PetApi extends BaseAPI { }; return this.request>({ - path: '/pet/findByStatus', + url: '/pet/findByStatus', method: 'GET', headers, query, - }); + }, opts?.responseOpts); }; /** * Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing. * Finds Pets by tags */ - findPetsByTags = ({ tags }: FindPetsByTagsRequest): Observable> => { + findPetsByTags({ tags }: FindPetsByTagsRequest): Observable> + findPetsByTags({ tags }: FindPetsByTagsRequest, opts?: OperationOpts): Observable>> + findPetsByTags({ tags }: FindPetsByTagsRequest, opts?: OperationOpts): Observable | RawAjaxResponse>> { throwIfNullOrUndefined(tags, 'tags', 'findPetsByTags'); const headers: HttpHeaders = { @@ -160,18 +168,20 @@ export class PetApi extends BaseAPI { }; return this.request>({ - path: '/pet/findByTags', + url: '/pet/findByTags', method: 'GET', headers, query, - }); + }, opts?.responseOpts); }; /** * Returns a single pet * Find pet by ID */ - getPetById = ({ petId }: GetPetByIdRequest): Observable => { + getPetById({ petId }: GetPetByIdRequest): Observable + getPetById({ petId }: GetPetByIdRequest, opts?: OperationOpts): Observable> + getPetById({ petId }: GetPetByIdRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(petId, 'petId', 'getPetById'); const headers: HttpHeaders = { @@ -179,16 +189,18 @@ export class PetApi extends BaseAPI { }; return this.request({ - path: '/pet/{petId}'.replace('{petId}', encodeURI(petId)), + url: '/pet/{petId}'.replace('{petId}', encodeURI(petId)), method: 'GET', headers, - }); + }, opts?.responseOpts); }; /** * Update an existing pet */ - updatePet = ({ body }: UpdatePetRequest): Observable => { + updatePet({ body }: UpdatePetRequest): Observable + updatePet({ body }: UpdatePetRequest, opts?: OperationOpts): Observable> + updatePet({ body }: UpdatePetRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(body, 'body', 'updatePet'); const headers: HttpHeaders = { @@ -203,17 +215,19 @@ export class PetApi extends BaseAPI { }; return this.request({ - path: '/pet', + url: '/pet', method: 'PUT', headers, body: body, - }); + }, opts?.responseOpts); }; /** * Updates a pet in the store with form data */ - updatePetWithForm = ({ petId, name, status }: UpdatePetWithFormRequest): Observable => { + updatePetWithForm({ petId, name, status }: UpdatePetWithFormRequest): Observable + updatePetWithForm({ petId, name, status }: UpdatePetWithFormRequest, opts?: OperationOpts): Observable> + updatePetWithForm({ petId, name, status }: UpdatePetWithFormRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(petId, 'petId', 'updatePetWithForm'); const headers: HttpHeaders = { @@ -231,17 +245,19 @@ export class PetApi extends BaseAPI { if (status !== undefined) { formData.append('status', status as any); } return this.request({ - path: '/pet/{petId}'.replace('{petId}', encodeURI(petId)), + url: '/pet/{petId}'.replace('{petId}', encodeURI(petId)), method: 'POST', headers, body: formData, - }); + }, opts?.responseOpts); }; /** * uploads an image */ - uploadFile = ({ petId, additionalMetadata, file }: UploadFileRequest): Observable => { + uploadFile({ petId, additionalMetadata, file }: UploadFileRequest): Observable + uploadFile({ petId, additionalMetadata, file }: UploadFileRequest, opts?: OperationOpts): Observable> + uploadFile({ petId, additionalMetadata, file }: UploadFileRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(petId, 'petId', 'uploadFile'); const headers: HttpHeaders = { @@ -259,11 +275,11 @@ export class PetApi extends BaseAPI { if (file !== undefined) { formData.append('file', file as any); } return this.request({ - path: '/pet/{petId}/uploadImage'.replace('{petId}', encodeURI(petId)), + url: '/pet/{petId}/uploadImage'.replace('{petId}', encodeURI(petId)), method: 'POST', headers, body: formData, - }); + }, opts?.responseOpts); }; } diff --git a/samples/client/petstore/typescript-rxjs/builds/es6-target/apis/StoreApi.ts b/samples/client/petstore/typescript-rxjs/builds/es6-target/apis/StoreApi.ts index 8ce04d16aa04..5575fe6c3d64 100644 --- a/samples/client/petstore/typescript-rxjs/builds/es6-target/apis/StoreApi.ts +++ b/samples/client/petstore/typescript-rxjs/builds/es6-target/apis/StoreApi.ts @@ -12,7 +12,7 @@ */ import { Observable } from 'rxjs'; -import { BaseAPI, HttpHeaders, throwIfNullOrUndefined, encodeURI } from '../runtime'; +import { BaseAPI, HttpHeaders, throwIfNullOrUndefined, encodeURI, OperationOpts, RawAjaxResponse } from '../runtime'; import { Order, } from '../models'; @@ -38,48 +38,56 @@ export class StoreApi extends BaseAPI { * For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors * Delete purchase order by ID */ - deleteOrder = ({ orderId }: DeleteOrderRequest): Observable => { + deleteOrder({ orderId }: DeleteOrderRequest): Observable + deleteOrder({ orderId }: DeleteOrderRequest, opts?: OperationOpts): Observable> + deleteOrder({ orderId }: DeleteOrderRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(orderId, 'orderId', 'deleteOrder'); return this.request({ - path: '/store/order/{orderId}'.replace('{orderId}', encodeURI(orderId)), + url: '/store/order/{orderId}'.replace('{orderId}', encodeURI(orderId)), method: 'DELETE', - }); + }, opts?.responseOpts); }; /** * Returns a map of status codes to quantities * Returns pet inventories by status */ - getInventory = (): Observable<{ [key: string]: number; }> => { + getInventory(): Observable<{ [key: string]: number; }> + getInventory(opts?: OperationOpts): Observable> + getInventory(opts?: OperationOpts): Observable<{ [key: string]: number; } | RawAjaxResponse<{ [key: string]: number; }>> { const headers: HttpHeaders = { ...(this.configuration.apiKey && { 'api_key': this.configuration.apiKey('api_key') }), // api_key authentication }; return this.request<{ [key: string]: number; }>({ - path: '/store/inventory', + url: '/store/inventory', method: 'GET', headers, - }); + }, opts?.responseOpts); }; /** * For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions * Find purchase order by ID */ - getOrderById = ({ orderId }: GetOrderByIdRequest): Observable => { + getOrderById({ orderId }: GetOrderByIdRequest): Observable + getOrderById({ orderId }: GetOrderByIdRequest, opts?: OperationOpts): Observable> + getOrderById({ orderId }: GetOrderByIdRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(orderId, 'orderId', 'getOrderById'); return this.request({ - path: '/store/order/{orderId}'.replace('{orderId}', encodeURI(orderId)), + url: '/store/order/{orderId}'.replace('{orderId}', encodeURI(orderId)), method: 'GET', - }); + }, opts?.responseOpts); }; /** * Place an order for a pet */ - placeOrder = ({ body }: PlaceOrderRequest): Observable => { + placeOrder({ body }: PlaceOrderRequest): Observable + placeOrder({ body }: PlaceOrderRequest, opts?: OperationOpts): Observable> + placeOrder({ body }: PlaceOrderRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(body, 'body', 'placeOrder'); const headers: HttpHeaders = { @@ -87,11 +95,11 @@ export class StoreApi extends BaseAPI { }; return this.request({ - path: '/store/order', + url: '/store/order', method: 'POST', headers, body: body, - }); + }, opts?.responseOpts); }; } diff --git a/samples/client/petstore/typescript-rxjs/builds/es6-target/apis/UserApi.ts b/samples/client/petstore/typescript-rxjs/builds/es6-target/apis/UserApi.ts index a223874786eb..5f08fdfd6580 100644 --- a/samples/client/petstore/typescript-rxjs/builds/es6-target/apis/UserApi.ts +++ b/samples/client/petstore/typescript-rxjs/builds/es6-target/apis/UserApi.ts @@ -12,7 +12,7 @@ */ import { Observable } from 'rxjs'; -import { BaseAPI, HttpHeaders, HttpQuery, throwIfNullOrUndefined, encodeURI } from '../runtime'; +import { BaseAPI, HttpHeaders, HttpQuery, throwIfNullOrUndefined, encodeURI, OperationOpts, RawAjaxResponse } from '../runtime'; import { User, } from '../models'; @@ -56,7 +56,9 @@ export class UserApi extends BaseAPI { * This can only be done by the logged in user. * Create user */ - createUser = ({ body }: CreateUserRequest): Observable => { + createUser({ body }: CreateUserRequest): Observable + createUser({ body }: CreateUserRequest, opts?: OperationOpts): Observable> + createUser({ body }: CreateUserRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(body, 'body', 'createUser'); const headers: HttpHeaders = { @@ -64,17 +66,19 @@ export class UserApi extends BaseAPI { }; return this.request({ - path: '/user', + url: '/user', method: 'POST', headers, body: body, - }); + }, opts?.responseOpts); }; /** * Creates list of users with given input array */ - createUsersWithArrayInput = ({ body }: CreateUsersWithArrayInputRequest): Observable => { + createUsersWithArrayInput({ body }: CreateUsersWithArrayInputRequest): Observable + createUsersWithArrayInput({ body }: CreateUsersWithArrayInputRequest, opts?: OperationOpts): Observable> + createUsersWithArrayInput({ body }: CreateUsersWithArrayInputRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(body, 'body', 'createUsersWithArrayInput'); const headers: HttpHeaders = { @@ -82,17 +86,19 @@ export class UserApi extends BaseAPI { }; return this.request({ - path: '/user/createWithArray', + url: '/user/createWithArray', method: 'POST', headers, body: body, - }); + }, opts?.responseOpts); }; /** * Creates list of users with given input array */ - createUsersWithListInput = ({ body }: CreateUsersWithListInputRequest): Observable => { + createUsersWithListInput({ body }: CreateUsersWithListInputRequest): Observable + createUsersWithListInput({ body }: CreateUsersWithListInputRequest, opts?: OperationOpts): Observable> + createUsersWithListInput({ body }: CreateUsersWithListInputRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(body, 'body', 'createUsersWithListInput'); const headers: HttpHeaders = { @@ -100,42 +106,48 @@ export class UserApi extends BaseAPI { }; return this.request({ - path: '/user/createWithList', + url: '/user/createWithList', method: 'POST', headers, body: body, - }); + }, opts?.responseOpts); }; /** * This can only be done by the logged in user. * Delete user */ - deleteUser = ({ username }: DeleteUserRequest): Observable => { + deleteUser({ username }: DeleteUserRequest): Observable + deleteUser({ username }: DeleteUserRequest, opts?: OperationOpts): Observable> + deleteUser({ username }: DeleteUserRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(username, 'username', 'deleteUser'); return this.request({ - path: '/user/{username}'.replace('{username}', encodeURI(username)), + url: '/user/{username}'.replace('{username}', encodeURI(username)), method: 'DELETE', - }); + }, opts?.responseOpts); }; /** * Get user by user name */ - getUserByName = ({ username }: GetUserByNameRequest): Observable => { + getUserByName({ username }: GetUserByNameRequest): Observable + getUserByName({ username }: GetUserByNameRequest, opts?: OperationOpts): Observable> + getUserByName({ username }: GetUserByNameRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(username, 'username', 'getUserByName'); return this.request({ - path: '/user/{username}'.replace('{username}', encodeURI(username)), + url: '/user/{username}'.replace('{username}', encodeURI(username)), method: 'GET', - }); + }, opts?.responseOpts); }; /** * Logs user into the system */ - loginUser = ({ username, password }: LoginUserRequest): Observable => { + loginUser({ username, password }: LoginUserRequest): Observable + loginUser({ username, password }: LoginUserRequest, opts?: OperationOpts): Observable> + loginUser({ username, password }: LoginUserRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(username, 'username', 'loginUser'); throwIfNullOrUndefined(password, 'password', 'loginUser'); @@ -145,27 +157,31 @@ export class UserApi extends BaseAPI { }; return this.request({ - path: '/user/login', + url: '/user/login', method: 'GET', query, - }); + }, opts?.responseOpts); }; /** * Logs out current logged in user session */ - logoutUser = (): Observable => { + logoutUser(): Observable + logoutUser(opts?: OperationOpts): Observable> + logoutUser(opts?: OperationOpts): Observable> { return this.request({ - path: '/user/logout', + url: '/user/logout', method: 'GET', - }); + }, opts?.responseOpts); }; /** * This can only be done by the logged in user. * Updated user */ - updateUser = ({ username, body }: UpdateUserRequest): Observable => { + updateUser({ username, body }: UpdateUserRequest): Observable + updateUser({ username, body }: UpdateUserRequest, opts?: OperationOpts): Observable> + updateUser({ username, body }: UpdateUserRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(username, 'username', 'updateUser'); throwIfNullOrUndefined(body, 'body', 'updateUser'); @@ -174,11 +190,11 @@ export class UserApi extends BaseAPI { }; return this.request({ - path: '/user/{username}'.replace('{username}', encodeURI(username)), + url: '/user/{username}'.replace('{username}', encodeURI(username)), method: 'PUT', headers, body: body, - }); + }, opts?.responseOpts); }; } diff --git a/samples/client/petstore/typescript-rxjs/builds/es6-target/package.json b/samples/client/petstore/typescript-rxjs/builds/es6-target/package.json index c9c00ca9cf31..69a56b7d42b5 100644 --- a/samples/client/petstore/typescript-rxjs/builds/es6-target/package.json +++ b/samples/client/petstore/typescript-rxjs/builds/es6-target/package.json @@ -6,14 +6,14 @@ "main": "./dist/index.js", "typings": "./dist/index.d.ts", "scripts" : { - "build": "tsc --outDir dist/", + "build": "node_modules/.bin/tsc --outDir dist/", "prepare": "npm run build" }, "dependencies": { "rxjs": "^6.3.3" }, "devDependencies": { - "typescript": "^3.0.1" + "typescript": "^3.7" }, "publishConfig": { "registry": "https://skimdb.npmjs.com/registry" diff --git a/samples/client/petstore/typescript-rxjs/builds/es6-target/runtime.ts b/samples/client/petstore/typescript-rxjs/builds/es6-target/runtime.ts index 42ddef55ae1c..b93291601c1c 100644 --- a/samples/client/petstore/typescript-rxjs/builds/es6-target/runtime.ts +++ b/samples/client/petstore/typescript-rxjs/builds/es6-target/runtime.ts @@ -11,7 +11,7 @@ * Do not edit the class manually. */ -import { Observable, of } from 'rxjs'; +import { Observable, of, Subscriber } from 'rxjs'; import { ajax, AjaxRequest, AjaxResponse } from 'rxjs/ajax'; import { map, concatMap } from 'rxjs/operators'; @@ -30,11 +30,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 { @@ -47,18 +47,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; } } @@ -84,31 +78,32 @@ export class BaseAPI { withPostMiddleware = (postMiddlewares: Array) => this.withMiddleware(postMiddlewares.map((post) => ({ post }))); - protected request = (requestOpts: RequestOpts): Observable => - this.rxjsRequest(this.createRequestArgs(requestOpts)).pipe( + protected request(requestOpts: RequestOpts): Observable + protected request(requestOpts: RequestOpts, responseOpts?: ResponseOpts): Observable> + protected request(requestOpts: RequestOpts, responseOpts?: ResponseOpts): Observable> { + 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 }: 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', }; } @@ -157,24 +152,35 @@ export type HttpHeaders = { [key: string]: string }; export type HttpQuery = Partial<{ [key: string]: string | number | null | boolean | Array }>; // 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'; } -export const encodeURI = (value: any) => encodeURIComponent(String(value)); +export interface ResponseOpts { + respone?: 'raw'; +} + +export interface OperationOpts { + responseOpts?: ResponseOpts; +} + +// AjaxResponse with typed response +export interface RawAjaxResponse extends AjaxResponse { + response: T; +} + +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 diff --git a/samples/client/petstore/typescript-rxjs/builds/es6-target/tsconfig.json b/samples/client/petstore/typescript-rxjs/builds/es6-target/tsconfig.json index 6cc02878fb79..00d4d5cc478a 100644 --- a/samples/client/petstore/typescript-rxjs/builds/es6-target/tsconfig.json +++ b/samples/client/petstore/typescript-rxjs/builds/es6-target/tsconfig.json @@ -6,6 +6,10 @@ "moduleResolution": "node", "outDir": "dist", "rootDir": ".", + "lib": [ + "dom", + "es2017" + ], "typeRoots": [ "node_modules/@types" ] diff --git a/samples/client/petstore/typescript-rxjs/builds/with-interfaces/apis/StoreApi.ts b/samples/client/petstore/typescript-rxjs/builds/with-interfaces/apis/StoreApi.ts deleted file mode 100644 index 8ce04d16aa04..000000000000 --- a/samples/client/petstore/typescript-rxjs/builds/with-interfaces/apis/StoreApi.ts +++ /dev/null @@ -1,97 +0,0 @@ -// tslint:disable -/** - * OpenAPI Petstore - * This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. - * - * The version of the OpenAPI document: 1.0.0 - * - * - * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). - * https://openapi-generator.tech - * Do not edit the class manually. - */ - -import { Observable } from 'rxjs'; -import { BaseAPI, HttpHeaders, throwIfNullOrUndefined, encodeURI } from '../runtime'; -import { - Order, -} from '../models'; - -export interface DeleteOrderRequest { - orderId: string; -} - -export interface GetOrderByIdRequest { - orderId: number; -} - -export interface PlaceOrderRequest { - body: Order; -} - -/** - * no description - */ -export class StoreApi extends BaseAPI { - - /** - * For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors - * Delete purchase order by ID - */ - deleteOrder = ({ orderId }: DeleteOrderRequest): Observable => { - throwIfNullOrUndefined(orderId, 'orderId', 'deleteOrder'); - - return this.request({ - path: '/store/order/{orderId}'.replace('{orderId}', encodeURI(orderId)), - method: 'DELETE', - }); - }; - - /** - * Returns a map of status codes to quantities - * Returns pet inventories by status - */ - getInventory = (): Observable<{ [key: string]: number; }> => { - const headers: HttpHeaders = { - ...(this.configuration.apiKey && { 'api_key': this.configuration.apiKey('api_key') }), // api_key authentication - }; - - return this.request<{ [key: string]: number; }>({ - path: '/store/inventory', - method: 'GET', - headers, - }); - }; - - /** - * For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions - * Find purchase order by ID - */ - getOrderById = ({ orderId }: GetOrderByIdRequest): Observable => { - throwIfNullOrUndefined(orderId, 'orderId', 'getOrderById'); - - return this.request({ - path: '/store/order/{orderId}'.replace('{orderId}', encodeURI(orderId)), - method: 'GET', - }); - }; - - /** - * Place an order for a pet - */ - placeOrder = ({ body }: PlaceOrderRequest): Observable => { - throwIfNullOrUndefined(body, 'body', 'placeOrder'); - - const headers: HttpHeaders = { - 'Content-Type': 'application/json', - }; - - return this.request({ - path: '/store/order', - method: 'POST', - headers, - body: body, - }); - }; - -} diff --git a/samples/client/petstore/typescript-rxjs/builds/with-interfaces/apis/UserApi.ts b/samples/client/petstore/typescript-rxjs/builds/with-interfaces/apis/UserApi.ts deleted file mode 100644 index a223874786eb..000000000000 --- a/samples/client/petstore/typescript-rxjs/builds/with-interfaces/apis/UserApi.ts +++ /dev/null @@ -1,184 +0,0 @@ -// tslint:disable -/** - * OpenAPI Petstore - * This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. - * - * The version of the OpenAPI document: 1.0.0 - * - * - * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). - * https://openapi-generator.tech - * Do not edit the class manually. - */ - -import { Observable } from 'rxjs'; -import { BaseAPI, HttpHeaders, HttpQuery, throwIfNullOrUndefined, encodeURI } from '../runtime'; -import { - User, -} from '../models'; - -export interface CreateUserRequest { - body: User; -} - -export interface CreateUsersWithArrayInputRequest { - body: Array; -} - -export interface CreateUsersWithListInputRequest { - body: Array; -} - -export interface DeleteUserRequest { - username: string; -} - -export interface GetUserByNameRequest { - username: string; -} - -export interface LoginUserRequest { - username: string; - password: string; -} - -export interface UpdateUserRequest { - username: string; - body: User; -} - -/** - * no description - */ -export class UserApi extends BaseAPI { - - /** - * This can only be done by the logged in user. - * Create user - */ - createUser = ({ body }: CreateUserRequest): Observable => { - throwIfNullOrUndefined(body, 'body', 'createUser'); - - const headers: HttpHeaders = { - 'Content-Type': 'application/json', - }; - - return this.request({ - path: '/user', - method: 'POST', - headers, - body: body, - }); - }; - - /** - * Creates list of users with given input array - */ - createUsersWithArrayInput = ({ body }: CreateUsersWithArrayInputRequest): Observable => { - throwIfNullOrUndefined(body, 'body', 'createUsersWithArrayInput'); - - const headers: HttpHeaders = { - 'Content-Type': 'application/json', - }; - - return this.request({ - path: '/user/createWithArray', - method: 'POST', - headers, - body: body, - }); - }; - - /** - * Creates list of users with given input array - */ - createUsersWithListInput = ({ body }: CreateUsersWithListInputRequest): Observable => { - throwIfNullOrUndefined(body, 'body', 'createUsersWithListInput'); - - const headers: HttpHeaders = { - 'Content-Type': 'application/json', - }; - - return this.request({ - path: '/user/createWithList', - method: 'POST', - headers, - body: body, - }); - }; - - /** - * This can only be done by the logged in user. - * Delete user - */ - deleteUser = ({ username }: DeleteUserRequest): Observable => { - throwIfNullOrUndefined(username, 'username', 'deleteUser'); - - return this.request({ - path: '/user/{username}'.replace('{username}', encodeURI(username)), - method: 'DELETE', - }); - }; - - /** - * Get user by user name - */ - getUserByName = ({ username }: GetUserByNameRequest): Observable => { - throwIfNullOrUndefined(username, 'username', 'getUserByName'); - - return this.request({ - path: '/user/{username}'.replace('{username}', encodeURI(username)), - method: 'GET', - }); - }; - - /** - * Logs user into the system - */ - loginUser = ({ username, password }: LoginUserRequest): Observable => { - throwIfNullOrUndefined(username, 'username', 'loginUser'); - throwIfNullOrUndefined(password, 'password', 'loginUser'); - - const query: HttpQuery = { // required parameters are used directly since they are already checked by throwIfNullOrUndefined - 'username': username, - 'password': password, - }; - - return this.request({ - path: '/user/login', - method: 'GET', - query, - }); - }; - - /** - * Logs out current logged in user session - */ - logoutUser = (): Observable => { - return this.request({ - path: '/user/logout', - method: 'GET', - }); - }; - - /** - * This can only be done by the logged in user. - * Updated user - */ - updateUser = ({ username, body }: UpdateUserRequest): Observable => { - throwIfNullOrUndefined(username, 'username', 'updateUser'); - throwIfNullOrUndefined(body, 'body', 'updateUser'); - - const headers: HttpHeaders = { - 'Content-Type': 'application/json', - }; - - return this.request({ - path: '/user/{username}'.replace('{username}', encodeURI(username)), - method: 'PUT', - headers, - body: body, - }); - }; - -} diff --git a/samples/client/petstore/typescript-rxjs/builds/with-npm-version/apis/PetApi.ts b/samples/client/petstore/typescript-rxjs/builds/with-npm-version/apis/PetApi.ts index 021781fd7abb..026381b12b2b 100644 --- a/samples/client/petstore/typescript-rxjs/builds/with-npm-version/apis/PetApi.ts +++ b/samples/client/petstore/typescript-rxjs/builds/with-npm-version/apis/PetApi.ts @@ -12,7 +12,7 @@ */ import { Observable } from 'rxjs'; -import { BaseAPI, HttpHeaders, HttpQuery, throwIfNullOrUndefined, encodeURI, COLLECTION_FORMATS } from '../runtime'; +import { BaseAPI, HttpHeaders, HttpQuery, throwIfNullOrUndefined, encodeURI, COLLECTION_FORMATS, OperationOpts, RawAjaxResponse } from '../runtime'; import { ApiResponse, Pet, @@ -63,7 +63,9 @@ export class PetApi extends BaseAPI { /** * Add a new pet to the store */ - addPet = ({ body }: AddPetRequest): Observable => { + addPet({ body }: AddPetRequest): Observable + addPet({ body }: AddPetRequest, opts?: OperationOpts): Observable> + addPet({ body }: AddPetRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(body, 'body', 'addPet'); const headers: HttpHeaders = { @@ -78,17 +80,19 @@ export class PetApi extends BaseAPI { }; return this.request({ - path: '/pet', + url: '/pet', method: 'POST', headers, body: body, - }); + }, opts?.responseOpts); }; /** * Deletes a pet */ - deletePet = ({ petId, apiKey }: DeletePetRequest): Observable => { + deletePet({ petId, apiKey }: DeletePetRequest): Observable + deletePet({ petId, apiKey }: DeletePetRequest, opts?: OperationOpts): Observable> + deletePet({ petId, apiKey }: DeletePetRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(petId, 'petId', 'deletePet'); const headers: HttpHeaders = { @@ -103,17 +107,19 @@ export class PetApi extends BaseAPI { }; return this.request({ - path: '/pet/{petId}'.replace('{petId}', encodeURI(petId)), + url: '/pet/{petId}'.replace('{petId}', encodeURI(petId)), method: 'DELETE', headers, - }); + }, opts?.responseOpts); }; /** * Multiple status values can be provided with comma separated strings * Finds Pets by status */ - findPetsByStatus = ({ status }: FindPetsByStatusRequest): Observable> => { + findPetsByStatus({ status }: FindPetsByStatusRequest): Observable> + findPetsByStatus({ status }: FindPetsByStatusRequest, opts?: OperationOpts): Observable>> + findPetsByStatus({ status }: FindPetsByStatusRequest, opts?: OperationOpts): Observable | RawAjaxResponse>> { throwIfNullOrUndefined(status, 'status', 'findPetsByStatus'); const headers: HttpHeaders = { @@ -131,18 +137,20 @@ export class PetApi extends BaseAPI { }; return this.request>({ - path: '/pet/findByStatus', + url: '/pet/findByStatus', method: 'GET', headers, query, - }); + }, opts?.responseOpts); }; /** * Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing. * Finds Pets by tags */ - findPetsByTags = ({ tags }: FindPetsByTagsRequest): Observable> => { + findPetsByTags({ tags }: FindPetsByTagsRequest): Observable> + findPetsByTags({ tags }: FindPetsByTagsRequest, opts?: OperationOpts): Observable>> + findPetsByTags({ tags }: FindPetsByTagsRequest, opts?: OperationOpts): Observable | RawAjaxResponse>> { throwIfNullOrUndefined(tags, 'tags', 'findPetsByTags'); const headers: HttpHeaders = { @@ -160,18 +168,20 @@ export class PetApi extends BaseAPI { }; return this.request>({ - path: '/pet/findByTags', + url: '/pet/findByTags', method: 'GET', headers, query, - }); + }, opts?.responseOpts); }; /** * Returns a single pet * Find pet by ID */ - getPetById = ({ petId }: GetPetByIdRequest): Observable => { + getPetById({ petId }: GetPetByIdRequest): Observable + getPetById({ petId }: GetPetByIdRequest, opts?: OperationOpts): Observable> + getPetById({ petId }: GetPetByIdRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(petId, 'petId', 'getPetById'); const headers: HttpHeaders = { @@ -179,16 +189,18 @@ export class PetApi extends BaseAPI { }; return this.request({ - path: '/pet/{petId}'.replace('{petId}', encodeURI(petId)), + url: '/pet/{petId}'.replace('{petId}', encodeURI(petId)), method: 'GET', headers, - }); + }, opts?.responseOpts); }; /** * Update an existing pet */ - updatePet = ({ body }: UpdatePetRequest): Observable => { + updatePet({ body }: UpdatePetRequest): Observable + updatePet({ body }: UpdatePetRequest, opts?: OperationOpts): Observable> + updatePet({ body }: UpdatePetRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(body, 'body', 'updatePet'); const headers: HttpHeaders = { @@ -203,17 +215,19 @@ export class PetApi extends BaseAPI { }; return this.request({ - path: '/pet', + url: '/pet', method: 'PUT', headers, body: body, - }); + }, opts?.responseOpts); }; /** * Updates a pet in the store with form data */ - updatePetWithForm = ({ petId, name, status }: UpdatePetWithFormRequest): Observable => { + updatePetWithForm({ petId, name, status }: UpdatePetWithFormRequest): Observable + updatePetWithForm({ petId, name, status }: UpdatePetWithFormRequest, opts?: OperationOpts): Observable> + updatePetWithForm({ petId, name, status }: UpdatePetWithFormRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(petId, 'petId', 'updatePetWithForm'); const headers: HttpHeaders = { @@ -231,17 +245,19 @@ export class PetApi extends BaseAPI { if (status !== undefined) { formData.append('status', status as any); } return this.request({ - path: '/pet/{petId}'.replace('{petId}', encodeURI(petId)), + url: '/pet/{petId}'.replace('{petId}', encodeURI(petId)), method: 'POST', headers, body: formData, - }); + }, opts?.responseOpts); }; /** * uploads an image */ - uploadFile = ({ petId, additionalMetadata, file }: UploadFileRequest): Observable => { + uploadFile({ petId, additionalMetadata, file }: UploadFileRequest): Observable + uploadFile({ petId, additionalMetadata, file }: UploadFileRequest, opts?: OperationOpts): Observable> + uploadFile({ petId, additionalMetadata, file }: UploadFileRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(petId, 'petId', 'uploadFile'); const headers: HttpHeaders = { @@ -259,11 +275,11 @@ export class PetApi extends BaseAPI { if (file !== undefined) { formData.append('file', file as any); } return this.request({ - path: '/pet/{petId}/uploadImage'.replace('{petId}', encodeURI(petId)), + url: '/pet/{petId}/uploadImage'.replace('{petId}', encodeURI(petId)), method: 'POST', headers, body: formData, - }); + }, opts?.responseOpts); }; } diff --git a/samples/client/petstore/typescript-rxjs/builds/with-npm-version/apis/StoreApi.ts b/samples/client/petstore/typescript-rxjs/builds/with-npm-version/apis/StoreApi.ts index 8ce04d16aa04..5575fe6c3d64 100644 --- a/samples/client/petstore/typescript-rxjs/builds/with-npm-version/apis/StoreApi.ts +++ b/samples/client/petstore/typescript-rxjs/builds/with-npm-version/apis/StoreApi.ts @@ -12,7 +12,7 @@ */ import { Observable } from 'rxjs'; -import { BaseAPI, HttpHeaders, throwIfNullOrUndefined, encodeURI } from '../runtime'; +import { BaseAPI, HttpHeaders, throwIfNullOrUndefined, encodeURI, OperationOpts, RawAjaxResponse } from '../runtime'; import { Order, } from '../models'; @@ -38,48 +38,56 @@ export class StoreApi extends BaseAPI { * For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors * Delete purchase order by ID */ - deleteOrder = ({ orderId }: DeleteOrderRequest): Observable => { + deleteOrder({ orderId }: DeleteOrderRequest): Observable + deleteOrder({ orderId }: DeleteOrderRequest, opts?: OperationOpts): Observable> + deleteOrder({ orderId }: DeleteOrderRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(orderId, 'orderId', 'deleteOrder'); return this.request({ - path: '/store/order/{orderId}'.replace('{orderId}', encodeURI(orderId)), + url: '/store/order/{orderId}'.replace('{orderId}', encodeURI(orderId)), method: 'DELETE', - }); + }, opts?.responseOpts); }; /** * Returns a map of status codes to quantities * Returns pet inventories by status */ - getInventory = (): Observable<{ [key: string]: number; }> => { + getInventory(): Observable<{ [key: string]: number; }> + getInventory(opts?: OperationOpts): Observable> + getInventory(opts?: OperationOpts): Observable<{ [key: string]: number; } | RawAjaxResponse<{ [key: string]: number; }>> { const headers: HttpHeaders = { ...(this.configuration.apiKey && { 'api_key': this.configuration.apiKey('api_key') }), // api_key authentication }; return this.request<{ [key: string]: number; }>({ - path: '/store/inventory', + url: '/store/inventory', method: 'GET', headers, - }); + }, opts?.responseOpts); }; /** * For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions * Find purchase order by ID */ - getOrderById = ({ orderId }: GetOrderByIdRequest): Observable => { + getOrderById({ orderId }: GetOrderByIdRequest): Observable + getOrderById({ orderId }: GetOrderByIdRequest, opts?: OperationOpts): Observable> + getOrderById({ orderId }: GetOrderByIdRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(orderId, 'orderId', 'getOrderById'); return this.request({ - path: '/store/order/{orderId}'.replace('{orderId}', encodeURI(orderId)), + url: '/store/order/{orderId}'.replace('{orderId}', encodeURI(orderId)), method: 'GET', - }); + }, opts?.responseOpts); }; /** * Place an order for a pet */ - placeOrder = ({ body }: PlaceOrderRequest): Observable => { + placeOrder({ body }: PlaceOrderRequest): Observable + placeOrder({ body }: PlaceOrderRequest, opts?: OperationOpts): Observable> + placeOrder({ body }: PlaceOrderRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(body, 'body', 'placeOrder'); const headers: HttpHeaders = { @@ -87,11 +95,11 @@ export class StoreApi extends BaseAPI { }; return this.request({ - path: '/store/order', + url: '/store/order', method: 'POST', headers, body: body, - }); + }, opts?.responseOpts); }; } diff --git a/samples/client/petstore/typescript-rxjs/builds/with-npm-version/apis/UserApi.ts b/samples/client/petstore/typescript-rxjs/builds/with-npm-version/apis/UserApi.ts index a223874786eb..5f08fdfd6580 100644 --- a/samples/client/petstore/typescript-rxjs/builds/with-npm-version/apis/UserApi.ts +++ b/samples/client/petstore/typescript-rxjs/builds/with-npm-version/apis/UserApi.ts @@ -12,7 +12,7 @@ */ import { Observable } from 'rxjs'; -import { BaseAPI, HttpHeaders, HttpQuery, throwIfNullOrUndefined, encodeURI } from '../runtime'; +import { BaseAPI, HttpHeaders, HttpQuery, throwIfNullOrUndefined, encodeURI, OperationOpts, RawAjaxResponse } from '../runtime'; import { User, } from '../models'; @@ -56,7 +56,9 @@ export class UserApi extends BaseAPI { * This can only be done by the logged in user. * Create user */ - createUser = ({ body }: CreateUserRequest): Observable => { + createUser({ body }: CreateUserRequest): Observable + createUser({ body }: CreateUserRequest, opts?: OperationOpts): Observable> + createUser({ body }: CreateUserRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(body, 'body', 'createUser'); const headers: HttpHeaders = { @@ -64,17 +66,19 @@ export class UserApi extends BaseAPI { }; return this.request({ - path: '/user', + url: '/user', method: 'POST', headers, body: body, - }); + }, opts?.responseOpts); }; /** * Creates list of users with given input array */ - createUsersWithArrayInput = ({ body }: CreateUsersWithArrayInputRequest): Observable => { + createUsersWithArrayInput({ body }: CreateUsersWithArrayInputRequest): Observable + createUsersWithArrayInput({ body }: CreateUsersWithArrayInputRequest, opts?: OperationOpts): Observable> + createUsersWithArrayInput({ body }: CreateUsersWithArrayInputRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(body, 'body', 'createUsersWithArrayInput'); const headers: HttpHeaders = { @@ -82,17 +86,19 @@ export class UserApi extends BaseAPI { }; return this.request({ - path: '/user/createWithArray', + url: '/user/createWithArray', method: 'POST', headers, body: body, - }); + }, opts?.responseOpts); }; /** * Creates list of users with given input array */ - createUsersWithListInput = ({ body }: CreateUsersWithListInputRequest): Observable => { + createUsersWithListInput({ body }: CreateUsersWithListInputRequest): Observable + createUsersWithListInput({ body }: CreateUsersWithListInputRequest, opts?: OperationOpts): Observable> + createUsersWithListInput({ body }: CreateUsersWithListInputRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(body, 'body', 'createUsersWithListInput'); const headers: HttpHeaders = { @@ -100,42 +106,48 @@ export class UserApi extends BaseAPI { }; return this.request({ - path: '/user/createWithList', + url: '/user/createWithList', method: 'POST', headers, body: body, - }); + }, opts?.responseOpts); }; /** * This can only be done by the logged in user. * Delete user */ - deleteUser = ({ username }: DeleteUserRequest): Observable => { + deleteUser({ username }: DeleteUserRequest): Observable + deleteUser({ username }: DeleteUserRequest, opts?: OperationOpts): Observable> + deleteUser({ username }: DeleteUserRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(username, 'username', 'deleteUser'); return this.request({ - path: '/user/{username}'.replace('{username}', encodeURI(username)), + url: '/user/{username}'.replace('{username}', encodeURI(username)), method: 'DELETE', - }); + }, opts?.responseOpts); }; /** * Get user by user name */ - getUserByName = ({ username }: GetUserByNameRequest): Observable => { + getUserByName({ username }: GetUserByNameRequest): Observable + getUserByName({ username }: GetUserByNameRequest, opts?: OperationOpts): Observable> + getUserByName({ username }: GetUserByNameRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(username, 'username', 'getUserByName'); return this.request({ - path: '/user/{username}'.replace('{username}', encodeURI(username)), + url: '/user/{username}'.replace('{username}', encodeURI(username)), method: 'GET', - }); + }, opts?.responseOpts); }; /** * Logs user into the system */ - loginUser = ({ username, password }: LoginUserRequest): Observable => { + loginUser({ username, password }: LoginUserRequest): Observable + loginUser({ username, password }: LoginUserRequest, opts?: OperationOpts): Observable> + loginUser({ username, password }: LoginUserRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(username, 'username', 'loginUser'); throwIfNullOrUndefined(password, 'password', 'loginUser'); @@ -145,27 +157,31 @@ export class UserApi extends BaseAPI { }; return this.request({ - path: '/user/login', + url: '/user/login', method: 'GET', query, - }); + }, opts?.responseOpts); }; /** * Logs out current logged in user session */ - logoutUser = (): Observable => { + logoutUser(): Observable + logoutUser(opts?: OperationOpts): Observable> + logoutUser(opts?: OperationOpts): Observable> { return this.request({ - path: '/user/logout', + url: '/user/logout', method: 'GET', - }); + }, opts?.responseOpts); }; /** * This can only be done by the logged in user. * Updated user */ - updateUser = ({ username, body }: UpdateUserRequest): Observable => { + updateUser({ username, body }: UpdateUserRequest): Observable + updateUser({ username, body }: UpdateUserRequest, opts?: OperationOpts): Observable> + updateUser({ username, body }: UpdateUserRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(username, 'username', 'updateUser'); throwIfNullOrUndefined(body, 'body', 'updateUser'); @@ -174,11 +190,11 @@ export class UserApi extends BaseAPI { }; return this.request({ - path: '/user/{username}'.replace('{username}', encodeURI(username)), + url: '/user/{username}'.replace('{username}', encodeURI(username)), method: 'PUT', headers, body: body, - }); + }, opts?.responseOpts); }; } diff --git a/samples/client/petstore/typescript-rxjs/builds/with-npm-version/package.json b/samples/client/petstore/typescript-rxjs/builds/with-npm-version/package.json index c9c00ca9cf31..69a56b7d42b5 100644 --- a/samples/client/petstore/typescript-rxjs/builds/with-npm-version/package.json +++ b/samples/client/petstore/typescript-rxjs/builds/with-npm-version/package.json @@ -6,14 +6,14 @@ "main": "./dist/index.js", "typings": "./dist/index.d.ts", "scripts" : { - "build": "tsc --outDir dist/", + "build": "node_modules/.bin/tsc --outDir dist/", "prepare": "npm run build" }, "dependencies": { "rxjs": "^6.3.3" }, "devDependencies": { - "typescript": "^3.0.1" + "typescript": "^3.7" }, "publishConfig": { "registry": "https://skimdb.npmjs.com/registry" diff --git a/samples/client/petstore/typescript-rxjs/builds/with-npm-version/runtime.ts b/samples/client/petstore/typescript-rxjs/builds/with-npm-version/runtime.ts index 42ddef55ae1c..b93291601c1c 100644 --- a/samples/client/petstore/typescript-rxjs/builds/with-npm-version/runtime.ts +++ b/samples/client/petstore/typescript-rxjs/builds/with-npm-version/runtime.ts @@ -11,7 +11,7 @@ * Do not edit the class manually. */ -import { Observable, of } from 'rxjs'; +import { Observable, of, Subscriber } from 'rxjs'; import { ajax, AjaxRequest, AjaxResponse } from 'rxjs/ajax'; import { map, concatMap } from 'rxjs/operators'; @@ -30,11 +30,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 { @@ -47,18 +47,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; } } @@ -84,31 +78,32 @@ export class BaseAPI { withPostMiddleware = (postMiddlewares: Array) => this.withMiddleware(postMiddlewares.map((post) => ({ post }))); - protected request = (requestOpts: RequestOpts): Observable => - this.rxjsRequest(this.createRequestArgs(requestOpts)).pipe( + protected request(requestOpts: RequestOpts): Observable + protected request(requestOpts: RequestOpts, responseOpts?: ResponseOpts): Observable> + protected request(requestOpts: RequestOpts, responseOpts?: ResponseOpts): Observable> { + 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 }: 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', }; } @@ -157,24 +152,35 @@ export type HttpHeaders = { [key: string]: string }; export type HttpQuery = Partial<{ [key: string]: string | number | null | boolean | Array }>; // 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'; } -export const encodeURI = (value: any) => encodeURIComponent(String(value)); +export interface ResponseOpts { + respone?: 'raw'; +} + +export interface OperationOpts { + responseOpts?: ResponseOpts; +} + +// AjaxResponse with typed response +export interface RawAjaxResponse extends AjaxResponse { + response: T; +} + +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 diff --git a/samples/client/petstore/typescript-rxjs/builds/with-npm-version/tsconfig.json b/samples/client/petstore/typescript-rxjs/builds/with-npm-version/tsconfig.json index d65f2a95afc1..59a60838a05c 100644 --- a/samples/client/petstore/typescript-rxjs/builds/with-npm-version/tsconfig.json +++ b/samples/client/petstore/typescript-rxjs/builds/with-npm-version/tsconfig.json @@ -8,7 +8,8 @@ "rootDir": ".", "lib": [ "es6", - "dom" + "dom", + "es2017" ], "typeRoots": [ "node_modules/@types" diff --git a/samples/client/petstore/typescript-rxjs/builds/with-interfaces/.gitignore b/samples/client/petstore/typescript-rxjs/builds/with-progress-subscriber/.gitignore similarity index 100% rename from samples/client/petstore/typescript-rxjs/builds/with-interfaces/.gitignore rename to samples/client/petstore/typescript-rxjs/builds/with-progress-subscriber/.gitignore diff --git a/samples/client/petstore/typescript-rxjs/builds/with-interfaces/.openapi-generator-ignore b/samples/client/petstore/typescript-rxjs/builds/with-progress-subscriber/.openapi-generator-ignore similarity index 100% rename from samples/client/petstore/typescript-rxjs/builds/with-interfaces/.openapi-generator-ignore rename to samples/client/petstore/typescript-rxjs/builds/with-progress-subscriber/.openapi-generator-ignore diff --git a/samples/client/petstore/typescript-rxjs/builds/with-interfaces/.openapi-generator/FILES b/samples/client/petstore/typescript-rxjs/builds/with-progress-subscriber/.openapi-generator/FILES similarity index 100% rename from samples/client/petstore/typescript-rxjs/builds/with-interfaces/.openapi-generator/FILES rename to samples/client/petstore/typescript-rxjs/builds/with-progress-subscriber/.openapi-generator/FILES diff --git a/samples/client/petstore/typescript-rxjs/builds/with-interfaces/.openapi-generator/VERSION b/samples/client/petstore/typescript-rxjs/builds/with-progress-subscriber/.openapi-generator/VERSION similarity index 100% rename from samples/client/petstore/typescript-rxjs/builds/with-interfaces/.openapi-generator/VERSION rename to samples/client/petstore/typescript-rxjs/builds/with-progress-subscriber/.openapi-generator/VERSION diff --git a/samples/client/petstore/typescript-rxjs/builds/with-interfaces/apis/PetApi.ts b/samples/client/petstore/typescript-rxjs/builds/with-progress-subscriber/apis/PetApi.ts similarity index 61% rename from samples/client/petstore/typescript-rxjs/builds/with-interfaces/apis/PetApi.ts rename to samples/client/petstore/typescript-rxjs/builds/with-progress-subscriber/apis/PetApi.ts index 021781fd7abb..0fc16a28bbfb 100644 --- a/samples/client/petstore/typescript-rxjs/builds/with-interfaces/apis/PetApi.ts +++ b/samples/client/petstore/typescript-rxjs/builds/with-progress-subscriber/apis/PetApi.ts @@ -12,7 +12,7 @@ */ import { Observable } from 'rxjs'; -import { BaseAPI, HttpHeaders, HttpQuery, throwIfNullOrUndefined, encodeURI, COLLECTION_FORMATS } from '../runtime'; +import { BaseAPI, HttpHeaders, HttpQuery, throwIfNullOrUndefined, encodeURI, COLLECTION_FORMATS, OperationOpts, RawAjaxResponse } from '../runtime'; import { ApiResponse, Pet, @@ -63,7 +63,10 @@ export class PetApi extends BaseAPI { /** * Add a new pet to the store */ - addPet = ({ body }: AddPetRequest): Observable => { + addPet({ body }: AddPetRequest): Observable + addPet({ body }: AddPetRequest, opts?: Pick): Observable + addPet({ body }: AddPetRequest, opts?: OperationOpts): Observable> + addPet({ body }: AddPetRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(body, 'body', 'addPet'); const headers: HttpHeaders = { @@ -78,17 +81,21 @@ export class PetApi extends BaseAPI { }; return this.request({ - path: '/pet', + url: '/pet', method: 'POST', headers, body: body, - }); + progressSubscriber: opts?.progressSubscriber, + }, opts?.responseOpts); }; /** * Deletes a pet */ - deletePet = ({ petId, apiKey }: DeletePetRequest): Observable => { + deletePet({ petId, apiKey }: DeletePetRequest): Observable + deletePet({ petId, apiKey }: DeletePetRequest, opts?: Pick): Observable + deletePet({ petId, apiKey }: DeletePetRequest, opts?: OperationOpts): Observable> + deletePet({ petId, apiKey }: DeletePetRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(petId, 'petId', 'deletePet'); const headers: HttpHeaders = { @@ -103,17 +110,21 @@ export class PetApi extends BaseAPI { }; return this.request({ - path: '/pet/{petId}'.replace('{petId}', encodeURI(petId)), + url: '/pet/{petId}'.replace('{petId}', encodeURI(petId)), method: 'DELETE', headers, - }); + progressSubscriber: opts?.progressSubscriber, + }, opts?.responseOpts); }; /** * Multiple status values can be provided with comma separated strings * Finds Pets by status */ - findPetsByStatus = ({ status }: FindPetsByStatusRequest): Observable> => { + findPetsByStatus({ status }: FindPetsByStatusRequest): Observable> + findPetsByStatus({ status }: FindPetsByStatusRequest, opts?: Pick): Observable> + findPetsByStatus({ status }: FindPetsByStatusRequest, opts?: OperationOpts): Observable>> + findPetsByStatus({ status }: FindPetsByStatusRequest, opts?: OperationOpts): Observable | RawAjaxResponse>> { throwIfNullOrUndefined(status, 'status', 'findPetsByStatus'); const headers: HttpHeaders = { @@ -131,18 +142,22 @@ export class PetApi extends BaseAPI { }; return this.request>({ - path: '/pet/findByStatus', + url: '/pet/findByStatus', method: 'GET', headers, query, - }); + progressSubscriber: opts?.progressSubscriber, + }, opts?.responseOpts); }; /** * Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing. * Finds Pets by tags */ - findPetsByTags = ({ tags }: FindPetsByTagsRequest): Observable> => { + findPetsByTags({ tags }: FindPetsByTagsRequest): Observable> + findPetsByTags({ tags }: FindPetsByTagsRequest, opts?: Pick): Observable> + findPetsByTags({ tags }: FindPetsByTagsRequest, opts?: OperationOpts): Observable>> + findPetsByTags({ tags }: FindPetsByTagsRequest, opts?: OperationOpts): Observable | RawAjaxResponse>> { throwIfNullOrUndefined(tags, 'tags', 'findPetsByTags'); const headers: HttpHeaders = { @@ -160,18 +175,22 @@ export class PetApi extends BaseAPI { }; return this.request>({ - path: '/pet/findByTags', + url: '/pet/findByTags', method: 'GET', headers, query, - }); + progressSubscriber: opts?.progressSubscriber, + }, opts?.responseOpts); }; /** * Returns a single pet * Find pet by ID */ - getPetById = ({ petId }: GetPetByIdRequest): Observable => { + getPetById({ petId }: GetPetByIdRequest): Observable + getPetById({ petId }: GetPetByIdRequest, opts?: Pick): Observable + getPetById({ petId }: GetPetByIdRequest, opts?: OperationOpts): Observable> + getPetById({ petId }: GetPetByIdRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(petId, 'petId', 'getPetById'); const headers: HttpHeaders = { @@ -179,16 +198,20 @@ export class PetApi extends BaseAPI { }; return this.request({ - path: '/pet/{petId}'.replace('{petId}', encodeURI(petId)), + url: '/pet/{petId}'.replace('{petId}', encodeURI(petId)), method: 'GET', headers, - }); + progressSubscriber: opts?.progressSubscriber, + }, opts?.responseOpts); }; /** * Update an existing pet */ - updatePet = ({ body }: UpdatePetRequest): Observable => { + updatePet({ body }: UpdatePetRequest): Observable + updatePet({ body }: UpdatePetRequest, opts?: Pick): Observable + updatePet({ body }: UpdatePetRequest, opts?: OperationOpts): Observable> + updatePet({ body }: UpdatePetRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(body, 'body', 'updatePet'); const headers: HttpHeaders = { @@ -203,17 +226,21 @@ export class PetApi extends BaseAPI { }; return this.request({ - path: '/pet', + url: '/pet', method: 'PUT', headers, body: body, - }); + progressSubscriber: opts?.progressSubscriber, + }, opts?.responseOpts); }; /** * Updates a pet in the store with form data */ - updatePetWithForm = ({ petId, name, status }: UpdatePetWithFormRequest): Observable => { + updatePetWithForm({ petId, name, status }: UpdatePetWithFormRequest): Observable + updatePetWithForm({ petId, name, status }: UpdatePetWithFormRequest, opts?: Pick): Observable + updatePetWithForm({ petId, name, status }: UpdatePetWithFormRequest, opts?: OperationOpts): Observable> + updatePetWithForm({ petId, name, status }: UpdatePetWithFormRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(petId, 'petId', 'updatePetWithForm'); const headers: HttpHeaders = { @@ -231,17 +258,21 @@ export class PetApi extends BaseAPI { if (status !== undefined) { formData.append('status', status as any); } return this.request({ - path: '/pet/{petId}'.replace('{petId}', encodeURI(petId)), + url: '/pet/{petId}'.replace('{petId}', encodeURI(petId)), method: 'POST', headers, body: formData, - }); + progressSubscriber: opts?.progressSubscriber, + }, opts?.responseOpts); }; /** * uploads an image */ - uploadFile = ({ petId, additionalMetadata, file }: UploadFileRequest): Observable => { + uploadFile({ petId, additionalMetadata, file }: UploadFileRequest): Observable + uploadFile({ petId, additionalMetadata, file }: UploadFileRequest, opts?: Pick): Observable + uploadFile({ petId, additionalMetadata, file }: UploadFileRequest, opts?: OperationOpts): Observable> + uploadFile({ petId, additionalMetadata, file }: UploadFileRequest, opts?: OperationOpts): Observable> { throwIfNullOrUndefined(petId, 'petId', 'uploadFile'); const headers: HttpHeaders = { @@ -259,11 +290,12 @@ export class PetApi extends BaseAPI { if (file !== undefined) { formData.append('file', file as any); } return this.request({ - path: '/pet/{petId}/uploadImage'.replace('{petId}', encodeURI(petId)), + url: '/pet/{petId}/uploadImage'.replace('{petId}', encodeURI(petId)), method: 'POST', headers, body: formData, - }); + progressSubscriber: opts?.progressSubscriber, + }, opts?.responseOpts); }; } diff --git a/samples/client/petstore/typescript-rxjs/builds/with-progress-subscriber/apis/StoreApi.ts b/samples/client/petstore/typescript-rxjs/builds/with-progress-subscriber/apis/StoreApi.ts new file mode 100644 index 000000000000..55672fd90697 --- /dev/null +++ b/samples/client/petstore/typescript-rxjs/builds/with-progress-subscriber/apis/StoreApi.ts @@ -0,0 +1,113 @@ +// tslint:disable +/** + * OpenAPI Petstore + * This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. + * + * The version of the OpenAPI document: 1.0.0 + * + * + * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). + * https://openapi-generator.tech + * Do not edit the class manually. + */ + +import { Observable } from 'rxjs'; +import { BaseAPI, HttpHeaders, throwIfNullOrUndefined, encodeURI, OperationOpts, RawAjaxResponse } from '../runtime'; +import { + Order, +} from '../models'; + +export interface DeleteOrderRequest { + orderId: string; +} + +export interface GetOrderByIdRequest { + orderId: number; +} + +export interface PlaceOrderRequest { + body: Order; +} + +/** + * no description + */ +export class StoreApi extends BaseAPI { + + /** + * For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors + * Delete purchase order by ID + */ + deleteOrder({ orderId }: DeleteOrderRequest): Observable + deleteOrder({ orderId }: DeleteOrderRequest, opts?: Pick): Observable + deleteOrder({ orderId }: DeleteOrderRequest, opts?: OperationOpts): Observable> + deleteOrder({ orderId }: DeleteOrderRequest, opts?: OperationOpts): Observable> { + throwIfNullOrUndefined(orderId, 'orderId', 'deleteOrder'); + + return this.request({ + url: '/store/order/{orderId}'.replace('{orderId}', encodeURI(orderId)), + method: 'DELETE', + progressSubscriber: opts?.progressSubscriber, + }, opts?.responseOpts); + }; + + /** + * Returns a map of status codes to quantities + * Returns pet inventories by status + */ + getInventory(): Observable<{ [key: string]: number; }> + getInventory(opts?: Pick): Observable<{ [key: string]: number; }> + getInventory(opts?: OperationOpts): Observable> + getInventory(opts?: OperationOpts): Observable<{ [key: string]: number; } | RawAjaxResponse<{ [key: string]: number; }>> { + const headers: HttpHeaders = { + ...(this.configuration.apiKey && { 'api_key': this.configuration.apiKey('api_key') }), // api_key authentication + }; + + return this.request<{ [key: string]: number; }>({ + url: '/store/inventory', + method: 'GET', + headers, + progressSubscriber: opts?.progressSubscriber, + }, opts?.responseOpts); + }; + + /** + * For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions + * Find purchase order by ID + */ + getOrderById({ orderId }: GetOrderByIdRequest): Observable + getOrderById({ orderId }: GetOrderByIdRequest, opts?: Pick): Observable + getOrderById({ orderId }: GetOrderByIdRequest, opts?: OperationOpts): Observable> + getOrderById({ orderId }: GetOrderByIdRequest, opts?: OperationOpts): Observable> { + throwIfNullOrUndefined(orderId, 'orderId', 'getOrderById'); + + return this.request({ + url: '/store/order/{orderId}'.replace('{orderId}', encodeURI(orderId)), + method: 'GET', + progressSubscriber: opts?.progressSubscriber, + }, opts?.responseOpts); + }; + + /** + * Place an order for a pet + */ + placeOrder({ body }: PlaceOrderRequest): Observable + placeOrder({ body }: PlaceOrderRequest, opts?: Pick): Observable + placeOrder({ body }: PlaceOrderRequest, opts?: OperationOpts): Observable> + placeOrder({ body }: PlaceOrderRequest, opts?: OperationOpts): Observable> { + throwIfNullOrUndefined(body, 'body', 'placeOrder'); + + const headers: HttpHeaders = { + 'Content-Type': 'application/json', + }; + + return this.request({ + url: '/store/order', + method: 'POST', + headers, + body: body, + progressSubscriber: opts?.progressSubscriber, + }, opts?.responseOpts); + }; + +} diff --git a/samples/client/petstore/typescript-rxjs/builds/with-progress-subscriber/apis/UserApi.ts b/samples/client/petstore/typescript-rxjs/builds/with-progress-subscriber/apis/UserApi.ts new file mode 100644 index 000000000000..1b216533a2fb --- /dev/null +++ b/samples/client/petstore/typescript-rxjs/builds/with-progress-subscriber/apis/UserApi.ts @@ -0,0 +1,216 @@ +// tslint:disable +/** + * OpenAPI Petstore + * This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. + * + * The version of the OpenAPI document: 1.0.0 + * + * + * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). + * https://openapi-generator.tech + * Do not edit the class manually. + */ + +import { Observable } from 'rxjs'; +import { BaseAPI, HttpHeaders, HttpQuery, throwIfNullOrUndefined, encodeURI, OperationOpts, RawAjaxResponse } from '../runtime'; +import { + User, +} from '../models'; + +export interface CreateUserRequest { + body: User; +} + +export interface CreateUsersWithArrayInputRequest { + body: Array; +} + +export interface CreateUsersWithListInputRequest { + body: Array; +} + +export interface DeleteUserRequest { + username: string; +} + +export interface GetUserByNameRequest { + username: string; +} + +export interface LoginUserRequest { + username: string; + password: string; +} + +export interface UpdateUserRequest { + username: string; + body: User; +} + +/** + * no description + */ +export class UserApi extends BaseAPI { + + /** + * This can only be done by the logged in user. + * Create user + */ + createUser({ body }: CreateUserRequest): Observable + createUser({ body }: CreateUserRequest, opts?: Pick): Observable + createUser({ body }: CreateUserRequest, opts?: OperationOpts): Observable> + createUser({ body }: CreateUserRequest, opts?: OperationOpts): Observable> { + throwIfNullOrUndefined(body, 'body', 'createUser'); + + const headers: HttpHeaders = { + 'Content-Type': 'application/json', + }; + + return this.request({ + url: '/user', + method: 'POST', + headers, + body: body, + progressSubscriber: opts?.progressSubscriber, + }, opts?.responseOpts); + }; + + /** + * Creates list of users with given input array + */ + createUsersWithArrayInput({ body }: CreateUsersWithArrayInputRequest): Observable + createUsersWithArrayInput({ body }: CreateUsersWithArrayInputRequest, opts?: Pick): Observable + createUsersWithArrayInput({ body }: CreateUsersWithArrayInputRequest, opts?: OperationOpts): Observable> + createUsersWithArrayInput({ body }: CreateUsersWithArrayInputRequest, opts?: OperationOpts): Observable> { + throwIfNullOrUndefined(body, 'body', 'createUsersWithArrayInput'); + + const headers: HttpHeaders = { + 'Content-Type': 'application/json', + }; + + return this.request({ + url: '/user/createWithArray', + method: 'POST', + headers, + body: body, + progressSubscriber: opts?.progressSubscriber, + }, opts?.responseOpts); + }; + + /** + * Creates list of users with given input array + */ + createUsersWithListInput({ body }: CreateUsersWithListInputRequest): Observable + createUsersWithListInput({ body }: CreateUsersWithListInputRequest, opts?: Pick): Observable + createUsersWithListInput({ body }: CreateUsersWithListInputRequest, opts?: OperationOpts): Observable> + createUsersWithListInput({ body }: CreateUsersWithListInputRequest, opts?: OperationOpts): Observable> { + throwIfNullOrUndefined(body, 'body', 'createUsersWithListInput'); + + const headers: HttpHeaders = { + 'Content-Type': 'application/json', + }; + + return this.request({ + url: '/user/createWithList', + method: 'POST', + headers, + body: body, + progressSubscriber: opts?.progressSubscriber, + }, opts?.responseOpts); + }; + + /** + * This can only be done by the logged in user. + * Delete user + */ + deleteUser({ username }: DeleteUserRequest): Observable + deleteUser({ username }: DeleteUserRequest, opts?: Pick): Observable + deleteUser({ username }: DeleteUserRequest, opts?: OperationOpts): Observable> + deleteUser({ username }: DeleteUserRequest, opts?: OperationOpts): Observable> { + throwIfNullOrUndefined(username, 'username', 'deleteUser'); + + return this.request({ + url: '/user/{username}'.replace('{username}', encodeURI(username)), + method: 'DELETE', + progressSubscriber: opts?.progressSubscriber, + }, opts?.responseOpts); + }; + + /** + * Get user by user name + */ + getUserByName({ username }: GetUserByNameRequest): Observable + getUserByName({ username }: GetUserByNameRequest, opts?: Pick): Observable + getUserByName({ username }: GetUserByNameRequest, opts?: OperationOpts): Observable> + getUserByName({ username }: GetUserByNameRequest, opts?: OperationOpts): Observable> { + throwIfNullOrUndefined(username, 'username', 'getUserByName'); + + return this.request({ + url: '/user/{username}'.replace('{username}', encodeURI(username)), + method: 'GET', + progressSubscriber: opts?.progressSubscriber, + }, opts?.responseOpts); + }; + + /** + * Logs user into the system + */ + loginUser({ username, password }: LoginUserRequest): Observable + loginUser({ username, password }: LoginUserRequest, opts?: Pick): Observable + loginUser({ username, password }: LoginUserRequest, opts?: OperationOpts): Observable> + loginUser({ username, password }: LoginUserRequest, opts?: OperationOpts): Observable> { + throwIfNullOrUndefined(username, 'username', 'loginUser'); + throwIfNullOrUndefined(password, 'password', 'loginUser'); + + const query: HttpQuery = { // required parameters are used directly since they are already checked by throwIfNullOrUndefined + 'username': username, + 'password': password, + }; + + return this.request({ + url: '/user/login', + method: 'GET', + query, + progressSubscriber: opts?.progressSubscriber, + }, opts?.responseOpts); + }; + + /** + * Logs out current logged in user session + */ + logoutUser(): Observable + logoutUser(opts?: Pick): Observable + logoutUser(opts?: OperationOpts): Observable> + logoutUser(opts?: OperationOpts): Observable> { + return this.request({ + url: '/user/logout', + method: 'GET', + progressSubscriber: opts?.progressSubscriber, + }, opts?.responseOpts); + }; + + /** + * This can only be done by the logged in user. + * Updated user + */ + updateUser({ username, body }: UpdateUserRequest): Observable + updateUser({ username, body }: UpdateUserRequest, opts?: Pick): Observable + updateUser({ username, body }: UpdateUserRequest, opts?: OperationOpts): Observable> + updateUser({ username, body }: UpdateUserRequest, opts?: OperationOpts): Observable> { + throwIfNullOrUndefined(username, 'username', 'updateUser'); + throwIfNullOrUndefined(body, 'body', 'updateUser'); + + const headers: HttpHeaders = { + 'Content-Type': 'application/json', + }; + + return this.request({ + url: '/user/{username}'.replace('{username}', encodeURI(username)), + method: 'PUT', + headers, + body: body, + progressSubscriber: opts?.progressSubscriber, + }, opts?.responseOpts); + }; + +} diff --git a/samples/client/petstore/typescript-rxjs/builds/with-interfaces/apis/index.ts b/samples/client/petstore/typescript-rxjs/builds/with-progress-subscriber/apis/index.ts similarity index 100% rename from samples/client/petstore/typescript-rxjs/builds/with-interfaces/apis/index.ts rename to samples/client/petstore/typescript-rxjs/builds/with-progress-subscriber/apis/index.ts diff --git a/samples/client/petstore/typescript-rxjs/builds/with-interfaces/index.ts b/samples/client/petstore/typescript-rxjs/builds/with-progress-subscriber/index.ts similarity index 100% rename from samples/client/petstore/typescript-rxjs/builds/with-interfaces/index.ts rename to samples/client/petstore/typescript-rxjs/builds/with-progress-subscriber/index.ts diff --git a/samples/client/petstore/typescript-rxjs/builds/with-interfaces/models/ApiResponse.ts b/samples/client/petstore/typescript-rxjs/builds/with-progress-subscriber/models/ApiResponse.ts similarity index 100% rename from samples/client/petstore/typescript-rxjs/builds/with-interfaces/models/ApiResponse.ts rename to samples/client/petstore/typescript-rxjs/builds/with-progress-subscriber/models/ApiResponse.ts diff --git a/samples/client/petstore/typescript-rxjs/builds/with-interfaces/models/Category.ts b/samples/client/petstore/typescript-rxjs/builds/with-progress-subscriber/models/Category.ts similarity index 100% rename from samples/client/petstore/typescript-rxjs/builds/with-interfaces/models/Category.ts rename to samples/client/petstore/typescript-rxjs/builds/with-progress-subscriber/models/Category.ts diff --git a/samples/client/petstore/typescript-rxjs/builds/with-interfaces/models/Order.ts b/samples/client/petstore/typescript-rxjs/builds/with-progress-subscriber/models/Order.ts similarity index 100% rename from samples/client/petstore/typescript-rxjs/builds/with-interfaces/models/Order.ts rename to samples/client/petstore/typescript-rxjs/builds/with-progress-subscriber/models/Order.ts diff --git a/samples/client/petstore/typescript-rxjs/builds/with-interfaces/models/Pet.ts b/samples/client/petstore/typescript-rxjs/builds/with-progress-subscriber/models/Pet.ts similarity index 100% rename from samples/client/petstore/typescript-rxjs/builds/with-interfaces/models/Pet.ts rename to samples/client/petstore/typescript-rxjs/builds/with-progress-subscriber/models/Pet.ts diff --git a/samples/client/petstore/typescript-rxjs/builds/with-interfaces/models/Tag.ts b/samples/client/petstore/typescript-rxjs/builds/with-progress-subscriber/models/Tag.ts similarity index 100% rename from samples/client/petstore/typescript-rxjs/builds/with-interfaces/models/Tag.ts rename to samples/client/petstore/typescript-rxjs/builds/with-progress-subscriber/models/Tag.ts diff --git a/samples/client/petstore/typescript-rxjs/builds/with-interfaces/models/User.ts b/samples/client/petstore/typescript-rxjs/builds/with-progress-subscriber/models/User.ts similarity index 100% rename from samples/client/petstore/typescript-rxjs/builds/with-interfaces/models/User.ts rename to samples/client/petstore/typescript-rxjs/builds/with-progress-subscriber/models/User.ts diff --git a/samples/client/petstore/typescript-rxjs/builds/with-interfaces/models/index.ts b/samples/client/petstore/typescript-rxjs/builds/with-progress-subscriber/models/index.ts similarity index 100% rename from samples/client/petstore/typescript-rxjs/builds/with-interfaces/models/index.ts rename to samples/client/petstore/typescript-rxjs/builds/with-progress-subscriber/models/index.ts diff --git a/samples/client/petstore/typescript-rxjs/builds/with-interfaces/runtime.ts b/samples/client/petstore/typescript-rxjs/builds/with-progress-subscriber/runtime.ts similarity index 67% rename from samples/client/petstore/typescript-rxjs/builds/with-interfaces/runtime.ts rename to samples/client/petstore/typescript-rxjs/builds/with-progress-subscriber/runtime.ts index 42ddef55ae1c..2fb0ef154ab6 100644 --- a/samples/client/petstore/typescript-rxjs/builds/with-interfaces/runtime.ts +++ b/samples/client/petstore/typescript-rxjs/builds/with-progress-subscriber/runtime.ts @@ -11,7 +11,7 @@ * Do not edit the class manually. */ -import { Observable, of } from 'rxjs'; +import { Observable, of, Subscriber } from 'rxjs'; import { ajax, AjaxRequest, AjaxResponse } from 'rxjs/ajax'; import { map, concatMap } from 'rxjs/operators'; @@ -30,11 +30,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 { @@ -47,18 +47,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; } } @@ -84,31 +78,33 @@ export class BaseAPI { withPostMiddleware = (postMiddlewares: Array) => this.withMiddleware(postMiddlewares.map((post) => ({ post }))); - protected request = (requestOpts: RequestOpts): Observable => - this.rxjsRequest(this.createRequestArgs(requestOpts)).pipe( + protected request(requestOpts: RequestOpts): Observable + protected request(requestOpts: RequestOpts, responseOpts?: ResponseOpts): Observable> + protected request(requestOpts: RequestOpts, responseOpts?: ResponseOpts): Observable> { + 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, progressSubscriber }: 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', + progressSubscriber, }; } @@ -157,24 +153,37 @@ export type HttpHeaders = { [key: string]: string }; export type HttpQuery = Partial<{ [key: string]: string | number | null | boolean | Array }>; // 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'; + progressSubscriber?: Subscriber; +} + +export interface ResponseOpts { + respone?: 'raw'; +} + +export interface OperationOpts { + progressSubscriber?: Subscriber; + responseOpts?: ResponseOpts; +} + +// AjaxResponse with typed response +export interface RawAjaxResponse 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 diff --git a/samples/client/petstore/typescript-rxjs/builds/with-interfaces/tsconfig.json b/samples/client/petstore/typescript-rxjs/builds/with-progress-subscriber/tsconfig.json similarity index 91% rename from samples/client/petstore/typescript-rxjs/builds/with-interfaces/tsconfig.json rename to samples/client/petstore/typescript-rxjs/builds/with-progress-subscriber/tsconfig.json index d65f2a95afc1..59a60838a05c 100644 --- a/samples/client/petstore/typescript-rxjs/builds/with-interfaces/tsconfig.json +++ b/samples/client/petstore/typescript-rxjs/builds/with-progress-subscriber/tsconfig.json @@ -8,7 +8,8 @@ "rootDir": ".", "lib": [ "es6", - "dom" + "dom", + "es2017" ], "typeRoots": [ "node_modules/@types"