@cosmicdrift/kumiko-renderer-web 0.79.2 → 0.79.3

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": "@cosmicdrift/kumiko-renderer-web",
3
- "version": "0.79.2",
3
+ "version": "0.79.3",
4
4
  "description": "Web-platform bindings for @cosmicdrift/kumiko-renderer. HTML default-primitives, browser history-based navigation, EventSource-backed live events, and a one-call createKumikoApp that mounts the whole stack via react-dom.",
5
5
  "license": "BUSL-1.1",
6
6
  "author": "Marc Frost <marc@cosmicdriftgamestudio.com>",
@@ -16,9 +16,9 @@
16
16
  "./styles.css": "./src/styles.css"
17
17
  },
18
18
  "dependencies": {
19
- "@cosmicdrift/kumiko-dispatcher-live": "0.79.2",
20
- "@cosmicdrift/kumiko-headless": "0.79.2",
21
- "@cosmicdrift/kumiko-renderer": "0.79.2",
19
+ "@cosmicdrift/kumiko-dispatcher-live": "0.79.3",
20
+ "@cosmicdrift/kumiko-headless": "0.79.3",
21
+ "@cosmicdrift/kumiko-renderer": "0.79.3",
22
22
  "@radix-ui/react-dialog": "^1.1.15",
23
23
  "@radix-ui/react-dropdown-menu": "^2.1.16",
24
24
  "@radix-ui/react-label": "^2.1.8",
@@ -0,0 +1,52 @@
1
+ import { describe, expect, spyOn, test } from "bun:test";
2
+ import type { Dispatcher, DispatcherError } from "@cosmicdrift/kumiko-headless";
3
+ import { postWithDownload } from "../lib/download";
4
+
5
+ function dispatcherReturning(result: unknown): Dispatcher {
6
+ return { query: async () => result } as unknown as Dispatcher; // @cast-boundary test-stub
7
+ }
8
+
9
+ describe("postWithDownload", () => {
10
+ test("success: navigates to the returned signed URL, returns null", async () => {
11
+ const assign = spyOn(window.location, "assign").mockImplementation(() => {});
12
+ const url = "https://store.example/export.zip?sig=abc";
13
+ const err = await postWithDownload(
14
+ dispatcherReturning({ isSuccess: true, data: { url } }),
15
+ "f:query:download-by-job",
16
+ { jobId: "j1" },
17
+ );
18
+ expect(err).toBeNull();
19
+ expect(assign).toHaveBeenCalledWith(url);
20
+ assign.mockRestore();
21
+ });
22
+
23
+ test("query failure: returns the dispatcher error, no navigation", async () => {
24
+ const assign = spyOn(window.location, "assign").mockImplementation(() => {});
25
+ const error: DispatcherError = {
26
+ code: "not_found",
27
+ httpStatus: 404,
28
+ i18nKey: "userDataRights.errors.download.notFound",
29
+ message: "nope",
30
+ };
31
+ const err = await postWithDownload(
32
+ dispatcherReturning({ isSuccess: false, error }),
33
+ "f:query:download-by-job",
34
+ { jobId: "j1" },
35
+ );
36
+ expect(err).toEqual(error);
37
+ expect(assign).not.toHaveBeenCalled();
38
+ assign.mockRestore();
39
+ });
40
+
41
+ test("success but no url: returns synthetic error, no navigation", async () => {
42
+ const assign = spyOn(window.location, "assign").mockImplementation(() => {});
43
+ const err = await postWithDownload(
44
+ dispatcherReturning({ isSuccess: true, data: {} }),
45
+ "f:query:download-by-job",
46
+ { jobId: "j1" },
47
+ );
48
+ expect(err?.code).toBe("download_url_missing");
49
+ expect(assign).not.toHaveBeenCalled();
50
+ assign.mockRestore();
51
+ });
52
+ });
package/src/index.ts CHANGED
@@ -122,6 +122,7 @@ export { filterByAccess, resolveDefaultId, WorkspaceShell } from "./layout/works
122
122
  export type { WorkspaceSwitcherProps } from "./layout/workspace-switcher";
123
123
  export { WorkspaceSwitcher } from "./layout/workspace-switcher";
124
124
  export { cn } from "./lib/cn";
125
+ export { postWithDownload } from "./lib/download";
125
126
  export { BareFormProvider, defaultPrimitives } from "./primitives";
126
127
  export type { ActionMenuProps, MenuItemDef } from "./primitives/action-menu";
127
128
  export { ActionMenu } from "./primitives/action-menu";
@@ -0,0 +1,29 @@
1
+ import type { Dispatcher, DispatcherError } from "@cosmicdrift/kumiko-headless";
2
+
3
+ // Dispatch a query/handler whose result carries `{ url }` (a signed,
4
+ // short-lived storage URL with `content-disposition: attachment`), then
5
+ // navigate the browser to it so the file downloads. The query rides the
6
+ // live dispatcher, so it carries the `X-CSRF-Token` header automatically —
7
+ // unlike a plain `<a href>` navigation, which sends only cookies and trips
8
+ // the CSRF double-submit check on any server-side re-dispatch.
9
+ //
10
+ // Web-only (uses `window`). Returns the dispatcher error on failure (so the
11
+ // caller can surface its i18nKey), or null on success.
12
+ export async function postWithDownload(
13
+ dispatcher: Dispatcher,
14
+ type: string,
15
+ payload: unknown,
16
+ ): Promise<DispatcherError | null> {
17
+ const res = await dispatcher.query<{ url?: string }>(type, payload);
18
+ if (!res.isSuccess) return res.error;
19
+ if (!res.data?.url) {
20
+ return {
21
+ code: "download_url_missing",
22
+ httpStatus: 502,
23
+ i18nKey: "errors.download.urlMissing",
24
+ message: "download query returned no url",
25
+ };
26
+ }
27
+ window.location.assign(res.data.url);
28
+ return null;
29
+ }