Skip to content

Commit

Permalink
Define resources for each route
Browse files Browse the repository at this point in the history
  • Loading branch information
razor-x authored Nov 1, 2024
1 parent 8a4d3ca commit 3b351b1
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 66 deletions.
12 changes: 11 additions & 1 deletion src/data/paths.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
---
/acs/systems:
title: Systems
resources:
- acs_system

/acs/users:
title: Users
resources:
- acs_user

/acs/entrances:
title: Entrances
resources:
- acs_entrance

/acs/access_groups:
title: Access Groups
resources:
- acs_access_group

/acs/credentials:
title: Credentials
title:
resources:
- acs_credential
109 changes: 44 additions & 65 deletions src/lib/layout-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,83 +129,62 @@ export function setApiRouteLayoutContext(
blueprint: Blueprint,
pathMetadata: PathMetadata,
): void {
const page = pathMetadata[route.path]
if (page == null) {
throw new Error(`Missing page metadata for ${route.path}`)
const metadata = pathMetadata[route.path]
if (metadata == null) {
throw new Error(`Missing path metadata for ${route.path}`)
}
file.title = page.title
file.title = metadata.title
file.path = route.path
file.endpoints = route.endpoints.map(({ path, name, description }) => ({
path,
name,
description: getFirstParagraph(description),
}))
file.resources = []

const endpointsWithResourceType = route.endpoints.filter(
(e) =>
e.response.responseType === 'resource' ||
e.response.responseType === 'resource_list',
)

const uniqueResources = new Set<string>()

for (const endpoint of endpointsWithResourceType) {
if (!('resourceType' in endpoint.response)) {
// eslint-disable-next-line no-console
console.warn(`No resourceType in ${endpoint.path} endpoint response`)
continue
}

const resourceName = endpoint.response.resourceType
const resource = blueprint.resources[resourceName]
file.resources = []
for (const resourceType of metadata.resources) {
const resource = blueprint.resources[resourceType]

if (resource == null) {
// eslint-disable-next-line no-console
console.warn(`No resource ${resourceName} in blueprint`)
continue
throw new Error(
`Path metadata for ${route.path} has invalid resource type ${resourceType}`,
)
}

if (!uniqueResources.has(resourceName)) {
uniqueResources.add(resourceName)

file.resources.push({
name: resourceName,
description: resource.description,
properties: resource.properties
.filter(({ isUndocumented }) => !isUndocumented)
.map((prop) => {
const {
name,
description,
format,
isDeprecated,
deprecationMessage,
} = prop
const contextResourceProp: ContextResourceProperty = {
name,
description,
format: normalizePropertyFormatForDocs(format),
isDeprecated,
deprecationMessage,
}

if ('values' in prop) {
contextResourceProp.enumValues = prop.values.map(
({ name }) => name,
)
}

if ('properties' in prop) {
contextResourceProp.objectProperties = flattenObjectProperties(
prop.properties,
)
}

return contextResourceProp
}),
})
}
file.resources.push({
name: resource.resourceType,
description: resource.description,
properties: resource.properties
.filter(({ isUndocumented }) => !isUndocumented)
.map((prop) => {
const {
name,
description,
format,
isDeprecated,
deprecationMessage,
} = prop
const contextResourceProp: ContextResourceProperty = {
name,
description,
format: normalizePropertyFormatForDocs(format),
isDeprecated,
deprecationMessage,
}

if ('values' in prop) {
contextResourceProp.enumValues = prop.values.map(({ name }) => name)
}

if ('properties' in prop) {
contextResourceProp.objectProperties = flattenObjectProperties(
prop.properties,
)
}

return contextResourceProp
}),
})
}
}

Expand Down
1 change: 1 addition & 0 deletions src/lib/path-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export const PathMetadataSchema = z.record(
z.string(),
z.object({
title: z.string().trim().min(1),
resources: z.array(z.string()),
}),
)

Expand Down

0 comments on commit 3b351b1

Please sign in to comment.