dapr

Dapr (Distributed Application Runtime) client for Kit

A pure Kit package that provides a client for Dapr's HTTP API. Communicates with the Dapr sidecar using Kit's built-in HTTP client and JSON encoding.

Files

FileDescription
.editorconfigEditor formatting configuration
.gitignoreGit ignore rules for build artifacts and dependencies
.tool-versionsasdf tool versions (Zig, Kit)
LICENSEMIT license file
README.mdThis file
examples/basic.kitBasic usage example
kit.tomlPackage manifest with metadata and dependencies
src/dapr.kitDapr sidecar configuration.
tests/dapr.test.kitTests for dapr
tests/error-types.test.kitTests for error-types

Dependencies

No Kit package dependencies.

Features

  • State Management: Save, get, delete, bulk operations, transactions
  • Pub/Sub: Publish events to topics (with optional metadata)
  • Service Invocation: Call other Dapr-enabled services
  • Secrets: Retrieve secrets from configured secret stores
  • Bindings: Invoke output bindings (with optional metadata)
  • Health: Check sidecar health and metadata

Requirements

  • A running Dapr sidecar (default: localhost:3500)
  • For production: dapr run --app-id my-app -- kit run myapp.kit

Installation

kit add dapr

Quick Start

import "dapr" as Dapr

# Use default config (localhost:3500)
config = Dapr.default-config

# Save state
match Dapr.state-save config "statestore" "key1" "value1"
  | Ok _ -> print "State saved"
  | Err e -> print "Error: ${show e}"

# Get state
match Dapr.state-get config "statestore" "key1"
  | Ok (Some value) -> print "Got: ${value}"
  | Ok None -> print "Key not found"
  | Err e -> print "Error: ${show e}"

Configuration

# Default config (localhost:3500)
config = Dapr.default-config

# From environment variables
config = Dapr.config-from-env  # Reads DAPR_HTTP_HOST, DAPR_HTTP_PORT

# Custom host and port
config = Dapr.config-with-host "dapr-sidecar.local" 3500

API Reference

State Management

# Save single key-value
state-save : Config -> String -> String -> a -> Result Unit DaprError

# Save with metadata (e.g., TTL)
state-save-with-metadata : Config -> String -> String -> a -> Map String String -> Result Unit DaprError

# Get value
state-get : Config -> String -> String -> Result (Option String) DaprError

# Delete key
state-delete : Config -> String -> String -> Result Unit DaprError

# Bulk get
state-bulk-get : Config -> String -> [String] -> Result [StateItem] DaprError

# Transaction (atomic multi-operation)
state-transaction : Config -> String -> [StateOperation] -> Result Unit DaprError

Pub/Sub

# Publish event
publish : Config -> String -> String -> a -> Result Unit DaprError

# Publish with metadata
publish-with-metadata : Config -> String -> String -> a -> Map String String -> Result Unit DaprError

Service Invocation

# Invoke method (POST)
invoke : Config -> String -> String -> String -> Result String DaprError

# Invoke with HTTP method
invoke-with-method : Config -> String -> String -> String -> String -> Result String DaprError

Secrets

# Get secret
secret-get : Config -> String -> String -> Result (Map String String) DaprError

# Get all secrets
secret-bulk-get : Config -> String -> Result (Map String (Map String String)) DaprError

Bindings

# Invoke output binding
binding-invoke : Config -> String -> String -> a -> Result String DaprError

# Invoke with metadata
binding-invoke-with-metadata : Config -> String -> String -> a -> Map String String -> Result String DaprError

Health

# Health check
health : Config -> Result Bool DaprError

# Get metadata
metadata : Config -> Result DaprMetadata DaprError

Types

Config

type Config = {
  host: String,        # Sidecar hostname (default: "localhost")
  port: Int,           # Sidecar HTTP port (default: 3500)
  api-version: String  # Dapr API version (default: "v1.0")
}

StateOperation

type StateOperation =
  | Upsert {key: String, value: String}
  | Delete {key: String}

DaprError

type DaprError =
  | DaprConnectionError {message: String}
  | DaprStateError {message: String}
  | DaprPubSubError {message: String}
  | DaprInvokeError {message: String}
  | DaprSecretError {message: String}
  | DaprBindingError {message: String}
  | DaprHealthError {message: String}

