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: migrate to TS #4892

Merged
merged 5 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
7 changes: 2 additions & 5 deletions packages/@uppy/companion-client/src/RequestClient.ts
Copy link
Contributor

@aduh95 aduh95 Jan 25, 2024

Choose a reason for hiding this comment

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

Is is OK make that change in a separate PR as it's very likely going to be a pain for AWS as well?

Copy link
Member Author

Choose a reason for hiding this comment

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

If you think this PR is ready to go I can also just merge it in here? If it's blocking and not approved I can do separate PR.

Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ import getSocketHost from '@uppy/utils/lib/getSocketHost'

import type Uppy from '@uppy/core'
import type { UppyFile, Meta, Body } from '@uppy/utils/lib/UppyFile'
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore untyped because we're getting rid of it
import type { RateLimitedQueue } from '@uppy/utils/lib/RateLimitedQueue'
import AuthError from './AuthError.ts'
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore We don't want TS to generate types for the package.json
Expand Down Expand Up @@ -249,7 +246,7 @@ export default class RequestClient<M extends Meta, B extends Body> {
async uploadRemoteFile(
file: UppyFile<M, B>,
reqBody: Record<string, unknown>,
options: { signal: AbortSignal; getQueue: () => RateLimitedQueue },
options: { signal: AbortSignal; getQueue: () => any },
): Promise<void> {
try {
const { signal, getQueue } = options || {}
Expand Down Expand Up @@ -376,7 +373,7 @@ export default class RequestClient<M extends Meta, B extends Body> {
signal,
}: {
file: UppyFile<M, B>
queue: RateLimitedQueue
queue: any
signal: AbortSignal
}): Promise<void> {
let removeEventHandlers: () => void
Expand Down
10 changes: 6 additions & 4 deletions packages/@uppy/core/src/Uppy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,9 @@ type ErrorCallback<M extends Meta, B extends Body> = (
type UploadErrorCallback<M extends Meta, B extends Body> = (
file: UppyFile<M, B> | undefined,
error: { message: string; details?: string },
response?: UppyFile<M, B>['response'] | undefined,
response?:
| Omit<NonNullable<UppyFile<M, B>['response']>, 'uploadURL'>
| undefined,
) => void
type UploadStalledCallback<M extends Meta, B extends Body> = (
error: { message: string; details?: string },
Expand Down Expand Up @@ -1376,7 +1378,7 @@ export class Uppy<M extends Meta, B extends Body> {
if (sizedFiles.length === 0) {
const progressMax = inProgress.length * 100
const currentProgress = unsizedFiles.reduce((acc, file) => {
return acc + file.progress.percentage
return acc + (file.progress.percentage as number)
}, 0)
const totalProgress = Math.round((currentProgress / progressMax) * 100)
this.setState({ totalProgress })
Expand Down Expand Up @@ -1871,7 +1873,7 @@ export class Uppy<M extends Meta, B extends Body> {
}

/** @protected */
getRequestClientForFile(file: UppyFile<M, B>): unknown {
getRequestClientForFile<Client>(file: UppyFile<M, B>): Client {
if (!file.remote)
throw new Error(
`Tried to get RequestClient for a non-remote file ${file.id}`,
Expand All @@ -1883,7 +1885,7 @@ export class Uppy<M extends Meta, B extends Body> {
throw new Error(
`requestClientId "${file.remote.requestClientId}" not registered for file "${file.id}"`,
)
return requestClient
return requestClient as Client
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/@uppy/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { default } from './Uppy.ts'
export { default as Uppy, type UppyEventMap } from './Uppy.ts'
export { default as Uppy, type UppyEventMap, type State } from './Uppy.ts'
export { default as UIPlugin } from './UIPlugin.ts'
export { default as BasePlugin } from './BasePlugin.ts'
export { debugLogger } from './loggers.ts'
Expand Down
6 changes: 3 additions & 3 deletions packages/@uppy/utils/src/FileProgress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export type FileProcessingInfo =

interface FileProgressBase {
progress?: number
uploadComplete: boolean
percentage: number
uploadComplete?: boolean
percentage?: number
bytesTotal: number
preprocess?: FileProcessingInfo
postprocess?: FileProcessingInfo
Expand All @@ -26,7 +26,7 @@ interface FileProgressBase {
export type FileProgressStarted = FileProgressBase & {
uploadStarted: number
bytesUploaded: number
progress: number
progress?: number
}
export type FileProgressNotStarted = FileProgressBase & {
uploadStarted: null
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 @@ -37,6 +37,6 @@ export interface UppyFile<M extends Meta, B extends Body> {
body: B
status: number
bytesUploaded?: number
uploadURL: string
uploadURL?: string
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { vi, describe, it, expect } from 'vitest'
import nock from 'nock'
import Core from '@uppy/core'
import XHRUpload from './index.js'
import XHRUpload from './index.ts'

describe('XHRUpload', () => {
describe('getResponseData', () => {
Expand All @@ -11,11 +12,14 @@ describe('XHRUpload', () => {
'access-control-allow-method': 'POST',
'access-control-allow-origin': '*',
})
.options('/').reply(200, {})
.post('/').reply(200, {})
.options('/')
.reply(200, {})
.post('/')
.reply(200, {})

const core = new Core()
const getResponseData = vi.fn(function getResponseData () {
const getResponseData = vi.fn(function getResponseData() {
// @ts-expect-error TS can't know the type
expect(this.some).toEqual('option')
return {}
})
Expand All @@ -26,6 +30,8 @@ describe('XHRUpload', () => {
getResponseData,
})
core.addFile({
type: 'image/png',
source: 'test',
name: 'test.jpg',
data: new Blob([new Uint8Array(8192)]),
})
Expand All @@ -43,8 +49,10 @@ describe('XHRUpload', () => {
'access-control-allow-method': 'POST',
'access-control-allow-origin': '*',
})
.options('/').reply(200, {})
.post('/').reply(200, {
.options('/')
.reply(200, {})
.post('/')
.reply(200, {
code: 40000,
message: 'custom upload error',
})
Expand All @@ -59,19 +67,21 @@ describe('XHRUpload', () => {
endpoint: 'https://fake-endpoint.uppy.io',
some: 'option',
validateStatus,
getResponseError (responseText) {
getResponseError(responseText) {
return JSON.parse(responseText).message
},
})
core.addFile({
type: 'image/png',
source: 'test',
name: 'test.jpg',
data: new Blob([new Uint8Array(8192)]),
})

return core.upload().then(result => {
return core.upload().then((result) => {
expect(validateStatus).toHaveBeenCalled()
expect(result.failed.length).toBeGreaterThan(0)
result.failed.forEach(file => {
expect(result!.failed!.length).toBeGreaterThan(0)
result!.failed!.forEach((file) => {
expect(file.error).toEqual('custom upload error')
})
})
Expand All @@ -80,17 +90,13 @@ describe('XHRUpload', () => {

describe('headers', () => {
it('can be a function', async () => {
const scope = nock('https://fake-endpoint.uppy.io')
.defaultReplyHeaders({
'access-control-allow-method': 'POST',
'access-control-allow-origin': '*',
'access-control-allow-headers': 'x-sample-header',
})
scope.options('/')
.reply(200, {})
scope.post('/')
.matchHeader('x-sample-header', 'test.jpg')
.reply(200, {})
const scope = nock('https://fake-endpoint.uppy.io').defaultReplyHeaders({
'access-control-allow-method': 'POST',
'access-control-allow-origin': '*',
'access-control-allow-headers': 'x-sample-header',
})
scope.options('/').reply(200, {})
scope.post('/').matchHeader('x-sample-header', 'test.jpg').reply(200, {})

const core = new Core()
core.use(XHRUpload, {
Expand All @@ -101,6 +107,8 @@ describe('XHRUpload', () => {
}),
})
core.addFile({
type: 'image/png',
source: 'test',
name: 'test.jpg',
data: new Blob([new Uint8Array(8192)]),
})
Expand Down
Loading
Loading