Skip to main content

middleware-openapi/src

Type Aliases

Asset

Asset = object
Defined in: packages/middleware-openapi/src/types.ts:5

Properties

chain
chain: string
Defined in: packages/middleware-openapi/src/types.ts:6
decimals
decimals: number
Defined in: packages/middleware-openapi/src/types.ts:8
recipient
recipient: string
Defined in: packages/middleware-openapi/src/types.ts:9
token
token: string
Defined in: packages/middleware-openapi/src/types.ts:7

AuthorizeResponse

AuthorizeResponse = { protocol: "x402v1"; verification: x402VerifyResponseV1; } | { protocol: "x402v2"; verification: x402VerifyResponseV2; } | { protocol: "mpp"; verification: mppReceipt; }
Defined in: packages/middleware-openapi/src/handler.ts:74

CaptureError

CaptureError = object
Defined in: packages/middleware-openapi/src/handler.ts:135

Properties

message?
optional message: string
Defined in: packages/middleware-openapi/src/handler.ts:137
status
status: number
Defined in: packages/middleware-openapi/src/handler.ts:136

CapturePhase

CapturePhase = "request" | "response"
Defined in: packages/middleware-openapi/src/handler.ts:133

CaptureRequestInfo

CaptureRequestInfo = object
Defined in: packages/middleware-openapi/src/handler.ts:148

Properties

headers
headers: Record<string, string>
Defined in: packages/middleware-openapi/src/handler.ts:151
method
method: string
Defined in: packages/middleware-openapi/src/handler.ts:149
path
path: string
Defined in: packages/middleware-openapi/src/handler.ts:150

CaptureResponse

CaptureResponse = object
Defined in: packages/middleware-openapi/src/handler.ts:154

Properties

amount
amount: Record<string, string>
Defined in: packages/middleware-openapi/src/handler.ts:157
error?
optional error: CaptureError
Defined in: packages/middleware-openapi/src/handler.ts:164
payment?
optional payment: SettledPayment
Defined in: packages/middleware-openapi/src/handler.ts:169
phase
phase: CapturePhase
Defined in: packages/middleware-openapi/src/handler.ts:155
request
request: CaptureRequestInfo
Defined in: packages/middleware-openapi/src/handler.ts:161
settled
settled: boolean
Defined in: packages/middleware-openapi/src/handler.ts:156
trace?
optional trace: EvalTrace
Defined in: packages/middleware-openapi/src/handler.ts:165

EvalContext

EvalContext = object
Defined in: packages/middleware-openapi/src/types.ts:42

Properties

request
request: object
Defined in: packages/middleware-openapi/src/types.ts:43
body
body: Record<string, unknown>
headers
headers: Record<string, string>
path
path: string
query
query: Record<string, string>
response?
optional response: object
Defined in: packages/middleware-openapi/src/types.ts:49
body
body: Record<string, unknown>
headers
headers: Record<string, string>
status
status: number

EvalError

EvalError = object
Defined in: packages/middleware-openapi/src/evaluator.ts:32

Properties

error
error: unknown
Defined in: packages/middleware-openapi/src/evaluator.ts:35
phase
phase: "authorize" | "capture"
Defined in: packages/middleware-openapi/src/evaluator.ts:33
rule
rule: PricingRule
Defined in: packages/middleware-openapi/src/evaluator.ts:34

EvalErrorHandler()

EvalErrorHandler = (err) => void
Defined in: packages/middleware-openapi/src/evaluator.ts:38

Parameters

err
EvalError

Returns

void

EvalTrace

EvalTrace = object
Defined in: packages/middleware-openapi/src/types.ts:61

Properties

authorize?
optional authorize: PhaseTrace
Defined in: packages/middleware-openapi/src/types.ts:64
capture
capture: PhaseTrace
Defined in: packages/middleware-openapi/src/types.ts:65
rule
rule: PricingRule
Defined in: packages/middleware-openapi/src/types.ts:63
ruleIndex
ruleIndex: number
Defined in: packages/middleware-openapi/src/types.ts:62

FaremeterSpec

FaremeterSpec = object
Defined in: packages/middleware-openapi/src/types.ts:37

Properties

assets
assets: Record<string, Asset>
Defined in: packages/middleware-openapi/src/types.ts:38
operations
operations: Record<string, OperationPricing>
Defined in: packages/middleware-openapi/src/types.ts:39

GatewayHandler

GatewayHandler = object
Defined in: packages/middleware-openapi/src/handler.ts:172

Methods

