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: do not throw when res is missing url #5132

Merged
merged 1 commit into from
May 2, 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
9 changes: 3 additions & 6 deletions packages/@uppy/xhr-upload/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,9 @@ export default class XHRUpload<
}

const body = this.opts.getResponseData(res.responseText, res)
const uploadURL = body[this.opts.responseUrlFieldName]
if (typeof uploadURL !== 'string') {
throw new Error(
`The received response did not include a valid URL for key ${this.opts.responseUrlFieldName}`,
)
}
const uploadURL = body?.[this.opts.responseUrlFieldName] as
| string
| undefined
Comment on lines +242 to +244
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
const uploadURL = body?.[this.opts.responseUrlFieldName] as
| string
| undefined
const uploadURL = body[this.opts.responseUrlFieldName]
if (!(typeof uploadURL === 'string' || uploadURL == null)) {
throw new Error(
`The received response did not include a valid URL for option responseUrlFieldName ${this.opts.responseUrlFieldName}`,
)
}

Copy link
Member Author

Choose a reason for hiding this comment

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

As you can read in the issue and my comment, the whole point is that this should not be required.

Copy link
Contributor

Choose a reason for hiding this comment

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

yes, and i think my suggested code is a safer way to check that rather than using a blind type assertion

Copy link
Member Author

Choose a reason for hiding this comment

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

This PR removes the error, an undefined url is allowed. It was a breaking change to throw an error. You suggestion makes the exact same again as before this PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

i'm not sure how my suggestion would make it the exact same as before this PR, as i added a check that allows undefined and null (== operator).

If uploadURL is undefined, !(typeof uploadURL === 'string' || uploadURL == null) evaluates to false, hence it will not throw the error.


for (const file of files) {
this.uppy.emit('upload-success', file, {
Expand Down
Loading