Skip to content

fileInterface

File interface with utility methods.

WARNING

The FileInterface object does not guarantee that the file actually exists. It is only a helper to represent a resource and make operations easier.

Example

ts
import { 
SF
} from "@duplojs/server-utils";
import {
E
,
unwrap
} from "@duplojs/utils";
const
file
=
SF
.
createFileInterface
("/tmp/example.json");
const
name
=
file
.
getName
();
// name: string | null const
parentPath
=
file
.
getParentPath
();
// parentPath: string | null const
stat
= await
file
.
stat
();
// stat: E.Success<SF.StatInfo> | SF.FileSystemLeft<"stat"> if (
E
.
isRight
(
stat
)) {
const
info
=
unwrap
(
stat
);
// info.isFile: boolean } if (
SF
.
isFileInterface
(
file
)) {
// file: SF.FileInterface
file
.
getParentPath
();
}

Syntax

typescript
function createFileInterface(
  path: string
): FileInterface
typescript
function isFileInterface(
  input: unknown
): input is FileInterface

Interface FileInterface

typescript
interface FileInterface {
  path: string;
  getName(): string | null;
  getMimeType(): string | null;
  getExtension(): string | null;
  getParentPath(): string | null;
  rename(newName: string): Promise<FileSystemLeft<"rename"> | E.Success<FileInterface>>;
  relocate(parentPath: string): Promise<FileSystemLeft<"relocate"> | E.Success<FileInterface>>;
  move(newPath: string): Promise<FileSystemLeft<"move"> | E.Success<FileInterface>>;
  exists(): Promise<FileSystemLeft<"exists"> | E.Ok>;
  remove(): Promise<FileSystemLeft<"remove"> | E.Ok>;
  stat(): Promise<FileSystemLeft<"stat"> | E.Success<StatInfo>>;
}

Parameters

  • path : path of the file.

Return value

  • FileInterface : interface with path, getters (getName, getExtension, getMimeType, getParentPath) and helper methods like rename(newName), exists(), relocate(parentPath), move(newPath), remove(), and stat().

See also

  • stat - Retrieves information about a path.

Released under the MIT license.