handleRequest()
handleRequest(ctx): Promise<GatewayRequestResult>
Defined in: packages/middleware-openapi/src/handler.ts:173
Parameters
ctx
body
null | Record<string, unknown> = “Record<string, unknown> | null”
headers
Record<string, string | string[]> = HEADER_MAP
method
string = "string"
operationKey
string = "string"
path
string = "string"
query
Record<string, string | string[]> = HEADER_MAP
Returns
Promise<GatewayRequestResult>
handleResponse()
handleResponse(ctx): Promise<GatewayResponseResult>
Defined in: packages/middleware-openapi/src/handler.ts:174
Parameters
ctx
body
null | Record<string, unknown> = “Record<string, unknown> | null”
headers
Record<string, string | string[]> = HEADER_MAP
method
string = "string"
operationKey
string = "string"
path
string = "string"
query
Record<string, string | string[]> = HEADER_MAP
response
{ body: Record<string, unknown>; headers: Record<string, string | string[]>; status: number; } = ...
response.body
Record<string, unknown> = "Record<string, unknown>"
response.headers
Record<string, string | string[]> = HEADER_MAP
response.status
number = "number"
Returns
Promise<GatewayResponseResult>

GatewayHandlerConfig

GatewayHandlerConfig = object
Defined in: packages/middleware-openapi/src/handler.ts:23

Properties

baseURL
baseURL: string
Defined in: packages/middleware-openapi/src/handler.ts:25
mppMethodHandlers?
optional mppMethodHandlers: MPPMethodHandler[]
Defined in: packages/middleware-openapi/src/handler.ts:27
onAuthorize()?
optional onAuthorize: (operationKey, result) => void | Promise<void>
Defined in: packages/middleware-openapi/src/handler.ts:68 Called when a two-phase rule’s payment is successfully verified at /request time. Does not fire for one-phase (capture-only) rules, which settle immediately and report through onCapture instead. Does not fire when verification fails (the request gets a 402). The hook is awaited — a slow async hook delays the caller. A throw or rejected promise is caught and logged without affecting the gateway response, which is already determined at this point.
Parameters
operationKey
string
result
AuthorizeResponse
Returns
void | Promise<void>
onCapture()?
optional onCapture: (operationKey, result) => void | Promise<void>
Defined in: packages/middleware-openapi/src/handler.ts:54 Called post-settlement when a pricing rule matched and produced a non-empty capture amount. result.phase indicates whether settlement happened at /request (one-phase) or /response (two-phase). For two-phase rules the hook fires at /response when settlement is attempted, regardless of whether it succeeded or failed (result.settled and result.error distinguish the outcome). For one-phase rules the hook fires at /request only on successful settlement — if the facilitator rejects the payment, the request gets a 402 and the hook is not invoked. The hook does NOT fire when the capture expression evaluates to zero across all assets. A zero-amount capture produces no settlement and no hook invocation. The hook is awaited — a slow async hook delays the caller. The return value is computed before the hook is invoked, so a throw or rejected promise is caught and logged without affecting it. Requires payment handlers (x402Handlers or mppMethodHandlers) to be configured. Without them no settlement occurs and this hook is never invoked.
Parameters
operationKey
string
result
CaptureResponse
Returns
void | Promise<void>
spec
spec: FaremeterSpec
Defined in: packages/middleware-openapi/src/handler.ts:24
supportedVersions?
optional supportedVersions: SupportedVersionsConfig
Defined in: packages/middleware-openapi/src/handler.ts:28
x402Handlers?
optional x402Handlers: FacilitatorHandler[]
Defined in: packages/middleware-openapi/src/handler.ts:26

GatewayRequestResult

GatewayRequestResult = object
Defined in: packages/middleware-openapi/src/handler.ts:123

Properties

body?
optional body: unknown
Defined in: packages/middleware-openapi/src/handler.ts:126
headers?
optional headers: Record<string, string>
Defined in: packages/middleware-openapi/src/handler.ts:125
status
status: number
Defined in: packages/middleware-openapi/src/handler.ts:124

GatewayResponseResult

GatewayResponseResult = object
Defined in: packages/middleware-openapi/src/handler.ts:129

Properties

status
status: number
Defined in: packages/middleware-openapi/src/handler.ts:130

OperationPricing

OperationPricing = object
Defined in: packages/middleware-openapi/src/types.ts:28

Properties

method
method: string
Defined in: packages/middleware-openapi/src/types.ts:29
path
path: string
Defined in: packages/middleware-openapi/src/types.ts:30
policy?
optional policy: PaymentPolicy
Defined in: packages/middleware-openapi/src/types.ts:34
rates?
optional rates: Rates
Defined in: packages/middleware-openapi/src/types.ts:32
rules?
optional rules: PricingRule[]
Defined in: packages/middleware-openapi/src/types.ts:33
transport
transport: TransportType
Defined in: packages/middleware-openapi/src/types.ts:31

PhaseTrace

PhaseTrace = object
Defined in: packages/middleware-openapi/src/types.ts:56

Properties

bindings
bindings: Record<string, unknown>
Defined in: packages/middleware-openapi/src/types.ts:57
coefficient
coefficient: number
Defined in: packages/middleware-openapi/src/types.ts:58

PriceResult

PriceResult = object
Defined in: packages/middleware-openapi/src/types.ts:68

Properties

