perspectapi-ts-sdk 3.7.0 → 5.0.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "perspectapi-ts-sdk",
3
- "version": "3.7.0",
3
+ "version": "5.0.0",
4
4
  "description": "TypeScript SDK for PerspectAPI - Cloudflare Workers compatible",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -6,7 +6,6 @@ import { BaseClient } from './base-client';
6
6
  import type { CacheManager } from '../cache/cache-manager';
7
7
  import type { CachePolicy } from '../cache/types';
8
8
  import type {
9
- NewsletterSubscription,
10
9
  CreateNewsletterSubscriptionRequest,
11
10
  NewsletterList,
12
11
  NewsletterCampaignDetail,
@@ -17,7 +16,6 @@ import type {
17
16
  NewsletterConfirmResponse,
18
17
  NewsletterUnsubscribeRequest,
19
18
  NewsletterUnsubscribeResponse,
20
- PaginatedResponse,
21
19
  ApiResponse,
22
20
  } from '../types';
23
21
  import { validateOptionalLimit } from '../utils/validators';
@@ -304,246 +302,6 @@ export class NewsletterClient extends BaseClient {
304
302
  );
305
303
  }
306
304
 
307
- // Admin methods (require authentication)
308
-
309
- /**
310
- * Get all newsletter subscriptions (admin only)
311
- */
312
- async getSubscriptions(
313
- siteName: string,
314
- params?: {
315
- page?: number;
316
- limit?: number;
317
- status?: string;
318
- list_id?: string;
319
- search?: string;
320
- startDate?: string;
321
- endDate?: string;
322
- }
323
- ): Promise<PaginatedResponse<NewsletterSubscription>> {
324
- return this.getPaginated<NewsletterSubscription>(
325
- this.newsletterEndpoint(siteName, '/newsletter/subscriptions'),
326
- params
327
- );
328
- }
329
-
330
- /**
331
- * Get subscription by ID (admin only)
332
- */
333
- async getSubscriptionById(
334
- siteName: string,
335
- id: string
336
- ): Promise<ApiResponse<NewsletterSubscription>> {
337
- return this.getSingle(
338
- this.newsletterEndpoint(siteName, `/newsletter/subscriptions/${encodeURIComponent(id)}`)
339
- );
340
- }
341
-
342
- /**
343
- * Update subscription status (admin only)
344
- */
345
- async updateSubscriptionStatus(
346
- siteName: string,
347
- id: string,
348
- status: 'confirmed' | 'unsubscribed' | 'bounced' | 'complained',
349
- notes?: string
350
- ): Promise<ApiResponse<{ message: string }>> {
351
- return this.patch(
352
- this.newsletterEndpoint(siteName, `/newsletter/subscriptions/${encodeURIComponent(id)}`),
353
- { status, notes }
354
- );
355
- }
356
-
357
- /**
358
- * Delete subscription (admin only)
359
- */
360
- async deleteSubscription(
361
- siteName: string,
362
- id: string
363
- ): Promise<ApiResponse<{ message: string }>> {
364
- return this.delete<{ message: string }>(
365
- this.newsletterEndpoint(siteName, `/newsletter/subscriptions/${encodeURIComponent(id)}`)
366
- );
367
- }
368
-
369
- /**
370
- * Bulk update subscriptions (admin only)
371
- */
372
- async bulkUpdateSubscriptions(
373
- siteName: string,
374
- data: {
375
- ids: string[];
376
- action: 'confirm' | 'unsubscribe' | 'delete' | 'add_to_list' | 'remove_from_list';
377
- list_id?: string;
378
- }
379
- ): Promise<ApiResponse<{
380
- success: boolean;
381
- updatedCount: number;
382
- failedCount: number;
383
- }>> {
384
- return this.create(
385
- this.newsletterEndpoint(siteName, '/newsletter/subscriptions/bulk-update'),
386
- data
387
- );
388
- }
389
-
390
- /**
391
- * Create newsletter list (admin only)
392
- */
393
- async createList(
394
- siteName: string,
395
- data: {
396
- list_name: string;
397
- slug: string;
398
- description?: string;
399
- is_public?: boolean;
400
- is_default?: boolean;
401
- double_opt_in?: boolean;
402
- welcome_email_enabled?: boolean;
403
- }
404
- ): Promise<ApiResponse<NewsletterList>> {
405
- return this.create<any, NewsletterList>(
406
- this.newsletterEndpoint(siteName, '/newsletter/lists'),
407
- data
408
- );
409
- }
410
-
411
- /**
412
- * Update newsletter list (admin only)
413
- */
414
- async updateList(
415
- siteName: string,
416
- listId: string,
417
- data: Partial<{
418
- list_name: string;
419
- description: string;
420
- is_public: boolean;
421
- is_default: boolean;
422
- double_opt_in: boolean;
423
- welcome_email_enabled: boolean;
424
- }>
425
- ): Promise<ApiResponse<{ message: string }>> {
426
- return this.update(
427
- this.newsletterEndpoint(siteName, `/newsletter/lists/${encodeURIComponent(listId)}`),
428
- data
429
- );
430
- }
431
-
432
- /**
433
- * Delete newsletter list (admin only)
434
- */
435
- async deleteList(
436
- siteName: string,
437
- listId: string
438
- ): Promise<ApiResponse<{ message: string }>> {
439
- return this.delete<{ message: string }>(
440
- this.newsletterEndpoint(siteName, `/newsletter/lists/${encodeURIComponent(listId)}`)
441
- );
442
- }
443
-
444
- /**
445
- * Get newsletter statistics (admin only)
446
- */
447
- async getStatistics(
448
- siteName: string,
449
- params?: {
450
- startDate?: string;
451
- endDate?: string;
452
- list_id?: string;
453
- }
454
- ): Promise<ApiResponse<{
455
- totalSubscribers: number;
456
- confirmedSubscribers: number;
457
- pendingSubscribers: number;
458
- unsubscribedCount: number;
459
- bouncedCount: number;
460
- subscribersByDay: Array<{
461
- date: string;
462
- count: number;
463
- }>;
464
- subscribersByList: Array<{
465
- list_id: string;
466
- list_name: string;
467
- count: number;
468
- }>;
469
- engagementMetrics: {
470
- averageOpenRate: number;
471
- averageClickRate: number;
472
- };
473
- }>> {
474
- return this.http.get(
475
- this.buildPath(this.newsletterEndpoint(siteName, '/newsletter/statistics')),
476
- params
477
- );
478
- }
479
-
480
- /**
481
- * Export newsletter subscriptions (admin only)
482
- */
483
- async exportSubscriptions(
484
- siteName: string,
485
- params?: {
486
- format?: 'csv' | 'json' | 'xlsx';
487
- status?: string;
488
- list_id?: string;
489
- startDate?: string;
490
- endDate?: string;
491
- }
492
- ): Promise<ApiResponse<{
493
- downloadUrl: string;
494
- expiresAt: string;
495
- }>> {
496
- return this.create(
497
- this.newsletterEndpoint(siteName, '/newsletter/export'),
498
- params || {}
499
- );
500
- }
501
-
502
- /**
503
- * Import newsletter subscriptions (admin only)
504
- */
505
- async importSubscriptions(
506
- siteName: string,
507
- data: {
508
- subscriptions: Array<{
509
- email: string;
510
- name?: string;
511
- status?: string;
512
- lists?: string[];
513
- }>;
514
- skip_confirmation?: boolean;
515
- update_existing?: boolean;
516
- }
517
- ): Promise<ApiResponse<{
518
- imported: number;
519
- updated: number;
520
- failed: number;
521
- errors?: Array<{ email: string; error: string }>;
522
- }>> {
523
- return this.create(
524
- this.newsletterEndpoint(siteName, '/newsletter/import'),
525
- data
526
- );
527
- }
528
-
529
- /**
530
- * Send test newsletter (admin only)
531
- */
532
- async sendTestNewsletter(
533
- siteName: string,
534
- data: {
535
- to: string;
536
- subject: string;
537
- html_content: string;
538
- text_content?: string;
539
- }
540
- ): Promise<ApiResponse<{ message: string; sent: boolean }>> {
541
- return this.create(
542
- this.newsletterEndpoint(siteName, '/newsletter/test'),
543
- data
544
- );
545
- }
546
-
547
305
  // ============================================
