Skip to content

koka-ts/koka

Repository files navigation

Koka - Lightweight TypeScript Effect Management Library Based on Algebraic Effects

Warning: This library is in early development and may undergo significant changes. Do not use in production environments.

Koka is a lightweight TypeScript effect management library based on algebraic effects, providing structured error handling, context management, and asynchronous operations with composability and type safety.

📚 Documentation Navigation

中文文档

📋 Quick Navigation

🚀 Quick Start

Installation

npm install koka
# or
yarn add koka
# or
pnpm add koka

Basic Usage

import { Eff } from 'koka'

// Error handling
class ValidationError extends Eff.Err('ValidationError')<string> {}

function* getUser(id: string) {
    if (!id) {
        yield* Eff.throw(new ValidationError('ID is required'))
    }
    return { id, name: 'John Doe' }
}

// Context management
class Discount extends Eff.Ctx('Discount')<number> {}

function* calculateTotal() {
    const discount = yield* Eff.get(Discount)
    return 100 * (1 - discount)
}

// Async operations
async function* fetchData() {
    const response = yield* Eff.await(fetch('/api/data'))
    return response.json()
}

// Run effects
const result = await Eff.run(
    Eff.try(getUser('123')).handle({
        ValidationError: (error) => ({ error }),
    }),
)

✨ Core Features

  • Type Safe - Full TypeScript support
  • Lightweight - Only ~3kB gzipped
  • Composable - Effects naturally compose
  • Async Ready - Seamless Promise integration
  • Design First - Support for predefined effect types

🔄 Comparison with Effect-TS

Feature Koka Effect-TS
Error Effects
Context Effects
Async Effects
Composability
Type Safety
Minimal API
Full Ecosystem
Learning Curve Low High
Package Size ~3kB ~50kB

Koka is a lightweight alternative to Effect-TS, focusing on providing core effect management functionality without the complete ecosystem.

📖 Documentation Structure

Tutorials

How-to Guides

Reference

Explanations

🤝 Contributing

PRs are welcome! Please ensure tests pass and new features include appropriate test coverage.

📄 License

MIT

About

Lightweight 3kB Effect-TS alternative library based on Algebraic Effects

Topics

Resources

License

Stars

Watchers

Forks

Contributors 2

  •  
  •  

Languages