@api-buddy/types
Version:
Shared types for API Buddy
163 lines • 5.57 kB
TypeScript
import type { AdapterType } from './types.js';
import type { z } from 'zod';
/**
* Base interface that all adapters must implement
* @template TConfig - Type of the configuration object
* @template TSchema - Type of the Zod schema for the configuration
* @template TType - Type of the adapter (from AdapterType)
*/
export interface BaseAdapter<TConfig = unknown, TSchema extends z.ZodType<any, any, any> = z.ZodType<any, any, any>, TType extends AdapterType = AdapterType> {
/** Unique identifier for the adapter instance */
readonly id: string;
/** Type of the adapter (auth, database, storage, etc.) */
readonly type: TType;
/** Current configuration of the adapter */
readonly config: TConfig;
/** Zod schema for validating the configuration */
readonly schema: TSchema;
/**
* Initialize the adapter with the given configuration
* @param config Configuration object for the adapter
*/
init(config: TConfig): Promise<void>;
/**
* Clean up resources when the adapter is no longer needed
*/
destroy(): Promise<void>;
/**
* Check if the adapter is properly initialized
*/
isInitialized(): boolean;
/**
* Update the adapter's configuration
* @param config Partial configuration to update
*/
updateConfig(config: Partial<TConfig>): Promise<void>;
/**
* Validate the configuration against the schema
* @param config Configuration to validate
* @returns The validated configuration
*/
validateConfig(config: unknown): TConfig;
}
/**
* Adapter that can execute operations
* @template TConfig - Type of the configuration object
* @template TSchema - Zod schema for the configuration
* @template TResult - Type of the result
* @template TInput - Type of the input parameters
*/
export interface ExecutableAdapter<TConfig = unknown, TSchema extends z.ZodType = z.ZodType, TResult = unknown, TInput = unknown> extends BaseAdapter<TConfig, TSchema> {
/**
* Execute an operation with the given parameters
* @param params Input parameters for the operation
*/
execute(params: TInput): Promise<TResult>;
}
/**
* Adapter that can subscribe to data changes
* @template TConfig - Type of the configuration object
* @template TSchema - Zod schema for the configuration
* @template TData - Type of the data being subscribed to
* @template TParams - Type of the subscription parameters
*/
export interface SubscribableAdapter<TConfig = unknown, TSchema extends z.ZodType = z.ZodType, TData = unknown, TParams = unknown> extends BaseAdapter<TConfig, TSchema> {
/**
* Subscribe to data changes
* @param callback Function to call when data changes
* @param params Optional parameters for the subscription
* @returns Unsubscribe function
*/
subscribe(callback: (data: TData) => void, params?: TParams): () => void;
}
/**
* Adapter that can be used as a factory to create other adapters
*/
export interface AdapterFactory<T extends BaseAdapter = BaseAdapter> {
/**
* Create a new adapter instance
* @param config Configuration for the adapter
*/
create(config: unknown): Promise<T>;
/**
* Get the default configuration for the adapter
*/
getDefaultConfig(): unknown;
/**
* Get the schema for the adapter configuration
*/
getSchema(): z.ZodType;
}
/**
* Event types that can be emitted by adapters
*/
export declare enum AdapterEventType {
INITIALIZED = "initialized",
DESTROYED = "destroyed",
ERROR = "error",
CONFIG_UPDATED = "config_updated",
STATE_CHANGED = "state_changed",
DATA_CHANGED = "data_changed",
AUTH_STATE_CHANGED = "auth_state_changed",
ID_TOKEN_CHANGED = "id_token_changed"
}
/**
* Base event interface for adapter events
* @template T - Type of the event data
*/
export interface AdapterEvent<T = unknown> {
/** Type of the event */
type: AdapterEventType;
/** Timestamp when the event occurred */
timestamp: Date;
/** Event data */
data?: T;
/** Error if the event represents an error */
error?: Error;
/** Metadata about the event */
metadata?: Record<string, unknown>;
}
/**
* Event listener function
* @template T - Type of the event data
*/
export type AdapterEventListener<T = unknown> = (event: AdapterEvent<T>) => void;
/**
* Event emitter interface for adapters
*/
export interface AdapterEventEmitter {
/**
* Add an event listener
* @param event Event type to listen for
* @param listener Callback function
*/
on<T = unknown>(event: AdapterEventType | string, listener: AdapterEventListener<T>): void;
/**
* Remove an event listener
* @param event Event type to remove listener from
* @param listener Callback function to remove
*/
off<T = unknown>(event: AdapterEventType | string, listener: AdapterEventListener<T>): void;
/**
* Emit an event
* @param event Event to emit
*/
emit<T = unknown>(event: AdapterEvent<T>): void;
}
/**
* Options for initializing an adapter
*/
export interface AdapterOptions {
/** Whether to automatically initialize the adapter */
autoInit?: boolean;
/** Whether to throw an error if initialization fails */
throwOnError?: boolean;
/** Logger instance */
logger?: {
debug: (...args: any[]) => void;
info: (...args: any[]) => void;
warn: (...args: any[]) => void;
error: (...args: any[]) => void;
};
}
//# sourceMappingURL=base.d.ts.map