viasocket-apps 0.3.0 → 0.4.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/README.md CHANGED
@@ -37,6 +37,18 @@ goes in your server's `.env`. The skill is
37
37
  viaSocket and mirrored here; the same text filled with your ids is served at
38
38
  `https://flow.viasocket.com/documentation/skill.md?org=<org_id>&project=<project_id>`.
39
39
 
40
+ Before you ship — with or without an agent — check the code against the live catalog:
41
+
42
+ ```sh
43
+ curl -fsSL https://flow.viasocket.com/viasocket-check.mjs -o viasocket-check.mjs
44
+ node viasocket-check.mjs src
45
+ ```
46
+
47
+ It finds every viaSocket id and URL in your code and says whether each names a real app, action or
48
+ trigger, is used where its call expects that kind of id (the two ids of an action look alike), and
49
+ whether any viasocket URL was built by hand. No dependencies; exits 1 on a failure, so it can be a
50
+ test. It reads no `.env` file and prints no line of code.
51
+
40
52
  ## Server
41
53
 
42
54
  ```js
@@ -94,8 +106,17 @@ const { authId } = await connect({ embedToken, serviceId: 'rowbu58rc', actions:
94
106
  // Send authId to your backend, which enables the app and stores the script_id.
95
107
  ```
96
108
 
97
- `connect()` rejects with a `ViaSocketConnectError` whose `code` is `'closed'` (the user gave up),
98
- `'rejected'` (the app said no) or `'script'` (the connect script did not load).
109
+ `serviceId` is the app's `service_id` exactly as the catalog returns it — a bare id such as
110
+ `rowbu58rc`, never the app's name or slug, never the id with a name joined on. `connect()` loads
111
+ viaSocket's connect script from `https://flow.viasocket.com/connect.js` and lets it open the popup;
112
+ that script is the only supported way to open it, so never build a viasocket URL for it by hand.
113
+
114
+ `connect()` rejects with a `ViaSocketConnectError` whose `code` is `'closed'` (the user seems to
115
+ have given up), `'rejected'` (the app or viaSocket said no), `'invalid'` (`serviceId` is not a
116
+ service id) or `'script'` (the connect script did not load). `'closed'` is best-effort: once the
117
+ popup is on the app's own sign-in page a close can only be inferred, and arrives about a minute
118
+ later; if the user then finishes anyway, `onLateSuccess` is called with the same result a resolved
119
+ `connect()` gives. Treat `'closed'` as "nothing yet", not as a verdict.
99
120
 
100
121
  ## Methods
101
122
 
@@ -169,7 +190,9 @@ carrying the API's own `message`, the HTTP `status`, and the parsed `body`.
169
190
 
170
191
  Defaults target production: `https://flow-api.viasocket.com` for the API and
171
192
  `https://flow.sokt.io` for running actions. Pass `apiBaseUrl` / `runBaseUrl` to point elsewhere,
172
- and `scriptUrl` to `connect()` for a non-production connect script.
193
+ and `scriptUrl` to `connect()` for a non-production connect script — the script picks its own API
194
+ and auth hosts from the origin it is served from, so that is that stack's copy
195
+ (`https://dev-flow.viasocket.com/connect.js`).
173
196
 
174
197
  ## License
175
198
 
@@ -8,13 +8,23 @@
8
8
  * It wraps viaSocket's standalone connect script — the popup, the postMessage handshake, the
9
9
  * three message types — behind one promise. Nothing here touches the signing secret: the
10
10
  * embedToken comes from your backend, already signed for this end user.
