From 90acab8e2deb18fc189ff81ff58a533e81df99a6 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Sat, 5 Dec 2026 07:27:06 +0800 Subject: [PATCH] refactor(json): reuse API and capture record projection (#16866) --- .../analysisView/recordedExchangeBody.ts | 9 +++------ src/domain/apiContract/parseApiContract.ts | 20 +++++++++---------- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/src/domain/analysisView/recordedExchangeBody.ts b/src/domain/analysisView/recordedExchangeBody.ts index b7b5d60e050..1067cea01f4 100644 --- a/src/domain/analysisView/recordedExchangeBody.ts +++ b/src/domain/analysisView/recordedExchangeBody.ts @@ -1,4 +1,4 @@ -import { isRecord } from "../../kernel/isRecord.js"; +import { asRecord } from "../../kernel/isRecord.js"; /** Parsed declarations borrow source strings; they do not decode bodies or normalize media types. */ export type RecordedRequestBody = | { readonly kind: "request-unavailable" } @@ -19,14 +19,11 @@ export type RecordedRequestBody = }; }; -const object = (value: unknown): Record | undefined => - isRecord(value) ? (value as Record) : undefined; - /** Read HAR request body declarations once without turning omission or null into absence. */ export const readRecordedRequestBody = ( reported: unknown, ): RecordedRequestBody => { - const request = object(object(reported)?.request); + const request = asRecord(asRecord(reported)?.request); if (request === undefined) return { kind: "request-unavailable" }; const size = request.bodySize; const bodySize: Extract< @@ -39,7 +36,7 @@ export const readRecordedRequestBody = ( ? { kind: "number", value: size } : { kind: "invalid" }; const source = request.postData; - const postData = object(source); + const postData = asRecord(source); return { kind: "request", bodySize, diff --git a/src/domain/apiContract/parseApiContract.ts b/src/domain/apiContract/parseApiContract.ts index 4828c1907cf..9585dd30e36 100644 --- a/src/domain/apiContract/parseApiContract.ts +++ b/src/domain/apiContract/parseApiContract.ts @@ -23,7 +23,7 @@ import { type ApiContractDiagnostic, type ApiContractOperation, } from "./apiContract.js"; -import { isRecord } from "../../kernel/isRecord.js"; +import { asRecord } from "../../kernel/isRecord.js"; const methods = [ "get", @@ -48,8 +48,6 @@ const pathMetadata = new Set([ ]); // HTTP methods are case-sensitive tokens; additionalOperations preserves that spelling. const httpToken = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/u; -const record = (value: unknown): Record | undefined => - isRecord(value) ? (value as Record) : undefined; const identity = (prefix: string, facts: unknown) => `${prefix}_${digestCanonicalValue(facts)}`; @@ -199,7 +197,7 @@ class ContractInventory { } operation(value: unknown, slot: OperationSlot): Result { - const object = record(value); + const object = asRecord(value); const declaredId = typeof object?.operationId === "string" ? object.operationId : null; if (!slot.seenMethods?.has(slot.method)) { @@ -254,7 +252,7 @@ const infoFacts = ( root: Record, inventory: ContractInventory, ): Result => { - const info = record(root.info); + const info = asRecord(root.info); const title = typeof info?.title === "string" ? info.title : null; const version = typeof info?.version === "string" ? info.version : null; const admitted = inventory.reserve( @@ -281,7 +279,7 @@ function* additionalOperations( inventory: ContractInventory, ): Generator> { const { pointer } = context; - const operations = record(value); + const operations = asRecord(value); if (operations === undefined) return inventory.diagnostic( "invalid-additional-operations", @@ -333,7 +331,7 @@ const pointerTarget = ( let value: unknown = root; for (const token of jsonPointerTokens(pointer)) { const container = - record(value) ?? (Array.isArray(value) ? value : undefined); + asRecord(value) ?? (Array.isArray(value) ? value : undefined); if (container === undefined || !Object.hasOwn(container, token)) return undefined; if (Array.isArray(container) && !ARRAY_INDEX_TOKEN.test(token)) @@ -378,7 +376,7 @@ function* resolvePathItem( return ok({ reason: "invalid-fragment", sources }); const target = pointerTarget(root, pointer); if (target === undefined) return ok({ reason: "missing-target", sources }); - const item = record(target); + const item = asRecord(target); if (item === undefined) return ok({ reason: "invalid-target", sources }); if (seen.has(item)) return ok({ reason: "cyclic-reference", sources }); seen.add(item); @@ -398,7 +396,7 @@ function* pathItem( }, ): Generator> { const { inventory, root, path, pointer, modern } = context; - const item = record(value); + const item = asRecord(value); if (item === undefined) return inventory.diagnostic( "invalid-path-item", @@ -598,7 +596,7 @@ export function* parseApiContractSteps( value: unknown, artifact: ApiContractArtifact, ): Generator> { - const root = record(value); + const root = asRecord(value); if (root === undefined || typeof root.openapi !== "string") return err( new AnalysisInputError(API_CONTRACT_OPERATION, undefined, [ @@ -625,7 +623,7 @@ export function* parseApiContractSteps( if (!versionAdmitted.ok) return versionAdmitted; const info = infoFacts(root, inventory); if (!info.ok) return info; - const paths = record(root.paths); + const paths = asRecord(root.paths); const pathsState = paths !== undefined ? "object"