@rawdash/connector-mailchimp 0.0.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 +131 -0
- package/dist/index.d.ts +601 -0
- package/dist/index.js +652 -0
- package/dist/index.js.map +1 -0
- package/package.json +43 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,652 @@
|
|
|
1
|
+
// ../../connector-shared/dist/index.js
|
|
2
|
+
var HTTP_CLIENT_VERSION = "0.0.0";
|
|
3
|
+
var DEFAULT_USER_AGENT = `rawdash-connector/${HTTP_CLIENT_VERSION} (+https://rawdash.dev)`;
|
|
4
|
+
function connectorUserAgent(connectorId) {
|
|
5
|
+
return `rawdash-connector-${connectorId}/${HTTP_CLIENT_VERSION} (+https://rawdash.dev)`;
|
|
6
|
+
}
|
|
7
|
+
function parseEpoch(value, unit) {
|
|
8
|
+
if (value === null || value === void 0) {
|
|
9
|
+
return null;
|
|
10
|
+
}
|
|
11
|
+
if (unit === "iso") {
|
|
12
|
+
if (typeof value !== "string") {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
const ms = new Date(value).getTime();
|
|
16
|
+
return Number.isFinite(ms) ? ms : null;
|
|
17
|
+
}
|
|
18
|
+
if (typeof value === "string" && value.trim() === "") {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
const n = typeof value === "number" ? value : Number(value);
|
|
22
|
+
if (!Number.isFinite(n)) {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
const result = unit === "s" ? n * 1e3 : n;
|
|
26
|
+
return Number.isFinite(result) ? result : null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// src/mailchimp.ts
|
|
30
|
+
import {
|
|
31
|
+
BaseConnector,
|
|
32
|
+
defineConfigFields,
|
|
33
|
+
defineConnectorDoc,
|
|
34
|
+
defineResources,
|
|
35
|
+
makeChunkedCursorGuard,
|
|
36
|
+
paginateChunked,
|
|
37
|
+
schemasFromResources,
|
|
38
|
+
selectActivePhases
|
|
39
|
+
} from "@rawdash/core";
|
|
40
|
+
import { z } from "zod";
|
|
41
|
+
var configFields = defineConfigFields(
|
|
42
|
+
z.object({
|
|
43
|
+
apiKey: z.object({ $secret: z.string() }).meta({
|
|
44
|
+
label: "API key",
|
|
45
|
+
description: "Mailchimp Marketing API key. The data-center suffix after the dash (e.g. `-us1`) selects the API host. Create one at Profile -> Extras -> API keys.",
|
|
46
|
+
placeholder: "abc123...-us1",
|
|
47
|
+
secret: true
|
|
48
|
+
}),
|
|
49
|
+
resources: z.array(z.enum(["campaigns", "lists", "automations", "campaign_stats"])).nonempty().optional().meta({
|
|
50
|
+
label: "Resources",
|
|
51
|
+
description: "Which Mailchimp resources to sync. Omit to sync all of them."
|
|
52
|
+
})
|
|
53
|
+
})
|
|
54
|
+
);
|
|
55
|
+
var doc = defineConnectorDoc({
|
|
56
|
+
displayName: "Mailchimp",
|
|
57
|
+
category: "marketing",
|
|
58
|
+
brandColor: "#FFE01B",
|
|
59
|
+
tagline: "Sync Mailchimp campaigns, audiences (lists), automations, and per-campaign engagement stats for marketing email analytics.",
|
|
60
|
+
vendor: {
|
|
61
|
+
name: "Mailchimp",
|
|
62
|
+
apiDocs: "https://mailchimp.com/developer/marketing/api/",
|
|
63
|
+
website: "https://mailchimp.com"
|
|
64
|
+
},
|
|
65
|
+
auth: {
|
|
66
|
+
summary: "A Mailchimp Marketing API key. The data-center suffix after the dash (e.g. `-us1`) selects the API host the connector talks to.",
|
|
67
|
+
setup: [
|
|
68
|
+
"In Mailchimp, open Profile -> Extras -> API keys and create a new API key.",
|
|
69
|
+
"Copy the full key including the trailing data-center suffix (e.g. `abc123...-us1`); the suffix selects the API host.",
|
|
70
|
+
'Store the key as a secret and reference it from config as `apiKey: secret("MAILCHIMP_API_KEY")`.'
|
|
71
|
+
]
|
|
72
|
+
},
|
|
73
|
+
rateLimit: "Mailchimp allows up to 10 simultaneous connections per account; per-endpoint rate limits are not advertised, so the connector keeps to sequential paginated requests.",
|
|
74
|
+
limitations: [
|
|
75
|
+
"Per-campaign report stats are rewritten on every sync because the /reports endpoint has no `since` filter.",
|
|
76
|
+
"Automations are synced as entities only; per-workflow open/click counts are out of scope.",
|
|
77
|
+
"Member-level data, ecommerce stores, and landing pages are out of scope."
|
|
78
|
+
]
|
|
79
|
+
});
|
|
80
|
+
var mailchimpCredentials = {
|
|
81
|
+
apiKey: {
|
|
82
|
+
description: "Mailchimp Marketing API key (with data-center suffix)",
|
|
83
|
+
auth: "required"
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
var PHASE_ORDER = [
|
|
87
|
+
"campaigns",
|
|
88
|
+
"lists",
|
|
89
|
+
"automations",
|
|
90
|
+
"campaign_stats"
|
|
91
|
+
];
|
|
92
|
+
var isMailchimpSyncCursor = makeChunkedCursorGuard(PHASE_ORDER);
|
|
93
|
+
var CAMPAIGN_ENTITY = "mailchimp_campaign";
|
|
94
|
+
var LIST_ENTITY = "mailchimp_list";
|
|
95
|
+
var AUTOMATION_ENTITY = "mailchimp_automation";
|
|
96
|
+
var CAMPAIGN_STATS_METRIC = "mailchimp_campaign_stats";
|
|
97
|
+
var PAGE_SIZE = 500;
|
|
98
|
+
var idString = z.string().min(1);
|
|
99
|
+
var campaignSchema = z.object({
|
|
100
|
+
id: idString,
|
|
101
|
+
status: z.string().nullish(),
|
|
102
|
+
type: z.string().nullish(),
|
|
103
|
+
create_time: z.string().nullish(),
|
|
104
|
+
send_time: z.string().nullish(),
|
|
105
|
+
emails_sent: z.number().nullish(),
|
|
106
|
+
recipients: z.object({
|
|
107
|
+
list_id: z.string().nullish(),
|
|
108
|
+
list_name: z.string().nullish()
|
|
109
|
+
}).nullish(),
|
|
110
|
+
settings: z.object({
|
|
111
|
+
subject_line: z.string().nullish(),
|
|
112
|
+
title: z.string().nullish(),
|
|
113
|
+
from_name: z.string().nullish(),
|
|
114
|
+
reply_to: z.string().nullish()
|
|
115
|
+
}).nullish()
|
|
116
|
+
});
|
|
117
|
+
var listSchema = z.object({
|
|
118
|
+
id: idString,
|
|
119
|
+
name: z.string().nullish(),
|
|
120
|
+
date_created: z.string().nullish(),
|
|
121
|
+
list_rating: z.number().nullish(),
|
|
122
|
+
stats: z.object({
|
|
123
|
+
member_count: z.number().nullish(),
|
|
124
|
+
unsubscribe_count: z.number().nullish(),
|
|
125
|
+
cleaned_count: z.number().nullish(),
|
|
126
|
+
open_rate: z.number().nullish(),
|
|
127
|
+
click_rate: z.number().nullish(),
|
|
128
|
+
campaign_count: z.number().nullish()
|
|
129
|
+
}).nullish()
|
|
130
|
+
});
|
|
131
|
+
var automationSchema = z.object({
|
|
132
|
+
id: idString,
|
|
133
|
+
create_time: z.string().nullish(),
|
|
134
|
+
start_time: z.string().nullish(),
|
|
135
|
+
status: z.string().nullish(),
|
|
136
|
+
emails_sent: z.number().nullish(),
|
|
137
|
+
recipients: z.object({
|
|
138
|
+
list_id: z.string().nullish(),
|
|
139
|
+
list_name: z.string().nullish()
|
|
140
|
+
}).nullish(),
|
|
141
|
+
settings: z.object({
|
|
142
|
+
title: z.string().nullish(),
|
|
143
|
+
from_name: z.string().nullish(),
|
|
144
|
+
reply_to: z.string().nullish()
|
|
145
|
+
}).nullish()
|
|
146
|
+
});
|
|
147
|
+
var reportSchema = z.object({
|
|
148
|
+
id: idString,
|
|
149
|
+
campaign_title: z.string().nullish(),
|
|
150
|
+
type: z.string().nullish(),
|
|
151
|
+
list_id: z.string().nullish(),
|
|
152
|
+
emails_sent: z.number().nullish(),
|
|
153
|
+
unsubscribed: z.number().nullish(),
|
|
154
|
+
send_time: z.string().nullish(),
|
|
155
|
+
opens: z.object({
|
|
156
|
+
opens_total: z.number().nullish(),
|
|
157
|
+
unique_opens: z.number().nullish(),
|
|
158
|
+
open_rate: z.number().nullish()
|
|
159
|
+
}).nullish(),
|
|
160
|
+
clicks: z.object({
|
|
161
|
+
clicks_total: z.number().nullish(),
|
|
162
|
+
unique_clicks: z.number().nullish(),
|
|
163
|
+
click_rate: z.number().nullish()
|
|
164
|
+
}).nullish(),
|
|
165
|
+
bounces: z.object({
|
|
166
|
+
hard_bounces: z.number().nullish(),
|
|
167
|
+
soft_bounces: z.number().nullish(),
|
|
168
|
+
syntax_errors: z.number().nullish()
|
|
169
|
+
}).nullish()
|
|
170
|
+
});
|
|
171
|
+
var mailchimpResources = defineResources({
|
|
172
|
+
[CAMPAIGN_ENTITY]: {
|
|
173
|
+
shape: "entity",
|
|
174
|
+
description: "Campaigns (regular, plaintext, A/B, RSS, etc.) with status, type, subject line, sender, audience, send time, and total emails sent.",
|
|
175
|
+
endpoint: "GET /campaigns",
|
|
176
|
+
fields: [
|
|
177
|
+
{
|
|
178
|
+
name: "status",
|
|
179
|
+
description: "Campaign status (save, paused, schedule, sending, sent)."
|
|
180
|
+
},
|
|
181
|
+
{
|
|
182
|
+
name: "type",
|
|
183
|
+
description: "Campaign type (regular, plaintext, absplit, rss, etc.)."
|
|
184
|
+
},
|
|
185
|
+
{ name: "subjectLine", description: "Email subject line." },
|
|
186
|
+
{ name: "title", description: "Internal campaign title." },
|
|
187
|
+
{ name: "fromName", description: "Sender display name." },
|
|
188
|
+
{ name: "replyTo", description: "Reply-to email address." },
|
|
189
|
+
{
|
|
190
|
+
name: "listId",
|
|
191
|
+
description: "Audience (list) id the campaign targets."
|
|
192
|
+
},
|
|
193
|
+
{ name: "listName", description: "Audience (list) display name." },
|
|
194
|
+
{
|
|
195
|
+
name: "createTime",
|
|
196
|
+
description: "When the campaign was created (Unix ms)."
|
|
197
|
+
},
|
|
198
|
+
{
|
|
199
|
+
name: "sendTime",
|
|
200
|
+
description: "When the campaign was sent (Unix ms)."
|
|
201
|
+
},
|
|
202
|
+
{ name: "emailsSent", description: "Total emails sent." }
|
|
203
|
+
],
|
|
204
|
+
responses: { campaigns: z.array(campaignSchema) }
|
|
205
|
+
},
|
|
206
|
+
[LIST_ENTITY]: {
|
|
207
|
+
shape: "entity",
|
|
208
|
+
description: "Audiences (lists) with member counts, engagement rates, and lifetime campaign count.",
|
|
209
|
+
endpoint: "GET /lists",
|
|
210
|
+
fields: [
|
|
211
|
+
{ name: "name", description: "Audience name." },
|
|
212
|
+
{ name: "memberCount", description: "Number of subscribed members." },
|
|
213
|
+
{
|
|
214
|
+
name: "unsubscribeCount",
|
|
215
|
+
description: "Number of unsubscribed members."
|
|
216
|
+
},
|
|
217
|
+
{ name: "cleanedCount", description: "Number of cleaned addresses." },
|
|
218
|
+
{
|
|
219
|
+
name: "openRate",
|
|
220
|
+
description: "Lifetime open rate as a fraction (0 to 1)."
|
|
221
|
+
},
|
|
222
|
+
{
|
|
223
|
+
name: "clickRate",
|
|
224
|
+
description: "Lifetime click rate as a fraction (0 to 1)."
|
|
225
|
+
},
|
|
226
|
+
{
|
|
227
|
+
name: "campaignCount",
|
|
228
|
+
description: "Number of campaigns sent to the audience."
|
|
229
|
+
},
|
|
230
|
+
{ name: "listRating", description: "Mailchimp star rating (0 to 5)." },
|
|
231
|
+
{
|
|
232
|
+
name: "createdAt",
|
|
233
|
+
description: "When the audience was created (Unix ms)."
|
|
234
|
+
}
|
|
235
|
+
],
|
|
236
|
+
responses: { lists: z.array(listSchema) }
|
|
237
|
+
},
|
|
238
|
+
[AUTOMATION_ENTITY]: {
|
|
239
|
+
shape: "entity",
|
|
240
|
+
description: "Automations (classic email workflows) with status, title, sender, audience, and lifetime emails sent.",
|
|
241
|
+
endpoint: "GET /automations",
|
|
242
|
+
fields: [
|
|
243
|
+
{
|
|
244
|
+
name: "status",
|
|
245
|
+
description: "Automation status (save, paused, sending)."
|
|
246
|
+
},
|
|
247
|
+
{ name: "title", description: "Automation title." },
|
|
248
|
+
{ name: "fromName", description: "Sender display name." },
|
|
249
|
+
{ name: "replyTo", description: "Reply-to email address." },
|
|
250
|
+
{
|
|
251
|
+
name: "listId",
|
|
252
|
+
description: "Audience (list) id the automation targets."
|
|
253
|
+
},
|
|
254
|
+
{ name: "listName", description: "Audience (list) display name." },
|
|
255
|
+
{
|
|
256
|
+
name: "emailsSent",
|
|
257
|
+
description: "Total emails sent over the workflow lifetime."
|
|
258
|
+
},
|
|
259
|
+
{
|
|
260
|
+
name: "createTime",
|
|
261
|
+
description: "When the automation was created (Unix ms)."
|
|
262
|
+
},
|
|
263
|
+
{
|
|
264
|
+
name: "startTime",
|
|
265
|
+
description: "When the automation was started (Unix ms)."
|
|
266
|
+
}
|
|
267
|
+
],
|
|
268
|
+
responses: { automations: z.array(automationSchema) }
|
|
269
|
+
},
|
|
270
|
+
[CAMPAIGN_STATS_METRIC]: {
|
|
271
|
+
shape: "metric",
|
|
272
|
+
description: "Per-campaign engagement stats (sent, opens, clicks, bounces, unsubscribes) timestamped at the campaign send time.",
|
|
273
|
+
endpoint: "GET /reports",
|
|
274
|
+
unit: "emails",
|
|
275
|
+
notes: "One sample per campaign; value is the sent count, and every other counter is exposed in attributes. The scope is cleared and rewritten on every sync because the /reports endpoint has no `since` filter.",
|
|
276
|
+
dimensions: [
|
|
277
|
+
{ name: "campaignId", description: "Campaign id." },
|
|
278
|
+
{ name: "campaignTitle", description: "Campaign internal title." },
|
|
279
|
+
{ name: "campaignType", description: "Campaign type." },
|
|
280
|
+
{ name: "listId", description: "Audience (list) id." },
|
|
281
|
+
{ name: "opensTotal", description: "Total opens." },
|
|
282
|
+
{ name: "uniqueOpens", description: "Unique opens." },
|
|
283
|
+
{ name: "openRate", description: "Open rate as a fraction (0 to 1)." },
|
|
284
|
+
{ name: "clicksTotal", description: "Total clicks." },
|
|
285
|
+
{ name: "uniqueClicks", description: "Unique clicks." },
|
|
286
|
+
{ name: "clickRate", description: "Click rate as a fraction (0 to 1)." },
|
|
287
|
+
{ name: "hardBounces", description: "Number of hard bounces." },
|
|
288
|
+
{ name: "softBounces", description: "Number of soft bounces." },
|
|
289
|
+
{ name: "unsubscribed", description: "Number of unsubscribes." }
|
|
290
|
+
],
|
|
291
|
+
responses: { campaign_stats: z.array(reportSchema) }
|
|
292
|
+
}
|
|
293
|
+
});
|
|
294
|
+
function isoToMs(value) {
|
|
295
|
+
return parseEpoch(value ?? null, "iso");
|
|
296
|
+
}
|
|
297
|
+
function isoToMsOrZero(value) {
|
|
298
|
+
return isoToMs(value) ?? 0;
|
|
299
|
+
}
|
|
300
|
+
function nullableNumber(value) {
|
|
301
|
+
return typeof value === "number" && Number.isFinite(value) ? value : null;
|
|
302
|
+
}
|
|
303
|
+
function counterValue(value) {
|
|
304
|
+
return typeof value === "number" && Number.isFinite(value) ? value : 0;
|
|
305
|
+
}
|
|
306
|
+
function dataCenterFromApiKey(apiKey) {
|
|
307
|
+
const dash = apiKey.lastIndexOf("-");
|
|
308
|
+
if (dash === -1 || dash === apiKey.length - 1) {
|
|
309
|
+
throw new Error(
|
|
310
|
+
"Mailchimp API key is missing the data-center suffix (e.g. `-us1`)."
|
|
311
|
+
);
|
|
312
|
+
}
|
|
313
|
+
const dc = apiKey.slice(dash + 1);
|
|
314
|
+
if (!/^[a-z]{1,4}\d{1,3}$/i.test(dc)) {
|
|
315
|
+
throw new Error(
|
|
316
|
+
`Mailchimp API key data-center suffix "${dc}" is not in the expected shape (e.g. "us1", "us21").`
|
|
317
|
+
);
|
|
318
|
+
}
|
|
319
|
+
return dc;
|
|
320
|
+
}
|
|
321
|
+
function encodeBasicAuth(username, password) {
|
|
322
|
+
const raw = `${username}:${password}`;
|
|
323
|
+
if (typeof btoa === "function") {
|
|
324
|
+
return `Basic ${btoa(raw)}`;
|
|
325
|
+
}
|
|
326
|
+
const bufferCtor = globalThis.Buffer;
|
|
327
|
+
if (bufferCtor) {
|
|
328
|
+
return `Basic ${bufferCtor.from(raw).toString("base64")}`;
|
|
329
|
+
}
|
|
330
|
+
throw new Error("No base64 encoder available in this runtime");
|
|
331
|
+
}
|
|
332
|
+
function nextOffsetCursor(currentOffset, pageSize) {
|
|
333
|
+
return String(currentOffset + pageSize);
|
|
334
|
+
}
|
|
335
|
+
function offsetFromPage(page) {
|
|
336
|
+
if (page === null) {
|
|
337
|
+
return 0;
|
|
338
|
+
}
|
|
339
|
+
const n = Number(page);
|
|
340
|
+
return Number.isFinite(n) && n >= 0 ? n : 0;
|
|
341
|
+
}
|
|
342
|
+
var id = "mailchimp";
|
|
343
|
+
var MailchimpConnector = class _MailchimpConnector extends BaseConnector {
|
|
344
|
+
static id = id;
|
|
345
|
+
static resources = mailchimpResources;
|
|
346
|
+
static schemas = schemasFromResources(mailchimpResources);
|
|
347
|
+
static create(input, ctx) {
|
|
348
|
+
const parsed = configFields.parse(input);
|
|
349
|
+
return new _MailchimpConnector(
|
|
350
|
+
{ resources: parsed.resources },
|
|
351
|
+
{ apiKey: parsed.apiKey },
|
|
352
|
+
ctx
|
|
353
|
+
);
|
|
354
|
+
}
|
|
355
|
+
id = id;
|
|
356
|
+
credentials = mailchimpCredentials;
|
|
357
|
+
get baseUrl() {
|
|
358
|
+
const dc = dataCenterFromApiKey(this.creds.apiKey);
|
|
359
|
+
return `https://${dc}.api.mailchimp.com/3.0`;
|
|
360
|
+
}
|
|
361
|
+
buildHeaders() {
|
|
362
|
+
return {
|
|
363
|
+
// Mailchimp accepts any username with the API key as the password.
|
|
364
|
+
Authorization: encodeBasicAuth("rawdash", this.creds.apiKey),
|
|
365
|
+
Accept: "application/json",
|
|
366
|
+
"User-Agent": connectorUserAgent("mailchimp")
|
|
367
|
+
};
|
|
368
|
+
}
|
|
369
|
+
async fetchList(path, resource, params, signal) {
|
|
370
|
+
const url = new URL(`${this.baseUrl}${path}`);
|
|
371
|
+
for (const [key, value] of Object.entries(params)) {
|
|
372
|
+
if (value === void 0 || value === null || value === "") {
|
|
373
|
+
continue;
|
|
374
|
+
}
|
|
375
|
+
url.searchParams.set(key, String(value));
|
|
376
|
+
}
|
|
377
|
+
const res = await this.get(url.toString(), {
|
|
378
|
+
resource,
|
|
379
|
+
headers: this.buildHeaders(),
|
|
380
|
+
signal
|
|
381
|
+
});
|
|
382
|
+
return res.body;
|
|
383
|
+
}
|
|
384
|
+
// -------------------------------------------------------------------------
|
|
385
|
+
// campaigns - GET /campaigns
|
|
386
|
+
// -------------------------------------------------------------------------
|
|
387
|
+
async fetchCampaignsPage(page, options, signal) {
|
|
388
|
+
const offset = offsetFromPage(page);
|
|
389
|
+
const body = await this.fetchList(
|
|
390
|
+
"/campaigns",
|
|
391
|
+
"campaigns",
|
|
392
|
+
{
|
|
393
|
+
count: PAGE_SIZE,
|
|
394
|
+
offset,
|
|
395
|
+
// Ascending send time keeps the offset cursor stable across pages.
|
|
396
|
+
sort_field: "send_time",
|
|
397
|
+
sort_dir: "ASC",
|
|
398
|
+
...options.since ? { since_send_time: options.since } : {}
|
|
399
|
+
},
|
|
400
|
+
signal
|
|
401
|
+
);
|
|
402
|
+
const campaigns = body.campaigns ?? [];
|
|
403
|
+
const next = campaigns.length < PAGE_SIZE ? null : nextOffsetCursor(offset, PAGE_SIZE);
|
|
404
|
+
return { items: campaigns, next };
|
|
405
|
+
}
|
|
406
|
+
async writeCampaigns(storage, items) {
|
|
407
|
+
for (const campaign of items) {
|
|
408
|
+
const settings = campaign.settings ?? {};
|
|
409
|
+
const recipients = campaign.recipients ?? {};
|
|
410
|
+
const attributes = {
|
|
411
|
+
status: campaign.status ?? null,
|
|
412
|
+
type: campaign.type ?? null,
|
|
413
|
+
subjectLine: settings.subject_line ?? null,
|
|
414
|
+
title: settings.title ?? null,
|
|
415
|
+
fromName: settings.from_name ?? null,
|
|
416
|
+
replyTo: settings.reply_to ?? null,
|
|
417
|
+
listId: recipients.list_id ?? null,
|
|
418
|
+
listName: recipients.list_name ?? null,
|
|
419
|
+
createTime: isoToMs(campaign.create_time),
|
|
420
|
+
sendTime: isoToMs(campaign.send_time),
|
|
421
|
+
emailsSent: nullableNumber(campaign.emails_sent)
|
|
422
|
+
};
|
|
423
|
+
const updatedAt = isoToMs(campaign.send_time) ?? isoToMsOrZero(campaign.create_time);
|
|
424
|
+
await storage.entity({
|
|
425
|
+
type: CAMPAIGN_ENTITY,
|
|
426
|
+
id: campaign.id,
|
|
427
|
+
attributes,
|
|
428
|
+
updated_at: updatedAt
|
|
429
|
+
});
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
// -------------------------------------------------------------------------
|
|
433
|
+
// lists - GET /lists
|
|
434
|
+
// -------------------------------------------------------------------------
|
|
435
|
+
async fetchListsPage(page, options, signal) {
|
|
436
|
+
const offset = offsetFromPage(page);
|
|
437
|
+
const body = await this.fetchList(
|
|
438
|
+
"/lists",
|
|
439
|
+
"lists",
|
|
440
|
+
{
|
|
441
|
+
count: PAGE_SIZE,
|
|
442
|
+
offset,
|
|
443
|
+
sort_field: "date_created",
|
|
444
|
+
sort_dir: "ASC",
|
|
445
|
+
...options.since ? { since_date_created: options.since } : {}
|
|
446
|
+
},
|
|
447
|
+
signal
|
|
448
|
+
);
|
|
449
|
+
const lists = body.lists ?? [];
|
|
450
|
+
const next = lists.length < PAGE_SIZE ? null : nextOffsetCursor(offset, PAGE_SIZE);
|
|
451
|
+
return { items: lists, next };
|
|
452
|
+
}
|
|
453
|
+
async writeLists(storage, items) {
|
|
454
|
+
for (const list of items) {
|
|
455
|
+
const stats = list.stats ?? {};
|
|
456
|
+
const attributes = {
|
|
457
|
+
name: list.name ?? null,
|
|
458
|
+
memberCount: nullableNumber(stats.member_count),
|
|
459
|
+
unsubscribeCount: nullableNumber(stats.unsubscribe_count),
|
|
460
|
+
cleanedCount: nullableNumber(stats.cleaned_count),
|
|
461
|
+
openRate: nullableNumber(stats.open_rate),
|
|
462
|
+
clickRate: nullableNumber(stats.click_rate),
|
|
463
|
+
campaignCount: nullableNumber(stats.campaign_count),
|
|
464
|
+
listRating: nullableNumber(list.list_rating),
|
|
465
|
+
createdAt: isoToMs(list.date_created)
|
|
466
|
+
};
|
|
467
|
+
await storage.entity({
|
|
468
|
+
type: LIST_ENTITY,
|
|
469
|
+
id: list.id,
|
|
470
|
+
attributes,
|
|
471
|
+
// Lists have no updated_at in the API; stamp with sync time so newer
|
|
472
|
+
// syncs win on conflict.
|
|
473
|
+
updated_at: Date.now()
|
|
474
|
+
});
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
// -------------------------------------------------------------------------
|
|
478
|
+
// automations - GET /automations
|
|
479
|
+
// -------------------------------------------------------------------------
|
|
480
|
+
async fetchAutomationsPage(page, signal) {
|
|
481
|
+
const offset = offsetFromPage(page);
|
|
482
|
+
const body = await this.fetchList(
|
|
483
|
+
"/automations",
|
|
484
|
+
"automations",
|
|
485
|
+
{
|
|
486
|
+
count: PAGE_SIZE,
|
|
487
|
+
offset
|
|
488
|
+
},
|
|
489
|
+
signal
|
|
490
|
+
);
|
|
491
|
+
const automations = body.automations ?? [];
|
|
492
|
+
const next = automations.length < PAGE_SIZE ? null : nextOffsetCursor(offset, PAGE_SIZE);
|
|
493
|
+
return { items: automations, next };
|
|
494
|
+
}
|
|
495
|
+
async writeAutomations(storage, items) {
|
|
496
|
+
for (const automation of items) {
|
|
497
|
+
const settings = automation.settings ?? {};
|
|
498
|
+
const recipients = automation.recipients ?? {};
|
|
499
|
+
const attributes = {
|
|
500
|
+
status: automation.status ?? null,
|
|
501
|
+
title: settings.title ?? null,
|
|
502
|
+
fromName: settings.from_name ?? null,
|
|
503
|
+
replyTo: settings.reply_to ?? null,
|
|
504
|
+
listId: recipients.list_id ?? null,
|
|
505
|
+
listName: recipients.list_name ?? null,
|
|
506
|
+
emailsSent: nullableNumber(automation.emails_sent),
|
|
507
|
+
createTime: isoToMs(automation.create_time),
|
|
508
|
+
startTime: isoToMs(automation.start_time)
|
|
509
|
+
};
|
|
510
|
+
const updatedAt = isoToMs(automation.start_time) ?? isoToMs(automation.create_time) ?? Date.now();
|
|
511
|
+
await storage.entity({
|
|
512
|
+
type: AUTOMATION_ENTITY,
|
|
513
|
+
id: automation.id,
|
|
514
|
+
attributes,
|
|
515
|
+
updated_at: updatedAt
|
|
516
|
+
});
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
// -------------------------------------------------------------------------
|
|
520
|
+
// campaign_stats - GET /reports (metric, one sample per campaign)
|
|
521
|
+
// -------------------------------------------------------------------------
|
|
522
|
+
async fetchReportsPage(page, signal) {
|
|
523
|
+
const offset = offsetFromPage(page);
|
|
524
|
+
const body = await this.fetchList(
|
|
525
|
+
"/reports",
|
|
526
|
+
"campaign_stats",
|
|
527
|
+
{
|
|
528
|
+
count: PAGE_SIZE,
|
|
529
|
+
offset
|
|
530
|
+
},
|
|
531
|
+
signal
|
|
532
|
+
);
|
|
533
|
+
const reports = body.reports ?? [];
|
|
534
|
+
const next = reports.length < PAGE_SIZE ? null : nextOffsetCursor(offset, PAGE_SIZE);
|
|
535
|
+
return { items: reports, next };
|
|
536
|
+
}
|
|
537
|
+
async writeCampaignStats(storage, items) {
|
|
538
|
+
for (const report of items) {
|
|
539
|
+
const ts = isoToMs(report.send_time);
|
|
540
|
+
if (ts === null) {
|
|
541
|
+
continue;
|
|
542
|
+
}
|
|
543
|
+
const opens = report.opens ?? {};
|
|
544
|
+
const clicks = report.clicks ?? {};
|
|
545
|
+
const bounces = report.bounces ?? {};
|
|
546
|
+
const sent = counterValue(report.emails_sent);
|
|
547
|
+
await storage.metric({
|
|
548
|
+
name: CAMPAIGN_STATS_METRIC,
|
|
549
|
+
ts,
|
|
550
|
+
value: sent,
|
|
551
|
+
attributes: {
|
|
552
|
+
campaignId: report.id,
|
|
553
|
+
campaignTitle: report.campaign_title ?? null,
|
|
554
|
+
campaignType: report.type ?? null,
|
|
555
|
+
listId: report.list_id ?? null,
|
|
556
|
+
sent,
|
|
557
|
+
opensTotal: counterValue(opens.opens_total),
|
|
558
|
+
uniqueOpens: counterValue(opens.unique_opens),
|
|
559
|
+
openRate: nullableNumber(opens.open_rate),
|
|
560
|
+
clicksTotal: counterValue(clicks.clicks_total),
|
|
561
|
+
uniqueClicks: counterValue(clicks.unique_clicks),
|
|
562
|
+
clickRate: nullableNumber(clicks.click_rate),
|
|
563
|
+
hardBounces: counterValue(bounces.hard_bounces),
|
|
564
|
+
softBounces: counterValue(bounces.soft_bounces),
|
|
565
|
+
unsubscribed: counterValue(report.unsubscribed)
|
|
566
|
+
}
|
|
567
|
+
});
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
// -------------------------------------------------------------------------
|
|
571
|
+
// Scope clearing (idempotency)
|
|
572
|
+
// -------------------------------------------------------------------------
|
|
573
|
+
async clearScopeOnFirstPage(storage, phase, isFull) {
|
|
574
|
+
if (phase === "campaign_stats") {
|
|
575
|
+
await storage.metrics([], { names: [CAMPAIGN_STATS_METRIC] });
|
|
576
|
+
return;
|
|
577
|
+
}
|
|
578
|
+
if (!isFull) {
|
|
579
|
+
return;
|
|
580
|
+
}
|
|
581
|
+
const entityType = ENTITY_TYPE_BY_PHASE[phase];
|
|
582
|
+
if (entityType) {
|
|
583
|
+
await storage.entities([], { types: [entityType] });
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
async writePhase(storage, phase, items) {
|
|
587
|
+
switch (phase) {
|
|
588
|
+
case "campaigns":
|
|
589
|
+
await this.writeCampaigns(storage, items);
|
|
590
|
+
return;
|
|
591
|
+
case "lists":
|
|
592
|
+
await this.writeLists(storage, items);
|
|
593
|
+
return;
|
|
594
|
+
case "automations":
|
|
595
|
+
await this.writeAutomations(storage, items);
|
|
596
|
+
return;
|
|
597
|
+
case "campaign_stats":
|
|
598
|
+
await this.writeCampaignStats(storage, items);
|
|
599
|
+
return;
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
async sync(options, storage, signal) {
|
|
603
|
+
const cursor = isMailchimpSyncCursor(options.cursor) ? options.cursor : void 0;
|
|
604
|
+
const isFull = options.mode === "full";
|
|
605
|
+
const phases = selectActivePhases(
|
|
606
|
+
(r) => r,
|
|
607
|
+
PHASE_ORDER,
|
|
608
|
+
this.settings.resources
|
|
609
|
+
);
|
|
610
|
+
return paginateChunked({
|
|
611
|
+
phases,
|
|
612
|
+
cursor,
|
|
613
|
+
signal,
|
|
614
|
+
logger: this.logger,
|
|
615
|
+
fetchPage: async (phase, page, sig) => {
|
|
616
|
+
switch (phase) {
|
|
617
|
+
case "campaigns":
|
|
618
|
+
return this.fetchCampaignsPage(page, options, sig);
|
|
619
|
+
case "lists":
|
|
620
|
+
return this.fetchListsPage(page, options, sig);
|
|
621
|
+
case "automations":
|
|
622
|
+
return this.fetchAutomationsPage(page, sig);
|
|
623
|
+
case "campaign_stats":
|
|
624
|
+
return this.fetchReportsPage(page, sig);
|
|
625
|
+
}
|
|
626
|
+
},
|
|
627
|
+
writeBatch: async (phase, items, page) => {
|
|
628
|
+
if (page === null) {
|
|
629
|
+
await this.clearScopeOnFirstPage(storage, phase, isFull);
|
|
630
|
+
}
|
|
631
|
+
await this.writePhase(storage, phase, items);
|
|
632
|
+
}
|
|
633
|
+
});
|
|
634
|
+
}
|
|
635
|
+
};
|
|
636
|
+
var ENTITY_TYPE_BY_PHASE = {
|
|
637
|
+
campaigns: CAMPAIGN_ENTITY,
|
|
638
|
+
lists: LIST_ENTITY,
|
|
639
|
+
automations: AUTOMATION_ENTITY
|
|
640
|
+
};
|
|
641
|
+
|
|
642
|
+
// src/index.ts
|
|
643
|
+
var index_default = MailchimpConnector;
|
|
644
|
+
export {
|
|
645
|
+
MailchimpConnector,
|
|
646
|
+
configFields,
|
|
647
|
+
index_default as default,
|
|
648
|
+
doc,
|
|
649
|
+
id,
|
|
650
|
+
mailchimpResources as resources
|
|
651
|
+
};
|
|
652
|
+
//# sourceMappingURL=index.js.map
|