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

@faremeter/middleware-openapi

v0.20.1

Published

OpenAPI pricing engine for faremeter middleware

Readme

@faremeter/middleware-openapi

OpenAPI-driven pricing engine for evaluating payment amounts from x-faremeter-* spec extensions. Parses pricing rules from an OpenAPI spec, evaluates them against request and response data, and delegates payment to the configured facilitator. Payment-protocol agnostic -- works with any scheme that implements the facilitator interface.

See OPENAPI-SPEC.md for the full specification of the x-faremeter-pricing extension.

Installation

pnpm install @faremeter/middleware-openapi

Features

  • Declarative pricing - Rates, rules, and expressions defined in the OpenAPI spec
  • JSONPath matching - Route requests to pricing rules via RFC 9535 with filter and selector support
  • Authorize + capture - Hold an estimated amount before the upstream runs, settle the actual cost after
  • Capture-only - Settle a fixed price before the upstream runs when no hold is needed
  • Custom functions - jsonSize() for payload estimation, coalesce() for null-safe defaults
  • Rate cascading - Document, path, and operation-level rate overrides with nearest-wins semantics
  • Construction-time validation - Invalid expressions, bad JSONPath refs, and forbidden $.response.* references are caught at startup

API Reference

Functions

buildContext

Build an evaluation context from HTTP request data.

| Function | Type | | -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- | | buildContext | (opts: { body: Record<string, unknown>; headers: Record<string, string>; query?: Record<string, string> or undefined; path: string; }) => EvalContext |

withResponse

Augment an evaluation context with HTTP response data for capture phase.

| Function | Type | | -------------- | ---------------------------------------------------------------------------------------------------------------------------------- | | withResponse | (ctx: EvalContext, response: { body: Record<string, unknown>; headers: Record<string, string>; status: number; }) => EvalContext |

createPricingEvaluator

Evaluates pricing rules from an OpenAPI spec against request/response context.

| Function | Type | | ------------------------ | ------------------------------------------------------------------------------------------------------------- | | createPricingEvaluator | (spec: FaremeterSpec, opts?: { onError?: EvalErrorHandler or undefined; } or undefined) => PricingEvaluator |

Parameters:

  • spec: - Parsed faremeter spec with assets, operations, and rates
  • opts: - Optional configuration including error handler

createGatewayHandler

| Function | Type | | ---------------------- | -------------------------------------------------- | | createGatewayHandler | (config: GatewayHandlerConfig) => GatewayHandler |

loadSpec

Load and parse an OpenAPI spec file, extracting x-faremeter pricing extensions.

| Function | Type | | ---------- | ---------------------------------------------- | | loadSpec | (filePath: string) => Promise<FaremeterSpec> |

Parameters:

  • filePath: - Path to the OpenAPI YAML or JSON file

extractSpec

Extract x-faremeter pricing extensions from a dereferenced OpenAPI document.

| Function | Type | | ------------- | ------------------------------------------------- | | extractSpec | (doc: Record<string, unknown>) => FaremeterSpec |

Parameters:

  • doc: - Dereferenced OpenAPI document as a plain object

Constants

requestContext

| Constant | Type | | ---------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | requestContext | Type<{ operationKey: string; method: string; path: string; headers: Record<string, string or string[]>; query: Record<string, string or string[]>; body: Record<string, unknown> or null; }, {}> |

responseContext

| Constant | Type | | ----------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | responseContext | Type<{ operationKey: string; method: string; path: string; headers: Record<string, string or string[]>; query: Record<string, string or string[]>; body: Record<string, unknown> or null; response: { ...; }; }, {}> |

Types

Asset

| Type | Type | | ------- | ------------------------------------------------------------------------ | | Asset | { chain: string; token: string; decimals: number; recipient: string; } |

Rates

Per-asset pricing rates. Each value is the number of atomic asset units charged per 1.0 of the rule's evaluated coefficient. Modelled as bigint because atomic units flow directly to on-chain settlement and must not lose precision to IEEE-754 rounding.

