strata-react 1.3.6 → 1.3.7
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/README.md +1 -0
- package/dist/components/outbox-status.d.ts +10 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +305 -2
- package/dist/index.js.map +1 -1
- package/dist/query/outbox.d.ts +85 -0
- package/package.json +1 -1
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { HttpError } from '../lib/errors';
|
|
2
|
+
/** One write to send: a JSON body to a root-relative path of the app's own server. */
|
|
3
|
+
export type OutboxRequest = {
|
|
4
|
+
method: "POST" | "PUT" | "PATCH" | "DELETE";
|
|
5
|
+
/** Root-relative (`/api/attempts`); the app's base is put in front when it is sent. */
|
|
6
|
+
path: string;
|
|
7
|
+
json?: unknown;
|
|
8
|
+
};
|
|
9
|
+
/** A write as it waits: the request, its id, when it was queued, what it follows and the words a person sees for it. */
|
|
10
|
+
export type OutboxItem = OutboxRequest & {
|
|
11
|
+
id: string;
|
|
12
|
+
queuedAt: string;
|
|
13
|
+
/** The id of an earlier write this one depends on. */
|
|
14
|
+
after?: string;
|
|
15
|
+
/** What it is, in the product's words ("Fox's answer"), for the refused list. */
|
|
16
|
+
label?: string;
|
|
17
|
+
};
|
|
18
|
+
/** A write the server refused, kept to be shown until dismissed. */
|
|
19
|
+
export type OutboxRefusal = {
|
|
20
|
+
id: string;
|
|
21
|
+
label?: string;
|
|
22
|
+
reason: string;
|
|
23
|
+
at: string;
|
|
24
|
+
};
|
|
25
|
+
/** Where the outbox stands. */
|
|
26
|
+
export type OutboxSnapshot = {
|
|
27
|
+
/** Writes not yet sent. */
|
|
28
|
+
waiting: number;
|
|
29
|
+
/** A send is under way. */
|
|
30
|
+
sending: boolean;
|
|
31
|
+
/** Why the last try stopped, when writes are waiting: "The server could not be reached." */
|
|
32
|
+
problem: string | null;
|
|
33
|
+
/** When it tries again on its own, while writes are waiting after a failure. */
|
|
34
|
+
retryAt: Date | null;
|
|
35
|
+
/** Refused writes, oldest first. */
|
|
36
|
+
refused: readonly OutboxRefusal[];
|
|
37
|
+
/** False when the browser gives no storage: the writes wait in this page only. */
|
|
38
|
+
persisted: boolean;
|
|
39
|
+
};
|
|
40
|
+
/** How a write's first try went. */
|
|
41
|
+
export type Delivery = "sent" | "waiting" | "refused";
|
|
42
|
+
/** Options for `createOutbox`. */
|
|
43
|
+
export type OutboxOptions = {
|
|
44
|
+
/** Names the queue in storage: `strata:outbox:<name>`. One per kind of work the app keeps. */
|
|
45
|
+
name: string;
|
|
46
|
+
/** Sends one write; default `fetchJson` to the path with the app's base. Throw an `HttpError` to have a 4xx refused. */
|
|
47
|
+
send?: (item: OutboxItem) => Promise<unknown>;
|
|
48
|
+
/** Where the queue is kept. Default `window.localStorage`; `null` keeps it in memory. */
|
|
49
|
+
storage?: Storage | null;
|
|
50
|
+
/** The first wait before trying again, doubled each time up to `maxRetryMs`. Default 2000. */
|
|
51
|
+
retryMs?: number;
|
|
52
|
+
/** Default 60000. */
|
|
53
|
+
maxRetryMs?: number;
|
|
54
|
+
};
|
|
55
|
+
/** A queue of writes kept on the device and sent when the server can take them. */
|
|
56
|
+
export type Outbox = {
|
|
57
|
+
/** Queues a write and tries to send the queue. `id` defaults to a random one; give the write's own id (an event id) so a refusal can be matched. */
|
|
58
|
+
add(request: OutboxRequest & {
|
|
59
|
+
id?: string;
|
|
60
|
+
after?: string;
|
|
61
|
+
label?: string;
|
|
62
|
+
}): {
|
|
63
|
+
id: string;
|
|
64
|
+
delivered: Promise<Delivery>;
|
|
65
|
+
};
|
|
66
|
+
/** Sends what waits, oldest first, until one fails. Resolves when this pass ends. */
|
|
67
|
+
flush(): Promise<void>;
|
|
68
|
+
snapshot(): OutboxSnapshot;
|
|
69
|
+
subscribe(listener: () => void): () => void;
|
|
70
|
+
/** Calls `listener` with each write this page has sent, whenever it went (the first try, a retry, or a write an earlier
|
|
71
|
+
* visit left): refresh what the server's answer changes here, as a `delivered` first try misses later sends. */
|
|
72
|
+
onSent(listener: (item: OutboxItem) => void): () => void;
|
|
73
|
+
/** Forgets a refused write (or all of them). */
|
|
74
|
+
dismiss(id?: string): void;
|
|
75
|
+
/** Sends what waits now, and again whenever the browser comes back online, the page is shown or another tab adds. Returns the stop. */
|
|
76
|
+
start(): () => void;
|
|
77
|
+
};
|
|
78
|
+
/** Whether a failure is the server's answer for good (refused) rather than for now (waiting). */
|
|
79
|
+
export declare function isRefusal(error: unknown): error is HttpError;
|
|
80
|
+
/** A queue of writes kept on the device (localStorage) and sent in order when the server can take them; see `useOutbox` and `OutboxStatus`. */
|
|
81
|
+
export declare function createOutbox({ name, send, storage, retryMs, maxRetryMs }: OutboxOptions): Outbox;
|
|
82
|
+
/** Where `outbox` stands, re-rendering when it changes. Call `outbox.start()` once for the app (`useOutboxStarted`). */
|
|
83
|
+
export declare function useOutbox(outbox: Outbox): OutboxSnapshot;
|
|
84
|
+
/** Starts `outbox` (sends what waits, and again on reconnect) for as long as the calling component is mounted: put it in the app's shell. */
|
|
85
|
+
export declare function useOutboxStarted(outbox: Outbox): void;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "strata-react",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.7",
|
|
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",
|