Skip to content

Commit 6cc72d3

Browse files
committed
fix: review remarks
1 parent 53839b9 commit 6cc72d3

File tree

8 files changed

+37
-55
lines changed

8 files changed

+37
-55
lines changed

src/apitypes/graphql/graphql.changes.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ export const compareDocuments = async (
7373
metaKey: DIFF_META_KEY,
7474
originsFlag: ORIGINS_SYMBOL,
7575
diffsAggregatedFlag: DIFFS_AGGREGATED_META_KEY,
76-
// mode: COMPARE_MODE_OPERATION,
7776
normalizedResult: true,
7877
},
7978
) as { merged: GraphApiSchema; diffs: Diff[] }

src/apitypes/graphql/graphql.consts.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
* limitations under the License.
1515
*/
1616
import { ResolvedVersionDocument, ZippableDocument } from '../../types'
17-
import { GraphQLDocumentType } from './graphql.types'
1817

1918
export const GRAPHQL_API_TYPE = 'graphql' as const
2019

@@ -47,5 +46,5 @@ export const GRAPHQL_TYPE = {
4746
} as const
4847

4948
export function isGraphqlDocument(document: ZippableDocument | ResolvedVersionDocument): boolean {
50-
return Object.values(GRAPHQL_DOCUMENT_TYPE).includes(document.type as GraphQLDocumentType)
49+
return Object.values(GRAPHQL_DOCUMENT_TYPE).some(type => document.type === type)
5150
}