| Type | Type | | ------- | ------------------------ | | Rates | Record<string, bigint> |

PricingRule

| Type | Type | | ------------- | --------------------------------------------------------- | | PricingRule | { match: string; authorize?: string; capture: string; } |

TransportType

| Type | Type | | --------------- | ------------------------------ | | TransportType | json" or "sse" or "websocket |

OperationPricing

| Type | Type | | ------------------ | ---------------------------------------------------------------------------------------------------------------- | | OperationPricing | { method: string; path: string; transport: TransportType; rates?: Rates; rules?: PricingRule[] or undefined; } |

FaremeterSpec

| Type | Type | | --------------- | ---------------------------------------------------------------------------------- | | FaremeterSpec | { assets: Record<string, Asset>; operations: Record<string, OperationPricing>; } |

EvalContext

| Type | Type | | ------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | EvalContext | { request: { body: Record<string, unknown>; headers: Record<string, string>; query: Record<string, string>; path: string; }; response?: { body: Record<string, unknown>; headers: Record<string, string>; status: number; }; } |

PhaseTrace

| Type | Type | | ------------ | ------------------------------------------------------------- | | PhaseTrace | { bindings: Record<string, unknown>; coefficient: number; } |

EvalTrace

| Type | Type | | ----------- | ---------------------------------------------------------------------------------------- | | EvalTrace | { ruleIndex: number; rule: PricingRule; authorize?: PhaseTrace; capture: PhaseTrace; } |

PriceResult

| Type | Type | | ------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | PriceResult | { matched: boolean; amount: Record<string, bigint>; // True when the matched rule has an explicit authorize// expression. When false, the authorize result was derived from // thecapture expression and the handler settles at /request // instead of deferring to /response. hasAuthorize?: boolean; ruleIndex?: number; rule?: PricingRule; trace?: PhaseTrace; } |

EvalError

| Type | Type | | ----------- | ------------------------------------------------------------------------- | | EvalError | { phase: "authorize" or "capture"; rule: PricingRule; error: unknown; } |

EvalErrorHandler

| Type | Type | | ------------------ | -------------------------- | | EvalErrorHandler | (err: EvalError) => void |

PricingEvaluator

| Type | Type | | ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | PricingEvaluator | { authorize(operationKey: string, ctx: EvalContext): PriceResult; capture(operationKey: string, ctx: EvalContext): PriceResult; getAssets(): FaremeterSpec["assets"]; } |

GatewayHandlerConfig

| Type | Type | | ---------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | GatewayHandlerConfig | { spec: FaremeterSpec; baseURL: string; x402Handlers?: FacilitatorHandler[]; mppMethodHandlers?: MPPMethodHandler[]; supportedVersions?: SupportedVersionsConfig; } |

RequestContext

| Type | Type | | ---------------- | ----------------------------- | | RequestContext | typeof requestContext.infer |

ResponseContext

| Type | Type | | ----------------- | ------------------------------ | | ResponseContext | typeof responseContext.infer |

GatewayResponse

| Type | Type | | ----------------- | ----------------------------------------------------------------------- | | GatewayResponse | { status: number; headers?: Record<string, string>; body?: unknown; } |

CaptureResponse

| Type | Type | | ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | CaptureResponse | { captured: boolean; settled: boolean; amount: Record<string, string>; // When settlement is attempted and fails, the facilitator's // machine-readable error payload is propagated here. Absent for // successful settlements and for one-phase rules where authorize // and capture produce the same amount. error?: unknown; trace?: EvalTrace; } |

GatewayHandler

| Type | Type | | ---------------- | ----------------------------------------------------------------------------------------------------------------------------------- | | GatewayHandler | { handleRequest(ctx: RequestContext): Promise<GatewayResponse>; handleResponse(ctx: ResponseContext): Promise<CaptureResponse>; } |