@mednours/backon 0.4.0 → 0.5.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 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
- `apiReferenceHtml(specUrl, title?)`, the page `serveApiDocs` renders, 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:
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, apiReferenceHtml } from "@mednours/backon";
189
+ import { generateOpenApiSpec, apiReferenceHtmlInline } from "@mednours/backon";
190
190
  import { app } from "./src/app";
191
191
 
192
- writeFileSync("openapi.json", JSON.stringify(generateOpenApiSpec(app, { title: "My API" }), null, 2));
193
- writeFileSync("docs.html", apiReferenceHtml("./openapi.json", "My API"));
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.4.0",
3
+ "version": "0.5.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",
@@ -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";
@@ -1,5 +1,10 @@
1
1
  import type { Express } from "express";
2
- import { buildOpenApiDocument, apiReferenceHtml, type OpenApiOptions, type CollectedRoute } from "../openapi-core";
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. Call after every other route is registered on `app` —
19
- * routes added afterward won't be reflected in either.
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) => res.type("html").send(apiReferenceHtml(specPath, options.title)));
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
 
@@ -97,8 +97,7 @@ export function buildOpenApiDocument(routes: CollectedRoute[], options: OpenApiO
97
97
  };
98
98
  }
99
99
 
100
- /** A minimal page that renders an OpenAPI document with Scalar, loaded from a CDN rather than bundled as a dependency. */
101
- export function apiReferenceHtml(specUrl: string, title = "API Reference"): string {
100
+ function pageShell(title: string, config: Record<string, unknown>): string {
102
101
  return `<!doctype html>
103
102
  <html>
104
103
  <head>
@@ -107,9 +106,39 @@ export function apiReferenceHtml(specUrl: string, title = "API Reference"): stri
107
106
  <meta name="viewport" content="width=device-width, initial-scale=1" />
108
107
  </head>
109
108
  <body>
110
- <script id="api-reference" data-url="${specUrl}"></script>
109
+ <div id="api-reference"></div>
111
110
  <script src="https://cdn.jsdelivr.net/npm/@scalar/api-reference"></script>
111
+ <script>
112
+ Scalar.createApiReference("#api-reference", ${JSON.stringify(config)});
113
+ </script>
112
114
  </body>
113
115
  </html>
114
116
  `;
115
117
  }
118
+
119
+ /**
120
+ * A minimal page that renders an OpenAPI document with Scalar (loaded from a
121
+ * CDN, not bundled as a dependency), fetching the document from `specUrl` —
122
+ * a real HTTP path the page can reach at render time, e.g. what
123
+ * `serveApiDocs()` mounts. Uses `Scalar.createApiReference()`, the
124
+ * documented initialization API — an older pattern (a magic script tag with
125
+ * a `data-url` attribute) is no longer reliable across Scalar versions and
126
+ * fails outright when the page is opened as a local file rather than served
127
+ * over HTTP, since fetching one `file://` URL from another is blocked by the
128
+ * browser. For that case, use `apiReferenceHtmlInline()` instead.
129
+ */
130
+ export function apiReferenceHtml(specUrl: string, title = "API Reference"): string {
131
+ return pageShell(title, { url: specUrl });
132
+ }
133
+
134
+ /**
135
+ * Same rendered page as `apiReferenceHtml()`, but with the OpenAPI document
136
+ * embedded directly in the page instead of fetched from a URL — no request
137
+ * happens at render time, so this works when the file is opened straight
138
+ * from disk (double-clicked, `file://`) and not just when served over HTTP.
139
+ * This is what a static-generation script (writing the page to disk rather
140
+ * than serving it from a running app) should use.
141
+ */
142
+ export function apiReferenceHtmlInline(spec: Record<string, unknown>, title = "API Reference"): string {
143
+ return pageShell(title, { content: spec });
144
+ }
package/src/openapi.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { Hono } from "hono";
2
- import { buildOpenApiDocument, apiReferenceHtml, type OpenApiOptions, type CollectedRoute } from "./openapi-core";
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. Call after every other route is registered — same
30
- * registration-order rule as any other route reading from `app.routes`.
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(apiReferenceHtml(specPath, options.title)));
40
+ app.get(path, (c) => c.html(apiReferenceHtmlInline(generateOpenApiSpec(app, options), options.title)));
38
41
  }