@composable-os/client 0.1.0-beta.11 → 0.1.0-beta.13
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/account.d.ts +1461 -0
- package/dist/account.js +845 -0
- package/dist/agreements.d.ts +529 -0
- package/dist/agreements.js +518 -0
- package/dist/binding.d.ts +27 -0
- package/dist/binding.js +52 -9
- package/dist/client.d.ts +1 -0
- package/dist/client.js +7 -4
- package/dist/hooks.d.ts +50 -5
- package/dist/hooks.js +14 -3
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/notes.d.ts +169 -0
- package/dist/notes.js +108 -0
- package/dist/owner.d.ts +686 -0
- package/dist/owner.js +197 -0
- package/dist/session.d.ts +13 -0
- package/dist/session.js +46 -1
- package/package.json +1 -1
package/dist/owner.js
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
// The owner's Today for a staff app (stage 60 step 6, ADR 0872): the headless entry the native
|
|
2
|
+
// block (`owner-today`, `owner-approvals`) and the web admin's Today share. Functions and hooks
|
|
3
|
+
// over the owner's client (`roles/owner.ts`) or the staff client, no markup and no platform API.
|
|
4
|
+
// Web and native share this logic, not their screens.
|
|
5
|
+
//
|
|
6
|
+
// It holds no rules. Which items a person sees, their numbers, whether one needs attention, the
|
|
7
|
+
// order they come in, who may approve what and when a second step is asked are the host's answers
|
|
8
|
+
// (`today.get`, `approval.*`): this reads them and puts them in words. It names no pack and no
|
|
9
|
+
// object: an item is whatever the host lists. Money is shown as the host gave it, in the
|
|
10
|
+
// currency's minor unit; nothing here adds up a figure.
|
|
11
|
+
//
|
|
12
|
+
// What is on screen only: money, the drill-in rows (they name members) and an approval's detail
|
|
13
|
+
// are read with `gcTime: 0`, and `forgetToday` drops them when the app goes to the background
|
|
14
|
+
// (ADR 0629's pattern).
|
|
15
|
+
import { ApiError, NetworkError } from "./errors.js";
|
|
16
|
+
import { packHooks } from "./hooks.js";
|
|
17
|
+
// --- Words -----------------------------------------------------------------------------------------
|
|
18
|
+
/** An item's value as it reads: `12`, `85%`, or money in the host's currency. */
|
|
19
|
+
export function todayValueWords(item, locale) {
|
|
20
|
+
if (item.format === "percent") {
|
|
21
|
+
return new Intl.NumberFormat(locale, { style: "percent", maximumFractionDigits: 0 }).format(item.value / 100);
|
|
22
|
+
}
|
|
23
|
+
if (item.format === "money" && item.currency)
|
|
24
|
+
return moneyWords(item.value, item.currency, locale);
|
|
25
|
+
return new Intl.NumberFormat(locale).format(item.value);
|
|
26
|
+
}
|
|
27
|
+
/** An amount in a currency's minor unit, in words: `$1,234.50`. */
|
|
28
|
+
function moneyWords(minor, currency, locale) {
|
|
29
|
+
const format = new Intl.NumberFormat(locale, { style: "currency", currency });
|
|
30
|
+
const digits = format.resolvedOptions().maximumFractionDigits ?? 2;
|
|
31
|
+
return format.format(minor / 10 ** digits);
|
|
32
|
+
}
|
|
33
|
+
/** The comparison under a value: `5 yesterday`, `$90.00 last week`, or null when there is none. */
|
|
34
|
+
export function todayCompareWords(item, locale) {
|
|
35
|
+
if (!item.compare)
|
|
36
|
+
return null;
|
|
37
|
+
const value = todayValueWords({ ...item, value: item.compare.value }, locale);
|
|
38
|
+
return `${value} ${item.compare.against === "yesterday" ? "yesterday" : "last week"}`;
|
|
39
|
+
}
|
|
40
|
+
/** The day at the top of the screen, in the business's zone: `Tuesday 24 November`. */
|
|
41
|
+
export function todayDayWords(answer, locale) {
|
|
42
|
+
// Noon UTC on the local date is the same date in every zone the host may name.
|
|
43
|
+
return new Intl.DateTimeFormat(locale, {
|
|
44
|
+
weekday: "long",
|
|
45
|
+
day: "numeric",
|
|
46
|
+
month: "long",
|
|
47
|
+
timeZone: "UTC",
|
|
48
|
+
}).format(new Date(`${answer.day}T12:00:00Z`));
|
|
49
|
+
}
|
|
50
|
+
/** What a setup change says on a phone: the host refuses it there (decision 8). */
|
|
51
|
+
export const REVIEW_ON_COMPUTER_WORDS = "Review on a computer";
|
|
52
|
+
/**
|
|
53
|
+
* Whether an action changes how the system is set up (an extension, new fields): listed on the
|
|
54
|
+
* phone with "Review on a computer", never offered Approve. The host refuses it from the app
|
|
55
|
+
* whatever the screen shows.
|
|
56
|
+
*/
|
|
57
|
+
export function needsComputer(action) {
|
|
58
|
+
return action.risk === "extension" || action.risk === "schema";
|
|
59
|
+
}
|
|
60
|
+
/** The actions still waiting in a change set, in the host's order. */
|
|
61
|
+
export function waitingActions(detail) {
|
|
62
|
+
return detail.actions.filter((a) => a.status === "pending");
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* The input that approves one action. `confirmed` is true once the person pressed the second
|
|
66
|
+
* step's button: its words are sent as the host gave them (ADR 0345); the host decides whether
|
|
67
|
+
* one was needed.
|
|
68
|
+
*/
|
|
69
|
+
export function approveInput(action, confirmed = false) {
|
|
70
|
+
return {
|
|
71
|
+
actionIds: [action.id],
|
|
72
|
+
...(confirmed && action.secondStep ? { confirm: action.secondStep.confirmLabel } : {}),
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
/** The input that rejects one action. */
|
|
76
|
+
export function rejectInput(action, note) {
|
|
77
|
+
return { actionIds: [action.id], ...(note?.trim() ? { note: note.trim() } : {}) };
|
|
78
|
+
}
|
|
79
|
+
/** A failed call in the screen's words, keeping the host's message where it has one. */
|
|
80
|
+
export function ownerFailure(error) {
|
|
81
|
+
if (error instanceof NetworkError) {
|
|
82
|
+
return { kind: "offline", message: "Could not reach the business. Check the connection." };
|
|
83
|
+
}
|
|
84
|
+
if (!(error instanceof ApiError)) {
|
|
85
|
+
return { kind: "other", message: "That did not work. Try again." };
|
|
86
|
+
}
|
|
87
|
+
const details = (error.details ?? {});
|
|
88
|
+
if (error.code === "second_step_required" && typeof details.confirmLabel === "string") {
|
|
89
|
+
return { kind: "secondStep", step: details, message: error.message };
|
|
90
|
+
}
|
|
91
|
+
if (details.reason === "review_on_computer") {
|
|
92
|
+
return { kind: "reviewOnComputer", message: REVIEW_ON_COMPUTER_WORDS };
|
|
93
|
+
}
|
|
94
|
+
if (error.status === 401)
|
|
95
|
+
return { kind: "signIn", message: "Sign in again." };
|
|
96
|
+
if (error.code === "forbidden")
|
|
97
|
+
return { kind: "notAllowed", message: error.message };
|
|
98
|
+
return { kind: "other", message: error.message };
|
|
99
|
+
}
|
|
100
|
+
// --- On screen only ---------------------------------------------------------------------------------
|
|
101
|
+
/** The reads that carry money, names or an approval's detail: never kept off screen. */
|
|
102
|
+
export const TODAY_PRIVATE_QUERIES = ["today.get", "today.rows", "approval.get"];
|
|
103
|
+
/**
|
|
104
|
+
* Drops Today's private reads from the app's query cache: call it when the app goes to the
|
|
105
|
+
* background (`AppState` on a phone) and on sign-out.
|
|
106
|
+
*/
|
|
107
|
+
export function forgetToday(queryClient) {
|
|
108
|
+
for (const name of TODAY_PRIVATE_QUERIES)
|
|
109
|
+
queryClient.removeQueries({ queryKey: [name] });
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* The operations each hook calls (ADR 0453). The owner's client holds them all once its release
|
|
113
|
+
* serves them; a release without Today's own operations yet offers the approvals and the inbox.
|
|
114
|
+
*/
|
|
115
|
+
export const OWNER_HOOKS_CALL = {
|
|
116
|
+
useToday: ["today.get"],
|
|
117
|
+
useTodayRows: ["today.rows"],
|
|
118
|
+
useAlerts: ["today.alerts"],
|
|
119
|
+
useSetAlerts: ["today.setAlerts"],
|
|
120
|
+
useWaiting: ["approval.list"],
|
|
121
|
+
useChangeSet: ["approval.get"],
|
|
122
|
+
useApprove: ["approval.approve"],
|
|
123
|
+
useReject: ["approval.reject"],
|
|
124
|
+
useNotices: ["notification.list"],
|
|
125
|
+
useUnreadCount: ["notification.unreadCount"],
|
|
126
|
+
useMarkRead: ["notification.markRead"],
|
|
127
|
+
useMe: ["user.me"],
|
|
128
|
+
};
|
|
129
|
+
/**
|
|
130
|
+
* Hooks for a React or React Native app:
|
|
131
|
+
* `const today = createOwnerHooks({ useQuery, useMutation }, api)`, with the owner's client (or
|
|
132
|
+
* the staff client, on the web). The hooks it cannot back are not there.
|
|
133
|
+
*/
|
|
134
|
+
export function createOwnerHooks(react, client) {
|
|
135
|
+
return packHooks(client, OWNER_HOOKS_CALL, (api) => allOwnerHooks(react, api));
|
|
136
|
+
}
|
|
137
|
+
function allOwnerHooks(react, api) {
|
|
138
|
+
const { useQuery, useMutation } = react;
|
|
139
|
+
return {
|
|
140
|
+
/** Today's items for the signed-in person: money among them, so on screen only. */
|
|
141
|
+
useToday() {
|
|
142
|
+
return useQuery({ ...api.query("today.get", {}), gcTime: 0 });
|
|
143
|
+
},
|
|
144
|
+
/** One item's rows (they name people): on screen only. `null` reads nothing. */
|
|
145
|
+
useTodayRows(key) {
|
|
146
|
+
return useQuery({
|
|
147
|
+
...api.query("today.rows", { key: key ?? "" }),
|
|
148
|
+
enabled: key !== null,
|
|
149
|
+
gcTime: 0,
|
|
150
|
+
});
|
|
151
|
+
},
|
|
152
|
+
/** The alerts this person receives and their overnight hours. */
|
|
153
|
+
useAlerts() {
|
|
154
|
+
return useQuery(api.query("today.alerts", {}));
|
|
155
|
+
},
|
|
156
|
+
/** Turn one of their own alerts on or off, or set their overnight hours. */
|
|
157
|
+
useSetAlerts() {
|
|
158
|
+
return useMutation(api.mutation("today.setAlerts"));
|
|
159
|
+
},
|
|
160
|
+
/** The change sets waiting for review that this person sees. */
|
|
161
|
+
useWaiting() {
|
|
162
|
+
return useQuery(api.query("approval.list", { status: "awaiting_review", limit: 50 }));
|
|
163
|
+
},
|
|
164
|
+
/** One change set with its actions, previews and second steps: on screen only. */
|
|
165
|
+
useChangeSet(changeSetId) {
|
|
166
|
+
return useQuery({
|
|
167
|
+
...api.query("approval.get", { changeSetId: changeSetId ?? "" }),
|
|
168
|
+
enabled: changeSetId !== null,
|
|
169
|
+
gcTime: 0,
|
|
170
|
+
});
|
|
171
|
+
},
|
|
172
|
+
/** Approve one action (`approveInput`); the host may answer with its second step. */
|
|
173
|
+
useApprove() {
|
|
174
|
+
return useMutation(api.mutation("approval.approve"));
|
|
175
|
+
},
|
|
176
|
+
/** Reject one action (`rejectInput`). */
|
|
177
|
+
useReject() {
|
|
178
|
+
return useMutation(api.mutation("approval.reject"));
|
|
179
|
+
},
|
|
180
|
+
/** The notice inbox, newest first. */
|
|
181
|
+
useNotices(unreadOnly = false) {
|
|
182
|
+
return useQuery(api.query("notification.list", { unreadOnly, limit: 30 }));
|
|
183
|
+
},
|
|
184
|
+
/** How many notices are unread. */
|
|
185
|
+
useUnreadCount() {
|
|
186
|
+
return useQuery(api.query("notification.unreadCount", {}));
|
|
187
|
+
},
|
|
188
|
+
/** Mark notices read: some by id, or all. */
|
|
189
|
+
useMarkRead() {
|
|
190
|
+
return useMutation(api.mutation("notification.markRead"));
|
|
191
|
+
},
|
|
192
|
+
/** Who is signed in. */
|
|
193
|
+
useMe() {
|
|
194
|
+
return useQuery(api.query("user.me", {}));
|
|
195
|
+
},
|
|
196
|
+
};
|
|
197
|
+
}
|
package/dist/session.d.ts
CHANGED
|
@@ -25,6 +25,15 @@ export interface TokenSessionOptions {
|
|
|
25
25
|
refreshEarlyMs?: number;
|
|
26
26
|
now?: () => number;
|
|
27
27
|
}
|
|
28
|
+
/** Which part of a sign-in by code failed: asking for the code, or signing in with it. */
|
|
29
|
+
export type SignInStep = "code" | "sign-in";
|
|
30
|
+
/**
|
|
31
|
+
* What to tell someone whose sign-in on a device failed, in plain words with what to do next
|
|
32
|
+
* (ADR 0455's refusals): sign-in not switched on (503), too many tries (429), no answer (offline),
|
|
33
|
+
* an address or code the host does not take. A screen shows it and keeps its button, so they can
|
|
34
|
+
* try again.
|
|
35
|
+
*/
|
|
36
|
+
export declare function signInErrorWords(error: unknown, step?: SignInStep): string;
|
|
28
37
|
export declare function tokenSession(options: TokenSessionOptions): {
|
|
29
38
|
/**
|
|
30
39
|
* Exchanges a sign-in for tokens. `credentials` are the headers that carry it, such as the
|
|
@@ -40,6 +49,10 @@ export declare function tokenSession(options: TokenSessionOptions): {
|
|
|
40
49
|
/**
|
|
41
50
|
* Signs in with the emailed code and keeps the device session the host answers with. A wrong,
|
|
42
51
|
* used or expired code throws `forbidden` (`invalid_code`). No cookie is involved.
|
|
52
|
+
*
|
|
53
|
+
* A customer signing in again in place (a sensitive act asked for a recent sign-in) names the
|
|
54
|
+
* device session it replaces by its refresh token (`replaces`): once the new sign-in has
|
|
55
|
+
* worked, the host ends that one, so it does not live on beside it. A wrong code ends nothing.
|
|
43
56
|
*/
|
|
44
57
|
signInWithCode(email: string, code: string, deviceName: string, audience?: SignInAudience): Promise<SignedIn>;
|
|
45
58
|
/**
|
package/dist/session.js
CHANGED
|
@@ -24,11 +24,50 @@ async function post(fetcher, url, body, headers = {}) {
|
|
|
24
24
|
catch (error) {
|
|
25
25
|
throw new NetworkError("Could not reach the host to sign in", error);
|
|
26
26
|
}
|
|
27
|
-
|
|
27
|
+
// Something in front of the host (a proxy, a gateway) can answer with a page, not JSON: the
|
|
28
|
+
// status still says what happened.
|
|
29
|
+
let parsed = {};
|
|
30
|
+
try {
|
|
31
|
+
parsed = JSON.parse(await response.text());
|
|
32
|
+
}
|
|
33
|
+
catch { }
|
|
28
34
|
if (response.status === 200 && parsed.data)
|
|
29
35
|
return parsed.data;
|
|
30
36
|
throw new ApiError(parsed.error?.code ?? "internal", parsed.error?.message ?? `The host answered ${response.status}`, response.status, parsed.error?.details);
|
|
31
37
|
}
|
|
38
|
+
/**
|
|
39
|
+
* What to tell someone whose sign-in on a device failed, in plain words with what to do next
|
|
40
|
+
* (ADR 0455's refusals): sign-in not switched on (503), too many tries (429), no answer (offline),
|
|
41
|
+
* an address or code the host does not take. A screen shows it and keeps its button, so they can
|
|
42
|
+
* try again.
|
|
43
|
+
*/
|
|
44
|
+
export function signInErrorWords(error, step = "code") {
|
|
45
|
+
const e = error !== null && typeof error === "object" ? error : null;
|
|
46
|
+
if (error instanceof NetworkError || e?.name === "NetworkError") {
|
|
47
|
+
return "We could not reach the business just now. Check your connection and try again.";
|
|
48
|
+
}
|
|
49
|
+
const reason = e?.details?.reason;
|
|
50
|
+
if (e?.status === 503 || reason === "sign_in_not_set_up") {
|
|
51
|
+
return "Signing in is not switched on here yet. Try again later, or ask the business.";
|
|
52
|
+
}
|
|
53
|
+
if (e?.status === 429 || e?.code === "rate_limited") {
|
|
54
|
+
const seconds = e?.details?.retryAfterSeconds;
|
|
55
|
+
const minutes = typeof seconds === "number" && seconds > 0 ? Math.ceil(seconds / 60) : null;
|
|
56
|
+
return minutes
|
|
57
|
+
? `Too many tries. Wait ${minutes} minute${minutes === 1 ? "" : "s"}, then try again.`
|
|
58
|
+
: "Too many tries. Wait a few minutes, then try again.";
|
|
59
|
+
}
|
|
60
|
+
if (reason === "invalid_code")
|
|
61
|
+
return "That code did not work. Check it, or ask for a new one.";
|
|
62
|
+
if (e?.status === 400 || e?.code === "invalid_input") {
|
|
63
|
+
return step === "code"
|
|
64
|
+
? "That does not look like an email address. Check it and try again."
|
|
65
|
+
: "Check the email address and the code, then try again.";
|
|
66
|
+
}
|
|
67
|
+
return step === "code"
|
|
68
|
+
? "We could not send a code just now. Try again in a moment."
|
|
69
|
+
: "We could not sign you in just now. Try again in a moment.";
|
|
70
|
+
}
|
|
32
71
|
export function tokenSession(options) {
|
|
33
72
|
const base = options.baseUrl.replace(/\/+$/, "");
|
|
34
73
|
const fetcher = options.fetch ?? globalThis.fetch?.bind(globalThis);
|
|
@@ -72,12 +111,18 @@ export function tokenSession(options) {
|
|
|
72
111
|
/**
|
|
73
112
|
* Signs in with the emailed code and keeps the device session the host answers with. A wrong,
|
|
74
113
|
* used or expired code throws `forbidden` (`invalid_code`). No cookie is involved.
|
|
114
|
+
*
|
|
115
|
+
* A customer signing in again in place (a sensitive act asked for a recent sign-in) names the
|
|
116
|
+
* device session it replaces by its refresh token (`replaces`): once the new sign-in has
|
|
117
|
+
* worked, the host ends that one, so it does not live on beside it. A wrong code ends nothing.
|
|
75
118
|
*/
|
|
76
119
|
async signInWithCode(email, code, deviceName, audience = "customer") {
|
|
120
|
+
const previous = audience === "customer" ? await options.store.load() : null;
|
|
77
121
|
const signedIn = await post(fetcher, `${base}/api/v1/auth/${audience}/sign-in`, {
|
|
78
122
|
email,
|
|
79
123
|
code,
|
|
80
124
|
deviceName,
|
|
125
|
+
...(previous?.refreshToken ? { replaces: previous.refreshToken } : {}),
|
|
81
126
|
});
|
|
82
127
|
const { account: _account, ...tokens } = signedIn;
|
|
83
128
|
await options.store.save(tokens);
|