Skip to content

Commit c21ae3c

Browse files
committed
refactor: move normalized id creation to apiBuilder method
1 parent 690460d commit c21ae3c

File tree

5 files changed

+41
-21
lines changed

5 files changed

+41
-21
lines changed

src/apitypes/rest/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { OpenAPIV3 } from 'openapi-types'
1919
import { buildRestDocument, dumpRestDocument } from './rest.document'
2020
import { REST_API_TYPE, REST_DOCUMENT_TYPE } from './rest.consts'
2121
import { compareRestOperationsData } from './rest.changes'
22-
import { buildRestOperations } from './rest.operations'
22+
import { buildRestOperations, createNormalizedOperationId } from './rest.operations'
2323
import { parseRestFile } from './rest.parser'
2424

2525
import { ApiBuilder } from '../../types'
@@ -34,4 +34,5 @@ export const restApiBuilder: ApiBuilder<OpenAPIV3.Document> = {
3434
buildOperations: buildRestOperations,
3535
dumpDocument: dumpRestDocument,
3636
compareOperationsData: compareRestOperationsData,
37+
createNormalizedOperationId: createNormalizedOperationId,
3738
}

src/apitypes/rest/rest.operations.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,14 @@
1717
import { OpenAPIV3 } from 'openapi-types'
1818

1919
import { buildRestOperation } from './rest.operation'
20-
import { OperationsBuilder } from '../../types'
21-
import { createBundlingErrorHandler, removeComponents, removeFirstSlash, slugify } from '../../utils'
20+
import { OperationIdNormalizer, OperationsBuilder } from '../../types'
21+
import {
22+
createBundlingErrorHandler,
23+
IGNORE_PATH_PARAM_UNIFIED_PLACEHOLDER,
24+
removeComponents,
25+
removeFirstSlash,
26+
slugify,
27+
} from '../../utils'
2228
import { getOperationBasePath } from './rest.utils'
2329
import type * as TYPE from './rest.types'
2430
import { HASH_FLAG, INLINE_REFS_FLAG, MESSAGE_SEVERITY, NORMALIZE_OPTIONS, ORIGINS_SYMBOL } from '../../consts'
@@ -106,3 +112,8 @@ export const buildRestOperations: OperationsBuilder<OpenAPIV3.Document> = async
106112
}
107113
return operations
108114
}
115+
116+
export const createNormalizedOperationId: OperationIdNormalizer = (operation) => {
117+
const { metadata: { path, method } } = operation
118+
return slugify(`${path}-${method}`, [], IGNORE_PATH_PARAM_UNIFIED_PLACEHOLDER)
119+
}

