Skip to content

Commit

Permalink
@uppy/tus: migrate to TS (#4899)
Browse files Browse the repository at this point in the history
Co-authored-by: Mikael Finstad <finstaden@gmail.com>
  • Loading branch information
Murderlon and mifi authored Feb 5, 2024
1 parent d127be6 commit 4d03bba
Show file tree
Hide file tree
Showing 6 changed files with 329 additions and 169 deletions.
39 changes: 0 additions & 39 deletions packages/@uppy/tus/src/getFingerprint.js

This file was deleted.

44 changes: 44 additions & 0 deletions packages/@uppy/tus/src/getFingerprint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type { Body, Meta, UppyFile } from '@uppy/utils/lib/UppyFile'
import * as tus from 'tus-js-client'

function isCordova() {
return (
typeof window !== 'undefined' &&
// @ts-expect-error may exist
(typeof window.PhoneGap !== 'undefined' ||
// @ts-expect-error may exist
typeof window.Cordova !== 'undefined' ||
// @ts-expect-error may exist
typeof window.cordova !== 'undefined')
)
}

function isReactNative() {
return (
typeof navigator !== 'undefined' &&
typeof navigator.product === 'string' &&
navigator.product.toLowerCase() === 'reactnative'
)
}

// We override tus fingerprint to uppy’s `file.id`, since the `file.id`
// now also includes `relativePath` for files added from folders.
// This means you can add 2 identical files, if one is in folder a,
// the other in folder b — `a/file.jpg` and `b/file.jpg`, when added
// together with a folder, will be treated as 2 separate files.
//
// For React Native and Cordova, we let tus-js-client’s default
// fingerprint handling take charge.
export default function getFingerprint<M extends Meta, B extends Body>(
uppyFile: UppyFile<M, B>,
): tus.UploadOptions['fingerprint'] {
return (file, options) => {
if (isCordova() || isReactNative()) {
return tus.defaultOptions.fingerprint!(file, options)
}

const uppyFingerprint = ['tus', uppyFile.id, options!.endpoint].join('-')

return Promise.resolve(uppyFingerprint)
}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,38 @@
import { describe, expect, it } from 'vitest'
import Core from '@uppy/core'
import Tus from './index.js'
import Tus from './index.ts'

describe('Tus', () => {
it('Throws errors if autoRetry option is true', () => {
const uppy = new Core()

expect(() => {
// @ts-expect-error removed
uppy.use(Tus, { autoRetry: true })
}).toThrowError(/The `autoRetry` option was deprecated and has been removed/)
}).toThrowError(
/The `autoRetry` option was deprecated and has been removed/,
)
})

it('Throws errors if autoRetry option is false', () => {
const uppy = new Core()

expect(() => {
// @ts-expect-error removed
uppy.use(Tus, { autoRetry: false })
}).toThrowError(/The `autoRetry` option was deprecated and has been removed/)
}).toThrowError(
/The `autoRetry` option was deprecated and has been removed/,
)
})

it('Throws errors if autoRetry option is `undefined`', () => {
const uppy = new Core()

expect(() => {
// @ts-expect-error removed
uppy.use(Tus, { autoRetry: undefined })
}).toThrowError(/The `autoRetry` option was deprecated and has been removed/)
}).toThrowError(
/The `autoRetry` option was deprecated and has been removed/,
)
})
})
Loading

0 comments on commit 4d03bba

Please sign in to comment.