@kayjs/kay 2.0.0-beta.3 → 2.0.0-beta.4

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.
Files changed (91) hide show
  1. package/llms.txt +42 -0
  2. package/package.json +24 -13
  3. package/src/build/bundle.ts +11 -51
  4. package/src/build/client-plugin.ts +108 -0
  5. package/src/build/compile.ts +4 -2
  6. package/src/build/graph.ts +16 -3
  7. package/src/build/index.ts +23 -15
  8. package/src/build/islands.ts +28 -30
  9. package/src/build/manifest.ts +6 -3
  10. package/src/build/report.ts +82 -13
  11. package/src/build/resolve.ts +6 -7
  12. package/src/build/routes.ts +12 -21
  13. package/src/build/server-entry.ts +1 -1
  14. package/src/cli/check.ts +49 -43
  15. package/src/cli/commands.ts +224 -0
  16. package/src/cli/index.ts +46 -302
  17. package/src/cli/new.ts +15 -509
  18. package/src/cli/upgrade-plugins.ts +168 -0
  19. package/src/cli/upgrade.ts +220 -0
  20. package/src/compiler/analyze.ts +319 -565
  21. package/src/compiler/ast.ts +39 -0
  22. package/src/compiler/data-keys.ts +233 -0
  23. package/src/compiler/dom.ts +18 -15
  24. package/src/compiler/interactivity.ts +174 -0
  25. package/src/compiler/mistakes.ts +19 -1
  26. package/src/compiler/server-only.ts +4 -1
  27. package/src/compiler/shared.ts +1 -1
  28. package/src/compiler/ssr.ts +28 -28
  29. package/src/context.ts +1 -1
  30. package/src/dev/index.ts +15 -23
  31. package/src/dom/extras.ts +43 -12
  32. package/src/dom/index.ts +25 -1
  33. package/src/index.ts +0 -13
  34. package/src/jsx-runtime.ts +1 -1
  35. package/src/plugins/context.ts +8 -6
  36. package/src/plugins/index.ts +2 -2
  37. package/src/plugins/load.ts +25 -2
  38. package/src/plugins/public.ts +6 -0
  39. package/src/plugins/services.ts +15 -3
  40. package/src/plugins/types.ts +7 -11
  41. package/src/server/action-registry.ts +10 -4
  42. package/src/server/action.ts +8 -2
  43. package/src/server/document.ts +13 -1
  44. package/src/server/errors.ts +7 -5
  45. package/src/server/index.ts +9 -10
  46. package/src/server/lifecycle.ts +22 -0
  47. package/src/server/plugin-host.ts +6 -8
  48. package/src/server/public.ts +15 -0
  49. package/src/server/request-scope.ts +22 -0
  50. package/src/server/stream.ts +4 -3
  51. package/src/ssr/context.ts +25 -0
  52. package/src/ssr/index.ts +10 -7
  53. package/src/test/browser.ts +180 -0
  54. package/src/test/index.ts +4 -1
  55. package/templates/_shared/AGENTS.md +5 -0
  56. package/templates/_shared/CLAUDE.md +1 -0
  57. package/templates/_shared/public/robots.txt +2 -0
  58. package/templates/_shared/tsconfig.json +19 -0
  59. package/templates/app/.env.example +4 -0
  60. package/templates/app/kay.config.ts +11 -0
  61. package/templates/app/package.json +20 -0
  62. package/templates/app/src/auth.ts +14 -0
  63. package/templates/app/src/routes/_layout.tsx +32 -0
  64. package/templates/app/src/routes/dashboard/_middleware.ts +3 -0
  65. package/templates/app/src/routes/dashboard/index.tsx +13 -0
  66. package/templates/app/src/routes/index.tsx +15 -0
  67. package/templates/app/src/routes/login.tsx +34 -0
  68. package/templates/app/src/styles/app.css +7 -0
  69. package/templates/blog/content/posts/welcome.md +9 -0
  70. package/templates/blog/kay.config.ts +23 -0
  71. package/templates/blog/package.json +21 -0
  72. package/templates/blog/src/posts.ts +26 -0
  73. package/templates/blog/src/routes/_layout.tsx +20 -0
  74. package/templates/blog/src/routes/blog/[slug].tsx +31 -0
  75. package/templates/blog/src/routes/index.tsx +29 -0
  76. package/templates/blog/src/styles/app.css +10 -0
  77. package/templates/fullstack/kay.config.ts +6 -0
  78. package/templates/fullstack/package.json +18 -0
  79. package/templates/fullstack/src/actions/greet.ts +19 -0
  80. package/templates/fullstack/src/components/Counter.tsx +10 -0
  81. package/templates/fullstack/src/components/GreetForm.tsx +26 -0
  82. package/templates/fullstack/src/routes/_layout.tsx +14 -0
  83. package/templates/fullstack/src/routes/api/health.ts +5 -0
  84. package/templates/fullstack/src/routes/index.tsx +13 -0
  85. package/templates/fullstack/src/styles/app.css +29 -0
  86. package/templates/minimal/kay.config.ts +6 -0
  87. package/templates/minimal/package.json +18 -0
  88. package/templates/minimal/src/components/Counter.tsx +10 -0
  89. package/templates/minimal/src/routes/_layout.tsx +14 -0
  90. package/templates/minimal/src/routes/index.tsx +11 -0
  91. package/templates/minimal/src/styles/app.css +22 -0
