@openfn/language-gmail 1.0.1 → 1.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
@@ -10,7 +10,7 @@ A number of options are available to isolated the desired messages and to custom
10
10
 
11
11
  ## Options
12
12
 
13
- Optional parameters include: `contents`, `query`, `email`, `processedIds`
13
+ Optional parameters include: `contents`, `query`, `email`, `processedIds`, `maxResults`
14
14
 
15
15
  ### options.contents
16
16
 
@@ -116,12 +116,24 @@ options.processedIds = [
116
116
  ];
117
117
  ```
118
118
 
119
+ ### options.maxResults
120
+
121
+ To prevent inadventant massive retrieval of messages, you can limit the number of results returned. The default value is 1000.
122
+
123
+ This works in conjuction with the `options.processedIds` parameter. For example:
124
+
125
+ - account contains messages [1, 2, 3]
126
+ - `options.processedIds = [1];`
127
+ - `options.maxResults = 1;`
128
+ - this will skip message #1 and resulting dataset will contain a single message #2
129
+
119
130
  ## Example jobs
120
131
 
121
132
  ```js
122
133
  const query = 'in:inbox newer_than:2d';
123
134
  const contents = ['body'];
124
- getContentsFromMessages({ query, contents });
135
+ const maxResults = 200;
136
+ getContentsFromMessages({ query, contents, maxResults });
125
137
  ```
126
138
 
127
139
  ```js
package/ast.json CHANGED
@@ -6,107 +6,56 @@
6
6
  "options"
7
7
  ],
8
8
  "docs": {
9
- "description": "Used to isolate the type of content to retrieve from the message.",
9
+ "description": "Downloads contents from messages of a Gmail account.",
10
10
  "tags": [
11
11
  {
12
- "title": "typedef",
12
+ "title": "public",
13
13
  "description": null,
14
- "type": {
15
- "type": "NameExpression",
16
- "name": "Object"
17
- },
18
- "name": "MessageContent"
14
+ "type": null
19
15
  },
20
16
  {
21
- "title": "public",
17
+ "title": "function",
22
18
  "description": null,
23
- "type": null
19
+ "name": null
24
20
  },
25
21
  {
26
- "title": "property",
27
- "description": "Message content type. Valid types: from, date, subject, body, archive, file.",
22
+ "title": "param",
23
+ "description": "Customized options including desired contents and query.",
28
24
  "type": {
29
- "type": "OptionalType",
30
- "expression": {
31
- "type": "NameExpression",
32
- "name": "string"
33
- }
25
+ "type": "NameExpression",
26
+ "name": "Options"
34
27
  },
35
- "name": "type"
28
+ "name": "options"
36
29
  },
37
30
  {
38
- "title": "property",
39
- "description": "A custom description for the content type.",
40
- "type": {
41
- "type": "OptionalType",
42
- "expression": {
43
- "type": "NameExpression",
44
- "name": "string"
45
- }
46
- },
47
- "name": "name"
48
- },
49
- {
50
- "title": "property",
51
- "description": "Identifier to isolate the desired attachment when type is 'archive'.\n Use a regular expression for pattern matching or a string for a literal match. Required if type is 'archive'.",
52
- "type": {
53
- "type": "OptionalType",
54
- "expression": {
55
- "type": "UnionType",
56
- "elements": [
57
- {
58
- "type": "NameExpression",
59
- "name": "RegExp"
60
- },
61
- {
62
- "type": "NameExpression",
63
- "name": "string"
64
- }
65
- ]
66
- }
67
- },
68
- "name": "archive"
69
- },
70
- {
71
- "title": "property",
72
- "description": "Identifier to isolate the desired attachment when type is 'file' or 'archive'.\n Use a regular expression for pattern matching or a string for a literal match. Required if type is 'file' or 'archive'.",
73
- "type": {
74
- "type": "OptionalType",
75
- "expression": {
76
- "type": "UnionType",
77
- "elements": [
78
- {
79
- "type": "NameExpression",
80
- "name": "RegExp"
81
- },
82
- {
83
- "type": "NameExpression",
84
- "name": "string"
85
- }
86
- ]
87
- }
88
- },
89
- "name": "file"
31
+ "title": "state",
32
+ "description": "{Array} data - The returned message objects, of the form `{ messageId, contents } `"
90
33
  },
91
34
  {
92
- "title": "property",
93
- "description": "Maximum number of characters to retrieve from the content.",
35
+ "title": "state",
36
+ "description": "{Array<string>} processedIds - An array of string ids processed by this request"
37
+ },
38
+ {
39
+ "title": "returns",
40
+ "description": null,
94
41
  "type": {
95
- "type": "OptionalType",
96
- "expression": {
97
- "type": "NullableType",
98
- "expression": {
99
- "type": "NameExpression",
100
- "name": "number"
101
- },
102
- "prefix": false
103
- }
104
- },
105
- "name": "maxLength"
42
+ "type": "NameExpression",
43
+ "name": "Operation"
44
+ }
45
+ },
46
+ {
47
+ "title": "example",
48
+ "description": "getContentsFromMessages(\n {\n query: 'subject:my+test+message'\n }\n)",
49
+ "caption": "Get a message with a specific subject"
50
+ },
51
+ {
52
+ "title": "example",
53
+ "description": "getContentsFromMessages(\n {\n query: 'after:15/01/2025',\n contents: [\n 'subject',\n { type: 'file', name: 'metadata', file: 'report.txt'}\n ]\n }\n)",
54
+ "caption": "Get messages after a specific date, with subject and report.txt attachment"
106
55
  }
107
56
  ]
