npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@vernali/contracts

v0.0.8

Published

Shared TypeScript interfaces and base error classes for the Vernali suite

Readme

@vernali/contracts

Shared TypeScript interfaces and base error classes for the entire Vernali ecosystem.

Installation

npm install @vernali/contracts

Requires Node.js >=18 and TypeScript >=5.4.

Overview

@vernali/contracts is the contracts package for the Vernali ecosystem. It contains exclusively TypeScript interfaces, types, and base error classes that every package in the suite must implement. Centralising these contracts enforces architectural consistency and strict typing across services without introducing coupling between concrete implementations.

The package is zero-dependency in production: it declares no dependencies or peerDependencies. The TypeScript compiler is the only development dependency.

Exported contracts

| Contract | Kind | Description | |---|---|---| | IApplication | Interface | Application contract: routing, middleware, dispatch, and static file serving | | IServerOptions | Interface | Server network parameters (port, hostname) | | IContext | Interface | State of a single HTTP request/response cycle | | IRouter / ILayer | Interfaces | Router and route unit: registration, matching, and stack execution | | IMiddleware / IMiddlewareFn / Next | Types | Signature for function-style or class-style middlewares; next is optional | | IBodyParser / ParsedBody | Interface / Type | Contract for extracting and typing the request body | | ILogger / ILogEntry / LogLevel | Interfaces / Type | Logger with severity levels and scoped child loggers per requestId | | IHttpError | Interface | Shape that all HTTP error objects must satisfy | | HttpException | Class | Concrete base class for HTTP errors | | NotFoundError | Class | 404 Not Found | | UnauthorizedError | Class | 401 Unauthorized | | BadRequestError | Class | 400 Bad Request | | ForbiddenError | Class | 403 Forbidden | | InternalServerError | Class | 500 Internal Server Error |


Dependencies

Production

This package has no runtime dependencies.

Development

| Package | Version | Purpose | |---|---|---| | typescript | ^5.4.0 | TypeScript compiler | | vitest | ^1.6.0 | Testing framework |

TypeScript configuration

The package compiles with a configuration targeting native ESM modules on Node.js:

| Option | Value | Effect | |---|---|---| | target | ES2022 | Enables native private fields and ES2022 class semantics | | module | NodeNext | Hybrid ESM/CJS resolution following Node.js module rules | | moduleResolution | NodeNext | Imports must include the explicit .js extension | | strict | true | Enables all strict type-checking rules | | noUncheckedIndexedAccess | true | Index access yields T \| undefined | | exactOptionalPropertyTypes | true | Distinguishes between an absent property and an explicit undefined | | declaration + declarationMap | true | Emits .d.ts files with source maps for IDE navigation |


Usage

Throwing an HTTP error

HttpException is the base class for all HTTP errors. The predefined subclasses cover the most common status codes; for any other code, instantiate HttpException directly.

import { HttpException, NotFoundError } from '@vernali/contracts';

// Semantic subclass: status code and status text are set automatically
throw new NotFoundError('The requested resource does not exist');

// Base class: status code, message, and error name are fully configurable
throw new HttpException(422, 'The "email" field is not valid', 'Unprocessable Entity');

All errors expose toJSON(), which serialises { statusCode, error, message } — ready to be sent as a response body.


Implementing a middleware

IMiddlewareFn<T> accepts two signatures: with next (to continue the chain) or without it (to terminate the cycle immediately). Both are equivalent from the composition engine's perspective.

import type { IMiddlewareFn } from '@vernali/contracts';

// Without next: the handler responds and ends the cycle
const healthCheck: IMiddlewareFn = async (ctx) => {
  ctx.status(200).json({ status: 'ok' });
};

// With next: runs its own logic and delegates to the next middleware
const requestLogger: IMiddlewareFn = async (ctx, next) => {
  const start = Date.now();
  await next();
  console.log(`${ctx.method} ${ctx.path} — ${Date.now() - start}ms`);
};

Implementing a Router

IRouter<T> defines the contract any router implementation must satisfy. The handle method shares the same signature as IMiddlewareFn, which allows routers to be nested inside other routers or the application without additional adapters.

import type { IRouter, ILayer, IContext, IMiddlewareFn, Next } from '@vernali/contracts';

class MyRouter implements IRouter {
  readonly stack: ILayer[] = [];

  // Registers loose middlewares, sub-routers, or method-agnostic routes
  use(path: string | RegExp | IRouter | IMiddlewareFn, ...handlers: IMiddlewareFn[]): this {
    return this;
  }

  // Unified entry point for adding a layer to the stack
  register(method: string | null, path: string | RegExp, ...handlers: IMiddlewareFn[]): this {
    return this;
  }

  // Matches all requests regardless of HTTP method
  all(path: string | RegExp, ...handlers: IMiddlewareFn[]): this {
    return this.register(null, path, ...handlers);
  }

  get(path: string, ...handlers: IMiddlewareFn[]): this {
    return this.register('GET', path, ...handlers);
  }

  // Executes the middleware stack; next is optional to allow nesting
  async handle(ctx: IContext, next?: Next): Promise<void> {
    if (next) await next();
  }
}

Extending the context with generics

All primary interfaces — IMiddlewareFn, IRouter, IApplication — are generic over T extends IContext. This allows additional properties to be added to the context and that typing to be propagated through the entire middleware stack without explicit casts.

import type { IContext, IMiddlewareFn, IApplication } from '@vernali/contracts';

// Extended context carrying authentication data and runtime configuration
interface AppContext extends IContext {
  user?: { id: string; role: 'admin' | 'user' };
  config: Record<string, string>;
}

// ctx is typed as AppContext in all parametrised middlewares
const requireAuth: IMiddlewareFn<AppContext> = async (ctx, next) => {
  if (!ctx.user) throw new Error('Unauthenticated');
  await next();
};

// The application is bound to the same context type
declare const app: IApplication<AppContext>;
app.use(requireAuth);