import IClient from "../../interfaces/Client/IClient"; import IClientActorBuilder from "../../interfaces/Client/IClientActorBuilder"; import IClientBinding from "../../interfaces/Client/IClientBinding"; import IClientConfiguration from "../../interfaces/Client/IClientConfiguration"; import IClientCrypto from "../../interfaces/Client/IClientCrypto"; import IClientHealth from "../../interfaces/Client/IClientHealth"; import IClientInvoker from "../../interfaces/Client/IClientInvoker"; import IClientLock from "../../interfaces/Client/IClientLock"; import IClientMetadata from "../../interfaces/Client/IClientMetadata"; import IClientProxy from "../../interfaces/Client/IClientProxy"; import IClientPubSub from "../../interfaces/Client/IClientPubSub"; import IClientSecret from "../../interfaces/Client/IClientSecret"; import IClientSidecar from "../../interfaces/Client/IClientSidecar"; import IClientState from "../../interfaces/Client/IClientState"; import IClientWorkflow from "../../interfaces/Client/IClientWorkflow"; import { DaprClientOptions } from "../../types/DaprClientOptions"; import { Logger } from "../../logger/Logger"; /** * Main client for interacting with Dapr runtime capabilities. * * DaprClient provides a unified interface for accessing all Dapr building blocks including * state management, pub/sub messaging, service invocation, secrets, actors, and more. * The client automatically selects the appropriate communication protocol (HTTP or gRPC) * based on configuration and manages the underlying protocol-specific implementation. * * @see {@link https://dapr.io/docs/developing-applications/building-blocks/ | Dapr Building Blocks} * * @example * ```typescript * import { DaprClient } from "@dapr/dapr"; * * // Create client with default configuration (HTTP to localhost:3500) * const client = new DaprClient(); * * // State management * await client.state.save("statestore", [ * { key: "user-1", value: { name: "Alice" } } * ]); * const user = await client.state.get("statestore", "user-1"); * * // Pub/Sub * await client.pubsub.publish("pubsub", "orders", { orderId: "123" }); * * // Service invocation * const result = await client.invoker.invoke("payment-service", "ProcessPayment", { * body: { amount: 99.99 } * }); * * // Secrets * const apiKey = await client.secret.get("vault", "api-key"); * * // Clean up * await client.stop(); * ``` */ export default class DaprClient { /** * Resolved client configuration after initialization. * Includes defaults and environment variable overrides. */ readonly options: DaprClientOptions; /** * Underlying protocol-specific client (HTTP or gRPC). * Typically accessed indirectly through the building block properties. * * @internal */ readonly daprClient: IClient; /** * Actor building block client. * Provides access to actor runtime, methods, reminders, and timers. * * @see {@link https://dapr.io/docs/developing-applications/building-blocks/actors/ | Dapr Actors} */ readonly actor: IClientActorBuilder; /** * Output bindings client. * Sends events to external systems via configured output bindings. * * @see {@link https://dapr.io/docs/developing-applications/building-blocks/bindings/ | Dapr Bindings} */ readonly binding: IClientBinding; /** * Configuration store client. * Retrieves and watches application configuration values. * * @see {@link https://dapr.io/docs/developing-applications/building-blocks/configuration/ | Dapr Configuration} */ readonly configuration: IClientConfiguration; /** * Cryptographic operations client. * Encrypts and decrypts data using configured crypto providers. * * @see {@link https://dapr.io/docs/developing-applications/building-blocks/cryptography/ | Dapr Cryptography} */ readonly crypto: IClientCrypto; /** * Health check client. * Probes the Dapr sidecar and application readiness. */ readonly health: IClientHealth; /** * Service invocation client. * Calls methods on other services via Dapr service invocation. * * @see {@link https://dapr.io/docs/developing-applications/building-blocks/service-invocation/ | Dapr Service Invocation} */ readonly invoker: IClientInvoker; /** * Distributed lock client. * Acquires and releases locks for coordinated access to resources. * * @see {@link https://dapr.io/docs/developing-applications/building-blocks/distributed-lock/ | Dapr Locks} */ readonly lock: IClientLock; /** * Metadata client. * Retrieves runtime metadata and active subscriptions. */ readonly metadata: IClientMetadata; /** * Typed service proxy client. * Creates type-safe proxies for service-to-service invocation. * * @see {@link https://dapr.io/docs/developing-applications/building-blocks/service-invocation/ | Dapr Service Invocation} */ readonly proxy: IClientProxy; /** * Pub/Sub client. * Publishes messages to topics and subscribes to topic handlers. * * @see {@link https://dapr.io/docs/developing-applications/building-blocks/pubsub/ | Dapr Pub/Sub} */ readonly pubsub: IClientPubSub; /** * Secrets client. * Retrieves secrets from configured secret stores. * * @see {@link https://dapr.io/docs/developing-applications/building-blocks/secrets/ | Dapr Secrets} */ readonly secret: IClientSecret; /** * Sidecar client. * Manages the Dapr sidecar lifecycle (shutdown, etc.). */ readonly sidecar: IClientSidecar; /** * State management client. * Manages durable state storage with transactions and querying. * * @see {@link https://dapr.io/docs/developing-applications/building-blocks/state-management/ | Dapr State Management} */ readonly state: IClientState; /** * Workflow client. * Manages workflow instances and job scheduling. * * @see {@link https://dapr.io/docs/developing-applications/building-blocks/workflow/ | Dapr Workflows} */ readonly workflow: IClientWorkflow; private readonly logger; /** * Creates a new Dapr client instance. * * Initializes the appropriate protocol-specific client (HTTP or gRPC) based on * configuration and wires up all building block implementations. Auto-selects * HTTP by default unless DAPR_PROTOCOL_ENVIRONMENT_VARIABLE is set. * * @param options - Optional client configuration * @param options.daprHost - Sidecar host (default: localhost, env: DAPR_HOST) * @param options.daprPort - Sidecar port (default: 3500 for HTTP, 50001 for gRPC, env: DAPR_PORT) * @param options.communicationProtocol - HTTP or gRPC (default: HTTP, env: DAPR_PROTOCOL) * @param options.daprApiToken - API token for sidecar auth (env: DAPR_API_TOKEN) * @param options.isKeepAlive - Enable keep-alive connections (default: true) * @param options.maxBodySizeMb - Maximum request body size (default: 4) * @param options.logger - Custom logger instance * @param options.actor - Actor configuration options * * @throws {Error} When daprPort is invalid or sidecar connection fails * * @example * ```typescript * // Use defaults (HTTP, localhost:3500) * const client = new DaprClient(); * * // Use gRPC with custom host/port * const client = new DaprClient({ * communicationProtocol: CommunicationProtocolEnum.GRPC, * daprHost: "dapr-sidecar.default.svc.cluster.local", * daprPort: "50001" * }); * * // With authentication * const client = new DaprClient({ * daprApiToken: "your-api-token" * }); * ``` */ constructor(options?: Partial); /** * Creates a DaprClient from an existing IClient instance. * * Useful for wrapping a custom or pre-configured protocol-specific client. * The returned DaprClient shares the same underlying client instance. * * @param client - Configured IClient implementation (HTTP or gRPC) * @returns DaprClient wrapping the provided client * * @example * ```typescript * const grpcClient = new GRPCClient({ daprPort: "50001" }); * const client = DaprClient.create(grpcClient); * ``` */ static create(client: IClient): DaprClient; /** * Waits for the Dapr sidecar to become available and ready. * * Polls the sidecar health check endpoint at regular intervals (default 50ms) * until it responds successfully or the timeout (60 seconds) is exceeded. * Useful in startup sequences to ensure the sidecar is ready before making API calls. * * @param fnIsSidecarStarted - Async function that returns true when sidecar is ready * @param logger - Logger instance for status messages * @returns Promise that resolves when sidecar is ready * * @throws {Error} With code "DAPR_SIDECAR_COULD_NOT_BE_STARTED" if timeout exceeded * * @example * ```typescript * const client = new DaprClient(); * await DaprClient.awaitSidecarStarted( * () => client.health.isHealthy(), * client["logger"] * ); * // Now safe to make API calls * ``` */ static awaitSidecarStarted(fnIsSidecarStarted: () => Promise, logger: Logger): Promise; /** * Stops the client and closes connections to the Dapr sidecar. * * Gracefully shuts down the underlying HTTP or gRPC client connection. * Should be called when the client is no longer needed to free up resources. * After stopping, the client cannot be reused. * * @returns Promise that resolves when the client is stopped * * @example * ```typescript * const client = new DaprClient(); * try { * // Use client... * } finally { * await client.stop(); * } * ``` */ stop(): Promise; /** * Starts the client and establishes connection to the Dapr sidecar. * * Initializes the underlying HTTP or gRPC client connection. * Should be called before making any API calls. May be called automatically * by the SDK depending on configuration. * * @returns Promise that resolves when the client is started * * @throws {Error} If connection to sidecar cannot be established * * @example * ```typescript * const client = new DaprClient(); * await client.start(); * // Now safe to make API calls * ``` */ start(): Promise; /** * Checks if the client is initialized and ready to use. * * Returns whether the underlying protocol-specific client has been successfully * initialized. Does not perform any network calls—just checks internal state. * * @returns true if client is initialized, false otherwise */ getIsInitialized(): boolean; }