payload-plugin-newsletter 0.16.6 → 0.16.8

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/dist/index.cjs CHANGED
@@ -262,29 +262,46 @@ var init_broadcast2 = __esm({
262
262
  async create(data) {
263
263
  try {
264
264
  this.validateRequiredFields(data, ["name", "subject", "content"]);
265
+ const requestBody = {
266
+ broadcast: {
267
+ name: data.name,
268
+ subject: data.subject,
269
+ preheader: data.preheader,
270
+ body: data.content,
271
+ html_body: true,
272
+ track_opens: data.trackOpens ?? true,
273
+ track_clicks: data.trackClicks ?? true,
274
+ reply_to: data.replyTo,
275
+ segment_ids: data.audienceIds
276
+ }
277
+ };
278
+ console.log("[BroadcastApiProvider] Creating broadcast:", {
279
+ url: `${this.apiUrl}/api/v1/broadcasts`,
280
+ method: "POST",
281
+ hasToken: !!this.token,
282
+ tokenLength: this.token?.length,
283
+ body: JSON.stringify(requestBody, null, 2)
284
+ });
265
285
  const response = await fetch(`${this.apiUrl}/api/v1/broadcasts`, {
266
286
  method: "POST",
267
287
  headers: {
268
288
  "Authorization": `Bearer ${this.token}`,
269
289
  "Content-Type": "application/json"
270
290
  },
271
- body: JSON.stringify({
272
- broadcast: {
273
- name: data.name,
274
- subject: data.subject,
275
- preheader: data.preheader,
276
- body: data.content,
277
- html_body: true,
278
- track_opens: data.trackOpens ?? true,
279
- track_clicks: data.trackClicks ?? true,
280
- reply_to: data.replyTo,
281
- segment_ids: data.audienceIds
282
- }
283
- })
291
+ body: JSON.stringify(requestBody)
284
292
  });
293
+ console.log("[BroadcastApiProvider] Response status:", response.status);
294
+ console.log("[BroadcastApiProvider] Response headers:", Object.fromEntries(response.headers.entries()));
285
295
  if (!response.ok) {
286
- const error = await response.text();
287
- throw new Error(`Broadcast API error: ${response.status} - ${error}`);
296
+ const errorText = await response.text();
297
+ console.error("[BroadcastApiProvider] Error response body:", errorText);
298
+ let errorDetails;
299
+ try {
300
+ errorDetails = JSON.parse(errorText);
301
+ console.error("[BroadcastApiProvider] Parsed error:", errorDetails);
302
+ } catch {
303
+ }
304
+ throw new Error(`Broadcast API error: ${response.status} - ${errorText}`);
288
305
  }
289
306
  const result = await response.json();
290
307
  return this.get(result.id.toString());
@@ -3171,6 +3188,9 @@ var EMAIL_SAFE_CONFIG = {
3171
3188
  FORBID_ATTR: ["class", "id", "onclick", "onload", "onerror"]
3172
3189
  };
3173
3190
  async function convertToEmailSafeHtml(editorState, options) {
3191
+ if (!editorState) {
3192
+ return "";
3193
+ }
3174
3194
  const rawHtml = await lexicalToEmailHtml(editorState, options?.mediaUrl);
3175
3195
  const sanitizedHtml = import_isomorphic_dompurify2.default.sanitize(rawHtml, EMAIL_SAFE_CONFIG);
3176
3196
  if (options?.wrapInTemplate) {
@@ -4377,6 +4397,10 @@ var createBroadcastsCollection = (pluginConfig) => {
4377
4397
  async ({ doc, operation, req, previousDoc }) => {
4378
4398
  if (!hasProviders) return doc;
4379
4399
  if (operation === "create") {
4400
+ if (!doc.subject || !doc.contentSection?.content) {
4401
+ req.payload.logger.info("Skipping provider sync - broadcast has no subject or content yet");
4402
+ return doc;
4403
+ }
4380
4404
  try {
4381
4405
  const providerConfig = await getBroadcastConfig(req, pluginConfig);
4382
4406
  if (!providerConfig || !providerConfig.token) {
@@ -4385,8 +4409,13 @@ var createBroadcastsCollection = (pluginConfig) => {
4385
4409
  }
4386
4410
  const { BroadcastApiProvider: BroadcastApiProvider2 } = await Promise.resolve().then(() => (init_broadcast2(), broadcast_exports));
4387
4411
  const provider = new BroadcastApiProvider2(providerConfig);
4412
+ req.payload.logger.info("Converting content to HTML...");
4388
4413
  const htmlContent = await convertToEmailSafeHtml(doc.contentSection?.content);
4389
- const providerBroadcast = await provider.create({
4414
+ if (!htmlContent || htmlContent.trim() === "") {
4415
+ req.payload.logger.info("Skipping provider sync - content is empty after conversion");
4416
+ return doc;
4417
+ }
4418
+ const createData = {
4390
4419
  name: doc.subject,
4391
4420
  // Use subject as name since we removed the name field
4392
4421
  subject: doc.subject,
@@ -4396,7 +4425,21 @@ var createBroadcastsCollection = (pluginConfig) => {
4396
4425
  trackClicks: doc.settings?.trackClicks,
4397
4426
  replyTo: doc.settings?.replyTo || providerConfig.replyTo,
4398
4427
  audienceIds: doc.audienceIds?.map((a) => a.audienceId)
4428
+ };
4429
+ req.payload.logger.info("Creating broadcast with data:", {
4430
+ name: createData.name,
4431
+ subject: createData.subject,
4432
+ preheader: createData.preheader || "NONE",
4433
+ contentLength: htmlContent ? htmlContent.length : 0,
4434
+ contentPreview: htmlContent ? htmlContent.substring(0, 100) + "..." : "EMPTY",
4435
+ trackOpens: createData.trackOpens,
4436
+ trackClicks: createData.trackClicks,
4437
+ replyTo: createData.replyTo,
4438
+ audienceIds: createData.audienceIds || [],
4439
+ apiUrl: providerConfig.apiUrl,
4440
+ hasToken: !!providerConfig.token
4399
4441
  });
4442
+ const providerBroadcast = await provider.create(createData);
4400
4443
  await req.payload.update({
4401
4444
  collection: "broadcasts",
4402
4445
  id: doc.id,
@@ -4412,17 +4455,34 @@ var createBroadcastsCollection = (pluginConfig) => {
4412
4455
  providerData: providerBroadcast.providerData
4413
4456
  };
4414
4457
  } catch (error) {
4458
+ req.payload.logger.error("Raw error from broadcast provider:");
4459
+ req.payload.logger.error(error);
4415
4460
  if (error instanceof Error) {
4416
- req.payload.logger.error("Failed to create broadcast in provider:", {
4461
+ req.payload.logger.error("Error is instance of Error:", {
4417
4462
  message: error.message,
4418
4463
  stack: error.stack,
4419
4464
  name: error.name,
4420
4465
  // If it's a BroadcastProviderError, it might have additional details
4421
- ...error.details
4466
+ ...error.details,
4467
+ // Check if it's a fetch response error
4468
+ ...error.response,
4469
+ ...error.data,
4470
+ ...error.status,
4471
+ ...error.statusText
4422
4472
  });
4473
+ } else if (typeof error === "string") {
4474
+ req.payload.logger.error("Error is a string:", error);
4475
+ } else if (error && typeof error === "object") {
4476
+ req.payload.logger.error("Error is an object:", JSON.stringify(error, null, 2));
4423
4477
  } else {
4424
- req.payload.logger.error("Failed to create broadcast in provider:", error);
4478
+ req.payload.logger.error("Unknown error type:", typeof error);
4425
4479
  }
4480
+ req.payload.logger.error("Failed broadcast document:", {
4481
+ id: doc.id,
4482
+ subject: doc.subject,
4483
+ hasContent: !!doc.contentSection?.content,
4484
+ contentType: doc.contentSection?.content ? typeof doc.contentSection.content : "none"
4485
+ });
4426
4486
  return doc;
4427
4487
  }
4428
4488
  }