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

@open-apiary/schema

v0.0.8

Published

The authoring API: the endpoint builder, the exception, the operation id, and the types of a manifest and a bundle. The package has no runtime of its own. The public [`open-apiary`](../open-apiary) package re-exports it.

Readme

@open-apiary/schema

The authoring API: the endpoint builder, the exception, the operation id, and the types of a manifest and a bundle. The package has no runtime of its own. The public open-apiary package re-exports it.

Install

pnpm add open-apiary zod

Endpoints

apiary has one function for each method: GET, POST, PUT, PATCH, and DELETE. Each takes the route, the validators of the input, and the validator of the output. The route is an OpenAPI path. A path validator has one member for each parameter of the route. GET and DELETE take no body and no form.

import { apiary, ConflictException } from "open-apiary";
import { z } from "zod";

export default apiary
  .POST("/users", { body: NewUser }, User)
  .handle(async (_ctx, input) => users.create(input.body))
  .withStatus(201)
  .withExceptions(ConflictException)
  .withScopes("users:write")
  .withTags("users")
  .withSpec({ summary: "Create a user" });

Forms

An endpoint that takes a form names a form validator in place of body: an object with one member for each field. An endpoint has one of the two, and not both.

export default apiary
  .POST(
    "/avatars",
    {
      form: z.object({
        name: z.string(),
        size: z.coerce.number().optional(),
        tags: z.array(z.string()).default([]),
        image: z.file().mime(["image/png", "image/jpeg"]),
      }),
    },
    Avatar,
  )
  .handle((ctx, input) =>
    ctx.resolve(AvatarsContract).store(input.form.name, input.form.image),
  );
  • The request is a multipart/form-data body, or an application/x-www-form-urlencoded one. The OpenAPI document lists both, and multipart/form-data alone when a field is a file.

  • A field arrives as text, so a number needs z.coerce, as in a query.

  • A file is a z.file(), and the handler reads a File.

  • A field that repeats is a z.array(). One value is a list of one.

  • A request with any other body, such as JSON, is a 415: the endpoint may answer with UnsupportedMediaTypeException, with no withExceptions.

  • handle sets the handler. It receives the context and the parsed input. The context has the caller, the endpoint, and resolve, which returns the provider of a contract. apiary.contract<T>(key) makes a contract, and a class under providers/ fills it. The handler gets everything else by import. It returns a value that the output validator accepts. The types follow the validators.

  • withAuth requires a caller, with no scope. The core answers with a 401 for a request with none, and the kit lists the scheme under security.

  • withScopes sets the scopes a caller needs, and so requires a caller. The core checks them, and answers with a 403 for a caller without them. The kit lists them under security.

  • A handler that handle takes after withAuth or withScopes has a ctx.caller that is always there. In a handler before them, and in one of an endpoint that requires no caller, it may be undefined.

  • An API adds the members of a caller to the Caller interface once, with declare module "open-apiary" { interface Caller { … } }, and every handler reads them typed.

  • An API declares its scopes once, with declare module "open-apiary" { interface Vocabulary { scope: … } }, and withScopes takes no other. Without the declaration it takes any string.

  • withStatus sets the success status. The default is 200, and 201 for POST.

  • withExceptions declares the exceptions that the endpoint may answer with: the classes of them. The kit lists each one under responses, and the core answers with a 500 for an error that is not among them.

  • withTags and withSpec extend the OpenAPI operation. withSpec merges over what the kit infers. operationId replaces the derived id.

Each method returns a new endpoint. The definition is under the DEFINITION symbol. isEndpoint tests for it.

Exceptions

apiary.exception(code, status, options) makes the class of an exception. A handler, or a provider under it, throws one, and the response is a problem details object, as RFC 9457 defines it.

export class UserNotFound extends apiary.exception("user_not_found", 404, {
  extensions: z.object({ id: z.uuid() }),
}) {}

throw new UserNotFound({ id }, "no user has this id");

| Member | In the problem | | ------------ | -------------------------------------------------------------------------------- | | code | code. It names the exception, and is unique in an API. | | status | status, and the status of the response. From 400 to 599. | | title | title. The default is the reason phrase of the status. | | type | type. The default is about:blank. Set it when a URI documents the exception. | | extensions | One member for each member of the validator. None can replace a member above. |

The constructor of a class with no extensions takes the detail of the throw. One with extensions takes them first, then the detail. The detail is the detail of the problem, and is absent when the throw gives none.

The package has a class for each common status, with no extensions: BadRequestException, UnauthorizedException, PaymentRequiredException, NotFoundException, MethodNotAllowedException, NotAcceptableException, RequestTimeoutException, ConflictException, GoneException, PreconditionFailedException, ContentTooLargeException, UnsupportedMediaTypeException, RangeNotSatisfiableException, UnprocessableContentException, LockedException, FailedDependencyException, PreconditionRequiredException, TooManyRequestsException, UnavailableForLegalReasonsException, NotImplementedException, BadGatewayException, ServiceUnavailableException, GatewayTimeoutException, and InsufficientStorageException.

A class declares the headers of its response with headers, a validator of each by its name in lower case. A throw gives them after the extensions and before the detail, and the OpenAPI document lists them under the response of every endpoint that declares the class.

export class QuotaExceeded extends apiary.exception("quota_exceeded", 429, {
  headers: z.object({ "retry-after": z.string() }),
}) {}

throw new QuotaExceeded({ "retry-after": "3600" }, "the quota is spent");

Five classes of the package declare one:

| Class | Header | Required | | ------------------------------ | ------------------ | -------- | | UnauthorizedException | www-authenticate | No | | MethodNotAllowedException | allow | Yes | | RangeNotSatisfiableException | content-range | Yes | | TooManyRequestsException | retry-after | Yes | | ServiceUnavailableException | retry-after | No |

The core answers with four of its own:

| Class | Status | Answered | | ----------------------- | ------ | ------------------------------------------------------------------------ | | InvalidInputException | 400 | When a part of the input fails its validator. errors lists the issues. | | UnauthorizedException | 401 | When the endpoint requires a caller and the request has none. | | ForbiddenException | 403 | When the caller lacks a scope of the endpoint. scopes lists them. | | InternalException | 500 | For every error that the endpoint does not declare. |

With no withExceptions, an endpoint with a validator may answer with the first, one that requires a caller with the second, and one with scopes with the third. exceptionsOf(definition) lists what an endpoint may answer with, isApiaryException tells an exception of an API from any other, and problemOf makes the problem of one.

Providers

Provider is the optional base class of a provider. It holds the context of its constructor as this.ctx, and reads the log of the API from it as this.log: the provider under LogContract, whose interface is Log. The core provides a class under it: see @open-apiary/core.

Operation ids

identify(method, route) derives the operation id of an endpoint: GET /users/{id} is getUsersById. operationId(definition) returns the operationId of withSpec when the endpoint sets one, else the derived id. The kit names the route module of an endpoint with it, and the core reports it as ctx.endpoint.id.

Types

  • Manifest is what apiary build writes as manifest.json: the data of each endpoint, with no validator and no handler.
  • Bundle is the default export of a built package: each endpoint by operation id, as its manifest entry and a load that imports its route module.
  • RouteModule is a route module. Its default export is the endpoint.