@particle-academy/gmail-inbox-js 0.1.0 → 0.2.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/CHANGELOG.md CHANGED
@@ -12,6 +12,41 @@ derived — somebody has to write what changed — but writing it in the
12
12
  provider repo would be a hand-edit that `.weaver.json` flags and the next
13
13
  sync destroys.
14
14
 
15
+ ## [0.2.0] — 2026-11-08
16
+
17
+ ### Added
18
+
19
+ - **`message_received`: a trigger for new Gmail mail, still read-only**
20
+ (`gmail.readonly` is the only scope). `mailbox_watch` makes Gmail publish
21
+ mailbox changes to a Cloud Pub/Sub topic in YOUR app's own Google Cloud
22
+ project, and a push subscription delivers each change to your host.
23
+ - **Every push is verified before anything reads it.** It carries a
24
+ Google-signed token in `Authorization`, checked for Google's issuer, the
25
+ subscription's audience (`pushAudience`), and the push service account
26
+ with `email_verified` true (`pushServiceAccount`). The service-account
27
+ check is what refuses a stranger's subscription aimed at your URL.
28
+ - **Routing.** The Pub/Sub envelope is unwrapped, then the push is routed by
29
+ `emailAddress`, only after verification. One topic serves every mailbox.
30
+ - **What arrives.** A push carries only the mailbox's NEW position, so the
31
+ trigger reads `history.list` from the cursor YOUR HOST stored. It reads
32
+ every page and publishes the changes under `history` with `cursor: {used,
33
+ next}`. Your host persists `cursor.next` when a run succeeds.
34
+ - **Received mail only, by default:** `labelId: INBOX` and `historyTypes:
35
+ messageAdded`, so no sent mail or drafts. Widen either in the trigger's
36
+ config.
37
+ - **A lapsed cursor** (Gmail's 404) or more than 20 pages fails by name, as
38
+ `cursor-expired` or `cursor-overflow`. Each carries a bounded resync query
39
+ (`newer_than:7d`; `resyncWindowDays` sets it, default 7). Watch again
40
+ first, then list with that query. The mailbox is never fully re-pulled.
41
+ - **The watch is a lease:** Gmail's `expiration` is in epoch milliseconds,
42
+ renewed by watching again, a day early. Google requires at least once
43
+ every 7 days and recommends daily.
44
+ - **Read state is never changed.**
45
+
46
+ New actions: `mailbox_watch`, `mailbox_stop` and `history_list`. New
47
+ credentials: `pushAudience` and `pushServiceAccount`, needed only for the
48
+ trigger. Requires fancy-connector-core 0.14.0.
49
+
15
50
  ## [0.1.0] — 2026-10-01
16
51
 
17
52
  ### Added
package/README.md CHANGED
@@ -35,7 +35,7 @@ Everything below is generated from `provider/manifest.json`, so it cannot disagr
35
35
 
36
36
  ### Credentials
37
37
 
38
- A Gmail Inbox connection holds 4 values.
38
+ A Gmail Inbox connection holds 6 values.
39
39
 
40
40
  **Two kinds of value, and mixing them up matters.** A `provider` credential is ONE value for the whole installation — an OAuth app's client secret serves every connected account. An `account` credential is one per connected account. A host that stores the second where it stores the first lets one account's credentials reach another's.
41
41
 
@@ -45,6 +45,8 @@ A Gmail Inbox connection holds 4 values.
45
45
  | **OAuth client secret** | per installation | **secret** | The client secret for the same OAuth app. One value for the whole installation. |
46
46
  | **Access token** | per connected account | **secret** | Per connected Google account, and it expires after ONE HOUR. The host refreshes it with the refresh token. |
47
47
  | **Refresh token** | per connected account | **secret** | Per connected Google account. Google issues one only when the consent request asks for offline access; without it the connection dies within the hour. |
48
+ | **Push audience** *(optional)* | per installation | not secret | The audience set on the Pub/Sub PUSH subscription (normally the endpoint URL your host mounts for message_received). Every push's Google-signed token must name it; an unchecked audience accepts any token Google signs for anyone. Needed only for the message_received trigger. |
49
+ | **Push service account** *(optional)* | per installation | not secret | The service account the push subscription authenticates as (its email). Every push's token must be for exactly this account: a stranger's subscription aimed at your URL carries a token Google signed correctly, and only this check refuses it. Needed only for message_received. |
48
50
 
49
51
  ### Authorising
50
52
 
@@ -67,6 +69,21 @@ The refresh tokens do **not** rotate: the same one is reusable, so a refresh may
67
69
 
68
70
  ### Actions
69
71
 
72
+ #### `history_list` — List Gmail mailbox history
73
+
74
+ List what changed in a mailbox since a history id: messages added or deleted, labels added or removed.
75
+
76
+ `GET /gmail/v1/users/{userId}/history` · reads only — safe to replay
77
+
78
+ | Input | Required | What it is |
79
+ |---|---|---|
80
+ | `userId` | yes | `me` is the connected account and is almost always what you want. |
81
+ | `startHistoryId` | yes | Changes AFTER this id. Take it from a watch, a message, or a previous list's historyId. Out of Gmail's range (typically older than a week) it answers 404. |
82
+ | `historyTypes` | no | Blank: every kind. messageAdded is new mail (and sent mail and drafts, unless a label narrows it). |
83
+ | `labelId` | no | Only messages with this label, e.g. INBOX for received mail. |
84
+ | `maxResults` | no | Gmail's own maximum is 500. |
85
+ | `pageToken` | no | From a previous page's nextPageToken. |
86
+
70
87
  #### `label_list` — List Gmail labels
71
88
 
72
89
  List every label on the mailbox, system and user-created.
@@ -77,6 +94,29 @@ List every label on the mailbox, system and user-created.
77
94
  |---|---|---|
78
95
  | `userId` | yes | Mailbox |
79
96
 
97
+ #### `mailbox_stop` — Stop watching a Gmail mailbox
98
+
99
+ Turn off push notifications for a mailbox.
100
+
101
+ `POST /gmail/v1/users/{userId}/stop` · idempotent — safe to replay
102
+
103
+ | Input | Required | What it is |
104
+ |---|---|---|
105
+ | `userId` | yes | `me` is the connected account and is almost always what you want. |
106
+
107
+ #### `mailbox_watch` — Watch a Gmail mailbox
108
+
109
+ Start (or renew) push notifications for a mailbox to a Cloud Pub/Sub topic.
110
+
111
+ `POST /gmail/v1/users/{userId}/watch` · idempotent — safe to replay
112
+
113
+ | Input | Required | What it is |
114
+ |---|---|---|
115
+ | `userId` | yes | `me` is the connected account and is almost always what you want. |
116
+ | `topicName` | yes | The full topic name, in YOUR app's own Google Cloud project: Google requires its project id to match the project whose OAuth client makes this call. One topic serves every mailbox. |
117
+ | `labelIds` | no | Comma-separated label ids to restrict notifications to (e.g. INBOX). Blank: every change is pushed. |
118
+ | `labelFilterBehavior` | no | Whether the labels above are the ONLY ones notified (include) or the ones NOT notified (exclude). |
119
+
80
120
  #### `message_get` — Gmail message
81
121
 
82
122
  Read one message's headers and preview. The body's plain/HTML text is NOT decoded -- see the raw payload for that.
@@ -114,6 +154,18 @@ Read every message in a conversation thread, oldest first.
114
154
  | `userId` | yes | Mailbox |
115
155
  | `id` | yes | A message's threadId, from message_list or message_get. |
116
156
 
157
+ ### Triggers
158
+
159
+ #### `message_received` — Gmail message received
160
+
161
+ Start when new mail arrives in a watched mailbox, with every new message since the last run.
162
+
163
+ A SUBSCRIPTION: Gmail Inbox delivers to your endpoint only while a subscription it issued is alive, at most 168 hours at a time. Its expiry is read from the `mailbox_watch` response (`expiration`); 86400 seconds before it, the host renews it (`mailbox_watch`). A lease that lapses is re-listed AND re-subscribed, because notifications during the gap are gone. Every delivery: the signature is verified before anything runs. The delivery only names what arrived, so the run's first step reads it: `history_list` with , published under `history`. A failed read fails the run.
164
+
165
+ **You have to set this up with the provider first:**
166
+
167
+ In YOUR app's Google Cloud project (the one owning the OAuth client): create a Pub/Sub topic, grant publish on it to gmail-api-push@system.gserviceaccount.com, and create a PUSH subscription to the route your host mounts for this trigger, with authentication on (a service account you choose; the endpoint URL as the audience). Put that audience and service account on the connection as pushAudience and pushServiceAccount, and fetch Google's signing keys for verification. Then call mailbox_watch with the topic for each mailbox and store its historyId as the trigger's cursor (startHistoryId); renew the watch daily. ONE topic serves every mailbox: route each push by its emailAddress, and only after the token verifies, to a mailbox you watched; drop an unmapped address with a 2xx. Persist cursor.next when a run succeeds. On cursor-expired or cursor-overflow, watch again for a fresh historyId FIRST, then list with message_list using the resyncQuery, and match it against the mail you already handled. This trigger never changes read state.
168
+
117
169
  ## Run it before you have credentials
118
170
 
119
171
  Every operation ships a **faker**, whether or not Gmail Inbox has a sandbox. Set a
@@ -0,0 +1,550 @@
1
+ import { callConnector, readWithCursor, verifyDelivery, decodeDelivery } from '@particle-academy/fancy-connector-core';
2
+
3
+ // src/faker.ts
4
+ function fakeHistoryList({ config, fake }) {
5
+ return {
6
+ "history": [
7
+ {
8
+ "id": "1234567891",
9
+ "messagesAdded": [
10
+ {
11
+ "message": {
12
+ "id": fake.hex(16),
13
+ "threadId": fake.hex(16),
14
+ "labelIds": [
15
+ "INBOX",
16
+ "UNREAD"
17
+ ]
18
+ }
19
+ }
20
+ ]
21
+ }
22
+ ],
23
+ "historyId": "1234567891"
24
+ };
25
+ }
26
+ function fakeLabelList({ config, fake }) {
27
+ return {
28
+ "labels": [
29
+ {
30
+ "id": "INBOX",
31
+ "name": "INBOX",
32
+ "type": "system"
33
+ },
34
+ {
35
+ "id": "UNREAD",
36
+ "name": "UNREAD",
37
+ "type": "system"
38
+ },
39
+ {
40
+ "id": "IMPORTANT",
41
+ "name": "IMPORTANT",
42
+ "type": "system",
43
+ "messagesTotal": 412,
44
+ "messagesUnread": 18,
45
+ "threadsTotal": 203,
46
+ "threadsUnread": 11
47
+ },
48
+ {
49
+ "id": fake.id("Label"),
50
+ "name": "Design Reviews",
51
+ "type": "user",
52
+ "messageListVisibility": "show",
53
+ "labelListVisibility": "labelShow",
54
+ "messagesTotal": 27,
55
+ "messagesUnread": 2,
56
+ "threadsTotal": 9,
57
+ "threadsUnread": 1
58
+ }
59
+ ]
60
+ };
61
+ }
62
+ function fakeMailboxStop({ config, fake }) {
63
+ return {};
64
+ }
65
+ function fakeMailboxWatch({ config, fake }) {
66
+ return {
67
+ "historyId": "1234567890",
68
+ "expiration": "1767312000000"
69
+ };
70
+ }
71
+ function fakeMessageGet({ config, fake }) {
72
+ return {
73
+ "id": config.id !== void 0 && config.id !== null && config.id !== "" ? String(config.id) : fake.hex(16),
74
+ "threadId": fake.hex(16),
75
+ "labelIds": [
76
+ "INBOX",
77
+ "UNREAD",
78
+ "IMPORTANT"
79
+ ],
80
+ "snippet": "Hi Ada, just confirming the design review is still on for Thursday at 2pm. Let me know if...",
81
+ "internalDate": "1790712000000",
82
+ "payload": {
83
+ "mimeType": "text/plain",
84
+ "headers": [
85
+ {
86
+ "name": "From",
87
+ "value": "Grace Hopper <grace@example.test>"
88
+ },
89
+ {
90
+ "name": "To",
91
+ "value": "Ada Lovelace <ada@example.test>"
92
+ },
93
+ {
94
+ "name": "Subject",
95
+ "value": "Re: Design review Thursday"
96
+ },
97
+ {
98
+ "name": "Date",
99
+ "value": "Tue, 29 Sep 2026 14:03:00 -0400"
100
+ }
101
+ ],
102
+ "body": {
103
+ "size": 612,
104
+ "data": fake.id("base64url_body")
105
+ }
106
+ }
107
+ };
108
+ }
109
+ function fakeMessageList({ config, fake }) {
110
+ return {
111
+ "messages": [
112
+ {
113
+ "id": fake.hex(16),
114
+ "threadId": fake.hex(16)
115
+ },
116
+ {
117
+ "id": fake.hex(16),
118
+ "threadId": fake.hex(16)
119
+ }
120
+ ],
121
+ "resultSizeEstimate": 2
122
+ };
123
+ }
124
+ function fakeThreadGet({ config, fake }) {
125
+ return {
126
+ "id": config.id !== void 0 && config.id !== null && config.id !== "" ? String(config.id) : fake.hex(16),
127
+ "historyId": Array.from({ length: 10 }, () => fake.int(0, 9)).join(""),
128
+ "messages": [
129
+ {
130
+ "id": fake.hex(16),
131
+ "labelIds": [
132
+ "INBOX",
133
+ "IMPORTANT"
134
+ ],
135
+ "snippet": "Hey team, scheduling the Q3 design review for Thursday at 2pm. Conference room B...",
136
+ "internalDate": "1790625600000",
137
+ "payload": {
138
+ "mimeType": "text/plain",
139
+ "headers": [
140
+ {
141
+ "name": "From",
142
+ "value": "Ada Lovelace <ada@example.test>"
143
+ },
144
+ {
145
+ "name": "To",
146
+ "value": "Grace Hopper <grace@example.test>"
147
+ },
148
+ {
149
+ "name": "Subject",
150
+ "value": "Design review Thursday"
151
+ },
152
+ {
153
+ "name": "Date",
154
+ "value": "Mon, 28 Sep 2026 09:00:00 -0400"
155
+ }
156
+ ],
157
+ "body": {
158
+ "size": 540,
159
+ "data": fake.id("base64url_body")
160
+ }
161
+ }
162
+ },
163
+ {
164
+ "id": fake.hex(16),
165
+ "labelIds": [
166
+ "INBOX",
167
+ "UNREAD",
168
+ "IMPORTANT"
169
+ ],
170
+ "snippet": "Hi Ada, just confirming the design review is still on for Thursday at 2pm. Let me know if...",
171
+ "internalDate": "1790712000000",
172
+ "payload": {
173
+ "mimeType": "text/plain",
174
+ "headers": [
175
+ {
176
+ "name": "From",
177
+ "value": "Grace Hopper <grace@example.test>"
178
+ },
179
+ {
180
+ "name": "To",
181
+ "value": "Ada Lovelace <ada@example.test>"
182
+ },
183
+ {
184
+ "name": "Subject",
185
+ "value": "Re: Design review Thursday"
186
+ },
187
+ {
188
+ "name": "Date",
189
+ "value": "Tue, 29 Sep 2026 14:03:00 -0400"
190
+ }
191
+ ],
192
+ "body": {
193
+ "size": 612,
194
+ "data": fake.id("base64url_body")
195
+ }
196
+ }
197
+ }
198
+ ]
199
+ };
200
+ }
201
+ function fakeMessageReceived({ config, fake }) {
202
+ return {
203
+ "subscription": "projects/my-project/subscriptions/gmail-push",
204
+ "messageId": Array.from({ length: 16 }, () => fake.int(0, 9)).join(""),
205
+ "publishTime": "2026-11-08T09:00:00.000Z",
206
+ "attributes": {},
207
+ "deliveryAttempt": null,
208
+ "data": {
209
+ "emailAddress": "user@example.test",
210
+ "historyId": 1234567891
211
+ }
212
+ };
213
+ }
214
+ var gmailInboxFaker = (operation, request) => {
215
+ switch (operation) {
216
+ case "history_list":
217
+ return fakeHistoryList(request);
218
+ case "label_list":
219
+ return fakeLabelList(request);
220
+ case "mailbox_stop":
221
+ return fakeMailboxStop(request);
222
+ case "mailbox_watch":
223
+ return fakeMailboxWatch(request);
224
+ case "message_get":
225
+ return fakeMessageGet(request);
226
+ case "message_list":
227
+ return fakeMessageList(request);
228
+ case "thread_get":
229
+ return fakeThreadGet(request);
230
+ case "message_received":
231
+ return fakeMessageReceived(request);
232
+ default:
233
+ throw new Error(
234
+ `gmail_inbox: no fake response is defined for "${operation}". Add a fixture under provider/fixtures/ and regenerate \u2014 a connector without a faker cannot be developed against, tested, or demonstrated.`
235
+ );
236
+ }
237
+ };
238
+
239
+ // src/service.ts
240
+ var CONNECTOR_API_VERSION = 1;
241
+ var GMAIL_INBOX_BASE_URLS = {
242
+ "live": "https://gmail.googleapis.com"
243
+ };
244
+ var GMAIL_INBOX_REQUIRES = [
245
+ "accessToken",
246
+ "refreshToken",
247
+ "clientId",
248
+ "clientSecret"
249
+ ];
250
+ function gmailInboxAuthorize(credentials, request, _mode) {
251
+ request.headers.Authorization = `Bearer ${credentials.accessToken ?? ""}`;
252
+ }
253
+ var GMAIL_INBOX = {
254
+ service: "gmail_inbox",
255
+ title: "Gmail Inbox",
256
+ sandbox: "none",
257
+ baseUrls: { ...GMAIL_INBOX_BASE_URLS },
258
+ requires: [...GMAIL_INBOX_REQUIRES],
259
+ authorize: gmailInboxAuthorize,
260
+ faker: gmailInboxFaker
261
+ };
262
+ var HISTORY_LIST_OPERATION = "history_list";
263
+ async function gmailInboxHistoryList(options) {
264
+ const config = options.config ?? {};
265
+ if (config.userId === void 0 || config.userId === null || config.userId === "") {
266
+ throw new Error(`history_list: "userId" is required (Mailbox).`);
267
+ }
268
+ if (config.startHistoryId === void 0 || config.startHistoryId === null || config.startHistoryId === "") {
269
+ throw new Error(`history_list: "startHistoryId" is required (Start history id).`);
270
+ }
271
+ {
272
+ const n = Number(config.maxResults);
273
+ const given = config.maxResults !== void 0 && config.maxResults !== null && config.maxResults !== "";
274
+ if (given && !(Number.isInteger(n) && n >= 1 && n <= 500)) {
275
+ throw new Error(
276
+ `history_list: "maxResults" must be a integer, got ${JSON.stringify(config.maxResults)}.`
277
+ );
278
+ }
279
+ }
280
+ return callConnector(GMAIL_INBOX, {
281
+ operation: HISTORY_LIST_OPERATION,
282
+ config,
283
+ input: options.input,
284
+ ...options.credentials === void 0 ? {} : { credentials: options.credentials },
285
+ ...options.mode === void 0 ? {} : { mode: options.mode },
286
+ ...options.connectionId === void 0 ? {} : { connectionId: options.connectionId },
287
+ ...options.attempts === void 0 ? {} : { attempts: options.attempts },
288
+ ...options.transport === void 0 ? {} : { transport: options.transport },
289
+ request: {
290
+ method: "GET",
291
+ path: `/gmail/v1/users/${encodeURIComponent(String(config.userId))}/history`,
292
+ query: {
293
+ "startHistoryId": String(config.startHistoryId),
294
+ ...config.historyTypes !== void 0 && config.historyTypes !== null && config.historyTypes !== "" ? { "historyTypes": String(config.historyTypes) } : {},
295
+ ...config.labelId !== void 0 && config.labelId !== null && config.labelId !== "" ? { "labelId": String(config.labelId) } : {},
296
+ "maxResults": config.maxResults !== void 0 && config.maxResults !== null && config.maxResults !== "" ? Math.trunc(Number(config.maxResults)) : 100,
297
+ ...config.pageToken !== void 0 && config.pageToken !== null && config.pageToken !== "" ? { "pageToken": String(config.pageToken) } : {}
298
+ }
299
+ }
300
+ });
301
+ }
302
+ var LABEL_LIST_OPERATION = "label_list";
303
+ async function gmailInboxLabelList(options) {
304
+ const config = options.config ?? {};
305
+ if (config.userId === void 0 || config.userId === null || config.userId === "") {
306
+ throw new Error(`label_list: "userId" is required (Mailbox).`);
307
+ }
308
+ return callConnector(GMAIL_INBOX, {
309
+ operation: LABEL_LIST_OPERATION,
310
+ config,
311
+ input: options.input,
312
+ ...options.credentials === void 0 ? {} : { credentials: options.credentials },
313
+ ...options.mode === void 0 ? {} : { mode: options.mode },
314
+ ...options.connectionId === void 0 ? {} : { connectionId: options.connectionId },
315
+ ...options.attempts === void 0 ? {} : { attempts: options.attempts },
316
+ ...options.transport === void 0 ? {} : { transport: options.transport },
317
+ request: {
318
+ method: "GET",
319
+ path: `/gmail/v1/users/${encodeURIComponent(String(config.userId))}/labels`,
320
+ query: {}
321
+ }
322
+ });
323
+ }
324
+ var MAILBOX_STOP_OPERATION = "mailbox_stop";
325
+ async function gmailInboxMailboxStop(options) {
326
+ const config = options.config ?? {};
327
+ if (config.userId === void 0 || config.userId === null || config.userId === "") {
328
+ throw new Error(`mailbox_stop: "userId" is required (Mailbox).`);
329
+ }
330
+ return callConnector(GMAIL_INBOX, {
331
+ operation: MAILBOX_STOP_OPERATION,
332
+ config,
333
+ input: options.input,
334
+ ...options.credentials === void 0 ? {} : { credentials: options.credentials },
335
+ ...options.mode === void 0 ? {} : { mode: options.mode },
336
+ ...options.connectionId === void 0 ? {} : { connectionId: options.connectionId },
337
+ ...options.attempts === void 0 ? {} : { attempts: options.attempts },
338
+ ...options.transport === void 0 ? {} : { transport: options.transport },
339
+ request: {
340
+ method: "POST",
341
+ path: `/gmail/v1/users/${encodeURIComponent(String(config.userId))}/stop`,
342
+ json: {}
343
+ }
344
+ });
345
+ }
346
+ var MAILBOX_WATCH_OPERATION = "mailbox_watch";
347
+ async function gmailInboxMailboxWatch(options) {
348
+ const config = options.config ?? {};
349
+ if (config.userId === void 0 || config.userId === null || config.userId === "") {
350
+ throw new Error(`mailbox_watch: "userId" is required (Mailbox).`);
351
+ }
352
+ if (config.topicName === void 0 || config.topicName === null || config.topicName === "") {
353
+ throw new Error(`mailbox_watch: "topicName" is required (Pub/Sub topic).`);
354
+ }
355
+ return callConnector(GMAIL_INBOX, {
356
+ operation: MAILBOX_WATCH_OPERATION,
357
+ config,
358
+ input: options.input,
359
+ ...options.credentials === void 0 ? {} : { credentials: options.credentials },
360
+ ...options.mode === void 0 ? {} : { mode: options.mode },
361
+ ...options.connectionId === void 0 ? {} : { connectionId: options.connectionId },
362
+ ...options.attempts === void 0 ? {} : { attempts: options.attempts },
363
+ ...options.transport === void 0 ? {} : { transport: options.transport },
364
+ request: {
365
+ method: "POST",
366
+ path: `/gmail/v1/users/${encodeURIComponent(String(config.userId))}/watch`,
367
+ json: {
368
+ "topicName": String(config.topicName).trim(),
369
+ ...config.labelIds !== void 0 && config.labelIds !== null && config.labelIds !== "" ? { "labelIds": labelidsList(config.labelIds) } : {},
370
+ ...config.labelFilterBehavior !== void 0 && config.labelFilterBehavior !== null && config.labelFilterBehavior !== "" ? { "labelFilterBehavior": String(config.labelFilterBehavior) } : {}
371
+ }
372
+ }
373
+ });
374
+ }
375
+ function labelidsList(value) {
376
+ const items = Array.isArray(value) ? value.map(String) : typeof value === "string" ? value.split(",") : [];
377
+ return items.map((item) => item.trim()).filter(Boolean);
378
+ }
379
+ var MESSAGE_GET_OPERATION = "message_get";
380
+ async function gmailInboxMessageGet(options) {
381
+ const config = options.config ?? {};
382
+ if (config.userId === void 0 || config.userId === null || config.userId === "") {
383
+ throw new Error(`message_get: "userId" is required (Mailbox).`);
384
+ }
385
+ if (config.id === void 0 || config.id === null || config.id === "") {
386
+ throw new Error(`message_get: "id" is required (Message ID).`);
387
+ }
388
+ return callConnector(GMAIL_INBOX, {
389
+ operation: MESSAGE_GET_OPERATION,
390
+ config,
391
+ input: options.input,
392
+ ...options.credentials === void 0 ? {} : { credentials: options.credentials },
393
+ ...options.mode === void 0 ? {} : { mode: options.mode },
394
+ ...options.connectionId === void 0 ? {} : { connectionId: options.connectionId },
395
+ ...options.attempts === void 0 ? {} : { attempts: options.attempts },
396
+ ...options.transport === void 0 ? {} : { transport: options.transport },
397
+ request: {
398
+ method: "GET",
399
+ path: `/gmail/v1/users/${encodeURIComponent(String(config.userId))}/messages/${encodeURIComponent(String(config.id))}`,
400
+ query: {
401
+ "format": "full"
402
+ }
403
+ }
404
+ });
405
+ }
406
+ var MESSAGE_LIST_OPERATION = "message_list";
407
+ async function gmailInboxMessageList(options) {
408
+ const config = options.config ?? {};
409
+ if (config.userId === void 0 || config.userId === null || config.userId === "") {
410
+ throw new Error(`message_list: "userId" is required (Mailbox).`);
411
+ }
412
+ {
413
+ const n = Number(config.maxResults);
414
+ const given = config.maxResults !== void 0 && config.maxResults !== null && config.maxResults !== "";
415
+ if (given && !(Number.isInteger(n) && n >= 1 && n <= 500)) {
416
+ throw new Error(
417
+ `message_list: "maxResults" must be a integer, got ${JSON.stringify(config.maxResults)}.`
418
+ );
419
+ }
420
+ }
421
+ return callConnector(GMAIL_INBOX, {
422
+ operation: MESSAGE_LIST_OPERATION,
423
+ config,
424
+ input: options.input,
425
+ ...options.credentials === void 0 ? {} : { credentials: options.credentials },
426
+ ...options.mode === void 0 ? {} : { mode: options.mode },
427
+ ...options.connectionId === void 0 ? {} : { connectionId: options.connectionId },
428
+ ...options.attempts === void 0 ? {} : { attempts: options.attempts },
429
+ ...options.transport === void 0 ? {} : { transport: options.transport },
430
+ request: {
431
+ method: "GET",
432
+ path: `/gmail/v1/users/${encodeURIComponent(String(config.userId))}/messages`,
433
+ query: {
434
+ ...config.q !== void 0 && config.q !== null && config.q !== "" ? { "q": String(config.q) } : {},
435
+ ...config.labelId !== void 0 && config.labelId !== null && config.labelId !== "" ? { "labelIds": String(config.labelId) } : {},
436
+ ...config.includeSpamTrash !== void 0 && config.includeSpamTrash !== null && config.includeSpamTrash !== "" ? { "includeSpamTrash": Boolean(config.includeSpamTrash) } : {},
437
+ "maxResults": config.maxResults !== void 0 && config.maxResults !== null && config.maxResults !== "" ? Math.trunc(Number(config.maxResults)) : 100,
438
+ ...config.pageToken !== void 0 && config.pageToken !== null && config.pageToken !== "" ? { "pageToken": String(config.pageToken) } : {}
439
+ }
440
+ }
441
+ });
442
+ }
443
+ var THREAD_GET_OPERATION = "thread_get";
444
+ async function gmailInboxThreadGet(options) {
445
+ const config = options.config ?? {};
446
+ if (config.userId === void 0 || config.userId === null || config.userId === "") {
447
+ throw new Error(`thread_get: "userId" is required (Mailbox).`);
448
+ }
449
+ if (config.id === void 0 || config.id === null || config.id === "") {
450
+ throw new Error(`thread_get: "id" is required (Thread ID).`);
451
+ }
452
+ return callConnector(GMAIL_INBOX, {
453
+ operation: THREAD_GET_OPERATION,
454
+ config,
455
+ input: options.input,
456
+ ...options.credentials === void 0 ? {} : { credentials: options.credentials },
457
+ ...options.mode === void 0 ? {} : { mode: options.mode },
458
+ ...options.connectionId === void 0 ? {} : { connectionId: options.connectionId },
459
+ ...options.attempts === void 0 ? {} : { attempts: options.attempts },
460
+ ...options.transport === void 0 ? {} : { transport: options.transport },
461
+ request: {
462
+ method: "GET",
463
+ path: `/gmail/v1/users/${encodeURIComponent(String(config.userId))}/threads/${encodeURIComponent(String(config.id))}`,
464
+ query: {
465
+ "format": "full"
466
+ }
467
+ }
468
+ });
469
+ }
470
+ var GMAIL_INBOX_MESSAGE_RECEIVED_OIDC_SCHEME = {
471
+ kind: "oidc-jwt",
472
+ header: "Authorization",
473
+ issuers: ["https://accounts.google.com", "accounts.google.com"],
474
+ principalClaim: "email",
475
+ verifiedClaim: "email_verified"
476
+ };
477
+ var GMAIL_INBOX_MESSAGE_RECEIVED_LEASE = {
478
+ expiresAtFrom: "expiration",
479
+ expiresAtUnit: "epoch-ms",
480
+ renewBeforeSeconds: 86400,
481
+ renewOperation: "mailbox_watch"
482
+ };
483
+ var GMAIL_INBOX_MESSAGE_RECEIVED_SUBSCRIPTION = { create: "mailbox_watch", renew: "mailbox_watch", stop: "mailbox_stop" };
484
+ var GMAIL_INBOX_MESSAGE_RECEIVED_DECODE = {
485
+ kind: "pubsub-push"
486
+ };
487
+ var GMAIL_INBOX_MESSAGE_RECEIVED = {
488
+ service: "gmail_inbox",
489
+ operation: "message_received",
490
+ delivery: "subscription",
491
+ setup: "In YOUR app's Google Cloud project (the one owning the OAuth client): create a Pub/Sub topic, grant publish on it to gmail-api-push@system.gserviceaccount.com, and create a PUSH subscription to the route your host mounts for this trigger, with authentication on (a service account you choose; the endpoint URL as the audience). Put that audience and service account on the connection as pushAudience and pushServiceAccount, and fetch Google's signing keys for verification. Then call mailbox_watch with the topic for each mailbox and store its historyId as the trigger's cursor (startHistoryId); renew the watch daily. ONE topic serves every mailbox: route each push by its emailAddress, and only after the token verifies, to a mailbox you watched; drop an unmapped address with a 2xx. Persist cursor.next when a run succeeds. On cursor-expired or cursor-overflow, watch again for a fresh historyId FIRST, then list with message_list using the resyncQuery, and match it against the mail you already handled. This trigger never changes read state.",
492
+ subscriptionTtl: 604800,
493
+ lease: GMAIL_INBOX_MESSAGE_RECEIVED_LEASE,
494
+ verification: {
495
+ scheme: GMAIL_INBOX_MESSAGE_RECEIVED_OIDC_SCHEME
496
+ },
497
+ decode: GMAIL_INBOX_MESSAGE_RECEIVED_DECODE,
498
+ route: { by: "emailAddress", path: "data.emailAddress" },
499
+ faker: gmailInboxFaker
500
+ };
501
+ var GMAIL_INBOX_MESSAGE_RECEIVED_CURSOR = {
502
+ param: "startHistoryId",
503
+ advanceFrom: "historyId",
504
+ page: { param: "pageToken", from: "nextPageToken", collect: "history", maxPages: 20 },
505
+ expiredStatus: 404
506
+ };
507
+ async function readGmailInboxMessageReceivedAfter(event, config, options = {}) {
508
+ const stored = config.startHistoryId ?? (config.mode === "fake" ? "1" : void 0);
509
+ const read = await readWithCursor(
510
+ GMAIL_INBOX_MESSAGE_RECEIVED_CURSOR,
511
+ stored,
512
+ async (params) => {
513
+ const result = await gmailInboxHistoryList({
514
+ config: {
515
+ userId: "me",
516
+ maxResults: 100,
517
+ ...config.connection === void 0 || config.connection === null ? {} : { connection: config.connection },
518
+ ...config.mode === void 0 || config.mode === null ? {} : { mode: config.mode },
519
+ labelId: config.labelId ?? "INBOX",
520
+ historyTypes: config.historyTypes ?? "messageAdded",
521
+ ...params
522
+ },
523
+ input: event,
524
+ ...options.credentials === void 0 ? {} : { credentials: options.credentials },
525
+ ...options.transport === void 0 ? {} : { transport: options.transport }
526
+ });
527
+ return result.data;
528
+ },
529
+ { resyncQuery: `newer_than:${String(config.resyncWindowDays ?? 7)}d` }
530
+ );
531
+ return {
532
+ ...event !== null && typeof event === "object" ? event : {},
533
+ history: read.data,
534
+ cursor: read.cursor
535
+ };
536
+ }
537
+ function verifyGmailInboxDelivery(delivery, expected, now) {
538
+ return verifyDelivery(GMAIL_INBOX_MESSAGE_RECEIVED, delivery, void 0, now, {
539
+ keys: expected.keys,
540
+ audience: expected.pushAudience ?? null,
541
+ principal: expected.pushServiceAccount ?? null
542
+ });
543
+ }
544
+ function decodeGmailInboxDelivery(raw) {
545
+ return decodeDelivery(GMAIL_INBOX_MESSAGE_RECEIVED_DECODE, raw);
546
+ }
547
+
548
+ export { CONNECTOR_API_VERSION, GMAIL_INBOX, GMAIL_INBOX_BASE_URLS, GMAIL_INBOX_MESSAGE_RECEIVED, GMAIL_INBOX_MESSAGE_RECEIVED_CURSOR, GMAIL_INBOX_MESSAGE_RECEIVED_DECODE, GMAIL_INBOX_MESSAGE_RECEIVED_LEASE, GMAIL_INBOX_MESSAGE_RECEIVED_OIDC_SCHEME, GMAIL_INBOX_MESSAGE_RECEIVED_SUBSCRIPTION, GMAIL_INBOX_REQUIRES, HISTORY_LIST_OPERATION, LABEL_LIST_OPERATION, MAILBOX_STOP_OPERATION, MAILBOX_WATCH_OPERATION, MESSAGE_GET_OPERATION, MESSAGE_LIST_OPERATION, THREAD_GET_OPERATION, decodeGmailInboxDelivery, gmailInboxAuthorize, gmailInboxFaker, gmailInboxHistoryList, gmailInboxLabelList, gmailInboxMailboxStop, gmailInboxMailboxWatch, gmailInboxMessageGet, gmailInboxMessageList, gmailInboxThreadGet, readGmailInboxMessageReceivedAfter, verifyGmailInboxDelivery };
549
+ //# sourceMappingURL=chunk-2PSRTESV.js.map
550
+ //# sourceMappingURL=chunk-2PSRTESV.js.map