-
-
Notifications
You must be signed in to change notification settings - Fork 23
feat(web): add the dashboard server façade + wire Sidequest.start({ dashboard }) #203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
merencia
wants to merge
1
commit into
feat/web-dashboard-app
Choose a base branch
from
feat/web-server-facade
base: feat/web-dashboard-app
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| import createConfig from "../../rollup.config.base.js"; | ||
| import pkg from "./package.json" with { type: "json" }; | ||
|
|
||
| // Node build for the management API (`@sidequest/web/api`). The React UI library keeps | ||
| // its own Vite build (see vite.lib.config.ts); both write into dist/ without clobbering. | ||
| export default createConfig(pkg, "src/api/index.ts"); | ||
| // Node builds for the management API (`@sidequest/web/api`) and the dashboard façade | ||
| // (`@sidequest/web/server`). The React UI library keeps its own Vite build (see | ||
| // vite.lib.config.ts) and the SPA its own (vite.app.config.ts); all write into dist/ | ||
| // without clobbering (preserveModules mirrors src/ → dist/api, dist/server, …). | ||
| export default createConfig(pkg, ["src/api/index.ts", "src/server/index.ts"]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| export {}; | ||
|
|
||
| declare global { | ||
| interface Window { | ||
| /** Base path the dashboard SPA is served under, injected by the `@sidequest/web` façade. */ | ||
| __SQ_DASHBOARD_BASE__?: string; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,15 @@ | ||
| import { createRoot } from "react-dom/client"; | ||
| import { createApiClient } from "../client"; | ||
| import "../../ui/styles.css"; | ||
| import "../dashboard.css"; | ||
| import { DashboardApp } from "./app"; | ||
|
|
||
| // The façade injects the base path the dashboard is served under (default "/"); the | ||
| // management API lives at `<base>/api`, so point the client there. | ||
| const base = window.__SQ_DASHBOARD_BASE__ ?? "/"; | ||
| const apiBase = `${base.replace(/\/$/, "")}/api`; | ||
|
|
||
| const root = document.getElementById("root"); | ||
| if (root) createRoot(root).render(<DashboardApp />); | ||
| if (root) { | ||
| createRoot(root).render(<DashboardApp client={createApiClient(apiBase)} />); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| // Dashboard SPA fixture asset — served as a static file by serve.test.ts. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <!doctype html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <title>Sidequest</title> | ||
| </head> | ||
| <body> | ||
| <div id="root"></div> | ||
| <script type="module" src="./assets/app.js"></script> | ||
| </body> | ||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export { createDashboardApp, normalizeBase, serveDashboard } from "./serve"; | ||
| export type { DashboardAuth, DashboardOptions, DashboardServer } from "./serve"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import { resolve } from "node:path"; | ||
| import { mockBackend } from "../api/testing/mock-backend"; | ||
| import { createDashboardApp, normalizeBase } from "./serve"; | ||
|
|
||
| const STATIC_DIR = resolve(import.meta.dirname, "__fixtures__/app"); | ||
|
|
||
| describe("normalizeBase", () => { | ||
| it("normalizes to '' (root) or '/prefix'", () => { | ||
| expect(normalizeBase()).toBe(""); | ||
| expect(normalizeBase("")).toBe(""); | ||
| expect(normalizeBase("/")).toBe(""); | ||
| expect(normalizeBase("admin")).toBe("/admin"); | ||
| expect(normalizeBase("/admin/")).toBe("/admin"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("createDashboardApp", () => { | ||
| const backend = mockBackend(); | ||
| const app = () => | ||
| createDashboardApp({ backend, staticDir: STATIC_DIR, version: "9.9.9", driver: "@sidequest/sqlite-backend" }); | ||
|
|
||
| it("routes the management API under /api", async () => { | ||
| const res = await app().request("/api/overview"); | ||
| expect(res.status).toBe(200); | ||
| expect(await res.json()).toMatchObject({ total: 0 }); | ||
| }); | ||
|
|
||
| it("serves the SPA shell with the injected base at root", async () => { | ||
| const res = await app().request("/"); | ||
| expect(res.status).toBe(200); | ||
| expect(res.headers.get("content-type")).toContain("text/html"); | ||
| expect(await res.text()).toContain('window.__SQ_DASHBOARD_BASE__="/"'); | ||
| }); | ||
|
|
||
| it("serves static assets with a content type", async () => { | ||
| const res = await app().request("/assets/app.js"); | ||
| expect(res.status).toBe(200); | ||
| expect(res.headers.get("content-type")).toContain("javascript"); | ||
| }); | ||
|
|
||
| it("falls back to the shell for unknown SPA paths", async () => { | ||
| const res = await app().request("/some/deep/route"); | ||
| expect(res.status).toBe(200); | ||
| expect(await res.text()).toContain("__SQ_DASHBOARD_BASE__"); | ||
| }); | ||
|
|
||
| it("serves under a base path and ignores paths outside it", async () => { | ||
| const scoped = createDashboardApp({ backend, staticDir: STATIC_DIR, basePath: "/dash" }); | ||
| expect((await scoped.request("/dash/api/overview")).status).toBe(200); | ||
| expect(await (await scoped.request("/dash/")).text()).toContain('window.__SQ_DASHBOARD_BASE__="/dash/"'); | ||
| expect((await scoped.request("/api/overview")).status).toBe(404); | ||
| }); | ||
|
|
||
| it("enforces basic auth when configured", async () => { | ||
| const secured = createDashboardApp({ backend, staticDir: STATIC_DIR, auth: { user: "admin", password: "s3cret" } }); | ||
| expect((await secured.request("/api/overview")).status).toBe(401); | ||
| const ok = await secured.request("/api/overview", { | ||
| headers: { Authorization: `Basic ${Buffer.from("admin:s3cret").toString("base64")}` }, | ||
| }); | ||
| expect(ok.status).toBe(200); | ||
| }); | ||
|
|
||
| it("throws without a backend or backend config", () => { | ||
| expect(() => createDashboardApp({ staticDir: STATIC_DIR })).toThrow(/backend/); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.