@mock-filelist/filelist
Version:
This package is a mock generator of FileList and File type object.
204 lines (201 loc) • 4.35 kB
JavaScript
// src/lib/types/helper.ts
import { readFileSync } from "fs";
var buildFile = (file) => {
if (typeof file === "undefined") {
throw new Error("File is not defined.");
}
return file;
};
var buildFileList = (files) => {
const dataTransfer = new DataTransfer();
for (const file of files) {
dataTransfer.items.add(file);
}
return dataTransfer.files;
};
var localFileSourceConverter = (file) => {
const buffer = readFileSync(file.filePath);
const blob = new Blob([buffer], { type: file.mimeType });
return new File([blob], file.name, {
type: file.mimeType
});
};
var remoteFileSourceConverter = async (source) => {
const object = await fetch(source.url).then((response) => {
if (!response.ok) {
throw new Error(
`Failed to fetch url: ${source.url}, status: ${response.status}, statusText: ${response.statusText}.`
);
}
return response.blob();
}).then((blob) => {
return new File([blob], source.name, { type: source.mimeType });
});
return object;
};
// src/lib/local.ts
var LocalFileListBuilder = class {
files = [];
addBlob(blob) {
const object = new File([blob.blob], blob.name, {
type: blob.mimeType
});
this.files.push(object);
return this;
}
addBlobs(blobs) {
for (const blob of blobs) {
const object = new File([blob.blob], blob.name, {
type: blob.mimeType
});
this.files.push(object);
}
return this;
}
addFile(file) {
const object = localFileSourceConverter(file);
this.files.push(object);
return this;
}
addFiles(files) {
for (const file of files) {
const object = localFileSourceConverter(file);
this.files.push(object);
}
return this;
}
addFileObject(file) {
this.files.push(file);
return this;
}
addFileObjects(files) {
for (const file of files) {
this.files.push(file);
}
return this;
}
build() {
return buildFileList(this.files);
}
buildFileArray() {
return this.files;
}
};
var LocalFileBuilder = class {
file;
constructor() {
this.file = void 0;
}
addBlob(blob) {
this.file = new File([blob.blob], blob.name, {
type: blob.mimeType
});
return this;
}
addFile(file) {
this.file = localFileSourceConverter(file);
return this;
}
addFileObject(file) {
this.file = file;
return this;
}
build() {
return buildFile(this.file);
}
};
// src/lib/remote.ts
var RemoteFileListBuilder = class {
files = [];
addBlob(blob) {
const object = new File([blob.blob], blob.name, {
type: blob.mimeType
});
this.files.push(object);
return this;
}
addBlobs(blobs) {
for (const blob of blobs) {
const object = new File([blob.blob], blob.name, {
type: blob.mimeType
});
this.files.push(object);
}
return this;
}
async addFile(file) {
const object = await remoteFileSourceConverter(file);
this.files.push(object);
return this;
}
async addFiles(files) {
for (const file of files) {
const object = await remoteFileSourceConverter(file);
this.files.push(object);
}
return this;
}
addFileObject(file) {
this.files.push(file);
return this;
}
addFileObjects(files) {
for (const file of files) {
this.files.push(file);
}
return this;
}
build() {
return buildFileList(this.files);
}
buildFileArray() {
return this.files;
}
};
var RemoteFileBuilder = class {
file;
constructor() {
this.file = void 0;
}
addBlob(blob) {
this.file = new File([blob.blob], blob.name, {
type: blob.mimeType
});
return this;
}
async addFile(file) {
this.file = await remoteFileSourceConverter(file);
return this;
}
addFileObject(file) {
this.file = file;
return this;
}
build() {
return buildFile(this.file);
}
};
// src/lib/merge.ts
var MergeFileListBuilder = class {
files = [];
addFileObjects(files) {
for (const file of files) {
this.files.push(file);
}
return this;
}
build() {
return buildFileList(this.files);
}
buildFileArray() {
return this.files;
}
};
export {
LocalFileBuilder,
LocalFileListBuilder,
MergeFileListBuilder,
RemoteFileBuilder,
RemoteFileListBuilder
};
//# sourceMappingURL=index.mjs.map