Skip to content

Commit

Permalink
improvements for #4922 (#4960)
Browse files Browse the repository at this point in the history
* prevent "any" which disables typechecking

* make body optional in response (because it is)

* add comment
  • Loading branch information
mifi authored Feb 28, 2024
1 parent 448e667 commit 8dba027
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
10 changes: 8 additions & 2 deletions packages/@uppy/companion-client/src/RequestClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,12 @@ export default class RequestClient<M extends Meta, B extends Body> {
break
}
case 'success': {
// payload.response exists for xhr-upload but not for tus/transloadit
// payload.response is sent from companion for xhr-upload (aka uploadMultipart in companion) and
// s3 multipart (aka uploadS3Multipart)
// but not for tus/transloadit (aka uploadTus)
// responseText is a string which may or may not be in JSON format
// this means that an upload destination of xhr or s3 multipart MUST respond with valid JSON
// to companion, or the JSON.parse will crash
const text = payload.response?.responseText

this.uppy.emit(
Expand All @@ -488,7 +493,8 @@ export default class RequestClient<M extends Meta, B extends Body> {
{
uploadURL: payload.url,
status: payload.response?.status ?? 200,
body: text ? JSON.parse(text) : undefined,
body:
text ? (JSON.parse(text) as B) : undefined,
},
)
socketAbortController?.abort?.()
Expand Down
2 changes: 1 addition & 1 deletion packages/@uppy/utils/src/UppyFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export interface UppyFile<M extends Meta, B extends Body> {
type: string
uploadURL?: string
response?: {
body: B
body?: B
status: number
bytesUploaded?: number
uploadURL?: string
Expand Down
4 changes: 3 additions & 1 deletion packages/@uppy/xhr-upload/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,9 @@ export default class XHRUpload<

if (opts.validateStatus(xhr.status, xhr.responseText, xhr)) {
const body = opts.getResponseData(xhr.responseText, xhr)
const uploadURL = body[opts.responseUrlFieldName] as string
const uploadURL = body?.[opts.responseUrlFieldName] as
| string
| undefined

const uploadResp = {
status: xhr.status,
Expand Down

0 comments on commit 8dba027

Please sign in to comment.