refactor(api): share OpenAPI view row selection (#17448)

This commit is contained in:
Vincent Koc authored and GitHub committed 2026-12-06 16:08:52 +08:00
1 parent 74712cde2b
commit 417d5673af
1 file changed
+44 -46
+44 -46
View File
@@ -66,52 +66,53 @@ export const apiContractViewItemsSchema = z.union([
const readReport = createAnalysisViewResultReader(
z.union([apiContractSchema, graphqlContractSchema]),
);
const selectOperations = (
report: ApiContract,
view: Extract<
z.output<typeof apiContractViewRequestSchema>,
{ collection: "operations" }
>,
) => {
const fields = view.fields ?? ["method", "path", "declared_id"];
return selectViewRows(
report.operations,
view,
matchingViewFields<ApiContract["operations"][number]>([
["operation_id", view.operation_ids],
["operation_key", view.operation_keys],
["method", view.methods],
["path", view.paths],
["state", view.states],
["declared_id", view.declared_ids],
]),
(row) => ({
...pickFields(row, fields),
operation_id: row.operation_id,
operation_key: row.operation_key,
source_pointer: row.source_pointer,
state: row.state,
reason: row.reason,
}),
);
};
const selectRows = (
report: ApiContract,
view: z.output<typeof apiContractViewRequestSchema>,
) => {
if (view.collection === "operations") return selectOperations(report, view);
const select = <Row extends Record<string, unknown>, Item>(
rows: readonly Row[],
filters: readonly [keyof Row, readonly string[] | undefined][],
defaults: readonly string[],
project: (row: Row, selected: Record<string, unknown>) => Item,
) => {
const fields = view.fields ?? defaults;
return selectViewRows(rows, view, matchingViewFields(filters), (row) =>
project(row, pickFields(row, fields)),
);
};
if (view.collection === "operations") {
return select(
report.operations,
[
["operation_id", view.operation_ids],
["operation_key", view.operation_keys],
["method", view.methods],
["path", view.paths],
["state", view.states],
["declared_id", view.declared_ids],
],
["method", "path", "declared_id"],
(row, selected) => ({
...selected,
operation_id: row.operation_id,
operation_key: row.operation_key,
source_pointer: row.source_pointer,
state: row.state,
reason: row.reason,
}),
);
}
if (view.collection === "path-item-references") {
const fields = view.fields ?? ["path", "reference", "target_pointer"];
return selectViewRows(
return select(
report.path_item_references,
view,
matchingViewFields<ApiContract["path_item_references"][number]>([
[
["reference_id", view.reference_ids],
["state", view.states],
]),
(row) => ({
...pickFields(row, fields),
],
["path", "reference", "target_pointer"],
(row, selected) => ({
...selected,
reference_id: row.reference_id,
source_pointer: row.source_pointer,
state: row.state,
@@ -120,15 +121,12 @@ const selectRows = (
}),
);
}
const fields = view.fields ?? ["message"];
return selectViewRows(
return select(
report.diagnostics,
view,
matchingViewFields<ApiContract["diagnostics"][number]>([
["code", view.codes],
]),
(row) => ({
...pickFields(row, fields),
[["code", view.codes]],
["message"],
(row, selected) => ({
...selected,
diagnostic_id: row.diagnostic_id,
source_pointer: row.source_pointer,
code: row.code,