src/apitypes/rest/rest.changes.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ export const compareDocuments = async (
108108
metaKey: DIFF_META_KEY,
109109
originsFlag: ORIGINS_SYMBOL,
110110
diffsAggregatedFlag: DIFFS_AGGREGATED_META_KEY,
111-
// mode: COMPARE_MODE_OPERATION,
112111
normalizedResult: true,
113112
},
114113
) as { merged: OpenAPIV3.Document; diffs: Diff[] }
@@ -123,8 +122,8 @@ export const compareDocuments = async (
123122
const tags = new Set<string>()
124123
const changedOperations: OperationChanges[] = []
125124

126-
for (const path of Object.keys((merged as OpenAPIV3.Document).paths)) {
127-
const pathData = (merged as OpenAPIV3.Document).paths[path]
125+
for (const path of Object.keys(merged.paths)) {
126+
const pathData = merged.paths[path]
128127
if (typeof pathData !== 'object' || !pathData) { continue }
129128

130129
for (const key of Object.keys(pathData)) {
@@ -179,7 +178,7 @@ export const compareDocuments = async (
179178
}
180179
}
181180

182-
return { operationChanges: changedOperations, tags: [...tags.values()] }
181+
return { operationChanges: changedOperations, tags: Array.from(tags) }
183182
}
184183

185184
async function reclassifyBreakingChanges(
@@ -207,10 +206,6 @@ async function reclassifyBreakingChanges(
207206
if (!previousOperation?.deprecatedItems) { return }
208207

209208
for (const diff of onlyBreaking) {
210-
if (diff.type !== breaking) {
211-
continue
212-
}
213-
214209
const deprecatedInVersionsCount = previousOperation?.deprecatedInPreviousVersions?.length ?? 0
215210
if (isOperationRemove(diff) && deprecatedInVersionsCount > 1) {
216211
diff.type = risky

src/apitypes/rest/rest.utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export const extractRootServersDiffs = (doc: OpenAPIV3.Document): Diff[] => {
100100
export const extractRootSecurityDiffs = (doc: OpenAPIV3.Document): Diff[] => {
101101
const addedSecurityDiff = (doc as WithDiffMetaRecord<OpenAPIV3.Document>)[DIFF_META_KEY]?.security
102102
const securityDiffs = Object.values((doc.security as WithDiffMetaRecord<OpenAPIV3.SecurityRequirementObject[]>)?.[DIFF_META_KEY] ?? {})
103-
const componentsSecuritySchemesDiffs = Object.values((doc.components?.securitySchemes as WithDiffMetaRecord<OpenAPIV3.ComponentsObject['securitySchemes']>)[DIFF_META_KEY] ?? {})
103+
const componentsSecuritySchemesDiffs = Object.values((doc.components?.securitySchemes as WithDiffMetaRecord<Record<string, OpenAPIV3.SecuritySchemeObject>>)[DIFF_META_KEY] ?? {})
104104
return [
105105
...(addedSecurityDiff ? [addedSecurityDiff] : []),
106106
...securityDiffs,

src/builder.ts

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
BuildConfigBase,
2020
BuildConfigFile,
2121
BuildConfigRef,
22+
BuilderType,
2223
ExportDocument,
2324
FileId,
2425
isPublishBuildConfig,
@@ -58,7 +59,6 @@ import {
5859
DEFAULT_BATCH_SIZE,
5960
DEFAULT_VALIDATION_RULES_SEVERITY_CONFIG,
6061
EXPORT_BUILD_TYPES,
61-
ExportBuildType,
6262
MESSAGE_SEVERITY,
6363
SUPPORTED_FILE_FORMATS,
6464
VERSION_STATUS,
@@ -283,6 +283,22 @@ export class PackageVersionBuilder implements IPackageVersionBuilder {
283283
return this.buildResult
284284
}
285285

286+
private findApiBuilderByApiType(apiType: BuilderType): ApiBuilder {
287+
const apiBuilder = this.apiBuilders.find(apiBuilder => apiBuilder.apiType === apiType)
288+
if (!apiBuilder) {
289+
throw new Error(`Cannot find apiBuilder for apiType ${apiType}`)
290+
}
291+
return apiBuilder
292+
}
293+
294+
private findApiBuilderBySpecType(type: string): ApiBuilder {
295+
const apiBuilder = this.apiBuilders.find(apiBuilder => apiBuilder.types.includes(type))
296+
if (!apiBuilder) {
297+
throw new Error(`Cannot find apiBuilder for specification type ${type}`)
298+
}
299+
return apiBuilder
300+
}
301+
286302
async parsedFileResolver(fileId: string): Promise<SourceFile | null> {
287303
if (this.parsedFiles.has(fileId)) {
288304
return this.parsedFiles.get(fileId) ?? null
@@ -325,11 +341,7 @@ export class PackageVersionBuilder implements IPackageVersionBuilder {
325341
if (!document) {
326342
throw new Error(`Raw document ${slug} is missing in local cache`)
327343
}
328-
const apiBuilder = this.apiBuilders.find(apiBuilder => apiBuilder.types.includes(document?.type))
329-
if (!apiBuilder) {
330-
// todo
331-
throw new Error(`Cannot find apiBuilder for type ${document?.type}`)
332-
}
344+
const apiBuilder = this.findApiBuilderBySpecType(document.type)
333345
return new File([apiBuilder.dumpDocument(document)], document.filename)
334346
}
335347

@@ -475,12 +487,14 @@ export class PackageVersionBuilder implements IPackageVersionBuilder {
475487
apiType?: OperationsApiType,
476488
): Promise<ResolvedVersionDocuments | null> {
477489
packageId = packageId ?? this.config.packageId
478-
// this is the case when a version has been built just now, and there's nothing to fetch yet, so
479-
// the only way to get the docs is to get them from buildResult, but the referenced packages map will be empty (packages: {})
480490
if (this.canBeResolvedLocally(version, packageId)) {
481-
const apiBuilder = this.apiBuilders.find(apiBuilder => apiBuilder.apiType === apiType)
482-
const currentApiTypeDocuments = this.documentList.filter(({ type }) => apiBuilder?.types.includes(type))
483-
return { documents: currentApiTypeDocuments, packages: {} }
491+
// this is the case when a version has been built just now, and there's nothing to fetch yet, so
492+
// the only way to get the docs is to get them from buildResult, but the referenced packages map will be empty (packages: {})
493+
if (apiType) {
494+
const apiBuilder = this.findApiBuilderByApiType(apiType)
495+
return { documents: this.documentList.filter(({ type }) => apiBuilder.types.includes(type)), packages: {} }
496+
}
497+
return { documents: this.documentList, packages: {} }
484498
}
485499

486500
const { versionDocumentsResolver } = this.params.resolvers
@@ -538,7 +552,7 @@ export class PackageVersionBuilder implements IPackageVersionBuilder {
538552
private canBeResolvedLocally(version: string, packageId: string | undefined): boolean {
539553
return this.config.buildType !== BUILD_TYPE.CHANGELOG &&
540554
this.config.buildType !== BUILD_TYPE.PREFIX_GROUPS_CHANGELOG &&
541-
!EXPORT_BUILD_TYPES.includes(this.config.buildType as ExportBuildType) &&
555+
EXPORT_BUILD_TYPES.every(type => type !== this.config.buildType) &&
542556
version === this.config.version &&
543557
packageId === this.config.packageId
544558
}

src/components/compare/bwc.validation.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ export function reclassifyNoBwcBreakingChanges(
2525
): Diff[] {
2626
if (current?.apiKind === API_KIND.NO_BWC || previous?.apiKind === API_KIND.NO_BWC) {
2727
return operationDiffs?.map((diff) => {
28-
if (diff.type !== BREAKING_CHANGE_TYPE) {
29-
return diff
30-
}
31-
return { ...diff, type: RISKY_CHANGE_TYPE }
28+
return diff.type === BREAKING_CHANGE_TYPE
29+
? { ...diff, type: RISKY_CHANGE_TYPE }
30+
: diff
3231
})
3332
}
3433
return operationDiffs

src/components/compare/compare.utils.ts

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -89,29 +89,6 @@ export function getOperationTypesFromTwoVersions(
8989
return [prevVersion?.operationTypes || [], currVersion?.operationTypes || []]
9090
}
9191

92-
type OperationIdWithoutGroupPrefix = string
93-
export type OperationIdentityMap = Record<OperationIdWithoutGroupPrefix, OperationId>
94-
95-
export function dedupePairs<A extends object, B extends object>(
96-
pairs: ReadonlyArray<[A, B]>,
97-
): Array<[A, B]> {
98-
const seen = new WeakMap<A, WeakSet<B>>()
99-
const out: Array<[A, B]> = []
100-
101-
for (const [a, b] of pairs) {
102-
let bs = seen.get(a)
103-
if (!bs) {
104-
bs = new WeakSet<B>()
105-
seen.set(a, bs)
106-
}
107-
if (bs.has(b)) continue
108-
bs.add(b)
109-
out.push([a, b])
110-
}
111-
112-
return out
113-
}
114-
11592
function dedupeTuples<T extends [object | undefined, object | undefined]>(
11693
tuples: T[],
11794
): T[] {
@@ -138,7 +115,7 @@ function dedupeTuples<T extends [object | undefined, object | undefined]>(
138115
return result
139116
}
140117

141-
export function normalizeOperationIds(operations: ResolvedOperation[], apiBuilder: ApiBuilder, groupSlug: string): [OperationId[], Record<NormalizedOperationId | OperationId, ResolvedOperation>] {
118+
export function normalizeOperationIds(operations: ResolvedOperation[], apiBuilder: ApiBuilder, groupSlug: string): [(NormalizedOperationId | OperationId)[], Record<NormalizedOperationId | OperationId, ResolvedOperation>] {
142119
const normalizedOperationIdToOperation: Record<NormalizedOperationId | OperationId, ResolvedOperation> = {}
143120
operations.forEach(operation => {
144121
const normalizedOperationId = apiBuilder.createNormalizedOperationId?.(operation) ?? operation.operationId
@@ -155,7 +132,6 @@ export function getOperationMetadata(operation: ResolvedOperation): OperationCha
155132

156133
// api specific
157134
...takeIfDefined({ method: operation.metadata?.method }),
158-
// todo originalPath is missing?
159135
...takeIfDefined({ path: operation.metadata?.path }),
160136
...takeIfDefined({ type: operation.metadata?.type }),
161137
}

src/types/external/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ export type TemplatePath = string
3030
export type VersionId = string | `${string}@${number}`
3131
export type OperationId = string
3232

33-
export type WithDiffMetaRecord<T> = T & {[DIFF_META_KEY]?: DiffMetaRecord}
34-
export type WithAggregatedDiffs<T> = T & {[DIFFS_AGGREGATED_META_KEY]: Diff[]}
33+
export type WithDiffMetaRecord<T extends object> = T & {[DIFF_META_KEY]?: DiffMetaRecord}
34+
export type WithAggregatedDiffs<T extends object> = T & {[DIFFS_AGGREGATED_META_KEY]: Diff[]}

0 commit comments

Comments
 (0)