strata-react 1.3.8 → 1.3.9

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.
@@ -17,7 +17,7 @@ export type Api = {
17
17
  };
18
18
  /**
19
19
  * A small client over `fetchJson` for the app's own server: root-relative paths get the app's base (so it works behind a reverse proxy),
20
- * and `post`, `put` and `patch` send their body as JSON. `api.post<Learner>("/api/learners", { nickname })`.
20
+ * and `post`, `put` and `patch` send their body as JSON (a `Blob`, bytes, `FormData` or a stream is sent as it is, with the Blob's own type). `api.post<Learner>("/api/learners", { nickname })`.
21
21
  */
22
22
  export declare function createApi({ base, ...options }?: ApiOptions): Api;
23
23
  export {};
@@ -0,0 +1,48 @@
1
+ import { QueryKey } from '@tanstack/react-query';
2
+ import { SpeechPart } from '../lib/speech';
3
+ import * as React from "react";
4
+ /** The clips an app has: each line's words and the URL of its recording (root-relative; the app's base is put in front). */
5
+ export type Clips = Readonly<Record<string, string>>;
6
+ /** The query key of the clip list: invalidate it after a recording is kept, `queryClient.invalidateQueries({ queryKey: clipsQueryKey })`. */
7
+ export declare const clipsQueryKey: QueryKey;
8
+ /** What `useClips` gives. */
9
+ export type ClipsContext = {
10
+ /** The list has arrived or failed: a prompt spoken on show can go now. Always true outside a provider. */
11
+ ready: boolean;
12
+ /** The language the provider names, for `speak`'s voice. */
13
+ lang?: string;
14
+ /** The parts of `text`: each line with its clip (through `withBase`) or null, and what the browser's voice says when it has none. */
15
+ parts(text: string): SpeechPart[];
16
+ /** Says `text` now, from its clips where it has them, else with the browser's voice. Returns whether it started. */
17
+ say(text: string): boolean;
18
+ /** What `text` is said as, for a button's name: the lines' words, a described line as described. */
19
+ words(text: string): string;
20
+ };
21
+ /** Props of `ClipsProvider`. */
22
+ export type ClipsProviderProps = {
23
+ /** Reads the list. Called once, and again after `clipsQueryKey` is invalidated. */
24
+ load: () => Promise<Clips>;
25
+ /** BCP 47 language of the prompts; picks the browser's voice for lines with no clip. */
26
+ lang?: string;
27
+ /** Cuts a prompt into the lines that have a clip each. Default: the whole text is one line. */
28
+ split?: (text: string) => string[];
29
+ /** What the browser's voice says for a line with no clip, when the line itself should not be read out. Return `undefined` to read it. */
30
+ describe?: (line: string) => string | undefined;
31
+ /** Replaces `clipsQueryKey`, for an app with two lists. */
32
+ queryKey?: QueryKey;
33
+ children: React.ReactNode;
34
+ };
35
+ /** Reads the app's clip list once and gives `useClips`, `SayAgain` and `useSayOnShow` each line's recording. Put it in the shell, inside the query provider. */
36
+ export declare function ClipsProvider({ load, lang, split, describe, queryKey, children }: ClipsProviderProps): React.JSX.Element;
37
+ /** The app's clips: `const { say, parts, words, ready } = useClips()`. The browser's voice alone outside a `ClipsProvider`. */
38
+ export declare function useClips(): ClipsContext;
39
+ /** Options for `useSayOnShow`. */
40
+ export type SayOnShowOptions = {
41
+ /** The person has turned speech off: nothing is said. */
42
+ muted?: boolean;
43
+ };
44
+ /**
45
+ * Says `text` once when it appears and the clips are `ready`, and again when `text` changes; stops it on unmount so a prompt does not talk
46
+ * after its screen is gone. `useSayOnShow(question.text)`.
47
+ */
48
+ export declare function useSayOnShow(text: string | null | undefined, { muted }?: SayOnShowOptions): void;
@@ -0,0 +1,33 @@
1
+ /** Options for `createReserve`. */
2
+ export type ReserveOptions<T, K extends string = string> = {
3
+ /** Names the reserve in storage: `strata:reserve:<name>:<key>`. One per kind of item the app keeps. */
4
+ name: string;
5
+ /** How many items to keep for each key. */
6
+ size: number;
7
+ /** Fetches `count` more items for a key from the server. */
8
+ fetch: (key: K, count: number) => Promise<readonly T[]>;
9
+ /** Whether a kept item was made the way a fresh one is. Kept items for which this is false are dropped. Default: all are. */
10
+ sameMake?: (kept: T, fresh: T) => boolean;
11
+ /** Where the items are kept. Default `window.localStorage`; `null` keeps nothing. */
12
+ storage?: Storage | null;
13
+ };
14
+ /** Items kept on the device for offline use. */
15
+ export type Reserve<T, K extends string = string> = {
16
+ /** Removes and returns the oldest item kept for `key`, or `undefined` when there is none. */
17
+ take(key: K): T | undefined;
18
+ /** After a fresh item arrived: drops kept items made differently, then tops up when fewer than half remain. Never rejects. */
19
+ refresh(key: K, fresh: T): Promise<void>;
20
+ /** How many items are kept for `key`. */
21
+ kept(key: K): number;
22
+ /** False when the browser gives no storage: nothing is kept. */
23
+ readonly persisted: boolean;
24
+ /** Replaces what is kept for `key` (up to `size`): for a test or a story that starts with a full reserve. */
25
+ seed(key: K, items: readonly T[]): void;
26
+ /** Forgets what is kept for `key`, or for every key. */
27
+ clear(key?: K): void;
28
+ subscribe(listener: () => void): () => void;
29
+ };
30
+ /** Items the server made ahead, kept on the device (localStorage) and used once each; see `useReserve`. */
31
+ export declare function createReserve<T, K extends string = string>({ name, size, fetch, sameMake, storage }: ReserveOptions<T, K>): Reserve<T, K>;
32
+ /** How many items `reserve` keeps for `key`, re-rendering as they are taken, topped up or changed by another tab: "6 rounds kept for offline". */
33
+ export declare function useReserve<T, K extends string>(reserve: Reserve<T, K>, key: K): number;
@@ -21,6 +21,11 @@ type Body<O> = O extends {
21
21
  [j in Json]: infer B;
22
22
  };
