viasocket-apps 0.1.0 → 0.1.1

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
@@ -83,6 +83,23 @@ const { authId } = await connect({ embedToken, serviceId: 'rowbu58rc' })
83
83
  | `viasocket.runAction(scriptId, actionVersionId, inputData)` | Run one action. Returns the app's own response. |
84
84
  | `user.subscribe(triggerVersionId, { authId, inputData, webhook \| code, meta })` | Subscribe to an app event. Returns `{ scriptId, hookUrl, … }`. |
85
85
  | `user.updateSubscription(scriptId, { code, meta })` | Change a live subscription in place. |
86
+
87
+ `webhook` is a URL of yours that we POST each event to. `code` is the alternative: a script we run
88
+ on viaSocket's servers each time the event fires. It is a string, executed away from your process,
89
+ so it must stand alone — no imports, nothing from your codebase. In scope there are `axios`, `fetch`
90
+ and `context`, and the event the app sent is `context.req.body`:
91
+
92
+ ```js
93
+ await user.subscribe(triggerVersionId, {
94
+ authId,
95
+ inputData,
96
+ code: `
97
+ const event = context.req.body
98
+ await axios.post("https://your-app.com/webhooks/viasocket", { event, user_id: "${endUserId}" })
99
+ return { forwarded: true }
100
+ `
101
+ })
102
+ ```
86
103
  | `user.listFlows()` | Every enabled app and subscription this user has. |
87
104
  | `user.disableFlow(scriptId)` / `user.enableFlow(scriptId)` | Turn a flow off or back on. Disabling a subscription ends it. |
88
105
  | `user.listConnections()` | Every app this user has connected. |
@@ -127,8 +127,9 @@ class UserScope {
127
127
  return { options: Array.isArray(data?.data) ? data.data : [], offset: data?.offset ?? null };
128
128
  }
129
129
  /**
130
- * Subscribes to an app event. Give `webhook` (a URL we POST each event to) or `code`
131
- * (JavaScript we run per event) — one or the other.
130
+ * Subscribes to an app event. Give `webhook` (a URL we POST each event to) or `code` (a
131
+ * self-contained script we run on our servers per event, with the event at
132
+ * `context.req.body`) — one or the other.
132
133
  */
