@swelte/create 0.0.0-stage → 0.1.5
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-MIT +21 -0
- package/README.md +16 -2
- package/bin/create.js +7 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +138 -0
- package/dist/index.js.map +1 -0
- package/dist/prompts.d.ts +10 -0
- package/dist/prompts.d.ts.map +1 -0
- package/dist/prompts.js +56 -0
- package/dist/prompts.js.map +1 -0
- package/dist/scaffold.d.ts +16 -0
- package/dist/scaffold.d.ts.map +1 -0
- package/dist/scaffold.js +167 -0
- package/dist/scaffold.js.map +1 -0
- package/package.json +59 -4
- package/templates/base/assets/README.md +12 -0
- package/templates/base/gitignore +8 -0
- package/templates/base/index.html +12 -0
- package/templates/base/package.json +35 -0
- package/templates/base/src/lib/README.md +9 -0
- package/templates/base/src/routes/+error.svelte +14 -0
- package/templates/base/src/routes/+page.svelte +12 -0
- package/templates/base/src/routes/+page.v +7 -0
- package/templates/base/svelte.config.js +34 -0
- package/templates/base/tsconfig.json +24 -0
- package/templates/base/vite.config.ts +28 -0
- package/templates/lib/src/lib/hello/api.v +15 -0
- package/templates/lib/src/routes/+page.svelte +36 -0
- package/templates/lib/src/routes/+page.v +9 -0
- package/templates/sample/src/lib/backend/secrets.v +12 -0
- package/templates/sample/src/lib/crypto/hash.v +13 -0
- package/templates/sample/src/routes/+page.svelte +179 -0
- package/templates/sample/src/routes/+page.v +22 -0
- package/templates/sample/src/routes/admin/+page.svelte +23 -0
- package/templates/sample/src/routes/admin/+page.v +8 -0
- package/templates/sample/src/routes/admin/+route.svelte +10 -0
- package/templates/sample/src/routes/admin/+route.v +15 -0
- package/templates/sample/src/routes/api/greet.v +127 -0
- package/templates/sample/src/routes/api/sibling.v +14 -0
- package/templates/sample/src/routes/files/[...rest].v +8 -0
- package/templates/sample/src/routes/users/[id].v +8 -0
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
let { status = 500, message = 'Something went wrong' }: { status?: number; message?: string } =
|
|
3
|
+
$props()
|
|
4
|
+
</script>
|
|
5
|
+
|
|
6
|
+
<main>
|
|
7
|
+
<h1>{status}</h1>
|
|
8
|
+
<p>{message}</p>
|
|
9
|
+
{#if status === 400 || status === 401 || status === 403}
|
|
10
|
+
<p><a href="/login">Log in</a> · <a href="/">Home</a></p>
|
|
11
|
+
{:else}
|
|
12
|
+
<p><a href="/">Home</a></p>
|
|
13
|
+
{/if}
|
|
14
|
+
</main>
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';
|
|
2
|
+
import { defineConfig } from '@swelte/core/config';
|
|
3
|
+
|
|
4
|
+
/** @type {import('@swelte/core/config').SwelteConfig} */
|
|
5
|
+
export default defineConfig({
|
|
6
|
+
// Required by the VS Code Svelte extension (svelte-language-server) to
|
|
7
|
+
// preprocess <script lang="ts"> blocks. Without this file the extension
|
|
8
|
+
// falls back to loading vite.config.ts via @sveltejs/load-config, which
|
|
9
|
+
// currently looks for a plugin named 'vite-plugin-svelte:config' while the
|
|
10
|
+
// installed @sveltejs/vite-plugin-svelte@5 exposes 'vite-plugin-svelte' —
|
|
11
|
+
// hence "No Svelte configuration found in vite config".
|
|
12
|
+
// See: node_modules/@sveltejs/vite-plugin-svelte/src/index.js (name) vs
|
|
13
|
+
// ~/.vscode/extensions/svelte.svelte-vscode-*/node_modules/@sveltejs/load-config/dist/src/index.js
|
|
14
|
+
preprocess: vitePreprocess(),
|
|
15
|
+
// Client <await> (Svelte 5 experimental.async) lives here — NOT only in
|
|
16
|
+
// vite.config.ts. The VS Code Svelte extension / svelte-language-server
|
|
17
|
+
// reads svelte.config.js directly and never sees vite.config.ts, so an
|
|
18
|
+
// await in markup reports `experimental_async_missing` in the editor even
|
|
19
|
+
// when `vite build` passes. vite.config.ts spreads this file, so the Vite
|
|
20
|
+
// build picks it up from here too (its own merge is just a fallback).
|
|
21
|
+
compilerOptions: { experimental: { async: true } },
|
|
22
|
+
// Illustrative only — all defaults. defineConfig() keeps this key
|
|
23
|
+
// non-enumerable: invisible to svelte() (no "invalid plugin options"
|
|
24
|
+
// warning) but still readable by @swelte/core's loadConfig.
|
|
25
|
+
swelte: {
|
|
26
|
+
// name: '__NAME__',
|
|
27
|
+
// routesDir: 'src/routes',
|
|
28
|
+
// libDir: 'src/lib',
|
|
29
|
+
// outDir: '.swelte',
|
|
30
|
+
// distDir: 'dist',
|
|
31
|
+
// port: 8131, // V backend ($PORT wins)
|
|
32
|
+
// corsOrigin: '', // e.g. 'https://example.com' or '*' ($SWELTE_CORS_ORIGIN wins)
|
|
33
|
+
},
|
|
34
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"moduleResolution": "bundler",
|
|
6
|
+
"strict": true,
|
|
7
|
+
"skipLibCheck": true,
|
|
8
|
+
"allowJs": true,
|
|
9
|
+
"checkJs": false,
|
|
10
|
+
"esModuleInterop": true,
|
|
11
|
+
"forceConsistentCasingInFileNames": true,
|
|
12
|
+
"noEmit": true,
|
|
13
|
+
"types": ["vite/client", "svelte"],
|
|
14
|
+
"paths": {
|
|
15
|
+
"#routes/*.v": ["./.swelte/types/routes/*.d.ts"],
|
|
16
|
+
"#lib/*.v": ["./.swelte/types/lib/*.d.ts"],
|
|
17
|
+
"#routes/*": ["./src/routes/*"],
|
|
18
|
+
"#lib/*": ["./src/lib/*"],
|
|
19
|
+
"$app/*": ["./.swelte/app/*"],
|
|
20
|
+
"#app/*": ["./.swelte/app/*", "./.swelte/*.d.ts"]
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"include": ["src", "vite.config.ts", ".swelte/types", ".swelte/v-shim.d.ts", ".swelte/app/*.d.ts", ".swelte/paths.d.ts", ".swelte/environment.d.ts"]
|
|
24
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { defineConfig } from 'vite'
|
|
2
|
+
import { svelte } from '@sveltejs/vite-plugin-svelte'
|
|
3
|
+
import { swelte } from '@swelte/core/vite'
|
|
4
|
+
import svelteConfig from './svelte.config.js'
|
|
5
|
+
|
|
6
|
+
// section 1.3 streaming: client <await> needs Svelte experimental.async.
|
|
7
|
+
// The SSR bundle is composed by adapters (generate:'server', always sync),
|
|
8
|
+
// so this flag never reaches sync renderRoute.
|
|
9
|
+
const svelteOptions = {
|
|
10
|
+
...svelteConfig,
|
|
11
|
+
compilerOptions: { ...((svelteConfig as { compilerOptions?: Record<string, unknown> }).compilerOptions ?? {}), experimental: { ...((((svelteConfig as { compilerOptions?: { experimental?: Record<string, unknown> } }).compilerOptions ?? {}).experimental ?? {}) as Record<string, unknown>), async: true } },
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const vBackendUrl =
|
|
15
|
+
process.env.SWELTE_V_BACKEND_URL ??
|
|
16
|
+
process.env.VITE_SWELTE_V_BACKEND_URL ??
|
|
17
|
+
'http://localhost:8131'
|
|
18
|
+
|
|
19
|
+
export default defineConfig({
|
|
20
|
+
plugins: [
|
|
21
|
+
svelte(svelteOptions as Parameters<typeof svelte>[0]),
|
|
22
|
+
// Dirs come from svelte.config.js `swelte` key; plugin falls back to
|
|
23
|
+
// the unified config for any option not passed here.
|
|
24
|
+
swelte({
|
|
25
|
+
vBackendUrl,
|
|
26
|
+
}),
|
|
27
|
+
],
|
|
28
|
+
})
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// Public Swelte library module (universal): same V compiles native +
|
|
2
|
+
// `v -b wasm`. Consumers import it as `my-lib/v/hello.v` (TS) and
|
|
3
|
+
// `npm_my_lib.hello` (V); the dogfood `.svelte` demo imports it as
|
|
4
|
+
// `#lib/hello/api.v` (local WASM, same module file).
|
|
5
|
+
module hello
|
|
6
|
+
|
|
7
|
+
pub struct HelloArgs {
|
|
8
|
+
pub:
|
|
9
|
+
name string
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
@[rpc]
|
|
13
|
+
pub fn hello(args HelloArgs) string {
|
|
14
|
+
return 'hello ' + args.name
|
|
15
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { HelloResult } from '#lib/hello/api.v'
|
|
3
|
+
import { hello } from '#lib/hello/api.v'
|
|
4
|
+
|
|
5
|
+
let name = $state('world')
|
|
6
|
+
let greeting: HelloResult = $state('')
|
|
7
|
+
let error = $state('')
|
|
8
|
+
</script>
|
|
9
|
+
|
|
10
|
+
<main>
|
|
11
|
+
<h1>__NAME__ (library dogfood)</h1>
|
|
12
|
+
<p>
|
|
13
|
+
This demo route is local only — it is never published. It exercises the public
|
|
14
|
+
<code>src/lib</code>
|
|
15
|
+
surface exactly like a consumer would.
|
|
16
|
+
</p>
|
|
17
|
+
<input bind:value={name} placeholder="your name">
|
|
18
|
+
<button
|
|
19
|
+
onclick={async () => {
|
|
20
|
+
error = ''
|
|
21
|
+
try {
|
|
22
|
+
greeting = await hello({ name })
|
|
23
|
+
} catch (e) {
|
|
24
|
+
error = String(e)
|
|
25
|
+
}
|
|
26
|
+
}}
|
|
27
|
+
>
|
|
28
|
+
Say hello (#lib WASM)
|
|
29
|
+
</button>
|
|
30
|
+
{#if greeting}
|
|
31
|
+
<p>{greeting}</p>
|
|
32
|
+
{/if}
|
|
33
|
+
{#if error}
|
|
34
|
+
<pre style="color:crimson">{error}</pre>
|
|
35
|
+
{/if}
|
|
36
|
+
</main>
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// Library dogfood route for `/`: minimal GET probe. The `.svelte` twin
|
|
2
|
+
// exercises the public src/lib surface via `#lib/...` exactly like a
|
|
3
|
+
// consumer would. Never published.
|
|
4
|
+
module plus_page
|
|
5
|
+
|
|
6
|
+
@[get]
|
|
7
|
+
pub fn hello(req SwelteRequest) string {
|
|
8
|
+
return 'hello dogfood'
|
|
9
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// Swelte isomorphic lib. Same V compiles to native + `v -b wasm`.
|
|
2
|
+
// Importable from `#lib/crypto/hash.v`; local WASM call, no fetch.
|
|
3
|
+
module crypto
|
|
4
|
+
|
|
5
|
+
pub struct HashArgs {
|
|
6
|
+
pub:
|
|
7
|
+
input string
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
@[rpc]
|
|
11
|
+
pub fn hash_string(args HashArgs) string {
|
|
12
|
+
return 'hash(' + args.input + ')'
|
|
13
|
+
}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { SwelteRpcError } from '@swelte/core'
|
|
3
|
+
import { tick } from 'svelte'
|
|
4
|
+
import type { HashStringResult } from '#lib/crypto/hash.v'
|
|
5
|
+
import { hash_string } from '#lib/crypto/hash.v'
|
|
6
|
+
import type {
|
|
7
|
+
GetGreetingResult,
|
|
8
|
+
GreetNarrowResult,
|
|
9
|
+
SaveNameResult,
|
|
10
|
+
SwelteRequest,
|
|
11
|
+
} from '#routes/+page.v'
|
|
12
|
+
import { get_greeting, greet_narrow, save_name } from '#routes/+page.v'
|
|
13
|
+
import type { Session, SwelteResponse } from '#routes/api/greet.v'
|
|
14
|
+
import { greet_pure, login, make_created, serve_pdf, tick_stream } from '#routes/api/greet.v'
|
|
15
|
+
|
|
16
|
+
let name = $state('world')
|
|
17
|
+
let greeting: GetGreetingResult = $state('')
|
|
18
|
+
let saved: SaveNameResult = $state('')
|
|
19
|
+
let session: Session | null = $state(null)
|
|
20
|
+
let pureSession: Session | null = $state(null)
|
|
21
|
+
let pdfInfo = $state('')
|
|
22
|
+
let createdInfo = $state('')
|
|
23
|
+
let hashed: HashStringResult = $state('')
|
|
24
|
+
let narrow: GreetNarrowResult = $state('')
|
|
25
|
+
let lastError = $state('')
|
|
26
|
+
let ticks: string[] = $state([])
|
|
27
|
+
|
|
28
|
+
function showError(e: unknown) {
|
|
29
|
+
console.error(e)
|
|
30
|
+
if (e instanceof SwelteRpcError) {
|
|
31
|
+
lastError = e.message
|
|
32
|
+
} else {
|
|
33
|
+
lastError = String(e)
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async function loadGreeting() {
|
|
38
|
+
lastError = ''
|
|
39
|
+
try {
|
|
40
|
+
greeting = await get_greeting({ payload: name })
|
|
41
|
+
} catch (e) {
|
|
42
|
+
showError(e)
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
async function saveName() {
|
|
46
|
+
lastError = ''
|
|
47
|
+
try {
|
|
48
|
+
saved = await save_name({ payload: name })
|
|
49
|
+
} catch (e) {
|
|
50
|
+
showError(e)
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
async function hashName() {
|
|
54
|
+
lastError = ''
|
|
55
|
+
try {
|
|
56
|
+
hashed = await hash_string({ input: name })
|
|
57
|
+
} catch (e) {
|
|
58
|
+
showError(e)
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
async function doLogin() {
|
|
62
|
+
lastError = ''
|
|
63
|
+
try {
|
|
64
|
+
{
|
|
65
|
+
const res: SwelteResponse = await login({ payload: name })
|
|
66
|
+
session = JSON.parse(
|
|
67
|
+
typeof res.body === 'string' ? res.body : await (res.body as Blob).text()
|
|
68
|
+
) as Session
|
|
69
|
+
} // SwelteResponse envelope: body is raw text for JSON (Blob only on binary CT)
|
|
70
|
+
} catch (e) {
|
|
71
|
+
showError(e)
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
async function loadPure() {
|
|
75
|
+
lastError = ''
|
|
76
|
+
try {
|
|
77
|
+
pureSession = await greet_pure({ payload: name })
|
|
78
|
+
} catch (e) {
|
|
79
|
+
showError(e)
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
async function loadPdf() {
|
|
83
|
+
lastError = ''
|
|
84
|
+
try {
|
|
85
|
+
const res = await serve_pdf({})
|
|
86
|
+
const blob = res.body as Blob
|
|
87
|
+
pdfInfo = `pdf ${blob.size}B ${blob.type}`
|
|
88
|
+
} catch (e) {
|
|
89
|
+
showError(e)
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
async function loadCreated() {
|
|
93
|
+
lastError = ''
|
|
94
|
+
try {
|
|
95
|
+
const res = await make_created({})
|
|
96
|
+
createdInfo = `created ${res.status}: ${typeof res.body === 'string' ? res.body : '(binary)'}`
|
|
97
|
+
} catch (e) {
|
|
98
|
+
showError(e)
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
async function loadNarrow() {
|
|
102
|
+
lastError = ''
|
|
103
|
+
try {
|
|
104
|
+
// Narrow-args verb: NOT rpc (no {fn,args} JSON body) — GET query
|
|
105
|
+
// ?q=narrow&name=..&age=.. ; numbers JSON-stringified on the wire.
|
|
106
|
+
narrow = await greet_narrow({ name, age: 42, nick: 'al' })
|
|
107
|
+
} catch (e) {
|
|
108
|
+
showError(e)
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
async function streamTicks() {
|
|
112
|
+
lastError = ''
|
|
113
|
+
ticks = []
|
|
114
|
+
try {
|
|
115
|
+
for await (const t of tick_stream({ n: '3', wait_ms: '500' })) {
|
|
116
|
+
ticks = [...ticks, t]
|
|
117
|
+
await tick()
|
|
118
|
+
}
|
|
119
|
+
} catch (e) {
|
|
120
|
+
showError(e)
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
</script>
|
|
124
|
+
|
|
125
|
+
<main>
|
|
126
|
+
<h1>Swelte example: __NAME__</h1>
|
|
127
|
+
<input bind:value={name} placeholder="your name">
|
|
128
|
+
<div style="display:flex; gap:0.5rem; margin-top:0.5rem; flex-wrap:wrap;">
|
|
129
|
+
<button onclick={loadGreeting}>Greet (#routes GET)</button>
|
|
130
|
+
<button onclick={saveName}>Save (#routes POST)</button>
|
|
131
|
+
<button onclick={hashName}>Hash (#lib WASM)</button>
|
|
132
|
+
<button onclick={doLogin}>Login (POST verb + cookie)</button>
|
|
133
|
+
<button onclick={loadPure}>Pure (rpc typed)</button>
|
|
134
|
+
<button onclick={loadPdf}>PDF (verb binary)</button>
|
|
135
|
+
<button onclick={loadCreated}>Created (verb 201)</button>
|
|
136
|
+
<button onclick={streamTicks}>Stream (#routes SSE)</button>
|
|
137
|
+
<button onclick={loadNarrow}>Narrow (#routes GET narrow-args)</button>
|
|
138
|
+
</div>
|
|
139
|
+
{#if greeting}
|
|
140
|
+
<p>{greeting}</p>
|
|
141
|
+
{/if}
|
|
142
|
+
{#if saved}
|
|
143
|
+
<p>{saved}</p>
|
|
144
|
+
{/if}
|
|
145
|
+
{#if hashed}
|
|
146
|
+
<p>{hashed}</p>
|
|
147
|
+
{/if}
|
|
148
|
+
{#if session}
|
|
149
|
+
<p>session {session.id} for {session.user} (remember={session.remember})</p>
|
|
150
|
+
{/if}
|
|
151
|
+
{#if pureSession}
|
|
152
|
+
<p>pure {pureSession.id} for {pureSession.user}</p>
|
|
153
|
+
{/if}
|
|
154
|
+
{#if pdfInfo}
|
|
155
|
+
<p>{pdfInfo}</p>
|
|
156
|
+
{/if}
|
|
157
|
+
{#if createdInfo}
|
|
158
|
+
<p>{createdInfo}</p>
|
|
159
|
+
{/if}
|
|
160
|
+
{#if ticks.length}
|
|
161
|
+
<p>stream: {ticks.join(' | ')}</p>
|
|
162
|
+
{/if}
|
|
163
|
+
{#if narrow}
|
|
164
|
+
<p>{narrow}</p>
|
|
165
|
+
{/if}
|
|
166
|
+
{#if lastError}
|
|
167
|
+
<pre style="color:crimson; white-space:pre-wrap;">{lastError}</pre>
|
|
168
|
+
{/if}
|
|
169
|
+
<hr>
|
|
170
|
+
<h2>No-JS proof (plain forms, no fetch)</h2>
|
|
171
|
+
<form method="post" action="/">
|
|
172
|
+
<input type="hidden" name="payload" value="nojs-alice">
|
|
173
|
+
<button type="submit">Save via plain form (→ 303 → /)</button>
|
|
174
|
+
</form>
|
|
175
|
+
<form method="post" action="/api/greet?upload" enctype="multipart/form-data">
|
|
176
|
+
<input type="file" name="file">
|
|
177
|
+
<button type="submit">Upload via plain form (→ 303)</button>
|
|
178
|
+
</form>
|
|
179
|
+
</main>
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// Swelte route handler for `/`. Named fns importable from `#routes/+page.v`.
|
|
2
|
+
module plus_page
|
|
3
|
+
|
|
4
|
+
@[get]
|
|
5
|
+
pub fn get_greeting(req SwelteRequest) string {
|
|
6
|
+
name := req.query['payload'] or { '""' }
|
|
7
|
+
return 'Hello, ' + name.trim('"') + '!'
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
@[post]
|
|
11
|
+
pub fn save_name(req SwelteRequest) string {
|
|
12
|
+
name := req.form['payload'] or { '' }
|
|
13
|
+
return 'saved:' + name
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// Narrow-args probe: typed verb fn (form → query → params). Projects inline as
|
|
17
|
+
// `args: { name: string; age: number; nick?: string | null }` — retrievable via
|
|
18
|
+
// `Parameters<typeof greet_narrow>[0]`, numbers JSON-stringified on the wire.
|
|
19
|
+
@[get('narrow')]
|
|
20
|
+
pub fn greet_narrow(name string, age int, nick ?string) string {
|
|
21
|
+
return 'hi ' + name + ' age=' + age.str() + ' nick=' + (nick or { '<none>' })
|
|
22
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { loadPage, type SwelteLoadResult } from '@swelte/core'
|
|
3
|
+
import { admin_whoami } from '#routes/admin/+page.v'
|
|
4
|
+
import ErrorPage from '../+error.svelte'
|
|
5
|
+
|
|
6
|
+
let result: SwelteLoadResult<string> | null = $state(null)
|
|
7
|
+
|
|
8
|
+
async function load() {
|
|
9
|
+
result = await loadPage(() => admin_whoami({}))
|
|
10
|
+
}
|
|
11
|
+
</script>
|
|
12
|
+
|
|
13
|
+
<main>
|
|
14
|
+
<h1>admin</h1>
|
|
15
|
+
{#if result?.error}
|
|
16
|
+
<ErrorPage status={result.error.status} message={result.error.message} />
|
|
17
|
+
{:else}
|
|
18
|
+
<button onclick={load}>Who am I (guarded GET)</button>
|
|
19
|
+
{#if result?.data}
|
|
20
|
+
<p>{result.data}</p>
|
|
21
|
+
{/if}
|
|
22
|
+
{/if}
|
|
23
|
+
</main>
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// Swelte admin wrapper: nav chrome (svelte) + subtree guard (V middleware).
|
|
2
|
+
module admin_route
|
|
3
|
+
|
|
4
|
+
@[before]
|
|
5
|
+
pub fn require_session(req SwelteRequest) !SwelteRequest {
|
|
6
|
+
token := req.cookies['sess'] or { return error('missing session') }
|
|
7
|
+
mut r := req
|
|
8
|
+
r.headers['x-user'] = token
|
|
9
|
+
return r
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
@[after]
|
|
13
|
+
pub fn stamp_admin(req SwelteRequest, res SwelteResponse) !SwelteResponse {
|
|
14
|
+
return res
|
|
15
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
// Swelte API route `/api/greet`. V struct -> TS interface projection.
|
|
2
|
+
module greet
|
|
3
|
+
|
|
4
|
+
import json
|
|
5
|
+
import time
|
|
6
|
+
|
|
7
|
+
pub struct Session {
|
|
8
|
+
pub:
|
|
9
|
+
id string
|
|
10
|
+
user string
|
|
11
|
+
remember bool
|
|
12
|
+
expires_at time.Time
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
pub struct LoginArgs {
|
|
16
|
+
pub:
|
|
17
|
+
payload string
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// login is HTTP (sets the sess cookie) → a verb, not @[rpc].
|
|
21
|
+
// @[rpc] stays protocol-ignorant: typed args in, typed value out (or throw → reject).
|
|
22
|
+
@[post]
|
|
23
|
+
pub fn login(req SwelteRequest) !SwelteResponse {
|
|
24
|
+
payload := req.form['payload'] or { req.query['payload'] or { '' } }
|
|
25
|
+
if payload == '' {
|
|
26
|
+
return error('missing payload')
|
|
27
|
+
}
|
|
28
|
+
sess := Session{
|
|
29
|
+
id: 'sess_' + payload
|
|
30
|
+
user: payload
|
|
31
|
+
remember: true
|
|
32
|
+
expires_at: time.now()
|
|
33
|
+
}
|
|
34
|
+
mut res := SwelteResponse{ status: 200, headers: {'Content-Type': 'application/json'}, body: json.encode(sess) }
|
|
35
|
+
res.cookies << SwelteCookie{ name: 'sess', value: sess.id, path: '/', http_only: true, same_site: 'Lax', max_age: 604800 }
|
|
36
|
+
return res
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
@[get]
|
|
40
|
+
pub fn get_greeting(req SwelteRequest) string {
|
|
41
|
+
name := req.query['payload'] or { '""' }
|
|
42
|
+
return 'Hello, ' + name.trim('"') + '! from Swelte V backend'
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
@[delete('session')]
|
|
46
|
+
pub fn kill_session(req SwelteRequest) string {
|
|
47
|
+
return 'killed'
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
@[delete]
|
|
51
|
+
pub fn wipe_sessions(req SwelteRequest) string {
|
|
52
|
+
return 'wiped'
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Slow probe (roadmap §11): sleeps `?ms=` ms so the handler-timeout
|
|
56
|
+
// watchdog can be exercised (SWELTE_HANDLER_TIMEOUT_MS).
|
|
57
|
+
@[get('slow')]
|
|
58
|
+
pub fn slow_probe(req SwelteRequest) string {
|
|
59
|
+
ms_str := req.query['ms'] or { '' }
|
|
60
|
+
ms := ms_str.int()
|
|
61
|
+
if ms > 0 {
|
|
62
|
+
time.sleep(time.millisecond * ms)
|
|
63
|
+
}
|
|
64
|
+
return 'slow-ok'
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
@[post('upload')]
|
|
68
|
+
pub fn upload_file(req SwelteRequest) !string {
|
|
69
|
+
names := req.files['file'] or { return error('missing file') }
|
|
70
|
+
if names == '' {
|
|
71
|
+
return error('missing file')
|
|
72
|
+
}
|
|
73
|
+
return 'got:' + names
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// SSE probe (roadmap §14, emit mode): GET + Accept: text/event-stream
|
|
77
|
+
// takes over the conn; each `emit` flushes one frame immediately —
|
|
78
|
+
// sleeps pace the frames (real incremental flush, nothing buffered).
|
|
79
|
+
// Plain JSON GETs keep hitting `get_greeting` above.
|
|
80
|
+
@[sse]
|
|
81
|
+
pub fn tick_stream(req SwelteRequest, emit fn (string) bool) {
|
|
82
|
+
n := req.query['n'] or { '3' }
|
|
83
|
+
w := req.query['wait_ms'] or { '0' }
|
|
84
|
+
mut count := n.int()
|
|
85
|
+
if count < 1 {
|
|
86
|
+
count = 1
|
|
87
|
+
}
|
|
88
|
+
if count > 50 {
|
|
89
|
+
count = 50
|
|
90
|
+
}
|
|
91
|
+
wait := w.int()
|
|
92
|
+
for i in 1 .. count + 1 {
|
|
93
|
+
if wait > 0 && wait <= 10000 {
|
|
94
|
+
time.sleep(time.millisecond * wait)
|
|
95
|
+
}
|
|
96
|
+
if !emit('tick ${i}') {
|
|
97
|
+
return
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Binary probe: serves bytes with Content-Type passthrough.
|
|
103
|
+
@[get('pdf')]
|
|
104
|
+
pub fn serve_pdf(req SwelteRequest) SwelteResponse {
|
|
105
|
+
_ = req
|
|
106
|
+
data := 'PDF-fixture'.bytes()
|
|
107
|
+
mut res := swelte_bytes(data, 'application/pdf')
|
|
108
|
+
res.headers['Content-Disposition'] = 'attachment; filename="doc.pdf"'
|
|
109
|
+
return res
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Status sugar probe: (int, string) -> status+text.
|
|
113
|
+
@[get('created')]
|
|
114
|
+
pub fn make_created(req SwelteRequest) (int, string) {
|
|
115
|
+
_ = req
|
|
116
|
+
return 201, 'created'
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Pure rpc: protocol-ignorant typed call (works in #routes/ fetch AND #lib/ WASM).
|
|
120
|
+
// No HTTP types allowed (no SwelteRequest/SwelteResponse/[]u8/(int,string)) — throw → TS reject.
|
|
121
|
+
@[rpc]
|
|
122
|
+
pub fn greet_pure(args LoginArgs) !Session {
|
|
123
|
+
if args.payload == '' {
|
|
124
|
+
return error('missing payload')
|
|
125
|
+
}
|
|
126
|
+
return Session{ id: 'pure_' + args.payload, user: args.payload, remember: false, expires_at: time.now() }
|
|
127
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// Shared helper fixture (roadmap §17): UNIQUE basename across the project,
|
|
2
|
+
// so editors resolve `./sibling.v` to the real signature (ambiguous
|
|
3
|
+
// basenames like `+page.v` keep the `*.v` fallback — use `#`-aliases).
|
|
4
|
+
module sibling
|
|
5
|
+
|
|
6
|
+
pub struct GreetIn {
|
|
7
|
+
pub:
|
|
8
|
+
name string
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
@[rpc]
|
|
12
|
+
pub fn g(args GreetIn) string {
|
|
13
|
+
return 'hi ' + args.name
|
|
14
|
+
}
|