23
23
  } ? B : never : never;
24
+ type RawBody<O> = O extends {
25
+ requestBody?: infer R;
26
+ } ? [NonNullable<R>] extends [never] ? never : NonNullable<R> extends {
27
+ content: infer C;
28
+ } ? [Exclude<keyof C, Json>] extends [never] ? never : Blob | BufferSource : never : never;
24
29
  type Answer<O> = O extends {
25
30
  responses: infer R;
26
31
  } ? R extends {
@@ -59,7 +64,9 @@ type Request<O> = (HasKeys<Params<O, "path">> extends true ? {
59
64
  query?: undefined;
60
65
  }) & Omit<RequestInit, "method" | "body">;
61
66
  type Optional<O> = [RequiredKeys<Request<O>>] extends [never] ? [request?: Request<O>] : [request: Request<O>];
62
- type Args<O, WithBody extends boolean> = WithBody extends true ? [Body<O>] extends [never] ? Optional<O> : [request: Request<O> & {
67
+ type Args<O, WithBody extends boolean> = WithBody extends true ? [Body<O>] extends [never] ? [RawBody<O>] extends [never] ? Optional<O> : [request: Request<O> & {
68
+ body: RawBody<O>;
69
+ }] : [request: Request<O> & {
63
70
  json: Body<O>;
64
71
  }] : Optional<O>;
65
72
  /** A client whose paths, parameters, JSON bodies and answers come from `strata-api-types`' `paths`. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "strata-react",
3
- "version": "1.3.8",
3
+ "version": "1.3.9",
4
4
  "description": "React + Tailwind v4 components that ship structure, behaviour, states and a token contract, and no look: each product brings a theme generated from its own subject.",
5
5
  "repository": {
6
6
  "type": "git",