import type { JsonPath } from './types.js'; export type ContractOutcome = 'success' | 'partial' | 'failure'; export type ValuePredicate = | { operator: 'equals'; value: unknown } | { operator: 'one_of'; values: readonly unknown[] } | { operator: 'exists'; value?: boolean } | { operator: 'truthy' } | { operator: 'falsy' }; export interface BodyCondition { path: JsonPath; predicate: ValuePredicate; } export interface OutcomeRule { outcome: ContractOutcome; when: BodyCondition | readonly BodyCondition[]; match?: 'all' | 'any'; guidance?: string; } export interface ResponseContract { id: string; /** Rules are evaluated in declaration order. */ rules?: readonly OutcomeRule[]; /** Outcome for a 2xx object body when no rule matches. */ default2xxOutcome: 'success' | 'protocol_failure'; /** Required only after a response has otherwise classified as success. */ requiredSuccessPaths?: readonly JsonPath[]; } export function booleanSuccessContract(options: { id: string; path?: JsonPath; allowMissingAsSuccess?: boolean; }): ResponseContract { const path = options.path ?? ['success']; return { id: options.id, rules: [ { outcome: 'failure', when: { path, predicate: { operator: 'equals', value: false } }, }, { outcome: 'success', when: { path, predicate: { operator: 'equals', value: true } }, }, ], default2xxOutcome: options.allowMissingAsSuccess ? 'success' : 'protocol_failure', }; } export function jsonObjectSuccessContract(id: string): ResponseContract { return { id, default2xxOutcome: 'success' }; }