mirror of
https://github.com/morluto/rea.git
synced 2026-12-06 14:59:05 +00:00
refactor(json): reuse API and capture record projection (#16866)
This commit is contained in:
1 parent
0af8817643
commit
90acab8e2d
2 files changed
+12
-17
No files matched your search
@@ -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<string, unknown> | undefined =>
|
||||
isRecord(value) ? (value as Record<string, unknown>) : 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,
|
||||
|
||||
@@ -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<string, unknown> | undefined =>
|
||||
isRecord(value) ? (value as Record<string, unknown>) : undefined;
|
||||
const identity = (prefix: string, facts: unknown) =>
|
||||
`${prefix}_${digestCanonicalValue(facts)}`;
|
||||
|
||||
@@ -199,7 +197,7 @@ class ContractInventory {
|
||||
}
|
||||
|
||||
operation(value: unknown, slot: OperationSlot): Result<void, AnalysisError> {
|
||||
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<string, unknown>,
|
||||
inventory: ContractInventory,
|
||||
): Result<ApiContract["info"], AnalysisError> => {
|
||||
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<void, Result<void, AnalysisError>> {
|
||||
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<void, Result<void, AnalysisError>> {
|
||||
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<void, Result<ApiContract, AnalysisError>> {
|
||||
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"
|
||||
|
||||
Reference in new issue
Block a user