Skip to content

Commit 2953c1e

Browse files
fix: Match events by route path
1 parent ff83914 commit 2953c1e

1 file changed

Lines changed: 29 additions & 23 deletions

File tree

src/lib/layout/api-route.ts

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,17 @@ export function setApiRouteLayoutContext(
8585
blueprint: Blueprint,
8686
pathMetadata: PathMetadata,
8787
): void {
88-
file.events = []
8988
const metadata = pathMetadata[route.path]
9089
if (metadata == null) {
9190
throw new Error(`Missing path metadata for ${route.path}`)
9291
}
92+
9393
file.title = metadata.title
9494
file.path = route.path
95+
96+
const eventsByRoutePath = groupEventsByRoutePath(blueprint.events)
97+
file.events = eventsByRoutePath.get(route.path) ?? []
98+
9599
file.endpoints = route.endpoints
96100
.filter(
97101
({ isUndocumented, title }) => !isUndocumented && title.length !== 0,
@@ -112,27 +116,6 @@ export function setApiRouteLayoutContext(
112116
)
113117
}
114118

115-
const resourceEvents: ApiRouteEvent[] = []
116-
for (const event of blueprint.events) {
117-
if (
118-
event.targetResourceType == null ||
119-
event.targetResourceType !== resourceType
120-
) {
121-
continue
122-
}
123-
124-
const routeEvent: ApiRouteEvent = {
125-
name: event.eventType,
126-
description: event.description,
127-
properties: event.properties
128-
.filter(({ isUndocumented }) => !isUndocumented)
129-
.map(mapBlueprintPropertyToRouteProperty),
130-
}
131-
132-
resourceEvents.push(routeEvent)
133-
file.events.push(routeEvent)
134-
}
135-
136119
const warningsProp = resource.properties.find((p) => p.name === 'warnings')
137120
const errorsProp = resource.properties.find((p) => p.name === 'errors')
138121
const resourceWarnings =
@@ -155,12 +138,35 @@ export function setApiRouteLayoutContext(
155138
properties,
156139
errors: resourceErrors,
157140
warnings: resourceWarnings,
158-
events: resourceEvents,
141+
events: eventsByRoutePath.get(resource.routePath) ?? [],
159142
resourceSamples: resource.resourceSamples.map(mapResourceSample),
160143
})
161144
}
162145
}
163146

147+
const groupEventsByRoutePath = (
148+
events: Blueprint['events'],
149+
): Map<string, ApiRouteEvent[]> => {
150+
const eventsByRoutePath = new Map<string, ApiRouteEvent[]>()
151+
152+
for (const event of events) {
153+
if (event.routePath == null) continue
154+
155+
const routeEvents = eventsByRoutePath.get(event.routePath) ?? []
156+
routeEvents.push({
157+
name: event.eventType,
158+
description: event.description,
159+
properties: event.properties
160+
.filter(({ isUndocumented }) => !isUndocumented)
161+
.map(mapBlueprintPropertyToRouteProperty),
162+
})
163+
164+
eventsByRoutePath.set(event.routePath, routeEvents)
165+
}
166+
167+
return eventsByRoutePath
168+
}
169+
164170
const getFirstParagraph = (text: string): string =>
165171
text.split('\n\n').at(0) ?? text
166172

0 commit comments

Comments
 (0)