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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@apolitical/contracts

v5.0.0

Published

An Nx library and NPM package for defining and interacting with Apolitical API endpoints, designed to streamline development with consistent contracts and robust type safety.

Readme

Apolitical / Contracts

An Nx library and NPM package for defining and interacting with Apolitical API endpoints, designed to streamline development with consistent contracts and robust type safety.

Note that @apolitical/contracts has been recently refactored to follow the RFC for consolidating contracts. Please refer to the RFC for more info on the new structure.

Overview

@apolitical/contracts provides:

  • Unified API contracts: Standardized TypeScript definitions for API endpoints
  • Developer-friendly structure: A simplified and intuitive folder layout
  • Type safety: Ensures reliable and predictable API interactions

For more on the underlying technology, check out ts-rest.

Structure

The new structure would look like this:

/src
  /apis
    /mgmt-api
      /accounts
        accounts.contract.ts
        connections.contract.ts
        index.ts
      mgmt-api.contract.ts
      mgmt-api.types.ts
      index.ts
    // more APIs here
  /ts
    /i18n
      i18n.constants.ts
      i18n.enums.ts
      i18n.types.ts
    // more files here
  /zod
    /extensions
    index.ts
  index.ts

Briefly:

  • /src/apis contains the contracts for each API
  • /src/ts contains the shared types, enums and constants
  • /src/zod contains the shared zod extensions

Usage

Creating a new resource

Let’s imagine you have to create a new endpoint POST /images within the assets-api, so then the steps to create the contract for that new endpoint are as follows:

  1. Locate the assets-api at /backend/v2/libs/contracts/src/apis/assets-api
  2. Given that the Image is a new resource within assets-api, we create a images.contract.ts file (as described here)
  3. Implement images subcontract with it’s new create method (as described here)
  4. Add the images subcontract to the assets top-contract at assets-api.contract.ts file
  5. Expose any shared types at assets-api.types.ts
    1. If there is a shared enum, define it at assets-api.enums.ts
    2. If there is a shared constant, define it at assets-api.constants.ts

Creating a nested resource

Let’s imagine you have to create a new endpoint POST /learners/:id/images within the assets-api, so then the steps to create the contract for that new endpoint are as follows:

  1. Locate the assets-api at /backend/v2/libs/contracts/src/apis/assets-api
  2. If the learners folder doesn’t exist create one (note that the learners folder is created to encapsulate the learners.contract.ts and the images.contract.ts as the images contract is a subcontract of learners).
  3. Follow the steps describe above, but exposing the subcontracts in right order of hierarchy, in this case assetslearnersimages

Integrating with Nest.js endpoints

We follow the Nest.js integration guide to integrate with Nest.js endpoints. In particular, we use the @TsRestHandler decorator to define the handler for the endpoint as we are progressing towards a fully ts-rest-fied Nest.js setup.

Consuming the client on the frontend

We follow the ts-rest client guide to consume the client on the frontend. In particular, we use the Fetch Client to interact with the APIs from the frontend. This means that 4/5xx status codes will not throw an error, so you must check for these codes explicitly.

Building

Run nx build contracts to build the library.

Running unit tests

Run nx test contracts to execute the unit tests via Jest.

Adding a locale

From the root of contracts run

node add-locale.js --locale <localeCode> --englishName <EnglishName> --nativeName <nativeName> --flag <flag>

for example

node add-locale.js --locale sv --englishName "Swedish" --nativeName "Svenska" --flag "🇸🇪"