@astralibx/core
v1.2.2
Published
Shared foundation for all @astralibx packages — base errors, types, and validation helpers
Maintainers
Readme
@astralibx/core
Shared foundation for all @astralibx packages -- base errors, types, and validation helpers.
Install
npm install @astralibx/corezod is included as a dependency -- no need to install it separately.
Quick Start
import { z } from 'zod';
import {
AlxError,
createConfigValidator,
baseDbSchema,
loggerSchema,
} from '@astralibx/core';
// 1. Extend AlxError for your package
class QueueError extends AlxError {
constructor(message: string) {
super(message, 'QUEUE_ERROR');
this.name = 'QueueError';
}
}
// 2. Compose a config schema from core fragments + your own fields
const queueConfigSchema = z.object({
db: baseDbSchema,
logger: loggerSchema.optional(),
concurrency: z.number().int().positive().default(5),
});
// 3. Validate config -- throws ConfigValidationError on invalid input
const validate = createConfigValidator(queueConfigSchema);
validate({
db: { connection: mongoClient.db() },
concurrency: 10,
});What's Included
Error classes
AlxError-- Base error for the ecosystem. Carries acodestring.ConfigValidationError-- Thrown on invalid config. Adds afieldproperty.
Type contracts
LogAdapter-- Logger-agnostic interface (info,warn,errormethods).BaseDbConfig-- Database connection + optionalcollectionPrefix.BaseRedisConfig-- Redis connection + optionalkeyPrefix.
Zod schemas
loggerSchema-- Validates aLogAdapter-shaped object.baseDbSchema-- ValidatesBaseDbConfig(connection must be non-null).baseRedisSchema-- ValidatesBaseRedisConfig(connection must be non-null).
Shared utilities
noopLogger-- No-opLogAdapterfor fallback when consumer doesn't provide a logger.RedisLock-- Distributed lock using RedisSET NX PXwith safe Lua release.getParam(req, name)-- Extract route param from Express request.getQueryString(req, name)-- Extract query string param from Express request.sendSuccess(res, data, status?)-- Send{ success: true, data }JSON response.sendError(res, error, status?)-- Send{ success: false, error }JSON response.
Helpers
createConfigValidator-- Takes a Zod schema, returns a validate function that throwsConfigValidationErroron failure.
Documentation
- Error Classes -- AlxError, ConfigValidationError, extending for your package
- Type Contracts -- LogAdapter, BaseDbConfig, BaseRedisConfig
- Validation -- Zod schemas, createConfigValidator, composing schemas
- Package Author Guide -- Building new @astralibx packages on top of core
License
MIT