11
+ *
12
+ * The script is served by viaSocket at a fixed URL and is the only supported way to open the
13
+ * popup: it registers the token, hands the session over, opens the right page for its
14
+ * environment, and listens for the result on both paths it can arrive by (the popup's own
15
+ * postMessage, and a relay for sign-in pages that sever the popup's link to this page). A
16
+ * viasocket URL built by hand opens a page that does not exist.
11
17
  */
12
18
  Object.defineProperty(exports, "__esModule", { value: true });
13
19
  exports.ViaSocketConnectError = void 0;
14
20
  exports.loadConnectScript = loadConnectScript;
15
21
  exports.connect = connect;
16
- const DEFAULT_SCRIPT_URL = 'https://embed.viasocket.com/prod-connectcomponent.js';
22
+ const DEFAULT_SCRIPT_URL = 'https://flow.viasocket.com/connect.js';
17
23
  const SCRIPT_ID = 'viasocket-connect-script';
24
+ /** A service id is one bare token of letters and digits: `rowo0bqrhj5g`, never `rowo0bqrhj5g_gmail`. */
25
+ const SERVICE_ID = /^[A-Za-z0-9]+$/;
26
+ /** How long a late success is still delivered after `closed` — the script's own bound. */
27
+ const LATE_SUCCESS_WINDOW_MS = 15 * 60 * 1000;
18
28
  class ViaSocketConnectError extends Error {
19
29
  code;
20
30
  serviceId;
@@ -58,11 +68,13 @@ function loadConnectScript(scriptUrl = DEFAULT_SCRIPT_URL) {
58
68
  });
59
69
  return scriptPromise;
60
70
  }
71
+ const RESULT_TYPES = ['viasocket_connection_success', 'viasocket_connection_error', 'viasocket_connection_closed'];
61
72
  /**
62
73
  * Opens the consent popup for one app and resolves with the connection it creates.
63
74
  *
64
- * Rejects with a ViaSocketConnectError whose `code` says why: 'closed' if the user gave up,
65
- * 'rejected' if the app said no, 'script' if the connect script could not load.
75
+ * Rejects with a ViaSocketConnectError whose `code` says why: 'closed' if the user seems to have
76
+ * given up (best-effort — see `onLateSuccess`), 'rejected' if the app or viaSocket said no,
77
+ * 'invalid' if the serviceId is not a service id, 'script' if the connect script could not load.
66
78
  */
