@wroud/preconditions is a flexible and extensible library for managing preconditions in JavaScript and TypeScript applications. It simplifies the process of validating and preparing entities (e.g., nodes, connections, features) by providing a robust framework for defining and applying preconditions.
- Entity-Specific Managers: Manage preconditions independently for different types of entities.
- Dynamic Applicability: Apply preconditions selectively to specific instances based on their data.
- Extensibility: Add new preconditions dynamically in a plugin-style manner.
- Check-Only and Fulfill Modes: Support both validating preconditions and attempting to fulfill them.
- TypeScript Support: Provides strong typing for enhanced developer experience.
- Pure ESM package
Install via npm:
npm install @wroud/preconditions
Install via yarn:
yarn add @wroud/preconditions
For detailed usage and API reference, visit the documentation site.
export interface Node {
id: string;
data: {
requiresDatabaseConnection?: boolean;
};
}
import type { IPrecondition } from "@wroud/preconditions";
export class DatabaseConnectionPrecondition implements IPrecondition<Node> {
isApplicable(node: Node): boolean {
return node.data.requiresDatabaseConnection === true;
}
async check(node: Node): Promise<boolean> {
return databaseConnection.isEstablished();
}
async fulfill(node: Node): Promise<void> {
await databaseConnection.connect();
}
}
import { PreconditionManager } from "@wroud/preconditions";
import { Node } from "./Node";
import { DatabaseConnectionPrecondition } from "./NodePreconditions";
const nodePreconditionManager = new PreconditionManager<Node>();
nodePreconditionManager.register(new DatabaseConnectionPrecondition());
const node: Node = { id: "node1", data: { requiresDatabaseConnection: true } };
const canLoad = await nodePreconditionManager.checkPreconditions(node);
if (canLoad) {
console.log("Node can be loaded.");
} else {
try {
await nodePreconditionManager.fulfillPreconditions(node);
console.log("Preconditions fulfilled. Node is ready to load.");
} catch (error) {
console.error("Failed to fulfill preconditions:", error);
}
}
All notable changes to this project will be documented in the CHANGELOG file.
This project is licensed under the MIT License. See the LICENSE file for details.