Skip to content

Commit 9305c38

Browse files
committed
refactor: export createCopyWithPrefixGroupOperationsOnly for reuse in UI
1 parent a3a6a64 commit 9305c38

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/apitypes/rest/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { ApiBuilder } from '../../types'
2525
import { calculateNormalizedOperationId } from '../../utils'
2626

2727
export * from './rest.consts'
28+
export { createCopyWithPrefixGroupOperationsOnly } from './rest.changes'
2829

2930
export const restApiBuilder: ApiBuilder<OpenAPIV3.Document> = {
3031
apiType: REST_API_TYPE,

src/apitypes/rest/rest.changes.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ export const compareDocuments = async (
9090
let currDocData = currFile && JSON.parse(await currFile.text())
9191

9292
if (prevDocData && previousGroup) {
93-
prevDocData = createCopyWithCurrentGroupOperationsOnly(prevDocData, previousGroup)
93+
prevDocData = createCopyWithPrefixGroupOperationsOnly(prevDocData, previousGroup)
9494
}
9595

9696
if (currDocData && currentGroup) {
97-
currDocData = createCopyWithCurrentGroupOperationsOnly(currDocData, currentGroup)
97+
currDocData = createCopyWithPrefixGroupOperationsOnly(currDocData, currentGroup)
9898
}
9999

100100
if (!prevDocData && currDocData) {
@@ -278,10 +278,23 @@ export function createCopyWithEmptyPathItems(template: RestOperationData): RestO
278278
}
279279
}
280280

281+
/**
282+
* Creates a copy of the given RestOperationData, but only includes path items belonging to the specified prefix group.
283+
* All returned paths are adjusted to include any relevant basePath prefixes.
284+
* All servers objects are removed from the resulting structure, as prefix group comparisons do not consider them.
285+
*
286+
* @param {RestOperationData} source - The source RestOperationData object to copy from.
287+
* @param {string} groupPrefix - The base path prefix (group) used to select which operations to include.
288+
* This should be a slash-bounded OpenAPI path group, e.g. "/api/v1/".
289+
* @returns {RestOperationData} A copy of the template including only paths belonging to the specified group,
290+
* with their paths remapped (prefix removed) and with all servers removed from path items and the root.
291+
*/
292+
export function createCopyWithPrefixGroupOperationsOnly(source: RestOperationData, groupPrefix: string): RestOperationData {
281293
validateGroupPrefix(groupPrefix, 'groupPrefix')
294+
282295
// eslint-disable-next-line @typescript-eslint/no-unused-vars
283-
const { paths, servers: rootServers, ...rest } = template
284-
const groupWithoutEdgeSlashes = trimSlashes(group)
296+
const { paths, servers: rootServers, ...rest } = source
297+
const groupWithoutEdgeSlashes = trimSlashes(groupPrefix)
285298

286299
// Since we are anyway composing synthetic specs for prefix groups comparison, we can incorporate
287300
// base paths from root servers and path item servers into the paths.
@@ -296,7 +309,7 @@ export function createCopyWithEmptyPathItems(template: RestOperationData): RestO
296309
.map(([pathKey, pathItem]) => {
297310
// Path item servers take precedence over root servers
298311
const pathItemServers = (pathItem as OpenAPIV3.PathItemObject)?.servers
299-
const basePath = getOperationBasePath(pathItemServers || template.servers || [])
312+
const basePath = getOperationBasePath(pathItemServers || source.servers || [])
300313

301314
// Prepend base path to the path
302315
const fullPath = basePath ? `/${trimSlashes(basePath)}/${trimSlashes(pathKey)}`.replace(/\/+/g, '/') : pathKey

0 commit comments

Comments
 (0)