src/components/compare/compare.operations.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import {
1818
ApiAudienceTransition,
19+
ApiBuilder,
1920
CompareContext,
2021
CompareOperationsPairContext,
2122
OperationChanges,
@@ -33,7 +34,6 @@ import {
3334
getOperationMetadata,
3435
getOperationsHashMapByApiType,
3536
getOperationTags,
36-
getOperationTypesFromTwoVersions,
3737
getUniqueApiTypesFromVersions,
3838
takeSubstringIf,
3939
totalChanges,
@@ -45,14 +45,12 @@ import {
4545
calculateImpactedSummary,
4646
executeInBatches,
4747
getSplittedVersionKey,
48-
IGNORE_PATH_PARAM_UNIFIED_PLACEHOLDER,
4948
removeFirstSlash,
5049
removeObjectDuplicates,
5150
slugify,
5251
} from '../../utils'
5352
import { asyncDebugPerformance, DebugPerformanceContext } from '../../utils/logs'
5453
import { validateBwcBreakingChanges } from './bwc.validation'
55-
import { REST_API_TYPE } from '../../apitypes'
5654

5755
export async function compareVersionsOperations(
5856
prev: VersionParams,
@@ -137,8 +135,8 @@ async function compareCurrentApiType(
137135
const { operations: prevOperations = [] } = await versionOperationsResolver(apiType, prev?.version ?? '', prev?.packageId ?? '', undefined, false) || {}
138136
const { operations: currOperations = [] } = await versionOperationsResolver(apiType, curr?.version ?? '', curr?.packageId ?? '', undefined, false) || {}
139137

140-
const [prevReducedOperationIdToHashMap, prevReducedOperationIdToOriginal] = getOperationsHashMapByApiType(apiType, prevOperations, ctx)
141-
const [currReducedOperationIdToHashMap, currReducedOperationIdToOriginal] = getOperationsHashMapByApiType(apiType, currOperations, ctx, true)
138+
const [prevReducedOperationIdToHashMap, prevReducedOperationIdToOriginal] = getOperationsHashMapByApiType(apiBuilder, prevOperations, ctx)
139+
const [currReducedOperationIdToHashMap, currReducedOperationIdToOriginal] = getOperationsHashMapByApiType(apiBuilder, currOperations, ctx, true)
142140

143141
const reducedOperationIds = new Set([...Object.keys(prevReducedOperationIdToHashMap), ...Object.keys(currReducedOperationIdToHashMap)])
144142
const operationsMapping: OperationsMappingResult = { [HANDLE_TYPE_ADDED]: [], [HANDLE_TYPE_REMOVED]: [], [HANDLE_TYPE_CHANGED]: {} }
@@ -249,7 +247,7 @@ async function compareCurrentApiType(
249247
const { operations: currOperationsWithoutData = [] } = await versionOperationsResolver(apiType, currVersion, currPackageId, previousBatch, false) || {}
250248
const { operations: prevOperationsWithoutData = [] } = await versionOperationsResolver(apiType, prevVersion, prevPackageId, currentBatch, false) || {}
251249

252-
const pairOperationsMap = createPairOperationsMap(currGroupSlug, prevGroupSlug, currOperationsWithoutData, prevOperationsWithoutData)
250+
const pairOperationsMap = createPairOperationsMap(currGroupSlug, prevGroupSlug, currOperationsWithoutData, prevOperationsWithoutData, apiBuilder)
253251
Object.values(pairOperationsMap).forEach((pair) => {
254252
calculateApiAudienceTransitions(pair.current, pair.previous, apiAudienceTransitions)
255253
})
@@ -359,19 +357,26 @@ async function compareCurrentApiType(
359357
]
360358
}
361359

362-
const createPairOperationsMap = (currGroupSlug: string, prevGroupSlug: string, currentOperations: ResolvedOperation[], previousOperations: ResolvedOperation[]): Record<string, { previous?: ResolvedOperation; current: ResolvedOperation }> => {
360+
const createPairOperationsMap = (
361+
currGroupSlug: string,
362+
prevGroupSlug: string,
363+
currentOperations: ResolvedOperation[],
364+
previousOperations: ResolvedOperation[],
365+
apiBuilder: ApiBuilder,
366+
): Record<string, {
367+
previous?: ResolvedOperation
368+
current: ResolvedOperation
369+
}> => {
363370

364371
const operationsMap: Record<string, { previous?: ResolvedOperation; current: ResolvedOperation }> = {}
365372

366373
for (const currentOperation of currentOperations) {
367-
// todo
368-
const normalizedOperationId = currentOperation.apiType === REST_API_TYPE ? slugify(`${currentOperation.metadata.path}-${currentOperation.metadata.method}`, [], IGNORE_PATH_PARAM_UNIFIED_PLACEHOLDER) : currentOperation.operationId
374+
const normalizedOperationId = apiBuilder.createNormalizedOperationId?.(currentOperation) ?? currentOperation.operationId
369375
operationsMap[takeSubstringIf(!!currGroupSlug, normalizedOperationId, currGroupSlug.length)] = { current: currentOperation }
370376
}
371377

372378
for (const previousOperation of previousOperations) {
373-
// todo
374-
const normalizedOperationId = previousOperation.apiType === REST_API_TYPE ? slugify(`${previousOperation.metadata.path}-${previousOperation.metadata.method}`, [], IGNORE_PATH_PARAM_UNIFIED_PLACEHOLDER) : previousOperation.operationId
379+
const normalizedOperationId = apiBuilder.createNormalizedOperationId?.(previousOperation) ?? previousOperation.operationId
375380
const prevOperationId = takeSubstringIf(!!prevGroupSlug, normalizedOperationId, prevGroupSlug.length)
376381
const operationsMappingElement = operationsMap[prevOperationId]
377382
if (operationsMappingElement) {

src/components/compare/compare.utils.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
*/
1616

1717
import {
18+
ApiBuilder,
1819
ChangeSummary,
1920
CompareContext,
2021
DIFF_TYPES,
2122
ImpactedOperationSummary,
23+
NormalizedOperationId,
2224
OperationChangesMetadata,
2325
OperationId,
2426
OperationsApiType,
@@ -94,22 +96,19 @@ export function getOperationTypesFromTwoVersions(
9496
type OperationIdWithoutGroupPrefix = string
9597
export type OperationIdentityMap = Record<OperationIdWithoutGroupPrefix, OperationId>
9698

97-
type NormalizedOperationId = `${NormalizedPath}-${OpenAPIV3.HttpMethods}`
98-
9999
export function getOperationsHashMapByApiType(
100-
currentApiType: OperationsApiType,
100+
apiBuilder: ApiBuilder,
101101
operations: ResolvedOperation[],
102102
ctx: CompareContext,
103103
areOperationsFromCurrentVersion: boolean = false,
104104
): [ResolvedVersionOperationsHashMap, OperationIdentityMap] {
105105
const { buildType, currentGroup, previousGroup } = ctx.config
106-
const currentApiTypeOperations = operations.filter(({ apiType }) => apiType === currentApiType)
107-
108106
const resolvedHashMap: ResolvedVersionOperationsHashMap = {}
109107
const normalizedToOriginalOperationIdMap: Record<NormalizedOperationId | OperationId, OperationId> = {}
110108

111-
for (const { operationId, apiType, metadata, dataHash } of currentApiTypeOperations) {
112-
const normalizedOperationId = apiType === REST_API_TYPE ? slugify(`${metadata.path}-${metadata.method}`, [], IGNORE_PATH_PARAM_UNIFIED_PLACEHOLDER) : operationId
109+
for (const operation of operations) {
110+
const { operationId, dataHash } = operation
111+
const normalizedOperationId = apiBuilder.createNormalizedOperationId?.(operation) ?? operationId
113112
resolvedHashMap[normalizedOperationId] = dataHash
114113
normalizedToOriginalOperationIdMap[normalizedOperationId] = operationId
115114
}

src/types/internal/apiBuilder.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ export interface CompareOperationsPairContext {
7575
currentPackageId: PackageId
7676
}
7777

78+
export type NormalizedOperationId = string
79+
7880
export type FileParser = (fileId: string, data: Blob) => Promise<File | undefined>
7981
export type DocumentBuilder<T> = (parsedFile: TextFile, file: BuildConfigFile, ctx: BuilderContext<T>) => Promise<VersionDocument<T>>
8082
export type OperationsBuilder<T, M = any> = (document: VersionDocument<T>, ctx: BuilderContext<T>, debugCtx?: DebugPerformanceContext) => Promise<ApiOperation<M>[]>
@@ -85,6 +87,7 @@ export type OperationChangesValidator = (
8587
previousOperation?: RestOperationData, // TODO remove
8688
prePreviousOperation?: RestOperationData, // TODO remove
8789
) => boolean
90+
export type OperationIdNormalizer = (operation: ResolvedOperation) => NormalizedOperationId
8891
export type BreakingChangeReclassifier = (changes: OperationChanges[], previousVersion: string, previousPackageId: string, ctx: CompareContext) => Promise<void>
8992

9093
export interface ApiBuilder<T = any, O = any, M = any> {
@@ -96,6 +99,7 @@ export interface ApiBuilder<T = any, O = any, M = any> {
9699
buildOperations?: OperationsBuilder<T, M>
97100
compareOperationsData?: OperationDataCompare<O>
98101
validateOperationChanges?: OperationChangesValidator
102+
createNormalizedOperationId?: OperationIdNormalizer
99103
}
100104

101105
// internal

0 commit comments

Comments
 (0)