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

@contract-first-api/core

v2.0.0

Published

Define shared API contracts and derive typed request and response helpers from Zod schemas.

Readme

@contract-first-api/core

@contract-first-api/core is the starting point of the workflow. You define your shared contract tree here, and the other packages build on top of it.

What you do with this package

Use it to:

  • describe contracts with path, method, request, response, and optional meta
  • keep request and response types shared between frontend and backend
  • create path-based helper types from the same tree
  • inspect or transform the tree when you need advanced behavior

Basic usage

import { initContracts } from "@contract-first-api/core";
import z from "zod";

type ContractMeta = {
  requiresAuth?: boolean;
  auditLabel?: string;
};

const { defineContract, mergeContracts } = initContracts<ContractMeta>();

const healthContracts = defineContract({
  health: {
    get: {
      method: "GET",
      path: "/health",
      meta: {
        auditLabel: "health.get",
      },
      response: z.object({
        status: z.literal("ok"),
        requestId: z.string(),
      }),
    },
  },
});

const todoContracts = defineContract({
  todos: {
    list: {
      method: "GET",
      path: "/todos",
      response: z.object({
        items: z.array(
          z.object({
            id: z.string(),
            title: z.string(),
            createdAt: z.string(),
          }),
        ),
      }),
    },
    create: {
      method: "POST",
      path: "/todos",
      meta: {
        requiresAuth: true,
        auditLabel: "todos.create",
      },
      request: {
        body: z.object({
          title: z.string().min(1),
        }),
      },
      response: z.object({
        id: z.string(),
        title: z.string(),
        createdAt: z.string(),
      }),
    },
  },
});

export const contracts = mergeContracts(healthContracts, todoContracts);

Common pattern

It is common to export helper types from the shared contract package so the backend and frontend stay aligned:

import type {
  ContractApiRequest,
  ContractApiResponse,
  DotPaths,
} from "@contract-first-api/core";

export type ExampleContracts = typeof contracts;
export type ApiPath = DotPaths<ExampleContracts>;
export type ApiMeta = ContractMeta;

export type ApiRequest<P extends ApiPath> = ContractApiRequest<
  ExampleContracts,
  P
>;

export type ApiResponse<P extends ApiPath> = ContractApiResponse<
  ExampleContracts,
  P
>;

That lets you write application code like:

type CreateTodoInput = ApiRequest<"todos.create">;
type Todo = ApiResponse<"todos.create">;
type Health = ApiResponse<"health.get">;
type Meta = ApiMeta;

Tree utilities

The package also exports a few small utilities for advanced transforms:

  • flattenContractTree(contracts) to turn the tree into a flat list of contracts with keySegments
  • mapContractTree(contracts, mappingFn) to preserve the tree shape while mapping every contract
  • mapObjectValues(tree, isLeaf, mappingFn) for generic tree transforms

Typical project structure

In practice this package usually lives in a shared workspace package:

  • shared/contracts defines the contract tree
  • backend imports the same tree to register routes
  • frontend imports the same tree to build a typed client

That shared-first setup is the intended workflow for this package.