amount
amount: Record<string, bigint>
Defined in: packages/middleware-openapi/src/types.ts:70
hasAuthorize?
optional hasAuthorize: boolean
Defined in: packages/middleware-openapi/src/types.ts:75
matched
matched: boolean
Defined in: packages/middleware-openapi/src/types.ts:69
rule?
optional rule: PricingRule
Defined in: packages/middleware-openapi/src/types.ts:77
ruleIndex?
optional ruleIndex: number
Defined in: packages/middleware-openapi/src/types.ts:76
trace?
optional trace: PhaseTrace
Defined in: packages/middleware-openapi/src/types.ts:78

PricingEvaluator

PricingEvaluator = object
Defined in: packages/middleware-openapi/src/evaluator.ts:375

Methods

authorize()
authorize(operationKey, ctx): PriceResult
Defined in: packages/middleware-openapi/src/evaluator.ts:376
Parameters
operationKey
string
ctx
EvalContext
Returns
PriceResult
capture()
capture(operationKey, ctx): PriceResult
Defined in: packages/middleware-openapi/src/evaluator.ts:377
Parameters
operationKey
string
ctx
EvalContext
Returns
PriceResult
getAssets()
getAssets(): Record<string, Asset>
Defined in: packages/middleware-openapi/src/evaluator.ts:378
Returns
Record<string, Asset>

PricingRule

PricingRule = object
Defined in: packages/middleware-openapi/src/types.ts:20

Properties

authorize?
optional authorize: string
Defined in: packages/middleware-openapi/src/types.ts:22
capture
capture: string
Defined in: packages/middleware-openapi/src/types.ts:23
match
match: string
Defined in: packages/middleware-openapi/src/types.ts:21

Rates

Rates = Record<string, bigint>
Defined in: packages/middleware-openapi/src/types.ts:18 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.

RequestContext

RequestContext = typeof requestContext.infer
Defined in: packages/middleware-openapi/src/handler.ts:105

ResponseContext

ResponseContext = typeof responseContext.infer
Defined in: packages/middleware-openapi/src/handler.ts:121

SettledPayment

SettledPayment = { protocol: "x402v1"; settlement: x402SettleResponseV1; } | { protocol: "x402v2"; settlement: x402SettleResponseV2; } | { protocol: "mpp"; settlement: mppReceipt; }
Defined in: packages/middleware-openapi/src/handler.ts:79

TransportType

TransportType = "json" | "sse" | "websocket"
Defined in: packages/middleware-openapi/src/types.ts:26

Variables

requestContext

const requestContext: Type<{ body: null | Record<string, unknown>; headers: Record<string, string | string[]>; method: string; operationKey: string; path: string; query: Record<string, string | string[]>; }, { }>
Defined in: packages/middleware-openapi/src/handler.ts:96

responseContext

const responseContext: Type<{ body: null | Record<string, unknown>; headers: Record<string, string | string[]>; method: string; operationKey: string; path: string; query: Record<string, string | string[]>; response: { body: Record<string, unknown>; headers: Record<string, string | string[]>; status: number; }; }, { }>
Defined in: packages/middleware-openapi/src/handler.ts:107

Functions

buildContext()

buildContext(opts): EvalContext
Defined in: packages/middleware-openapi/src/context.ts:6 Build an evaluation context from HTTP request data.

Parameters

opts
body
Record<string, unknown>
headers
Record<string, string>
path
string
query?
Record<string, string>

Returns

EvalContext

createGatewayHandler()

createGatewayHandler(config): GatewayHandler
Defined in: packages/middleware-openapi/src/handler.ts:450

Parameters

config
GatewayHandlerConfig

Returns

GatewayHandler

createPricingEvaluator()

createPricingEvaluator(spec, opts?): PricingEvaluator
Defined in: packages/middleware-openapi/src/evaluator.ts:387 Evaluates pricing rules from an OpenAPI spec against request/response context.

Parameters

spec
FaremeterSpec Parsed faremeter spec with assets, operations, and rates
opts?
Optional configuration including error handler
onError?
EvalErrorHandler

Returns

PricingEvaluator

extractSpec()

extractSpec(doc): FaremeterSpec
Defined in: packages/middleware-openapi/src/parser.ts:285 Extract x-faremeter pricing extensions from a dereferenced OpenAPI document.

Parameters

doc
Record<string, unknown> Dereferenced OpenAPI document as a plain object

Returns

FaremeterSpec

loadSpec()

loadSpec(filePath): Promise<FaremeterSpec>
Defined in: packages/middleware-openapi/src/parser.ts:266 Load and parse an OpenAPI spec file, extracting x-faremeter pricing extensions.

Parameters

filePath
string Path to the OpenAPI YAML or JSON file

Returns

Promise<FaremeterSpec>

withResponse()

withResponse(ctx, response): EvalContext
Defined in: packages/middleware-openapi/src/context.ts:25 Augment an evaluation context with HTTP response data for capture phase.

Parameters

ctx
EvalContext
response
body
Record<string, unknown>
headers
Record<string, string>
status
number

Returns

EvalContext