@hadoku/game-ui 0.1.190 → 0.1.192

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/dist/http.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import type { ZodType } from 'zod';
1
2
  /** `fetch`, narrowed to what the clients call, so a test can pass a stub. */
2
3
  export type Fetch = (input: string, init?: RequestInit) => Promise<Response>;
3
4
  /** The page's own transport: every client's default, and what a page-long read is kept for (`readOnce`). */
@@ -26,3 +27,33 @@ export declare function serverMessage(body: unknown, status: number): string;
26
27
  export declare function getJson(url: string, fetchImpl?: Fetch): Promise<Response>;
27
28
  /** A JSON body to an `/api` URL. `init` adds to the request, never replaces the cookie or the content type. */
28
29
  export declare function sendJson(url: string, method: string, body: unknown, fetchImpl?: Fetch, init?: RequestInit): Promise<Response>;
30
+ /** A write's answer: the reply's body, or the sentence a person reads instead. */
31
+ export type Written<T> = {
32
+ ok: true;
33
+ data: T;
34
+ } | {
35
+ ok: false;
36
+ message: string;
37
+ };
38
+ /**
39
+ * How a read's failures read: a refusal is "<failed>: <the server's sentence>." (the sentence alone
40
+ * when `failed` is null), and a reply its schema refuses is `miss`, whole.
41
+ */
42
+ export interface Wording {
43
+ failed: string | null;
44
+ miss: string;
45
+ }
46
+ /**
47
+ * A write to an `/api` URL: `payload` as JSON (`undefined`: no body, as a DELETE sends), answered with
48
+ * its reply's body unread, or the sentence to show — unreachable, or the server's refusal led by
49
+ * `failed` (null: its sentence alone). `replyAs` reads the body.
50
+ */
51
+ export declare function writeJson(url: string, method: string, payload: unknown, failed: string | null, fetchImpl?: Fetch): Promise<Written<unknown>>;
52
+ /** A write's answer as `schema` reads its body: a body it refuses is `miss`. */
53
+ export declare function replyAs<T>(written: Written<unknown>, schema: ZodType<T>, miss: string): Written<T>;
54
+ /**
55
+ * A GET of an `/api` URL, its body as `schema` reads it; throws the sentence a person reads. A string
56
+ * `what` names what is read: "Could not load <what>: <the server's sentence>." and "The server sent
57
+ * something this app cannot read as <what>.".
58
+ */
59
+ export declare function readJson<T>(url: string, schema: ZodType<T>, what: string | Wording, fetchImpl?: Fetch): Promise<T>;
package/dist/http.js CHANGED
@@ -81,3 +81,46 @@ export function sendJson(url, method, body, fetchImpl = pageFetch, init = {}) {
81
81
  body: JSON.stringify(body),
82
82
  });
83
83
  }
84
+ /** What every call says when the request never got an answer. */
85
+ const UNREACHABLE = 'Could not reach the server.';
86
+ /** The request, then its body; a refusal led by `failed` (null: the server's sentence alone). */
87
+ async function answer(request, failed) {
88
+ let res;
89
+ try {
90
+ res = await request();
91
+ }
92
+ catch {
93
+ return { ok: false, message: UNREACHABLE };
94
+ }
95
+ const body = await bodyOf(res);
96
+ if (res.ok)
97
+ return { ok: true, data: body };
98
+ return { ok: false, message: failed === null ? serverMessage(body, res.status) : `${failed}: ${serverMessage(body, res.status)}.` };
99
+ }
100
+ /**
101
+ * A write to an `/api` URL: `payload` as JSON (`undefined`: no body, as a DELETE sends), answered with
102
+ * its reply's body unread, or the sentence to show — unreachable, or the server's refusal led by
103
+ * `failed` (null: its sentence alone). `replyAs` reads the body.
104
+ */
105
+ export function writeJson(url, method, payload, failed, fetchImpl = pageFetch) {
106
+ return answer(() => (payload === undefined ? fetchImpl(url, { method, credentials: 'same-origin', headers: { accept: 'application/json' } }) : sendJson(url, method, payload, fetchImpl)), failed);
107
+ }
108
+ /** A write's answer as `schema` reads its body: a body it refuses is `miss`. */
109
+ export function replyAs(written, schema, miss) {
110
+ if (!written.ok)
111
+ return written;
112
+ const parsed = schema.safeParse(written.data);
113
+ return parsed.success ? { ok: true, data: parsed.data } : { ok: false, message: miss };
114
+ }
115
+ /**
116
+ * A GET of an `/api` URL, its body as `schema` reads it; throws the sentence a person reads. A string
117
+ * `what` names what is read: "Could not load <what>: <the server's sentence>." and "The server sent
118
+ * something this app cannot read as <what>.".
119
+ */
120
+ export async function readJson(url, schema, what, fetchImpl = pageFetch) {
121
+ const { failed, miss } = typeof what === 'string' ? { failed: `Could not load ${what}`, miss: `The server sent something this app cannot read as ${what}.` } : what;
122
+ const read = replyAs(await answer(() => getJson(url, fetchImpl), failed), schema, miss);
123
+ if (!read.ok)
124
+ throw new Error(read.message);
125
+ return read.data;
126
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hadoku/game-ui",
3
- "version": "0.1.190",
4
- "gitHead": "ebb090b33c52c01759f4b26e99a22be659f7c601",
3
+ "version": "0.1.192",
4
+ "gitHead": "b5239194b4ee158dc5648320e27c0e59565ee057",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "description": "The browser parts any hosted game shares, above ui-shell: the room shell and session, seats and looks, the room route, the front page's generic pieces (gate, polls, gallery, patch notes), the bundle slot and relay channel a game's client mounts in, settings and trim screens, the HTTP and socket helpers, and the generic components (modal, popover, avatar, action button, radio cards). Knows no game; Magic's UI (games/magic/ui) plugs in, composed by the site (apps/ui). MIT; imports only react, react-dom, socket.io-client, @wolffm/* and the platform packages.",
@@ -53,8 +53,8 @@
53
53
  "react-dom": "^19.2.8"
54
54
  },
55
55
  "dependencies": {
56
- "@wolffm/platform-wire": "npm:@hadoku/platform-wire@0.1.194",
57
- "@wolffm/ui-shell": "npm:@hadoku/ui-shell@0.1.194",
56
+ "@wolffm/platform-wire": "npm:@hadoku/platform-wire@0.1.196",
57
+ "@wolffm/ui-shell": "npm:@hadoku/ui-shell@0.1.196",
58
58
  "@wolffm/prefs-client": "npm:@hadoku/prefs-client@^1.2.3",
59
59
  "@wolffm/rooms-client": "npm:@hadoku/rooms-client@^0.45.150",
60
60
  "@wolffm/rooms-protocol": "npm:@hadoku/rooms-protocol@^0.45.150",