@openfn/language-gmail 1.0.1 → 1.1.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 +14 -2
- package/dist/index.cjs +42 -37
- package/dist/index.js +42 -37
- package/package.json +1 -1
- package/types/Adaptor.d.ts +1 -0
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
|
-
|
|
135
|
+
const maxResults = 200;
|
|
136
|
+
getContentsFromMessages({ query, contents, maxResults });
|
|
125
137
|
```
|
|
126
138
|
|
|
127
139
|
```js
|
package/dist/index.cjs
CHANGED
|
@@ -288,60 +288,65 @@ function getContentsFromMessages(options) {
|
|
|
288
288
|
const [resolvedOptions] = (0, import_util.expandReferences)(state, options);
|
|
289
289
|
const defaultOptions = {
|
|
290
290
|
contents: ["from", "date", "subject"],
|
|
291
|
-
userId: "me"
|
|
291
|
+
userId: "me",
|
|
292
|
+
maxResults: 1e3
|
|
292
293
|
};
|
|
293
294
|
const opts = {
|
|
294
295
|
userId: resolvedOptions.email ?? defaultOptions.userId,
|
|
295
296
|
query: resolvedOptions.query,
|
|
296
|
-
processedIds: resolvedOptions.processedIds
|
|
297
|
+
processedIds: resolvedOptions.processedIds,
|
|
298
|
+
maxResults: resolvedOptions.maxResults ?? defaultOptions.maxResults
|
|
297
299
|
};
|
|
298
300
|
const contentIndicators = getContentIndicators(
|
|
299
301
|
defaultOptions.contents,
|
|
300
302
|
resolvedOptions.contents
|
|
301
303
|
);
|
|
302
304
|
const contents = [];
|
|
303
|
-
const
|
|
305
|
+
const newIds = [];
|
|
304
306
|
const previousIds = Array.isArray(opts.processedIds) ? opts.processedIds : [];
|
|
305
307
|
let nextPageToken = null;
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
const
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
if (messageContent && content[contentIndicator.name]) {
|
|
332
|
-
throw new Error(
|
|
333
|
-
`Duplicate content name detected: ${contentIndicator.name}`
|
|
308
|
+
doNextPageToken:
|
|
309
|
+
do {
|
|
310
|
+
const messagesResult = await getMessagesResult(
|
|
311
|
+
opts.userId,
|
|
312
|
+
opts.query,
|
|
313
|
+
nextPageToken
|
|
314
|
+
);
|
|
315
|
+
if (!((_a = messagesResult.messages) == null ? void 0 : _a.length)) {
|
|
316
|
+
console.log("No messages found.");
|
|
317
|
+
break;
|
|
318
|
+
}
|
|
319
|
+
nextPageToken = messagesResult.nextPageToken;
|
|
320
|
+
for (const message of messagesResult.messages) {
|
|
321
|
+
newIds.push(message.id);
|
|
322
|
+
if (previousIds.includes(message.id)) {
|
|
323
|
+
continue;
|
|
324
|
+
}
|
|
325
|
+
const content = {
|
|
326
|
+
messageId: message.id
|
|
327
|
+
};
|
|
328
|
+
const messageResult = await getMessageResult(opts.userId, message.id);
|
|
329
|
+
for (const contentIndicator of contentIndicators) {
|
|
330
|
+
const messageContent = await getMessageContent(
|
|
331
|
+
messageResult,
|
|
332
|
+
contentIndicator
|
|
334
333
|
);
|
|
334
|
+
if (messageContent && content[contentIndicator.name]) {
|
|
335
|
+
throw new Error(
|
|
336
|
+
`Duplicate content name detected: ${contentIndicator.name}`
|
|
337
|
+
);
|
|
338
|
+
}
|
|
339
|
+
content[_b = contentIndicator.name] ?? (content[_b] = messageContent);
|
|
340
|
+
}
|
|
341
|
+
contents.push(content);
|
|
342
|
+
if (contents.length >= opts.maxResults) {
|
|
343
|
+
break doNextPageToken;
|
|
335
344
|
}
|
|
336
|
-
content[_b = contentIndicator.name] ?? (content[_b] = messageContent);
|
|
337
345
|
}
|
|
338
|
-
|
|
339
|
-
}
|
|
340
|
-
currentIds.push(...currentPageIds);
|
|
341
|
-
} while (nextPageToken);
|
|
346
|
+
} while (nextPageToken);
|
|
342
347
|
const nextState = {
|
|
343
348
|
...(0, import_language_common.composeNextState)(state, contents),
|
|
344
|
-
processedIds:
|
|
349
|
+
processedIds: newIds
|
|
345
350
|
};
|
|
346
351
|
return nextState;
|
|
347
352
|
};
|
package/dist/index.js
CHANGED
|
@@ -268,60 +268,65 @@ function getContentsFromMessages(options) {
|
|
|
268
268
|
const [resolvedOptions] = expandReferences(state, options);
|
|
269
269
|
const defaultOptions = {
|
|
270
270
|
contents: ["from", "date", "subject"],
|
|
271
|
-
userId: "me"
|
|
271
|
+
userId: "me",
|
|
272
|
+
maxResults: 1e3
|
|
272
273
|
};
|
|
273
274
|
const opts = {
|
|
274
275
|
userId: resolvedOptions.email ?? defaultOptions.userId,
|
|
275
276
|
query: resolvedOptions.query,
|
|
276
|
-
processedIds: resolvedOptions.processedIds
|
|
277
|
+
processedIds: resolvedOptions.processedIds,
|
|
278
|
+
maxResults: resolvedOptions.maxResults ?? defaultOptions.maxResults
|
|
277
279
|
};
|
|
278
280
|
const contentIndicators = getContentIndicators(
|
|
279
281
|
defaultOptions.contents,
|
|
280
282
|
resolvedOptions.contents
|
|
281
283
|
);
|
|
282
284
|
const contents = [];
|
|
283
|
-
const
|
|
285
|
+
const newIds = [];
|
|
284
286
|
const previousIds = Array.isArray(opts.processedIds) ? opts.processedIds : [];
|
|
285
287
|
let nextPageToken = null;
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
const
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
if (messageContent && content[contentIndicator.name]) {
|
|
312
|
-
throw new Error(
|
|
313
|
-
`Duplicate content name detected: ${contentIndicator.name}`
|
|
288
|
+
doNextPageToken:
|
|
289
|
+
do {
|
|
290
|
+
const messagesResult = await getMessagesResult(
|
|
291
|
+
opts.userId,
|
|
292
|
+
opts.query,
|
|
293
|
+
nextPageToken
|
|
294
|
+
);
|
|
295
|
+
if (!((_a = messagesResult.messages) == null ? void 0 : _a.length)) {
|
|
296
|
+
console.log("No messages found.");
|
|
297
|
+
break;
|
|
298
|
+
}
|
|
299
|
+
nextPageToken = messagesResult.nextPageToken;
|
|
300
|
+
for (const message of messagesResult.messages) {
|
|
301
|
+
newIds.push(message.id);
|
|
302
|
+
if (previousIds.includes(message.id)) {
|
|
303
|
+
continue;
|
|
304
|
+
}
|
|
305
|
+
const content = {
|
|
306
|
+
messageId: message.id
|
|
307
|
+
};
|
|
308
|
+
const messageResult = await getMessageResult(opts.userId, message.id);
|
|
309
|
+
for (const contentIndicator of contentIndicators) {
|
|
310
|
+
const messageContent = await getMessageContent(
|
|
311
|
+
messageResult,
|
|
312
|
+
contentIndicator
|
|
314
313
|
);
|
|
314
|
+
if (messageContent && content[contentIndicator.name]) {
|
|
315
|
+
throw new Error(
|
|
316
|
+
`Duplicate content name detected: ${contentIndicator.name}`
|
|
317
|
+
);
|
|
318
|
+
}
|
|
319
|
+
content[_b = contentIndicator.name] ?? (content[_b] = messageContent);
|
|
320
|
+
}
|
|
321
|
+
contents.push(content);
|
|
322
|
+
if (contents.length >= opts.maxResults) {
|
|
323
|
+
break doNextPageToken;
|
|
315
324
|
}
|
|
316
|
-
content[_b = contentIndicator.name] ?? (content[_b] = messageContent);
|
|
317
325
|
}
|
|
318
|
-
|
|
319
|
-
}
|
|
320
|
-
currentIds.push(...currentPageIds);
|
|
321
|
-
} while (nextPageToken);
|
|
326
|
+
} while (nextPageToken);
|
|
322
327
|
const nextState = {
|
|
323
328
|
...composeNextState(state, contents),
|
|
324
|
-
processedIds:
|
|
329
|
+
processedIds: newIds
|
|
325
330
|
};
|
|
326
331
|
return nextState;
|
|
327
332
|
};
|
package/package.json
CHANGED
package/types/Adaptor.d.ts
CHANGED
|
@@ -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.
|