oaspect 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 oaspect contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,19 @@
1
+ # oaspect
2
+
3
+ Interactive API reference for OpenAPI 3.x and Swagger 2.0 documents (JSON or YAML).
4
+
5
+ ```html
6
+ <script src="https://cdn.jsdelivr.net/npm/oaspect" data-spec-url="/openapi.yaml"></script>
7
+ ```
8
+
9
+ ```bash
10
+ npx oaspect build openapi.yaml -o docs.html
11
+ npx oaspect serve openapi.yaml
12
+ ```
13
+
14
+ ```js
15
+ import { createProxyHandler } from "oaspect/server";
16
+ ```
17
+
18
+ See the [main README](https://github.com/oaspect/oaspect) for the web component,
19
+ React usage, configuration and translations. MIT licensed.
@@ -0,0 +1,127 @@
1
+ #!/usr/bin/env node
2
+ // oaspect CLI: build a self-contained HTML reference or serve a live preview.
3
+ import { readFile, writeFile } from "node:fs/promises";
4
+ import { basename, dirname, extname, resolve } from "node:path";
5
+ import { fileURLToPath } from "node:url";
6
+ import { parseArgs } from "node:util";
7
+ import { renderHtml } from "../src/html.js";
8
+ import { serve } from "../src/serve.js";
9
+ import { isUrl, loadSpec } from "../src/spec.js";
10
+
11
+ const root = resolve(dirname(fileURLToPath(import.meta.url)), "..");
12
+ const bundlePath = resolve(root, "dist/oaspect.js");
13
+
14
+ const HELP = `oaspect — interactive API reference for OpenAPI documents
15
+
16
+ Usage:
17
+ oaspect build <spec> [-o docs.html] [options] Write a self-contained HTML file
18
+ oaspect serve <spec> [--port 8080] [options] Live preview with a "Try" proxy
19
+
20
+ <spec> is a JSON or YAML file path or an http(s) URL (OpenAPI 3.x or Swagger 2.0).
21
+
22
+ Options:
23
+ -o, --output <file> build: output file (default: <spec name>.html)
24
+ --title <text> page and header title (default: info.title)
25
+ --locale <code> UI language: en, tr, ar… (default: en)
26
+ --theme <light|dark> force a theme
27
+ --logo <url> header logo image
28
+ --no-prerender build: render in the browser only (smaller file, no
29
+ content without JavaScript or for search engines)
30
+ --port <number> serve: port (default: 8080)
31
+ --host <address> serve: bind address (default: 127.0.0.1)
32
+ --proxy-hosts <list> serve: hosts the proxy may call (default: any, loopback only)
33
+ --no-proxy serve: disable the "Try" proxy
34
+ -h, --help show this help
35
+ -v, --version show the version
36
+ `;
37
+
38
+ function fail(message) {
39
+ console.error(`oaspect: ${message}`);
40
+ process.exit(1);
41
+ }
42
+
43
+ const { values, positionals } = (() => {
44
+ try {
45
+ return parseArgs({
46
+ allowPositionals: true,
47
+ options: {
48
+ output: { type: "string", short: "o" },
49
+ title: { type: "string" },
50
+ locale: { type: "string" },
51
+ theme: { type: "string" },
52
+ logo: { type: "string" },
53
+ port: { type: "string" },
54
+ host: { type: "string" },
55
+ "proxy-hosts": { type: "string" },
56
+ "no-proxy": { type: "boolean" },
57
+ "no-prerender": { type: "boolean" },
58
+ help: { type: "boolean", short: "h" },
59
+ version: { type: "boolean", short: "v" },
60
+ },
61
+ });
62
+ } catch (error) {
63
+ fail(error.message);
64
+ }
65
+ })();
66
+
67
+ if (values.version) {
68
+ const { version } = JSON.parse(await readFile(resolve(root, "package.json"), "utf8"));
69
+ console.log(version);
70
+ process.exit(0);
71
+ }
72
+
73
+ const [command, source] = positionals;
74
+ if (values.help || !command) {
75
+ console.log(HELP);
76
+ process.exit(values.help ? 0 : 1);
77
+ }
78
+ if (!["build", "serve"].includes(command)) fail(`unknown command "${command}". Run oaspect --help.`);
79
+ if (!source) fail(`${command} needs a spec file or URL.`);
80
+ if (values.theme && !["light", "dark"].includes(values.theme)) fail("--theme must be light or dark.");
81
+
82
+ const config = {
83
+ ...(values.title ? { title: values.title } : {}),
84
+ ...(values.locale ? { defaultLocale: values.locale } : {}),
85
+ ...(values.theme ? { theme: values.theme } : {}),
86
+ ...(values.logo ? { logo: values.logo } : {}),
87
+ };
88
+
89
+ try {
90
+ if (command === "build") {
91
+ const spec = await loadSpec(source);
92
+ const output = resolve(values.output ?? `${basename(isUrl(source) ? new URL(source).pathname : source, extname(source)) || "openapi"}.html`);
93
+ // No "Source" menu or ?url= loading in a static file; there is no proxy
94
+ // either. Prerendered pages render every section (no lazy placeholders)
95
+ // so the HTML holds the whole reference.
96
+ const pageConfig = { ...config, urlParam: null, features: { sourceMenu: false }, ...(values["no-prerender"] ? {} : { lazy: false }) };
97
+ let prerendered = {};
98
+ if (!values["no-prerender"]) {
99
+ const { renderToHtml, css } = await import("../dist/ssr.js");
100
+ prerendered = { markup: renderToHtml({ ...pageConfig, spec }), css };
101
+ }
102
+ const html = renderHtml({
103
+ title: values.title ?? spec.info?.title ?? "API Reference",
104
+ config: pageConfig,
105
+ spec,
106
+ script: { inline: await readFile(bundlePath, "utf8") },
107
+ ...prerendered,
108
+ });
109
+ await writeFile(output, html);
110
+ console.log(`Wrote ${output} (${(Buffer.byteLength(html) / 1024).toFixed(0)} KB)`);
111
+ } else {
112
+ const proxyHosts = values["proxy-hosts"]?.split(",").map((host) => host.trim()).filter(Boolean);
113
+ const port = values.port ? Number.parseInt(values.port, 10) : 8080;
114
+ if (!Number.isInteger(port) || port < 0 || port > 65535) fail("--port must be a number between 0 and 65535.");
115
+ const { url } = await serve({
116
+ source,
117
+ port,
118
+ host: values.host ?? "127.0.0.1",
119
+ proxy: values["no-proxy"] ? false : proxyHosts?.length ? proxyHosts : true,
120
+ bundlePath,
121
+ config,
122
+ });
123
+ console.log(`oaspect serving ${source} at ${url}`);
124
+ }
125
+ } catch (error) {
126
+ fail(error.message);
127
+ }