@mednours/backon 0.4.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -4
- package/package.json +1 -1
- package/src/express/index.ts +1 -1
- package/src/express/openapi.ts +14 -4
- package/src/index.ts +1 -1
- package/src/openapi-core.ts +37 -3
- package/src/openapi.ts +7 -4
package/README.md
CHANGED
|
@@ -182,17 +182,20 @@ serveApiDocs(app, "/docs", { title: "My API" });
|
|
|
182
182
|
// GET /docs -> a rendered API reference page
|
|
183
183
|
```
|
|
184
184
|
|
|
185
|
-
`
|
|
185
|
+
`apiReferenceHtmlInline(spec, title?)`, the same rendered page `serveApiDocs` uses, is also exported directly — useful for a build step that writes the spec and the reference page to static files instead of serving them from a running app. It embeds the document straight into the page rather than pointing at a URL, so the result works when opened directly from disk (double-clicked, `file://`), not only when served over HTTP — a page that instead fetches a sibling `openapi.json` fails there, since a browser blocks one `file://` URL from fetching another:
|
|
186
186
|
|
|
187
187
|
```ts
|
|
188
188
|
import { writeFileSync } from "node:fs";
|
|
189
|
-
import { generateOpenApiSpec,
|
|
189
|
+
import { generateOpenApiSpec, apiReferenceHtmlInline } from "@mednours/backon";
|
|
190
190
|
import { app } from "./src/app";
|
|
191
191
|
|
|
192
|
-
|
|
193
|
-
writeFileSync("
|
|
192
|
+
const spec = generateOpenApiSpec(app, { title: "My API" });
|
|
193
|
+
writeFileSync("openapi.json", JSON.stringify(spec, null, 2));
|
|
194
|
+
writeFileSync("docs.html", apiReferenceHtmlInline(spec, "My API"));
|
|
194
195
|
```
|
|
195
196
|
|
|
197
|
+
`apiReferenceHtml(specUrl, title?)` is the URL-based counterpart, for a reference page that fetches its spec from a real HTTP path instead — this is what `serveApiDocs` used to use for its own page, before switching to the inline version to remove the extra request. Still useful directly for pointing a reference page at a spec hosted somewhere else.
|
|
198
|
+
|
|
196
199
|
On Express, routes are read from a registry `createApp()` and `Router()` (exported from `@mednours/backon/express`) both populate as routes are added — using Express's own unwrapped `express.Router()` instead means those routes won't appear in the generated spec.
|
|
197
200
|
|
|
198
201
|
```ts
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mednours/backon",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Opinionated backend framework wrapper for Bun. Wraps Hono (default) or Express (via @mednours/backon/express) with the same baked-in security, logging, and validation defaults.",
|
|
6
6
|
"license": "MIT",
|
package/src/express/index.ts
CHANGED
|
@@ -42,4 +42,4 @@ export type { UploadOptions, UploadHandler } from "./upload";
|
|
|
42
42
|
|
|
43
43
|
export { generateOpenApiSpec, serveApiDocs } from "./openapi";
|
|
44
44
|
export type { OpenApiOptions } from "../openapi-core";
|
|
45
|
-
export { apiReferenceHtml } from "../openapi-core";
|
|
45
|
+
export { apiReferenceHtml, apiReferenceHtmlInline } from "../openapi-core";
|
package/src/express/openapi.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import type { Express } from "express";
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
buildOpenApiDocument,
|
|
4
|
+
apiReferenceHtmlInline,
|
|
5
|
+
type OpenApiOptions,
|
|
6
|
+
type CollectedRoute,
|
|
7
|
+
} from "../openapi-core";
|
|
3
8
|
import { collectRoutes, type TrackedRouter } from "./router-registry";
|
|
4
9
|
|
|
5
10
|
/**
|
|
@@ -15,13 +20,18 @@ export function generateOpenApiSpec(app: Express, options: OpenApiOptions = {}):
|
|
|
15
20
|
|
|
16
21
|
/**
|
|
17
22
|
* Mounts two routes: the OpenAPI document itself as JSON, and a page that
|
|
18
|
-
* renders it.
|
|
19
|
-
*
|
|
23
|
+
* renders it. The page embeds the document directly rather than fetching it
|
|
24
|
+
* from the JSON route — one less request, and it keeps working if the JSON
|
|
25
|
+
* route is ever removed or renamed. Call after every other route is
|
|
26
|
+
* registered on `app` — routes added afterward won't be reflected in
|
|
27
|
+
* either.
|
|
20
28
|
*
|
|
21
29
|
* serveApiDocs(app, "/docs", { title: "My API" });
|
|
22
30
|
*/
|
|
23
31
|
export function serveApiDocs(app: Express, path = "/docs", options: OpenApiOptions = {}): void {
|
|
24
32
|
const specPath = `${path}/openapi.json`;
|
|
25
33
|
app.get(specPath, (_req, res) => res.json(generateOpenApiSpec(app, options)));
|
|
26
|
-
app.get(path, (_req, res) =>
|
|
34
|
+
app.get(path, (_req, res) =>
|
|
35
|
+
res.type("html").send(apiReferenceHtmlInline(generateOpenApiSpec(app, options), options.title))
|
|
36
|
+
);
|
|
27
37
|
}
|
package/src/index.ts
CHANGED
|
@@ -30,7 +30,7 @@ export type { UploadOptions, UploadHandler } from "./upload";
|
|
|
30
30
|
|
|
31
31
|
export { generateOpenApiSpec, serveApiDocs } from "./openapi";
|
|
32
32
|
export type { OpenApiOptions } from "./openapi";
|
|
33
|
-
export { apiReferenceHtml } from "./openapi-core";
|
|
33
|
+
export { apiReferenceHtml, apiReferenceHtmlInline } from "./openapi-core";
|
|
34
34
|
|
|
35
35
|
export type { BackonContext, Env } from "./types";
|
|
36
36
|
|
package/src/openapi-core.ts
CHANGED
|
@@ -97,8 +97,12 @@ export function buildOpenApiDocument(routes: CollectedRoute[], options: OpenApiO
|
|
|
97
97
|
};
|
|
98
98
|
}
|
|
99
99
|
|
|
100
|
-
|
|
101
|
-
|
|
100
|
+
function pageShell(title: string, config: Record<string, unknown>): string {
|
|
101
|
+
// showDeveloperTools defaults to "localhost" in Scalar itself, which shows
|
|
102
|
+
// a Configure/Share/Deploy toolbar above the reference — promotion for
|
|
103
|
+
// Scalar's own hosted account, unrelated to this document. Off by default
|
|
104
|
+
// here since it renders whether or not the project uses Scalar's service.
|
|
105
|
+
const fullConfig = { showDeveloperTools: "never", ...config };
|
|
102
106
|
return `<!doctype html>
|
|
103
107
|
<html>
|
|
104
108
|
<head>
|
|
@@ -107,9 +111,39 @@ export function apiReferenceHtml(specUrl: string, title = "API Reference"): stri
|
|
|
107
111
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
108
112
|
</head>
|
|
109
113
|
<body>
|
|
110
|
-
<
|
|
114
|
+
<div id="api-reference"></div>
|
|
111
115
|
<script src="https://cdn.jsdelivr.net/npm/@scalar/api-reference"></script>
|
|
116
|
+
<script>
|
|
117
|
+
Scalar.createApiReference("#api-reference", ${JSON.stringify(fullConfig)});
|
|
118
|
+
</script>
|
|
112
119
|
</body>
|
|
113
120
|
</html>
|
|
114
121
|
`;
|
|
115
122
|
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* A minimal page that renders an OpenAPI document with Scalar (loaded from a
|
|
126
|
+
* CDN, not bundled as a dependency), fetching the document from `specUrl` —
|
|
127
|
+
* a real HTTP path the page can reach at render time, e.g. what
|
|
128
|
+
* `serveApiDocs()` mounts. Uses `Scalar.createApiReference()`, the
|
|
129
|
+
* documented initialization API — an older pattern (a magic script tag with
|
|
130
|
+
* a `data-url` attribute) is no longer reliable across Scalar versions and
|
|
131
|
+
* fails outright when the page is opened as a local file rather than served
|
|
132
|
+
* over HTTP, since fetching one `file://` URL from another is blocked by the
|
|
133
|
+
* browser. For that case, use `apiReferenceHtmlInline()` instead.
|
|
134
|
+
*/
|
|
135
|
+
export function apiReferenceHtml(specUrl: string, title = "API Reference"): string {
|
|
136
|
+
return pageShell(title, { url: specUrl });
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Same rendered page as `apiReferenceHtml()`, but with the OpenAPI document
|
|
141
|
+
* embedded directly in the page instead of fetched from a URL — no request
|
|
142
|
+
* happens at render time, so this works when the file is opened straight
|
|
143
|
+
* from disk (double-clicked, `file://`) and not just when served over HTTP.
|
|
144
|
+
* This is what a static-generation script (writing the page to disk rather
|
|
145
|
+
* than serving it from a running app) should use.
|
|
146
|
+
*/
|
|
147
|
+
export function apiReferenceHtmlInline(spec: Record<string, unknown>, title = "API Reference"): string {
|
|
148
|
+
return pageShell(title, { content: spec });
|
|
149
|
+
}
|
package/src/openapi.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Hono } from "hono";
|
|
2
|
-
import { buildOpenApiDocument,
|
|
2
|
+
import { buildOpenApiDocument, apiReferenceHtmlInline, type OpenApiOptions, type CollectedRoute } from "./openapi-core";
|
|
3
3
|
import type { RouteHandlerFn } from "./define-route";
|
|
4
4
|
|
|
5
5
|
export type { OpenApiOptions };
|
|
@@ -26,13 +26,16 @@ export function generateOpenApiSpec(app: Hono<any>, options: OpenApiOptions = {}
|
|
|
26
26
|
|
|
27
27
|
/**
|
|
28
28
|
* Mounts two routes: the OpenAPI document itself as JSON, and a page that
|
|
29
|
-
* renders it.
|
|
30
|
-
*
|
|
29
|
+
* renders it. The page embeds the document directly rather than fetching it
|
|
30
|
+
* from the JSON route — one less request, and it keeps working if the JSON
|
|
31
|
+
* route is ever removed or renamed. Call after every other route is
|
|
32
|
+
* registered — same registration-order rule as any other route reading from
|
|
33
|
+
* `app.routes`.
|
|
31
34
|
*
|
|
32
35
|
* serveApiDocs(app, "/docs", { title: "My API" });
|
|
33
36
|
*/
|
|
34
37
|
export function serveApiDocs(app: Hono<any>, path = "/docs", options: OpenApiOptions = {}): void {
|
|
35
38
|
const specPath = `${path}/openapi.json`;
|
|
36
39
|
app.get(specPath, (c) => c.json(generateOpenApiSpec(app, options)));
|
|
37
|
-
app.get(path, (c) => c.html(
|
|
40
|
+
app.get(path, (c) => c.html(apiReferenceHtmlInline(generateOpenApiSpec(app, options), options.title)));
|
|
38
41
|
}
|