Skip to content

Commit 7dc03a2

Browse files
authored
New Components - smashsend (#17511)
* new components * pnpm-lock.yaml * fix generateMeta
1 parent 6a12b63 commit 7dc03a2

File tree

19 files changed

+665
-7
lines changed

19 files changed

+665
-7
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import smashsend from "../../smashsend.app.mjs";
2+
import { parseObject } from "../../common/utils.mjs";
3+
4+
export default {
5+
key: "smashsend-create-or-update-contact",
6+
name: "Create or Update Contact",
7+
description: "Create a new contact or update an existing contact. [See the documentation](https://smashsend.com/docs/api/contacts)",
8+
version: "0.0.1",
9+
type: "action",
10+
props: {
11+
smashsend,
12+
email: {
13+
type: "string",
14+
label: "Email",
15+
description: "The email address of the contact. If the contact already exists, this will update the contact.",
16+
},
17+
firstName: {
18+
type: "string",
19+
label: "First Name",
20+
description: "The first name of the contact",
21+
optional: true,
22+
},
23+
lastName: {
24+
type: "string",
25+
label: "Last Name",
26+
description: "The last name of the contact",
27+
optional: true,
28+
},
29+
phone: {
30+
type: "string",
31+
label: "Phone",
32+
description: "The phone number of the contact",
33+
optional: true,
34+
},
35+
avatarUrl: {
36+
type: "string",
37+
label: "Avatar URL",
38+
description: "The URL of the contact's avatar",
39+
optional: true,
40+
},
41+
countryCode: {
42+
type: "string",
43+
label: "Country Code",
44+
description: "Two-letter ISO country code like `US`",
45+
optional: true,
46+
},
47+
properties: {
48+
type: "object",
49+
label: "Properties",
50+
description: "Additional properties of the contact. Properties must exist in your workspace.",
51+
optional: true,
52+
},
53+
},
54+
async run({ $ }) {
55+
const { contact } = await this.smashsend.createContact({
56+
$,
57+
data: {
58+
properties: {
59+
email: this.email,
60+
firstName: this.firstName,
61+
lastName: this.lastName,
62+
phone: this.phone,
63+
avatarUrl: this.avatarUrl,
64+
countryCode: this.countryCode,
65+
...parseObject(this.properties),
66+
},
67+
},
68+
});
69+
if (contact?.id) {
70+
$.export("$summary", `Successfully ${contact.updatedAt
71+
? "updated"
72+
: "created"} contact ${contact.id}`);
73+
}
74+
return contact;
75+
},
76+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import smashsend from "../../smashsend.app.mjs";
2+
3+
export default {
4+
key: "smashsend-delete-contact",
5+
name: "Delete Contact",
6+
description: "Delete a contact. [See the documentation](https://smashsend.com/docs/api/contacts)",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
smashsend,
11+
contactId: {
12+
propDefinition: [
13+
smashsend,
14+
"contactId",
15+
],
16+
},
17+
},
18+
async run({ $ }) {
19+
const response = await this.smashsend.deleteContact({
20+
$,
21+
contactId: this.contactId,
22+
});
23+
$.export("$summary", `Successfully deleted contact ${this.contactId}`);
24+
return response;
25+
},
26+
};
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import smashsend from "../../smashsend.app.mjs";
2+
3+
export default {
4+
key: "smashsend-list-contacts",
5+
name: "List Contacts",
6+
description: "List all contacts. [See the documentation](https://smashsend.com/docs/api/contacts)",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
smashsend,
11+
sort: {
12+
type: "string",
13+
label: "Sort",
14+
description: "Sort order: “createdAt.desc” or “createdAt.asc” (default: “createdAt.desc”)",
15+
options: [
16+
"createdAt.desc",
17+
"createdAt.asc",
18+
],
19+
optional: true,
20+
},
21+
search: {
22+
type: "string",
23+
label: "Search",
24+
description: "Search for contacts by name, email, or phone number",
25+
optional: true,
26+
},
27+
status: {
28+
type: "string",
29+
label: "Status",
30+
description: "Filter contacts by status",
31+
options: [
32+
"SUBSCRIBED",
33+
"UNSUBSCRIBED",
34+
"BANNED",
35+
],
36+
optional: true,
37+
},
38+
maxResults: {
39+
type: "integer",
40+
label: "Max Results",
41+
description: "The maximum number of results to return",
42+
default: 100,
43+
optional: true,
44+
},
45+
},
46+
async run({ $ }) {
47+
const contacts = await this.smashsend.getPaginatedResources({
48+
fn: this.smashsend.listContacts,
49+
params: {
50+
sort: this.sort,
51+
search: this.search,
52+
status: this.status,
53+
},
54+
resourceKey: "contacts",
55+
max: this.maxResults,
56+
});
57+
$.export("$summary", `Successfully fetched ${contacts.length} contact${contacts.length === 1
58+
? ""
59+
: "s"}`);
60+
return contacts;
61+
},
62+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import smashsend from "../../smashsend.app.mjs";
2+
3+
export default {
4+
key: "smashsend-search-contacts",
5+
name: "Search Contacts",
6+
description: "Search for contacts by email address. [See the documentation](https://smashsend.com/docs/api/contacts)",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
smashsend,
11+
email: {
12+
type: "string",
13+
label: "Email",
14+
description: "The email address to search for",
15+
},
16+
},
17+
async run({ $ }) {
18+
const { contact } = await this.smashsend.searchContacts({
19+
$,
20+
params: {
21+
email: this.email,
22+
},
23+
});
24+
if (contact?.id) {
25+
$.export("$summary", `Successfully fetched contact ${contact.id}`);
26+
}
27+
return contact;
28+
},
29+
};

