All files / src/lib remote.mts

100% Statements 68/68
100% Branches 17/17
100% Functions 14/14
100% Lines 68/68

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87    1x     1x 13x   1x 2x 2x 2x 2x 2x 2x   1x 2x 4x 4x 4x 4x 4x 2x 2x   1x 2x 2x 2x   1x 2x 4x 4x 2x 2x   1x 2x 2x 2x   1x 2x 4x 4x 2x 2x   1x 6x 6x   1x 6x 6x 1x   1x   1x 4x 4x   1x 1x 1x 1x 1x 1x   1x 1x 1x 1x   1x 1x 1x 1x   1x 3x 3x 1x  
import { BlobFileSource, RemoteFileSource } from './types/args.mts';
import { RemoteFileBuilderType, RemoteFileListBuilderType } from './types/builder.mts';
import { buildFile, buildFileList, remoteFileSourceConverter } from './types/helper.mts';
 
/* Remote Class */
export class RemoteFileListBuilder implements RemoteFileListBuilderType {
  private files: (Promise<File> | File)[] = [];
 
  addBlob(blob: BlobFileSource): this {
    const object = new File([blob.blob], blob.name, {
      type: blob.mimeType,
    });
    this.files.push(object);
    return this;
  }
 
  addBlobs(blobs: BlobFileSource[]): this {
    for (const blob of blobs) {
      const object = new File([blob.blob], blob.name, {
        type: blob.mimeType,
      });
      this.files.push(object);
    }
    return this;
  }
 
  addFile(file: RemoteFileSource): this {
    this.files.push(remoteFileSourceConverter(file));
    return this;
  }
 
  addFiles(files: RemoteFileSource[]): this {
    for (const file of files) {
      this.files.push(remoteFileSourceConverter(file));
    }
    return this;
  }
 
  addFileObject(file: File): this {
    this.files.push(file);
    return this;
  }
 
  addFileObjects(files: Iterable<File>): this {
    for (const file of Array.from(files)) {
      this.files.push(file);
    }
    return this;
  }
 
  async build(): Promise<FileList> {
    return buildFileList(await Promise.all(this.files));
  }
 
  async buildFileArray(): Promise<File[]> {
    return await Promise.all(this.files);
  }
}
 
export class RemoteFileBuilder implements RemoteFileBuilderType {
  private file: Promise<File> | File | undefined;
  constructor() {
    this.file = undefined;
  }
 
  addBlob(blob: BlobFileSource): this {
    this.file = new File([blob.blob], blob.name, {
      type: blob.mimeType,
    });
    return this;
  }
 
  addFile(file: RemoteFileSource): this {
    this.file = remoteFileSourceConverter(file);
    return this;
  }
 
  addFileObject(file: File): this {
    this.file = file;
    return this;
  }
 
  async build(): Promise<File> {
    return buildFile(await this.file);
  }
}