8000 feat: Add `app.getOpenApiSpec` so you can get the spec without starting · aklinker1/zeta@693477c · GitHub
[go: up one dir, main page]

Skip to content

Commit 693477c

Browse files
committed
feat: Add app.getOpenApiSpec so you can get the spec without starting
the app Useful in scripts
1 parent e309d7a commit 693477c

3 files changed

Lines changed: 23 additions & 0 deletions

File tree

‎README.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,16 @@ const User = z
558558

559559
When building your app's spec, Zeta will find these `ref` properties and move the object schemas into `components.schemas`.
560560

561+
### Getting the OpenAPI Spec
562+
563+
You can get the OpenAPI spec by calling `app.getOpenApiSpec()`. You do not need to listen to any ports or fetch the `/openapi.json` endpoint. It's a simple function call away.
564+
565+
```ts
566+
const app = createApp(...)
567+
568+
app.getOpenApiSpec() // { version: "3.1", info: { ... }, paths: { ... }, ... }
569+
```
570+
561571
## Composing Multiple Apps
562572

563573
By default, an app's context (hooks, decorators) is isolated. To make a child app's context available to its parent, you must explicitly chain `.export()` at the end of its definition. This effectively merges its isolated lifecycle hooks into the parent's.

‎src/app.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,13 @@ export function createApp<TPrefix extends BasePrefix = "">(
210210
};
211211
},
212212

213+
getOpenApiSpec: () => {
214+
const res = buildOpenApiDocs(options, app);
215+
if (res.type === "error") throw res.error;
216+
217+
return res.spec;
218+
},
219+
213220
export: () => {
214221
app["~zeta"].exported = true;
215222
return app as any;

‎src/types.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ export interface App<TAppData extends AppData = AppData> {
5858
*/
5959
build: () => ServerSideFetch;
6060

61+
/**
62+
* Returns your application's OpenAPI spec. You do not need to listen to a
63+
* port to call this method.
64+
*/
65+
getOpenApiSpec: () => OpenAPI.Document;
66+
6167
/**
6268
* Mark the app as "exported". When an exported app is `use`d by a
6369
* parent app, the parent app will inherit all of it's hooks and modifiers.

0 commit comments

Comments
 (0)
0