@metacall/protocol
Version:
Tool for deploying into MetaCall FaaS platform.
132 lines (131 loc) • 4.98 kB
TypeScript
/// <reference types="node" />
import { Readable } from 'stream';
import { Create, Deployment, LogType, MetaCallJSON } from './deployment';
import { Plans } from './plan';
export declare class ProtocolError extends Error {
status?: number;
data?: unknown;
constructor(message: string, status?: number, data?: unknown);
}
/**
* Type guard for protocol-specific errors.
* @param err - The unknown error to check.
* @returns True if the error is an ProtocolError, false otherwise.
*/
export declare const isProtocolError: (err: unknown) => err is ProtocolError;
declare type SubscriptionMap = Record<string, number>;
export interface SubscriptionDeploy {
id: string;
plan: Plans;
date: number;
deploy: string;
}
export declare enum ResourceType {
Package = "Package",
Repository = "Repository"
}
export interface Resource {
id: string;
}
export interface Branches {
branches: [string];
}
export declare enum InvokeType {
Call = "call",
Await = "await"
}
export interface DeployCreateRequest {
suffix: string;
resourceType: ResourceType;
release: string;
env: {
name: string;
value: string;
}[];
plan: Plans;
version: string;
}
export interface DeployDeleteRequest {
prefix: string;
suffix: string;
version: string;
}
export interface RepositoryAddRequest {
url: string;
branch: string;
jsons: MetaCallJSON[];
}
export interface RepositoryBranchListRequest {
url: string;
}
export interface RepositoryFileListRequest {
url: string;
branch: string;
}
export interface API {
refresh(): Promise<string>;
ready(): Promise<boolean>;
validate(): Promise<boolean>;
deployEnabled(): Promise<boolean>;
listSubscriptions(): Promise<SubscriptionMap>;
listSubscriptionsDeploys(): Promise<SubscriptionDeploy[]>;
inspect(): Promise<Deployment[]>;
inspectByName(suffix: string): Promise<Deployment>;
upload(name: string, blob: Blob | Readable, jsons?: MetaCallJSON[], runners?: string[]): Promise<Resource>;
add(url: string, branch: string, jsons: MetaCallJSON[]): Promise<Resource>;
deploy(name: string, env: {
name: string;
value: string;
}[], plan: Plans, resourceType: ResourceType, release?: string, version?: string): Promise<Create>;
deployDelete(prefix: string, suffix: string, version: string): Promise<string>;
availableJobLogs(suffix: string): Promise<string[]>;
logs(container: string, type: LogType, prefix: string, suffix: string, version?: string): Promise<string>;
branchList(url: string): Promise<Branches>;
fileList(url: string, branch: string): Promise<string[]>;
invoke<Result, Args = unknown>(type: InvokeType, prefix: string, suffix: string, version: string, name: string, args?: Args): Promise<Result>;
call<Result, Args = unknown>(prefix: string, suffix: string, version: string, name: string, args?: Args): Promise<Result>;
await<Result, Args = unknown>(prefix: string, suffix: string, version: string, name: string, args?: Args): Promise<Result>;
}
declare const _default: (token: string, baseURL: string) => API;
export default _default;
export declare const MaxRetries = 100;
export declare const MaxRetryInterval = 5000;
export declare const MaxFuncLength = 64;
/**
* Executes an asynchronous function with automatic retry logic.
*
* The function will be retried up to `maxRetries` times, waiting `interval`
* milliseconds between each attempt. If all retries fail, the last error is
* wrapped in a new `Error` with a descriptive message, including:
* - Function name (or string representation truncated to `MaxFuncLength` chars if anonymous)
* - Number of retries attempted
* - Original error message
*
* Error handling is fully type-safe:
* - If the error is an ProtocolError (checked via `isProtocolError`), its
* message is used.
* - If the error is a standard `Error`, its `message` is used.
* - Otherwise, the error is converted to a string.
*
* @typeParam T - The return type of the function being retried.
* @param fn - A lambda or bound function returning a `Promise<T>`. The
* function should contain the logic you want to retry.
* @param maxRetries - Maximum number of retry attempts. Default: `MaxRetries`.
* @param interval - Delay between retries in milliseconds. Default: `MaxRetryInterval`.
* @returns A `Promise` resolving to the return value of `fn` if successful.
* @throws Error If all retry attempts fail, throws a new Error containing
* information about the function and the last error.
*
* @example
* ```ts
* const deployment = await waitFor(() => api.inspectByName('my-suffix'));
* ```
*
* @example
* ```ts
* const result = await waitFor(
* () => api.deploy(name, env, plan, resourceType)
* );
* ```
*/
export declare const waitFor: <T>(fn: (cancel: (message: string) => void) => Promise<T>, maxRetries?: number, interval?: number) => Promise<T>;