• Joined on 2026-02-19

@phoenix-tekhne/pattern-core (0.2.0)

Published 2026-05-28 07:20:31 -07:00 by dmonphx

Installation

@phoenix-tekhne:registry=https://gitrepo.cstudiosinc.com/api/packages/dmonphx/npm/
npm install @phoenix-tekhne/pattern-core@0.2.0
"@phoenix-tekhne/pattern-core": "0.2.0"

About this package

@phoenix-tekhne/pattern-core

Core utility patterns for building enterprise applications with Phoenix Tekhne.

Overview

Pattern-Core provides reusable architectural patterns for service layers, caching, error handling, and event management. These patterns follow functional programming principles and enterprise-grade best practices.

Patterns

Result Type

Functional error handling with Ok and Err variants.

import { Ok, Err, ResultBuilder, unwrapOr } from "@phoenix-tekhne/pattern-core";

function fetchUser(id: string): Result<User, Error> {
  try {
    const user = db.getUser(id);
    return Ok(user);
  } catch (e) {
    return Err(new Error("User not found"));
  }
}

// Safe unwrapping
const user = unwrapOr(fetchUser("123"), null);

Cache

TTL-based caching with automatic eviction.

import { Cache } from "@phoenix-tekhne/pattern-core";

const cache = new Cache<string>({ ttlMs: 60000, maxSize: 100 });
cache.set("key", "value");
const value = cache.get("key"); // 'value' or undefined if expired

TypedEventEmitter

Type-safe event system.

import { TypedEventEmitter } from "@phoenix-tekhne/pattern-core";

interface Events {
  message: string;
  count: number;
}

const emitter = new TypedEventEmitter<Events>();
emitter.on("message", (msg) => console.log(msg));
emitter.emit("message", "Hello!");

TekhneService

Base class for service layers.

import { TekhneService, ServiceConfig } from "@phoenix-tekhne/pattern-core";

class UserService extends TekhneService {
  constructor(config: ServiceConfig) {
    super(config);
  }

  async initialize() {
    this.getLogger().info("Service initialized");
  }
}

Repository

Generic data storage pattern.

import { Repository } from "@phoenix-tekhne/pattern-core";

interface User {
  id: string;
  name: string;
}

const userRepo = new Repository<User, string>({
  getId: (user) => user.id,
});

userRepo.add({ id: "1", name: "John" });
const user = userRepo.findById("1");

Installation

npm install @phoenix-tekhne/pattern-core

Dependencies

  • React >= 19.0.0 (peer dependency)

For AI Agents: Quick Reference

When to Use This Package

Use Case Pattern Example
Function might fail Result<T, E> Database calls, API requests
Need caching Cache<T> Expensive computations, API responses
Event system TypedEventEmitter<T> Cross-component communication
Service layer TekhneService Business logic, data access
Data collection Repository<T, ID> CRUD operations, collections

Pattern Selection Guide

// Error handling - Always use Result instead of throwing
const user = await fetchUser(id); // ❌ Might throw
const result = await fetchUserSafe(id); // ✅ Returns Result<User, Error>

// Check result type
if (ResultBuilder.isOk(result)) {
  const user = result.value;
} else {
  console.error(result.error);
}

// Safe unwrapping with defaults
const user = unwrapOr(result, defaultUser);

Key Files

File Purpose Use When
Result.ts Error handling Any async operation that might fail
Cache.ts Caching Data that doesn't change frequently
TypedEventEmitter.ts Events Component communication
TekhneService.ts Services Creating new service classes
Repository.ts Data storage Managing collections

Common Patterns

// Result Pattern
import { Ok, Err, unwrapOr } from "@phoenix-tekhne/pattern-core";

function divide(a: number, b: number): Result<number, Error> {
  if (b === 0) return Err(new Error("Division by zero"));
  return Ok(a / b);
}

// Cache Pattern
const cache = new Cache<string>({ ttlMs: 60000 });
const cached = cache.get(key) ?? computeAndCache(key);

// Service Pattern
class UserService extends TekhneService {
  async getUser(id: string): Promise<Result<User, ServiceError>> {
    return this.withErrorHandling(async () => {
      return await this.db.users.findById(id);
    });
  }
}

Documentation

Each pattern includes:

  • Full TypeScript definitions
  • Unit tests (*.test.ts)
  • Usage examples

Contributing

See AGENTS.md in repository root for contribution guidelines.

License

MIT - Phoenix Inceptions Media LLC

Dependencies

Development Dependencies

ID Version
@testing-library/jest-dom ^6.9.1
@types/react ^19.2.14
happy-dom ^13.10.1
typescript ^5.0.0
vite ^5.0.0
vite-plugin-dts 4.5.4
vitest ^1.6.1

Peer Dependencies

ID Version
react >=19.0.0
Details
npm
2026-05-28 07:20:31 -07:00
8
latest
19 KiB
Assets (1)
Versions (1) View all
0.2.0 2026-05-28