|
| 1 | +import fs from 'node:fs/promises' |
| 2 | +import path from 'node:path' |
| 3 | +import { isRight, whenRight, whenRights } from '@skyleague/axioms' |
| 4 | +import { listLambdaHandlers } from '@skyleague/esbuild-lambda' |
| 5 | +import type { HTTPHandler, RequestAuthorizerEventHandler } from '@skyleague/event-horizon' |
| 6 | +import { openapiFromHandlers } from '@skyleague/event-horizon/spec' |
| 7 | +import { globby } from 'globby' |
| 8 | +import yaml from 'yaml' |
| 9 | +import type { Argv } from 'yargs' |
| 10 | +import { StarChartHandler } from '../../definition/definition.type.js' |
| 11 | +import type { HttpTrigger } from '../../definition/events/http.type.js' |
| 12 | +import { rootDirectory } from '../../lib/constants.js' |
| 13 | + |
| 14 | +export function builder(yargs: Argv) { |
| 15 | + return yargs |
| 16 | + .option('output', { |
| 17 | + type: 'string', |
| 18 | + }) |
| 19 | + .option('cwd', { |
| 20 | + type: 'string', |
| 21 | + default: rootDirectory, |
| 22 | + coerce: (cwd) => path.join(process.cwd(), cwd), |
| 23 | + }) |
| 24 | +} |
| 25 | + |
| 26 | +export async function handler(argv: ReturnType<typeof builder>['argv']): Promise<void> { |
| 27 | + const { buildDir: _buildDir, artifactDir: _artifactDir, output, cwd } = await argv |
| 28 | + const fnDirs = ['src/**/functions'] |
| 29 | + |
| 30 | + const stacks = await globby(fnDirs, { cwd: cwd, onlyDirectories: true }) |
| 31 | + const handlerDrectories = ( |
| 32 | + await Promise.all(stacks.flatMap(async (fnDir) => listLambdaHandlers(path.join(cwd, fnDir)))) |
| 33 | + ).flat() |
| 34 | + |
| 35 | + // for each handler load the corresponding handler.yml file |
| 36 | + const handlers = await Promise.all( |
| 37 | + handlerDrectories.map(async (handler) => { |
| 38 | + const yamlHandler = `${handler}/handler.yml` |
| 39 | + if (!(await fs.stat(yamlHandler)).isFile()) { |
| 40 | + return { left: 'File not found' } |
| 41 | + } |
| 42 | + |
| 43 | + const content = await fs.readFile(yamlHandler) |
| 44 | + const handlerContent = yaml.parse(content.toString()) |
| 45 | + const eitherHandler = StarChartHandler.parse(handlerContent) |
| 46 | + if ('right' in eitherHandler) { |
| 47 | + const definition = eitherHandler.right |
| 48 | + const handlerFile = path.join(handler, 'index.ts') |
| 49 | + |
| 50 | + if (!(await fs.stat(handlerFile)).isFile()) { |
| 51 | + return { left: 'Handler file not found' } |
| 52 | + } |
| 53 | + |
| 54 | + return { |
| 55 | + right: { |
| 56 | + definition, |
| 57 | + handler: (await import(handler)).handler, |
| 58 | + directory: handler, |
| 59 | + }, |
| 60 | + } |
| 61 | + } |
| 62 | + return eitherHandler |
| 63 | + }), |
| 64 | + ) |
| 65 | + const securitySchemes = whenRights(handlers, (xs) => { |
| 66 | + return { |
| 67 | + right: xs |
| 68 | + .filter((x) => 'request' in x.handler && x.definition.securitySchemes !== undefined) |
| 69 | + .map((x) => { |
| 70 | + const securitySchemes = x.definition.securitySchemes |
| 71 | + const definition = x.handler.request as RequestAuthorizerEventHandler |
| 72 | + return { |
| 73 | + ...definition.security, |
| 74 | + ...securitySchemes, |
| 75 | + } |
| 76 | + }), |
| 77 | + } |
| 78 | + }) |
| 79 | + const httpHandlers = whenRights(handlers, (xs) => { |
| 80 | + return { |
| 81 | + right: xs |
| 82 | + .filter((x) => 'http' in x.handler) |
| 83 | + .flatMap((x) => { |
| 84 | + const definition = x.handler.http as HTTPHandler |
| 85 | + const httpEvents = x.definition.events?.filter((e): e is HttpTrigger => 'http' in e && e.http !== undefined) |
| 86 | + |
| 87 | + return ( |
| 88 | + httpEvents?.map((e, i) => ({ |
| 89 | + directory: `${x.directory}-${i}`, |
| 90 | + handler: { |
| 91 | + ...e.http, |
| 92 | + http: { |
| 93 | + ...definition, |
| 94 | + ...e.http, |
| 95 | + }, |
| 96 | + }, |
| 97 | + })) ?? [] |
| 98 | + ) |
| 99 | + }), |
| 100 | + } |
| 101 | + }) |
| 102 | + const packageJson = JSON.parse((await fs.readFile(path.join(cwd, 'package.json'))).toString()) |
| 103 | + |
| 104 | + const openapi = whenRight(httpHandlers, (xs) => { |
| 105 | + const securitySchemesComponent = isRight(securitySchemes) |
| 106 | + ? { securitySchemes: Object.assign({}, ...securitySchemes.right) } |
| 107 | + : {} |
| 108 | + const openapi = openapiFromHandlers(Object.fromEntries(xs.map((x) => [x.directory, x.handler])), { |
| 109 | + info: { title: packageJson.name, version: packageJson.version }, |
| 110 | + components: { |
| 111 | + ...securitySchemesComponent, |
| 112 | + }, |
| 113 | + }) |
| 114 | + return { right: openapi } |
| 115 | + }) |
| 116 | + |
| 117 | + if (!isRight(openapi)) { |
| 118 | + throw new Error(JSON.stringify(openapi.left)) |
| 119 | + } |
| 120 | + |
| 121 | + const content = JSON.stringify(openapi.right, null, 2) |
| 122 | + if (output !== undefined) { |
| 123 | + await fs.writeFile(output, content) |
| 124 | + } else { |
| 125 | + console.log(content) |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +export default { |
| 130 | + command: 'openapi', |
| 131 | + describe: 'Build OpenAPI', |
| 132 | + builder, |
| 133 | + handler, |
| 134 | +} |
0 commit comments