@@ -0,0 +1,180 @@
1
+ // @kayjs/kay/test browser tests: the built app served on a loopback port of its own, its pages opened in Chromium with islands hydrated.
2
+
3
+ import type { TestApp, TestAppOptions } from './index'
4
+ import { existsSync } from 'node:fs'
5
+ import { testApp } from './index'
6
+
7
+ export interface BrowserTestOptions extends TestAppOptions {
8
+ /** Chrome or Chromium executable; defaults to `BUN_CHROME_PATH`, Playwright's own, then the system's. */
9
+ browserPath?: string | undefined
10
+ /** Milliseconds `open`, `waitFor` and `click` wait before they throw. Default 10000. */
11
+ timeout?: number | undefined
12
+ }
13
+
14
+ export interface BrowserPage {
15
+ readonly url: string
16
+ /** Page errors, `console.error` messages and failed requests (status 400 and up, but the favicon) seen since the page opened. */
17
+ readonly errors: string[]
18
+ /** Runs `fn` (or the expression string) in the page and returns its JSON-serializable result. */
19
+ evaluate<T>(fn: string | (() => T | Promise<T>)): Promise<T>
20
+ /** Waits for a CSS selector to match, or for `fn` to return a truthy value in the page. */
21
+ waitFor(selectorOrFn: string | (() => unknown)): Promise<void>
22
+ click(selector: string): Promise<void>
23
+ fill(selector: string, value: string): Promise<void>
24
+ /** The text content of the first element matching `selector`. */
25
+ text(selector: string): Promise<string>
26
+ close(): Promise<void>
27
+ }
28
+
29
+ export interface BrowserTest {
30
+ /** The built app, for requests that need no browser. */
31
+ readonly app: TestApp
32
+ /** `http://127.0.0.1:<port>`, a port the test owns. */
33
+ readonly origin: string
34
+ /** Opens `path` and resolves once the page has loaded and the islands that hydrate on load have hydrated. */
35
+ open(path: string): Promise<BrowserPage>
36
+ /** Closes the browser and the server. */
37
+ close(): Promise<void>
38
+ }
39
+
40
+ const DEFAULT_TIMEOUT_MS = 10_000
41
+
42
+ // The few playwright-core types used here, so an app's typecheck never needs playwright-core installed.
43
+ interface Page {
44
+ on(event: 'pageerror', listener: (error: Error) => void): void
45
+ on(event: 'response', listener: (response: { status: () => number, url: () => string }) => void): void
46
+ on(event: 'console', listener: (message: { type: () => string, text: () => string }) => void): void
47
+ goto(url: string, options: { waitUntil: 'load', timeout: number }): Promise<unknown>
48
+ evaluate<T>(fn: string | (() => T | Promise<T>)): Promise<T>
49
+ waitForSelector(selector: string, options: { timeout: number }): Promise<unknown>
50
+ waitForFunction(fn: string | (() => unknown), arg: undefined, options: { timeout: number }): Promise<unknown>
51
+ click(selector: string, options: { timeout: number }): Promise<void>
52
+ fill(selector: string, value: string, options: { timeout: number }): Promise<void>
53
+ locator(selector: string): { first: () => { innerText: (options: { timeout: number }) => Promise<string> } }
54
+ }
55
+ interface BrowserContext {
56
+ newPage(): Promise<Page>
57
+ close(): Promise<void>
58
+ }
59
+ interface Browser {
60
+ newContext(): Promise<BrowserContext>
61
+ close(): Promise<void>
62
+ }
63
+
64
+ // True once every island that hydrates on load has hydrated; the ones waiting to be seen, idle or touched are left to the test.
65
+ const ISLANDS_HYDRATED = `[...document.querySelectorAll('kay-island')].every(e => ['visible', 'idle', 'interaction'].includes(e.getAttribute('h')) || e._d !== undefined)`
66
+
67
+ interface PlaywrightCore {
68
+ chromium: { launch: (options: { headless: boolean, executablePath: string }) => Promise<Browser>, executablePath: () => string }
69
+ }
70
+
71
+ async function loadPlaywright(): Promise<PlaywrightCore> {
72
+ try {
73
+ return await import('playwright-core') as unknown as PlaywrightCore
74
+ }
75
+ catch {
76
+ throw new Error('kay: browser tests need playwright-core, and it is not installed: run `bun add -d playwright-core`.')
77
+ }
78
+ }
79
+
80
+ function findBrowser(playwright: PlaywrightCore, given: string | undefined): string {
81
+ if (given !== undefined) {
82
+ if (!existsSync(given))
83
+ throw new Error(`kay: no browser at ${given}: give the path of Chrome or Chromium as \`browserPath\`, or set BUN_CHROME_PATH.`)
84
+ return given
85
+ }
86
+ const found = [process.env.BUN_CHROME_PATH, playwright.chromium.executablePath(), '/usr/bin/chromium', '/usr/bin/google-chrome']
87
+ .find((path): path is string => typeof path === 'string' && path.length > 0 && existsSync(path))
88
+ if (found === undefined)
89
+ throw new Error('kay: browser tests found no Chrome or Chromium: install one, run `bunx playwright install chromium`, or give its path as `browserPath` or BUN_CHROME_PATH.')
90
+ return found
91
+ }
92
+
93
+ function wrapPage(page: Page, context: BrowserContext, timeout: number, url: string): BrowserPage {
94
+ const errors: string[] = []
95
+ page.on('pageerror', error => errors.push(error.message))
96
+ // A failed load logs a console error without its URL, so the response names it instead; the favicon browsers ask for unprompted is no mistake of the app.
97
+ page.on('response', (response) => {
98
+ if (response.status() >= 400 && !response.url().endsWith('/favicon.ico'))
99
+ errors.push(`${response.status()} ${response.url()}`)
100
+ })
101
+ page.on('console', (message) => {
102
+ if (message.type() === 'error' && !message.text().startsWith('Failed to load resource'))
103
+ errors.push(message.text())
104
+ })
105
+ return {
106
+ url,
107
+ errors,
108
+ async evaluate(fn) {
109
+ return await page.evaluate(fn as () => never)
110
+ },
111
+ async waitFor(selectorOrFn) {
112
+ if (typeof selectorOrFn === 'string')
113
+ await page.waitForSelector(selectorOrFn, { timeout })
114
+ else
115
+ await page.waitForFunction(selectorOrFn, undefined, { timeout })
116
+ },
117
+ async click(selector) {
118
+ await page.click(selector, { timeout })
119
+ },
120
+ async fill(selector, value) {
121
+ await page.fill(selector, value, { timeout })
122
+ },
123
+ async text(selector) {
124
+ return await page.locator(selector).first().innerText({ timeout })
125
+ },
126
+ async close() {
127
+ await context.close()
128
+ },
129
+ }
130
+ }
131
+
132
+ /** Builds the app for production, serves it on 127.0.0.1 on a free port and opens its pages in Chromium. */
133
+ export async function testBrowser(options: BrowserTestOptions): Promise<BrowserTest> {
134
+ const playwright = await loadPlaywright()
135
+ const executablePath = findBrowser(playwright, options.browserPath)
136
+ const timeout = options.timeout ?? DEFAULT_TIMEOUT_MS
137
+ const app = await testApp({ ...options, minify: options.minify ?? true })
138
+ const server = app.app.serve(0, '127.0.0.1')
139
+ const origin = `http://127.0.0.1:${server.port}`
140
+ const stop = async () => {
141
+ server.stop(true)
142
+ await app.app.dispose?.()
143
+ }
144
+ let browser: Browser
145
+ try {
146
+ browser = await playwright.chromium.launch({ headless: true, executablePath })
147
+ }
148
+ catch (error) {
149
+ await stop()
150
+ throw error
151
+ }
152
+ return {
153
+ app,
154
+ origin,
155
+ async open(path) {
156
+ const context = await browser.newContext()
157
+ const page = await context.newPage()
158
+ const url = new URL(path, origin).href
159
+ const wrapped = wrapPage(page, context, timeout, url)
160
+ // Not `networkidle`: a page that polls or streams never goes idle.
161
+ await page.goto(url, { waitUntil: 'load', timeout })
162
+ try {
163
+ await page.waitForFunction(ISLANDS_HYDRATED, undefined, { timeout })
164
+ }
165
+ catch (error) {
166
+ const seen = wrapped.errors.length > 0 ? `; the page reported: ${wrapped.errors.join('; ')}` : ''
167
+ throw new Error(`kay: the islands of ${url} did not hydrate within ${timeout} ms${seen}.`, { cause: error })
168
+ }
169
+ return wrapped
170
+ },
171
+ async close() {
172
+ try {
173
+ await browser.close()
174
+ }
175
+ finally {
176
+ await stop()
177
+ }
178
+ },
179
+ }
180
+ }
package/src/test/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- // kay/test: in-process testing for pages, middleware, cookies, and actions — zero port binding.
1
+ // @kayjs/kay/test: in-process testing for pages, middleware, cookies, and actions — zero port binding.
2
2
 
