Skip to content

Commit

Permalink
fix: fix #34 fetch request factory formData propagation
Browse files Browse the repository at this point in the history
  • Loading branch information
psrebniak committed Sep 28, 2021
1 parent db9ca36 commit 344bd59
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 46 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 344bd59

Please sign in to comment.