diff --git a/src/domain/analysisView/apiContractView.ts b/src/domain/analysisView/apiContractView.ts index ce73e5f84a..82ab85106c 100644 --- a/src/domain/analysisView/apiContractView.ts +++ b/src/domain/analysisView/apiContractView.ts @@ -66,52 +66,53 @@ export const apiContractViewItemsSchema = z.union([ const readReport = createAnalysisViewResultReader( z.union([apiContractSchema, graphqlContractSchema]), ); -const selectOperations = ( - report: ApiContract, - view: Extract< - z.output, - { collection: "operations" } - >, -) => { - const fields = view.fields ?? ["method", "path", "declared_id"]; - return selectViewRows( - report.operations, - view, - matchingViewFields([ - ["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, ) => { - if (view.collection === "operations") return selectOperations(report, view); + const select = , Item>( + rows: readonly Row[], + filters: readonly [keyof Row, readonly string[] | undefined][], + defaults: readonly string[], + project: (row: Row, selected: Record) => 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([ + [ ["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([ - ["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,