Reference
Types
Type reference for the @fileway/core public API
All types are exported from @fileway/core. See Observability for the semantics of errors, logging, and onError.
UploadOptions
interface UploadOptions {
filename: string;
mimeType?: string;
path?: string;
metadata?: Record<string, string>;
size?: number;
signal?: AbortSignal;
onProgress?: (progress: UploadProgress) => void;
streamFactory?: () => ReadableStream<Uint8Array>; // sync today; async return is a planned future feature
id?: string; // stable upload id; generated per upload() call when omitted
}See Safe retries and upload status for id.
OperationOptions
interface OperationOptions {
signal?: AbortSignal; // cancels the request in flight and any pending retries
}Accepted by get, delete, getUrl, getPresignedUrl and getUploadStatus on the client, and by the matching BaseDriver methods.
UploadProgress
interface UploadProgress {
bytes: number;
total?: number;
progress?: number; // 0..1, present only when total is known
}UploadResult<TMeta>
interface UploadResult<TMeta extends Record<string, unknown>> {
id: string;
url: string;
path: string;
size: number;
meta: TMeta;
}PresignedUrlOptions
interface PresignedUrlOptions extends OperationOptions {
expiresIn?: number; // seconds; default 3600. At most 604800 on S3, GCS and Azure
disposition?: "inline" | "attachment"; // app-signed links only
filename?: string; // app-signed links only
id?: string; // app-signed links only, passed to links.check
}LinksConfig, VerifiedLink, PresignedUploadOptions
interface LinksConfig {
secret: string | string[]; // base64, 32+ bytes each; the first signs, all verify
origin: string; // e.g. "https://files.myapp.com"
mode?: "fallback" | "always";
maxExpiresIn?: number; // default 604800
cors?: string[];
inlineTypes?: string[];
check?(link: VerifiedLink, request: Request): boolean | Promise<boolean>;
}
interface PresignedUploadOptions extends OperationOptions {
filename: string;
path?: string;
id?: string;
mimeType?: string;
maxSize?: number;
expiresIn?: number; // default 900
direct?: boolean; // true: straight to the provider; default: through your app when links is set
size?: number; // exact bytes; required for direct S3 links
}
interface PresignedUpload {
url: string;
method: "PUT";
headers: Record<string, string>; // send all of them with the body
id: string;
path?: string; // where the file will be stored
}
// What the client passes to a driver's getPresignedUploadUrl
interface DirectUploadOptions extends OperationOptions {
path: string; // from the driver's pathFor
id: string;
mimeType?: string;
size?: number;
maxSize: number;
expiresIn: number;
}GetOptions and DriverCapabilities
interface GetOptions extends OperationOptions {
range?: { start: number; end?: number }; // inclusive byte offsets
}
interface DriverCapabilities {
range?: boolean; // get honors options.range
maxUploadBytes?: number; // default limit for upload links
directUpload?: { // the driver issues direct upload links
enforcesSize: boolean; // false on Azure
maxExpiresIn: number; // seconds
};
}Every built-in driver supports ranges. Custom drivers declare capabilities to opt in.
ListOptions, ListPage, ListedObject, CopyOptions
interface ListOptions extends OperationOptions {
prefix?: string; // "photos/" lists a folder; omitted lists the root
recursive?: boolean; // default false: one folder level
limit?: number; // 1 to 1000, default 100
cursor?: string; // from the previous page
}
interface ListPage {
objects: ListedObject[];
folders: string[]; // one level below prefix, each ending in "/"
cursor?: string; // present when there are more results
}
interface ListedObject {
path: string;
size: number;
lastModified?: Date;
etag?: string;
contentType?: string;
}
interface CopyOptions extends OperationOptions {
overwrite?: boolean; // default false: an existing destination is a conflict
}RetryConfig
Controls automatic retries for transient failures. Passed as retry in FilewayConfig. Only network, timeout, rate-limited, provider-error, and HTTP 429/5xx statuses are retried; validation, config, auth-failed, not-found, bucket-not-found, size-exceeded, and AbortError are never retried. See Retries for how it behaves.
interface RetryConfig {
maxRetries?: number; // attempts after the first; default 3, 0 disables
baseDelayMs?: number; // first delay; default 300
maxDelayMs?: number; // cap on any single delay; default 10000
multiplier?: number; // exponential backoff factor; default 2
jitter?: boolean; // full jitter over [0, delay]; default true
retryHttpStatuses?: number[]; // extra HTTP statuses to treat as retryable
retryGenericErrors?: boolean; // retry non-StorageError failures; default: only TypeError
idleTimeoutMs?: number; // cancel and retry an attempt with no progress for this long; default off
}BaseDriver<TMeta>
interface BaseDriver<TMeta extends Record<string, unknown> = Record<string, unknown>> {
name: string;
upload(
stream: ReadableStream<Uint8Array>,
options: UploadOptions,
context?: UploadContext,
): Promise<UploadResult<TMeta>>;
delete(path: string, options?: OperationOptions): Promise<void>; // missing object: no error
getUrl(path: string, options?: OperationOptions): Promise<string>;
get(path: string, options?: OperationOptions): Promise<ReadableStream<Uint8Array>>;
getPresignedUrl?(path: string, options?: PresignedUrlOptions): Promise<string>;
getPresignedUploadUrl?(options: DirectUploadOptions): Promise<PresignedUpload>; // with pathFor and capabilities.directUpload
// Optional: enable lost-response recovery and getUploadStatus.
pathFor?(options: UploadOptions & { id: string }): string;
head?(path: string, options?: OperationOptions): Promise<StoredObject<TMeta> | null>;
isPending?(path: string, options?: OperationOptions): Promise<boolean>;
// Optional: listing and managing files (see Managing Files).
list?(options?: ListOptions): Promise<ListPage>;
copy?(from: string, to: string, options?: CopyOptions): Promise<void>;
move?(from: string, to: string, options?: CopyOptions): Promise<void>;
}
interface UploadContext {
uploadToken: string; // unique per upload() call; store it with the object
}
interface StoredObject<TMeta> extends UploadResult<TMeta> {
uploadToken?: string;
lastModified?: Date;
contentType?: string;
}
type UploadStatus<TMeta> =
| { state: "complete"; object: StoredObject<TMeta> }
| { state: "pending" }
| { state: "missing" }
| { state: "unsupported" };A custom driver that implements pathFor and head gets lost-response recovery and getUploadStatus for free. pathFor must return exactly where upload stores an object with that id, and head should return the context.uploadToken it was stored with when the provider can keep it. Pass options.signal to every request so callers can cancel. A driver may accept extra options on its methods (a type that extends OperationOptions); the client passes them through.
DriverMeta<TDriver> is the result.meta type of a driver's uploads.
MiddlewareHook
interface MiddlewareHook<TMeta extends Record<string, unknown> = Record<string, unknown>> {
beforeUpload?: (
stream: ReadableStream<Uint8Array>,
options: UploadOptions,
) => Promise<
| { stream?: ReadableStream<Uint8Array>; options?: UploadOptions; size?: number }
| void
>;
afterUpload?: (result: UploadResult<TMeta>, options: UploadOptions) => Promise<void>;
}Inline middleware on a client gets TMeta inferred from the driver. A reusable middleware typed as plain MiddlewareHook works with any driver.
## `FilewayConfig`
```ts
interface FilewayConfig<TDriver extends BaseDriver> {
driver: TDriver;
middlewares?: MiddlewareHook<DriverMeta<TDriver>>[];
logger?: Logger;
onError?: (error: unknown, context: ErrorContext) => void | Promise<void>;
retry?: RetryConfig;
plugins?: FilewayPlugin[];
basePath?: string; // where client.handler is mounted; default "/api/fileway"
links?: LinksConfig;
waitUntil?: (promise: Promise<unknown>) => void; // the platform's waitUntil on serverless
}FilewayPlugin, FilewayEvent, TrackOptions
interface FilewayPlugin {
id: string;
init?(ctx: PluginContext): void;
middlewares?: MiddlewareHook[];
onEvent?(event: FilewayEvent): void | Promise<void>; // not awaited
routes?: Record<string, PluginRoute>; // "GET /path" or "GET /path/*"
}
interface PluginContext {
client: FilewayClient<BaseDriver>;
driverName: string;
basePath: string; // where client.handler is mounted, e.g. "/api/fileway"
features: ClientFeatures;
log: Logger;
waitUntil(promise: Promise<unknown>): void; // background work; uses config.waitUntil when set
}
// What the client can do with its driver; fallbacks count (move as copy + delete,
// download links as app-signed links). Also available as client.features.
interface ClientFeatures {
list: boolean;
stat: boolean;
copy: boolean;
move: boolean;
range: boolean;
downloadLinks: boolean;
uploadLinks: { app: boolean; direct: boolean };
maxUploadBytes?: number;
}
type PluginRoute = (request: Request, ctx: PluginContext & { params: { wildcard?: string } }) => Promise<Response> | Response;
interface FilewayEvent {
id: string;
time: string;
operation: EventOperation; // every Operation except "retry"
outcome: "success" | "error" | "aborted";
path?: string;
size?: number;
mimeType?: string;
durationMs: number;
attempts: number;
driver: string;
error?: { name: string; message: string; code?: StorageErrorCode; statusCode?: number };
actor?: string;
tags?: Record<string, string>;
}
interface TrackOptions {
actor?: string;
tags?: Record<string, string>;
}FilewayConfig also takes plugins?: FilewayPlugin[] and basePath?: string, and client.handler is a FilewayHandler. See Plugins and Events.
Logger
interface Logger {
debug?(message: string, meta?: Record<string, unknown>): void;
info?(message: string, meta?: Record<string, unknown>): void;
warn?(message: string, meta?: Record<string, unknown>): void;
error?(message: string, meta?: Record<string, unknown>): void;
}ErrorContext
interface ErrorContext {
operation: "upload" | "delete" | "get" | "getUrl" | "getPresignedUrl" | "getUploadStatus" | "list" | "stat" | "copy" | "move" | "retry";
durationMs: number;
filename?: string;
path?: string;
mimeType?: string;
size?: number;
}StorageError & StorageErrorCode
type StorageErrorCode =
| "validation"
| "config"
| "not-found"
| "bucket-not-found"
| "auth-failed"
| "size-exceeded"
| "conflict" // copy or move onto an existing path without overwrite; not retried
| "rate-limited" // the provider is throttling (HTTP 429 or its own code); retried
| "timeout" // an attempt made no progress within retry.idleTimeoutMs; retried
| "network"
| "provider-error";
class StorageError extends Error {
readonly code: StorageErrorCode;
readonly statusCode?: number;
readonly provider?: string;
readonly cause?: unknown;
toJSON(): { name: string; code: StorageErrorCode; message: string; statusCode?: number; provider?: string };
}ValidationError is a StorageError subclass with code === "validation". isAbortError(err) detects the AbortError thrown by cancellation.
Helpers for driver authors
@fileway/core also exports validateUploadOptions, urlEncodePath, abortError, encodeCursor and decodeCursor for custom drivers. Retry timing, progress tracking and ID validation are internal to the client. See the error reference for what each code means and when to retry.
Was this page helpful?