67
79
  async function connect(options) {
68
80
  const serviceId = options?.serviceId;
@@ -70,36 +82,62 @@ async function connect(options) {
70
82
  throw new ViaSocketConnectError('unavailable', serviceId ?? '', 'connect: embedToken is required');
71
83
  if (!serviceId)
72
84
  throw new ViaSocketConnectError('unavailable', '', 'connect: serviceId is required');
85
+ if (typeof serviceId !== 'string' || !SERVICE_ID.test(serviceId)) {
86
+ throw new ViaSocketConnectError('invalid', String(serviceId), `connect: "${serviceId}" is not a service id. Pass the service_id exactly as the catalog returns it (a bare id such as rowo0bqrhj5g), not the app's name or slug, and not the id with a name joined on`);
87
+ }
73
88
  await loadConnectScript(options.scriptUrl);
74
89
  const open = window.openViasocketConnection;
75
90
  if (typeof open !== 'function') {
76
91
  throw new ViaSocketConnectError('script', serviceId, 'The connect script loaded but did not expose openViasocketConnection()');
77
92
  }
78
93
  return new Promise((resolve, reject) => {
79
- const onMessage = (event) => {
94
+ let closedReported = false;
95
+ let lateTimer = null;
96
+ const cleanup = () => {
97
+ window.removeEventListener('message', onMessage);
98
+ if (lateTimer)
99
+ clearTimeout(lateTimer);
100
+ };
101
+ const toResult = (payload) => {
102
+ const connection = (payload.data ?? {});
103
+ const authId = typeof connection.id === 'string' ? connection.id : '';
104
+ return authId ? { authId, serviceId, connection } : null;
105
+ };
106
+ function onMessage(event) {
80
107
  const payload = event?.data;
81
- if (!payload || typeof payload.type !== 'string' || !payload.type.startsWith('viasocket_connection'))
108
+ if (!payload || typeof payload.type !== 'string' || !RESULT_TYPES.includes(payload.type))
82
109
  return;
83
110
  // Two apps can be connecting in one page; only this app's messages are ours.
84
111
  if (payload.serviceId && payload.serviceId !== serviceId)
85
112
  return;
86
- window.removeEventListener('message', onMessage);
113
+ if (payload.type === 'viasocket_connection_closed') {
114
+ if (closedReported)
115
+ return;
116
+ closedReported = true;
117
+ // Keep listening: a close on the app's sign-in page is inferred, and the script still
118
+ // delivers a success that arrives after it.
119
+ lateTimer = setTimeout(cleanup, LATE_SUCCESS_WINDOW_MS);
120
+ reject(new ViaSocketConnectError('closed', serviceId, 'The popup seems to have been closed before the connection finished'));
121
+ return;
122
+ }
123
+ cleanup();
87
124
  if (payload.type === 'viasocket_connection_success') {
88
- const connection = (payload.data ?? {});
89
- const authId = typeof connection.id === 'string' ? connection.id : '';
90
- if (!authId) {
125
+ const result = toResult(payload);
126
+ if (closedReported) {
127
+ if (result)
128
+ options.onLateSuccess?.(result);
129
+ return;
130
+ }
131
+ if (!result) {
91
132
  reject(new ViaSocketConnectError('rejected', serviceId, 'The connection succeeded but carried no id'));
92
133
  return;
93
134
  }
94
- resolve({ authId, serviceId, connection });
135
+ resolve(result);
95
136
  }
96
- else if (payload.type === 'viasocket_connection_error') {
137
+ else if (!closedReported) {
97
138
  reject(new ViaSocketConnectError('rejected', serviceId, payload.error?.message || 'The app rejected the connection'));
98
139
  }
99
- else if (payload.type === 'viasocket_connection_closed') {
100
- reject(new ViaSocketConnectError('closed', serviceId, 'The popup was closed before the connection finished'));
101
- }
102
- };
140
+ }
103
141
  window.addEventListener('message', onMessage);
104
142
  const scriptOptions = {};
105
143
  if (options.actions?.length)
@@ -7,14 +7,26 @@
7
7
  * It wraps viaSocket's standalone connect script — the popup, the postMessage handshake, the
8
8
  * three message types — behind one promise. Nothing here touches the signing secret: the
9
9
  * embedToken comes from your backend, already signed for this end user.
10
+ *
11
+ * The script is served by viaSocket at a fixed URL and is the only supported way to open the
12
+ * popup: it registers the token, hands the session over, opens the right page for its
13
+ * environment, and listens for the result on both paths it can arrive by (the popup's own
14
+ * postMessage, and a relay for sign-in pages that sever the popup's link to this page). A
15
+ * viasocket URL built by hand opens a page that does not exist.
10
16
  */
11
17
  export type ConnectErrorCode =
12
- /** The user closed the popup before finishing. Nothing was created. */
18
+ /**
19
+ * The popup seems to have been closed before finishing. Best-effort: once the popup is on the
20
+ * app's own sign-in page a close can only be inferred, and arrives about a minute later. If the
21
+ * user in fact finishes after that, `onLateSuccess` is called with the connection.
22
+ */
13
23
  'closed'
14
- /** The app refused the authorisation. */
24
+ /** The app refused the authorisation, or viaSocket refused the request (an id that names no app). */
15
25
  | 'rejected'
16
26
  /** The connect script could not be loaded. */
17
27
  | 'script'
28
+ /** The `serviceId` is not a bare service id — the app's name, its slug, or an id with a name joined on. */
29
+ | 'invalid'
18
30
  /** Not running in a browser, or the popup could not be opened. */
19
31
  | 'unavailable';
20
32
  export declare class ViaSocketConnectError extends Error {
@@ -25,7 +37,10 @@ export declare class ViaSocketConnectError extends Error {
25
37
  export interface ConnectOptions {
26
38
  /** Signed on your backend for the current end user — see `viasocket.user(id).token()`. */
27
39
  embedToken: string;
28
- /** The app to connect. Its service_id is shown on every app in the Apps API reference. */
40
+ /**
41
+ * The app to connect: its `service_id` exactly as the catalog returns it (`rowo0bqrhj5g`).
42
+ * Never the app's name or slug, never the id with a name joined on.
43
+ */
29
44
  serviceId: string;
30
45
  /**
31
46
  * The `action_id`s and `trigger_id`s this product uses (listed under each one in the app's
@@ -35,7 +50,17 @@ export interface ConnectOptions {
35
50
  actions?: string[];
36
51
  /** With `actions`: skip the action list and open the app's consent screen directly. */
37
52
  skipActionSelection?: boolean;
38
- /** Where the connect script is served from. Only override for a non-production environment. */
53
+ /**
54
+ * Called if the connection completes after `connect()` already rejected with `'closed'` — the
55
+ * close was inferred while the user was still on the app's sign-in page. Treat it exactly like
56
+ * a resolved `connect()`.
57
+ */
58
+ onLateSuccess?: (result: ConnectResult) => void;
59
+ /**
60
+ * Where the connect script is served from. The script picks its API and auth hosts from its
61
+ * own origin, so for a non-production stack point this at that stack's copy
62
+ * (`https://dev-flow.viasocket.com/connect.js`).
63
+ */
39
64
  scriptUrl?: string;
40
65
  }
41
66
  /** The third argument of the connect script's `openViasocketConnection`. */
@@ -52,7 +77,7 @@ export interface ConnectResult {
52
77
  }
53
78
  declare global {
54
79
  interface Window {
55
- openViasocketConnection?: (embedToken: string, serviceId: string, options?: ConnectScriptOptions) => void;
80
+ openViasocketConnection?: (embedToken: string, serviceId: string, options?: ConnectScriptOptions) => Promise<unknown> | void;
56
81
  }
57
82
  }
58
83
  /**
@@ -63,8 +88,9 @@ export declare function loadConnectScript(scriptUrl?: string): Promise<void>;
63
88
  /**
64
89
  * Opens the consent popup for one app and resolves with the connection it creates.
65
90
  *
66
- * Rejects with a ViaSocketConnectError whose `code` says why: 'closed' if the user gave up,
67
- * 'rejected' if the app said no, 'script' if the connect script could not load.
91
+ * Rejects with a ViaSocketConnectError whose `code` says why: 'closed' if the user seems to have
92
+ * given up (best-effort — see `onLateSuccess`), 'rejected' if the app or viaSocket said no,
93
+ * 'invalid' if the serviceId is not a service id, 'script' if the connect script could not load.
68
94
  */
69
95
  export declare function connect(options: ConnectOptions): Promise<ConnectResult>;
70
96
  export {};
@@ -7,9 +7,19 @@
7
7
  * It wraps viaSocket's standalone connect script — the popup, the postMessage handshake, the
8
8
  * three message types — behind one promise. Nothing here touches the signing secret: the
9
9
  * embedToken comes from your backend, already signed for this end user.
10
+ *
11
+ * The script is served by viaSocket at a fixed URL and is the only supported way to open the
12
+ * popup: it registers the token, hands the session over, opens the right page for its
13
+ * environment, and listens for the result on both paths it can arrive by (the popup's own
14
+ * postMessage, and a relay for sign-in pages that sever the popup's link to this page). A
15
+ * viasocket URL built by hand opens a page that does not exist.
10
16
  */
11
- const DEFAULT_SCRIPT_URL = 'https://embed.viasocket.com/prod-connectcomponent.js';
17
+ const DEFAULT_SCRIPT_URL = 'https://flow.viasocket.com/connect.js';
12
18
  const SCRIPT_ID = 'viasocket-connect-script';
19
+ /** A service id is one bare token of letters and digits: `rowo0bqrhj5g`, never `rowo0bqrhj5g_gmail`. */
20
+ const SERVICE_ID = /^[A-Za-z0-9]+$/;
21
+ /** How long a late success is still delivered after `closed` — the script's own bound. */
22
+ const LATE_SUCCESS_WINDOW_MS = 15 * 60 * 1000;
13
23
  export class ViaSocketConnectError extends Error {
14
24
  code;
15
25
  serviceId;
@@ -52,11 +62,13 @@ export function loadConnectScript(scriptUrl = DEFAULT_SCRIPT_URL) {
52
62
  });
53
63
  return scriptPromise;
54
64
  }
65
+ const RESULT_TYPES = ['viasocket_connection_success', 'viasocket_connection_error', 'viasocket_connection_closed'];
55
66
  /**
56
67
  * Opens the consent popup for one app and resolves with the connection it creates.
57
68
  *
58
- * Rejects with a ViaSocketConnectError whose `code` says why: 'closed' if the user gave up,
59
- * 'rejected' if the app said no, 'script' if the connect script could not load.
69
+ * Rejects with a ViaSocketConnectError whose `code` says why: 'closed' if the user seems to have
70
+ * given up (best-effort — see `onLateSuccess`), 'rejected' if the app or viaSocket said no,
71
+ * 'invalid' if the serviceId is not a service id, 'script' if the connect script could not load.
60
72
  */
61
73
  export async function connect(options) {
62
74
  const serviceId = options?.serviceId;
@@ -64,36 +76,62 @@ export async function connect(options) {
64
76
  throw new ViaSocketConnectError('unavailable', serviceId ?? '', 'connect: embedToken is required');
65
77
  if (!serviceId)
66
78
  throw new ViaSocketConnectError('unavailable', '', 'connect: serviceId is required');
79
+ if (typeof serviceId !== 'string' || !SERVICE_ID.test(serviceId)) {
80
+ throw new ViaSocketConnectError('invalid', String(serviceId), `connect: "${serviceId}" is not a service id. Pass the service_id exactly as the catalog returns it (a bare id such as rowo0bqrhj5g), not the app's name or slug, and not the id with a name joined on`);
81
+ }
67
82
  await loadConnectScript(options.scriptUrl);
68
83
  const open = window.openViasocketConnection;
69
84
  if (typeof open !== 'function') {
70
85
  throw new ViaSocketConnectError('script', serviceId, 'The connect script loaded but did not expose openViasocketConnection()');
71
86
  }
72
87
  return new Promise((resolve, reject) => {
73
- const onMessage = (event) => {
88
+ let closedReported = false;
89
+ let lateTimer = null;
90
+ const cleanup = () => {
91
+ window.removeEventListener('message', onMessage);
92
+ if (lateTimer)
93
+ clearTimeout(lateTimer);
94
+ };
95
+ const toResult = (payload) => {
96
+ const connection = (payload.data ?? {});
97
+ const authId = typeof connection.id === 'string' ? connection.id : '';
98
+ return authId ? { authId, serviceId, connection } : null;
99
+ };
100
+ function onMessage(event) {
74
101
  const payload = event?.data;
75
- if (!payload || typeof payload.type !== 'string' || !payload.type.startsWith('viasocket_connection'))
102
+ if (!payload || typeof payload.type !== 'string' || !RESULT_TYPES.includes(payload.type))
76
103
  return;
77
104
  // Two apps can be connecting in one page; only this app's messages are ours.
78
105
  if (payload.serviceId && payload.serviceId !== serviceId)
79
106
  return;
80
- window.removeEventListener('message', onMessage);
107
+ if (payload.type === 'viasocket_connection_closed') {
108
+ if (closedReported)
109
+ return;
110
+ closedReported = true;
111
+ // Keep listening: a close on the app's sign-in page is inferred, and the script still
112
+ // delivers a success that arrives after it.
113
+ lateTimer = setTimeout(cleanup, LATE_SUCCESS_WINDOW_MS);
114
+ reject(new ViaSocketConnectError('closed', serviceId, 'The popup seems to have been closed before the connection finished'));
115
+ return;
116
+ }
117
+ cleanup();
81
118
  if (payload.type === 'viasocket_connection_success') {
82
- const connection = (payload.data ?? {});
83
- const authId = typeof connection.id === 'string' ? connection.id : '';
84
- if (!authId) {
119
+ const result = toResult(payload);
120
+ if (closedReported) {
121
+ if (result)
122
+ options.onLateSuccess?.(result);
123
+ return;
124
+ }
125
+ if (!result) {
85
126
  reject(new ViaSocketConnectError('rejected', serviceId, 'The connection succeeded but carried no id'));
86
127
  return;
87
128
  }
88
- resolve({ authId, serviceId, connection });
129
+ resolve(result);
89
130
  }
90
- else if (payload.type === 'viasocket_connection_error') {
131
+ else if (!closedReported) {
91
132
  reject(new ViaSocketConnectError('rejected', serviceId, payload.error?.message || 'The app rejected the connection'));
92
133
  }
93
- else if (payload.type === 'viasocket_connection_closed') {
94
- reject(new ViaSocketConnectError('closed', serviceId, 'The popup was closed before the connection finished'));
95
- }
96
- };
134
+ }
97
135
  window.addEventListener('message', onMessage);
98
136
  const scriptOptions = {};
99
137
  if (options.actions?.length)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "viasocket-apps",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "Server-side client for the viaSocket Apps API: let your users connect Slack, GitHub, Google Sheets or any of 2,300+ apps from your own product, then run actions and receive events through one API.",
5
5
  "keywords": [
6
6
  "viasocket",