UNPKG

@schorts/shared-kernel

Version:

A modular, type-safe foundation for building expressive, maintainable applications. This package provides core abstractions for domain modeling, HTTP integration, authentication, state management, and more โ€” designed to be framework-agnostic and highly ex

126 lines (80 loc) โ€ข 4.39 kB
# ๐Ÿ“ฆ Shared Kernel A modular, type-safe foundation for building expressive, maintainable applications. This package provides core abstractions for domain modeling, HTTP integration, authentication, state management, and more โ€” designed to be framework-agnostic and highly extensible. ## ๐Ÿš€ Installation ```bash npm install @schorts/shared-kernel --save ``` ## ๐Ÿงฑ Modules ### ๐Ÿ” Auth - **AuthProvider:** Abstract interface for authentication strategies. - **RequireAuthDecorator:** Method-level decorator for enforcing authentication. ### ๐Ÿ›ก๏ธ RBAC (Role-Based Access Control) - **RBACPolicy:** Abstract base class for defining role-based permission logic. Supports wildcard actions (manage) and resources (*), ownership checks, and composable access control strategies. - **Permission:** Lightweight value object representing an action-resource pair (e.g., read:orders, manage:*). ### ๐Ÿ“Š Criteria - **Criteria:** Fluent query builder for filtering, sorting, and pagination. ### ๐Ÿ“ฃ Domain Events - **DomainEvent:** Base class for domain-driven event dispatching. ### ๐Ÿงฌ Entities - **Entity:** Base class for identity-based domain entities. - **EntityRegistry:** Dynamic registry for entity constructors, enabling polymorphic and type-safe instantiation of domain entities from serialized data. ### ๐Ÿงน Formatters - **PascalCamelToSnake:** Utility for converting PascalCase or camelCase to snake_case. ### ๐ŸŒ HTTP - **HTTPProvider:** Abstract interface for HTTP transport. - **FetchHTTPProvider:** Concrete implementation using fetch. ### ๐ŸŒ Internationalization (i18n) - **TranslationResolver:** Infrastructure-agnostic interface for resolving localized strings within the domain. Enables domain errors, validations, and events to be presented in different languages without coupling to any specific i18n implementation. Supports injection into domain exceptions, decorators, and services. Translation keys are centralized in registries for discoverability and tooling, with patterns to extend keys per bounded context. ### ๐Ÿ”— JSON:API - **JSONAPIConnector:** Connector for interacting with JSON:API-compliant endpoints. ### ๐Ÿงฉ Models - **BaseModel:** Base class for serializable, type-safe models. ### ๐ŸŽฏ Result - **Result:** Type-safe wrapper for success/failure outcomes. Encapsulates either a value or an error, enforcing disciplined error handling without exceptions. Includes static factories (`success`, `error`), accessors (`getValue`, `getError`), and guards (`isSuccess`, `isFailure`). ### ๐Ÿ›  Persistence - **DAO:** Generic interface defining data access operations for domain entities. - **UnitOfWork:** Interface enabling transactional coordination across multiple persistence operations. ### ๐Ÿง  State Manager - **StateManager:** Abstract reactive state manager with listener support. - **SessionStorageStateManager:** Concrete implementation using session storage. ### ๐Ÿงช Value Objects - **CoordinatesValue** - **EmailValue** - **EnumValue** - **IntegerValue** - **PhoneValue** - **SlugValue** - **StringValue** - **UUIDValue** - **BooleanValue** - **DateValue** Each value object enforces domain constraints and immutability, ensuring correctness at the boundary of your system. ## ๐Ÿง  Philosophy This kernel is built around: - **Type safety:** Every abstraction is strongly typed and composable. - **Domain expressiveness:** Value objects, entities, and events encode business logic directly. - **Extensibility:** Plug in your own HTTP, auth, or state strategies. - **Framework independence:** Use it with Node.js, frontend apps, or serverless functions. ## ๐Ÿ“ฆ Example Usage ```ts import { Criteria } from "@schorts/shared-kernel/criteria"; import { FetchHTTPProvider } from "@schorts/shared-kernel/http"; import { JSONAPIConnector } from "@schorts/shared-kernel/json-api"; // Didn't know what url use for the example hehe const usersURL = new URL("https://github.com/schorts99"); const criteria = new Criteria() .where("status", "EQUAL", "active") .orderBy("created_at", "DESC") .limitResults(10); const connector = new JSONAPIConnector(new FetchHTTPProvider()); // UserModel is your own Model const users = await connector.findMany<UserModel>(usersURL, criteria); ``` ## ๐Ÿงช Testing Each module is fully covered with unit tests. To run them: ```bash npm run test ``` ## ๐Ÿ“œ License LGPL