Skip to content

Commit

Permalink
Split layout module
Browse files Browse the repository at this point in the history
  • Loading branch information
razor-x authored Nov 1, 2024
1 parent b68365f commit 60966e5
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 104 deletions.
92 changes: 92 additions & 0 deletions src/lib/layout/api-endpoint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import type { CodeSampleSdk, Endpoint } from '@seamapi/blueprint'

const supportedSdks: CodeSampleSdk[] = [
'javascript',
'python',
'php',
'ruby',
'go',
'seam_cli',
]

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

export function setEndpointLayoutContext(
file: Partial<EndpointLayoutContext>,
endpoint: Endpoint,
): void {
file.description = endpoint.description
file.title = endpoint.title
file.path = endpoint.path

file.request = {
preferredMethod: endpoint.request?.preferredMethod ?? '',
parameters: endpoint.request.parameters
.filter(({ isUndocumented }) => !isUndocumented)
.map((param) => ({
name: param.name,
required: param.isRequired,
description: param.description,
jsonType: param.jsonType,
})),
}

file.response = {
description: endpoint.response.description,
resourceType: null,
escapedResourceType: null,
responseKey: null,
responseType: null,
}

if (endpoint.response.responseType !== 'void') {
const { resourceType, responseKey, responseType } = endpoint.response
file.response.resourceType = resourceType
file.response.escapedResourceType = resourceType.replaceAll('_', '\\_')
file.response.responseKey = responseKey
file.response.responseType = responseType
}

file.codeSamples = endpoint.codeSamples.map((sample) => {
const codeEntries = Object.entries(sample.code).filter(([k]) =>
supportedSdks.includes(k as CodeSampleSdk),
)
return {
title: sample.title,
description: sample.description,
code: Object.fromEntries(codeEntries),
}
})
}
111 changes: 8 additions & 103 deletions src/lib/layout/layout-context.ts → src/lib/layout/api-route.ts
Original file line number Diff line number Diff line change
@@ -1,103 +1,14 @@
import type {
Blueprint,
CodeSampleSdk,
Endpoint,
Property,
Route,
} from '@seamapi/blueprint'
import type { Blueprint, Endpoint, Property, Route } from '@seamapi/blueprint'
import { pascalCase } from 'change-case'

import type { PathMetadata } from '../path-metadata.js'

const supportedSdks: CodeSampleSdk[] = [
'javascript',
'python',
'php',
'ruby',
'go',
'seam_cli',
]
import type { PathMetadata } from 'lib/path-metadata.js'

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

export function setEndpointLayoutContext(
file: Partial<EndpointLayoutContext>,
endpoint: Endpoint,
): void {
file.description = endpoint.description
file.title = endpoint.title
file.path = endpoint.path

file.request = {
preferredMethod: endpoint.request?.preferredMethod ?? '',
parameters: endpoint.request.parameters
.filter(({ isUndocumented }) => !isUndocumented)
.map((param) => ({
name: param.name,
required: param.isRequired,
description: param.description,
jsonType: param.jsonType,
})),
}

file.response = {
description: endpoint.response.description,
resourceType: null,
escapedResourceType: null,
responseKey: null,
responseType: null,
}

if (endpoint.response.responseType !== 'void') {
const { resourceType, responseKey, responseType } = endpoint.response
file.response.resourceType = resourceType
file.response.escapedResourceType = resourceType.replaceAll('_', '\\_')
file.response.responseKey = responseKey
file.response.responseType = responseType
}

file.codeSamples = endpoint.codeSamples.map((sample) => {
const codeEntries = Object.entries(sample.code).filter(([k]) =>
supportedSdks.includes(k as CodeSampleSdk),
)
return {
title: sample.title,
description: sample.description,
code: Object.fromEntries(codeEntries),
}
})
resources: ContextResource[]
endpoints: ContextEndpoint[]
}

type ContextResourceProperty = Pick<
Expand All @@ -108,20 +19,14 @@ type ContextResourceProperty = Pick<
enumValues?: string[]
objectProperties?: ContextResourceProperty[]
}

interface ContextResource {
name: string
description: string
properties: ContextResourceProperty[]
}
type ContextEndpoint = Pick<Endpoint, 'path' | 'description'>

export interface RouteLayoutContext {
title: string
description: string
path: string
resources: ContextResource[]
endpoints: ContextEndpoint[]
}
type ContextEndpoint = Pick<Endpoint, 'path' | 'description'>

export function setApiRouteLayoutContext(
file: Partial<RouteLayoutContext>,
Expand Down
3 changes: 2 additions & 1 deletion src/lib/layout/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './layout-context.js'
export * from './api-endpoint.js'
export * from './api-route.js'

0 comments on commit 60966e5

Please sign in to comment.