From 4b809f393a3f5de577926a2fca7c74dc8418d4a8 Mon Sep 17 00:00:00 2001 From: "Sergey S. Volkov" Date: Mon, 5 Apr 2021 01:49:57 +0300 Subject: [PATCH] Release 8.0.0 (#216) * BREAKING_CHANGES: remove default `json` format of the response type * fix: missing `schema.$ref` in inline enum schemas * Allow passing custom fetch function (#218) * Allow passing custom fetch function * Fixing a mistake * Using ApiConfig instead of second param * fix: tests * chore: refresh schemas * Fix: query param array serialization (#223) * make the query params serialization conform to the default open api specs * uri encode second part Co-authored-by: stijn.lammens * docs: update CHANGELOG * fix: unused E generic type * chore: refresh generated test schemas * bump: up version to 8.0.0 Co-authored-by: Sushruth Shastry <2114206+sushruth@users.noreply.github.com> Co-authored-by: Stijn Lammens Co-authored-by: stijn.lammens --- CHANGELOG.md | 18 ++++++ package-lock.json | 2 +- package.json | 6 +- src/schema.js | 4 ++ src/typeFormatters.js | 14 +++-- .../base/http-clients/axios-http-client.eta | 12 ++-- .../base/http-clients/fetch-http-client.eta | 31 +++++----- tests/generated/v2.0/adafruit.ts | 49 ++++++++------- tests/generated/v2.0/another-example.ts | 49 ++++++++------- tests/generated/v2.0/another-schema.ts | 49 ++++++++------- tests/generated/v2.0/api-with-examples.ts | 49 ++++++++------- tests/generated/v2.0/authentiq.ts | 49 ++++++++------- tests/generated/v2.0/enums.ts | 49 ++++++++------- tests/generated/v2.0/example1.ts | 49 ++++++++------- tests/generated/v2.0/file-formdata-example.ts | 49 ++++++++------- tests/generated/v2.0/furkot-example.ts | 49 ++++++++------- tests/generated/v2.0/giphy.ts | 49 ++++++++------- tests/generated/v2.0/github-swagger.ts | 49 ++++++++------- tests/generated/v2.0/path-args.ts | 49 ++++++++------- tests/generated/v2.0/petstore-expanded.ts | 49 ++++++++------- tests/generated/v2.0/petstore-minimal.ts | 49 ++++++++------- tests/generated/v2.0/petstore-simple.ts | 49 ++++++++------- tests/generated/v2.0/petstore-swagger-io.ts | 49 ++++++++------- .../v2.0/petstore-with-external-docs.ts | 49 ++++++++------- tests/generated/v2.0/petstore.ts | 49 ++++++++------- tests/generated/v2.0/query-path-param.ts | 49 ++++++++------- tests/generated/v2.0/uber.ts | 49 ++++++++------- tests/generated/v3.0/additional-properties.ts | 49 ++++++++------- .../generated/v3.0/additional-properties2.ts | 49 ++++++++------- tests/generated/v3.0/allof-example.ts | 49 ++++++++------- tests/generated/v3.0/anyof-example.ts | 49 ++++++++------- tests/generated/v3.0/api-with-examples.ts | 49 ++++++++------- tests/generated/v3.0/callback-example.ts | 49 ++++++++------- tests/generated/v3.0/components-responses.ts | 49 ++++++++------- tests/generated/v3.0/explode-param-3.0.1.ts | 49 ++++++++------- tests/generated/v3.0/full-swagger-scheme.ts | 49 ++++++++------- tests/generated/v3.0/link-example.ts | 49 ++++++++------- tests/generated/v3.0/no-definitions-schema.ts | 49 ++++++++------- tests/generated/v3.0/nullable-refs.ts | 49 ++++++++------- tests/generated/v3.0/oneof-example.ts | 49 ++++++++------- tests/generated/v3.0/personal-api-example.ts | 49 ++++++++------- tests/generated/v3.0/petstore-expanded.ts | 49 ++++++++------- tests/generated/v3.0/petstore.ts | 49 ++++++++------- tests/generated/v3.0/recursive-schema.ts | 49 ++++++++------- tests/generated/v3.0/responses.ts | 49 ++++++++------- tests/generated/v3.0/swaggerhub-template.ts | 49 ++++++++------- tests/generated/v3.0/tsoa-odd-types-3.0.2.ts | 49 ++++++++------- tests/generated/v3.0/up-banking.ts | 60 ++++++++++++------- tests/generated/v3.0/uspto.ts | 49 ++++++++------- tests/generated/v3.0/wrong-enum-subtypes.ts | 49 ++++++++------- tests/generated/v3.0/wrong-schema-names.ts | 49 ++++++++------- tests/helpers/validateGeneratedModule.js | 22 +++---- tests/schemas/v3.0/up-banking.json | 15 +++++ tests/spec/axios/schema.ts | 12 ++-- tests/spec/axiosSingleHttpClient/schema.ts | 12 ++-- tests/spec/defaultAsSuccess/schema.ts | 49 ++++++++------- tests/spec/defaultResponse/schema.ts | 49 ++++++++------- tests/spec/enumNamesAsValues/schema.ts | 49 ++++++++------- tests/spec/extractRequestParams/schema.ts | 49 ++++++++------- tests/spec/js/schema.d.ts | 4 +- tests/spec/js/schema.js | 47 ++++++++------- tests/spec/jsAxios/schema.d.ts | 6 +- tests/spec/jsAxios/schema.js | 8 ++- tests/spec/modular/http-client.ts | 49 ++++++++------- tests/spec/moduleNameFirstTag/schema.ts | 49 ++++++++------- tests/spec/moduleNameIndex/schema.ts | 49 ++++++++------- tests/spec/partialBaseTemplate/schema.ts | 49 ++++++++------- tests/spec/partialDefaultTemplate/schema.ts | 49 ++++++++------- tests/spec/responses/schema.ts | 49 ++++++++------- tests/spec/singleHttpClient/schema.ts | 49 ++++++++------- tests/spec/templates/schema.ts | 2 +- .../templates/spec_templates/http-client.eta | 2 +- tests/spec/typeSuffixPrefix/schema.ts | 49 ++++++++------- tests/spec/unionEnums/schema.ts | 49 ++++++++------- 74 files changed, 1626 insertions(+), 1395 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 48bd593b..9d13e1f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,23 @@ # next release +# 8.0.0 + +BREAKING_CHANGES: +- remove default `json` format of the response type (both for `axios` and `fetch` http clients) (issue #213, thanks @po5i) + +Features: +- Allow passing custom fetch function (`fetch` http client only) +- Allow to set global response type format through `HttpClient` constructor + Example: +```ts + const httpClient = new HttpClient({ format: 'json' }); + // all request responses will been formatted as json +``` +Fixes: +- Missing `schema.$ref` in inline enum schemas +- Array query param values are serialized with the (non-default) comma separated style (issue #222, thanks @Styn, PR #223) +- TypeScript error "TS6133: 'E' is declared but its value is never read." (`axios` http client) (issue #220, thanks @pmbednarczyk ) + # 7.0.1 Fixes: diff --git a/package-lock.json b/package-lock.json index d2aba6b8..890ac917 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "swagger-typescript-api", - "version": "7.0.1", + "version": "8.0.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 36894bcc..5d9a31a2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "swagger-typescript-api", - "version": "7.0.1", + "version": "8.0.0", "description": "Generate typescript/javascript api from swagger schema", "scripts": { "cli:json": "node index.js -r -d -p ./swagger-test-cli.json -n swagger-test-cli.ts", @@ -9,8 +9,8 @@ "node:debug": "node --nolazy swagger-test-cli/generate.js", "contributors": "all-contributors generate", "cli:help": "node index.js -h", - "test-all": "node ./node_modules/npm-run-all/bin/npm-run-all/index.js generate validate test:* --continue-on-error", - "test-specific": "node ./node_modules/npm-run-all/bin/npm-run-all/index.js test:* --continue-on-error", + "test-all": "node ./node_modules/npm-run-all/bin/npm-run-all/index.js generate validate test:*", + "test-specific": "node ./node_modules/npm-run-all/bin/npm-run-all/index.js generate validate test:*", "generate": "node tests/generate.js", "generate:debug": "node --nolazy tests/generate.js", "validate": "node tests/validate.js", diff --git a/src/schema.js b/src/schema.js index 503af388..33710049 100644 --- a/src/schema.js +++ b/src/schema.js @@ -235,6 +235,8 @@ const attachParsedRef = (originalSchema, parsedSchema) => { const schemaParsers = { [SCHEMA_TYPES.ENUM]: (schema, typeName) => { + const refType = getRefType(schema); + const $ref = (refType && refType.$ref) || null; const enumNamesAsValues = config.enumNamesAsValues; const keyType = getType(schema); const enumNames = getEnumNames(schema); @@ -277,6 +279,8 @@ const schemaParsers = { return attachParsedRef(schema, { ...(_.isObject(schema) ? schema : {}), + $ref: $ref, + typeName: ($ref && refType.typeName) || null, $parsedSchema: true, schemaType: SCHEMA_TYPES.ENUM, type: SCHEMA_TYPES.ENUM, diff --git a/src/typeFormatters.js b/src/typeFormatters.js index 937a0d6e..bca14db5 100644 --- a/src/typeFormatters.js +++ b/src/typeFormatters.js @@ -57,12 +57,14 @@ const inlineExtraFormatters = { [SCHEMA_TYPES.ENUM]: (parsedSchema) => { return { ...parsedSchema, - content: _.uniq( - _.compact([ - ..._.map(parsedSchema.content, ({ value }) => `${value}`), - parsedSchema.nullable && TS_KEYWORDS.NULL, - ]), - ).join(" | "), + content: parsedSchema.$ref + ? parsedSchema.typeName + : _.uniq( + _.compact([ + ..._.map(parsedSchema.content, ({ value }) => `${value}`), + parsedSchema.nullable && TS_KEYWORDS.NULL, + ]), + ).join(" | "), }; }, }; diff --git a/templates/base/http-clients/axios-http-client.eta b/templates/base/http-clients/axios-http-client.eta index 86c87ecd..4958b0b1 100644 --- a/templates/base/http-clients/axios-http-client.eta +++ b/templates/base/http-clients/axios-http-client.eta @@ -26,6 +26,7 @@ export type RequestParams = Omit extends Omit { securityWorker?: (securityData: SecurityDataType | null) => Promise | AxiosRequestConfig | void; secure?: boolean; + format?: ResponseType; } export enum ContentType { @@ -39,10 +40,12 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private secure?: boolean; + private format?: ResponseType; - constructor({ securityWorker, secure, ...axiosConfig }: ApiConfig = {}) { + constructor({ securityWorker, secure, format, ...axiosConfig }: ApiConfig = {}) { this.instance = axios.create({ ...axiosConfig, baseURL: axiosConfig.baseURL || "<%~ apiConfig.baseUrl %>" }) this.secure = secure; + this.format = format; this.securityWorker = securityWorker; } @@ -63,17 +66,18 @@ export class HttpClient { }; } - public request = async ({ + public request = async ({ secure, path, type, query, - format = "json", + format, body, ...params }: FullRequestParams): Promise> => { const secureParams = ((typeof secure === 'boolean' ? secure : this.secure) && this.securityWorker && (await this.securityWorker(this.securityData))) || {}; const requestParams = this.mergeRequestParams(params, secureParams); + const responseFormat = (format && this.format) || void 0; return this.instance.request({ ...requestParams, @@ -82,7 +86,7 @@ export class HttpClient { ...(requestParams.headers || {}), }, params: query, - responseType: format, + responseType: responseFormat, data: body, url: path, }); diff --git a/templates/base/http-clients/fetch-http-client.eta b/templates/base/http-clients/fetch-http-client.eta index cd5a29eb..b7bcf5c2 100644 --- a/templates/base/http-clients/fetch-http-client.eta +++ b/templates/base/http-clients/fetch-http-client.eta @@ -31,6 +31,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise } export interface HttpResponse extends Response { @@ -51,6 +52,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: 'same-origin', @@ -67,16 +69,10 @@ export class HttpClient { this.securityData = data; } - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + "=" + encodeURIComponent( - Array.isArray(value) ? value.join(",") : - typeof value === "number" ? value : - `${value}` - ) - ); + const encodedKey = encodeURIComponent(key); + return `${value.map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`).join('&')}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -86,7 +82,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -118,7 +114,7 @@ export class HttpClient { }, }; } - + private createAbortSignal = (cancelToken: CancelToken): AbortSignal | undefined => { if (this.abortControllers.has(cancelToken)) { const abortController = this.abortControllers.get(cancelToken); @@ -127,7 +123,7 @@ export class HttpClient { } return void 0; } - + const abortController = new AbortController(); this.abortControllers.set(cancelToken, abortController); return abortController.signal; @@ -141,14 +137,14 @@ export class HttpClient { this.abortControllers.delete(cancelToken); } } - + public request = async ({ body, secure, path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -157,8 +153,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch( + return this.customFetch( `${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, @@ -174,7 +171,7 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() + const data = !responseFormat ? r : await response[responseFormat]() .then((data) => { if (r.ok) { r.data = data; @@ -187,7 +184,7 @@ export class HttpClient { r.error = e; return r; }); - + if (cancelToken) { this.abortControllers.delete(cancelToken); } diff --git a/tests/generated/v2.0/adafruit.ts b/tests/generated/v2.0/adafruit.ts index ac1f44f1..d1a05171 100644 --- a/tests/generated/v2.0/adafruit.ts +++ b/tests/generated/v2.0/adafruit.ts @@ -192,6 +192,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -212,6 +213,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -228,14 +230,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -245,7 +245,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -308,7 +308,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -321,8 +321,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -335,19 +336,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/generated/v2.0/another-example.ts b/tests/generated/v2.0/another-example.ts index 548f8b02..05e5bbd3 100644 --- a/tests/generated/v2.0/another-example.ts +++ b/tests/generated/v2.0/another-example.ts @@ -168,6 +168,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -188,6 +189,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -204,14 +206,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -221,7 +221,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -284,7 +284,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -297,8 +297,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -311,19 +312,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/generated/v2.0/another-schema.ts b/tests/generated/v2.0/another-schema.ts index 5c2a12b5..b3fdbbdc 100644 --- a/tests/generated/v2.0/another-schema.ts +++ b/tests/generated/v2.0/another-schema.ts @@ -60,6 +60,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -80,6 +81,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -96,14 +98,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -113,7 +113,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -176,7 +176,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -189,8 +189,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -203,19 +204,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/generated/v2.0/api-with-examples.ts b/tests/generated/v2.0/api-with-examples.ts index d655bb99..55cf244c 100644 --- a/tests/generated/v2.0/api-with-examples.ts +++ b/tests/generated/v2.0/api-with-examples.ts @@ -37,6 +37,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -57,6 +58,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -73,14 +75,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -90,7 +90,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -153,7 +153,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -166,8 +166,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -180,19 +181,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/generated/v2.0/authentiq.ts b/tests/generated/v2.0/authentiq.ts index 8866c0e4..e5512bc3 100644 --- a/tests/generated/v2.0/authentiq.ts +++ b/tests/generated/v2.0/authentiq.ts @@ -89,6 +89,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -109,6 +110,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -125,14 +127,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -142,7 +142,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -205,7 +205,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -218,8 +218,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -232,19 +233,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/generated/v2.0/enums.ts b/tests/generated/v2.0/enums.ts index 96478218..2c882efe 100644 --- a/tests/generated/v2.0/enums.ts +++ b/tests/generated/v2.0/enums.ts @@ -102,6 +102,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -122,6 +123,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -138,14 +140,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -155,7 +155,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -218,7 +218,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -231,8 +231,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -245,19 +246,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/generated/v2.0/example1.ts b/tests/generated/v2.0/example1.ts index ec9decec..00b4f427 100644 --- a/tests/generated/v2.0/example1.ts +++ b/tests/generated/v2.0/example1.ts @@ -64,6 +64,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -84,6 +85,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -100,14 +102,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -117,7 +117,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -180,7 +180,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -193,8 +193,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -207,19 +208,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/generated/v2.0/file-formdata-example.ts b/tests/generated/v2.0/file-formdata-example.ts index 68983b3e..fc6c0e78 100644 --- a/tests/generated/v2.0/file-formdata-example.ts +++ b/tests/generated/v2.0/file-formdata-example.ts @@ -37,6 +37,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -57,6 +58,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -73,14 +75,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -90,7 +90,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -153,7 +153,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -166,8 +166,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -180,19 +181,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/generated/v2.0/furkot-example.ts b/tests/generated/v2.0/furkot-example.ts index 90548a41..6ae341d7 100644 --- a/tests/generated/v2.0/furkot-example.ts +++ b/tests/generated/v2.0/furkot-example.ts @@ -100,6 +100,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -120,6 +121,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -136,14 +138,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -153,7 +153,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -216,7 +216,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -229,8 +229,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -243,19 +244,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/generated/v2.0/giphy.ts b/tests/generated/v2.0/giphy.ts index 0f151ac9..4e51559c 100644 --- a/tests/generated/v2.0/giphy.ts +++ b/tests/generated/v2.0/giphy.ts @@ -319,6 +319,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -339,6 +340,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -355,14 +357,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -372,7 +372,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -435,7 +435,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -448,8 +448,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -462,19 +463,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/generated/v2.0/github-swagger.ts b/tests/generated/v2.0/github-swagger.ts index 46ccbe0f..e5fa0292 100644 --- a/tests/generated/v2.0/github-swagger.ts +++ b/tests/generated/v2.0/github-swagger.ts @@ -1472,6 +1472,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -1492,6 +1493,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -1508,14 +1510,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -1525,7 +1525,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -1588,7 +1588,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -1601,8 +1601,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -1615,19 +1616,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/generated/v2.0/path-args.ts b/tests/generated/v2.0/path-args.ts index 57ccad47..17de507d 100644 --- a/tests/generated/v2.0/path-args.ts +++ b/tests/generated/v2.0/path-args.ts @@ -37,6 +37,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -57,6 +58,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -73,14 +75,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -90,7 +90,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -153,7 +153,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -166,8 +166,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -180,19 +181,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/generated/v2.0/petstore-expanded.ts b/tests/generated/v2.0/petstore-expanded.ts index 72c05765..9564fcb1 100644 --- a/tests/generated/v2.0/petstore-expanded.ts +++ b/tests/generated/v2.0/petstore-expanded.ts @@ -74,6 +74,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -94,6 +95,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -110,14 +112,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -127,7 +127,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -190,7 +190,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -203,8 +203,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -217,19 +218,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/generated/v2.0/petstore-minimal.ts b/tests/generated/v2.0/petstore-minimal.ts index 381fa225..ab712307 100644 --- a/tests/generated/v2.0/petstore-minimal.ts +++ b/tests/generated/v2.0/petstore-minimal.ts @@ -45,6 +45,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -65,6 +66,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -81,14 +83,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -98,7 +98,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -161,7 +161,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -174,8 +174,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -188,19 +189,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/generated/v2.0/petstore-simple.ts b/tests/generated/v2.0/petstore-simple.ts index fd0f646f..1a529ea2 100644 --- a/tests/generated/v2.0/petstore-simple.ts +++ b/tests/generated/v2.0/petstore-simple.ts @@ -60,6 +60,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -80,6 +81,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -96,14 +98,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -113,7 +113,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -176,7 +176,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -189,8 +189,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -203,19 +204,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/generated/v2.0/petstore-swagger-io.ts b/tests/generated/v2.0/petstore-swagger-io.ts index fb977013..2eac9a00 100644 --- a/tests/generated/v2.0/petstore-swagger-io.ts +++ b/tests/generated/v2.0/petstore-swagger-io.ts @@ -105,6 +105,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -125,6 +126,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -141,14 +143,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -158,7 +158,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -221,7 +221,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -234,8 +234,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -248,19 +249,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/generated/v2.0/petstore-with-external-docs.ts b/tests/generated/v2.0/petstore-with-external-docs.ts index c903a343..8997920e 100644 --- a/tests/generated/v2.0/petstore-with-external-docs.ts +++ b/tests/generated/v2.0/petstore-with-external-docs.ts @@ -50,6 +50,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -70,6 +71,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -86,14 +88,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -103,7 +103,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -166,7 +166,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -179,8 +179,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -193,19 +194,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/generated/v2.0/petstore.ts b/tests/generated/v2.0/petstore.ts index cb3169d2..5cabb1e9 100644 --- a/tests/generated/v2.0/petstore.ts +++ b/tests/generated/v2.0/petstore.ts @@ -52,6 +52,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -72,6 +73,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -88,14 +90,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -105,7 +105,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -168,7 +168,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -181,8 +181,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -195,19 +196,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/generated/v2.0/query-path-param.ts b/tests/generated/v2.0/query-path-param.ts index ffff4f64..c90add11 100644 --- a/tests/generated/v2.0/query-path-param.ts +++ b/tests/generated/v2.0/query-path-param.ts @@ -37,6 +37,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -57,6 +58,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -73,14 +75,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -90,7 +90,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -153,7 +153,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -166,8 +166,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -180,19 +181,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/generated/v2.0/uber.ts b/tests/generated/v2.0/uber.ts index d7a5bc8a..97ef8720 100644 --- a/tests/generated/v2.0/uber.ts +++ b/tests/generated/v2.0/uber.ts @@ -132,6 +132,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -152,6 +153,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -168,14 +170,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -185,7 +185,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -248,7 +248,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -261,8 +261,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -275,19 +276,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/generated/v3.0/additional-properties.ts b/tests/generated/v3.0/additional-properties.ts index e6906ee6..6dab8d21 100644 --- a/tests/generated/v3.0/additional-properties.ts +++ b/tests/generated/v3.0/additional-properties.ts @@ -44,6 +44,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -64,6 +65,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -80,14 +82,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -97,7 +97,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -160,7 +160,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -173,8 +173,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -187,19 +188,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/generated/v3.0/additional-properties2.ts b/tests/generated/v3.0/additional-properties2.ts index e488763a..35b11561 100644 --- a/tests/generated/v3.0/additional-properties2.ts +++ b/tests/generated/v3.0/additional-properties2.ts @@ -41,6 +41,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -61,6 +62,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -77,14 +79,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -94,7 +94,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -157,7 +157,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -170,8 +170,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -184,19 +185,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/generated/v3.0/allof-example.ts b/tests/generated/v3.0/allof-example.ts index e65a7689..806224bb 100644 --- a/tests/generated/v3.0/allof-example.ts +++ b/tests/generated/v3.0/allof-example.ts @@ -45,6 +45,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -65,6 +66,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -81,14 +83,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -98,7 +98,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -161,7 +161,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -174,8 +174,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -188,19 +189,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/generated/v3.0/anyof-example.ts b/tests/generated/v3.0/anyof-example.ts index 58de0d18..b28443fb 100644 --- a/tests/generated/v3.0/anyof-example.ts +++ b/tests/generated/v3.0/anyof-example.ts @@ -47,6 +47,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -67,6 +68,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -83,14 +85,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -100,7 +100,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -163,7 +163,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -176,8 +176,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -190,19 +191,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/generated/v3.0/api-with-examples.ts b/tests/generated/v3.0/api-with-examples.ts index ec5fa6a2..803cecff 100644 --- a/tests/generated/v3.0/api-with-examples.ts +++ b/tests/generated/v3.0/api-with-examples.ts @@ -37,6 +37,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -57,6 +58,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -73,14 +75,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -90,7 +90,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -153,7 +153,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -166,8 +166,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -180,19 +181,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/generated/v3.0/callback-example.ts b/tests/generated/v3.0/callback-example.ts index 4fe7a648..0b2dbc46 100644 --- a/tests/generated/v3.0/callback-example.ts +++ b/tests/generated/v3.0/callback-example.ts @@ -37,6 +37,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -57,6 +58,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -73,14 +75,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -90,7 +90,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -153,7 +153,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -166,8 +166,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -180,19 +181,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/generated/v3.0/components-responses.ts b/tests/generated/v3.0/components-responses.ts index 5b8762af..19cb9986 100644 --- a/tests/generated/v3.0/components-responses.ts +++ b/tests/generated/v3.0/components-responses.ts @@ -37,6 +37,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -57,6 +58,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -73,14 +75,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -90,7 +90,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -153,7 +153,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -166,8 +166,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -180,19 +181,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/generated/v3.0/explode-param-3.0.1.ts b/tests/generated/v3.0/explode-param-3.0.1.ts index 7f96e23f..468ae1a0 100644 --- a/tests/generated/v3.0/explode-param-3.0.1.ts +++ b/tests/generated/v3.0/explode-param-3.0.1.ts @@ -57,6 +57,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -77,6 +78,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -93,14 +95,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -110,7 +110,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -173,7 +173,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -186,8 +186,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -200,19 +201,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/generated/v3.0/full-swagger-scheme.ts b/tests/generated/v3.0/full-swagger-scheme.ts index dd6b8c7a..bc16b2f2 100644 --- a/tests/generated/v3.0/full-swagger-scheme.ts +++ b/tests/generated/v3.0/full-swagger-scheme.ts @@ -8981,6 +8981,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -9001,6 +9002,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -9017,14 +9019,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -9034,7 +9034,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -9097,7 +9097,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -9110,8 +9110,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -9124,19 +9125,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/generated/v3.0/link-example.ts b/tests/generated/v3.0/link-example.ts index 795512e2..0010ac2a 100644 --- a/tests/generated/v3.0/link-example.ts +++ b/tests/generated/v3.0/link-example.ts @@ -54,6 +54,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -74,6 +75,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -90,14 +92,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -107,7 +107,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -170,7 +170,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -183,8 +183,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -197,19 +198,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/generated/v3.0/no-definitions-schema.ts b/tests/generated/v3.0/no-definitions-schema.ts index 405f3434..5bfef145 100644 --- a/tests/generated/v3.0/no-definitions-schema.ts +++ b/tests/generated/v3.0/no-definitions-schema.ts @@ -50,6 +50,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -70,6 +71,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -86,14 +88,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -103,7 +103,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -166,7 +166,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -179,8 +179,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -193,19 +194,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/generated/v3.0/nullable-refs.ts b/tests/generated/v3.0/nullable-refs.ts index eba313e4..af503cd9 100644 --- a/tests/generated/v3.0/nullable-refs.ts +++ b/tests/generated/v3.0/nullable-refs.ts @@ -51,6 +51,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -71,6 +72,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -87,14 +89,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -104,7 +104,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -167,7 +167,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -180,8 +180,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -194,19 +195,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/generated/v3.0/oneof-example.ts b/tests/generated/v3.0/oneof-example.ts index 89d00ac4..404c3251 100644 --- a/tests/generated/v3.0/oneof-example.ts +++ b/tests/generated/v3.0/oneof-example.ts @@ -47,6 +47,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -67,6 +68,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -83,14 +85,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -100,7 +100,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -163,7 +163,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -176,8 +176,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -190,19 +191,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/generated/v3.0/personal-api-example.ts b/tests/generated/v3.0/personal-api-example.ts index 611dbc2f..395d7f0c 100644 --- a/tests/generated/v3.0/personal-api-example.ts +++ b/tests/generated/v3.0/personal-api-example.ts @@ -238,6 +238,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -258,6 +259,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -274,14 +276,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -291,7 +291,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -354,7 +354,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -367,8 +367,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -381,19 +382,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/generated/v3.0/petstore-expanded.ts b/tests/generated/v3.0/petstore-expanded.ts index 69b12319..ba61ee3b 100644 --- a/tests/generated/v3.0/petstore-expanded.ts +++ b/tests/generated/v3.0/petstore-expanded.ts @@ -50,6 +50,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -70,6 +71,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -86,14 +88,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -103,7 +103,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -166,7 +166,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -179,8 +179,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -193,19 +194,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/generated/v3.0/petstore.ts b/tests/generated/v3.0/petstore.ts index 746205c7..b7ef4ddb 100644 --- a/tests/generated/v3.0/petstore.ts +++ b/tests/generated/v3.0/petstore.ts @@ -54,6 +54,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -74,6 +75,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -90,14 +92,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -107,7 +107,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -170,7 +170,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -183,8 +183,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -197,19 +198,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/generated/v3.0/recursive-schema.ts b/tests/generated/v3.0/recursive-schema.ts index 1144d17f..9d5a6cf5 100644 --- a/tests/generated/v3.0/recursive-schema.ts +++ b/tests/generated/v3.0/recursive-schema.ts @@ -51,6 +51,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -71,6 +72,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -87,14 +89,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -104,7 +104,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -167,7 +167,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -180,8 +180,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -194,19 +195,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/generated/v3.0/responses.ts b/tests/generated/v3.0/responses.ts index 5e56e815..97611894 100644 --- a/tests/generated/v3.0/responses.ts +++ b/tests/generated/v3.0/responses.ts @@ -37,6 +37,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -57,6 +58,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -73,14 +75,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -90,7 +90,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -153,7 +153,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -166,8 +166,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -180,19 +181,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/generated/v3.0/swaggerhub-template.ts b/tests/generated/v3.0/swaggerhub-template.ts index 6aa1354c..033a48a0 100644 --- a/tests/generated/v3.0/swaggerhub-template.ts +++ b/tests/generated/v3.0/swaggerhub-template.ts @@ -37,6 +37,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -57,6 +58,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -73,14 +75,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -90,7 +90,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -153,7 +153,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -166,8 +166,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -180,19 +181,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/generated/v3.0/tsoa-odd-types-3.0.2.ts b/tests/generated/v3.0/tsoa-odd-types-3.0.2.ts index 0d7a02c4..b0268624 100644 --- a/tests/generated/v3.0/tsoa-odd-types-3.0.2.ts +++ b/tests/generated/v3.0/tsoa-odd-types-3.0.2.ts @@ -158,6 +158,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -178,6 +179,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -194,14 +196,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -211,7 +211,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -274,7 +274,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -287,8 +287,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -301,19 +302,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/generated/v3.0/up-banking.ts b/tests/generated/v3.0/up-banking.ts index 9d94e477..b6e8417d 100644 --- a/tests/generated/v3.0/up-banking.ts +++ b/tests/generated/v3.0/up-banking.ts @@ -9,6 +9,16 @@ * --------------------------------------------------------------- */ +/** + * Bla bla bla foo bar baz + */ +export enum SomeEnumName { + Foo = "Foo", + Bar = "Bar", + Baz = "Baz", + Bad = "Bad", +} + /** * Specifies the type of bank account. Currently returned values are `SAVER` and `TRANSACTIONAL`. @@ -565,6 +575,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -585,6 +596,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -601,14 +613,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -618,7 +628,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -681,7 +691,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -694,8 +704,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -708,19 +719,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); @@ -944,6 +957,7 @@ export class Api extends HttpClient { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -74,6 +75,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -90,14 +92,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -107,7 +107,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -170,7 +170,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -183,8 +183,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -197,19 +198,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/generated/v3.0/wrong-enum-subtypes.ts b/tests/generated/v3.0/wrong-enum-subtypes.ts index d38352df..f1a55537 100644 --- a/tests/generated/v3.0/wrong-enum-subtypes.ts +++ b/tests/generated/v3.0/wrong-enum-subtypes.ts @@ -39,6 +39,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -59,6 +60,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -75,14 +77,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -92,7 +92,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -155,7 +155,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -168,8 +168,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -182,19 +183,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/generated/v3.0/wrong-schema-names.ts b/tests/generated/v3.0/wrong-schema-names.ts index d6a904c4..30f58c9f 100644 --- a/tests/generated/v3.0/wrong-schema-names.ts +++ b/tests/generated/v3.0/wrong-schema-names.ts @@ -61,6 +61,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -81,6 +82,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -97,14 +99,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -114,7 +114,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -177,7 +177,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -190,8 +190,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -204,19 +205,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/helpers/validateGeneratedModule.js b/tests/helpers/validateGeneratedModule.js index 2369e392..07a5efbd 100644 --- a/tests/helpers/validateGeneratedModule.js +++ b/tests/helpers/validateGeneratedModule.js @@ -21,30 +21,22 @@ const getDiagnosticsFromPath = (pathToFile) => // }).diagnostics module.exports = ({ pathToFile }) => { - process.stdout.write(`validating ${relative("", pathToFile)}: `); - const diagnostics = getDiagnosticsFromPath(pathToFile); + const relativePathToFile = relative("", pathToFile); + console.log(`validating ${relativePathToFile}: errors ${diagnostics.length}`); diagnostics.forEach(({ messageText, file, start }) => { var message = tsc.flattenDiagnosticMessageText(messageText, "\n"); if (!file) { - console.error(message); + console.error(`${relativePathToFile}\r\n`, message); return; } var { line, character } = file.getLineAndCharacterOfPosition(start); - console.error(`${file.fileName} (${line + 1},${character + 1}): ${message}`); + console.error( + `${relativePathToFile}\r\n`, + `${file.fileName} (${line + 1},${character + 1}): ${message}`, + ); }); - process.stdout.write(`errors ${diagnostics.length}`); - - if (diagnostics.length) { - process.stdout.write(`\r\n`); - } else { - if (process.stdout.clearLine && process.stdout.cursorTo) { - process.stdout.clearLine(process.stdout); - process.stdout.cursorTo(0); - } - } - return diagnostics; }; diff --git a/tests/schemas/v3.0/up-banking.json b/tests/schemas/v3.0/up-banking.json index 5421f7ef..e6b3fe51 100644 --- a/tests/schemas/v3.0/up-banking.json +++ b/tests/schemas/v3.0/up-banking.json @@ -22,6 +22,12 @@ } }, "schemas": { + "SomeEnumName": { + "enum": ["Foo", "Bar", "Baz", "Bad"], + "type": "string", + "description": "Bla bla bla foo bar baz", + "x-enumNames": ["Foo", "Bar", "Baz", "Bad"] + }, "AccountTypeEnum": { "enum": ["SAVER", "TRANSACTIONAL"], "description": "Specifies the type of bank account. Currently returned values are `SAVER`\nand `TRANSACTIONAL`.\n" @@ -1904,6 +1910,15 @@ "example": "good-life", "description": "The category identifier for which to filter transactions.\nBoth parent and child categories can be filtered through\nthis parameter. Providing an invalid category identifier\nresults in a `404` response.\n" }, + { + "name": "someEnumName", + "in": "query", + "description": "Blablabla bla", + "schema": { + "$ref": "#/components/schemas/SomeEnumName" + }, + "x-enumNames": ["Foo", "Bar", "Baz", "Bad"] + }, { "name": "filter[tag]", "in": "query", diff --git a/tests/spec/axios/schema.ts b/tests/spec/axios/schema.ts index 0aa129ce..9d124903 100644 --- a/tests/spec/axios/schema.ts +++ b/tests/spec/axios/schema.ts @@ -1470,6 +1470,7 @@ export interface ApiConfig extends Omit Promise | AxiosRequestConfig | void; secure?: boolean; + format?: ResponseType; } export enum ContentType { @@ -1483,10 +1484,12 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private secure?: boolean; + private format?: ResponseType; - constructor({ securityWorker, secure, ...axiosConfig }: ApiConfig = {}) { + constructor({ securityWorker, secure, format, ...axiosConfig }: ApiConfig = {}) { this.instance = axios.create({ ...axiosConfig, baseURL: axiosConfig.baseURL || "https://api.github.com" }); this.secure = secure; + this.format = format; this.securityWorker = securityWorker; } @@ -1507,12 +1510,12 @@ export class HttpClient { }; } - public request = async ({ + public request = async ({ secure, path, type, query, - format = "json", + format, body, ...params }: FullRequestParams): Promise> => { @@ -1522,6 +1525,7 @@ export class HttpClient { (await this.securityWorker(this.securityData))) || {}; const requestParams = this.mergeRequestParams(params, secureParams); + const responseFormat = (format && this.format) || void 0; return this.instance.request({ ...requestParams, @@ -1530,7 +1534,7 @@ export class HttpClient { ...(requestParams.headers || {}), }, params: query, - responseType: format, + responseType: responseFormat, data: body, url: path, }); diff --git a/tests/spec/axiosSingleHttpClient/schema.ts b/tests/spec/axiosSingleHttpClient/schema.ts index a987e97e..d3dfc2b8 100644 --- a/tests/spec/axiosSingleHttpClient/schema.ts +++ b/tests/spec/axiosSingleHttpClient/schema.ts @@ -1470,6 +1470,7 @@ export interface ApiConfig extends Omit Promise | AxiosRequestConfig | void; secure?: boolean; + format?: ResponseType; } export enum ContentType { @@ -1483,10 +1484,12 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private secure?: boolean; + private format?: ResponseType; - constructor({ securityWorker, secure, ...axiosConfig }: ApiConfig = {}) { + constructor({ securityWorker, secure, format, ...axiosConfig }: ApiConfig = {}) { this.instance = axios.create({ ...axiosConfig, baseURL: axiosConfig.baseURL || "https://api.github.com" }); this.secure = secure; + this.format = format; this.securityWorker = securityWorker; } @@ -1507,12 +1510,12 @@ export class HttpClient { }; } - public request = async ({ + public request = async ({ secure, path, type, query, - format = "json", + format, body, ...params }: FullRequestParams): Promise> => { @@ -1522,6 +1525,7 @@ export class HttpClient { (await this.securityWorker(this.securityData))) || {}; const requestParams = this.mergeRequestParams(params, secureParams); + const responseFormat = (format && this.format) || void 0; return this.instance.request({ ...requestParams, @@ -1530,7 +1534,7 @@ export class HttpClient { ...(requestParams.headers || {}), }, params: query, - responseType: format, + responseType: responseFormat, data: body, url: path, }); diff --git a/tests/spec/defaultAsSuccess/schema.ts b/tests/spec/defaultAsSuccess/schema.ts index 13f8dcec..49f3737b 100644 --- a/tests/spec/defaultAsSuccess/schema.ts +++ b/tests/spec/defaultAsSuccess/schema.ts @@ -89,6 +89,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -109,6 +110,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -125,14 +127,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -142,7 +142,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -205,7 +205,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -218,8 +218,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -232,19 +233,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/spec/defaultResponse/schema.ts b/tests/spec/defaultResponse/schema.ts index bb09c563..64463587 100644 --- a/tests/spec/defaultResponse/schema.ts +++ b/tests/spec/defaultResponse/schema.ts @@ -37,6 +37,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -57,6 +58,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -73,14 +75,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -90,7 +90,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -153,7 +153,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -166,8 +166,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -180,19 +181,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/spec/enumNamesAsValues/schema.ts b/tests/spec/enumNamesAsValues/schema.ts index 97158894..06f53e2d 100644 --- a/tests/spec/enumNamesAsValues/schema.ts +++ b/tests/spec/enumNamesAsValues/schema.ts @@ -235,6 +235,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -255,6 +256,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -271,14 +273,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -288,7 +288,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -351,7 +351,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -364,8 +364,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -378,19 +379,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/spec/extractRequestParams/schema.ts b/tests/spec/extractRequestParams/schema.ts index d132cd5f..d3de6026 100644 --- a/tests/spec/extractRequestParams/schema.ts +++ b/tests/spec/extractRequestParams/schema.ts @@ -129,6 +129,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -149,6 +150,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -165,14 +167,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -182,7 +182,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -245,7 +245,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -258,8 +258,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -272,19 +273,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/spec/js/schema.d.ts b/tests/spec/js/schema.d.ts index 207daa47..c4030348 100644 --- a/tests/spec/js/schema.d.ts +++ b/tests/spec/js/schema.d.ts @@ -1727,6 +1727,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { data: D; @@ -1743,10 +1744,11 @@ export declare class HttpClient { private securityData; private securityWorker?; private abortControllers; + private customFetch; private baseApiParams; constructor(apiConfig?: ApiConfig); setSecurityData: (data: SecurityDataType | null) => void; - private addQueryParam; + private addArrayQueryParam; protected toQueryString(rawQuery?: QueryParamsType): string; protected addQueryParams(rawQuery?: QueryParamsType): string; private contentFormatters; diff --git a/tests/spec/js/schema.js b/tests/spec/js/schema.js index cbd9910f..156726b9 100644 --- a/tests/spec/js/schema.js +++ b/tests/spec/js/schema.js @@ -20,6 +20,7 @@ export class HttpClient { this.baseUrl = "https://api.github.com"; this.securityData = null; this.abortControllers = new Map(); + this.customFetch = fetch; this.baseApiParams = { credentials: "same-origin", headers: {}, @@ -58,7 +59,7 @@ export class HttpClient { this.abortControllers.delete(cancelToken); } }; - this.request = async ({ body, secure, path, type, query, format = "json", baseUrl, cancelToken, ...params }) => { + this.request = async ({ body, secure, path, type, query, format, baseUrl, cancelToken, ...params }) => { const secureParams = ((typeof secure === "boolean" ? secure : this.baseApiParams.secure) && this.securityWorker && @@ -67,7 +68,8 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + const responseFormat = format && requestParams.format; + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -79,19 +81,21 @@ export class HttpClient { const r = response; r.data = null; r.error = null; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); } @@ -101,13 +105,12 @@ export class HttpClient { }; Object.assign(this, apiConfig); } - addQueryParam(query, key) { + addArrayQueryParam(query, key) { const value = query[key]; - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } toQueryString(rawQuery) { const query = rawQuery || {}; @@ -116,7 +119,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key]) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } diff --git a/tests/spec/jsAxios/schema.d.ts b/tests/spec/jsAxios/schema.d.ts index b908b8a1..8dd6346f 100644 --- a/tests/spec/jsAxios/schema.d.ts +++ b/tests/spec/jsAxios/schema.d.ts @@ -1724,6 +1724,7 @@ export interface ApiConfig extends Omit Promise | AxiosRequestConfig | void; secure?: boolean; + format?: ResponseType; } export declare enum ContentType { Json = "application/json", @@ -1735,10 +1736,11 @@ export declare class HttpClient { private securityData; private securityWorker?; private secure?; - constructor({ securityWorker, secure, ...axiosConfig }?: ApiConfig); + private format?; + constructor({ securityWorker, secure, format, ...axiosConfig }?: ApiConfig); setSecurityData: (data: SecurityDataType | null) => void; private mergeRequestParams; - request: ({ + request: ({ secure, path, type, diff --git a/tests/spec/jsAxios/schema.js b/tests/spec/jsAxios/schema.js index fc81ef3f..226efe0e 100644 --- a/tests/spec/jsAxios/schema.js +++ b/tests/spec/jsAxios/schema.js @@ -17,18 +17,19 @@ export var ContentType; ContentType["UrlEncoded"] = "application/x-www-form-urlencoded"; })(ContentType || (ContentType = {})); export class HttpClient { - constructor({ securityWorker, secure, ...axiosConfig } = {}) { + constructor({ securityWorker, secure, format, ...axiosConfig } = {}) { this.securityData = null; this.setSecurityData = (data) => { this.securityData = data; }; - this.request = async ({ secure, path, type, query, format = "json", body, ...params }) => { + this.request = async ({ secure, path, type, query, format, body, ...params }) => { const secureParams = ((typeof secure === "boolean" ? secure : this.secure) && this.securityWorker && (await this.securityWorker(this.securityData))) || {}; const requestParams = this.mergeRequestParams(params, secureParams); + const responseFormat = (format && this.format) || void 0; return this.instance.request({ ...requestParams, headers: { @@ -36,13 +37,14 @@ export class HttpClient { ...(requestParams.headers || {}), }, params: query, - responseType: format, + responseType: responseFormat, data: body, url: path, }); }; this.instance = axios.create({ ...axiosConfig, baseURL: axiosConfig.baseURL || "https://api.github.com" }); this.secure = secure; + this.format = format; this.securityWorker = securityWorker; } mergeRequestParams(params1, params2) { diff --git a/tests/spec/modular/http-client.ts b/tests/spec/modular/http-client.ts index f2f70b5c..985d4401 100644 --- a/tests/spec/modular/http-client.ts +++ b/tests/spec/modular/http-client.ts @@ -37,6 +37,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -57,6 +58,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -73,14 +75,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -90,7 +90,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -153,7 +153,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -166,8 +166,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -180,19 +181,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/spec/moduleNameFirstTag/schema.ts b/tests/spec/moduleNameFirstTag/schema.ts index 455e2641..ef250ad5 100644 --- a/tests/spec/moduleNameFirstTag/schema.ts +++ b/tests/spec/moduleNameFirstTag/schema.ts @@ -165,6 +165,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -185,6 +186,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -201,14 +203,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -218,7 +218,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -281,7 +281,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -294,8 +294,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -308,19 +309,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/spec/moduleNameIndex/schema.ts b/tests/spec/moduleNameIndex/schema.ts index c3d68aec..3f4ec33b 100644 --- a/tests/spec/moduleNameIndex/schema.ts +++ b/tests/spec/moduleNameIndex/schema.ts @@ -165,6 +165,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -185,6 +186,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -201,14 +203,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -218,7 +218,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -281,7 +281,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -294,8 +294,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -308,19 +309,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/spec/partialBaseTemplate/schema.ts b/tests/spec/partialBaseTemplate/schema.ts index 2691f04d..381b89ae 100644 --- a/tests/spec/partialBaseTemplate/schema.ts +++ b/tests/spec/partialBaseTemplate/schema.ts @@ -49,6 +49,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -69,6 +70,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -85,14 +87,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -102,7 +102,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -165,7 +165,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -178,8 +178,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -192,19 +193,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/spec/partialDefaultTemplate/schema.ts b/tests/spec/partialDefaultTemplate/schema.ts index 3e853066..a78115ce 100644 --- a/tests/spec/partialDefaultTemplate/schema.ts +++ b/tests/spec/partialDefaultTemplate/schema.ts @@ -45,6 +45,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -65,6 +66,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -81,14 +83,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -98,7 +98,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -161,7 +161,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -174,8 +174,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -188,19 +189,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/spec/responses/schema.ts b/tests/spec/responses/schema.ts index f67ef1ad..d2d30a1c 100644 --- a/tests/spec/responses/schema.ts +++ b/tests/spec/responses/schema.ts @@ -89,6 +89,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -109,6 +110,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -125,14 +127,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -142,7 +142,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -205,7 +205,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -218,8 +218,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -232,19 +233,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/spec/singleHttpClient/schema.ts b/tests/spec/singleHttpClient/schema.ts index 324b4ab1..033b0640 100644 --- a/tests/spec/singleHttpClient/schema.ts +++ b/tests/spec/singleHttpClient/schema.ts @@ -37,6 +37,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -57,6 +58,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -73,14 +75,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -90,7 +90,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -153,7 +153,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -166,8 +166,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -180,19 +181,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/spec/templates/schema.ts b/tests/spec/templates/schema.ts index 25209a8f..79756117 100644 --- a/tests/spec/templates/schema.ts +++ b/tests/spec/templates/schema.ts @@ -1579,7 +1579,7 @@ export class HttpClient { }; } -/* CUSTOM TEMPLATE */ +/* HTTP CLIENT TEMPLATE */ /** * @title GitHub * @version v3 diff --git a/tests/spec/templates/spec_templates/http-client.eta b/tests/spec/templates/spec_templates/http-client.eta index 15f3a468..c2fc9560 100644 --- a/tests/spec/templates/spec_templates/http-client.eta +++ b/tests/spec/templates/spec_templates/http-client.eta @@ -137,4 +137,4 @@ export class HttpClient<<%~ apiConfig.generic.map(g => `${g.name} = unknown`).jo } } -/* CUSTOM TEMPLATE */ \ No newline at end of file +/* HTTP CLIENT TEMPLATE */ \ No newline at end of file diff --git a/tests/spec/typeSuffixPrefix/schema.ts b/tests/spec/typeSuffixPrefix/schema.ts index 818275f5..4a957753 100644 --- a/tests/spec/typeSuffixPrefix/schema.ts +++ b/tests/spec/typeSuffixPrefix/schema.ts @@ -1482,6 +1482,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -1502,6 +1503,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -1518,14 +1520,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -1535,7 +1535,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -1598,7 +1598,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -1611,8 +1611,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -1625,19 +1626,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken); diff --git a/tests/spec/unionEnums/schema.ts b/tests/spec/unionEnums/schema.ts index cb14c7a3..84a58df1 100644 --- a/tests/spec/unionEnums/schema.ts +++ b/tests/spec/unionEnums/schema.ts @@ -49,6 +49,7 @@ export interface ApiConfig { baseUrl?: string; baseApiParams?: Omit; securityWorker?: (securityData: SecurityDataType | null) => Promise | RequestParams | void; + customFetch?: (input: RequestInfo, init?: RequestInit) => Promise; } export interface HttpResponse extends Response { @@ -69,6 +70,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private abortControllers = new Map(); + private customFetch = fetch; private baseApiParams: RequestParams = { credentials: "same-origin", @@ -85,14 +87,12 @@ export class HttpClient { this.securityData = data; }; - private addQueryParam(query: QueryParamsType, key: string) { + private addArrayQueryParam(query: QueryParamsType, key: string) { const value = query[key]; - - return ( - encodeURIComponent(key) + - "=" + - encodeURIComponent(Array.isArray(value) ? value.join(",") : typeof value === "number" ? value : `${value}`) - ); + const encodedKey = encodeURIComponent(key); + return `${value + .map((val: any) => `${encodedKey}=${encodeURIComponent(typeof val === "number" ? val : `${val}`)}`) + .join("&")}`; } protected toQueryString(rawQuery?: QueryParamsType): string { @@ -102,7 +102,7 @@ export class HttpClient { .map((key) => typeof query[key] === "object" && !Array.isArray(query[key]) ? this.toQueryString(query[key] as QueryParamsType) - : this.addQueryParam(query, key), + : this.addArrayQueryParam(query, key), ) .join("&"); } @@ -165,7 +165,7 @@ export class HttpClient { path, type, query, - format = "json", + format, baseUrl, cancelToken, ...params @@ -178,8 +178,9 @@ export class HttpClient { const requestParams = this.mergeRequestParams(params, secureParams); const queryString = query && this.toQueryString(query); const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format && requestParams.format; - return fetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { + return this.customFetch(`${baseUrl || this.baseUrl || ""}${path}${queryString ? `?${queryString}` : ""}`, { ...requestParams, headers: { ...(type && type !== ContentType.FormData ? { "Content-Type": type } : {}), @@ -192,19 +193,21 @@ export class HttpClient { r.data = (null as unknown) as T; r.error = (null as unknown) as E; - const data = await response[format]() - .then((data) => { - if (r.ok) { - r.data = data; - } else { - r.error = data; - } - return r; - }) - .catch((e) => { - r.error = e; - return r; - }); + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); if (cancelToken) { this.abortControllers.delete(cancelToken);