diff --git a/src/lib/template-context.ts b/src/lib/template-context.ts index 47b25ba8..76415866 100644 --- a/src/lib/template-context.ts +++ b/src/lib/template-context.ts @@ -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, endpoint: Endpoint, - blueprint: Blueprint, ): void { file.description = endpoint.description file.title = endpoint.title @@ -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, } @@ -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) => ({ @@ -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 + +export interface ResourceTemplateContext { + resources: ContextResource[] + endpoints: ContextEndpoint[] +} + +export function setApiResourceTemplateContext( + file: Partial, + 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() + + 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 - } - > - }> }