UNPKG

@daiso-tech/core

Version:

The library offers flexible, framework-agnostic solutions for modern web applications, built on adaptable components that integrate seamlessly with popular frameworks like Next Js.

119 lines (118 loc) 5.77 kB
/** * @module CircuitBreaker */ import { type ICircuitBreakerFactoryResolver, type CircuitBreakerTrigger, type ICircuitBreakerFactory, type ICircuitBreakerAdapter } from "../../../../circuit-breaker/contracts/_module.js"; import { type CircuitBreakerFactorySettingsBase } from "../../../../circuit-breaker/implementations/derivables/circuit-breaker-factory/_module.js"; import { type EventBusInput } from "../../../../event-bus/contracts/_module.js"; import { type IExecutionContext } from "../../../../execution-context/contracts/_module.js"; import { type INamespace } from "../../../../namespace/contracts/_module.js"; import { type ITimeSpan } from "../../../../time-span/contracts/_module.js"; import { type ErrorPolicy, type WaitUntil } from "../../../../utilities/_module.js"; /** * IMPORT_PATH: `"@daiso-tech/core/circuit-breaker"` * @group Derivables */ export type CircuitBreakerAdapters<TAdapters extends string> = Partial<Record<TAdapters, ICircuitBreakerAdapter>>; /** * Configuration for `CircuitBreakerFactoryResolver`. * Registers named circuit-breaker adapters and optionally designates a default. * * IMPORT_PATH: `"@daiso-tech/core/circuit-breaker"` * @group Derivables */ export type CircuitBreakerFactoryResolverSettings<TAdapters extends string> = CircuitBreakerFactorySettingsBase & { /** * Named registry of circuit-breaker adapters. Each key is an adapter alias and the corresponding value is the adapter instance. */ adapters: CircuitBreakerAdapters<TAdapters>; /** * The alias of the adapter to use when none is explicitly specified. Must be a key in the `adapters` map. */ defaultAdapter?: NoInfer<TAdapters>; }; /** * The `CircuitBreakerFactoryResolver` class is immutable. * * IMPORT_PATH: `"@daiso-tech/core/circuit-breaker"` * @group Derivables */ export declare class CircuitBreakerFactoryResolver<TAdapters extends string> implements ICircuitBreakerFactoryResolver<TAdapters> { private readonly settings; /** * @example * ```ts * import { CircuitBreakerFactoryResolver } from "@daiso-tech/core/circuit-breaker"; * import { MemoryCircuitBreakerStorageAdapter } from "@daiso-tech/core/circuit-breaker/memory-circuit-breaker-storate-adapter"; * import { DatabaseCircuitBreakerAdapter } from "@daiso-tech/core/circuit-breaker/database-circuit-breaker-adapter"; * import { RedisCircuitBreakerAdapter } from "@daiso-tech/core/circuit-breaker/redis-circuit-breaker-adapter"; * import { Serde } from "@daiso-tech/core/serde"; * import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/super-json-serde-adapter"; * import Redis from "ioredis" * * const serde = new Serde(new SuperJsonSerdeAdapter()); * const circuitBreakerFactoryResolver = new CircuitBreakerFactoryResolver({ * serde, * adapters: { * memory: new DatabaseCircuitBreakerAdapter({ * adapter: new MemoryCircuitBreakerStorageAdapter() * }), * redis: new RedisCircuitBreakerAdapter({ * database: new Redis("YOUR_REDIS_CONNECTION") * }), * }, * defaultAdapter: "memory", * }); * ``` */ constructor(settings: CircuitBreakerFactoryResolverSettings<TAdapters>); setNamespace(namespace: INamespace): CircuitBreakerFactoryResolver<TAdapters>; setEventBus(eventBus: EventBusInput): CircuitBreakerFactoryResolver<TAdapters>; setDefaultSlowCallTime(slowCallTime?: ITimeSpan): CircuitBreakerFactoryResolver<TAdapters>; setDefaultTrigger(trigger?: CircuitBreakerTrigger): CircuitBreakerFactoryResolver<TAdapters>; setDefaultErrorPolicy(defaultErrorPolicy: ErrorPolicy): CircuitBreakerFactoryResolver<TAdapters>; setWaitUntil(waitUntil: WaitUntil): CircuitBreakerFactoryResolver<TAdapters>; setExecutionContext(executionContext: IExecutionContext): CircuitBreakerFactoryResolver<TAdapters>; /** * @example * ```ts * import { CircuitBreakerFactoryResolver } from "@daiso-tech/core/circuit-breaker"; * import { MemoryCircuitBreakerStorageAdapter } from "@daiso-tech/core/circuit-breaker/memory-circuit-breaker-storate-adapter"; * import { DatabaseCircuitBreakerAdapter } from "@daiso-tech/core/circuit-breaker/database-circuit-breaker-adapter"; * import { RedisCircuitBreakerAdapter } from "@daiso-tech/core/circuit-breaker/redis-circuit-breaker-adapter"; * import { Serde } from "@daiso-tech/core/serde"; * import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/super-json-serde-adapter"; * import Redis from "ioredis" * * const serde = new Serde(new SuperJsonSerdeAdapter()); * const circuitBreakerFactoryResolver = new CircuitBreakerFactoryResolver({ * serde, * adapters: { * memory: new DatabaseCircuitBreakerAdapter({ * adapter: new MemoryCircuitBreakerStorageAdapter() * }), * redis: new RedisCircuitBreakerAdapter({ * database: new Redis("YOUR_REDIS_CONNECTION") * }), * }, * defaultAdapter: "memory", * }); * * // Will apply circuit breaker logic the default adapter which is MemoryCircuitBreakerStorageAdapter * await circuitBreakerFactoryResolver * .use() * .create("a") * .runOrFail(async () => { * // ... code to apply circuit breaker logic * }); * * // Will apply circuit breaker logic using the RedisCircuitBreakerAdapter * await circuitBreakerFactoryResolver * .use("redis") * .create("a") * .runOrFail(async () => { * // ... code to apply circuit breaker logic * }); * ``` */ use(adapterName?: TAdapters | undefined): ICircuitBreakerFactory; }