components/smashsend/common/utils.mjs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
export const parseObject = (obj) => {
2+
if (!obj) {
3+
return {};
4+
}
5+
if (typeof obj === "string") {
6+
try {
7+
return JSON.parse(obj);
8+
} catch {
9+
return obj;
10+
}
11+
}
12+
if (Array.isArray(obj)) {
13+
return obj.map(parseObject);
14+
}
15+
if (typeof obj === "object") {
16+
return Object.fromEntries(Object.entries(obj).map(([
17+
key,
18+
value,
19+
]) => [
20+
key,
21+
parseObject(value),
22+
]));
23+
}
24+
return obj;
25+
};

components/smashsend/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/smashsend",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "Pipedream SmashSend Components",
55
"main": "smashsend.app.mjs",
66
"keywords": [
@@ -11,5 +11,8 @@
1111
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
1212
"publishConfig": {
1313
"access": "public"
14+
},
15+
"dependencies": {
16+
"@pipedream/platform": "^3.1.0"
1417
}
15-
}
18+
}
Lines changed: 125 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,132 @@
1+
import { axios } from "@pipedream/platform";
2+
13
export default {
24
type: "app",
35
app: "smashsend",
4-
propDefinitions: {},
6+
propDefinitions: {
7+
contactId: {
8+
type: "string",
9+
label: "Contact ID",
10+
description: "The ID of the contact to update",
11+
async options({ prevContext }) {
12+
const params = prevContext?.cursor
13+
? {
14+
cursor: prevContext.cursor,
15+
}
16+
: {};
17+
const { contacts } = await this.listContacts(params);
18+
const {
19+
items, cursor,
20+
} = contacts;
21+
return {
22+
options: items?.map(({
23+
id: value, properties: {
24+
firstName, lastName, email,
25+
},
26+
}) => ({
27+
label: (firstName || lastName)
28+
? (`${firstName} ${lastName}`).trim() + " - " + email
29+
: email,
30+
value,
31+
})) || [],
32+
context: {
33+
cursor,
34+
},
35+
};
36+
},
37+
},
38+
},
539
methods: {
6-
// this.$auth contains connected account data
7-
authKeys() {
8-
console.log(Object.keys(this.$auth));
40+
_baseUrl() {
41+
return "https://api.smashsend.com/v1";
42+
},
43+
_makeRequest({
44+
$ = this, path, ...opts
45+
}) {
46+
return axios($, {
47+
url: `${this._baseUrl()}${path}`,
48+
headers: {
49+
Authorization: `Bearer ${this.$auth.api_key}`,
50+
},
51+
...opts,
52+
});
53+
},
54+
createWebhook(opts = {}) {
55+
return this._makeRequest({
56+
method: "POST",
57+
path: "/webhooks",
58+
...opts,
59+
});
60+
},
61+
deleteWebhook({
62+
hookId, ...opts
63+
}) {
64+
return this._makeRequest({
65+
method: "DELETE",
66+
path: `/webhooks/${hookId}`,
67+
...opts,
68+
});
69+
},
70+
listContacts(opts = {}) {
71+
return this._makeRequest({
72+
path: "/contacts",
73+
...opts,
74+
});
75+
},
76+
searchContacts(opts = {}) {
77+
return this._makeRequest({
78+
path: "/contacts/search",
79+
...opts,
80+
});
81+
},
82+
createContact(opts = {}) {
83+
return this._makeRequest({
84+
method: "POST",
85+
path: "/contacts",
86+
...opts,
87+
});
88+
},
89+
deleteContact({
90+
contactId, ...opts
91+
}) {
92+
return this._makeRequest({
93+
method: "DELETE",
94+
path: `/contacts/${contactId}`,
95+
...opts,
96+
});
97+
},
98+
async *paginate({
99+
fn, params = {}, resourceKey, max,
100+
}) {
101+
let count = 0;
102+
do {
103+
const response = await fn({
104+
params,
105+
});
106+
const {
107+
cursor, hasMore, items,
108+
} = response[resourceKey];
109+
if (!items?.length) {
110+
return;
111+
}
112+
for (const item of items) {
113+
yield item;
114+
if (max && ++count >= max) {
115+
return;
116+
}
117+
}
118+
if (!hasMore) {
119+
return;
120+
}
121+
params.cursor = cursor;
122+
} while (true);
123+
},
124+
async getPaginatedResources(opts) {
125+
const results = [];
126+
for await (const item of this.paginate(opts)) {
127+
results.push(item);
128+
}
129+
return results;
9130
},
10131
},
11132
};

0 commit comments

Comments
 (0)