Something went wrong. Try again.
New TLE Community website using Eleventy. tlecommunity.com
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879import spec from '@tlecommunity/api-spec' with { type: 'json' }
const slugify = (value) => String(value) .toLowerCase() .replace(/[^a-z0-9]+/g, '-') .replace(/(^-|-$)/g, '')
// Error codes are only recorded in prose, e.g. '... Throws 1002, 1010, 1011, 1012, and 1013.'.// Anchor to the 'Throws' keyword so unrelated numbers in a description ('1024 characters') are// ignored, then pull the 3-4 digit codes out of the clause.const errorCodesFromDescription = (description = '') => { const clause = description.match(/Throws\s+((?:\d+[,\s]+(?:and\s+)?)*\d+)/i) if (!clause) return [] return [...new Set(clause[1].match(/\b\d{3,4}\b/g) || [])]}
export default () => { // Refuse to emit a reference from the stale v1.0.0 artifact (no tags, no summaries). if (!spec.tags?.length || !spec.info?.version) { throw new Error( `@tlecommunity/api-spec looks stale (version=${spec.info?.version}, tags=${spec.tags?.length}).` ) }
const tagDescriptions = new Map((spec.tags || []).map((tag) => [tag.name, tag.description || ''])) const byTag = new Map()
for (const [route, item] of Object.entries(spec.paths)) { const op = item.post if (!op) continue
const tag = op.tags?.[0] || 'untagged' const envelope = op.responses?.['200']?.content?.['application/json']?.schema
const entry = { method: route.split('/').pop(), route, anchor: slugify(route.split('/').pop()), summary: op.summary || '', description: op.description || '', auth: Array.isArray(op.security) && op.security.length > 0, requestSchema: op.requestBody?.content?.['application/json']?.schema ?? null, resultSchema: envelope?.properties?.result ?? envelope ?? null, response200Description: op.responses?.['200']?.description || '', errorCodes: errorCodesFromDescription(op.description), }
if (!byTag.has(tag)) byTag.set(tag, []) byTag.get(tag).push(entry) }
const modules = [...byTag.entries()] .map(([name, operations]) => ({ name, slug: slugify(name), description: tagDescriptions.get(name) || '', operations: operations.sort((a, b) => a.method.localeCompare(b.method)), operationCount: operations.length, })) .sort((a, b) => a.name.localeCompare(b.name))
const seen = new Set() for (const module of modules) { if (seen.has(module.slug)) throw new Error(`Duplicate API reference slug: ${module.slug}`) seen.add(module.slug) }
return { info: spec.info, servers: spec.servers || [], modules, moduleCount: modules.length, schemas: spec.components?.schemas ?? {}, securitySchemes: spec.components?.securitySchemes ?? {}, generatedAt: new Date().toISOString(), }}