Skip to content

Commit 5da951f

Browse files
committed
fix: Filter resourceSamples on endpoints like on route
1 parent c644115 commit 5da951f

File tree

2 files changed

+36
-18
lines changed

2 files changed

+36
-18
lines changed

src/lib/layout/api-endpoint.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
mapResourceSample,
1919
normalizePropertyFormatForDocs,
2020
type ResourceSampleContext,
21+
resourceSampleFilter,
2122
} from './api-route.js'
2223

2324
const supportedSdks: SdkName[] = [
@@ -189,6 +190,7 @@ export function setEndpointLayoutContext(
189190
endpoint,
190191
resources,
191192
actionAttempts,
193+
pathMetadata,
192194
).map(mapResourceSample)
193195

194196
const [primaryCodeSample, ...additionalCodeSamples] = endpoint.codeSamples
@@ -245,6 +247,7 @@ const getResourceSamples = (
245247
endpoint: Endpoint,
246248
resources: Resource[],
247249
actionAttempts: ActionAttempt[],
250+
pathMetadata: PathMetadata,
248251
): ResourceSample[] => {
249252
const { response } = endpoint
250253

@@ -268,7 +271,18 @@ const getResourceSamples = (
268271

269272
if (resource == null) return []
270273

271-
const sample = resource.resourceSamples[0]
274+
const metadata = pathMetadata[resource.routePath]
275+
if (metadata == null) {
276+
throw new Error(`Missing path metadata for ${resource.routePath}`)
277+
}
278+
279+
const sample = resource.resourceSamples.filter(
280+
resourceSampleFilter({
281+
include: metadata.include_groups,
282+
exclude: metadata.exclude_groups,
283+
}),
284+
)[0]
285+
272286
if (sample == null) return []
273287

274288
return [

src/lib/layout/api-route.ts

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,7 @@ export const setApiRouteLayoutContext = (
182182
? groupProperties(
183183
legacyProperty.properties,
184184
legacyProperty.propertyGroups,
185-
{
186-
include: metadata.include_groups,
187-
exclude: metadata.exclude_groups,
188-
},
185+
groupOptions,
189186
)
190187
: null
191188

@@ -199,24 +196,31 @@ export const setApiRouteLayoutContext = (
199196
hidePreamble: route.path !== resource.routePath,
200197
events: eventsByRoutePath.get(resource.routePath) ?? [],
201198
resourceSamples: resource.resourceSamples
202-
.filter(({ title }) => {
203-
if (groupOptions.include != null) {
204-
return groupOptions.include.some((x) =>
205-
title.toLowerCase().includes(x.split('_')[0]?.slice(0, -1) ?? ''),
206-
)
207-
}
208-
if (groupOptions.exclude != null) {
209-
return !groupOptions.exclude.some((x) =>
210-
title.toLowerCase().includes(x.split('_')[0]?.slice(0, -1) ?? ''),
211-
)
212-
}
213-
return true
214-
})
199+
.filter(resourceSampleFilter(groupOptions))
215200
.map(mapResourceSample),
216201
})
217202
}
218203
}
219204

205+
export const resourceSampleFilter =
206+
(groupOptions: {
207+
include: string[] | undefined
208+
exclude?: string[] | undefined
209+
}) =>
210+
({ title }: ResourceSample): boolean => {
211+
if (groupOptions.include != null) {
212+
return groupOptions.include.some((x) =>
213+
title.toLowerCase().includes(x.split('_')[0]?.slice(0, -1) ?? ''),
214+
)
215+
}
216+
if (groupOptions.exclude != null) {
217+
return !groupOptions.exclude.some((x) =>
218+
title.toLowerCase().includes(x.split('_')[0]?.slice(0, -1) ?? ''),
219+
)
220+
}
221+
return true
222+
}
223+
220224
const groupVariants = (
221225
property: Property | null,
222226
{

0 commit comments

Comments
 (0)