Skip to content

Commit

Permalink
Set context for resource page
Browse files Browse the repository at this point in the history
  • Loading branch information
andrii-balitskyi committed Aug 22, 2024
1 parent 0561465 commit d5f7a9b
Showing 1 changed file with 100 additions and 42 deletions.
142 changes: 100 additions & 42 deletions src/lib/template-context.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,39 @@
import type { Blueprint, Endpoint } from '@seamapi/blueprint'
import type { Blueprint, Endpoint, Property, Route } from '@seamapi/blueprint'

export interface EndpointTemplateContext {
description: string
title: string
path: string
request: {
preferredMethod: string
parameters: Array<{
name: string
required: boolean
description: string
}>
}
response: {
description: string
resourceType: string | null
responseKey: string | null
}
codeSamples: Array<{
title: string
description: string
code: Record<
string,
{
title: string
request: string
response: string
}
>
}>
}

export function setEndpointTemplateContext(
file: Partial<EndpointTemplateContext>,
endpoint: Endpoint,
blueprint: Blueprint,
): void {
file.description = endpoint.description
file.title = endpoint.title
Expand All @@ -15,13 +45,13 @@ export function setEndpointTemplateContext(
name: param.name,
required: param.isRequired,
description: param.description,
type: param.type,
// TODO: uncomment when blueprint is fixed
// type: param.type,
})),
}

file.response = {
description: endpoint.response.description,
properties: null,
resourceType: null,
responseKey: null,
}
Expand All @@ -30,12 +60,6 @@ export function setEndpointTemplateContext(
const { resourceType, responseKey } = endpoint.response
file.response.resourceType = resourceType
file.response.responseKey = responseKey
const resource = blueprint.resources[resourceType]
file.response.properties =
resource?.properties.map((property) => ({
name: property.name,
description: property.description,
})) ?? null
}

file.codeSamples = endpoint.codeSamples.map((sample) => ({
Expand All @@ -45,37 +69,71 @@ export function setEndpointTemplateContext(
}))
}

export interface EndpointTemplateContext {
description: string
title: string
path: string
request: {
preferredMethod: string
parameters: Array<{
name: string
required: boolean
description: string
}>
}
response: {
description: string
resourceType: string | null
responseKey: string | null
properties: null | Array<{
name: string
description: string
}>
type ContextResourceProperty = Pick<
Property,
'name' | 'jsonType' | 'description' | 'format' | 'isDeprecated'
>
interface ContextResource {
name: string
properties: ContextResourceProperty[]
}
type ContextEndpoint = Pick<Endpoint, 'path' | 'description'>

export interface ResourceTemplateContext {
resources: ContextResource[]
endpoints: ContextEndpoint[]
}

export function setApiResourceTemplateContext(
file: Partial<ResourceTemplateContext>,
route: Route,
blueprint: Blueprint,
): void {
file.endpoints = route.endpoints.map(({ path, description }) => ({
path,
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]

if (resource == null) {
// eslint-disable-next-line no-console
console.warn(`No resource ${resourceName} in blueprint`)
continue
}

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

file.resources.push({
name: resourceName,
properties: resource.properties.map(
({ name, jsonType, description, format, isDeprecated }) => ({
name,
jsonType,
description,
format,
isDeprecated,
}),
),
})
}
}
codeSamples: Array<{
title: string
description: string
code: Record<
string,
{
title: string
request: string
response: string
}
>
}>
}

0 comments on commit d5f7a9b

Please sign in to comment.