import type { JsonPath } from './types.js'; import type { BodyCondition, OutcomeRule, ResponseContract, ValuePredicate, } from './response-contract.js'; import { isHttpSuccess, type TransportResponse, } from './transport-response.js'; export type ResponseClassificationKind = | 'success' | 'partial' | 'backend_failure' | 'protocol_failure'; export interface ClassifiedResponse { kind: ResponseClassificationKind; response: TransportResponse; /** Present only when the upstream JSON root can safely receive a sibling _ccl field. */ upstreamRoot?: Record; reason?: string; guidance?: string; } function isObjectRoot(value: unknown): value is Record { return !!value && typeof value === 'object' && !Array.isArray(value); } function valueAtPath(root: unknown, path: JsonPath): { exists: boolean; value: unknown } { let current = root; for (const part of path) { if (typeof part === 'number') { if (!Array.isArray(current) || part < 0 || part >= current.length) { return { exists: false, value: undefined }; } current = current[part]; continue; } if (!current || typeof current !== 'object' || !Object.prototype.hasOwnProperty.call(current, part)) { return { exists: false, value: undefined }; } current = (current as Record)[part]; } return { exists: true, value: current }; } function matchesPredicate(actual: { exists: boolean; value: unknown }, predicate: ValuePredicate): boolean { switch (predicate.operator) { case 'equals': return actual.exists && Object.is(actual.value, predicate.value); case 'one_of': return actual.exists && predicate.values.some((value) => Object.is(actual.value, value)); case 'exists': return actual.exists === (predicate.value ?? true); case 'truthy': return actual.exists && !!actual.value; case 'falsy': return actual.exists && !actual.value; } } function matchesCondition(root: Record, condition: BodyCondition): boolean { return matchesPredicate(valueAtPath(root, condition.path), condition.predicate); } function matchesRule(root: Record, rule: OutcomeRule): boolean { const conditions = Array.isArray(rule.when) ? rule.when : [rule.when]; return rule.match === 'any' ? conditions.some((condition) => matchesCondition(root, condition)) : conditions.every((condition) => matchesCondition(root, condition)); } function missingRequiredPath(root: Record, paths: readonly JsonPath[] | undefined): JsonPath | undefined { return paths?.find((path) => !valueAtPath(root, path).exists); } function printablePath(path: JsonPath): string { return path.map((part, index) => ( typeof part === 'number' ? `[${part}]` : `${index === 0 ? '' : '.'}${part}` )).join(''); } export class ResponseClassifier { classify(response: TransportResponse, contract: ResponseContract): ClassifiedResponse { if (response.body.kind !== 'json') { return { kind: 'protocol_failure', response, reason: response.body.kind === 'empty' ? 'empty_response_body' : 'non_json_response_body', }; } if (!isObjectRoot(response.body.value)) { return { kind: 'protocol_failure', response, reason: 'json_root_must_be_object', }; } const root = response.body.value; if (Object.prototype.hasOwnProperty.call(root, '_ccl')) { return { kind: 'protocol_failure', response, reason: 'reserved_field_conflict:_ccl', }; } if (!isHttpSuccess(response)) { return { kind: 'backend_failure', response, upstreamRoot: root }; } const matched = contract.rules?.find((rule) => matchesRule(root, rule)); if (matched) { if (matched.outcome === 'failure') { return { kind: 'backend_failure', response, upstreamRoot: root, guidance: matched.guidance, }; } if (matched.outcome === 'partial') { return { kind: 'partial', response, upstreamRoot: root, guidance: matched.guidance, }; } const missing = missingRequiredPath(root, contract.requiredSuccessPaths); if (missing) { return { kind: 'protocol_failure', response, upstreamRoot: root, reason: `missing_required_success_field:${printablePath(missing)}`, }; } return { kind: 'success', response, upstreamRoot: root, guidance: matched.guidance }; } if (contract.default2xxOutcome === 'success') { const missing = missingRequiredPath(root, contract.requiredSuccessPaths); if (missing) { return { kind: 'protocol_failure', response, upstreamRoot: root, reason: `missing_required_success_field:${printablePath(missing)}`, }; } return { kind: 'success', response, upstreamRoot: root }; } return { kind: 'protocol_failure', response, upstreamRoot: root, reason: `unclassified_response:${contract.id}`, }; } }