nvs build --openapi
nvs build --openapi <file> prints an OpenAPI 3.1 document for the routes of a program.
The document has one paths entry for each method with the #[Route] attribute. Each entry has the HTTP method, an operationId of the form Class.method, the path parameters with their schemas, and the responses. The title of the document is the name of the entry file without its extension.
The command prints to standard output and runs nothing. Redirect the output to a file to keep it.
Good to know: nvs build needs --compile or --openapi. Without one of them it stops with an error.
Examples
A document for the frontend team
<?nvs// Shows a small web API and the OpenAPI document that describes it.// `nvs build --openapi 01-a-document-for-the-frontend-team.nvs > openapi.json` writes that document.// Another team can read the file, or generate a client from it, without reading this code.
class Orders{ /// Lists every open order. #[Core\Route(path: '/orders', method: Core\Http\Method::Get)] #[Core\Access(allow: Core\Audience::Public)] public function index(): string { return 'all open orders'; }
/// Shows one order. /// The document says that `id` is a whole number of 0 or more, because the parameter is a `uint`. #[Core\Route(path: '/orders/{id}', method: Core\Http\Method::Get)] #[Core\Access(allow: Core\Audience::Public)] public function show(uint $id): string { return 'order ' . $id; }}
// Run with `nvs run`, the program only prints a link to one order: `/orders/1042`.echo Core\Router::url(Orders::show, ['id' => 1042]), "\n";/orders/1042