548
306
  // Tracking methods (for first-party tracking)
549
307
  // ============================================
@@ -0,0 +1,426 @@
1
+ /**
2
+ * Newsletter management client for PerspectAPI SDK
3
+ * Strict management surface under /newsletter/management/*
4
+ */
5
+
6
+ import { BaseClient } from './base-client';
7
+ import type { CacheManager } from '../cache/cache-manager';
8
+ import type {
9
+ ApiResponse,
10
+ NewsletterCampaignTestSendRequest,
11
+ NewsletterCampaignTestSendResponse,
12
+ NewsletterExportCreateRequest,
13
+ NewsletterExportCreateResponse,
14
+ NewsletterManagementCampaign,
15
+ NewsletterManagementCampaignListResponse,
16
+ NewsletterManagementList,
17
+ NewsletterManagementSeries,
18
+ NewsletterManagementStatsResponse,
19
+ NewsletterManagementSubscription,
20
+ NewsletterManagementSubscriptionsListResponse,
21
+ NewsletterSubscriptionMembershipUpdateRequest,
22
+ NewsletterSubscriptionSyncRequest,
23
+ NewsletterSubscriptionSyncResponse,
24
+ NewsletterSubscriptionsBulkUpdateRequest,
25
+ NewsletterSubscriptionsBulkUpdateResponse,
26
+ NewsletterSubscriptionsImportRequest,
27
+ NewsletterSubscriptionsImportResponse,
28
+ } from '../types';
29
+
30
+ export class NewsletterManagementClient extends BaseClient {
31
+ constructor(http: any, cache?: CacheManager) {
32
+ super(http, '/api/v1', cache);
33
+ }
34
+
35
+ private managementEndpoint(siteName: string, endpoint: string): string {
36
+ return this.siteScopedEndpoint(
37
+ siteName,
38
+ `/newsletter/management${endpoint}`,
39
+ { includeSitesSegment: false },
40
+ );
41
+ }
42
+
43
+ async listSubscriptions(
44
+ siteName: string,
45
+ params?: {
46
+ page?: number;
47
+ limit?: number;
48
+ status?: 'pending' | 'confirmed' | 'unsubscribed' | 'bounced' | 'complained';
49
+ list_id?: string;
50
+ search?: string;
51
+ startDate?: string;
52
+ endDate?: string;
53
+ },
54
+ ): Promise<ApiResponse<NewsletterManagementSubscriptionsListResponse>> {
55
+ return this.http.get(
56
+ this.buildPath(this.managementEndpoint(siteName, '/subscriptions')),
57
+ params,
58
+ );
59
+ }
60
+
61
+ async getSubscriptionById(
62
+ siteName: string,
63
+ subscriptionId: string,
64
+ ): Promise<ApiResponse<NewsletterManagementSubscription>> {
65
+ return this.getSingle(
66
+ this.managementEndpoint(
67
+ siteName,
68
+ `/subscriptions/${encodeURIComponent(subscriptionId)}`,
69
+ ),
70
+ );
71
+ }
72
+
73
+ async getSubscriptionByEmail(
74
+ siteName: string,
75
+ email: string,
76
+ ): Promise<ApiResponse<NewsletterManagementSubscription>> {
77
+ return this.http.get(
78
+ this.buildPath(this.managementEndpoint(siteName, '/subscriptions/by-email')),
79
+ { email },
80
+ );
81
+ }
82
+
83
+ async syncSubscription(
84
+ siteName: string,
85
+ data: NewsletterSubscriptionSyncRequest,
86
+ ): Promise<ApiResponse<NewsletterSubscriptionSyncResponse>> {
87
+ return this.create(this.managementEndpoint(siteName, '/subscriptions/sync'), data);
88
+ }
89
+
90
+ async updateSubscription(
91
+ siteName: string,
92
+ subscriptionId: string,
93
+ data: Omit<NewsletterSubscriptionSyncRequest, 'email'>,
94
+ ): Promise<ApiResponse<NewsletterSubscriptionSyncResponse>> {
95
+ return this.patch(
96
+ this.managementEndpoint(
97
+ siteName,
98
+ `/subscriptions/${encodeURIComponent(subscriptionId)}`,
99
+ ),
100
+ data,
101
+ );
102
+ }
103
+
104
+ async deleteSubscription(
105
+ siteName: string,
106
+ subscriptionId: string,
107
+ ): Promise<ApiResponse<{ deleted: boolean; subscription_id: string }>> {
108
+ return this.delete(
109
+ this.managementEndpoint(
110
+ siteName,
111
+ `/subscriptions/${encodeURIComponent(subscriptionId)}`,
112
+ ),
113
+ );
114
+ }
115
+
116
+ async bulkUpdateSubscriptions(
117
+ siteName: string,
118
+ data: NewsletterSubscriptionsBulkUpdateRequest,
119
+ ): Promise<ApiResponse<NewsletterSubscriptionsBulkUpdateResponse>> {
120
+ return this.create(this.managementEndpoint(siteName, '/subscriptions/bulk'), data);
121
+ }
122
+
123
+ async updateSubscriptionListMembership(
124
+ siteName: string,
125
+ subscriptionId: string,
126
+ data: NewsletterSubscriptionMembershipUpdateRequest,
127
+ ): Promise<ApiResponse<{
128
+ subscription: NewsletterManagementSubscription;
129
+ mode: 'add' | 'remove' | 'replace';
130
+ list_ids: string[];
131
+ }>> {
132
+ return this.create(
133
+ this.managementEndpoint(
134
+ siteName,
135
+ `/subscriptions/${encodeURIComponent(subscriptionId)}/list-membership`,
136
+ ),
137
+ data,
138
+ );
139
+ }
140
+
141
+ async importSubscriptions(
142
+ siteName: string,
143
+ data: NewsletterSubscriptionsImportRequest,
144
+ ): Promise<ApiResponse<NewsletterSubscriptionsImportResponse>> {
145
+ return this.create(
146
+ this.managementEndpoint(siteName, '/subscriptions/import'),
147
+ data,
148
+ );
149
+ }
150
+
151
+ async listLists(
152
+ siteName: string,
153
+ params?: { status?: 'active' | 'archived' },
154
+ ): Promise<ApiResponse<{ items: NewsletterManagementList[]; total: number }>> {
155
+ return this.http.get(
156
+ this.buildPath(this.managementEndpoint(siteName, '/lists')),
157
+ params,
158
+ );
159
+ }
160
+
161
+ async getListById(
162
+ siteName: string,
163
+ listId: string,
164
+ ): Promise<ApiResponse<NewsletterManagementList>> {
165
+ return this.getSingle(
166
+ this.managementEndpoint(siteName, `/lists/${encodeURIComponent(listId)}`),
167
+ );
168
+ }
169
+
170
+ async createList(
171
+ siteName: string,
172
+ data: {
173
+ list_name: string;
174
+ slug: string;
175
+ description?: string | null;
176
+ is_public?: boolean;
177
+ is_default?: boolean;
178
+ double_opt_in?: boolean;
179
+ welcome_email_enabled?: boolean;
180
+ },
181
+ ): Promise<ApiResponse<NewsletterManagementList>> {
182
+ return this.create(this.managementEndpoint(siteName, '/lists'), data);
183
+ }
184
+
185
+ async updateList(
186
+ siteName: string,
187
+ listId: string,
188
+ data: Partial<{
189
+ list_name: string | null;
190
+ slug: string | null;
191
+ description: string | null;
192
+ is_public: boolean;
193
+ is_default: boolean;
194
+ double_opt_in: boolean;
195
+ welcome_email_enabled: boolean;
196
+ status: 'active' | 'archived';
197
+ }>,
198
+ ): Promise<ApiResponse<NewsletterManagementList>> {
199
+ return this.update(
200
+ this.managementEndpoint(siteName, `/lists/${encodeURIComponent(listId)}`),
201
+ data,
202
+ );
203
+ }
204
+
205
+ async deleteList(
206
+ siteName: string,
207
+ listId: string,
208
+ ): Promise<ApiResponse<{ deleted: boolean; list_id: string }>> {
209
+ return this.delete(
210
+ this.managementEndpoint(siteName, `/lists/${encodeURIComponent(listId)}`),
211
+ );
212
+ }
213
+
214
+ async listSeries(
215
+ siteName: string,
216
+ params?: { status?: 'active' | 'paused' | 'archived' },
217
+ ): Promise<ApiResponse<{ items: NewsletterManagementSeries[]; total: number }>> {
218
+ return this.http.get(
219
+ this.buildPath(this.managementEndpoint(siteName, '/series')),
220
+ params,
221
+ );
222
+ }
223
+
224
+ async getSeriesById(
225
+ siteName: string,
226
+ seriesId: string,
227
+ ): Promise<ApiResponse<NewsletterManagementSeries>> {
228
+ return this.getSingle(
229
+ this.managementEndpoint(siteName, `/series/${encodeURIComponent(seriesId)}`),
230
+ );
231
+ }
232
+
233
+ async createSeries(
234
+ siteName: string,
235
+ data: {
236
+ series_name: string;
237
+ description?: string | null;
238
+ status?: 'active' | 'paused' | 'archived';
239
+ cadence?: 'manual' | 'daily' | 'weekly' | 'monthly';
240
+ timezone?: string | null;
241
+ send_time?: string | null;
242
+ day_of_week?: number | null;
243
+ day_of_month?: number | null;
244
+ next_send_at?: string | null;
245
+ list_ids?: string[];
246
+ from_name?: string | null;
247
+ from_email?: string | null;
248
+ reply_to_email?: string | null;
249
+ subject_prefix?: string | null;
250
+ tags?: string[];
251
+ notes?: string | null;
252
+ },
253
+ ): Promise<ApiResponse<NewsletterManagementSeries>> {
254
+ return this.create(this.managementEndpoint(siteName, '/series'), data);
255
+ }
256
+
257
+ async updateSeries(
258
+ siteName: string,
259
+ seriesId: string,
260
+ data: Partial<{
261
+ series_name: string;
262
+ description?: string | null;
263
+ status?: 'active' | 'paused' | 'archived';
264
+ cadence?: 'manual' | 'daily' | 'weekly' | 'monthly';
265
+ timezone?: string | null;
266
+ send_time?: string | null;
267
+ day_of_week?: number | null;
268
+ day_of_month?: number | null;
269
+ next_send_at?: string | null;
270
+ list_ids?: string[];
271
+ from_name?: string | null;
272
+ from_email?: string | null;
273
+ reply_to_email?: string | null;
274
+ subject_prefix?: string | null;
275
+ tags?: string[];
276
+ notes?: string | null;
277
+ }>,
278
+ ): Promise<ApiResponse<NewsletterManagementSeries>> {
279
+ return this.update(
280
+ this.managementEndpoint(siteName, `/series/${encodeURIComponent(seriesId)}`),
281
+ data,
282
+ );
283
+ }
284
+
285
+ async deleteSeries(
286
+ siteName: string,
287
+ seriesId: string,
288
+ ): Promise<ApiResponse<{ deleted: boolean; series_id: string }>> {
289
+ return this.delete(
290
+ this.managementEndpoint(siteName, `/series/${encodeURIComponent(seriesId)}`),
291
+ );
292
+ }
293
+
294
+ async listCampaigns(
295
+ siteName: string,
296
+ params?: {
297
+ page?: number;
298
+ limit?: number;
299
+ status?: 'draft' | 'scheduled' | 'sending' | 'sent' | 'cancelled';
300
+ },
301
+ ): Promise<ApiResponse<NewsletterManagementCampaignListResponse>> {
302
+ return this.http.get(
303
+ this.buildPath(this.managementEndpoint(siteName, '/campaigns')),
304
+ params,
305
+ );
306
+ }
307
+
308
+ async getCampaignById(
309
+ siteName: string,
310
+ campaignId: string,
311
+ ): Promise<ApiResponse<NewsletterManagementCampaign>> {
312
+ return this.getSingle(
313
+ this.managementEndpoint(siteName, `/campaigns/${encodeURIComponent(campaignId)}`),
314
+ );
315
+ }
316
+
317
+ async createCampaign(
318
+ siteName: string,
319
+ data: {
320
+ campaign_name: string;
321
+ subject: string;
322
+ markdown_content: string;
323
+ slug?: string | null;
324
+ slug_prefix?: string | null;
325
+ status?: 'draft' | 'scheduled' | 'cancelled';
326
+ scheduled_at?: string | null;
327
+ preview_text?: string | null;
328
+ from_name?: string | null;
329
+ from_email?: string | null;
330
+ reply_to_email?: string | null;
331
+ template_id?: string | null;
332
+ series_id?: string | null;
333
+ list_ids?: string[];
334
+ tags?: string[];
335
+ notes?: string | null;
336
+ },
337
+ ): Promise<ApiResponse<NewsletterManagementCampaign>> {
338
+ return this.create(this.managementEndpoint(siteName, '/campaigns'), data);
339
+ }
340
+
341
+ async updateCampaign(
342
+ siteName: string,
343
+ campaignId: string,
344
+ data: Partial<{
345
+ campaign_name: string;
346
+ subject: string;
347
+ markdown_content: string;
348
+ slug?: string | null;
349
+ slug_prefix?: string | null;
350
+ status?: 'draft' | 'scheduled' | 'cancelled';
351
+ scheduled_at?: string | null;
352
+ preview_text?: string | null;
353
+ from_name?: string | null;
354
+ from_email?: string | null;
355
+ reply_to_email?: string | null;
356
+ template_id?: string | null;
357
+ series_id?: string | null;
358
+ list_ids?: string[];
359
+ tags?: string[];
360
+ notes?: string | null;
361
+ }>,
362
+ ): Promise<ApiResponse<NewsletterManagementCampaign>> {
363
+ return this.update(
364
+ this.managementEndpoint(siteName, `/campaigns/${encodeURIComponent(campaignId)}`),
365
+ data,
366
+ );
367
+ }
368
+
369
+ async deleteCampaign(
370
+ siteName: string,
371
+ campaignId: string,
372
+ ): Promise<ApiResponse<{ deleted: boolean; campaign_id: string }>> {
373
+ return this.delete(
374
+ this.managementEndpoint(siteName, `/campaigns/${encodeURIComponent(campaignId)}`),
375
+ );
376
+ }
377
+
378
+ async sendCampaignTest(
379
+ siteName: string,
380
+ campaignId: string,
381
+ data: NewsletterCampaignTestSendRequest,
382
+ ): Promise<ApiResponse<NewsletterCampaignTestSendResponse>> {
383
+ return this.create(
384
+ this.managementEndpoint(
385
+ siteName,
386
+ `/campaigns/${encodeURIComponent(campaignId)}/test-send`,
387
+ ),
388
+ data,
389
+ );
390
+ }
391
+
392
+ async getStats(
393
+ siteName: string,
394
+ params?: {
395
+ startDate?: string;
396
+ endDate?: string;
397
+ list_id?: string;
398
+ },
399
+ ): Promise<ApiResponse<NewsletterManagementStatsResponse>> {
400
+ return this.http.get(
401
+ this.buildPath(this.managementEndpoint(siteName, '/stats')),
402
+ params,
403
+ );
404
+ }
405
+
406
+ async createExport(
407
+ siteName: string,
408
+ data: NewsletterExportCreateRequest,
409
+ ): Promise<ApiResponse<NewsletterExportCreateResponse>> {
410
+ return this.create(
411
+ this.managementEndpoint(siteName, '/exports'),
412
+ data,
413
+ );
414
+ }
415
+
416
+ async downloadExport(
417
+ siteName: string,
418
+ exportId: string,
419
+ ): Promise<ApiResponse<string>> {
420
+ return this.http.get(
421
+ this.buildPath(
422
+ this.managementEndpoint(siteName, `/exports/${encodeURIComponent(exportId)}/download`),
423
+ ),
424
+ );
425
+ }
426
+ }