Skip to content

Commit a11afb4

Browse files
committed
feat: add OpenAPI spec
1 parent d9c89e1 commit a11afb4

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/bootstrap.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Server } from "hyper-express";
33
import { dispatchHandlerController } from "./dispatch-handler/controller";
44
import { getConfig } from "./utils/config";
55
import { getLogger } from "./utils/logger";
6+
import { getOpenApiSpec } from "./utils/openapi";
67

78
/**
89
* Describes the result of the bootstrap function.
@@ -32,6 +33,20 @@ export const bootstrap = (async (): Promise<BootstrapResult> => {
3233
// Register the handler controller.
3334
server.post("/dispatch", dispatchHandlerController);
3435

36+
// Serve the OpenAPI specification as a static file.
37+
server.get("/openapi.yaml", async (_, res) => {
38+
const content = await getOpenApiSpec();
39+
40+
res
41+
.status(200)
42+
.header(
43+
"cache-control",
44+
"public, max-age 172800, stale-while-revalidate 172800"
45+
)
46+
.header("content-type", "application/yaml")
47+
.send(content);
48+
});
49+
3550
// Set up a global not found handler
3651
server.set_not_found_handler((_, res) => {
3752
res.status(404).json({

src/utils/openapi.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import fsp from "fs/promises";
2+
import path from "path";
3+
4+
let _cache: string | null = null;
5+
6+
/**
7+
* Provides the OpenAPI specification as a string in YAML format.
8+
*/
9+
export async function getOpenApiSpec(): Promise<string> {
10+
if (!_cache) {
11+
const specPath = path.join(__dirname, "../assets/openapi/openapi.yaml");
12+
13+
try {
14+
_cache = await fsp.readFile(specPath, "utf-8");
15+
} catch (err) {
16+
console.error("Failed to read OpenAPI spec file", err);
17+
18+
throw err;
19+
}
20+
}
21+
22+
return _cache;
23+
}

0 commit comments

Comments
 (0)