133
134
  async subscribe(triggerVersionId, params) {
134
135
  if (!params?.webhook === !params?.code) {
package/dist/cjs/http.js CHANGED
@@ -2,10 +2,16 @@
2
2
  /**
3
3
  * The one place a request is made and a response judged.
4
4
  *
5
- * viaSocket answers every call with the same envelope, and it can say "no" two ways: a non-2xx
6
- * status, or a 2xx whose body carries `success: false`. Callers should never have to remember
7
- * that, so both become a ViaSocketError here with the API's own message attached — that message
8
- * is almost always the explanation.
5
+ * The embed endpoints answer with one envelope, `{ success, data }`, and can say "no" two ways:
6
+ * a non-2xx status, or a 2xx whose body carries `success: false`. Callers should never have to
7
+ * remember that, so both become a ViaSocketError here with the API's own message attached —
8
+ * that message is almost always the explanation.
9
+ *
10
+ * The action runner is the exception: some actions answer in that envelope, many hand back their
11
+ * own result at the top level with no `success` or `data` key at all (Gmail's list-mails returns
12
+ * `{ emails, nextPageToken }`). So a body only counts as an envelope when it actually carries a
13
+ * boolean `success`; anything else is the result itself. Getting this wrong returned `undefined`
14
+ * for every such action.
9
15
  */
10
16
  Object.defineProperty(exports, "__esModule", { value: true });
11
17
  exports.request = request;
@@ -29,9 +35,15 @@ async function request(options) {
29
35
  catch {
30
36
  payload = text;
31
37
  }
32
- const envelope = payload && typeof payload === 'object' ? payload : null;
38
+ const isEnvelope = payload !== null && typeof payload === 'object' && typeof payload.success === 'boolean';
39
+ const envelope = isEnvelope ? payload : null;
33
40
  if (!response.ok || envelope?.success === false) {
34
- const message = envelope?.message || envelope?.error?.message || `viaSocket responded ${response.status} for ${options.method} ${options.url}`;
41
+ const raw = payload && typeof payload === 'object' ? payload : null;
42
+ const message = envelope?.message ||
43
+ envelope?.error?.message ||
44
+ raw?.message ||
45
+ raw?.error?.message ||
46
+ `viaSocket responded ${response.status} for ${options.method} ${options.url}`;
35
47
  throw new errors_js_1.ViaSocketError(message, { status: response.status, body: payload });
36
48
  }
37
49
  return (envelope ?? { success: true, data: payload });
@@ -56,8 +56,9 @@ export declare class UserScope {
56
56
  */
57
57
  listOptions(actionVersionId: string, params: ListOptionsParams): Promise<ListOptionsResult>;
58
58
  /**
59
- * Subscribes to an app event. Give `webhook` (a URL we POST each event to) or `code`
60
- * (JavaScript we run per event) — one or the other.
59
+ * Subscribes to an app event. Give `webhook` (a URL we POST each event to) or `code` (a
60
+ * self-contained script we run on our servers per event, with the event at
61
+ * `context.req.body`) — one or the other.
61
62
  */
62
63
  subscribe(triggerVersionId: string, params: SubscribeParams): Promise<SubscribeResult>;
63
64
  /** Changes a live subscription in place. The script_id does not change. */
@@ -123,8 +123,9 @@ export class UserScope {
123
123
  return { options: Array.isArray(data?.data) ? data.data : [], offset: data?.offset ?? null };
124
124
  }
125
125
  /**
126
- * Subscribes to an app event. Give `webhook` (a URL we POST each event to) or `code`
127
- * (JavaScript we run per event) — one or the other.
126
+ * Subscribes to an app event. Give `webhook` (a URL we POST each event to) or `code` (a
127
+ * self-contained script we run on our servers per event, with the event at
128
+ * `context.req.body`) — one or the other.
128
129
  */
129
130
  async subscribe(triggerVersionId, params) {
130
131
  if (!params?.webhook === !params?.code) {
@@ -1,10 +1,16 @@
1
1
  /**
2
2
  * The one place a request is made and a response judged.
3
3
  *
4
- * viaSocket answers every call with the same envelope, and it can say "no" two ways: a non-2xx
5
- * status, or a 2xx whose body carries `success: false`. Callers should never have to remember
6
- * that, so both become a ViaSocketError here with the API's own message attached — that message
7
- * is almost always the explanation.
4
+ * The embed endpoints answer with one envelope, `{ success, data }`, and can say "no" two ways:
5
+ * a non-2xx status, or a 2xx whose body carries `success: false`. Callers should never have to
6
+ * remember that, so both become a ViaSocketError here with the API's own message attached —
7
+ * that message is almost always the explanation.
8
+ *
9
+ * The action runner is the exception: some actions answer in that envelope, many hand back their
10
+ * own result at the top level with no `success` or `data` key at all (Gmail's list-mails returns
11
+ * `{ emails, nextPageToken }`). So a body only counts as an envelope when it actually carries a
12
+ * boolean `success`; anything else is the result itself. Getting this wrong returned `undefined`
13
+ * for every such action.
8
14
  */
9
15
  import type { Envelope } from './types.js';
10
16
  export interface RequestOptions {
package/dist/esm/http.js CHANGED
@@ -1,10 +1,16 @@
1
1
  /**
2
2
  * The one place a request is made and a response judged.
3
3
  *
4
- * viaSocket answers every call with the same envelope, and it can say "no" two ways: a non-2xx
5
- * status, or a 2xx whose body carries `success: false`. Callers should never have to remember
6
- * that, so both become a ViaSocketError here with the API's own message attached — that message
7
- * is almost always the explanation.
4
+ * The embed endpoints answer with one envelope, `{ success, data }`, and can say "no" two ways:
5
+ * a non-2xx status, or a 2xx whose body carries `success: false`. Callers should never have to
6
+ * remember that, so both become a ViaSocketError here with the API's own message attached —
7
+ * that message is almost always the explanation.
8
+ *
9
+ * The action runner is the exception: some actions answer in that envelope, many hand back their
10
+ * own result at the top level with no `success` or `data` key at all (Gmail's list-mails returns
11
+ * `{ emails, nextPageToken }`). So a body only counts as an envelope when it actually carries a
12
+ * boolean `success`; anything else is the result itself. Getting this wrong returned `undefined`
13
+ * for every such action.
8
14
  */
9
15
  import { ViaSocketError } from './errors.js';
10
16
  export async function request(options) {
@@ -26,9 +32,15 @@ export async function request(options) {
26
32
  catch {
27
33
  payload = text;
28
34
  }
29
- const envelope = payload && typeof payload === 'object' ? payload : null;
35
+ const isEnvelope = payload !== null && typeof payload === 'object' && typeof payload.success === 'boolean';
36
+ const envelope = isEnvelope ? payload : null;
30
37
  if (!response.ok || envelope?.success === false) {
31
- const message = envelope?.message || envelope?.error?.message || `viaSocket responded ${response.status} for ${options.method} ${options.url}`;
38
+ const raw = payload && typeof payload === 'object' ? payload : null;
39
+ const message = envelope?.message ||
40
+ envelope?.error?.message ||
41
+ raw?.message ||
42
+ raw?.error?.message ||
43
+ `viaSocket responded ${response.status} for ${options.method} ${options.url}`;
32
44
  throw new ViaSocketError(message, { status: response.status, body: payload });
33
45
  }
34
46
  return (envelope ?? { success: true, data: payload });
@@ -54,7 +54,14 @@ export interface SubscribeParams {
54
54
  inputData: Record<string, unknown>;
55
55
  /** Your endpoint. We POST every event to it. Use this or `code`, not both. */
56
56
  webhook?: string;
57
- /** JavaScript run on each event instead of a webhook. */
57
+ /**
58
+ * Instead of a webhook: a script we run on viaSocket's servers each time the event fires.
59
+ *
60
+ * It is a string of JavaScript executed away from your process, so it must stand alone — no
61
+ * imports and no reference to anything in your codebase. In scope there: `axios`, `fetch` and
62
+ * `context`; the event the app sent is `context.req.body`. Bake in any value of yours (a user
63
+ * id, a URL) when you build the string.
64
+ */
58
65
  code?: string;
59
66
  /** Anything of yours; handed back with every delivery. */
60
67
  meta?: Record<string, unknown>;
@@ -70,6 +77,7 @@ export interface SubscribeResult {
70
77
  meta: Record<string, unknown>;
71
78
  }
72
79
  export interface UpdateSubscriptionParams {
80
+ /** A new script to run per event. Same rules as `SubscribeParams.code`. */
73
81
  code?: string;
74
82
  meta?: Record<string, unknown>;
75
83
  }
package/package.json CHANGED
@@ -1,8 +1,17 @@
1
1
  {
2
2
  "name": "viasocket-apps",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
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
- "keywords": ["viasocket", "integrations", "oauth", "api", "webhooks", "slack", "github", "google-sheets"],
5
+ "keywords": [
6
+ "viasocket",
7
+ "integrations",
8
+ "oauth",
9
+ "api",
10
+ "webhooks",
11
+ "slack",
12
+ "github",
13
+ "google-sheets"
14
+ ],
6
15
  "license": "MIT",
7
16
  "author": "Walkover Web Solution",
8
17
  "homepage": "https://github.com/Walkover-Web-Solution/viasocket-apps#readme",
@@ -31,7 +40,10 @@
31
40
  "require": "./dist/cjs/browser.js"
32
41
  }
33
42
  },
34
- "files": ["dist", "README.md"],
43
+ "files": [
44
+ "dist",
45
+ "README.md"
46
+ ],
35
47
  "sideEffects": false,
36
48
  "scripts": {
37
49
  "build": "tsc -p tsconfig.esm.json && tsc -p tsconfig.cjs.json && node scripts/finish-build.mjs",