Skip to content

Commit 9891933

Browse files
committed
pagination
1 parent c0ea92b commit 9891933

File tree

8 files changed

+151
-47
lines changed

8 files changed

+151
-47
lines changed

components/microsoft_outlook/actions/create-contact/create-contact.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default {
55
key: "microsoft_outlook-create-contact",
66
version: "0.0.11",
77
name: "Create Contact",
8-
description: "Add a contact to the root Contacts folder, [See the docs](https://docs.microsoft.com/en-us/graph/api/user-post-contacts)",
8+
description: "Add a contact to the root Contacts folder, [See the documentation](https://docs.microsoft.com/en-us/graph/api/user-post-contacts)",
99
props: {
1010
microsoftOutlook,
1111
givenName: {

components/microsoft_outlook/actions/create-draft-email/create-draft-email.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default {
55
key: "microsoft_outlook-create-draft-email",
66
version: "0.0.11",
77
name: "Create Draft Email",
8-
description: "Create a draft email, [See the docs](https://docs.microsoft.com/en-us/graph/api/user-post-messages)",
8+
description: "Create a draft email, [See the documentation](https://docs.microsoft.com/en-us/graph/api/user-post-messages)",
99
props: {
1010
microsoftOutlook,
1111
recipients: {

components/microsoft_outlook/actions/find-contacts/find-contacts.mjs

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,48 @@ export default {
55
key: "microsoft_outlook-find-contacts",
66
version: "0.0.11",
77
name: "Find Contacts",
8-
description: "Finds contacts with given search string",
8+
description: "Finds contacts with the given search string. [See the documentation](https://docs.microsoft.com/en-us/graph/api/user-list-contacts)",
99
props: {
1010
microsoftOutlook,
1111
searchString: {
1212
label: "Search string",
1313
description: "Provide email address, given name, surname or display name (case sensitive)",
1414
type: "string",
1515
},
16+
maxResults: {
17+
propDefinition: [
18+
microsoftOutlook,
19+
"maxResults",
20+
],
21+
},
1622
},
1723
async run({ $ }) {
18-
const contactList = await this.microsoftOutlook.listContacts({
19-
$,
20-
});
21-
const relatedContacts = contactList.value.filter((c) => {
22-
return c.displayName.includes(this.searchString) ||
23-
c.givenName.includes(this.searchString) ||
24-
c.surname.includes(this.searchString) ||
25-
c.emailAddresses.find((e) =>
26-
e.address == this.searchString ||
27-
e.name.includes(this.searchString));
24+
const contacts = this.microsoftOutlook.paginate({
25+
fn: this.microsoftOutlook.listContacts,
26+
args: {
27+
$,
28+
},
2829
});
30+
31+
const relatedContacts = [];
32+
for await (const contact of contacts) {
33+
if (
34+
contact.displayName.includes(this.searchString) ||
35+
contact.givenName.includes(this.searchString) ||
36+
contact.surname.includes(this.searchString) ||
37+
contact.emailAddresses.find(
38+
(e) => e.address == this.searchString || e.name.includes(this.searchString),
39+
)
40+
) {
41+
relatedContacts.push(contact);
42+
if (this.maxResults && relatedContacts.length >= this.maxResults) {
43+
break;
44+
}
45+
}
46+
}
47+
2948
// eslint-disable-next-line multiline-ternary
30-
$.export("$summary", `${relatedContacts.length} contact${relatedContacts.length != 1 ? "s" : ""} has been filtered.`);
49+
$.export("$summary", `${relatedContacts.length} matching contact${relatedContacts.length != 1 ? "s" : ""} found`);
3150
return relatedContacts;
3251
},
3352
};

components/microsoft_outlook/actions/find-email/find-email.mjs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,32 @@ export default {
1515
optional: true,
1616
},
1717
maxResults: {
18-
type: "integer",
19-
label: "Max Results",
20-
description: "The maximum number of results to return",
21-
optional: true,
18+
propDefinition: [
19+
microsoftOutlook,
20+
"maxResults",
21+
],
2222
},
2323
},
2424
async run({ $ }) {
25-
const { value } = await this.microsoftOutlook.listMessages({
26-
$,
27-
params: {
28-
"$filter": this.filter,
29-
"$top": this.maxResults,
25+
const items = this.microsoftOutlook.paginate({
26+
fn: this.microsoftOutlook.listMessages,
27+
args: {
28+
$,
29+
params: {
30+
"$filter": this.filter,
31+
},
3032
},
33+
max: this.maxResults,
3134
});
32-
$.export("$summary", `Successfully retrieved ${value.length} message${value.length != 1
35+
36+
const emails = [];
37+
for await (const item of items) {
38+
emails.push(item);
39+
}
40+
41+
$.export("$summary", `Successfully retrieved ${emails.length} message${emails.length != 1
3342
? "s"
3443
: ""}.`);
35-
return value;
44+
return emails;
3645
},
3746
};

components/microsoft_outlook/actions/list-contacts/list-contacts.mjs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,39 @@ export default {
55
key: "microsoft_outlook-list-contacts",
66
version: "0.0.11",
77
name: "List Contacts",
8-
description: "Get a contact collection from the default contacts folder, [See the docs](https://docs.microsoft.com/en-us/graph/api/user-list-contacts)",
8+
description: "Get a contact collection from the default contacts folder, [See the documentation](https://docs.microsoft.com/en-us/graph/api/user-list-contacts)",
99
props: {
1010
microsoftOutlook,
1111
filterAddress: {
12-
label: "Email adress",
12+
label: "Email Address",
1313
description: "If this is given, only contacts with the given address will be retrieved.",
1414
type: "string",
1515
optional: true,
1616
},
17+
maxResults: {
18+
propDefinition: [
19+
microsoftOutlook,
20+
"maxResults",
21+
],
22+
},
1723
},
1824
async run({ $ }) {
19-
const response = await this.microsoftOutlook.listContacts({
20-
$,
21-
filterAddress: this.filterAddress,
25+
const items = this.microsoftOutlook.paginate({
26+
fn: this.microsoftOutlook.listContacts,
27+
args: {
28+
$,
29+
filterAddress: this.filterAddress,
30+
},
31+
max: this.maxResults,
2232
});
33+
34+
const contacts = [];
35+
for await (const item of items) {
36+
contacts.push(item);
37+
}
38+
2339
// eslint-disable-next-line multiline-ternary
24-
$.export("$summary", `${response.value.length} contact${response.value.length != 1 ? "s" : ""} has been retrieved.`);
25-
return response.value;
40+
$.export("$summary", `${contacts.length} contact${contacts.length != 1 ? "s" : ""} retrieved.`);
41+
return contacts;
2642
},
2743
};

components/microsoft_outlook/actions/list-folders/list-folders.mjs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,30 @@ export default {
88
type: "action",
99
props: {
1010
microsoftOutlook,
11+
maxResults: {
12+
propDefinition: [
13+
microsoftOutlook,
14+
"maxResults",
15+
],
16+
},
1117
},
1218
async run({ $ }) {
13-
const { value } = await this.microsoftOutlook.listFolders({
14-
$,
19+
const items = this.microsoftOutlook.paginate({
20+
fn: this.microsoftOutlook.listFolders,
21+
args: {
22+
$,
23+
},
24+
max: this.maxResults,
1525
});
16-
$.export("$summary", `Successfully retrieved ${value.length} folder${value.length != 1
26+
27+
const folders = [];
28+
for await (const item of items) {
29+
folders.push(item);
30+
}
31+
32+
$.export("$summary", `Successfully retrieved ${folders.length} folder${folders.length != 1
1733
? "s"
1834
: ""}.`);
19-
return value;
35+
return folders;
2036
},
2137
};

components/microsoft_outlook/microsoft_outlook.app.mjs

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import fs from "fs";
33
import path from "path";
44
import { encode } from "js-base64";
55
import mime from "mime-types";
6+
const DEFAULT_LIMIT = 50;
67

78
export default {
89
type: "app",
@@ -62,8 +63,14 @@ export default {
6263
label: "Contact",
6364
description: "The contact to be updated",
6465
type: "string",
65-
async options() {
66-
const contactResponse = await this.listContacts();
66+
async options({ page }) {
67+
const limit = DEFAULT_LIMIT;
68+
const contactResponse = await this.listContacts({
69+
params: {
70+
$top: limit,
71+
$skip: limit * page,
72+
},
73+
});
6774
return contactResponse.value.map((co) => ({
6875
label: co.displayName,
6976
value: co.id,
@@ -127,7 +134,7 @@ export default {
127134
label: "Message ID",
128135
description: "The identifier of the message to update",
129136
async options({ page }) {
130-
const limit = 50;
137+
const limit = DEFAULT_LIMIT;
131138
const { value } = await this.listMessages({
132139
params: {
133140
$top: limit,
@@ -147,8 +154,14 @@ export default {
147154
type: "string[]",
148155
label: "Folder IDs to Monitor",
149156
description: "Specify the folder IDs or names in Outlook that you want to monitor for new emails. Leave empty to monitor all folders (excluding \"Sent Items\" and \"Drafts\").",
150-
async options() {
151-
const { value: folders } = await this.listFolders();
157+
async options({ page }) {
158+
const limit = DEFAULT_LIMIT;
159+
const { value: folders } = await this.listFolders({
160+
params: {
161+
$top: limit,
162+
$skip: limit * page,
163+
},
164+
});
152165
return folders?.map(({
153166
id: value, displayName: label,
154167
}) => ({
@@ -157,6 +170,13 @@ export default {
157170
})) || [];
158171
},
159172
},
173+
maxResults: {
174+
type: "integer",
175+
label: "Max Results",
176+
description: "The maximum number of results to return",
177+
default: 100,
178+
optional: true,
179+
},
160180
},
161181
methods: {
162182
_getUrl(path) {
@@ -304,16 +324,15 @@ export default {
304324
filterAddress,
305325
...args
306326
} = {}) {
307-
const paramsContainer = {};
327+
args.params = {
328+
...args?.params,
329+
};
308330
if (filterAddress) {
309-
paramsContainer.params = {
310-
"$filter": `emailAddresses/any(a:a/address eq '${filterAddress}')`,
311-
};
331+
args.params["$filter"] = `emailAddresses/any(a:a/address eq '${filterAddress}')`;
312332
}
313333
return await this._makeRequest({
314334
method: "GET",
315335
path: "/me/contacts",
316-
...paramsContainer,
317336
...args,
318337
});
319338
},
@@ -384,5 +403,30 @@ export default {
384403
...args,
385404
});
386405
},
406+
async *paginate({
407+
fn, args = {}, max,
408+
}) {
409+
const limit = DEFAULT_LIMIT;
410+
args = {
411+
...args,
412+
params: {
413+
...args?.params,
414+
$top: limit,
415+
$skip: 0,
416+
},
417+
};
418+
let total, count = 0;
419+
do {
420+
const { value } = await fn(args); console.log(args.params);
421+
for (const item of value) {
422+
yield item;
423+
if (max && ++count >= max) {
424+
return;
425+
}
426+
}
427+
total = value?.length;
428+
args.params["$skip"] += limit;
429+
} while (total);
430+
},
387431
},
388432
};

components/microsoft_outlook/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/microsoft_outlook",
3-
"version": "1.4.0",
3+
"version": "1.4.1",
44
"description": "Pipedream Microsoft Outlook Components",
55
"main": "microsoft_outlook.app.mjs",
66
"keywords": [

0 commit comments

Comments
 (0)