Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import mailerlite from "../../mailerlite.app.mjs";
export default {
key: "mailerlite-create-subscriber",
name: "Create Subscriber",
description: "Create a new subscriber. [See docs](https://developers.mailerlite.com/docs/subscribers.html#create-update-subscriber)",
version: "0.0.3",
description: "Create a new subscriber. [See the documentation](https://developers.mailerlite.com/docs/subscribers.html#create-update-subscriber)",
version: "0.0.4",
type: "action",
props: {
mailerlite,
Expand Down Expand Up @@ -34,11 +34,16 @@ export default {
async run({ $ }) {
const data = {
email: this.email,
name: this.name,
type: this.type,
fields: {
name: this.name,
},
status: this.type,
};

const resp = await this.mailerlite.createSubscriber(utils.removeUndefined(data));
const resp = await this.mailerlite.createSubscriber({
$,
data: utils.removeUndefined(data),
});
$.export("$summary", "Successfully created subscriber");
return resp;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import mailerlite from "../../mailerlite.app.mjs";
export default {
key: "mailerlite-list-subscribers",
name: "List Subscribers",
description: "Lists all subscribers in a group. [See the docs here](https://developers.mailerlite.com/docs/subscribers.html#list-all-subscribers)",
version: "0.0.3",
description: "Lists all subscribers in a group. [See the documentation](https://developers.mailerlite.com/docs/subscribers.html#list-all-subscribers)",
version: "0.0.4",
type: "action",
props: {
mailerlite,
Expand Down Expand Up @@ -42,7 +42,11 @@ export default {
let resp;

do {
resp = await this.mailerlite.listSubscribers(this.group, params);
resp = await this.mailerlite.listSubscribers({
$,
group: this.group,
params,
});
subscribers.push(...resp);
params.offset += params.limit;
} while (resp?.length === params.limit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import mailerlite from "../../mailerlite.app.mjs";
export default {
key: "mailerlite-remove-subscriber-from-group",
name: "Remove Subscriber From Group",
description: "Removes single subscriber from specified group. [See the docs here](https://developers.mailerlite.com/docs/groups.html#unassign-subscriber-from-a-group)",
version: "0.0.4",
description: "Removes single subscriber from specified group. [See the documentation](https://developers.mailerlite.com/docs/groups.html#unassign-subscriber-from-a-group)",
version: "0.0.5",
type: "action",
props: {
mailerlite,
Expand Down Expand Up @@ -37,10 +37,11 @@ export default {
},
},
async run({ $ }) {
const response = await this.mailerlite.removeSubscriberFromGroup(
this.group,
encodeURIComponent(this.subscriber),
);
const response = await this.mailerlite.removeSubscriberFromGroup({
$,
subscriber: this.subscriber,
group: this.group,
});

$.export("$summary", "Removed subscriber from group");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import mailerlite from "../../mailerlite.app.mjs";
export default {
key: "mailerlite-subscribe-to-group",
name: "Subscribe to MailerLite Group",
description: "Add a subscriber to a group. [See the docs here](https://developers.mailerlite.com/docs/groups.html#assign-subscriber-to-a-group)",
version: "0.3.2",
description: "Add a subscriber to a group. [See the documentation](https://developers.mailerlite.com/docs/groups.html#assign-subscriber-to-a-group)",
version: "0.3.3",
type: "action",
props: {
mailerlite,
Expand All @@ -14,19 +14,20 @@ export default {
"group",
],
},
email: {
subscriber: {
propDefinition: [
mailerlite,
"subscriber",
],
description: "Email of the active subscriber to add to group",
description: "ID of the active subscriber to add to group",
},
},
async run({ $ }) {
const data = {
email: this.email,
};
const resp = await this.mailerlite.addSubscriberToGroup(data, this.group);
const resp = await this.mailerlite.addSubscriberToGroup({
$,
subscriber: this.subscriber,
group: this.group,
});
$.export("$summary", "Added subscriber to group");
return resp;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import mailerlite from "../../mailerlite.app.mjs";
export default {
key: "mailerlite-update-subscriber",
name: "Update Subscriber",
description: "Updates single active subscriber. [See docs](https://developers.mailerlite.com/docs/subscribers.html#create-update-subscriber)",
version: "0.0.3",
description: "Updates single active subscriber. [See the documentation](https://developers.mailerlite.com/docs/subscribers.html#create-update-subscriber)",
version: "0.0.4",
type: "action",
props: {
mailerlite,
Expand Down Expand Up @@ -34,6 +34,9 @@ export default {
},
async additionalProps() {
const props = {};
if (!this.fields?.length) {
return props;
}
for (const field of this.fields) {
props[field.value] = {
type: "string",
Expand All @@ -45,14 +48,20 @@ export default {
},
async run({ $ }) {
const fields = {};
for (const field of this.fields) {
fields[field.value] = this[field.value];
if (this.fields?.length) {
for (const field of this.fields) {
fields[field.value] = this[field.value];
}
}
const data = {
type: this.type,
status: this.type,
fields,
};
const resp = await this.mailerlite.updateSubscriber(data, this.subscriber);
const resp = await this.mailerlite.updateSubscriber({
$,
subscriber: this.subscriber,
data,
});
$.export("$summary", "Successfully updated subscriber");
return resp;
},
Expand Down
170 changes: 104 additions & 66 deletions components/mailerlite/mailerlite.app.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import MailerLite from "mailerlite-api-v2-node";
import { axios } from "@pipedream/platform";
import constants from "./common/constants.mjs";

export default {
Expand Down Expand Up @@ -54,11 +54,14 @@ export default {
limit,
offset,
};
const subscribers = await this.listSubscribers(group, params);
const subscribers = await this.listSubscribers({
group,
params,
});
return {
options: subscribers.map((subscriber) => ({
label: subscriber.name,
value: subscriber.email,
label: subscriber.email,
value: subscriber.id,
})),
context: {
offset: offset + limit,
Expand All @@ -77,7 +80,9 @@ export default {
limit,
offset,
};
const groups = await this.listGroups(params);
const groups = await this.listGroups({
params,
});
return {
options: groups.map((group) => ({
label: group.name,
Expand Down Expand Up @@ -106,67 +111,100 @@ export default {
},
},
methods: {
_getApiKey() {
return this.$auth.api_key;
},
async _getClient(baseAPI) {
let options;
if (baseAPI === "connect") {
options = {
baseURL: "https://connect.mailerlite.com/api",
headers: {
Authorization: `Bearer ${this._getApiKey()}`,
},
};
}
const client = MailerLite.default;
return client(this._getApiKey(), options);
},
async listGroups(params) {
const client = await this._getClient();
return client.getGroups(params);
},
async listSubscribers(group, params = {}) {
const client = await this._getClient();
if (group) {
const { type = "active" } = params;
delete params.type;
return client.getGroupSubscribersByType(group, type, params);
}
// getSubscribers returns active subscribers only
return client.getSubscribers(params);
},
async listFields() {
const client = await this._getClient();
return client.getFields();
},
async listCampaigns(status = "sent") {
const client = await this._getClient();
return client.getCampaigns(status);
},
async createSubscriber(data) {
const client = await this._getClient();
return client.addSubscriber(data);
},
async createHook(data) {
const client = await this._getClient("connect");
return client.createWebhook(data);
},
async updateSubscriber(data, subscriber) {
const client = await this._getClient();
return client.updateSubscriber(subscriber, data);
},
async addSubscriberToGroup(data, group) {
const client = await this._getClient();
return client.addSubscriberToGroup(group, data);
},
async removeHook(hookId) {
const client = await this._getClient("connect");
return client.removeWebhook(hookId);
},
async removeSubscriberFromGroup(group, subscriber) {
const client = await this._getClient();
return client.removeGroupSubscriber(group, subscriber);
_baseUrl() {
return "https://connect.mailerlite.com/api";
},
_makeRequest({
$ = this,
path,
...args
}) {
return axios($, {
url: `${this._baseUrl()}${path}`,
headers: {
Authorization: `Bearer ${this.$auth.api_key}`,
},
...args,
});
},
async listGroups(opts = {}) {
const { data } = await this._makeRequest({
path: "/groups",
...opts,
});
return data;
},
async listSubscribers({
group, params = {}, ...opts
}) {
const { type = "active" } = params;
delete params.type;
params["filter[status]"] = type;
const { data } = await this._makeRequest({
path: group
? `/groups/${group}/subscribers`
: "/subscribers",
params,
...opts,
});
return data;
},
async listFields(opts = {}) {
const { data } = await this._makeRequest({
path: "/fields",
...opts,
});
return data;
},
createSubscriber(opts = {}) {
return this._makeRequest({
method: "POST",
path: "/subscribers",
...opts,
});
},
createHook(opts = {}) {
return this._makeRequest({
method: "POST",
path: "/webhooks",
...opts,
});
},
updateSubscriber({
subscriber, ...opts
}) {
return this._makeRequest({
method: "PUT",
path: `/subscribers/${subscriber}`,
...opts,
});
},
addSubscriberToGroup({
subscriber, group, ...opts
}) {
return this._makeRequest({
method: "POST",
path: `/subscribers/${subscriber}/groups/${group}`,
...opts,
});
},
removeHook({
hookId, ...opts
}) {
return this._makeRequest({
method: "DELETE",
path: `/webhooks/${hookId}`,
...opts,
});
},
removeSubscriberFromGroup({
subscriber, group, ...opts
}) {
return this._makeRequest({
method: "DELETE",
path: `/subscribers/${subscriber}/groups/${group}`,
...opts,
});
},
},
};
6 changes: 3 additions & 3 deletions components/mailerlite/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pipedream/mailerlite",
"version": "1.1.0",
"version": "1.1.1",
"description": "Pipedream Mailerlite Components",
"main": "mailerlite.app.mjs",
"keywords": [
Expand All @@ -14,7 +14,7 @@
"access": "public"
},
"dependencies": {
"lodash.pickby": "^4.6.0",
"mailerlite-api-v2-node": "^1.2.0"
"@pipedream/platform": "^3.0.3",
"lodash.pickby": "^4.6.0"
}
}
Loading
Loading