@ashley-shrok/viewmodel-shell 0.3.3 → 0.3.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.ts +29 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ashley-shrok/viewmodel-shell",
3
- "version": "0.3.3",
3
+ "version": "0.3.5",
4
4
  "description": "A server-driven UI framework where the wire format is structured enough that agents can build full-stack apps without ever opening a browser and all UI tests are pure unit tests with no browser runtime. Server returns a JSON tree of typed nodes; a thin TypeScript adapter renders it to DOM. Backend-agnostic — a .NET reference backend ships with the repo, but any language can produce the JSON contract.",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/src/index.ts CHANGED
@@ -190,11 +190,24 @@ export interface ShellOptions {
190
190
  onLoading?: (loading: boolean) => void;
191
191
  /** Called before each dispatch — merge the returned headers into every POST request. */
192
192
  getRequestHeaders?: () => Record<string, string> | Promise<Record<string, string>>;
193
+ /** Called when the server responds with a redirect URL. Defaults to window.location.href = url. */
194
+ onRedirect?: (url: string) => void;
195
+ }
196
+
197
+ export interface ShellSideEffect {
198
+ /** "set-local-storage" | "set-session-storage" — unknown types are silently ignored. */
199
+ type: string;
200
+ key?: string;
201
+ value?: string;
193
202
  }
194
203
 
195
204
  export interface ShellResponse {
196
205
  vm: ViewNode;
197
206
  state: unknown;
207
+ /** When set, the shell navigates to this URL instead of re-rendering. */
208
+ redirect?: string;
209
+ /** Applied in order before redirect or re-render. */
210
+ sideEffects?: ShellSideEffect[];
198
211
  }
199
212
 
200
213
  export class ViewModelShell {
@@ -254,6 +267,22 @@ export class ViewModelShell {
254
267
  });
255
268
  if (!res.ok) throw new Error(`Action '${action.name}' failed: ${res.status}`);
256
269
  const body = (await res.json()) as ShellResponse;
270
+ for (const effect of body.sideEffects ?? []) {
271
+ if (effect.type === "set-local-storage" && effect.key != null) {
272
+ localStorage.setItem(effect.key, effect.value ?? "");
273
+ } else if (effect.type === "set-session-storage" && effect.key != null) {
274
+ sessionStorage.setItem(effect.key, effect.value ?? "");
275
+ }
276
+ // unknown types silently ignored — forward-compatible
277
+ }
278
+ if (body.redirect) {
279
+ if (this.options.onRedirect) {
280
+ this.options.onRedirect(body.redirect);
281
+ } else {
282
+ window.location.href = body.redirect;
283
+ }
284
+ return;
285
+ }
257
286
  this.currentVm = body.vm;
258
287
  this.currentState = body.state;
259
288
  adapter.render(body.vm, (a) => this.dispatch(a));