@zudojs/constants
v1.0.0
Published
Shared constants, enums, and type-safe literals for the Zudojs framework.
Readme
@zudojs/constants
Shared constants, enums, branded types, and type-safe literals for the Zudojs framework.
Installation
npm install @zudojs/constantsQuick Start
import type { UserId, Timestamp } from "@zudojs/constants";
import {
HttpStatus,
HttpMethods,
createUserId,
createTimestamp,
resolveEnvironment,
formatDuration,
} from "@zudojs/constants";
const id: UserId = createUserId("user_123");
const now: Timestamp = createTimestamp(new Date().toISOString());
const status = HttpStatus.OK; // 200
const env = resolveEnvironment(); // "development" | "test" | "staging" | "production"
formatDuration(150_000); // "2m 30s"All constant objects are Object.freezed as const maps, and every map has a
matching literal-union type (HttpStatusCode, HttpMethod, Environment,
LifecycleState, SchemaIssueCode, ...). Sets such as HTTP_METHODS and
SCHEMA_FORBIDDEN_KEYS are immutable at runtime: add/delete/clear throw.
Modules
http
HttpMethods,HTTP_METHODS,SAFE_HTTP_METHODS,IDEMPOTENT_HTTP_METHODSHttpStatusplus range helpers (isSuccessStatus,isClientError, ...)HttpHeadercommon header namesContentTypes,Charset(MIME charset labels),buildContentType()
environment
Environments,ENVIRONMENTS,isValidEnvironment()resolveEnvironment(env?, options?),isProduction(),isDevelopment(),isTest()
resolveEnvironment reads NODE_ENV (case-insensitively, accepting the
aliases dev/prod). An unrecognized value falls back to "development" but
logs a console.warn once per distinct value, so a typo such as
NODE_ENV=prodution is never silent. Use { strict: true } to throw an
InvalidConstantError instead, or { silent: true } to suppress the warning.
import { resolveEnvironment, isProduction } from "@zudojs/constants";
resolveEnvironment({ NODE_ENV: "PROD" }); // "production"
resolveEnvironment({ NODE_ENV: "prodution" }); // "development" + one-time warning
resolveEnvironment({ NODE_ENV: "prodution" }, { strict: true }); // throws InvalidConstantError
isProduction(); // reads process.envtime
TimeMsdurations (SECOND ... YEAR),DefaultTimeout,DefaultRetryTimeUnits/TimeUnit,toMilliseconds(value, unit),formatDuration(ms)
import { TimeMs, toMilliseconds, formatDuration } from "@zudojs/constants";
const ttl = 5 * TimeMs.MINUTE; // 300_000
toMilliseconds(2, "hours"); // 7_200_000
formatDuration(90_000); // "1m 30s"
formatDuration(4_500_000); // "1h 15m"common
- Branded types (
UserId,EventId,Timestamp,Url,EmailAddress, ...) withcreateXfactories — the validating ones (createTimestamp,createUrl,createEmailAddress,createHexString,createBase64String,createJsonString) throwInvalidConstantErroron bad input Limits,Defaults,Sentineland sentinelsNONE,UNINITIALIZED,EMPTY- Lifecycle state machine:
LifecycleState,LifecyclePhase,LIFECYCLE_VALID_TRANSITIONS, timeouts/retries - Schema constants:
SchemaIssueCode,SCHEMA_FORBIDDEN_KEYS(immutable at runtime),SCHEMA_STRING_FORMATS - Serialization constants:
SerializationFormat,SerializationContentType,SerializationLimits,SerializationTags
validation
ValidationPattern— the single source of truth for regexes (EMAIL, UUID, UUID_V4, IPV4, IPV6, ISO_DATE_TIME, URL, SEMVER, PHONE, FILE_NAME, ...).SCHEMA_STRING_FORMATSre-exports these rather than redefining them.ValidationLength,ValidationRange
import { ValidationPattern, ValidationLength } from "@zudojs/constants";
ValidationPattern.SEMVER.test("1.0.0-alpha-1"); // true (semver.org grammar)
ValidationPattern.IPV6.test("::ffff:192.0.2.1"); // true
ValidationPattern.FILE_NAME.test(".."); // false (path traversal)
ValidationLength.EMAIL; // 254cache
CacheStrategies,CacheDuration,buildCacheControl()
priority
Priorities,PriorityWeight,comparePriority()
runtime
- Injectable
Clock/Randominterfaces withsystemClock/systemRandom(fullynode:crypto-backed, safe for tokens and salts) and deterministiccreateMockClock()/createMockRandom(seed)for tests MockClockaddsadvance(ms)andset(timestampOrDate)
import {
createMockClock,
createMockRandom,
systemRandom,
} from "@zudojs/constants";
const clock = createMockClock(0);
clock.advance(1_000);
clock.set(new Date("2024-01-01T00:00:00Z"));
clock.now(); // 1704067200000
const rng = createMockRandom(42); // same seed => same sequence
rng.randomInt(1, 6);
systemRandom.randomString(32); // CSPRNG-backedErrors
InvalidConstantError,ConstantContextError(built on@zudojs/errors; error codes themselves live in@zudojs/errors)
