- Adjusted .prettierrc to ensure consistent formatting with a newline at the end of the file. - Reformatted eslint.config.mjs for improved readability by aligning array elements. - Updated pnpm-lock.yaml to use single quotes for consistency across dependencies. - Simplified worktree setup in .cursor/worktrees.json for cleaner configuration. - Enhanced documentation in .cursor/plans to clarify architecture refactoring. - Refactored various service files for improved readability and maintainability, including rate-limiting and auth services. - Updated imports and exports across multiple files for consistency and clarity. - Improved error handling and logging in service methods to enhance debugging capabilities. - Streamlined utility functions for better performance and maintainability across the domain packages.
65 lines
1.4 KiB
TypeScript
65 lines
1.4 KiB
TypeScript
/**
|
|
* Toolkit - Type Assertions
|
|
*
|
|
* Runtime assertion utilities for type safety.
|
|
*/
|
|
|
|
export class AssertionError extends Error {
|
|
constructor(message: string) {
|
|
super(message);
|
|
this.name = "AssertionError";
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Assert that a value is truthy
|
|
*/
|
|
export function assert(condition: unknown, message = "Assertion failed"): asserts condition {
|
|
if (!condition) {
|
|
throw new AssertionError(message);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Assert that a value is defined (not null or undefined)
|
|
*/
|
|
export function assertDefined<T>(
|
|
value: T | null | undefined,
|
|
message = "Value must be defined"
|
|
): asserts value is T {
|
|
if (value === null || value === undefined) {
|
|
throw new AssertionError(message);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Assert that a value is a string
|
|
*/
|
|
export function assertString(
|
|
value: unknown,
|
|
message = "Value must be a string"
|
|
): asserts value is string {
|
|
if (typeof value !== "string") {
|
|
throw new AssertionError(message);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Assert that a value is a number
|
|
*/
|
|
export function assertNumber(
|
|
value: unknown,
|
|
message = "Value must be a number"
|
|
): asserts value is number {
|
|
if (typeof value !== "number" || isNaN(value)) {
|
|
throw new AssertionError(message);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Assert that a value is never reached (exhaustiveness check)
|
|
*/
|
|
export function assertNever(value: never, message = "Unexpected value"): never {
|
|
throw new AssertionError(`${message}: ${JSON.stringify(value)}`);
|
|
}
|