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

@batkit/rfc9457

v1.0.0

Published

Zod schemas and TypeScript types for RFC 9457 (Problem Details for HTTP APIs)

Readme

@batkit/rfc9457

Zod schemas and TypeScript types for RFC 9457 (Problem Details for HTTP APIs).

Installation

npm install @batkit/rfc9457

Overview

This package provides ready-to-use Zod schemas, TypeScript types, and utility functions for working with RFC 9457 Problem Details in HTTP APIs. It works in both Node.js and browser environments.

Features

  • ✅ Full RFC 9457 compliance
  • ✅ Zod schemas for runtime validation
  • ✅ TypeScript types for compile-time safety
  • ✅ Support for extension members
  • ✅ Helper functions for creating and validating Problem Details
  • ✅ Works in Node.js and browsers
  • ✅ ESM and CommonJS support

Usage

Basic Example

import { createProblemDetails, PROBLEM_DETAILS_CONTENT_TYPE } from "@batkit/rfc9457";

// Create a problem details object
const problem = createProblemDetails({
  status: 404,
  title: "Resource Not Found",
  detail: "The requested user was not found",
  instance: "/users/123",
});

// Use in an HTTP response
res.status(problem.status!).set("Content-Type", PROBLEM_DETAILS_CONTENT_TYPE).json(problem);

Validation

import { validateProblemDetails, isProblemDetails } from "@batkit/rfc9457";

// Validate an object
const result = validateProblemDetails(data);
if (result.success) {
  console.log("Valid problem details:", result.data);
} else {
  console.error("Validation errors:", result.error);
}

// Type guard
if (isProblemDetails(response)) {
  console.log(`Error status: ${response.status}`);
}

Extended Problem Details

RFC 9457 allows adding custom fields to problem details:

import { createExtendedProblemDetails } from "@batkit/rfc9457";

interface ValidationProblem {
  validationErrors: Array<{ field: string; message: string }>;
}

const problem = createExtendedProblemDetails<ValidationProblem>({
  type: "https://api.example.com/errors/validation",
  status: 400,
  title: "Validation Failed",
  detail: "The request body contains invalid data",
  validationErrors: [
    { field: "email", message: "Must be a valid email address" },
    { field: "age", message: "Must be a positive integer" },
  ],
});

Using Schemas Directly

import { ProblemDetailsSchema, ExtendedProblemDetailsSchema } from "@batkit/rfc9457";

// Parse and validate
const problem = ProblemDetailsSchema.parse({
  status: 500,
  title: "Internal Server Error",
});

// With custom fields
const extendedProblem = ExtendedProblemDetailsSchema.parse({
  status: 503,
  title: "Service Unavailable",
  retryAfter: 60,
});

Creating Headers

import { createProblemDetailsHeaders } from "@batkit/rfc9457";

// Basic headers
const headers = createProblemDetailsHeaders();
// { 'Content-Type': 'application/problem+json' }

// With additional headers
const headersWithCors = createProblemDetailsHeaders({
  "Access-Control-Allow-Origin": "*",
  "Cache-Control": "no-cache",
});

API Reference

Types

ProblemDetails

TypeScript type for RFC 9457 Problem Details:

interface ProblemDetails {
  type?: string; // Default: 'about:blank'
  title?: string;
  status?: number;
  detail?: string;
  instance?: string;
}

ExtendedProblemDetails

Type for Problem Details with additional custom fields:

type ExtendedProblemDetails = ProblemDetails & Record<string, unknown>;

Schemas

ProblemDetailsSchema

Zod schema for standard RFC 9457 Problem Details. Does not allow additional properties.

ExtendedProblemDetailsSchema

Zod schema that allows additional properties for custom extensions.

Functions

validateProblemDetails(data: unknown)

Validates a problem details object against the RFC 9457 schema.

Returns: SafeParseReturnType from Zod

validateExtendedProblemDetails(data: unknown)

Validates an extended problem details object (allows additional properties).

Returns: SafeParseReturnType from Zod

createProblemDetails(details)

Creates a Problem Details object with proper defaults.

Parameters:

  • details: Partial problem details (requires status)

Returns: ProblemDetails

createExtendedProblemDetails<T>(details)

Creates an extended Problem Details object with additional typed properties.

Type Parameters:

  • T: Additional properties type

Parameters:

  • details: Problem details with additional properties (requires status)

Returns: ExtendedProblemDetails & T

createProblemDetailsHeaders(additionalHeaders?)

Helper to create HTTP response headers for Problem Details.

Parameters:

  • additionalHeaders: Optional additional headers

Returns: Headers object with Content-Type set to application/problem+json

isProblemDetails(value: unknown)

Type guard to check if an object is a valid Problem Details object.

Returns: boolean

Constants

PROBLEM_DETAILS_CONTENT_TYPE

The RFC 9457 content type: 'application/problem+json'

RFC 9457 Compliance

This package implements the full RFC 9457 specification:

  • type: URI reference identifying the problem type (default: about:blank)
  • title: Short, human-readable summary
  • status: HTTP status code (validated to be 100-599)
  • detail: Human-readable explanation
  • instance: URI reference identifying this specific occurrence
  • Extension members: Additional properties allowed in extended schema

Browser Compatibility

This package works in all modern browsers and Node.js environments. It has no Node.js-specific dependencies.

Related Packages

License

MIT © Ken Courville