Skip to content

Commit 90cb697

Browse files
committed
A few more examples in docs
1 parent 277a7bd commit 90cb697

File tree

5 files changed

+154
-40
lines changed

5 files changed

+154
-40
lines changed

packages/docs-site/src/content/docs/reference/30-wiring-routing.mdx

Lines changed: 92 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -39,39 +39,34 @@ o.country = "Germany"
3939
version 1.5
4040
4141
const lorem = {
42-
"ipsum": "dolor sit amet",
43-
"consetetur": 8.9
42+
"ipsum":"dolor sit amet",
43+
"consetetur":8.9
4444
}
4545
4646
bridge Query.greet {
47+
# witnout alias
48+
with const
49+
# with alias
50+
with input as inp
51+
with output as o
4752
48-
# witnout alias
49-
50-
with const
51-
52-
# with alias
53-
54-
with input as inp
55-
with output as o
56-
57-
# Pull value from input node
58-
59-
o.nameFromInput <- inp.name
60-
61-
# Constants
62-
63-
o.constNumer = 7.7
64-
o.constString = "just a string"
53+
# Pull value from input node
54+
o.nameFromInput <- inp.name
6555
66-
# Bridge variables also need pulling
56+
# Constants
57+
o.constNumer = 7.7
58+
o.constString = "just a string"
6759
68-
o.constFromConst <- const.lorem
60+
# Bridge variables also need pulling
61+
o.constFromConst <- const.lorem
6962
}
7063
`}
7164

72-
inputJson={`{
65+
inputJson={`
66+
{
7367
"name": "Hello Bridge"
74-
}`}
68+
}
69+
`}
7570

7671
autoRun={true}
7772
/>
@@ -106,6 +101,47 @@ api.body.user {
106101
107102
```
108103

104+
<MiniPlaygroundModal
105+
client:load
106+
bridge={`
107+
version 1.5
108+
109+
bridge Query.createPayload {
110+
with std.audit
111+
with input as i
112+
with output as o
113+
114+
audit.v1.value = 1
115+
audit.v1.deep.isTrue = false
116+
audit.v1.deep.input.first <- i.first
117+
audit.v1.deep.input.last <- i.last
118+
119+
# Equal to the above
120+
audit.v2 {
121+
.value = 1
122+
.deep {
123+
.isTrue = false
124+
.input {
125+
.first <- i.first
126+
.last <- i.last
127+
}
128+
}
129+
}
130+
131+
o <- audit
132+
}
133+
`}
134+
135+
inputJson={`
136+
{
137+
"first": "Bob",
138+
"last": "Bobik"
139+
}
140+
`}
141+
142+
autoRun={true}
143+
/>
144+
109145
## 3. Spreading Objects (`...`)
110146

111147
If you want to map an entire object into a specific target without writing out every single field, you can use the spread operator (`...`) inside a Path Scoping Block.
@@ -128,6 +164,37 @@ bridge Query.getUser {
128164
}
129165
130166
```
167+
<MiniPlaygroundModal
168+
client:load
169+
bridge={`
170+
version 1.5
171+
172+
bridge Query.createPayload {
173+
with std.audit
174+
with input as i
175+
with output as o
176+
177+
audit.v2 {
178+
...i
179+
.last = "Overridden Last Name"
180+
.deep {
181+
.isTrue = false
182+
}
183+
}
184+
185+
o <- audit
186+
}
187+
`}
188+
189+
inputJson={`
190+
{
191+
"first": "Bob",
192+
"last": "Bobik"
193+
}
194+
`}
195+
196+
autoRun={true}
197+
/>
131198

132199
## 4. The Universal `alias`
133200

@@ -170,6 +237,8 @@ o.pass <- isPassing
170237
o.total <- total
171238
172239
```
240+
<MiniPlaygroundModal client:load exampleId="alias" autoRun={true} />
241+
173242

174243
## 5. Forcing Execution (`force`)
175244

