Skip to content

Commit

Permalink
Merge pull request #765 from Availity/fix/upload-types
Browse files Browse the repository at this point in the history
fix(upload-core): update types
  • Loading branch information
jordan-a-young authored Oct 22, 2024
2 parents 558c349 + 09936a3 commit 24d79a0
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 32 deletions.
73 changes: 49 additions & 24 deletions packages/upload-core/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,65 +4,90 @@ export interface Options extends UploadOptions {
bucketId: string;
customerId: string;
clientId: string;
endpoint?: string;
maxAvScanRetries?: number;
onPreStart?: (() => boolean)[];
pollingTime?: number;
retryDelays?: number[];
stripFileNamePathSegments?: boolean;
}

export type FileUpload = File | Blob | Pick<ReadableStreamDefaultReader, 'read'>;

declare class Upload {
private upload: TusUpload;
upload: TusUpload;

private options: Options;
options: Options;

private status: 'accepted' | 'pending' | 'rejected' | 'decrypting';
file: File;

private errorMessage: string;
id: string;

private onSuccess: (() => void)[];
avScanRetries: number;

private onError: ((error: Error) => void)[];
bytesScanned: number;

constructor(file: FileUpload, options: Options);
bytesSent: number;

inStatusCategory(status: number, category: number): boolean;
bytesTotal: number;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
scan(data: any): void;
error: Error | null;

getPercentage(): number;
errorMessage: string | null;

getToken(): string;
onError: ((error: Error) => void)[];

start(): void;
onPreStart: (() => boolean)[];

generateId(): string;
onProgress: (() => void)[];

onSuccess: (() => void)[];

percentage: number;

preStartValidationResults: boolean[];

status: 'accepted' | 'pending' | 'rejected' | 'encrypted' | 'decrypting';

timeoutId: NodeJS.Timeout | undefined;

waitForPassword: boolean;

constructor(file: FileUpload, options: Options);

abort(): void;

fingerprint(file: FileUpload, options?: Options, callback?: (arg: null, key: string) => string): string;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
sendPassword(pw: any): void;
generateId(): string;

isValidSize(): boolean;
getPercentage(): number;

isAllowedFileTypes(): boolean;
getResult(xhr: XMLHttpRequest): { status: string; message: string };

getToken(): string;

inStatusCategory(status: number, category: number): boolean;

isAllowedFileNameCharacters(): boolean;

isAllowedFileTypes(): boolean;

isValidFile(): boolean;

trimFileName(fileName: string): string;
isValidSize(): boolean;

getResult(xhr: XMLHttpRequest): { status: string; message: string };
parseErrorMessage(message: string, error?: Error): void;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
scan(data: any): void;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
sendPassword(pw: any): void;

setError(status: string, message: string, error?: Error): void;

parseErrorMessage(message: string, error?: Error): void;
start(): void;

abort(): void;
trimFileName(fileName: string): string;
}

export default Upload;
36 changes: 28 additions & 8 deletions packages/upload-core/src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,60 +63,65 @@ describe('upload-core', () => {

describe('options', () => {
it('should throw error for missing files', () => {
expect(() => {
// eslint-disable-next-line no-new
new Upload();
}).toThrow('[file] must be defined and of type File');
expect(() => new Upload()).toThrow('[file] must be defined and of type File');
});

it('should throw error with missing bucket id', () => {
expect(() => {
// eslint-disable-next-line no-new
new Upload([]);
}).toThrow('[options.bucketId] must be defined');
expect(() => new Upload([])).toThrow('[options.bucketId] must be defined');
});

it('should allow override to defaults', () => {
const file = Buffer.from([...'hello world']);
file.name = 'fileName.png';

const upload = new Upload(file, optionsWithRetry);

expect(upload.options.retryDelays[0]).toBe(optionsWithRetry.retryDelays[0]);
});

it('should not start upload if any one of the functions in onPreStart returns false', () => {
const file = Buffer.from([...'hello world']);
file.name = 'somefile.png';

const upload = new Upload(file, optionsWithOnPreStartFail);

upload.start();

expect(upload.status).toEqual('rejected');
expect(upload.errorMessage).toEqual('preStart validation failed');
});

it('should allow single file as constructor argument', () => {
const file = Buffer.from([...'hello world']);
file.name = 'fileName.png';

const upload = new Upload(file, options);

expect(upload.isValidFile()).toBeTruthy();
});

it('should throw error for invalid file type', () => {
const file = Buffer.from([...'hello world']);
file.name = 'notCoolFile.docx';

const upload = new Upload(file, optionsWithFileTypes);

expect(upload.isValidFile()).toBeFalsy();
});

it('should allow the correct file type', () => {
const file = Buffer.from([...'hello world']);
file.name = 'coolFile.png';

const upload = new Upload(file, optionsWithFileTypes);

expect(upload.isValidFile()).toBeTruthy();
});

it('should use default options', () => {
const file = Buffer.from([...'hello world']);
file.name = 'optionsFile.png';

const upload = new Upload(file, options);

expect(upload.options.endpoint).toBe('https://dev.local/ms/api/availity/internal/core/vault/upload/v1/resumable');
Expand All @@ -126,7 +131,9 @@ describe('upload-core', () => {
const file = Buffer.from([...'hello world!']);
file.name = 'sizeFile.pdf';
file.size = 1e7;

const upload = new Upload(file, optionsWithFileSize);

expect(upload.isValidFile()).toBeFalsy();
});

Expand Down Expand Up @@ -178,9 +185,12 @@ describe('upload-core', () => {
it('should pass status of decrypting', () => {
const file = Buffer.from([...'hello world']);
file.name = 'decryptThisFile.png';

const upload = new Upload(file, options);

upload.setError('encrypted', 'Encrypted files require a password');
upload.setError('decrypting', 'Decrypting file');

expect(upload.status).toBe('decrypting');
});

Expand Down Expand Up @@ -244,7 +254,9 @@ describe('upload-core', () => {

const file = Buffer.from('hello world!');
file.name = 'a';

const upload = new Upload(file, options);

const success = jest.fn();
upload.onSuccess.push(success, () => {
expect(success).toHaveBeenCalled();
Expand Down Expand Up @@ -292,10 +304,12 @@ describe('upload-core', () => {

const file = Buffer.from('hello world!');
file.name = 'a';

const upload = new Upload(file, {
...options,
pollingTime: 50, // so that Jest does not time out our test while waiting for retires
});

const error = jest.fn();
const errorMessage = new Error('AV scan timed out, max retries exceeded');

Expand All @@ -320,6 +334,7 @@ describe('upload-core', () => {
it('should pickup upload object on each array of functions in onPreStart', () => {
const file = Buffer.from('hello world!');
file.name = 'a';

const upload = new Upload(file, {
...optionsWithOnPreStartFail,
onPreStart: [
Expand All @@ -329,6 +344,7 @@ describe('upload-core', () => {
},
],
});

upload.start();
});

Expand Down Expand Up @@ -364,7 +380,9 @@ describe('upload-core', () => {

const file = Buffer.from('hello world!');
file.name = 'a';

const upload = new Upload(file, optionsWithOnPreStartPass);

upload.onSuccess.push(() => {
expect(upload.s3References).toBeDefined();
resolve();
Expand Down Expand Up @@ -411,7 +429,9 @@ describe('upload-core', () => {

const file = Buffer.from('hello world!');
file.name = 'a';

const upload = new Upload(file, optionsWithOnPreStartPass);

const success = jest.fn();
upload.onSuccess.push(success, () => {
expect(success).toHaveBeenCalled();
Expand Down

0 comments on commit 24d79a0

Please sign in to comment.