Examples

State Transactions

import "dapr" as Dapr

config = Dapr.default-config

# Atomic multi-operation
ops = [
  Upsert {key: "user:1", value: "{\"name\":\"Alice\"}"},
  Upsert {key: "user:2", value: "{\"name\":\"Bob\"}"},
  Delete {key: "user:old"}
]

match Dapr.state-transaction config "statestore" ops
  | Ok _ -> print "Transaction committed"
  | Err e -> print "Error: ${show e}"

State with TTL

import "dapr" as Dapr
import Map

config = Dapr.default-config
meta = Map.from-list [("ttlInSeconds", "3600")]

match Dapr.state-save-with-metadata config "statestore" "session:123" {user: "alice"} meta
  | Ok _ -> print "Session saved with 1-hour TTL"
  | Err e -> print "Error: ${show e}"

Pub/Sub

import "dapr" as Dapr

config = Dapr.default-config

match Dapr.publish config "pubsub" "orders" {order-id: "123", total: 99.99}
  | Ok _ -> print "Event published"
  | Err e -> print "Error: ${show e}"

Service Invocation

import "dapr" as Dapr

config = Dapr.default-config

match Dapr.invoke config "order-service" "create" "{\"item\": \"widget\"}"
  | Ok response -> print "Response: ${response}"
  | Err e -> print "Error: ${show e}"

Running with Dapr

# Development (with Dapr CLI)
dapr run --app-id my-app -- kit run myapp.kit

# With specific components
dapr run --app-id my-app --components-path ./components -- kit run myapp.kit

License

MIT License - see LICENSE for details.

Exported Functions & Types

Dapr sidecar configuration.

default-config

Default configuration for local development.

config-from-env

Create configuration from environment variables.

Config

config-with-host

Create configuration with custom host and port.

String -> Int -> Config

DaprError

Dapr error type for typed error handling.

Variants

DaprConnectionError {message}
DaprStateError {message}
DaprPubSubError {message}
DaprInvokeError {message}
DaprSecretError {message}
DaprBindingError {message}
DaprHealthError {message}

State item for bulk operations.

StateOperation

State operation for transactions.

Variants

Upsert {key, value}
Delete {key}

Dapr metadata response from /metadata endpoint.

state-save

Save a single key-value pair to a state store.

Config -> String -> String -> a -> Result Unit DaprError

state-save-with-metadata

Save a single key-value pair to a state store with metadata.

Config -> String -> String -> a -> Map String String -> Result Unit DaprError

state-get

Get a value from a state store.

Config -> String -> String -> Result (Option String) DaprError

state-delete

Delete a key from a state store.

Config -> String -> String -> Result Unit DaprError

state-bulk-get

Get multiple keys from a state store in bulk.

Config -> String -> [String] -> Result [StateItem] DaprError

state-transaction

Execute a state transaction with multiple operations.

Config -> String -> [StateOperation] -> Result Unit DaprError

publish

Publish an event to a topic.

Config -> String -> String -> a -> Result Unit DaprError

publish-with-metadata

Publish an event to a topic with metadata.

Config -> String -> String -> a -> Map String String -> Result Unit DaprError

invoke

Invoke a method on another Dapr-enabled service.

Config -> String -> String -> String -> Result String DaprError

invoke-with-method

Invoke a method with a specific HTTP method.

Config -> String -> String -> String -> String -> Result String DaprError

secret-get

Get a secret from a secret store.

Config -> String -> String -> Result (Map String String) DaprError

secret-bulk-get

Get all secrets from a secret store.

Config -> String -> Result (Map String (Map String String)) DaprError

binding-invoke

Invoke an output binding.

Config -> String -> String -> a -> Result String DaprError

binding-invoke-with-metadata

Invoke an output binding with metadata.

Config -> String -> String -> a -> Map String String -> Result String DaprError

health

Check if the Dapr sidecar is healthy.

Config -> Result Bool DaprError

metadata

Get Dapr sidecar metadata.

Config -> Result DaprMetadata DaprError