@eloxiv/client 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +6 -2
- package/dist/Account.d.ts +131 -0
- package/dist/Account.d.ts.map +1 -0
- package/dist/Account.js +169 -0
- package/dist/Account.js.map +1 -0
- package/dist/Agent.d.ts +496 -0
- package/dist/Agent.d.ts.map +1 -0
- package/dist/Agent.js +228 -0
- package/dist/Agent.js.map +1 -0
- package/dist/AgentSync.d.ts +990 -0
- package/dist/AgentSync.d.ts.map +1 -0
- package/dist/AgentSync.js +46 -0
- package/dist/AgentSync.js.map +1 -0
- package/dist/AppState.d.ts +127 -0
- package/dist/AppState.d.ts.map +1 -0
- package/dist/AppState.js +145 -0
- package/dist/AppState.js.map +1 -0
- package/dist/AppStateSync.d.ts +193 -0
- package/dist/AppStateSync.d.ts.map +1 -0
- package/dist/AppStateSync.js +23 -0
- package/dist/AppStateSync.js.map +1 -0
- package/dist/Auth.d.ts +95 -0
- package/dist/Auth.d.ts.map +1 -0
- package/dist/Auth.js +139 -0
- package/dist/Auth.js.map +1 -0
- package/dist/Avatars.d.ts +53 -0
- package/dist/Avatars.d.ts.map +1 -0
- package/dist/Avatars.js +121 -0
- package/dist/Avatars.js.map +1 -0
- package/dist/BrowserAnalytics.d.ts +56 -0
- package/dist/BrowserAnalytics.d.ts.map +1 -0
- package/dist/BrowserAnalytics.js +94 -0
- package/dist/BrowserAnalytics.js.map +1 -0
- package/dist/Chat.d.ts +712 -0
- package/dist/Chat.d.ts.map +1 -0
- package/dist/Chat.js +883 -0
- package/dist/Chat.js.map +1 -0
- package/dist/ChatIntegrity.d.ts +63 -0
- package/dist/ChatIntegrity.d.ts.map +1 -0
- package/dist/ChatIntegrity.js +89 -0
- package/dist/ChatIntegrity.js.map +1 -0
- package/dist/ChatSends.d.ts +103 -0
- package/dist/ChatSends.d.ts.map +1 -0
- package/dist/ChatSends.js +157 -0
- package/dist/ChatSends.js.map +1 -0
- package/dist/Client.d.ts +113 -0
- package/dist/Client.d.ts.map +1 -0
- package/dist/Client.js +189 -0
- package/dist/Client.js.map +1 -0
- package/dist/Collab.d.ts +138 -0
- package/dist/Collab.d.ts.map +1 -0
- package/dist/Collab.js +454 -0
- package/dist/Collab.js.map +1 -0
- package/dist/CollabTools.d.ts +129 -0
- package/dist/CollabTools.d.ts.map +1 -0
- package/dist/CollabTools.js +277 -0
- package/dist/CollabTools.js.map +1 -0
- package/dist/Connections.d.ts +288 -0
- package/dist/Connections.d.ts.map +1 -0
- package/dist/Connections.js +316 -0
- package/dist/Connections.js.map +1 -0
- package/dist/Consent.d.ts +338 -0
- package/dist/Consent.d.ts.map +1 -0
- package/dist/Consent.js +142 -0
- package/dist/Consent.js.map +1 -0
- package/dist/Desktop.d.ts +61 -0
- package/dist/Desktop.d.ts.map +1 -0
- package/dist/Desktop.js +101 -0
- package/dist/Desktop.js.map +1 -0
- package/dist/Directory.d.ts +36 -0
- package/dist/Directory.d.ts.map +1 -0
- package/dist/Directory.js +42 -0
- package/dist/Directory.js.map +1 -0
- package/dist/ErrorReporter.d.ts +48 -0
- package/dist/ErrorReporter.d.ts.map +1 -0
- package/dist/ErrorReporter.js +70 -0
- package/dist/ErrorReporter.js.map +1 -0
- package/dist/ExpoSqlite.d.ts +7 -0
- package/dist/ExpoSqlite.d.ts.map +1 -0
- package/dist/ExpoSqlite.js +7 -0
- package/dist/ExpoSqlite.js.map +1 -0
- package/dist/FileSync.d.ts +265 -0
- package/dist/FileSync.d.ts.map +1 -0
- package/dist/FileSync.js +29 -0
- package/dist/FileSync.js.map +1 -0
- package/dist/FileTables.gen.d.ts +109 -0
- package/dist/FileTables.gen.d.ts.map +1 -0
- package/dist/FileTables.gen.js +23 -0
- package/dist/FileTables.gen.js.map +1 -0
- package/dist/Foldkit.d.ts +487 -0
- package/dist/Foldkit.d.ts.map +1 -0
- package/dist/Foldkit.js +617 -0
- package/dist/Foldkit.js.map +1 -0
- package/dist/History.d.ts +133 -0
- package/dist/History.d.ts.map +1 -0
- package/dist/History.js +210 -0
- package/dist/History.js.map +1 -0
- package/dist/InboxSync.d.ts +428 -0
- package/dist/InboxSync.d.ts.map +1 -0
- package/dist/InboxSync.js +37 -0
- package/dist/InboxSync.js.map +1 -0
- package/dist/KernelTables.gen.d.ts +1522 -0
- package/dist/KernelTables.gen.d.ts.map +1 -0
- package/dist/KernelTables.gen.js +307 -0
- package/dist/KernelTables.gen.js.map +1 -0
- package/dist/Make.d.ts +75 -0
- package/dist/Make.d.ts.map +1 -0
- package/dist/Make.js +70 -0
- package/dist/Make.js.map +1 -0
- package/dist/Models.d.ts +88 -0
- package/dist/Models.d.ts.map +1 -0
- package/dist/Models.js +80 -0
- package/dist/Models.js.map +1 -0
- package/dist/Optimistic.d.ts +120 -0
- package/dist/Optimistic.d.ts.map +1 -0
- package/dist/Optimistic.js +231 -0
- package/dist/Optimistic.js.map +1 -0
- package/dist/PlatformSync.d.ts +273 -0
- package/dist/PlatformSync.d.ts.map +1 -0
- package/dist/PlatformSync.js +92 -0
- package/dist/PlatformSync.js.map +1 -0
- package/dist/Poll.d.ts +113 -0
- package/dist/Poll.d.ts.map +1 -0
- package/dist/Poll.js +179 -0
- package/dist/Poll.js.map +1 -0
- package/dist/Preferences.d.ts +18 -0
- package/dist/Preferences.d.ts.map +1 -0
- package/dist/Preferences.js +22 -0
- package/dist/Preferences.js.map +1 -0
- package/dist/Proposals.d.ts +29 -0
- package/dist/Proposals.d.ts.map +1 -0
- package/dist/Proposals.js +41 -0
- package/dist/Proposals.js.map +1 -0
- package/dist/React.d.ts +17 -0
- package/dist/React.d.ts.map +1 -0
- package/dist/React.js +17 -0
- package/dist/React.js.map +1 -0
- package/dist/Reader.d.ts +35 -0
- package/dist/Reader.d.ts.map +1 -0
- package/dist/Reader.js +136 -0
- package/dist/Reader.js.map +1 -0
- package/dist/Resources.d.ts +98 -0
- package/dist/Resources.d.ts.map +1 -0
- package/dist/Resources.js +161 -0
- package/dist/Resources.js.map +1 -0
- package/dist/Rows.d.ts +4 -0
- package/dist/Rows.d.ts.map +1 -0
- package/dist/Rows.js +16 -0
- package/dist/Rows.js.map +1 -0
- package/dist/Runtime.d.ts +254 -0
- package/dist/Runtime.d.ts.map +1 -0
- package/dist/Runtime.js +1249 -0
- package/dist/Runtime.js.map +1 -0
- package/dist/RuntimeRuns.d.ts +74 -0
- package/dist/RuntimeRuns.d.ts.map +1 -0
- package/dist/RuntimeRuns.js +118 -0
- package/dist/RuntimeRuns.js.map +1 -0
- package/dist/ServicesSync.d.ts +286 -0
- package/dist/ServicesSync.d.ts.map +1 -0
- package/dist/ServicesSync.js +40 -0
- package/dist/ServicesSync.js.map +1 -0
- package/dist/Session.d.ts +226 -0
- package/dist/Session.d.ts.map +1 -0
- package/dist/Session.js +763 -0
- package/dist/Session.js.map +1 -0
- package/dist/ShareGrant.d.ts +47 -0
- package/dist/ShareGrant.d.ts.map +1 -0
- package/dist/ShareGrant.js +20 -0
- package/dist/ShareGrant.js.map +1 -0
- package/dist/Sharing.d.ts +203 -0
- package/dist/Sharing.d.ts.map +1 -0
- package/dist/Sharing.js +520 -0
- package/dist/Sharing.js.map +1 -0
- package/dist/Skew.d.ts +52 -0
- package/dist/Skew.d.ts.map +1 -0
- package/dist/Skew.js +77 -0
- package/dist/Skew.js.map +1 -0
- package/dist/SpacesSync.d.ts +247 -0
- package/dist/SpacesSync.d.ts.map +1 -0
- package/dist/SpacesSync.js +30 -0
- package/dist/SpacesSync.js.map +1 -0
- package/dist/Storage.d.ts +51 -0
- package/dist/Storage.d.ts.map +1 -0
- package/dist/Storage.js +107 -0
- package/dist/Storage.js.map +1 -0
- package/dist/Sync.d.ts +48 -0
- package/dist/Sync.d.ts.map +1 -0
- package/dist/Sync.js +48 -0
- package/dist/Sync.js.map +1 -0
- package/dist/SyncTables.d.ts +13 -0
- package/dist/SyncTables.d.ts.map +1 -0
- package/dist/SyncTables.js +13 -0
- package/dist/SyncTables.js.map +1 -0
- package/dist/Tab.d.ts +36 -0
- package/dist/Tab.d.ts.map +1 -0
- package/dist/Tab.js +44 -0
- package/dist/Tab.js.map +1 -0
- package/dist/Transport.d.ts +139 -0
- package/dist/Transport.d.ts.map +1 -0
- package/dist/Transport.js +232 -0
- package/dist/Transport.js.map +1 -0
- package/dist/Upload.d.ts +41 -0
- package/dist/Upload.d.ts.map +1 -0
- package/dist/Upload.js +93 -0
- package/dist/Upload.js.map +1 -0
- package/dist/WebMcp.d.ts +78 -0
- package/dist/WebMcp.d.ts.map +1 -0
- package/dist/WebMcp.js +204 -0
- package/dist/WebMcp.js.map +1 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +27 -0
- package/dist/index.js.map +1 -0
- package/package.json +97 -3
- package/src/Account.ts +249 -0
- package/src/Agent.ts +387 -0
- package/src/AgentSync.ts +59 -0
- package/src/AppState.ts +222 -0
- package/src/AppStateSync.ts +26 -0
- package/src/Auth.ts +225 -0
- package/src/Avatars.ts +144 -0
- package/src/BrowserAnalytics.ts +133 -0
- package/src/Chat.ts +1150 -0
- package/src/ChatIntegrity.ts +130 -0
- package/src/ChatSends.ts +209 -0
- package/src/Client.ts +319 -0
- package/src/Collab.ts +524 -0
- package/src/CollabTools.ts +381 -0
- package/src/Connections.ts +517 -0
- package/src/Consent.ts +176 -0
- package/src/Desktop.ts +139 -0
- package/src/Directory.ts +71 -0
- package/src/ErrorReporter.ts +112 -0
- package/src/ExpoSqlite.ts +6 -0
- package/src/FileSync.ts +34 -0
- package/src/FileTables.gen.ts +23 -0
- package/src/Foldkit.ts +960 -0
- package/src/History.ts +337 -0
- package/src/InboxSync.ts +47 -0
- package/src/KernelTables.gen.ts +329 -0
- package/src/Make.ts +120 -0
- package/src/Models.ts +134 -0
- package/src/Optimistic.ts +364 -0
- package/src/PlatformSync.ts +156 -0
- package/src/Poll.ts +227 -0
- package/src/Preferences.ts +42 -0
- package/src/Proposals.ts +57 -0
- package/src/React.ts +26 -0
- package/src/Reader.ts +180 -0
- package/src/Resources.ts +257 -0
- package/src/Rows.ts +16 -0
- package/src/Runtime.ts +1409 -0
- package/src/RuntimeRuns.ts +180 -0
- package/src/ServicesSync.ts +58 -0
- package/src/Session.ts +814 -0
- package/src/ShareGrant.ts +20 -0
- package/src/Sharing.ts +696 -0
- package/src/Skew.ts +97 -0
- package/src/SpacesSync.ts +61 -0
- package/src/Storage.ts +167 -0
- package/src/Sync.ts +125 -0
- package/src/SyncTables.ts +12 -0
- package/src/Tab.ts +59 -0
- package/src/Transport.ts +321 -0
- package/src/Upload.ts +125 -0
- package/src/WebMcp.ts +279 -0
- package/src/index.ts +26 -0
- package/src/web-globals.d.ts +103 -0
package/dist/Auth.d.ts
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/** Email and password sign-in against the kernel's Better Auth routes, returning a bearer token. */
|
|
2
|
+
export interface Session {
|
|
3
|
+
readonly token: string;
|
|
4
|
+
readonly user: {
|
|
5
|
+
readonly id: string;
|
|
6
|
+
readonly email: string;
|
|
7
|
+
readonly name: string;
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
export declare class AuthError extends Error {
|
|
11
|
+
readonly name = "AuthError";
|
|
12
|
+
readonly status: number;
|
|
13
|
+
constructor(message: string, status: number);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* A password sign-in of an account with two-factor authentication: no session yet. Finish it with
|
|
17
|
+
* `verifyTwoFactor({ challenge, code })` (Account.ts) within ten minutes; the challenge allows a few attempts.
|
|
18
|
+
*/
|
|
19
|
+
export declare class TwoFactorRequired extends AuthError {
|
|
20
|
+
readonly challenge: string;
|
|
21
|
+
/** The second factors the account can use: `totp` (and every account with it has backup codes). */
|
|
22
|
+
readonly methods: ReadonlyArray<string>;
|
|
23
|
+
constructor(challenge: string, methods: ReadonlyArray<string>);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Better Auth rejects state-changing requests without an Origin header. Browsers
|
|
27
|
+
* set it themselves (and ignore ours); other runtimes (React Native, Bun, Node)
|
|
28
|
+
* send the kernel's own origin, which Better Auth trusts.
|
|
29
|
+
*/
|
|
30
|
+
export declare const originOf: (baseUrl: string) => string;
|
|
31
|
+
/**
|
|
32
|
+
* Email and password to a session. An account with two-factor authentication rejects with `TwoFactorRequired`
|
|
33
|
+
* (holding the challenge `verifyTwoFactor` finishes it with); wrong credentials with `AuthError` (401).
|
|
34
|
+
*/
|
|
35
|
+
export declare const signIn: (baseUrl: string, credentials: {
|
|
36
|
+
readonly email: string;
|
|
37
|
+
readonly password: string;
|
|
38
|
+
}) => Promise<Session>;
|
|
39
|
+
export declare const signUp: (baseUrl: string, account: {
|
|
40
|
+
readonly email: string;
|
|
41
|
+
readonly password: string;
|
|
42
|
+
readonly name: string;
|
|
43
|
+
}) => Promise<Session>;
|
|
44
|
+
/** One organisation the signed-in user belongs to. */
|
|
45
|
+
export interface OrganizationMembership {
|
|
46
|
+
readonly orgId: string;
|
|
47
|
+
readonly name: string;
|
|
48
|
+
readonly roles: ReadonlyArray<string>;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* The organisations the signed-in user belongs to, oldest membership first (the first is where calls act when
|
|
52
|
+
* they name no organisation). Rejects with `AuthError` (status 401) without a valid session.
|
|
53
|
+
*/
|
|
54
|
+
export declare const listOrganizations: (baseUrl: string, token: string) => Promise<ReadonlyArray<OrganizationMembership>>;
|
|
55
|
+
/** A member of an organisation, with their roles and groups (Directory `listMembers`). */
|
|
56
|
+
export interface OrganizationMember {
|
|
57
|
+
readonly userId: string;
|
|
58
|
+
readonly email: string;
|
|
59
|
+
readonly roles: ReadonlyArray<string>;
|
|
60
|
+
readonly groups: ReadonlyArray<string>;
|
|
61
|
+
}
|
|
62
|
+
/** The members of `orgId` (the caller must be one of them), oldest membership first. */
|
|
63
|
+
export declare const listMembers: (baseUrl: string, token: string, orgId: string) => Promise<ReadonlyArray<OrganizationMember>>;
|
|
64
|
+
/** A role of an organisation, with the permissions it grants (Directory `listRoles`). */
|
|
65
|
+
export interface OrganizationRole {
|
|
66
|
+
readonly id: string;
|
|
67
|
+
readonly name: string;
|
|
68
|
+
readonly permissions: ReadonlyArray<string>;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Why a member administration call was refused: the kernel's typed error (`Unauthenticated`, `Forbidden`: no
|
|
72
|
+
* `eloxiv.directory.manage`, or not a person's own session; `InvalidInput`: no such account, role or member, or the
|
|
73
|
+
* change would leave the organisation with no one who can manage it), with its message.
|
|
74
|
+
*/
|
|
75
|
+
export declare class MemberAdminError extends Error {
|
|
76
|
+
readonly tag: string;
|
|
77
|
+
readonly status: number;
|
|
78
|
+
constructor(tag: string, message: string, status: number);
|
|
79
|
+
}
|
|
80
|
+
/** The roles of `orgId` (any member may list). */
|
|
81
|
+
export declare const listRoles: (baseUrl: string, token: string, orgId: string) => Promise<ReadonlyArray<OrganizationRole>>;
|
|
82
|
+
/** Add the existing user with this address to `orgId`, with `roles` (default `member`). Needs directory management. */
|
|
83
|
+
export declare const addMember: (baseUrl: string, token: string, orgId: string, input: {
|
|
84
|
+
readonly email: string;
|
|
85
|
+
readonly roles?: ReadonlyArray<string>;
|
|
86
|
+
}) => Promise<void>;
|
|
87
|
+
/** Remove a member from `orgId`; their next call is refused. Never the organisation's last administrator. */
|
|
88
|
+
export declare const removeMember: (baseUrl: string, token: string, orgId: string, userId: string) => Promise<void>;
|
|
89
|
+
/** Give a member a role. */
|
|
90
|
+
export declare const assignRole: (baseUrl: string, token: string, orgId: string, userId: string, roleId: string) => Promise<void>;
|
|
91
|
+
/** Take a role from a member. Never the last role that lets anyone manage the organisation. */
|
|
92
|
+
export declare const unassignRole: (baseUrl: string, token: string, orgId: string, userId: string, roleId: string) => Promise<void>;
|
|
93
|
+
/** Revoke the session on the server (Better Auth `sign-out`); the token stops authenticating everywhere. */
|
|
94
|
+
export declare const signOut: (baseUrl: string, token: string) => Promise<void>;
|
|
95
|
+
//#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CAAC,CAC6D,CAAA;AAEvG,uHAAuH;AACvH,eAAO,MAAM,SAAS,YACX,MAAM,SACR,MAAM,SACN,MAAM,SACN;IAAE,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,aAAa,CAAC,MAAM,CAAC,CAAA;CAAE,KACxE,OAAO,CAAC,IAAI,CAEd,CAAA;AAED,6GAA6G;AAC7G,eAAO,MAAM,YAAY,YAAmB,MAAM,SAAS,MAAM,SAAS,MAAM,UAAU,MAAM,KAAG,OAAO,CAAC,IAAI,CAE9G,CAAA;AAED,4BAA4B;AAC5B,eAAO,MAAM,UAAU,YACZ,MAAM,SACR,MAAM,SACN,MAAM,UACL,MAAM,UACN,MAAM,KACb,OAAO,CAAC,IAAI,CAEd,CAAA;AAED,+FAA+F;AAC/F,eAAO,MAAM,YAAY,YACd,MAAM,SACR,MAAM,SACN,MAAM,UACL,MAAM,UACN,MAAM,KACb,OAAO,CAAC,IAAI,CAEd,CAAA;AAED,4GAA4G;AAC5G,eAAO,MAAM,OAAO,YAAmB,MAAM,SAAS,MAAM,KAAG,OAAO,CAAC,IAAI,CAO1E,CAAA"}
|
package/dist/Auth.js
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
export class AuthError extends Error {
|
|
2
|
+
name = "AuthError";
|
|
3
|
+
status;
|
|
4
|
+
constructor(message, status) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.status = status;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* A password sign-in of an account with two-factor authentication: no session yet. Finish it with
|
|
11
|
+
* `verifyTwoFactor({ challenge, code })` (Account.ts) within ten minutes; the challenge allows a few attempts.
|
|
12
|
+
*/
|
|
13
|
+
export class TwoFactorRequired extends AuthError {
|
|
14
|
+
challenge;
|
|
15
|
+
/** The second factors the account can use: `totp` (and every account with it has backup codes). */
|
|
16
|
+
methods;
|
|
17
|
+
constructor(challenge, methods) {
|
|
18
|
+
super("This account needs a second factor: a code from an authenticator app or a backup code", 401);
|
|
19
|
+
this.challenge = challenge;
|
|
20
|
+
this.methods = methods;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Better Auth rejects state-changing requests without an Origin header. Browsers
|
|
25
|
+
* set it themselves (and ignore ours); other runtimes (React Native, Bun, Node)
|
|
26
|
+
* send the kernel's own origin, which Better Auth trusts.
|
|
27
|
+
*/
|
|
28
|
+
export const originOf = (baseUrl) => /^[a-z][a-z0-9+.-]*:\/\/[^/]+/i.exec(baseUrl)?.[0] ?? baseUrl;
|
|
29
|
+
const post = async (baseUrl, path, body) => {
|
|
30
|
+
const response = await fetch(`${baseUrl}/auth${path}`, {
|
|
31
|
+
method: "POST",
|
|
32
|
+
headers: { "content-type": "application/json", origin: originOf(baseUrl) },
|
|
33
|
+
body: JSON.stringify(body),
|
|
34
|
+
});
|
|
35
|
+
if (!response.ok)
|
|
36
|
+
throw new AuthError(`${path} failed: ${await response.text()}`, response.status);
|
|
37
|
+
const json = (await response.json());
|
|
38
|
+
// An account with two-factor authentication: no session until a second factor (Account.ts `verifyTwoFactor`).
|
|
39
|
+
if (json.twoFactorRedirect === true) {
|
|
40
|
+
throw new TwoFactorRequired(json.challenge ?? "", json.twoFactorMethods ?? []);
|
|
41
|
+
}
|
|
42
|
+
const token = response.headers.get("set-auth-token");
|
|
43
|
+
if (token === null)
|
|
44
|
+
throw new AuthError("The server did not return a bearer token", response.status);
|
|
45
|
+
return { token, user: json.user };
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Email and password to a session. An account with two-factor authentication rejects with `TwoFactorRequired`
|
|
49
|
+
* (holding the challenge `verifyTwoFactor` finishes it with); wrong credentials with `AuthError` (401).
|
|
50
|
+
*/
|
|
51
|
+
export const signIn = (baseUrl, credentials) => post(baseUrl, "/sign-in/email", credentials);
|
|
52
|
+
export const signUp = (baseUrl, account) => post(baseUrl, "/sign-up/email", account);
|
|
53
|
+
/**
|
|
54
|
+
* The organisations the signed-in user belongs to, oldest membership first (the first is where calls act when
|
|
55
|
+
* they name no organisation). Rejects with `AuthError` (status 401) without a valid session.
|
|
56
|
+
*/
|
|
57
|
+
export const listOrganizations = async (baseUrl, token) => {
|
|
58
|
+
const response = await fetch(`${baseUrl}/api/me/organizations`, { headers: { authorization: `Bearer ${token}` } });
|
|
59
|
+
if (!response.ok)
|
|
60
|
+
throw new AuthError(`/api/me/organizations failed: ${await response.text()}`, response.status);
|
|
61
|
+
return (await response.json());
|
|
62
|
+
};
|
|
63
|
+
/** The members of `orgId` (the caller must be one of them), oldest membership first. */
|
|
64
|
+
export const listMembers = async (baseUrl, token, orgId) => {
|
|
65
|
+
const response = await fetch(`${baseUrl}/api/org/members`, {
|
|
66
|
+
headers: { authorization: `Bearer ${token}`, "x-eloxiv-org": orgId },
|
|
67
|
+
});
|
|
68
|
+
if (!response.ok)
|
|
69
|
+
throw new AuthError(`/api/org/members failed: ${await response.text()}`, response.status);
|
|
70
|
+
return (await response.json());
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Why a member administration call was refused: the kernel's typed error (`Unauthenticated`, `Forbidden`: no
|
|
74
|
+
* `eloxiv.directory.manage`, or not a person's own session; `InvalidInput`: no such account, role or member, or the
|
|
75
|
+
* change would leave the organisation with no one who can manage it), with its message.
|
|
76
|
+
*/
|
|
77
|
+
export class MemberAdminError extends Error {
|
|
78
|
+
tag;
|
|
79
|
+
status;
|
|
80
|
+
constructor(tag, message, status) {
|
|
81
|
+
super(message);
|
|
82
|
+
this.tag = tag;
|
|
83
|
+
this.status = status;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
const memberCall = async (baseUrl, token, orgId, method, path, body) => {
|
|
87
|
+
const response = await fetch(`${baseUrl}${path}`, {
|
|
88
|
+
method,
|
|
89
|
+
headers: {
|
|
90
|
+
authorization: `Bearer ${token}`,
|
|
91
|
+
"x-eloxiv-org": orgId,
|
|
92
|
+
...(body === undefined ? {} : { "content-type": "application/json" }),
|
|
93
|
+
},
|
|
94
|
+
...(body === undefined ? {} : { body: JSON.stringify(body) }),
|
|
95
|
+
});
|
|
96
|
+
const text = await response.text();
|
|
97
|
+
let parsed;
|
|
98
|
+
try {
|
|
99
|
+
parsed = text.length === 0 ? {} : JSON.parse(text);
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
parsed = undefined;
|
|
103
|
+
}
|
|
104
|
+
if (!response.ok) {
|
|
105
|
+
const error = (parsed ?? {});
|
|
106
|
+
throw new MemberAdminError(typeof error._tag === "string" ? error._tag : "Unknown", typeof error.message === "string" ? error.message : `${path} failed (${response.status})`, response.status);
|
|
107
|
+
}
|
|
108
|
+
return parsed;
|
|
109
|
+
};
|
|
110
|
+
const member = (userId) => `/api/org/members/${encodeURIComponent(userId)}`;
|
|
111
|
+
/** The roles of `orgId` (any member may list). */
|
|
112
|
+
export const listRoles = async (baseUrl, token, orgId) => (await memberCall(baseUrl, token, orgId, "GET", "/api/org/roles"));
|
|
113
|
+
/** Add the existing user with this address to `orgId`, with `roles` (default `member`). Needs directory management. */
|
|
114
|
+
export const addMember = async (baseUrl, token, orgId, input) => {
|
|
115
|
+
await memberCall(baseUrl, token, orgId, "POST", "/api/org/members", input);
|
|
116
|
+
};
|
|
117
|
+
/** Remove a member from `orgId`; their next call is refused. Never the organisation's last administrator. */
|
|
118
|
+
export const removeMember = async (baseUrl, token, orgId, userId) => {
|
|
119
|
+
await memberCall(baseUrl, token, orgId, "DELETE", member(userId));
|
|
120
|
+
};
|
|
121
|
+
/** Give a member a role. */
|
|
122
|
+
export const assignRole = async (baseUrl, token, orgId, userId, roleId) => {
|
|
123
|
+
await memberCall(baseUrl, token, orgId, "POST", `${member(userId)}/roles`, { roleId });
|
|
124
|
+
};
|
|
125
|
+
/** Take a role from a member. Never the last role that lets anyone manage the organisation. */
|
|
126
|
+
export const unassignRole = async (baseUrl, token, orgId, userId, roleId) => {
|
|
127
|
+
await memberCall(baseUrl, token, orgId, "DELETE", `${member(userId)}/roles/${encodeURIComponent(roleId)}`);
|
|
128
|
+
};
|
|
129
|
+
/** Revoke the session on the server (Better Auth `sign-out`); the token stops authenticating everywhere. */
|
|
130
|
+
export const signOut = async (baseUrl, token) => {
|
|
131
|
+
const response = await fetch(`${baseUrl}/auth/sign-out`, {
|
|
132
|
+
method: "POST",
|
|
133
|
+
headers: { "content-type": "application/json", origin: originOf(baseUrl), authorization: `Bearer ${token}` },
|
|
134
|
+
body: "{}",
|
|
135
|
+
});
|
|
136
|
+
if (!response.ok)
|
|
137
|
+
throw new AuthError(`/sign-out failed: ${await response.text()}`, response.status);
|
|
138
|
+
};
|
|
139
|
+
//# sourceMappingURL=Auth.js.map
|
package/dist/Auth.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Auth.js","sourceRoot":"","sources":["../src/Auth.ts"],"names":[],"mappings":"AAMA,MAAM,OAAO,SAAU,SAAQ,KAAK;IAChB,IAAI,GAAG,WAAW,CAAA;IAC3B,MAAM,CAAQ;IACvB,YAAY,OAAe,EAAE,MAAc;QACzC,KAAK,CAAC,OAAO,CAAC,CAAA;QACd,IAAI,CAAC,MAAM,GAAG,MAAM,CAAA;IACtB,CAAC;CACF;AAED;;;GAGG;AACH,MAAM,OAAO,iBAAkB,SAAQ,SAAS;IACrC,SAAS,CAAQ;IAC1B,mGAAmG;IAC1F,OAAO,CAAuB;IACvC,YAAY,SAAiB,EAAE,OAA8B;QAC3D,KAAK,CAAC,uFAAuF,EAAE,GAAG,CAAC,CAAA;QACnG,IAAI,CAAC,SAAS,GAAG,SAAS,CAAA;QAC1B,IAAI,CAAC,OAAO,GAAG,OAAO,CAAA;IACxB,CAAC;CACF;AAED;;;;GAIG;AACH,MAAM,CAAC,MAAM,QAAQ,GAAG,CAAC,OAAe,EAAU,EAAE,CAAC,+BAA+B,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,OAAO,C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AAG,MAAM,CAAC,MAAM,CAAC,QAAQ,EAAE,EAAE,MAAM,EAAE,CAAC,CAAA;AACxF,CAAC,CAAA;AAED,+FAA+F;AAC/F,MAAM,CAAC,MAAM,YAAY,GAAG,KAAK,EAC/B,OAAe,EACf,KAAa,EACb,KAAa,EACb,MAAc,EACd,MAAc,EACC,EAAE;IACjB,MAAM,UAAU,CAAC,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,QAAQ,EAAE,GAAG,MAAM,CAAC,MAAM,CAAC,UAAU,kBAAkB,CAAC,MAAM,CAAC,EAAE,CAAC,CAAA;AAC5G,CAAC,CAAA;AAED,4GAA4G;AAC5G,MAAM,CAAC,MAAM,OAAO,GAAG,KAAK,EAAE,OAAe,EAAE,KAAa,EAAiB,EAAE;IAC7E,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,GAAG,OAAO,gBAAgB,EAAE;QACvD,MAAM,EAAE,MAAM;QACd,OAAO,EAAE,EAAE,cAAc,EAAE,kBAAkB,EAAE,MAAM,EAAE,QAAQ,CAAC,OAAO,CAAC,EAAE,aAAa,EAAE,UAAU,KAAK,EAAE,EAAE;QAC5G,IAAI,EAAE,IAAI;KACX,CAAC,CAAA;IACF,IAAI,CAAC,QAAQ,CAAC,EAAE;QAAE,MAAM,IAAI,SAAS,CAAC,qBAAqB,MAAM,QAAQ,CAAC,IAAI,EAAE,EAAE,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAA;AACtG,CAAC,CAAA"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Avatars, headless: people's profile photos as the kernel keeps them (`/api/me/avatar`, `/api/users/<id>/avatar`).
|
|
3
|
+
* One client per signed-in person, shared by every view: a photo is fetched once however many views ask at once
|
|
4
|
+
* (lookups in flight are shared), kept until it changes (revalidated by its ETag), and every view subscribed to a
|
|
5
|
+
* person hears of their new photo the moment this client uploads or removes it. A person sees only their own photo
|
|
6
|
+
* and those of people who share an organisation with them (the kernel answers no photo for anyone else).
|
|
7
|
+
*
|
|
8
|
+
* The SDK owns no form and no image codec (it runs where there is no canvas): `compress` is the app's (a browser's
|
|
9
|
+
* canvas or `createImageBitmap`, a native module), applied to every upload; the kernel takes PNG, JPEG, WebP or GIF of
|
|
10
|
+
* at most 256 KiB, and the SDK refuses anything else before sending it.
|
|
11
|
+
*/
|
|
12
|
+
/** An image's bytes and type. */
|
|
13
|
+
export interface AvatarImage {
|
|
14
|
+
readonly contentType: string;
|
|
15
|
+
readonly bytes: Uint8Array;
|
|
16
|
+
}
|
|
17
|
+
/** A person's photo, with its version (its ETag). */
|
|
18
|
+
export interface Avatar extends AvatarImage {
|
|
19
|
+
readonly etag: string;
|
|
20
|
+
}
|
|
21
|
+
/** The largest photo the kernel keeps. */
|
|
22
|
+
export declare const MAX_AVATAR_BYTES: number;
|
|
23
|
+
export declare const AVATAR_TYPES: ReadonlyArray<string>;
|
|
24
|
+
export declare class AvatarError extends Error {
|
|
25
|
+
readonly status: number;
|
|
26
|
+
constructor(message: string, status: number);
|
|
27
|
+
}
|
|
28
|
+
export interface AvatarClientOptions {
|
|
29
|
+
readonly baseUrl: string;
|
|
30
|
+
/** The signed-in person's session token, read for every request. */
|
|
31
|
+
readonly token: () => string;
|
|
32
|
+
/** The signed-in person's user id (whose photo `upload` and `remove` change). */
|
|
33
|
+
readonly userId: () => string;
|
|
34
|
+
/** Make a chosen photo fit (resize, re-encode); applied to every upload. Default: sent as chosen. */
|
|
35
|
+
readonly compress?: ((image: AvatarImage) => Promise<AvatarImage>) | undefined;
|
|
36
|
+
}
|
|
37
|
+
/** `undefined`: the person has no photo (or is not one this person may see). */
|
|
38
|
+
export type AvatarListener = (avatar: Avatar | undefined) => void;
|
|
39
|
+
export declare const avatarClient: (options: AvatarClientOptions) => {
|
|
40
|
+
get: (userId: string, lookup?: {
|
|
41
|
+
readonly refresh?: boolean;
|
|
42
|
+
}) => Promise<Avatar | undefined>;
|
|
43
|
+
/** Hear of a person's photo changing (an upload or removal through this client, or a refreshed lookup). */
|
|
44
|
+
subscribe: (userId: string, listener: AvatarListener) => (() => void);
|
|
45
|
+
/** Forget a cached photo, so the next `get` asks the kernel. */
|
|
46
|
+
invalidate: (userId: string) => undefined;
|
|
47
|
+
/** Set the signed-in person's photo (compressed first, when the app gives a compressor). */
|
|
48
|
+
upload: (chosen: AvatarImage) => Promise<Avatar>;
|
|
49
|
+
/** Remove the signed-in person's photo. */
|
|
50
|
+
remove: () => Promise<void>;
|
|
51
|
+
};
|
|
52
|
+
export type AvatarClient = ReturnType<typeof avatarClient>;
|
|
53
|
+
//# sourceMappingURL=Avatars.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Avatars.d.ts","sourceRoot":"","sources":["../src/Avatars.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,iCAAiC;AACjC,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAA;IAC5B,QAAQ,CAAC,KAAK,EAAE,UAAU,CAAA;CAC3B;AAED,qDAAqD;AACrD,MAAM,WAAW,MAAO,SAAQ,WAAW;IACzC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;CACtB;AAED,0CAA0C;AAC1C,eAAO,MAAM,gBAAgB,QAAa,CAAA;AAC1C,eAAO,MAAM,YAAY,EAAE,aAAa,CAAC,MAAM,CAA0D,CAAA;AAEzG,qBAAa,WAAY,SAAQ,KAAK;IACpC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;IACvB,YAAY,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAG1C;CACF;AAED,MAAM,WAAW,mBAAmB;IAClC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;IACxB,oEAAoE;IACpE,QAAQ,CAAC,KAAK,EAAE,MAAM,MAAM,CAAA;IAC5B,iFAAiF;IACjF,QAAQ,CAAC,MAAM,EAAE,MAAM,MAAM,CAAA;IAC7B,qGAAqG;IACrG,QAAQ,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,KAAK,EAAE,WAAW,KAAK,OAAO,CAAC,WAAW,CAAC,CAAC,GAAG,SAAS,CAAA;CAC/E;AAED,gFAAgF;AAChF,MAAM,MAAM,cAAc,GAAG,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,KAAK,IAAI,CAAA;AAEjE,eAAO,MAAM,YAAY,YAAa,mBAAmB;kBAmClC,MAAM,WAAU;QAAE,QAAQ,CAAC,OAAO,CAAC,EAAE,OAAO,CAAA;KAAE,KAAQ,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC;IAiBpG,2GAA2G;wBACvF,MAAM,YAAY,cAAc,KAAG,CAAC,MAAM,IAAI,CAAC;IAMnE,gEAAgE;yBAC3C,MAAM;IAC3B,4FAA4F;qBACrE,WAAW,KAAG,OAAO,CAAC,MAAM,CAAC;IAoBpD,2CAA2C;kBACzB,OAAO,CAAC,IAAI,CAAC;CAUlC,CAAA;AAED,MAAM,MAAM,YAAY,GAAG,UAAU,CAAC,OAAO,YAAY,CAAC,CAAA"}
|
package/dist/Avatars.js
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Avatars, headless: people's profile photos as the kernel keeps them (`/api/me/avatar`, `/api/users/<id>/avatar`).
|
|
3
|
+
* One client per signed-in person, shared by every view: a photo is fetched once however many views ask at once
|
|
4
|
+
* (lookups in flight are shared), kept until it changes (revalidated by its ETag), and every view subscribed to a
|
|
5
|
+
* person hears of their new photo the moment this client uploads or removes it. A person sees only their own photo
|
|
6
|
+
* and those of people who share an organisation with them (the kernel answers no photo for anyone else).
|
|
7
|
+
*
|
|
8
|
+
* The SDK owns no form and no image codec (it runs where there is no canvas): `compress` is the app's (a browser's
|
|
9
|
+
* canvas or `createImageBitmap`, a native module), applied to every upload; the kernel takes PNG, JPEG, WebP or GIF of
|
|
10
|
+
* at most 256 KiB, and the SDK refuses anything else before sending it.
|
|
11
|
+
*/
|
|
12
|
+
/** The largest photo the kernel keeps. */
|
|
13
|
+
export const MAX_AVATAR_BYTES = 256 * 1024;
|
|
14
|
+
export const AVATAR_TYPES = ["image/png", "image/jpeg", "image/webp", "image/gif"];
|
|
15
|
+
export class AvatarError extends Error {
|
|
16
|
+
status;
|
|
17
|
+
constructor(message, status) {
|
|
18
|
+
super(message);
|
|
19
|
+
this.status = status;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
export const avatarClient = (options) => {
|
|
23
|
+
const cache = new Map();
|
|
24
|
+
const inflight = new Map();
|
|
25
|
+
const listeners = new Map();
|
|
26
|
+
const base = options.baseUrl.replace(/\/+$/u, "");
|
|
27
|
+
const auth = () => ({ authorization: `Bearer ${options.token()}` });
|
|
28
|
+
const publish = (userId, avatar) => {
|
|
29
|
+
cache.set(userId, avatar);
|
|
30
|
+
for (const listener of listeners.get(userId) ?? []) {
|
|
31
|
+
try {
|
|
32
|
+
listener(avatar);
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
// A listener's failure is its own.
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
const fetchAvatar = async (userId) => {
|
|
40
|
+
const cached = cache.get(userId);
|
|
41
|
+
const response = await fetch(`${base}/api/users/${encodeURIComponent(userId)}/avatar`, {
|
|
42
|
+
headers: { ...auth(), ...(cached === undefined ? {} : { "if-none-match": `"${cached.etag}"` }) },
|
|
43
|
+
});
|
|
44
|
+
if (response.status === 304 && cached !== undefined)
|
|
45
|
+
return cached;
|
|
46
|
+
if (response.status === 404)
|
|
47
|
+
return undefined;
|
|
48
|
+
if (!response.ok)
|
|
49
|
+
throw new AvatarError(`The avatar could not be read (${response.status})`, response.status);
|
|
50
|
+
const etag = (response.headers.get("etag") ?? "").replaceAll('"', "");
|
|
51
|
+
return {
|
|
52
|
+
contentType: response.headers.get("content-type") ?? "application/octet-stream",
|
|
53
|
+
bytes: new Uint8Array(await response.arrayBuffer()),
|
|
54
|
+
etag,
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
/** A person's photo: the cached one, or one lookup shared by every caller asking at the same time. */
|
|
58
|
+
const get = (userId, lookup = {}) => {
|
|
59
|
+
if (lookup.refresh !== true && cache.has(userId))
|
|
60
|
+
return Promise.resolve(cache.get(userId));
|
|
61
|
+
const pending = inflight.get(userId);
|
|
62
|
+
if (pending !== undefined)
|
|
63
|
+
return pending;
|
|
64
|
+
const started = fetchAvatar(userId)
|
|
65
|
+
.then((avatar) => {
|
|
66
|
+
const before = cache.get(userId);
|
|
67
|
+
if (!cache.has(userId) || before?.etag !== avatar?.etag)
|
|
68
|
+
publish(userId, avatar);
|
|
69
|
+
return avatar;
|
|
70
|
+
})
|
|
71
|
+
.finally(() => inflight.delete(userId));
|
|
72
|
+
inflight.set(userId, started);
|
|
73
|
+
return started;
|
|
74
|
+
};
|
|
75
|
+
return {
|
|
76
|
+
get,
|
|
77
|
+
/** Hear of a person's photo changing (an upload or removal through this client, or a refreshed lookup). */
|
|
78
|
+
subscribe: (userId, listener) => {
|
|
79
|
+
const set = listeners.get(userId) ?? new Set();
|
|
80
|
+
set.add(listener);
|
|
81
|
+
listeners.set(userId, set);
|
|
82
|
+
return () => void set.delete(listener);
|
|
83
|
+
},
|
|
84
|
+
/** Forget a cached photo, so the next `get` asks the kernel. */
|
|
85
|
+
invalidate: (userId) => void cache.delete(userId),
|
|
86
|
+
/** Set the signed-in person's photo (compressed first, when the app gives a compressor). */
|
|
87
|
+
upload: async (chosen) => {
|
|
88
|
+
const image = options.compress === undefined ? chosen : await options.compress(chosen);
|
|
89
|
+
if (!AVATAR_TYPES.includes(image.contentType)) {
|
|
90
|
+
throw new AvatarError(`An avatar is PNG, JPEG, WebP or GIF, not ${image.contentType}`, 415);
|
|
91
|
+
}
|
|
92
|
+
if (image.bytes.length > MAX_AVATAR_BYTES) {
|
|
93
|
+
throw new AvatarError(`An avatar is at most ${MAX_AVATAR_BYTES} bytes (this one: ${image.bytes.length})`, 413);
|
|
94
|
+
}
|
|
95
|
+
const response = await fetch(`${base}/api/me/avatar`, {
|
|
96
|
+
method: "PUT",
|
|
97
|
+
headers: { ...auth(), "content-type": image.contentType },
|
|
98
|
+
// A copy on its own ArrayBuffer (what every runtime's fetch takes as a body).
|
|
99
|
+
body: new Uint8Array(image.bytes),
|
|
100
|
+
});
|
|
101
|
+
if (!response.ok)
|
|
102
|
+
throw new AvatarError(`The avatar was refused: ${await response.text()}`, response.status);
|
|
103
|
+
const { etag } = (await response.json());
|
|
104
|
+
const avatar = { contentType: image.contentType, bytes: image.bytes, etag };
|
|
105
|
+
publish(options.userId(), avatar);
|
|
106
|
+
return avatar;
|
|
107
|
+
},
|
|
108
|
+
/** Remove the signed-in person's photo. */
|
|
109
|
+
remove: async () => {
|
|
110
|
+
const response = await fetch(`${base}/api/me/avatar`, {
|
|
111
|
+
method: "DELETE",
|
|
112
|
+
headers: { ...auth(), "content-type": "application/json" },
|
|
113
|
+
body: "{}",
|
|
114
|
+
});
|
|
115
|
+
if (!response.ok)
|
|
116
|
+
throw new AvatarError(`The avatar could not be removed (${response.status})`, response.status);
|
|
117
|
+
publish(options.userId(), undefined);
|
|
118
|
+
},
|
|
119
|
+
};
|
|
120
|
+
};
|
|
121
|
+
//# sourceMappingURL=Avatars.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Avatars.js","sourceRoot":"","sources":["../src/Avatars.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAaH,0CAA0C;AAC1C,MAAM,CAAC,MAAM,gBAAgB,GAAG,GAAG,GAAG,IAAI,CAAA;AAC1C,MAAM,CAAC,MAAM,YAAY,GAA0B,CAAC,WAAW,EAAE,YAAY,EAAE,YAAY,EAAE,WAAW,CAAC,CAAA;AAEzG,MAAM,OAAO,WAAY,SAAQ,KAAK;IAC3B,MAAM,CAAQ;IACvB,YAAY,OAAe,EAAE,MAAc;QACzC,KAAK,CAAC,OAAO,CAAC,CAAA;QACd,IAAI,CAAC,MAAM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gBAAE,MAAM,IAAI,WAAW,CAAC,oCAAoC,QAAQ,CAAC,MAAM,GAAG,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAA;YAChH,OAAO,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,SAAS,CAAC,CAAA;QACtC,CAAC;KACF,CAAA;AACH,CAAC,CAAA"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An opt-in collector of a web client's product analytics (the Telemetry contract's `telemetry.track`): pageviews by
|
|
3
|
+
* route name, lifecycle (the page shown, hidden, left) and how long the client's action calls took, as the app's
|
|
4
|
+
* declared `client.*` events. **Nothing is collected without analytics consent** (the consent controller's, read at
|
|
5
|
+
* each event; the kernel checks the person's consent again when the batch leaves), routes are the router's names for its
|
|
6
|
+
* registered routes (`journal.entry`; `unknown` otherwise, never a path), and the person is the kernel's keyed hash, never their id.
|
|
7
|
+
*
|
|
8
|
+
* It uses no DOM API itself: the app passes the page (`window`, for its `visibilitychange` and `pagehide` events and
|
|
9
|
+
* `document.visibilityState`), so the root SDK stays free of DOM types and runs under Hermes.
|
|
10
|
+
*/
|
|
11
|
+
import type { AnalyticsEventDeclaration, ConsentCategory, TrackEvent } from "@eloxiv/contracts";
|
|
12
|
+
export declare const PAGEVIEW_EVENT = "client.pageview";
|
|
13
|
+
export declare const LIFECYCLE_EVENT = "client.lifecycle";
|
|
14
|
+
export declare const ACTION_TIMED_EVENT = "client.action_timed";
|
|
15
|
+
/** The route a pageview names when the router knows none. */
|
|
16
|
+
export declare const UNKNOWN_PAGE = "unknown";
|
|
17
|
+
/**
|
|
18
|
+
* The collector's events, for the app to declare (`KernelApp.analytics.events`): its routes and action names become
|
|
19
|
+
* the enums the kernel holds every event to, so nothing else can be sent.
|
|
20
|
+
*/
|
|
21
|
+
export declare const browserAnalyticsEvents: (declared: {
|
|
22
|
+
readonly routes: ReadonlyArray<string>;
|
|
23
|
+
readonly actions: ReadonlyArray<string>;
|
|
24
|
+
}) => Readonly<Record<string, AnalyticsEventDeclaration>>;
|
|
25
|
+
/** The page's events and visibility (a browser's `window`, its `document.visibilityState`). */
|
|
26
|
+
export interface PageSource {
|
|
27
|
+
readonly addEventListener: (type: "visibilitychange" | "pagehide", listener: () => void) => void;
|
|
28
|
+
readonly removeEventListener: (type: "visibilitychange" | "pagehide", listener: () => void) => void;
|
|
29
|
+
readonly visibility: () => "visible" | "hidden";
|
|
30
|
+
}
|
|
31
|
+
export interface BrowserAnalyticsOptions {
|
|
32
|
+
/** Send an event (`client.call("telemetry.track", event)` of the typed client, say). */
|
|
33
|
+
readonly track: (event: TrackEvent) => Promise<unknown>;
|
|
34
|
+
/** Whose consent decides (the consent controller): nothing is sent unless `analytics` is granted. */
|
|
35
|
+
readonly consent: {
|
|
36
|
+
readonly granted: (category: ConsentCategory) => boolean;
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* The route names (lower-case tokens such as `journal.entry`, as analytics enums are) and action names the app
|
|
40
|
+
* declared (`browserAnalyticsEvents`): others are `unknown` or not timed.
|
|
41
|
+
*/
|
|
42
|
+
readonly routes: ReadonlyArray<string>;
|
|
43
|
+
readonly actions: ReadonlyArray<string>;
|
|
44
|
+
}
|
|
45
|
+
export declare const browserAnalytics: (options: BrowserAnalyticsOptions) => {
|
|
46
|
+
/** A page shown, by its route's name from the router (`unknown` for one the app did not declare). */
|
|
47
|
+
pageview: (route: string | undefined) => Promise<void>;
|
|
48
|
+
/** Run an action call and send how long it took (only for actions the app declared). */
|
|
49
|
+
timed: <A>(action: string, call: () => Promise<A>) => Promise<A>;
|
|
50
|
+
/** Send the page's lifecycle (shown, hidden, left); returns the uninstall. */
|
|
51
|
+
install: (page: PageSource) => () => void;
|
|
52
|
+
/** Wait for events being sent. */
|
|
53
|
+
settled: () => Promise<void>;
|
|
54
|
+
};
|
|
55
|
+
export type BrowserAnalytics = ReturnType<typeof browserAnalytics>;
|
|
56
|
+
//# sourceMappingURL=BrowserAnalytics.d.ts.map
|
|
@@ -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KAAE,CAAA;IAC9E;;;OAGG;IACH,QAAQ,CAAC,MAAM,EAAE,aAAa,CAAC,MAAM,CAAC,CAAA;IACtC,QAAQ,CAAC,OAAO,EAAE,aAAa,CAAC,MAAM,CAAC,CAAA;CACxC;AAED,eAAO,MAAM,gBAAgB,YAAa,uBAAuB;IAyB7D,qGAAqG;IACrG,QAAQ,UAAU,MAAM,GAAG,SAAS;IAKpC,wFAAwF;IACxF,KAAK,GAAS,CAAC,UAAU,MAAM,QAAQ,MAAM,OAAO,CAAC,CAAC,CAAC,KAAG,OAAO,CAAC,CAAC,CAAC;IAgBpE,8EAA8E;IAC9E,OAAO,SAAS,UAAU;IAU1B,kCAAkC;IAClC,OAAO;CAEV,CAAA;AAED,MAAM,MAAM,gBAAgB,GAAG,UAAU,CAAC,OAAO,gBAAgB,CAAC,CAAA"}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
export const PAGEVIEW_EVENT = "client.pageview";
|
|
2
|
+
export const LIFECYCLE_EVENT = "client.lifecycle";
|
|
3
|
+
export const ACTION_TIMED_EVENT = "client.action_timed";
|
|
4
|
+
/** The route a pageview names when the router knows none. */
|
|
5
|
+
export const UNKNOWN_PAGE = "unknown";
|
|
6
|
+
/**
|
|
7
|
+
* The collector's events, for the app to declare (`KernelApp.analytics.events`): its routes and action names become
|
|
8
|
+
* the enums the kernel holds every event to, so nothing else can be sent.
|
|
9
|
+
*/
|
|
10
|
+
export const browserAnalyticsEvents = (declared) => ({
|
|
11
|
+
[PAGEVIEW_EVENT]: {
|
|
12
|
+
description: "A page of the client was shown, by its route's name",
|
|
13
|
+
category: "analytics",
|
|
14
|
+
properties: { route: { kind: "enum", values: [...declared.routes, UNKNOWN_PAGE] } },
|
|
15
|
+
},
|
|
16
|
+
[LIFECYCLE_EVENT]: {
|
|
17
|
+
description: "The client's page was shown, hidden or left",
|
|
18
|
+
category: "analytics",
|
|
19
|
+
properties: { state: { kind: "enum", values: ["visible", "hidden", "left"] } },
|
|
20
|
+
},
|
|
21
|
+
[ACTION_TIMED_EVENT]: {
|
|
22
|
+
description: "How long one of the client's action calls took",
|
|
23
|
+
category: "analytics",
|
|
24
|
+
properties: {
|
|
25
|
+
action: { kind: "enum", values: [...declared.actions] },
|
|
26
|
+
outcome: { kind: "enum", values: ["ok", "failed"] },
|
|
27
|
+
duration_ms: { kind: "number" },
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
export const browserAnalytics = (options) => {
|
|
32
|
+
const routes = new Set(options.routes);
|
|
33
|
+
const actions = new Set(options.actions);
|
|
34
|
+
let sending = Promise.resolve();
|
|
35
|
+
/** One event, only with analytics consent now; a failure to send is dropped, never thrown into the app. */
|
|
36
|
+
const send = (event) => {
|
|
37
|
+
if (!options.consent.granted("analytics"))
|
|
38
|
+
return sending;
|
|
39
|
+
const previous = sending;
|
|
40
|
+
sending = (async () => {
|
|
41
|
+
await previous;
|
|
42
|
+
// Again just before it leaves: consent withdrawn while earlier events were sending stops this one.
|
|
43
|
+
if (!options.consent.granted("analytics"))
|
|
44
|
+
return;
|
|
45
|
+
try {
|
|
46
|
+
await options.track(event);
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
// Dropped: analytics never breaks the app.
|
|
50
|
+
}
|
|
51
|
+
})();
|
|
52
|
+
return sending;
|
|
53
|
+
};
|
|
54
|
+
const lifecycle = (state) => send({ event: LIFECYCLE_EVENT, properties: { state } });
|
|
55
|
+
return {
|
|
56
|
+
/** A page shown, by its route's name from the router (`unknown` for one the app did not declare). */
|
|
57
|
+
pageview: (route) => send({
|
|
58
|
+
event: PAGEVIEW_EVENT,
|
|
59
|
+
properties: { route: route !== undefined && routes.has(route) ? route : UNKNOWN_PAGE },
|
|
60
|
+
}),
|
|
61
|
+
/** Run an action call and send how long it took (only for actions the app declared). */
|
|
62
|
+
timed: async (action, call) => {
|
|
63
|
+
const started = Date.now();
|
|
64
|
+
let outcome = "failed";
|
|
65
|
+
try {
|
|
66
|
+
const result = await call();
|
|
67
|
+
outcome = "ok";
|
|
68
|
+
return result;
|
|
69
|
+
}
|
|
70
|
+
finally {
|
|
71
|
+
if (actions.has(action)) {
|
|
72
|
+
void send({
|
|
73
|
+
event: ACTION_TIMED_EVENT,
|
|
74
|
+
properties: { action, outcome, duration_ms: Date.now() - started },
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
/** Send the page's lifecycle (shown, hidden, left); returns the uninstall. */
|
|
80
|
+
install: (page) => {
|
|
81
|
+
const onVisibility = () => void lifecycle(page.visibility());
|
|
82
|
+
const onLeave = () => void lifecycle("left");
|
|
83
|
+
page.addEventListener("visibilitychange", onVisibility);
|
|
84
|
+
page.addEventListener("pagehide", onLeave);
|
|
85
|
+
return () => {
|
|
86
|
+
page.removeEventListener("visibilitychange", onVisibility);
|
|
87
|
+
page.removeEventListener("pagehide", onLeave);
|
|
88
|
+
};
|
|
89
|
+
},
|
|
90
|
+
/** Wait for events being sent. */
|
|
91
|
+
settled: () => sending,
|
|
92
|
+
};
|
|
93
|
+
};
|
|
94
|
+
//# sourceMappingURL=BrowserAnalytics.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BrowserAnalytics.js","sourceRoot":"","sources":["../src/BrowserAnalytics.ts"],"names":[],"mappings":"AAYA,MAAM,CAAC,MAAM,cAAc,GAAG,iBAAiB,CAAA;AAC/C,MAAM,CAAC,MAAM,eAAe,GAAG,kBAAkB,CAAA;AACjD,MAAM,CAAC,MAAM,kBAAkB,GAAG,qBAAqB,CAAA;AAEvD,6DAA6D;AAC7D,MAAM,CAAC,MAAM,YAAY,GAAG,SAAS,CAAA;AAErC;;;GAGG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAG,CAAC,QAGtC,EAAuD,EAAE,CAAC,CAAC;IAC1D,CAAC,cAAc,CAAC,EAAE;QAChB,WAAW,EAAE,qDAAqD;QAClE,QAAQ,EAAE,WAAW;QACrB,UAAU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