packages/docs-site/worker-configuration.d.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
/* eslint-disable */
2-
// Generated by Wrangler by running `wrangler types` (hash: e41227086db6ad8bb19b68d77b165868)
2+
// Generated by Wrangler by running `wrangler types` (hash: 3687270ff097b92829087e49bb8b5282)
33
// Runtime types generated with workerd@1.20260301.1 2026-02-24 global_fetch_strictly_public,nodejs_compat
44
declare namespace Cloudflare {
5-
interface GlobalProps {
6-
mainModule: typeof import("./dist/_worker.js/index");
7-
}
85
interface Env {
96
SHARES: KVNamespace;
107
ASSETS: Fetcher;

packages/playground/src/App.tsx

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect } from "react";
1+
import { useEffect, useCallback } from "react";
22
import { Playground } from "./Playground";
33
import { examples } from "./examples";
44
import { usePlaygroundState } from "./usePlaygroundState";
@@ -26,18 +26,43 @@ export function App() {
2626
context,
2727
} = state;
2828

29-
// Load shared playground state from ?s=<id> on first mount
29+
// Load shared playground state from ?s=<id> on first mount,
30+
// or select built-in example from #example-id hash.
3031
useEffect(() => {
3132
const id = getShareIdFromUrl();
32-
if (!id) return;
33-
clearShareIdFromUrl();
34-
loadShare(id)
35-
.then((payload) => state.loadSharePayload(payload))
36-
.catch(() => {
37-
// silently ignore — invalid/expired share id
38-
});
33+
if (id) {
34+
clearShareIdFromUrl();
35+
// Clear the hash too — a custom share supersedes any example reference
36+
window.history.replaceState(
37+
null,
38+
"",
39+
window.location.pathname + window.location.search,
40+
);
41+
loadShare(id)
42+
.then((payload) => state.loadSharePayload(payload))
43+
.catch(() => {
44+
// silently ignore — invalid/expired share id
45+
});
46+
return;
47+
}
48+
const hash = window.location.hash.slice(1);
49+
if (hash) {
50+
const idx = examples.findIndex((e) => e.id === hash);
51+
if (idx !== -1) selectExample(idx);
52+
}
3953
}, []); // eslint-disable-line react-hooks/exhaustive-deps
4054

55+
const handleSelectExample = useCallback(
56+
(index: number) => {
57+
selectExample(index);
58+
const ex = examples[index];
59+
if (ex) {
60+
window.history.replaceState(null, "", "#" + ex.id);
61+
}
62+
},
63+
[selectExample],
64+
);
65+
4166
const isStandalone = mode === "standalone";
4267

4368
return (
@@ -57,7 +82,7 @@ export function App() {
5782
<span className="text-xs text-slate-600">Example:</span>
5883
<Select
5984
value={String(exampleIndex)}
60-
onValueChange={(v) => selectExample(Number(v))}
85+
onValueChange={(v) => handleSelectExample(Number(v))}
6186
>
6287
<SelectTrigger className="w-44">
6388
<SelectValue />
@@ -100,7 +125,7 @@ export function App() {
100125
<span className="text-xs text-slate-600">Example:</span>
101126
<Select
102127
value={String(exampleIndex)}
103-
onValueChange={(v) => selectExample(Number(v))}
128+
onValueChange={(v) => handleSelectExample(Number(v))}
104129
>
105130
<SelectTrigger className="w-full">
106131
<SelectValue />

packages/playground/src/DialogPlayground.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useState, useEffect, useRef } from "react";
22
import { Playground } from "./Playground";
33
import { usePlaygroundState } from "./usePlaygroundState";
4+
import { examples } from "./examples";
45
import { Button } from "@/components/ui/button";
56
import { Dialog, DialogContent, DialogTrigger } from "@/components/ui/dialog";
67

@@ -9,10 +10,12 @@ export type DialogPlaygroundProps = {
910
label?: string;
1011
/** Index of the built-in example to load initially (default: 0). */
1112
initialExample?: number;
13+
/** ID of a built-in example to load initially. Takes precedence over `initialExample`. */
14+
exampleId?: string;
1215
/** Disables switching to GraphQL mode. */
1316
hideGqlSwitch?: boolean;
1417

15-
/** Optional custom bridge DSL. If provided, overrides `initialExample`. */
18+
/** Optional custom bridge DSL. If provided, overrides `initialExample`/`exampleId`. */
1619
bridge?: string;
1720
/** Optional custom context JSON string. */
1821
contextStr?: string;
@@ -82,6 +85,7 @@ function PlaygroundInner({
8285
export function DialogPlayground({
8386
label = "Open Playground",
8487
initialExample = 0,
88+
exampleId,
8589
hideGqlSwitch = true,
8690
bridge,
8791
contextStr,
@@ -90,6 +94,11 @@ export function DialogPlayground({
9094
outputFields,
9195
autoRun,
9296
}: DialogPlaygroundProps) {
97+
const resolvedExample =
98+
exampleId !== undefined
99+
? (examples.findIndex((e) => e.id === exampleId) ?? 0)
100+
: initialExample;
101+
93102
const [open, setOpen] = useState(false);
94103

95104
return (
@@ -110,7 +119,7 @@ export function DialogPlayground({
110119
>
111120
{open && (
112121
<PlaygroundInner
113-
initialExample={initialExample}
122+
initialExample={resolvedExample}
114123
hideGqlSwitch={hideGqlSwitch}
115124
bridge={bridge}
116125
contextStr={contextStr}

0 commit comments

Comments
 (0)