diff --git a/packages/swagger-typescript-client-generator-runtime/lib/whatwg-fetch.js b/packages/swagger-typescript-client-generator-runtime/lib/whatwg-fetch.js index bb7d73a..1d7bd5a 100644 --- a/packages/swagger-typescript-client-generator-runtime/lib/whatwg-fetch.js +++ b/packages/swagger-typescript-client-generator-runtime/lib/whatwg-fetch.js @@ -23,6 +23,9 @@ var WhatWgFetchRequestFactory = function (baseUrl, options) { return function (p return data; }, new FormData()); } + else if (formData) { + fetchOptions.body = formData; + } var hasQuery = query && Object.keys(query).length > 0; var fullUrl = [ baseUrl, diff --git a/packages/swagger-typescript-client-generator-runtime/src/whatwg-fetch.ts b/packages/swagger-typescript-client-generator-runtime/src/whatwg-fetch.ts index c597903..3432efe 100644 --- a/packages/swagger-typescript-client-generator-runtime/src/whatwg-fetch.ts +++ b/packages/swagger-typescript-client-generator-runtime/src/whatwg-fetch.ts @@ -11,50 +11,50 @@ export interface WhatWgFetchRequestFactoryOptions { fetch?: WhatWgFetchFunctionType } -export const WhatWgFetchRequestFactory = ( - baseUrl: string, - options: WhatWgFetchRequestFactoryOptions -): RequestFactoryType => ( - path, - query, - body, - formData, - headers, - method, - configuration: never -) => { - const headersObject = new Headers(options.requestInit.headers || {}) - - new Headers(headers).forEach((value, key) => { - headersObject.set(key, String(value)) - }) - - const fetchOptions: RequestInit = Object.assign({}, options.requestInit, { - method: method, - headers: headersObject - }) - - if (body && typeof body === "string") { - fetchOptions.body = body - } else if (body && typeof body === "object" && Object.keys(body).length > 0) { - fetchOptions.body = JSON.stringify(body) - } else if (formData && Object.keys(formData).length > 0) { - fetchOptions.body = Object.keys(formData).reduce((data, key) => { - data.append(key, formData[key]) - return data - }, new FormData()) +export const WhatWgFetchRequestFactory = + ( + baseUrl: string, + options: WhatWgFetchRequestFactoryOptions + ): RequestFactoryType => + (path, query, body, formData, headers, method, configuration: never) => { + const headersObject = new Headers(options.requestInit.headers || {}) + + new Headers(headers).forEach((value, key) => { + headersObject.set(key, String(value)) + }) + + const fetchOptions: RequestInit = Object.assign({}, options.requestInit, { + method: method, + headers: headersObject, + }) + + if (body && typeof body === "string") { + fetchOptions.body = body + } else if ( + body && + typeof body === "object" && + Object.keys(body).length > 0 + ) { + fetchOptions.body = JSON.stringify(body) + } else if (formData && Object.keys(formData).length > 0) { + fetchOptions.body = Object.keys(formData).reduce((data, key) => { + data.append(key, formData[key]) + return data + }, new FormData()) + } else if (formData) { + fetchOptions.body = formData + } + + const hasQuery = query && Object.keys(query).length > 0 + const fullUrl = [ + baseUrl, + path, + hasQuery ? (path.includes("?") ? "&" : "?") : "", + hasQuery ? serialize(query) : "", + ].join("") + + const callback: WhatWgFetchFunctionType = + typeof options.fetch === "function" ? options.fetch : fetch + + return callback(fullUrl, fetchOptions) } - - const hasQuery = query && Object.keys(query).length > 0 - const fullUrl = [ - baseUrl, - path, - hasQuery ? (path.includes("?") ? "&" : "?") : "", - hasQuery ? serialize(query) : "" - ].join("") - - const callback: WhatWgFetchFunctionType = - typeof options.fetch === "function" ? options.fetch : fetch - - return callback(fullUrl, fetchOptions) -}