BREAKING CHANGE(core): Make API strict-by-default: remove *Strict variants, throw on not-found/exists conflicts, add explicit exists() methods, update docs/tests and bump deps

This commit is contained in:
2025-11-20 13:20:19 +00:00
parent 0c631383e1
commit 5889396134
15 changed files with 2644 additions and 1562 deletions

View File

@@ -69,7 +69,7 @@ export class Directory {
path: string;
createWithContents?: string | Buffer;
getFromTrash?: boolean;
}): Promise<File | null> {
}): Promise<File> {
const pathDescriptor = {
directory: this,
path: optionsArg.path,
@@ -83,7 +83,7 @@ export class Directory {
return trashedFile;
}
if (!exists && !optionsArg.createWithContents) {
return null;
throw new Error(`File not found at path '${optionsArg.path}'`);
}
if (!exists && optionsArg.createWithContents) {
await File.create({
@@ -98,17 +98,26 @@ export class Directory {
});
}
/**
* gets a file strictly
* @param args
* @returns
* Check if a file exists in this directory
*/
public async getFileStrict(...args: Parameters<Directory['getFile']>) {
const file = await this.getFile(...args);
if (!file) {
throw new Error(`File not found at path '${args[0].path}'`);
}
return file;
public async fileExists(optionsArg: { path: string }): Promise<boolean> {
const pathDescriptor = {
directory: this,
path: optionsArg.path,
};
return this.bucketRef.fastExists({
path: await helpers.reducePathDescriptorToPath(pathDescriptor),
});
}
/**
* Check if a subdirectory exists
*/
public async directoryExists(dirNameArg: string): Promise<boolean> {
const directories = await this.listDirectories();
return directories.some(dir => dir.name === dirNameArg);
}
/**
@@ -206,7 +215,7 @@ export class Directory {
* if the path is a file path, it will be treated as a file and the parent directory will be returned
*/
couldBeFilePath?: boolean;
} = {}): Promise<Directory | null> {
} = {}): Promise<Directory> {
const dirNameArray = dirNameArg.split('/').filter(str => str.trim() !== "");
@@ -253,16 +262,12 @@ export class Directory {
wantedDirectory = await getDirectory(directoryToSearchIn, dirNameToSearch, counter === dirNameArray.length);
}
return wantedDirectory || null;
if (!wantedDirectory) {
throw new Error(`Directory not found at path '${dirNameArg}'`);
}
return wantedDirectory;
}
public async getSubDirectoryByNameStrict(...args: Parameters<Directory['getSubDirectoryByName']>) {
const directory = await this.getSubDirectoryByName(...args);
if (!directory) {
throw new Error(`Directory not found at path '${args[0]}'`);
}
return directory;
}
/**
* moves the directory
@@ -360,7 +365,7 @@ export class Directory {
*/
mode?: 'permanent' | 'trash';
}) {
const file = await this.getFileStrict({
const file = await this.getFile({
path: optionsArg.path,
});
await file.delete({