@yourrealm/sdk 0.0.0-alpha.1451.8a8a7d44 → 0.0.0-alpha.1458.c91ff3b5

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yourrealm/sdk",
3
- "version": "0.0.0-alpha.1451.8a8a7d44",
3
+ "version": "0.0.0-alpha.1458.c91ff3b5",
4
4
  "description": "The Realm SDK: createApp, the r.* permission factories, the ui surface, the realm-sdk CLI and test harness.",
5
5
  "license": "AGPL-3.0-only WITH Classpath-exception-2.0",
6
6
  "repository": {
@@ -106,9 +106,9 @@
106
106
  "@std/path": "npm:@jsr/std__path@^1",
107
107
  "@std/tar": "npm:@jsr/std__tar@^0.1",
108
108
  "@std/testing": "npm:@jsr/std__testing@^1",
109
- "@local/docker": "0.1.0",
110
109
  "@local/logger": "0.1.0",
111
- "@yourrealm/noise": "0.1.0"
110
+ "@yourrealm/noise": "0.1.0",
111
+ "@local/docker": "0.1.0"
112
112
  },
113
113
  "scripts": {
114
114
  "typecheck": "deno check",
package/src/sandbox.ts CHANGED
@@ -1241,14 +1241,21 @@ export async function runGate(
1241
1241
  // ---- widget prefill (loader request stitching) -----------------------------
1242
1242
 
1243
1243
  // Run every loader's request(seedState, config) inside the VM and collect the
1244
- // descriptors. `seedState` is the surface's `stateSchema.parse({})` — the exact
1245
- // seed the iframe runtime mounts with — so a descriptor produced here matches
1244
+ // descriptors. `seedState` follows `seedFrom` in ui-runtime/mount.ts — the page
1245
+ // query parsed through `stateSchema`, else its `{}`-parse, else `{}` — the exact
1246
+ // seed the iframe runtime mounts with, so a descriptor produced here matches
1246
1247
  // the query key the runtime later builds from `request(state, config)`, and the
1247
1248
  // prefilled data seeds cleanly with no first-paint refetch. A loader whose
1248
1249
  // request is null (or throws) on the seed is lazy for this config → omitted.
1249
- function stitchSnippet(collection: "pages" | "widgets", key: string, config: unknown): string {
1250
+ function stitchSnippet(
1251
+ collection: "pages" | "widgets",
1252
+ key: string,
1253
+ config: unknown,
1254
+ search: Record<string, string> | undefined,
1255
+ ): string {
1250
1256
  return [
1251
1257
  `var __config = ${JSON.stringify(config ?? null)};`,
1258
+ `var __search = ${JSON.stringify(search ?? {})};`,
1252
1259
  `if (__config == null) __config = {};`, // mirror the runtime's `opts.config ?? {}`
1253
1260
  `(function () {`,
1254
1261
  ` var ui = __app.ui;`,
@@ -1256,7 +1263,8 @@ function stitchSnippet(collection: "pages" | "widgets", key: string, config: unk
1256
1263
  ` if (!surface) return JSON.stringify({});`,
1257
1264
  ` var seedState = {};`,
1258
1265
  ` if (surface.stateSchema && typeof surface.stateSchema.safeParse === "function") {`,
1259
- ` var __p = surface.stateSchema.safeParse({});`,
1266
+ ` var __q = surface.stateSchema.safeParse(__search);`,
1267
+ ` var __p = __q.success ? __q : surface.stateSchema.safeParse({});`,
1260
1268
  ` seedState = __p.success ? __p.data : {};`,
1261
1269
  ` }`,
1262
1270
  ` var out = {};`,
@@ -1289,10 +1297,12 @@ export async function stitchLoaderRequests(opts: {
1289
1297
  kind: "page" | "widget";
1290
1298
  key: string;
1291
1299
  config: unknown;
1300
+ // A page's URL query (see `seedFrom` in ui-runtime/mount.ts). Absent → `{}`.
1301
+ search?: Record<string, string>;
1292
1302
  }): Promise<Record<string, RequestManifest | null>> {
1293
1303
  const vm = getVm({ key: `prefill:${opts.appId}:${opts.bundleId}`, bundle: opts.bundle });
1294
1304
  const collection = opts.kind === "widget" ? "widgets" : "pages";
1295
- const result = await vm.call(stitchSnippet(collection, opts.key, opts.config));
1305
+ const result = await vm.call(stitchSnippet(collection, opts.key, opts.config, opts.search));
1296
1306
  return (result ?? {}) as Record<string, RequestManifest | null>;
1297
1307
  }
1298
1308
 
@@ -67,11 +67,15 @@ interface RenderApi {
67
67
  queryClient: QueryClient;
68
68
  }
69
69
 
70
+ interface StateSchemaLike {
71
+ safeParse(v: unknown): { success: boolean; data?: unknown };
72
+ }
73
+
70
74
  interface SurfaceLike {
71
75
  title: string;
72
- // The live state schema rides the bundle; its `{}`-parse seeds the
73
- // widget's initial state.
74
- stateSchema?: { safeParse(v: unknown): { success: boolean; data?: unknown } };
76
+ // The live state schema rides the bundle and seeds the initial state —
77
+ // see `seedFrom`.
78
+ stateSchema?: StateSchemaLike;
75
79
  loaders?: Record<string, SurfaceLoader>;
76
80
  actions?: Record<string, SurfaceAction>;
77
81
  render: (api: RenderApi) => ComponentChildren;
@@ -95,6 +99,9 @@ interface MountOpts {
95
99
  // as the loader's queryFn output *before* merge, so the runtime applies
96
100
  // merge to it too — see `applyMerge`.
97
101
  initialData?: Record<string, unknown>;
102
+ // A page's URL query, flat (last occurrence wins), inlined by Home. Seeds
103
+ // the initial state — see `seedFrom`. Widgets never get one.
104
+ search?: Record<string, string>;
98
105
  }
99
106
 
100
107
  interface PendingHandler {
@@ -201,6 +208,21 @@ function applyMerge(loader: SurfaceLoader, raw: unknown): unknown {
201
208
  return raw;
202
209
  }
203
210
 
211
+ // The surface's initial state: the page query parsed through `stateSchema`,
212
+ // else the `{}`-parse, else `{}`. A query that fails to parse is dropped
213
+ // whole, never key by key. Mirrored by `stitchSnippet` in sandbox.ts, so
214
+ // Home's prefill runs `request()` on this exact seed — keep the two in step.
215
+ export function seedFrom(
216
+ schema: StateSchemaLike | undefined,
217
+ search: Record<string, string> | undefined,
218
+ ): unknown {
219
+ if (!schema) return {};
220
+ const fromQuery = schema.safeParse(search ?? {});
221
+ if (fromQuery.success) return fromQuery.data;
222
+ const empty = schema.safeParse({});
223
+ return empty.success ? empty.data : {};
224
+ }
225
+
204
226
  // Assemble the render fn's props from the handles built this render. Pure, so
205
227
  // the prop wiring is testable without a DOM.
206
228
  export function renderApi(args: {
@@ -240,14 +262,13 @@ export function mount(opts: MountOpts): void {
240
262
  },
241
263
  });
242
264
 
243
- // The widget's config (resolved by Home) and initial state (the live
244
- // stateSchema's `{}`-parse). `seedState` is a stable reference so
265
+ // The widget's config (resolved by Home) and initial state (`seedFrom`).
266
+ // `seedState` is a stable reference so
245
267
  // `initialData` applies only while the widget is still at its seed: that
246
268
  // gates the prefill to the query key Home actually ran to produce it
247
269
  // (`request(seedState, config)`), so a re-keyed loader can't inherit it.
248
270
  const config = opts.config ?? {};
249
- const seedParse = opts.surface.stateSchema?.safeParse({});
250
- const seedState: unknown = seedParse?.success ? seedParse.data : {};
271
+ const seedState = seedFrom(opts.surface.stateSchema, opts.search);
251
272
 
252
273
  // Capture the loader/action name sets ONCE, before the component
253
274
  // renders. The render loop calls one useQuery per loader and one