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

@uppy/xhr-upload: fix getResponseData regression #4964

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 11 additions & 5 deletions packages/@uppy/xhr-upload/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ export interface XhrUploadOpts<M extends Meta, B extends Body>
body: string,
xhr: XMLHttpRequest,
) => boolean
getResponseData?: (
body: string,
xhr: XMLHttpRequest,
) => NonNullable<UppyFile<M, B>['response']>['body']
getResponseData?: (body: string, xhr: XMLHttpRequest) => B
getResponseError?: (body: string, xhr: XMLHttpRequest) => Error | NetworkError
allowedMetaFields?: string[] | null
bundle?: boolean
Expand Down Expand Up @@ -117,7 +114,16 @@ const defaultOptions = {
withCredentials: false,
responseType: '',
getResponseData(responseText) {
return JSON.parse(responseText)
let parsedResponse = {}
try {
parsedResponse = JSON.parse(responseText)
} catch {
// ignore
}
// We don't have access to the B (Body) generic here
// so we have to cast it to any. The user facing types
// remain correct, this is only to please the merging of default options.
return parsedResponse as any
Comment on lines +117 to +126
Copy link
Member

@aduh95 aduh95 Feb 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think an empty object is a correct response here, undefined would be more appropriate, no?

Suggested change
let parsedResponse = {}
try {
parsedResponse = JSON.parse(responseText)
} catch {
// ignore
}
// We don't have access to the B (Body) generic here
// so we have to cast it to any. The user facing types
// remain correct, this is only to please the merging of default options.
return parsedResponse as any
try {
return JSON.parse(responseText)
} catch {
// ignore
}

or

Suggested change
let parsedResponse = {}
try {
parsedResponse = JSON.parse(responseText)
} catch {
// ignore
}
// We don't have access to the B (Body) generic here
// so we have to cast it to any. The user facing types
// remain correct, this is only to please the merging of default options.
return parsedResponse as any
if (responseText) return JSON.parse(responseText)

Copy link
Member Author

@Murderlon Murderlon Feb 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a regression because it used to return {}, until I accidentally removed it in #4892. So {} is what is has to be for now.

},
getResponseError(_, response) {
let error = new Error('Upload error')
Expand Down