@astro-spec/ui 0.0.0-0 → 1.0.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 otoneko.
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,41 @@
1
+ # @astro-spec/ui
2
+
3
+ > Serve an API reference page for Astro Actions with Scalar
4
+
5
+ Adds a [Scalar](https://scalar.com) API Reference page for the OpenAPI document that [`@astro-spec/openapi`](../openapi#readme) serves. You can read the actions and try them from the browser.
6
+
7
+ ## Install
8
+
9
+ ```sh
10
+ npm install @astro-spec/ui @astro-spec/openapi
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```ts
16
+ // astro.config.mjs
17
+ import astroSpec from '@astro-spec/openapi'
18
+ import astroSpecUi from '@astro-spec/ui'
19
+ import { defineConfig } from 'astro/config'
20
+
21
+ export default defineConfig({
22
+ integrations: [astroSpec(), astroSpecUi()],
23
+ })
24
+ ```
25
+
26
+ Run `astro dev` and open `/docs`.
27
+
28
+ ## Options
29
+
30
+ | Option | Default | Description |
31
+ | --- | --- | --- |
32
+ | `path` | `'/docs'` | Where the page is served. |
33
+ | `specPath` | `'/openapi.json'` | The `path` option of `@astro-spec/openapi`. |
34
+ | `title` | `'API Reference'` | The page title. |
35
+ | `production` | `false` | Whether the built site serves the page: `false`, `'prerender'` or `'runtime'`, as in `@astro-spec/openapi`. Set the same value there, or the page has no document to load. |
36
+ | `scriptUrl` | Scalar 1.x on jsDelivr | The ES module that exports Scalar's `createApiReference`. Point it at a self-hosted copy if your site cannot load scripts from a CDN. |
37
+ | `configuration` | `{}` | Extra [Scalar configuration](https://scalar.com/products/api-references/configuration), such as `theme`. |
38
+
39
+ ## License
40
+
41
+ [MIT](https://github.com/betterlib/astro-spec/blob/main/LICENSE)
@@ -0,0 +1,46 @@
1
+ import { AstroIntegration } from "astro";
2
+ type AstroSpecUiOptions = {
3
+ /** Where the reference page is served. Defaults to `/docs`. */
4
+ path?: string;
5
+ /** The path `@astro-spec/openapi` serves the document at. Defaults to `/openapi.json`. */
6
+ specPath?: string;
7
+ /** The page title. Defaults to `API Reference`. */
8
+ title?: string;
9
+ /**
10
+ * Whether the built site serves the page, as in `@astro-spec/openapi`. `astro dev` always
11
+ * serves it. Set `production` of `@astro-spec/openapi` too, or the page has no document to show.
12
+ */
13
+ production?: false | 'prerender' | 'runtime';
14
+ /** Defaults to the latest Scalar 1.x on jsDelivr. */
15
+ scriptUrl?: string;
16
+ /** Extra [Scalar configuration](https://scalar.com/products/api-references/configuration). */
17
+ configuration?: Record<string, unknown>;
18
+ };
19
+ /**
20
+ * Serves a [Scalar](https://scalar.com) API Reference page for the document that
21
+ * `@astro-spec/openapi` serves.
22
+ *
23
+ * @example
24
+ * ```ts
25
+ * // astro.config.mjs
26
+ * import astroSpec from '@astro-spec/openapi'
27
+ * import astroSpecUi from '@astro-spec/ui'
28
+ *
29
+ * export default defineConfig({
30
+ * integrations: [astroSpec(), astroSpecUi({ path: '/docs' })],
31
+ * })
32
+ * ```
33
+ */
34
+ declare const astroSpecUi: ({ path, specPath, title, production, scriptUrl, configuration }?: AstroSpecUiOptions) => AstroIntegration;
35
+ type ReferencePageOptions = {
36
+ title: string;
37
+ /** The URL of the OpenAPI document, base path included. */
38
+ specUrl: string;
39
+ /** The ES module that exports Scalar's `createApiReference`. */
40
+ scriptUrl: string;
41
+ /** Extra Scalar configuration, merged over `{ url: specUrl }`. */
42
+ configuration: Record<string, unknown>;
43
+ };
44
+ /** The inline module script that mounts Scalar's API Reference on `#app`. */
45
+ export declare const referencePageScript: ({ specUrl, scriptUrl, configuration }: ReferencePageOptions) => string;
46
+ export { type AstroSpecUiOptions, type ReferencePageOptions, astroSpecUi as default };
package/dist/index.mjs ADDED
@@ -0,0 +1,56 @@
1
+ // src/integration.ts
2
+ const OPTIONS_MODULE_ID = "virtual:astro-spec/ui-options";
3
+ const optionsVitePlugin = (options) => ({
4
+ name: "astro-spec:ui-options",
5
+ resolveId: (id) => id === OPTIONS_MODULE_ID ? `\0${OPTIONS_MODULE_ID}` : void 0,
6
+ load: (id) => id === `\0${OPTIONS_MODULE_ID}` ? `export default ${JSON.stringify(options)}` : void 0
7
+ });
8
+ /**
9
+ * Serves a [Scalar](https://scalar.com) API Reference page for the document that
10
+ * `@astro-spec/openapi` serves.
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * // astro.config.mjs
15
+ * import astroSpec from '@astro-spec/openapi'
16
+ * import astroSpecUi from '@astro-spec/ui'
17
+ *
18
+ * export default defineConfig({
19
+ * integrations: [astroSpec(), astroSpecUi({ path: '/docs' })],
20
+ * })
21
+ * ```
22
+ */
23
+ const astroSpecUi = ({ path = "/docs", specPath = "/openapi.json", title = "API Reference", production = false, scriptUrl = "https://cdn.jsdelivr.net/npm/@scalar/api-reference@1/esm.js", configuration = {} } = {}) => ({
24
+ name: "@astro-spec/ui",
25
+ hooks: { "astro:config:setup": ({ config, command, injectRoute, updateConfig }) => {
26
+ const specUrl = `${config.base.replace(/\/$/, "")}${specPath}`;
27
+ updateConfig({ vite: {
28
+ plugins: [optionsVitePlugin({
29
+ title,
30
+ specUrl,
31
+ scriptUrl,
32
+ configuration
33
+ })],
34
+ ssr: { noExternal: ["@astro-spec/ui"] }
35
+ } });
36
+ if (command === "dev") injectRoute({
37
+ pattern: path,
38
+ entrypoint: "@astro-spec/ui/reference-page.astro",
39
+ prerender: false
40
+ });
41
+ if (command === "build" && production) injectRoute({
42
+ pattern: path,
43
+ entrypoint: "@astro-spec/ui/reference-page.astro",
44
+ prerender: production === "prerender"
45
+ });
46
+ } }
47
+ });
48
+ // src/render-reference-page.ts
49
+ const toScriptLiteral = (value) => JSON.stringify(value).replaceAll("<", "\\u003c");
50
+ /** The inline module script that mounts Scalar's API Reference on `#app`. */
51
+ const referencePageScript = ({ specUrl, scriptUrl, configuration }) => `import { createApiReference } from ${toScriptLiteral(scriptUrl)}
52
+ createApiReference('#app', ${toScriptLiteral({
53
+ url: specUrl,
54
+ ...configuration
55
+ })})`;
56
+ export { astroSpecUi as default, referencePageScript };
@@ -0,0 +1,18 @@
1
+ ---
2
+ import options from 'virtual:astro-spec/ui-options'
3
+ // Shipped next to the bundled index.mjs in dist.
4
+ import { referencePageScript } from './index.mjs'
5
+ ---
6
+
7
+ <!doctype html>
8
+ <html lang="en">
9
+ <head>
10
+ <meta charset="utf-8" />
11
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
12
+ <title>{options.title}</title>
13
+ </head>
14
+ <body>
15
+ <div id="app"></div>
16
+ <script type="module" is:inline set:html={referencePageScript(options)} />
17
+ </body>
18
+ </html>
package/package.json CHANGED
@@ -1,10 +1,54 @@
1
1
  {
2
2
  "name": "@astro-spec/ui",
3
- "version": "0.0.0-0",
4
- "license": "MIT",
3
+ "version": "1.0.0",
4
+ "description": "Serve an API reference page for Astro Actions with Scalar",
5
+ "keywords": [
6
+ "astro",
7
+ "astro-integration",
8
+ "withastro",
9
+ "openapi",
10
+ "astro-actions"
11
+ ],
12
+ "homepage": "https://github.com/betterlib/astro-spec/tree/main/packages/ui#readme",
13
+ "bugs": {
14
+ "url": "https://github.com/betterlib/astro-spec/issues"
15
+ },
5
16
  "repository": {
6
17
  "type": "git",
7
18
  "url": "git+https://github.com/betterlib/astro-spec.git",
8
19
  "directory": "packages/ui"
20
+ },
21
+ "license": "MIT",
22
+ "author": "otoneko.",
23
+ "type": "module",
24
+ "exports": {
25
+ ".": {
26
+ "types": "./dist/index.d.mts",
27
+ "default": "./dist/index.mjs"
28
+ },
29
+ "./reference-page.astro": "./dist/reference-page.astro",
30
+ "./package.json": "./package.json"
31
+ },
32
+ "files": [
33
+ "dist"
34
+ ],
35
+ "engines": {
36
+ "node": ">=22.12.0"
37
+ },
38
+ "peerDependencies": {
39
+ "astro": "^4.15.0 || ^5.0.0 || ^6.0.0 || ^7.0.0",
40
+ "@astro-spec/openapi": "1.0.0"
41
+ },
42
+ "publishConfig": {
43
+ "access": "public",
44
+ "provenance": true
45
+ },
46
+ "devDependencies": {
47
+ "astro": "^7.4.14",
48
+ "@astro-spec/openapi": "1.0.0"
49
+ },
50
+ "scripts": {
51
+ "build": "tsdown",
52
+ "typecheck": "tsc --noEmit"
9
53
  }
10
- }
54
+ }