3
3
  import type { BuildManifest, BuildOptions } from '../build/index'
4
4
  import type { KayApp, ServerAction, ServerRuntimeConfig } from '../server/index'
@@ -131,3 +131,6 @@ export async function testApp(options: TestAppOptions): Promise<TestApp> {
131
131
  },
132
132
  }
133
133
  }
134
+
135
+ export { testBrowser } from './browser'
136
+ export type { BrowserPage, BrowserTest, BrowserTestOptions } from './browser'
@@ -0,0 +1,5 @@
1
+ # __APP_NAME__
2
+
3
+ A kay app. Read `node_modules/@kayjs/kay/llms.txt` before writing code: it holds kay's whole API for the version installed here, on one page.
4
+
5
+ @node_modules/@kayjs/kay/llms.txt
@@ -0,0 +1 @@
1
+ @AGENTS.md
@@ -0,0 +1,2 @@
1
+ User-agent: *
2
+ Allow: /
@@ -0,0 +1,19 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ESNext",
4
+ "module": "ESNext",
5
+ "moduleResolution": "bundler",
6
+ "jsx": "react-jsx",
7
+ "jsxImportSource": "@kayjs/kay",
8
+ "strict": true,
9
+ "noEmit": true,
10
+ "skipLibCheck": true,
11
+ "types": [
12
+ "bun"
13
+ ]
14
+ },
15
+ "include": [
16
+ "src",
17
+ "kay.config.ts"
18
+ ]
19
+ }
@@ -0,0 +1,4 @@
1
+ # Generate a 32+ byte secret for production, e.g.:
2
+ # openssl rand -base64 32
3
+ AUTH_SECRET=
4
+ AUTH_DB=./data/auth.sqlite
@@ -0,0 +1,11 @@
1
+ import { ui } from '@kayjs/ui/plugin'
2
+ import { defineConfig } from '@kayjs/kay/config'
3
+ import { auth } from './src/auth'
4
+
5
+ export default defineConfig({
6
+ lang: 'en',
7
+ plugins: [
8
+ ui({ grammar: 'editorial' }),
9
+ auth,
10
+ ],
11
+ })
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "__APP_NAME__",
3
+ "private": true,
4
+ "type": "module",
5
+ "scripts": {
6
+ "dev": "kay dev",
7
+ "build": "kay build",
8
+ "start": "kay start",
9
+ "check": "kay check"
10
+ },
11
+ "dependencies": {
12
+ "@kayjs/kay": "__KAY_VERSION__",
13
+ "@kayjs/auth": "__KAY_VERSION__",
14
+ "@kayjs/ui": "__KAY_VERSION__"
15
+ },
16
+ "devDependencies": {
17
+ "@types/bun": "latest",
18
+ "typescript": "^6.0.3"
19
+ }
20
+ }
@@ -0,0 +1,14 @@
1
+ import { join } from 'node:path'
2
+ import { createAuth } from '@kayjs/auth'
3
+ import { sqliteAuthStore } from '@kayjs/auth/sqlite'
4
+
5
+ // Set AUTH_SECRET (at least 32 bytes) in production — see .env.example.
6
+ const defaultDb = import.meta.dir.startsWith('/$bunfs')
7
+ ? './data/auth.sqlite'
8
+ : join(import.meta.dir, '../data/auth.sqlite')
9
+
10
+ export const auth = createAuth({
11
+ store: sqliteAuthStore(process.env.AUTH_DB || defaultDb),
12
+ secret: process.env.AUTH_SECRET || undefined,
13
+ defaultRedirect: '/dashboard',
14
+ })
@@ -0,0 +1,32 @@
1
+ import type { LayoutProps } from '@kayjs/kay'
2
+ import { Button, Cluster, Toaster } from '@kayjs/ui'
3
+ import { Head } from '@kayjs/kay'
4
+ import '../styles/app.css'
5
+
6
+ export default function Layout({ children, locals }: LayoutProps) {
7
+ const user = locals.user
8
+ return (
9
+ <>
10
+ <Head>
11
+ <title>__APP_NAME__</title>
12
+ </Head>
13
+ <div class="wrap">
14
+ <header data-layout="split" class="site-header">
15
+ <a href="/" class="site-brand">__APP_NAME__</a>
16
+ {user
17
+ ? (
18
+ <Cluster>
19
+ <Button href="/dashboard" variant="ghost" size="sm">{user.email}</Button>
20
+ <form action="/auth/signout" method="post">
21
+ <Button type="submit" variant="outline" size="sm">Sign out</Button>
22
+ </form>
23
+ </Cluster>
24
+ )
25
+ : <Button href="/login" variant="outline" size="sm">Sign in</Button>}
26
+ </header>
27
+ <main>{children}</main>
28
+ </div>
29
+ <Toaster />
30
+ </>
31
+ )
32
+ }
@@ -0,0 +1,3 @@
1
+ import { auth } from '../../auth'
2
+
3
+ export default auth.guard({ redirectTo: '/login' })
@@ -0,0 +1,13 @@
1
+ import type { PageProps } from '@kayjs/kay'
2
+ import { Card, CardBody, CardHeader } from '@kayjs/ui'
3
+
4
+ export default async function DashboardPage({ locals }: PageProps) {
5
+ return (
6
+ <Card>
7
+ <CardHeader>Dashboard</CardHeader>
8
+ <CardBody>
9
+ <p class="m-0">Signed in as <strong>{locals.user?.email}</strong>.</p>
10
+ </CardBody>
11
+ </Card>
12
+ )
13
+ }
@@ -0,0 +1,15 @@
1
+ import { Badge, Button, Cluster, Stack } from '@kayjs/ui'
2
+
3
+ export default async function Home() {
4
+ return (
5
+ <Stack gap="md">
6
+ <Cluster><Badge tone="accent">Kay v2</Badge><Badge>SQLite Auth</Badge></Cluster>
7
+ <h1 class="page-title">Welcome to __APP_NAME__</h1>
8
+ <p class="page-lead">Zero-dependency Argon2id auth, @kayjs/ui pure CSS grammar, and Bun speed.</p>
9
+ <Cluster>
10
+ <Button href="/login" tone="accent">Get started</Button>
11
+ <Button href="/dashboard" variant="outline">Dashboard</Button>
12
+ </Cluster>
13
+ </Stack>
14
+ )
15
+ }
@@ -0,0 +1,34 @@
1
+ import { Button, Card, CardBody, CardHeader, Field, Grid, Stack } from '@kayjs/ui'
2
+
3
+ export default function LoginPage() {
4
+ return (
5
+ <Grid min="md" gap="lg">
6
+ <Card>
7
+ <CardHeader>Sign in</CardHeader>
8
+ <CardBody>
9
+ <form action="/auth/signin" method="post">
10
+ <Stack gap="sm">
11
+ <input type="hidden" name="redirect" value="/dashboard" />
12
+ <Field id="in-email" label="Email" required><input id="in-email" name="email" type="email" required /></Field>
13
+ <Field id="in-pass" label="Password" required><input id="in-pass" name="password" type="password" required /></Field>
14
+ <Button type="submit" tone="accent">Sign in</Button>
15
+ </Stack>
16
+ </form>
17
+ </CardBody>
18
+ </Card>
19
+ <Card>
20
+ <CardHeader>Create account</CardHeader>
21
+ <CardBody>
22
+ <form action="/auth/signup" method="post">
23
+ <Stack gap="sm">
24
+ <input type="hidden" name="redirect" value="/dashboard" />
25
+ <Field id="up-email" label="Email" required><input id="up-email" name="email" type="email" required /></Field>
26
+ <Field id="up-pass" label="Password" hint="Min 12 characters" required><input id="up-pass" name="password" type="password" minlength="12" required /></Field>
27
+ <Button type="submit" tone="accent">Create account</Button>
28
+ </Stack>
29
+ </form>
30
+ </CardBody>
31
+ </Card>
32
+ </Grid>
33
+ )
34
+ }
@@ -0,0 +1,7 @@
1
+ body { margin: 0; background: var(--ui-paper); color: var(--ui-ink); font-family: var(--ui-font-body); }
2
+ .wrap { max-width: 52rem; margin: 0 auto; padding: 1.5rem 1.25rem 4rem; }
3
+ .site-header { padding-bottom: 1.25rem; margin-bottom: 2rem; border-bottom: 1px solid var(--ui-line); }
4
+ .site-brand { font-family: var(--ui-font-display); font-size: 1.3rem; font-weight: 700; text-decoration: none; color: inherit; }
5
+ .page-title { margin: 0; font-family: var(--ui-font-display); }
6
+ .page-lead { margin: 0; color: var(--ui-muted); }
7
+ .m-0 { margin: 0; }
@@ -0,0 +1,9 @@
1
+ ---
2
+ title: Welcome to __APP_NAME__
3
+ date: 2026-11-23
4
+ description: A zero-JS static Markdown blog built with Kay v2.
5
+ tags: [kay, bun]
6
+ ---
7
+ ## Fast by Default
8
+
9
+ This post is rendered at build time using Bun's native Zig Markdown engine and ships **0 KB** of client JavaScript.
@@ -0,0 +1,23 @@
1
+ import { content } from '@kayjs/content'
2
+ import { seo } from '@kayjs/seo'
3
+ import { ui } from '@kayjs/ui/plugin'
4
+ import { defineConfig } from '@kayjs/kay/config'
5
+ import { posts } from './src/posts'
6
+
7
+ export default defineConfig({
8
+ lang: 'en',
9
+ plugins: [
10
+ ui({ grammar: 'editorial' }),
11
+ seo({ site: 'https://example.com', titleTemplate: '%s — __APP_NAME__' }),
12
+ content({
13
+ feeds: [{
14
+ path: '/rss.xml',
15
+ title: '__APP_NAME__',
16
+ description: 'Articles & notes',
17
+ site: 'https://example.com',
18
+ linkPrefix: '/blog',
19
+ collection: posts,
20
+ }],
21
+ }),
22
+ ],
23
+ })
@@ -0,0 +1,21 @@
1
+ {
2
+ "name": "__APP_NAME__",
3
+ "private": true,
4
+ "type": "module",
5
+ "scripts": {
6
+ "dev": "kay dev",
7
+ "build": "kay build",
8
+ "start": "kay start",
9
+ "check": "kay check"
10
+ },
11
+ "dependencies": {
12
+ "@kayjs/kay": "__KAY_VERSION__",
13
+ "@kayjs/content": "__KAY_VERSION__",
14
+ "@kayjs/seo": "__KAY_VERSION__",
15
+ "@kayjs/ui": "__KAY_VERSION__"
16
+ },
17
+ "devDependencies": {
18
+ "@types/bun": "latest",
19
+ "typescript": "^6.0.3"
20
+ }
21
+ }
@@ -0,0 +1,26 @@
1
+ import type { StandardSchemaV1 } from '@kayjs/kay/server'
2
+ import { defineCollection } from '@kayjs/content'
3
+
4
+ export interface PostMeta {
5
+ title: string
6
+ date: string
7
+ description?: string
8
+ tags?: string[]
9
+ draft?: boolean
10
+ }
11
+
12
+ // Any Standard Schema validator works here (Zod, Valibot, ArkType); this one keeps the template free of dependencies.
13
+ const postSchema: StandardSchemaV1<unknown, PostMeta> = {
14
+ '~standard': {
15
+ version: 1,
16
+ vendor: 'app',
17
+ validate(raw: unknown) {
18
+ const data = raw as Partial<PostMeta>
19
+ if (typeof data.title !== 'string' || typeof data.date !== 'string')
20
+ return { issues: [{ message: 'A post needs a title and a date', path: ['title'] }] }
21
+ return { value: data as PostMeta }
22
+ },
23
+ },
24
+ }
25
+
26
+ export const posts = defineCollection({ dir: 'content/posts', schema: postSchema })
@@ -0,0 +1,20 @@
1
+ import type { KayChild } from '@kayjs/kay'
2
+ import { Head } from '@kayjs/kay'
3
+ import '../styles/app.css'
4
+
5
+ export default function Layout({ children }: { children: KayChild }) {
6
+ return (
7
+ <>
8
+ <Head>
9
+ <title>__APP_NAME__</title>
10
+ </Head>
11
+ <div class="wrap">
12
+ <header data-layout="split" class="site-header">
13
+ <a href="/" class="site-brand">__APP_NAME__</a>
14
+ <a href="/rss.xml" class="site-rss">RSS</a>
15
+ </header>
16
+ <main>{children}</main>
17
+ </div>
18
+ </>
19
+ )
20
+ }
@@ -0,0 +1,31 @@
1
+ import type { RequestContext } from '@kayjs/kay/server'
2
+ import { Button, Stack } from '@kayjs/ui'
3
+ import { Head } from '@kayjs/kay'
4
+ import { posts } from '../../posts'
5
+
6
+ export const prerender = true
7
+ export function paths() {
8
+ return posts.paths('/blog')
9
+ }
10
+
11
+ export async function load(ctx: RequestContext) {
12
+ const post = posts.get(ctx.params.slug ?? '')
13
+ if (!post)
14
+ return new Response('Not found', { status: 404 })
15
+ return { post }
16
+ }
17
+
18
+ export default function PostPage({ data }: { data: { post: NonNullable<ReturnType<typeof posts.get>> } }) {
19
+ const { post } = data
20
+ return (
21
+ <Stack gap="md">
22
+ <Head><title>{post.data.title}</title></Head>
23
+ <article>
24
+ <h1 class="post-title">{post.data.title}</h1>
25
+ <p class="post-meta">{post.data.date} · {post.readingMinutes} min read</p>
26
+ <div innerHTML={post.html} />
27
+ </article>
28
+ <div><Button href="/" variant="outline">← All posts</Button></div>
29
+ </Stack>
30
+ )
31
+ }
@@ -0,0 +1,29 @@
1
+ import { Badge, Card, CardBody, CardFooter, CardHeader, Cluster, Stack } from '@kayjs/ui'
2
+ import { posts } from '../posts'
3
+
4
+ export const prerender = true
5
+
6
+ export default async function BlogIndex() {
7
+ const list = posts.all()
8
+ return (
9
+ <Stack gap="lg">
10
+ <h1 class="page-title">Articles</h1>
11
+ {list.map(post => (
12
+ <Card key={post.slug}>
13
+ <CardHeader>
14
+ <a href={`/blog/${post.slug}`} class="post-link">{post.data.title}</a>
15
+ </CardHeader>
16
+ <CardBody>
17
+ <p class="post-desc">{post.data.description ?? ''}</p>
18
+ </CardBody>
19
+ <CardFooter>
20
+ <Cluster>
21
+ <span>{post.data.date} · {post.readingMinutes} min read</span>
22
+ {(post.data.tags ?? []).map(tag => <Badge key={tag}>{tag}</Badge>)}
23
+ </Cluster>
24
+ </CardFooter>
25
+ </Card>
26
+ ))}
27
+ </Stack>
28
+ )
29
+ }
@@ -0,0 +1,10 @@
1
+ body { margin: 0; background: var(--ui-paper); color: var(--ui-ink); font-family: var(--ui-font-body); }
2
+ .wrap { max-width: 46rem; margin: 0 auto; padding: 2rem 1.25rem 4rem; }
3
+ .site-header { padding-bottom: 1.25rem; margin-bottom: 2rem; border-bottom: 1px solid var(--ui-line); }
4
+ .site-brand { font-family: var(--ui-font-display); font-size: 1.35rem; font-weight: 700; text-decoration: none; color: inherit; }
5
+ .site-rss { font-size: 0.9rem; color: var(--ui-muted); }
6
+ .page-title { margin: 0; font-family: var(--ui-font-display); }
7
+ .post-link { color: inherit; text-decoration: none; }
8
+ .post-desc { margin: 0; color: var(--ui-muted); }
9
+ .post-title { font-family: var(--ui-font-display); margin-bottom: 0.25rem; }
10
+ .post-meta { color: var(--ui-muted); margin-top: 0; }
@@ -0,0 +1,6 @@
1
+ import { defineConfig } from '@kayjs/kay/config'
2
+
3
+ export default defineConfig({
4
+ lang: 'en',
5
+ plugins: [],
6
+ })
@@ -0,0 +1,18 @@
1
+ {
2
+ "name": "__APP_NAME__",
3
+ "private": true,
4
+ "type": "module",
5
+ "scripts": {
6
+ "dev": "kay dev",
7
+ "build": "kay build",
8
+ "start": "kay start",
9
+ "check": "kay check"
10
+ },
11
+ "dependencies": {
12
+ "@kayjs/kay": "__KAY_VERSION__"
13
+ },
14
+ "devDependencies": {
15
+ "@types/bun": "latest",
16
+ "typescript": "^6.0.3"
17
+ }
18
+ }
@@ -0,0 +1,19 @@
1
+ import type { StandardSchemaV1 } from '@kayjs/kay/server'
2
+ import { action } from '@kayjs/kay/server'
3
+
4
+ const nameSchema: StandardSchemaV1<{ name: string }> = {
5
+ '~standard': {
6
+ version: 1,
7
+ vendor: 'kay',
8
+ validate(raw: unknown) {
9
+ const name = String((raw as { name?: unknown })?.name ?? '').trim()
10
+ if (name.length < 2)
11
+ return { issues: [{ message: 'Enter at least 2 characters', path: ['name'] }] }
12
+ return { value: { name } }
13
+ },
14
+ },
15
+ }
16
+
17
+ export const greet = action(nameSchema, async ({ name }) => {
18
+ return { message: `Hello, ${name}!` }
19
+ })
@@ -0,0 +1,10 @@
1
+ import { signal } from '@kayjs/kay'
2
+
3
+ export function Counter({ initial = 0 }: { initial?: number }) {
4
+ const count = signal(initial)
5
+ return (
6
+ <button type="button" onClick={() => count.value++}>
7
+ Count: {count.value}
8
+ </button>
9
+ )
10
+ }