@proveanything/smartlinks 2.0.55 → 2.0.57
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/api/integrations.d.ts +21 -1
- package/dist/api/integrations.js +25 -0
- package/dist/api/realtime.d.ts +31 -88
- package/dist/api/realtime.js +239 -93
- package/dist/docs/API_SUMMARY.md +264 -4
- package/dist/docs/app-manifest.md +7 -0
- package/dist/docs/connectors.md +204 -0
- package/dist/docs/integrations.md +4 -0
- package/dist/docs/overview.md +3 -2
- package/dist/docs/realtime.md +118 -765
- package/dist/http.js +3 -0
- package/dist/openapi.yaml +435 -3
- package/dist/types/appManifest.d.ts +166 -0
- package/dist/types/integrations.d.ts +43 -1
- package/dist/types/realtime.d.ts +44 -0
- package/docs/API_SUMMARY.md +264 -4
- package/docs/app-manifest.md +7 -0
- package/docs/connectors.md +204 -0
- package/docs/integrations.md +4 -0
- package/docs/overview.md +3 -2
- package/docs/realtime.md +118 -765
- package/openapi.yaml +435 -3
- package/package.json +1 -1
- package/scripts/doctor.mjs +25 -0
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { IntegrationFlow, CreateFlowInput, UpdateFlowInput, ListFlowsQuery, FlowList, RunFlowInput, RunFlowResult, RunFlowSummary, RunFlowEnqueued, RecordTypesResponse, FlowRun, ListRunsQuery, RunList, ListRunItemsQuery, RunItemList, LoggingWindow, SetLoggingInput } from "../types/integrations.js";
|
|
1
|
+
import type { IntegrationFlow, CreateFlowInput, UpdateFlowInput, ListFlowsQuery, FlowList, RunFlowInput, RunFlowResult, RunFlowSummary, RunFlowEnqueued, RecordTypesResponse, FlowRun, ListRunsQuery, RunList, ListRunItemsQuery, RunItemList, LoggingWindow, SetLoggingInput, InstalledConnector, SaveConnectorInstallationInput, ConnectorConsoleStep } from "../types/integrations.js";
|
|
2
2
|
export declare namespace integrations {
|
|
3
3
|
/** List flows in a collection. GET /integrations/flows */
|
|
4
4
|
function listFlows(collectionId: string, query?: ListFlowsQuery): Promise<FlowList>;
|
|
@@ -57,4 +57,24 @@ export declare namespace integrations {
|
|
|
57
57
|
function cancelLogging(collectionId: string, connectionId: string): Promise<{
|
|
58
58
|
canceled: boolean;
|
|
59
59
|
}>;
|
|
60
|
+
/** The collection's installed connector apps. GET /integrations/connectors */
|
|
61
|
+
function listConnectors(collectionId: string): Promise<{
|
|
62
|
+
connectors: InstalledConnector[];
|
|
63
|
+
}>;
|
|
64
|
+
/** Save a connector's non-secret Connect-form values and/or settings. PUT /integrations/connectors/:appId/installation */
|
|
65
|
+
function saveConnectorInstallation(collectionId: string, appId: string, input: SaveConnectorInstallationInput): Promise<{
|
|
66
|
+
installation: {
|
|
67
|
+
connection: Record<string, unknown>;
|
|
68
|
+
settings: Record<string, unknown>;
|
|
69
|
+
};
|
|
70
|
+
}>;
|
|
71
|
+
/**
|
|
72
|
+
* Run a connector step from the console: `testConnection` → ConnectorTestResult, `describeTarget` →
|
|
73
|
+
* ConnectorTargetSchema, `sample` → ConnectorRecords, `transform` (a dry run) → ConnectorTransformResult.
|
|
74
|
+
* The server adds the installation; pass `flowId` to layer that flow's settings.
|
|
75
|
+
* POST /integrations/connectors/:appId/steps/:step
|
|
76
|
+
*/
|
|
77
|
+
function runConnectorStep<T = unknown>(collectionId: string, appId: string, step: ConnectorConsoleStep, input?: Record<string, unknown> & {
|
|
78
|
+
flowId?: string;
|
|
79
|
+
}): Promise<T>;
|
|
60
80
|
}
|
package/dist/api/integrations.js
CHANGED
|
@@ -135,4 +135,29 @@ export var integrations;
|
|
|
135
135
|
return del(`/admin/collection/${enc(collectionId)}/integrations/logging/${enc(connectionId)}`);
|
|
136
136
|
}
|
|
137
137
|
integrations.cancelLogging = cancelLogging;
|
|
138
|
+
// --- Connectors (docs/connectors.md) ------------------------------------
|
|
139
|
+
// Connector apps installed on the collection: their declared contract, the installation's
|
|
140
|
+
// non-secret settings, and which secrets are set. Secrets themselves go through
|
|
141
|
+
// `secrets.put(collectionId, name, value, { appId })` for the connector's appId.
|
|
142
|
+
const connectorsBase = (collectionId) => `/admin/collection/${enc(collectionId)}/integrations/connectors`;
|
|
143
|
+
/** The collection's installed connector apps. GET /integrations/connectors */
|
|
144
|
+
async function listConnectors(collectionId) {
|
|
145
|
+
return request(connectorsBase(collectionId));
|
|
146
|
+
}
|
|
147
|
+
integrations.listConnectors = listConnectors;
|
|
148
|
+
/** Save a connector's non-secret Connect-form values and/or settings. PUT /integrations/connectors/:appId/installation */
|
|
149
|
+
async function saveConnectorInstallation(collectionId, appId, input) {
|
|
150
|
+
return put(`${connectorsBase(collectionId)}/${enc(appId)}/installation`, input);
|
|
151
|
+
}
|
|
152
|
+
integrations.saveConnectorInstallation = saveConnectorInstallation;
|
|
153
|
+
/**
|
|
154
|
+
* Run a connector step from the console: `testConnection` → ConnectorTestResult, `describeTarget` →
|
|
155
|
+
* ConnectorTargetSchema, `sample` → ConnectorRecords, `transform` (a dry run) → ConnectorTransformResult.
|
|
156
|
+
* The server adds the installation; pass `flowId` to layer that flow's settings.
|
|
157
|
+
* POST /integrations/connectors/:appId/steps/:step
|
|
158
|
+
*/
|
|
159
|
+
async function runConnectorStep(collectionId, appId, step, input = {}) {
|
|
160
|
+
return post(`${connectorsBase(collectionId)}/${enc(appId)}/steps/${enc(step)}`, input);
|
|
161
|
+
}
|
|
162
|
+
integrations.runConnectorStep = runConnectorStep;
|
|
138
163
|
})(integrations || (integrations = {}));
|
package/dist/api/realtime.d.ts
CHANGED
|
@@ -1,103 +1,46 @@
|
|
|
1
|
-
import type { AblyTokenRequest, RealtimeTokenRequest } from "../types/realtime.js";
|
|
1
|
+
import type { AblyTokenRequest, RealtimeTokenRequest, RealtimeChangeEvent, RealtimeChangeFilter, RealtimeMessage, RealtimeScope } from "../types/realtime.js";
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* One of your app's declared channels (manifest `realtime.channels`).
|
|
4
4
|
*
|
|
5
|
-
* Provides access to real-time communication channels using Ably.
|
|
6
|
-
* Supports both public (user-scoped) and admin tokens for different permission levels.
|
|
7
|
-
*
|
|
8
|
-
* Channel Patterns:
|
|
9
|
-
* - With appId: collection:{collectionId}:app:{appId}:*
|
|
10
|
-
* - Without appId: collection:{collectionId}:*
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
5
|
* ```ts
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
* //
|
|
17
|
-
* const tokenRequest = await realtime.getPublicToken({
|
|
18
|
-
* collectionId: 'abc123',
|
|
19
|
-
* appId: 'xyz789'
|
|
20
|
-
* })
|
|
21
|
-
*
|
|
22
|
-
* // Initialize Ably client
|
|
23
|
-
* const ably = new Ably.Realtime.Promise({
|
|
24
|
-
* authCallback: async (data, callback) => {
|
|
25
|
-
* callback(null, tokenRequest)
|
|
26
|
-
* }
|
|
27
|
-
* })
|
|
28
|
-
*
|
|
29
|
-
* // Subscribe to a channel
|
|
30
|
-
* const channel = ably.channels.get('collection:abc123:app:xyz789:chat:room-1')
|
|
31
|
-
* await channel.subscribe((message) => {
|
|
32
|
-
* console.log('Received:', message.data)
|
|
33
|
-
* })
|
|
34
|
-
*
|
|
35
|
-
* // Publish a message
|
|
36
|
-
* await channel.publish('message', { text: 'Hello!', userId: '123' })
|
|
6
|
+
* const draw = realtime.channel({ collectionId, appId }, "draw")
|
|
7
|
+
* const off = draw.subscribe("status", (m) => setStatus(m.data))
|
|
8
|
+
* await draw.publish("status", { phase: "spinning" }) // if the channel lets this viewer publish
|
|
37
9
|
* ```
|
|
38
10
|
*/
|
|
11
|
+
export declare function channel(scope: RealtimeScope & {
|
|
12
|
+
appId: string;
|
|
13
|
+
}, name: string): {
|
|
14
|
+
/** Listen for an event (or "*" for every event). Returns a function that stops listening. */
|
|
15
|
+
subscribe(event: string, onMessage: (msg: RealtimeMessage) => void): () => void;
|
|
16
|
+
/** Send a message. Refused (403) unless the channel's `publish` rule allows this viewer. */
|
|
17
|
+
publish(event: string, data?: unknown): Promise<{
|
|
18
|
+
ok: true;
|
|
19
|
+
}>;
|
|
20
|
+
};
|
|
39
21
|
/**
|
|
40
|
-
*
|
|
22
|
+
* Something changed: refetch it. IDs only — never data.
|
|
41
23
|
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
24
|
+
* Admins of the collection see every change; everyone else sees changes to public things (public app
|
|
25
|
+
* records, products). Filter by `type` ('product' | 'proof' | 'asset' | 'record' | 'collection' |
|
|
26
|
+
* 'collection.apps' | 'settings'), `id`, `appId`, `recordType`, `productId`, `proofId`.
|
|
44
27
|
*
|
|
45
|
-
* Requires user authentication (Bearer token).
|
|
46
|
-
*
|
|
47
|
-
* @param params - Token request parameters
|
|
48
|
-
* @param params.collectionId - The collection scope (required)
|
|
49
|
-
* @param params.appId - Optional app scope for more specific permissions
|
|
50
|
-
* @returns Ably TokenRequest that can be used with Ably.Realtime client
|
|
51
|
-
*
|
|
52
|
-
* @example
|
|
53
28
|
* ```ts
|
|
54
|
-
* const
|
|
55
|
-
* collectionId: 'my-collection-id',
|
|
56
|
-
* appId: 'my-app-id'
|
|
57
|
-
* })
|
|
58
|
-
*
|
|
59
|
-
* // Use with Ably
|
|
60
|
-
* const ably = new Ably.Realtime.Promise({
|
|
61
|
-
* authCallback: async (data, callback) => {
|
|
62
|
-
* callback(null, tokenRequest)
|
|
63
|
-
* }
|
|
64
|
-
* })
|
|
65
|
-
*
|
|
66
|
-
* // Subscribe to channels matching the pattern
|
|
67
|
-
* const channel = ably.channels.get('collection:my-collection-id:app:my-app-id:chat:general')
|
|
68
|
-
* await channel.subscribe('message', (msg) => console.log(msg.data))
|
|
29
|
+
* const off = realtime.onChange({ collectionId }, { type: "record", appId, recordType: "entry" }, () => refetch())
|
|
69
30
|
* ```
|
|
70
31
|
*/
|
|
32
|
+
export declare function onChange(scope: RealtimeScope, filter: RealtimeChangeFilter, onChangeFn: (change: RealtimeChangeEvent) => void): () => void;
|
|
33
|
+
/**
|
|
34
|
+
* Messages for the signed-in person only (e.g. "scan" — a tag they just scanned on another device).
|
|
35
|
+
*/
|
|
36
|
+
export declare function onMine(scope: RealtimeScope, onMessage: (msg: RealtimeMessage) => void): () => void;
|
|
37
|
+
/**
|
|
38
|
+
* @deprecated Use `realtime.channel()` / `realtime.onChange()`. Returns a raw Ably TokenRequest for
|
|
39
|
+
* `collection:{collectionId}:*` (subscribe; publish only for the collection's admins).
|
|
40
|
+
*/
|
|
71
41
|
export declare function getPublicToken(params: RealtimeTokenRequest): Promise<AblyTokenRequest>;
|
|
72
42
|
/**
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
* This endpoint returns an Ably TokenRequest that can be used to initialize an Ably client
|
|
76
|
-
* with admin permissions to receive system notifications and alerts.
|
|
77
|
-
*
|
|
78
|
-
* Admin users get subscribe-only (read-only) access to the interaction:{userId} channel pattern.
|
|
79
|
-
*
|
|
80
|
-
* Requires admin authentication (Bearer token).
|
|
81
|
-
*
|
|
82
|
-
* @returns Ably TokenRequest that can be used with Ably.Realtime client
|
|
83
|
-
*
|
|
84
|
-
* @example
|
|
85
|
-
* ```ts
|
|
86
|
-
* const tokenRequest = await realtime.getAdminToken()
|
|
87
|
-
*
|
|
88
|
-
* // Use with Ably
|
|
89
|
-
* const ably = new Ably.Realtime.Promise({
|
|
90
|
-
* authCallback: async (data, callback) => {
|
|
91
|
-
* callback(null, tokenRequest)
|
|
92
|
-
* }
|
|
93
|
-
* })
|
|
94
|
-
*
|
|
95
|
-
* // Subscribe to admin interaction channel
|
|
96
|
-
* const userId = 'my-user-id'
|
|
97
|
-
* const channel = ably.channels.get(`interaction:${userId}`)
|
|
98
|
-
* await channel.subscribe((message) => {
|
|
99
|
-
* console.log('Admin notification:', message.data)
|
|
100
|
-
* })
|
|
101
|
-
* ```
|
|
43
|
+
* @deprecated Use `realtime.onMine()`. Returns a raw Ably TokenRequest for `interaction:{userId}`
|
|
44
|
+
* (subscribe only).
|
|
102
45
|
*/
|
|
103
46
|
export declare function getAdminToken(): Promise<AblyTokenRequest>;
|
package/dist/api/realtime.js
CHANGED
|
@@ -1,113 +1,259 @@
|
|
|
1
1
|
// src/api/realtime.ts
|
|
2
|
-
import { request } from "../http.js";
|
|
2
|
+
import { request, post } from "../http.js";
|
|
3
|
+
/** One live stream per (collection, app): every listener on this page shares it. */
|
|
4
|
+
class Stream {
|
|
5
|
+
constructor(collectionId, appId, onEmpty) {
|
|
6
|
+
this.collectionId = collectionId;
|
|
7
|
+
this.appId = appId;
|
|
8
|
+
this.onEmpty = onEmpty;
|
|
9
|
+
this.listeners = new Map(); // logical name ("draw", "feed:admin") → listeners
|
|
10
|
+
this.physical = new Map(); // physical channel → logical name
|
|
11
|
+
this.granted = new Set();
|
|
12
|
+
this.source = null;
|
|
13
|
+
this.lastEvent = null;
|
|
14
|
+
this.refreshTimer = null;
|
|
15
|
+
this.retryTimer = null;
|
|
16
|
+
this.pending = null;
|
|
17
|
+
this.retries = 0;
|
|
18
|
+
this.closed = false;
|
|
19
|
+
}
|
|
20
|
+
add(name, listener) {
|
|
21
|
+
let set = this.listeners.get(name);
|
|
22
|
+
const isNew = !set;
|
|
23
|
+
if (!set)
|
|
24
|
+
this.listeners.set(name, (set = new Set()));
|
|
25
|
+
set.add(listener);
|
|
26
|
+
if (isNew || !this.source)
|
|
27
|
+
this.schedule();
|
|
28
|
+
return () => {
|
|
29
|
+
const s = this.listeners.get(name);
|
|
30
|
+
if (!s)
|
|
31
|
+
return;
|
|
32
|
+
s.delete(listener);
|
|
33
|
+
if (!s.size)
|
|
34
|
+
this.listeners.delete(name);
|
|
35
|
+
if (!this.listeners.size)
|
|
36
|
+
this.close();
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
/** What was granted (empty until connected). */
|
|
40
|
+
has(name) {
|
|
41
|
+
return this.granted.has(name);
|
|
42
|
+
}
|
|
43
|
+
schedule(delay = 30) {
|
|
44
|
+
if (this.pending)
|
|
45
|
+
clearTimeout(this.pending);
|
|
46
|
+
this.pending = setTimeout(() => {
|
|
47
|
+
this.pending = null;
|
|
48
|
+
void this.open();
|
|
49
|
+
}, delay);
|
|
50
|
+
}
|
|
51
|
+
async open() {
|
|
52
|
+
var _a;
|
|
53
|
+
if (this.closed || typeof EventSource === "undefined")
|
|
54
|
+
return;
|
|
55
|
+
const names = [...this.listeners.keys()];
|
|
56
|
+
const feeds = names.filter((n) => n.startsWith("feed:")).map((n) => n.slice(5));
|
|
57
|
+
const channels = names.filter((n) => !n.startsWith("feed:"));
|
|
58
|
+
let reply;
|
|
59
|
+
try {
|
|
60
|
+
reply = await post("/public/realtime/connect", Object.assign(Object.assign({ collectionId: this.collectionId }, (this.appId ? { appId: this.appId } : {})), { feeds,
|
|
61
|
+
channels }));
|
|
62
|
+
}
|
|
63
|
+
catch (err) {
|
|
64
|
+
return this.retryLater(err);
|
|
65
|
+
}
|
|
66
|
+
if (this.closed)
|
|
67
|
+
return;
|
|
68
|
+
(_a = this.source) === null || _a === void 0 ? void 0 : _a.close();
|
|
69
|
+
this.source = null;
|
|
70
|
+
this.physical.clear();
|
|
71
|
+
this.granted = new Set(reply.channels.map((c) => c.name));
|
|
72
|
+
for (const c of reply.channels)
|
|
73
|
+
this.physical.set(c.channel, c.name);
|
|
74
|
+
if (reply.transport !== "sse" || !reply.url)
|
|
75
|
+
return; // nothing this viewer may see
|
|
76
|
+
const url = this.lastEvent ? `${reply.url}&lastEvent=${encodeURIComponent(this.lastEvent)}` : reply.url;
|
|
77
|
+
const source = new EventSource(url);
|
|
78
|
+
this.source = source;
|
|
79
|
+
source.addEventListener("message", (e) => this.deliver(e));
|
|
80
|
+
source.addEventListener("error", (e) => {
|
|
81
|
+
const data = e.data;
|
|
82
|
+
// A stream error with a body (e.g. the token expired): get a fresh token and resume.
|
|
83
|
+
if (data || source.readyState === EventSource.CLOSED)
|
|
84
|
+
this.retryLater(data);
|
|
85
|
+
});
|
|
86
|
+
source.addEventListener("open", () => {
|
|
87
|
+
this.retries = 0;
|
|
88
|
+
});
|
|
89
|
+
// Renew a minute before the token expires (the stream can't renew its own token).
|
|
90
|
+
if (this.refreshTimer)
|
|
91
|
+
clearTimeout(this.refreshTimer);
|
|
92
|
+
if (reply.expiresAt) {
|
|
93
|
+
const ms = Math.max(10000, reply.expiresAt - Date.now() - 60000);
|
|
94
|
+
this.refreshTimer = setTimeout(() => this.schedule(0), ms);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
retryLater(_why) {
|
|
98
|
+
var _a;
|
|
99
|
+
if (this.closed)
|
|
100
|
+
return;
|
|
101
|
+
(_a = this.source) === null || _a === void 0 ? void 0 : _a.close();
|
|
102
|
+
this.source = null;
|
|
103
|
+
if (this.retryTimer)
|
|
104
|
+
clearTimeout(this.retryTimer);
|
|
105
|
+
const ms = Math.min(30000, 1000 * 2 ** this.retries++);
|
|
106
|
+
this.retryTimer = setTimeout(() => this.schedule(0), ms);
|
|
107
|
+
}
|
|
108
|
+
deliver(e) {
|
|
109
|
+
var _a, _b;
|
|
110
|
+
if (e.lastEventId)
|
|
111
|
+
this.lastEvent = e.lastEventId;
|
|
112
|
+
let env;
|
|
113
|
+
try {
|
|
114
|
+
env = JSON.parse(String(e.data));
|
|
115
|
+
}
|
|
116
|
+
catch (_c) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
const name = env.channel ? this.physical.get(env.channel) : undefined;
|
|
120
|
+
if (!name)
|
|
121
|
+
return;
|
|
122
|
+
let data = env.data;
|
|
123
|
+
if (typeof data === "string" && env.encoding && /json/.test(env.encoding)) {
|
|
124
|
+
try {
|
|
125
|
+
data = JSON.parse(data);
|
|
126
|
+
}
|
|
127
|
+
catch (_d) {
|
|
128
|
+
/* leave as the string */
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
const msg = { channel: name, event: (_a = env.name) !== null && _a !== void 0 ? _a : "", data, at: env.timestamp };
|
|
132
|
+
for (const l of [...((_b = this.listeners.get(name)) !== null && _b !== void 0 ? _b : [])]) {
|
|
133
|
+
try {
|
|
134
|
+
l(msg);
|
|
135
|
+
}
|
|
136
|
+
catch (err) {
|
|
137
|
+
console.error("[SmartLinks realtime] a listener threw", err);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
close() {
|
|
142
|
+
var _a;
|
|
143
|
+
this.closed = true;
|
|
144
|
+
for (const t of [this.pending, this.refreshTimer, this.retryTimer])
|
|
145
|
+
if (t)
|
|
146
|
+
clearTimeout(t);
|
|
147
|
+
(_a = this.source) === null || _a === void 0 ? void 0 : _a.close();
|
|
148
|
+
this.source = null;
|
|
149
|
+
this.onEmpty();
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
const streams = new Map();
|
|
153
|
+
function streamFor(scope) {
|
|
154
|
+
var _a, _b;
|
|
155
|
+
if (!(scope === null || scope === void 0 ? void 0 : scope.collectionId))
|
|
156
|
+
throw new Error("realtime: collectionId is required");
|
|
157
|
+
const key = `${scope.collectionId}|${(_a = scope.appId) !== null && _a !== void 0 ? _a : ""}`;
|
|
158
|
+
let s = streams.get(key);
|
|
159
|
+
if (!s) {
|
|
160
|
+
s = new Stream(scope.collectionId, (_b = scope.appId) !== null && _b !== void 0 ? _b : null, () => streams.delete(key));
|
|
161
|
+
streams.set(key, s);
|
|
162
|
+
}
|
|
163
|
+
return s;
|
|
164
|
+
}
|
|
3
165
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* Provides access to real-time communication channels using Ably.
|
|
7
|
-
* Supports both public (user-scoped) and admin tokens for different permission levels.
|
|
8
|
-
*
|
|
9
|
-
* Channel Patterns:
|
|
10
|
-
* - With appId: collection:{collectionId}:app:{appId}:*
|
|
11
|
-
* - Without appId: collection:{collectionId}:*
|
|
166
|
+
* One of your app's declared channels (manifest `realtime.channels`).
|
|
12
167
|
*
|
|
13
|
-
* @example
|
|
14
168
|
* ```ts
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
* //
|
|
18
|
-
* const tokenRequest = await realtime.getPublicToken({
|
|
19
|
-
* collectionId: 'abc123',
|
|
20
|
-
* appId: 'xyz789'
|
|
21
|
-
* })
|
|
22
|
-
*
|
|
23
|
-
* // Initialize Ably client
|
|
24
|
-
* const ably = new Ably.Realtime.Promise({
|
|
25
|
-
* authCallback: async (data, callback) => {
|
|
26
|
-
* callback(null, tokenRequest)
|
|
27
|
-
* }
|
|
28
|
-
* })
|
|
29
|
-
*
|
|
30
|
-
* // Subscribe to a channel
|
|
31
|
-
* const channel = ably.channels.get('collection:abc123:app:xyz789:chat:room-1')
|
|
32
|
-
* await channel.subscribe((message) => {
|
|
33
|
-
* console.log('Received:', message.data)
|
|
34
|
-
* })
|
|
35
|
-
*
|
|
36
|
-
* // Publish a message
|
|
37
|
-
* await channel.publish('message', { text: 'Hello!', userId: '123' })
|
|
169
|
+
* const draw = realtime.channel({ collectionId, appId }, "draw")
|
|
170
|
+
* const off = draw.subscribe("status", (m) => setStatus(m.data))
|
|
171
|
+
* await draw.publish("status", { phase: "spinning" }) // if the channel lets this viewer publish
|
|
38
172
|
* ```
|
|
39
173
|
*/
|
|
174
|
+
export function channel(scope, name) {
|
|
175
|
+
return {
|
|
176
|
+
/** Listen for an event (or "*" for every event). Returns a function that stops listening. */
|
|
177
|
+
subscribe(event, onMessage) {
|
|
178
|
+
return streamFor(scope).add(name, (m) => {
|
|
179
|
+
if (event === "*" || m.event === event)
|
|
180
|
+
onMessage(m);
|
|
181
|
+
});
|
|
182
|
+
},
|
|
183
|
+
/** Send a message. Refused (403) unless the channel's `publish` rule allows this viewer. */
|
|
184
|
+
publish(event, data) {
|
|
185
|
+
return post("/public/realtime/publish", {
|
|
186
|
+
collectionId: scope.collectionId,
|
|
187
|
+
appId: scope.appId,
|
|
188
|
+
channel: name,
|
|
189
|
+
event,
|
|
190
|
+
data: data === undefined ? null : data,
|
|
191
|
+
});
|
|
192
|
+
},
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
function matches(change, filter) {
|
|
196
|
+
for (const k of ["type", "id", "appId", "recordType", "productId", "proofId"]) {
|
|
197
|
+
if (filter[k] !== undefined && change[k] !== filter[k])
|
|
198
|
+
return false;
|
|
199
|
+
}
|
|
200
|
+
return true;
|
|
201
|
+
}
|
|
40
202
|
/**
|
|
41
|
-
*
|
|
203
|
+
* Something changed: refetch it. IDs only — never data.
|
|
42
204
|
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
205
|
+
* Admins of the collection see every change; everyone else sees changes to public things (public app
|
|
206
|
+
* records, products). Filter by `type` ('product' | 'proof' | 'asset' | 'record' | 'collection' |
|
|
207
|
+
* 'collection.apps' | 'settings'), `id`, `appId`, `recordType`, `productId`, `proofId`.
|
|
45
208
|
*
|
|
46
|
-
* Requires user authentication (Bearer token).
|
|
47
|
-
*
|
|
48
|
-
* @param params - Token request parameters
|
|
49
|
-
* @param params.collectionId - The collection scope (required)
|
|
50
|
-
* @param params.appId - Optional app scope for more specific permissions
|
|
51
|
-
* @returns Ably TokenRequest that can be used with Ably.Realtime client
|
|
52
|
-
*
|
|
53
|
-
* @example
|
|
54
209
|
* ```ts
|
|
55
|
-
* const
|
|
56
|
-
* collectionId: 'my-collection-id',
|
|
57
|
-
* appId: 'my-app-id'
|
|
58
|
-
* })
|
|
59
|
-
*
|
|
60
|
-
* // Use with Ably
|
|
61
|
-
* const ably = new Ably.Realtime.Promise({
|
|
62
|
-
* authCallback: async (data, callback) => {
|
|
63
|
-
* callback(null, tokenRequest)
|
|
64
|
-
* }
|
|
65
|
-
* })
|
|
66
|
-
*
|
|
67
|
-
* // Subscribe to channels matching the pattern
|
|
68
|
-
* const channel = ably.channels.get('collection:my-collection-id:app:my-app-id:chat:general')
|
|
69
|
-
* await channel.subscribe('message', (msg) => console.log(msg.data))
|
|
210
|
+
* const off = realtime.onChange({ collectionId }, { type: "record", appId, recordType: "entry" }, () => refetch())
|
|
70
211
|
* ```
|
|
71
212
|
*/
|
|
213
|
+
export function onChange(scope, filter, onChangeFn) {
|
|
214
|
+
const s = streamFor({ collectionId: scope.collectionId });
|
|
215
|
+
// Ask for both feeds; an admin is granted both (public changes reach the admin feed too), so the
|
|
216
|
+
// public feed is ignored once the admin feed is granted — no change arrives twice.
|
|
217
|
+
const handle = (fromAdminFeed) => (m) => {
|
|
218
|
+
if (m.event !== "change" || !m.data || typeof m.data !== "object")
|
|
219
|
+
return;
|
|
220
|
+
if (!fromAdminFeed && s.has("feed:admin"))
|
|
221
|
+
return;
|
|
222
|
+
const change = m.data;
|
|
223
|
+
if (matches(change, filter))
|
|
224
|
+
onChangeFn(change);
|
|
225
|
+
};
|
|
226
|
+
const offAdmin = s.add("feed:admin", handle(true));
|
|
227
|
+
const offPublic = s.add("feed:public", handle(false));
|
|
228
|
+
return () => {
|
|
229
|
+
offAdmin();
|
|
230
|
+
offPublic();
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Messages for the signed-in person only (e.g. "scan" — a tag they just scanned on another device).
|
|
235
|
+
*/
|
|
236
|
+
export function onMine(scope, onMessage) {
|
|
237
|
+
return streamFor({ collectionId: scope.collectionId }).add("feed:me", onMessage);
|
|
238
|
+
}
|
|
239
|
+
// ---------------------------------------------------------------------------------------------------
|
|
240
|
+
// Legacy (raw Ably tokens). New code uses channel / onChange above — no provider in your app.
|
|
241
|
+
// ---------------------------------------------------------------------------------------------------
|
|
242
|
+
/**
|
|
243
|
+
* @deprecated Use `realtime.channel()` / `realtime.onChange()`. Returns a raw Ably TokenRequest for
|
|
244
|
+
* `collection:{collectionId}:*` (subscribe; publish only for the collection's admins).
|
|
245
|
+
*/
|
|
72
246
|
export async function getPublicToken(params) {
|
|
73
247
|
const queryParams = new URLSearchParams();
|
|
74
|
-
queryParams.append(
|
|
75
|
-
if (params.appId)
|
|
76
|
-
queryParams.append(
|
|
77
|
-
}
|
|
248
|
+
queryParams.append("collectionId", params.collectionId);
|
|
249
|
+
if (params.appId)
|
|
250
|
+
queryParams.append("appId", params.appId);
|
|
78
251
|
return request(`/public/push/token?${queryParams.toString()}`);
|
|
79
252
|
}
|
|
80
253
|
/**
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
* This endpoint returns an Ably TokenRequest that can be used to initialize an Ably client
|
|
84
|
-
* with admin permissions to receive system notifications and alerts.
|
|
85
|
-
*
|
|
86
|
-
* Admin users get subscribe-only (read-only) access to the interaction:{userId} channel pattern.
|
|
87
|
-
*
|
|
88
|
-
* Requires admin authentication (Bearer token).
|
|
89
|
-
*
|
|
90
|
-
* @returns Ably TokenRequest that can be used with Ably.Realtime client
|
|
91
|
-
*
|
|
92
|
-
* @example
|
|
93
|
-
* ```ts
|
|
94
|
-
* const tokenRequest = await realtime.getAdminToken()
|
|
95
|
-
*
|
|
96
|
-
* // Use with Ably
|
|
97
|
-
* const ably = new Ably.Realtime.Promise({
|
|
98
|
-
* authCallback: async (data, callback) => {
|
|
99
|
-
* callback(null, tokenRequest)
|
|
100
|
-
* }
|
|
101
|
-
* })
|
|
102
|
-
*
|
|
103
|
-
* // Subscribe to admin interaction channel
|
|
104
|
-
* const userId = 'my-user-id'
|
|
105
|
-
* const channel = ably.channels.get(`interaction:${userId}`)
|
|
106
|
-
* await channel.subscribe((message) => {
|
|
107
|
-
* console.log('Admin notification:', message.data)
|
|
108
|
-
* })
|
|
109
|
-
* ```
|
|
254
|
+
* @deprecated Use `realtime.onMine()`. Returns a raw Ably TokenRequest for `interaction:{userId}`
|
|
255
|
+
* (subscribe only).
|
|
110
256
|
*/
|
|
111
257
|
export async function getAdminToken() {
|
|
112
|
-
return request(
|
|
258
|
+
return request("/admin/auth/push");
|
|
113
259
|
}
|