108
57
  },
109
- "valid": false
58
+ "valid": true
110
59
  }
111
60
  ],
112
61
  "exports": [],
package/dist/index.cjs CHANGED
@@ -102,17 +102,11 @@ async function getMessageResult(userId, messageId) {
102
102
  headers: (_b = data == null ? void 0 : data.payload) == null ? void 0 : _b.headers
103
103
  };
104
104
  }
105
- function getContentIndicators(defaultContentRequests, contentRequests) {
106
- const indicators = /* @__PURE__ */ new Map();
107
- const requests = [
108
- ...defaultContentRequests || [],
109
- ...contentRequests || []
110
- ];
111
- for (const request of requests) {
112
- const indicator = getContentIndicator(request);
113
- indicators.set(indicator.type, indicator);
114
- }
115
- return Array.from(indicators.values());
105
+ function getContentIndicators(defaultContentRequests = [], contentRequests = []) {
106
+ const contentIndicators = contentRequests.map(getContentIndicator);
107
+ const contentNames = new Set(contentIndicators.map(({ name }) => name));
108
+ const defaultContentIndicators = defaultContentRequests.map(getContentIndicator).filter(({ name }) => !contentNames.has(name));
109
+ return [...defaultContentIndicators, ...contentIndicators];
116
110
  }
117
111
  function getContentIndicator(contentRequest) {
118
112
  const contentIndicator = typeof contentRequest === "string" ? { type: contentRequest } : { ...contentRequest };
@@ -288,60 +282,65 @@ function getContentsFromMessages(options) {
288
282
  const [resolvedOptions] = (0, import_util.expandReferences)(state, options);
289
283
  const defaultOptions = {
290
284
  contents: ["from", "date", "subject"],
291
- userId: "me"
285
+ userId: "me",
286
+ maxResults: 1e3
292
287
  };
293
288
  const opts = {
294
289
  userId: resolvedOptions.email ?? defaultOptions.userId,
295
290
  query: resolvedOptions.query,
296
- processedIds: resolvedOptions.processedIds
291
+ processedIds: resolvedOptions.processedIds,
292
+ maxResults: resolvedOptions.maxResults ?? defaultOptions.maxResults
297
293
  };
298
294
  const contentIndicators = getContentIndicators(
299
295
  defaultOptions.contents,
300
296
  resolvedOptions.contents
301
297
  );
302
298
  const contents = [];
303
- const currentIds = [];
299
+ const newIds = [];
304
300
  const previousIds = Array.isArray(opts.processedIds) ? opts.processedIds : [];
305
301
  let nextPageToken = null;
306
- do {
307
- const messagesResult = await getMessagesResult(
308
- opts.userId,
309
- opts.query,
310
- nextPageToken
311
- );
312
- if (!((_a = messagesResult.messages) == null ? void 0 : _a.length)) {
313
- console.log("No messages found.");
314
- break;
315
- }
316
- nextPageToken = messagesResult.nextPageToken;
317
- const currentPageIds = messagesResult.messages.map((message) => message.id);
318
- const unprocessedIds = currentPageIds.filter(
319
- (id) => !previousIds.includes(id)
320
- );
321
- for (const messageId of unprocessedIds) {
322
- const content = {
323
- messageId
324
- };
325
- const messageResult = await getMessageResult(opts.userId, messageId);
326
- for (const contentIndicator of contentIndicators) {
327
- const messageContent = await getMessageContent(
328
- messageResult,
329
- contentIndicator
330
- );
331
- if (messageContent && content[contentIndicator.name]) {
332
- throw new Error(
333
- `Duplicate content name detected: ${contentIndicator.name}`
302
+ doNextPageToken:
303
+ do {
304
+ const messagesResult = await getMessagesResult(
305
+ opts.userId,
306
+ opts.query,
307
+ nextPageToken
308
+ );
309
+ if (!((_a = messagesResult.messages) == null ? void 0 : _a.length)) {
310
+ console.log("No messages found.");
311
+ break;
312
+ }
313
+ nextPageToken = messagesResult.nextPageToken;
314
+ for (const message of messagesResult.messages) {
315
+ newIds.push(message.id);
316
+ if (previousIds.includes(message.id)) {
317
+ continue;
318
+ }
319
+ const content = {
320
+ messageId: message.id
321
+ };
322
+ const messageResult = await getMessageResult(opts.userId, message.id);
323
+ for (const contentIndicator of contentIndicators) {
324
+ const messageContent = await getMessageContent(
325
+ messageResult,
326
+ contentIndicator
334
327
  );
328
+ if (messageContent && content[contentIndicator.name]) {
329
+ throw new Error(
330
+ `Duplicate content name detected: ${contentIndicator.name}`
331
+ );
332
+ }
333
+ content[_b = contentIndicator.name] ?? (content[_b] = messageContent);
334
+ }
335
+ contents.push(content);
336
+ if (contents.length >= opts.maxResults) {
337
+ break doNextPageToken;
335
338
  }
336
- content[_b = contentIndicator.name] ?? (content[_b] = messageContent);
337
339
  }
338
- contents.push(content);
339
- }
340
- currentIds.push(...currentPageIds);
341
- } while (nextPageToken);
340
+ } while (nextPageToken);
342
341
  const nextState = {
343
342
  ...(0, import_language_common.composeNextState)(state, contents),
344
- processedIds: currentIds
343
+ processedIds: newIds
345
344
  };
346
345
  return nextState;
347
346
  };
package/dist/index.js CHANGED
@@ -67,17 +67,11 @@ async function getMessageResult(userId, messageId) {
67
67
  headers: (_b = data == null ? void 0 : data.payload) == null ? void 0 : _b.headers
68
68
  };
69
69
  }
70
- function getContentIndicators(defaultContentRequests, contentRequests) {
71
- const indicators = /* @__PURE__ */ new Map();
72
- const requests = [
73
- ...defaultContentRequests || [],
74
- ...contentRequests || []
75
- ];
76
- for (const request of requests) {
77
- const indicator = getContentIndicator(request);
78
- indicators.set(indicator.type, indicator);
79
- }
80
- return Array.from(indicators.values());
70
+ function getContentIndicators(defaultContentRequests = [], contentRequests = []) {
71
+ const contentIndicators = contentRequests.map(getContentIndicator);
72
+ const contentNames = new Set(contentIndicators.map(({ name }) => name));
73
+ const defaultContentIndicators = defaultContentRequests.map(getContentIndicator).filter(({ name }) => !contentNames.has(name));
74
+ return [...defaultContentIndicators, ...contentIndicators];
81
75
  }
82
76
  function getContentIndicator(contentRequest) {
83
77
  const contentIndicator = typeof contentRequest === "string" ? { type: contentRequest } : { ...contentRequest };
@@ -268,60 +262,65 @@ function getContentsFromMessages(options) {
268
262
  const [resolvedOptions] = expandReferences(state, options);
269
263
  const defaultOptions = {
270
264
  contents: ["from", "date", "subject"],
271
- userId: "me"
265
+ userId: "me",
266
+ maxResults: 1e3
272
267
  };
273
268
  const opts = {
274
269
  userId: resolvedOptions.email ?? defaultOptions.userId,
275
270
  query: resolvedOptions.query,
276
- processedIds: resolvedOptions.processedIds
271
+ processedIds: resolvedOptions.processedIds,
272
+ maxResults: resolvedOptions.maxResults ?? defaultOptions.maxResults
277
273
  };
278
274
  const contentIndicators = getContentIndicators(
279
275
  defaultOptions.contents,
280
276
  resolvedOptions.contents
281
277
  );
282
278
  const contents = [];
283
- const currentIds = [];
279
+ const newIds = [];
284
280
  const previousIds = Array.isArray(opts.processedIds) ? opts.processedIds : [];
285
281
  let nextPageToken = null;
286
- do {
287
- const messagesResult = await getMessagesResult(
288
- opts.userId,
289
- opts.query,
290
- nextPageToken
291
- );
292
- if (!((_a = messagesResult.messages) == null ? void 0 : _a.length)) {
293
- console.log("No messages found.");
294
- break;
295
- }
296
- nextPageToken = messagesResult.nextPageToken;
297
- const currentPageIds = messagesResult.messages.map((message) => message.id);
298
- const unprocessedIds = currentPageIds.filter(
299
- (id) => !previousIds.includes(id)
300
- );
301
- for (const messageId of unprocessedIds) {
302
- const content = {
303
- messageId
304
- };
305
- const messageResult = await getMessageResult(opts.userId, messageId);
306
- for (const contentIndicator of contentIndicators) {
307
- const messageContent = await getMessageContent(
308
- messageResult,
309
- contentIndicator
310
- );
311
- if (messageContent && content[contentIndicator.name]) {
312
- throw new Error(
313
- `Duplicate content name detected: ${contentIndicator.name}`
282
+ doNextPageToken:
283
+ do {
284
+ const messagesResult = await getMessagesResult(
285
+ opts.userId,
286
+ opts.query,
287
+ nextPageToken
288
+ );
289
+ if (!((_a = messagesResult.messages) == null ? void 0 : _a.length)) {
290
+ console.log("No messages found.");
291
+ break;
292
+ }
293
+ nextPageToken = messagesResult.nextPageToken;
294
+ for (const message of messagesResult.messages) {
295
+ newIds.push(message.id);
296
+ if (previousIds.includes(message.id)) {
297
+ continue;
298
+ }
299
+ const content = {
300
+ messageId: message.id
301
+ };
302
+ const messageResult = await getMessageResult(opts.userId, message.id);
303
+ for (const contentIndicator of contentIndicators) {
304
+ const messageContent = await getMessageContent(
305
+ messageResult,
306
+ contentIndicator
314
307
  );
308
+ if (messageContent && content[contentIndicator.name]) {
309
+ throw new Error(
310
+ `Duplicate content name detected: ${contentIndicator.name}`
311
+ );
312
+ }
313
+ content[_b = contentIndicator.name] ?? (content[_b] = messageContent);
314
+ }
315
+ contents.push(content);
316
+ if (contents.length >= opts.maxResults) {
317
+ break doNextPageToken;
315
318
  }
316
- content[_b = contentIndicator.name] ?? (content[_b] = messageContent);
317
319
  }
318
- contents.push(content);
319
- }
320
- currentIds.push(...currentPageIds);
321
- } while (nextPageToken);
320
+ } while (nextPageToken);
322
321
  const nextState = {
323
322
  ...composeNextState(state, contents),
324
- processedIds: currentIds
323
+ processedIds: newIds
325
324
  };
326
325
  return nextState;
327
326
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openfn/language-gmail",
3
- "version": "1.0.1",
3
+ "version": "1.1.1",
4
4
  "description": "OpenFn gmail adaptor",
5
5
  "type": "module",
6
6
  "exports": {
@@ -19,6 +19,7 @@
19
19
  * An array of strings or MessageContent objects used to specify which parts of the message to retrieve.
20
20
  * @property {Array<string>} [processedIds] - Ignore message ids which have already been processed.
21
21
  * @property {string?} [email] - The user account to retrieve messages from. Defaults to the authenticated user.
22
+ * @property {int?} [maxResults] - Maximum number of messages to process per request. Default is 1000.
22
23
  */
23
24
  /**
24
25
  * Downloads contents from messages of a Gmail account.
package/types/Utils.d.ts CHANGED
@@ -8,7 +8,7 @@ export function getMessageResult(userId: any, messageId: any): Promise<{
8
8
  parts: any;
9
9
  headers: any;
10
10
  }>;
11
- export function getContentIndicators(defaultContentRequests: any, contentRequests: any): any[];
11
+ export function getContentIndicators(defaultContentRequests?: any[], contentRequests?: any[]): any[];
12
12
  export function getMessageContent(message: any, desiredContent: any): Promise<any>;
13
13
  export function createConnection(state: any): any;
14
14
  export function removeConnection(state: any): any;