-
Notifications
You must be signed in to change notification settings - Fork 12
feat(agent-bff): emit the OpenAPI document from a forest-bff openapi subcommand #1811
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Tonours
wants to merge
2
commits into
feature/prd-886-openapi-static-document
Choose a base branch
from
feature/prd-887-openapi-cli-export
base: feature/prd-886-openapi-static-document
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| import type BFFHttpServer from './http/bff-http-server'; | ||
| import type { Logger } from './ports/logger-port'; | ||
|
|
||
| import { mkdirSync, writeFileSync } from 'fs'; | ||
| import path from 'path'; | ||
|
|
||
| import runCli from './cli-core'; | ||
| import { extractErrorMessage } from './errors'; | ||
| import { generateOpenApiDocument, serializeOpenApi } from './openapi/openapi-document'; | ||
| import version from './version'; | ||
|
|
||
| export const DEFAULT_OUTPUT_FILE = 'openapi.json'; | ||
|
|
||
| const OUTPUT_FLAG = '--output'; | ||
|
|
||
| export const USAGE = `Usage: forest-bff [command] | ||
|
|
||
| Commands: | ||
| (none) Start the BFF server, configured from the environment. | ||
| openapi Write the OpenAPI document to stdout. Needs no configuration. | ||
| --output [file] Write to a file instead of stdout, defaulting | ||
| to ${DEFAULT_OUTPUT_FILE} in the current directory. | ||
|
|
||
| Options: | ||
| -h, --help Show this help and exit. | ||
| -v, --version Show the package version and exit.`; | ||
|
|
||
| export const HINT = "Run 'forest-bff --help' for usage."; | ||
|
|
||
| const HELP_FLAGS = new Set(['-h', '--help']); | ||
| const VERSION_FLAGS = new Set(['-v', '--version']); | ||
|
|
||
| export interface DispatchOutcome { | ||
| exitCode: number; | ||
| server?: BFFHttpServer; | ||
| } | ||
|
|
||
| export function renderOpenApi(): string { | ||
| return `${serializeOpenApi(generateOpenApiDocument(version))}\n`; | ||
| } | ||
|
|
||
| function rejectCli(reason: string): DispatchOutcome { | ||
| process.stderr.write(`${reason}\n${HINT}\n`); | ||
|
|
||
| return { exitCode: 1 }; | ||
| } | ||
|
|
||
| interface OutputOption { | ||
| file?: string; | ||
| extras: string[]; | ||
| } | ||
|
|
||
| function parseOutputOption(rest: string[]): OutputOption { | ||
| const index = rest.indexOf(OUTPUT_FLAG); | ||
|
|
||
| if (index === -1) return { extras: rest }; | ||
|
|
||
| const candidate = rest[index + 1]; | ||
| const takesValue = candidate !== undefined && candidate !== '' && !candidate.startsWith('-'); | ||
|
|
||
| return { | ||
| file: takesValue ? candidate : DEFAULT_OUTPUT_FILE, | ||
| extras: [...rest.slice(0, index), ...rest.slice(index + (takesValue ? 2 : 1))], | ||
| }; | ||
| } | ||
|
|
||
| function writeOpenApiFile(file: string): DispatchOutcome { | ||
| const asDirectory = file.endsWith('/') || file.endsWith(path.sep); | ||
| const destination = path.resolve( | ||
| process.cwd(), | ||
| asDirectory ? path.join(file, DEFAULT_OUTPUT_FILE) : file, | ||
| ); | ||
|
|
||
| try { | ||
| mkdirSync(path.dirname(destination), { recursive: true }); | ||
| writeFileSync(destination, renderOpenApi()); | ||
| } catch (error) { | ||
| process.stderr.write(`Cannot write ${destination}: ${extractErrorMessage(error)}\n`); | ||
|
|
||
| return { exitCode: 1 }; | ||
| } | ||
|
|
||
| process.stderr.write(`Wrote the OpenAPI document to ${destination}\n`); | ||
|
|
||
| return { exitCode: 0 }; | ||
| } | ||
|
|
||
| export default async function dispatchCli( | ||
| argv: string[], | ||
| env: NodeJS.ProcessEnv, | ||
| logger?: Logger, | ||
| ): Promise<DispatchOutcome> { | ||
| const [subcommand, ...rest] = argv; | ||
|
|
||
| if (subcommand === undefined) { | ||
| return { exitCode: 0, server: await runCli(env, logger) }; | ||
| } | ||
|
|
||
| if (HELP_FLAGS.has(subcommand)) { | ||
| process.stdout.write(`${USAGE}\n`); | ||
|
|
||
| return { exitCode: 0 }; | ||
| } | ||
|
|
||
| if (VERSION_FLAGS.has(subcommand)) { | ||
| process.stdout.write(`${version}\n`); | ||
|
|
||
| return { exitCode: 0 }; | ||
| } | ||
|
|
||
| if (subcommand !== 'openapi') { | ||
| return rejectCli( | ||
| subcommand === OUTPUT_FLAG | ||
| ? `${OUTPUT_FLAG} only applies to the openapi command` | ||
| : `Unknown command: ${subcommand}`, | ||
| ); | ||
| } | ||
|
|
||
| const { file, extras } = parseOutputOption(rest); | ||
|
|
||
| if (extras.length > 0) { | ||
| return rejectCli( | ||
| `openapi accepts only --output, got: ${extras.map(extra => JSON.stringify(extra)).join(' ')}`, | ||
| ); | ||
| } | ||
|
|
||
| if (file !== undefined) { | ||
| return writeOpenApiFile(file); | ||
| } | ||
|
|
||
| process.stdout.write(renderOpenApi()); | ||
|
|
||
| return { exitCode: 0 }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,12 @@ | ||
| #!/usr/bin/env node | ||
| /* istanbul ignore file */ | ||
| import runCli, { reportFatalError } from './cli-core'; | ||
| import { reportFatalError } from './cli-core'; | ||
| import dispatchCli from './cli-dispatch'; | ||
|
|
||
| if (require.main === module) { | ||
| runCli(process.env).catch(reportFatalError); | ||
| dispatchCli(process.argv.slice(2), process.env) | ||
| .then(({ exitCode }) => { | ||
| if (exitCode !== 0) process.exitCode = exitCode; | ||
| }) | ||
| .catch(reportFatalError); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.