Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(treaty2): file getting sent as json #73

Merged
merged 2 commits into from
Apr 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 50 additions & 63 deletions src/treaty2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,71 +195,33 @@ const createProxy = (
}

return (async () => {
let contentType: string =
(headers instanceof Headers
? headers.get('content-type')
: Array.isArray(headers)
? headers.find((x) => {
if (Array.isArray(x))
if (x[0] === 'headers') return x[1]

return false
})
: typeof headers === 'function'
? headers(path, options ?? {})
: headers?.contentType) ||
options?.headers?.contentType

let fetchInit = {
method: method?.toUpperCase(),
body,
...conf,
headers
} satisfies FetchRequestInit

fetchInit.headers = {
...headers,
...processHeaders(
// For GET and HEAD, options is moved to body (1st param)
isGetOrHead ? body?.headers : options?.headers,
path,
fetchInit
)
}

const fetchOpts =
isGetOrHead && typeof body === 'object'
? body.fetch
: options?.fetch

fetchInit = {
...fetchInit,
...fetchOpts
}

if (isGetOrHead) delete fetchInit.body

if (onRequest) {
if (!Array.isArray(onRequest)) onRequest = [onRequest]

for (const value of onRequest) {
const temp = await value(path, fetchInit)

if (typeof temp === 'object')
fetchInit = {
...fetchInit,
...temp,
headers: {
...fetchInit.headers,
...processHeaders(
temp.headers,
path,
fetchInit
)
}
}
headers: {
...(headers as Record<string, string>),
'content-type': contentType
}
}

// ? Duplicate because end-user might add a body in onRequest
if (isGetOrHead) delete fetchInit.body
} satisfies FetchRequestInit

if (
fetchInit.body !== undefined &&
!fetchInit.headers['content-type']
)
if (typeof fetchInit.body === 'object') {
;(fetchInit.headers as Record<string, string>)[
'content-type'
] = 'application/json'

fetchInit.body = JSON.stringify(fetchInit.body)
} else if (hasFile(fetchInit.body)) {
if (!contentType)
if (hasFile(body)) {
const formData = new FormData()

// FormData is 1 level deep
Expand Down Expand Up @@ -311,12 +273,37 @@ const createProxy = (
formData.append(key, field as string)
}

// contentType = 'multipart/form-data'
fetchInit.body = formData
} else if (fetchInit.body !== undefined) {
;(fetchInit.headers as Record<string, string>)[
'content-type'
] = 'text/plain'
} else if (typeof body === 'object') {
contentType = 'application/json'

fetchInit.body = JSON.stringify(body)
} else if (body !== undefined && body !== null)
contentType = 'text/plain'

if (contentType === undefined)
delete fetchInit.headers['content-type']

if (isGetOrHead) delete fetchInit.body

if (onRequest) {
if (!Array.isArray(onRequest)) onRequest = [onRequest]

for (const value of onRequest) {
const temp = await value(path, fetchInit)

if (typeof temp === 'object')
fetchInit = {
...fetchInit,
...temp,
headers: {
...fetchInit.headers,
...temp.headers
}
}
}
}

const url = domain + path + q
const response = await (elysia?.handle(
Expand Down
Loading