UNPKG

@iota-big3/sdk-gateway

Version:

Universal API Gateway with protocol translation, intelligent routing, rate limiting, health checking, and caching

168 lines 5.95 kB
/** * @iota-big3/sdk-patterns * Standard patterns and interfaces for consistent API design across IOTA SDK */ export type Result<T, E = Error> = { success: true; data: T; } | { success: false; error: E; }; export declare const Ok: <T>(data: T) => Result<T, never>; export declare const Err: <E = Error>(error: E) => Result<never, E>; export interface CRUDOperations<T, ID = string> { createAsync(data: Omit<T, 'id'>): Promise<Result<T>>; getAsync(id: ID): Promise<Result<T>>; listAsync(filter?: Partial<T>): Promise<Result<T[]>>; updateAsync(id: ID, data: Partial<T>): Promise<Result<T>>; deleteAsync(id: ID): Promise<Result<void>>; } export interface ReadOperations<T, ID = string> { getAsync(id: ID): Promise<Result<T>>; listAsync(filter?: Partial<T>): Promise<Result<T[]>>; existsAsync(id: ID): Promise<Result<boolean>>; countAsync(filter?: Partial<T>): Promise<Result<number>>; } export interface WriteOperations<T, ID = string> { createAsync(data: Omit<T, 'id'>): Promise<Result<T>>; updateAsync(id: ID, data: Partial<T>): Promise<Result<T>>; deleteAsync(id: ID): Promise<Result<void>>; upsertAsync(id: ID, data: Omit<T, 'id'>): Promise<Result<T>>; } export interface Repository<T, ID = string> extends ReadOperations<T, ID>, WriteOperations<T, ID> { } export interface EventEmitterPattern<TEvents extends Record<string, any>> { on<K extends keyof TEvents>(event: K, handler: (data: TEvents[K]) => void): void; off<K extends keyof TEvents>(event: K, handler: (data: TEvents[K]) => void): void; emit<K extends keyof TEvents>(event: K, data: TEvents[K]): void; once<K extends keyof TEvents>(event: K, handler: (data: TEvents[K]) => void): void; } export declare class ValidationError extends Error { field: string; value: unknown; constructor(field: string, value: unknown, message?: string); } export declare class NotFoundError extends Error { resource: string; id: string | number; constructor(resource: string, id: string | number); } export declare class AuthenticationError extends Error { constructor(message?: string); } export declare class AuthorizationError extends Error { action: string; resource: string; constructor(action: string, resource: string); } export declare class NetworkError extends Error { statusCode?: number | undefined; constructor(statusCode?: number | undefined, message?: string); } export interface AsyncOperation<T> { executeAsync(): Promise<Result<T>>; cancelAsync?(): Promise<void>; getStatusAsync?(): Promise<'pending' | 'completed' | 'failed' | 'cancelled'>; } export interface RetryConfig { maxAttempts: number; delayMs: number; backoffMultiplier?: number; maxDelayMs?: number; } export interface HealthCheckable { healthCheckAsync(): Promise<Result<HealthStatus>>; } export interface HealthStatus { healthy: boolean; message?: string; details?: Record<string, any>; timestamp: Date; } export interface Configurable<TConfig> { getConfigAsync(): Promise<Result<TConfig>>; updateConfigAsync(config: Partial<TConfig>): Promise<Result<TConfig>>; validateConfigAsync(config: TConfig): Promise<Result<boolean>>; } export interface PaginationOptions { page: number; pageSize: number; sortBy?: string; sortOrder?: 'asc' | 'desc'; } export interface PaginatedResult<T> { items: T[]; totalCount: number; page: number; pageSize: number; totalPages: number; hasNextPage: boolean; hasPreviousPage: boolean; } export interface Factory<T, TOptions = any> { createAsync(options: TOptions): Promise<Result<T>>; validateOptionsAsync(options: TOptions): Promise<Result<boolean>>; } export interface Observer<T> { update(data: T): void; } export interface Observable<T> { subscribe(observer: Observer<T>): void; unsubscribe(observer: Observer<T>): void; notify(data: T): void; } export interface ServiceLifecycle { startAsync(): Promise<Result<void>>; stopAsync(): Promise<Result<void>>; restartAsync?(): Promise<Result<void>>; isRunningAsync(): Promise<boolean>; } export interface Validator<T> { validateAsync(data: T): Promise<Result<T>>; validatePartialAsync?(data: Partial<T>): Promise<Result<Partial<T>>>; getValidationRulesAsync?(): Promise<ValidationRule[]>; } export interface ValidationRule { field: string; rule: string; message: string; } export interface Transformer<TInput, TOutput> { transformAsync(input: TInput): Promise<Result<TOutput>>; canTransformAsync?(input: TInput): Promise<boolean>; } export interface Cacheable<T, TKey = string> { getFromCacheAsync(key: TKey): Promise<Result<T | null>>; setInCacheAsync(key: TKey, value: T, ttl?: number): Promise<Result<void>>; invalidateCacheAsync(key: TKey): Promise<Result<void>>; clearCacheAsync(): Promise<Result<void>>; } export interface BatchOperations<T, ID = string> { createManyAsync(items: Omit<T, 'id'>[]): Promise<Result<T[]>>; updateManyAsync(updates: Array<{ id: ID; data: Partial<T>; }>): Promise<Result<T[]>>; deleteManyAsync(ids: ID[]): Promise<Result<void>>; } export declare const MethodNaming: { create: string[]; read: string[]; update: string[]; delete: string[]; validate: string[]; transform: string[]; lifecycle: string[]; event: string[]; async: string[]; boolean: string[]; }; export declare const ParameterOrder: string[]; export type ID = string | number; export type Timestamp = Date | number | string; export type SortOrder = 'asc' | 'desc'; export type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'OPTIONS' | 'HEAD'; export type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>; export type RequiredBy<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>; //# sourceMappingURL=index.d.ts.map