Skip to content

Commit

Permalink
@uppy/xhr-upload: fix when responseType is set to JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
Murderlon committed Feb 17, 2025
1 parent 4f04568 commit a2ea691
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions packages/@uppy/xhr-upload/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,18 @@ export default class XHRUpload<
})

let body = await this.opts.getResponseData?.(res)
try {
body ??= JSON.parse(res.responseText) as B
} catch (cause) {
throw new Error(
'@uppy/xhr-upload expects a JSON response (with a `url` property). To parse non-JSON responses, use `getResponseData` to turn your response into JSON.',
{ cause },
)

if (res.responseType === 'json') {
body ??= res.response
} else {
try {
body ??= JSON.parse(res.responseText) as B
} catch (cause) {
throw new Error(
'@uppy/xhr-upload expects a JSON response (with a `url` property). To parse non-JSON responses, use `getResponseData` to turn your response into JSON.',
{ cause },
)
}
}

const uploadURL = typeof body?.url === 'string' ? body.url : undefined
Expand Down

0 comments on commit a2ea691

Please sign in to comment.