perspectapi-ts-sdk 4.1.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/dist/index.js CHANGED
@@ -37,6 +37,8 @@ __export(index_exports, {
37
37
  NoopCacheAdapter: () => NoopCacheAdapter,
38
38
  OrganizationsClient: () => OrganizationsClient,
39
39
  PerspectApiClient: () => PerspectApiClient,
40
+ PerspectApiV2Client: () => PerspectApiV2Client,
41
+ PerspectV2Error: () => PerspectV2Error,
40
42
  ProductsClient: () => ProductsClient,
41
43
  SiteUsersClient: () => SiteUsersClient,
42
44
  SitesClient: () => SitesClient,
@@ -45,6 +47,7 @@ __export(index_exports, {
45
47
  createApiError: () => createApiError,
46
48
  createCheckoutSession: () => createCheckoutSession,
47
49
  createPerspectApiClient: () => createPerspectApiClient,
50
+ createPerspectApiV2Client: () => createPerspectApiV2Client,
48
51
  default: () => perspect_api_client_default,
49
52
  generateResponsiveImageHtml: () => generateResponsiveImageHtml,
50
53
  generateResponsiveUrls: () => generateResponsiveUrls,
@@ -2784,15 +2787,16 @@ var SiteUsersClient = class extends BaseClient {
2784
2787
  );
2785
2788
  }
2786
2789
  /**
2787
- * Cancel a subscription (marks for cancellation at period end)
2790
+ * Cancel a subscription.
2788
2791
  * @param siteName - The site name
2789
2792
  * @param id - Subscription ID
2790
2793
  * @param csrfToken - CSRF token (required)
2794
+ * @param options - Cancellation mode/details
2791
2795
  */
2792
- async cancelSubscription(siteName, id, csrfToken) {
2796
+ async cancelSubscription(siteName, id, csrfToken, options) {
2793
2797
  return this.create(
2794
2798
  this.siteUserEndpoint(siteName, `/users/me/subscriptions/${encodeURIComponent(id)}/cancel`),
2795
- {},
2799
+ options ?? {},
2796
2800
  csrfToken
2797
2801
  );
2798
2802
  }
@@ -3022,11 +3026,12 @@ var SiteUsersClient = class extends BaseClient {
3022
3026
  * @param siteName - The site name
3023
3027
  * @param userId - User ID
3024
3028
  * @param subscriptionId - Subscription ID
3029
+ * @param options - Cancellation mode/details
3025
3030
  */
3026
- async cancelUserSubscription(siteName, userId, subscriptionId) {
3031
+ async cancelUserSubscription(siteName, userId, subscriptionId, options) {
3027
3032
  return this.create(
3028
3033
  this.siteUserEndpoint(siteName, `/users/${encodeURIComponent(userId)}/subscriptions/${encodeURIComponent(subscriptionId)}/cancel`),
3029
- {}
3034
+ options ?? {}
3030
3035
  );
3031
3036
  }
3032
3037
  /**
@@ -3319,6 +3324,440 @@ function createPerspectApiClient(config) {
3319
3324
  }
3320
3325
  var perspect_api_client_default = PerspectApiClient;
3321
3326
 
3327
+ // src/v2/client/base-v2-client.ts
3328
+ var PerspectV2Error = class extends Error {
3329
+ type;
3330
+ code;
3331
+ param;
3332
+ status;
3333
+ constructor(error, status) {
3334
+ super(error.message);
3335
+ this.name = "PerspectV2Error";
3336
+ this.type = error.type;
3337
+ this.code = error.code;
3338
+ this.param = error.param;
3339
+ this.status = status;
3340
+ }
3341
+ };
3342
+ var BaseV2Client = class {
3343
+ http;
3344
+ basePath;
3345
+ constructor(http, basePath) {
3346
+ this.http = http;
3347
+ this.basePath = basePath;
3348
+ }
3349
+ buildPath(endpoint) {
3350
+ const clean = endpoint.replace(/^\//, "");
3351
+ return clean ? `${this.basePath}/${clean}` : this.basePath;
3352
+ }
3353
+ sitePath(siteName, resource, suffix = "") {
3354
+ const encoded = encodeURIComponent(siteName.trim());
3355
+ const cleanSuffix = suffix.replace(/^\//, "");
3356
+ const end = cleanSuffix ? `/${cleanSuffix}` : "";
3357
+ return `/sites/${encoded}/${resource}${end}`;
3358
+ }
3359
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
3360
+ toParams(params) {
3361
+ if (!params) return void 0;
3362
+ const out = {};
3363
+ for (const [k, v] of Object.entries(params)) {
3364
+ if (v !== void 0 && v !== null) out[k] = String(v);
3365
+ }
3366
+ return Object.keys(out).length > 0 ? out : void 0;
3367
+ }
3368
+ /** GET a single resource. */
3369
+ async getOne(path, params) {
3370
+ const response = await this.http.get(path, this.toParams(params));
3371
+ if (!response.success) {
3372
+ throw this.toError(response);
3373
+ }
3374
+ return response.data;
3375
+ }
3376
+ /** GET a list of resources with cursor pagination. */
3377
+ async getList(path, params) {
3378
+ const response = await this.http.get(path, this.toParams(params));
3379
+ if (!response.success) {
3380
+ throw this.toError(response);
3381
+ }
3382
+ return response.data;
3383
+ }
3384
+ /** POST to create a resource. */
3385
+ async post(path, body) {
3386
+ const response = await this.http.post(path, body);
3387
+ if (!response.success) {
3388
+ throw this.toError(response);
3389
+ }
3390
+ return response.data;
3391
+ }
3392
+ /** PATCH to update a resource. */
3393
+ async patchOne(path, body) {
3394
+ const response = await this.http.patch(path, body);
3395
+ if (!response.success) {
3396
+ throw this.toError(response);
3397
+ }
3398
+ return response.data;
3399
+ }
3400
+ /** DELETE a resource. */
3401
+ async deleteOne(path) {
3402
+ const response = await this.http.delete(path);
3403
+ if (!response.success) {
3404
+ throw this.toError(response);
3405
+ }
3406
+ return response.data;
3407
+ }
3408
+ /**
3409
+ * Auto-paginating async generator.
3410
+ * Yields every item across all pages.
3411
+ *
3412
+ * Usage:
3413
+ * for await (const item of client.listAutoPaginate(path, params)) { ... }
3414
+ */
3415
+ async *listAutoPaginate(path, params) {
3416
+ let startingAfter;
3417
+ let hasMore = true;
3418
+ while (hasMore) {
3419
+ const queryParams = { ...params };
3420
+ if (startingAfter) queryParams.starting_after = startingAfter;
3421
+ const page = await this.getList(path, queryParams);
3422
+ for (const item of page.data) {
3423
+ yield item;
3424
+ }
3425
+ hasMore = page.has_more;
3426
+ if (page.data.length > 0) {
3427
+ startingAfter = page.data[page.data.length - 1].id;
3428
+ } else {
3429
+ hasMore = false;
3430
+ }
3431
+ }
3432
+ }
3433
+ toError(response) {
3434
+ const data = response.data;
3435
+ const errorObj = data?.error ?? response.error;
3436
+ if (errorObj && typeof errorObj === "object" && "type" in errorObj) {
3437
+ return new PerspectV2Error(errorObj, 400);
3438
+ }
3439
+ return new PerspectV2Error(
3440
+ { type: "api_error", code: "unknown", message: response.message ?? "Unknown error" },
3441
+ 500
3442
+ );
3443
+ }
3444
+ };
3445
+
3446
+ // src/v2/client/content-client.ts
3447
+ var ContentV2Client = class extends BaseV2Client {
3448
+ async list(siteName, params) {
3449
+ return this.getList(this.sitePath(siteName, "content"), params);
3450
+ }
3451
+ async *listAutoPaginated(siteName, params) {
3452
+ yield* this.listAutoPaginate(this.sitePath(siteName, "content"), params);
3453
+ }
3454
+ async get(siteName, idOrSlug) {
3455
+ return this.getOne(this.sitePath(siteName, "content", idOrSlug));
3456
+ }
3457
+ async create(siteName, data) {
3458
+ return this.post(this.sitePath(siteName, "content"), data);
3459
+ }
3460
+ async update(siteName, id, data) {
3461
+ return this.patchOne(this.sitePath(siteName, "content", id), data);
3462
+ }
3463
+ async del(siteName, id) {
3464
+ return this.deleteOne(this.sitePath(siteName, "content", id));
3465
+ }
3466
+ async publish(siteName, id) {
3467
+ return this.post(this.sitePath(siteName, "content", `${id}/publish`));
3468
+ }
3469
+ async unpublish(siteName, id) {
3470
+ return this.post(this.sitePath(siteName, "content", `${id}/unpublish`));
3471
+ }
3472
+ };
3473
+
3474
+ // src/v2/client/products-client.ts
3475
+ var ProductsV2Client = class extends BaseV2Client {
3476
+ async list(siteName, params) {
3477
+ return this.getList(this.sitePath(siteName, "products"), params);
3478
+ }
3479
+ async *listAutoPaginated(siteName, params) {
3480
+ yield* this.listAutoPaginate(this.sitePath(siteName, "products"), params);
3481
+ }
3482
+ async get(siteName, idOrSlug) {
3483
+ return this.getOne(this.sitePath(siteName, "products", idOrSlug));
3484
+ }
3485
+ async create(siteName, data) {
3486
+ return this.post(this.sitePath(siteName, "products"), data);
3487
+ }
3488
+ async update(siteName, id, data) {
3489
+ return this.patchOne(this.sitePath(siteName, "products", id), data);
3490
+ }
3491
+ async del(siteName, id) {
3492
+ return this.deleteOne(this.sitePath(siteName, "products", id));
3493
+ }
3494
+ };
3495
+
3496
+ // src/v2/client/categories-client.ts
3497
+ var CategoriesV2Client = class extends BaseV2Client {
3498
+ async list(siteName, params) {
3499
+ return this.getList(this.sitePath(siteName, "categories"), params);
3500
+ }
3501
+ async get(siteName, id) {
3502
+ return this.getOne(this.sitePath(siteName, "categories", id));
3503
+ }
3504
+ async create(siteName, data) {
3505
+ return this.post(this.sitePath(siteName, "categories"), data);
3506
+ }
3507
+ async update(siteName, id, data) {
3508
+ return this.patchOne(this.sitePath(siteName, "categories", id), data);
3509
+ }
3510
+ async del(siteName, id) {
3511
+ return this.deleteOne(this.sitePath(siteName, "categories", id));
3512
+ }
3513
+ };
3514
+
3515
+ // src/v2/client/collections-client.ts
3516
+ var CollectionsV2Client = class extends BaseV2Client {
3517
+ async list(siteName, params) {
3518
+ return this.getList(this.sitePath(siteName, "collections"), params);
3519
+ }
3520
+ async getCurrent(siteName) {
3521
+ const result = await this.getOne(
3522
+ this.sitePath(siteName, "collections", "current")
3523
+ );
3524
+ return result ?? null;
3525
+ }
3526
+ async get(siteName, id) {
3527
+ return this.getOne(this.sitePath(siteName, "collections", id));
3528
+ }
3529
+ async create(siteName, data) {
3530
+ return this.post(this.sitePath(siteName, "collections"), data);
3531
+ }
3532
+ async update(siteName, id, data) {
3533
+ return this.patchOne(this.sitePath(siteName, "collections", id), data);
3534
+ }
3535
+ async del(siteName, id) {
3536
+ return this.deleteOne(this.sitePath(siteName, "collections", id));
3537
+ }
3538
+ // --- Items ---
3539
+ async listItems(siteName, collectionId) {
3540
+ return this.getList(
3541
+ this.sitePath(siteName, "collections", `${collectionId}/items`)
3542
+ );
3543
+ }
3544
+ async addItem(siteName, collectionId, data) {
3545
+ return this.post(
3546
+ this.sitePath(siteName, "collections", `${collectionId}/items`),
3547
+ data
3548
+ );
3549
+ }
3550
+ async removeItem(siteName, collectionId, itemId) {
3551
+ return this.deleteOne(
3552
+ this.sitePath(siteName, "collections", `${collectionId}/items/${itemId}`)
3553
+ );
3554
+ }
3555
+ };
3556
+
3557
+ // src/v2/client/orders-client.ts
3558
+ var OrdersV2Client = class extends BaseV2Client {
3559
+ async list(siteName, params) {
3560
+ return this.getList(this.sitePath(siteName, "orders"), params);
3561
+ }
3562
+ async *listAutoPaginated(siteName, params) {
3563
+ yield* this.listAutoPaginate(this.sitePath(siteName, "orders"), params);
3564
+ }
3565
+ async get(siteName, id) {
3566
+ return this.getOne(this.sitePath(siteName, "orders", id));
3567
+ }
3568
+ };
3569
+
3570
+ // src/v2/client/site-users-client.ts
3571
+ var SiteUsersV2Client = class extends BaseV2Client {
3572
+ // --- OTP Auth ---
3573
+ async requestOtp(siteName, data) {
3574
+ return this.post(this.sitePath(siteName, "users", "request-otp"), data);
3575
+ }
3576
+ async verifyOtp(siteName, data) {
3577
+ return this.post(this.sitePath(siteName, "users", "verify-otp"), data);
3578
+ }
3579
+ // --- Admin ---
3580
+ async list(siteName, params) {
3581
+ return this.getList(this.sitePath(siteName, "users"), params);
3582
+ }
3583
+ async *listAutoPaginated(siteName, params) {
3584
+ yield* this.listAutoPaginate(this.sitePath(siteName, "users"), params);
3585
+ }
3586
+ async get(siteName, id) {
3587
+ return this.getOne(this.sitePath(siteName, "users", id));
3588
+ }
3589
+ async update(siteName, id, data) {
3590
+ return this.patchOne(this.sitePath(siteName, "users", id), data);
3591
+ }
3592
+ };
3593
+
3594
+ // src/v2/client/newsletter-client.ts
3595
+ var NewsletterV2Client = class extends BaseV2Client {
3596
+ // --- Subscribe / Unsubscribe ---
3597
+ async subscribe(siteName, data) {
3598
+ return this.post(
3599
+ this.sitePath(siteName, "newsletter", "subscribe"),
3600
+ data
3601
+ );
3602
+ }
3603
+ async confirm(siteName, token) {
3604
+ return this.getOne(
3605
+ this.sitePath(siteName, "newsletter", `confirm/${token}`)
3606
+ );
3607
+ }
3608
+ async unsubscribe(siteName, data) {
3609
+ return this.post(
3610
+ this.sitePath(siteName, "newsletter", "unsubscribe"),
3611
+ data
3612
+ );
3613
+ }
3614
+ // --- Subscriptions (admin) ---
3615
+ async listSubscriptions(siteName, params) {
3616
+ return this.getList(
3617
+ this.sitePath(siteName, "newsletter", "subscriptions"),
3618
+ params
3619
+ );
3620
+ }
3621
+ async getSubscription(siteName, id) {
3622
+ return this.getOne(
3623
+ this.sitePath(siteName, "newsletter", `subscriptions/${id}`)
3624
+ );
3625
+ }
3626
+ // --- Lists ---
3627
+ async listLists(siteName) {
3628
+ return this.getList(
3629
+ this.sitePath(siteName, "newsletter", "lists")
3630
+ );
3631
+ }
3632
+ // --- Campaigns ---
3633
+ async listCampaigns(siteName, params) {
3634
+ return this.getList(
3635
+ this.sitePath(siteName, "newsletter", "campaigns"),
3636
+ params
3637
+ );
3638
+ }
3639
+ async getCampaign(siteName, idOrSlug) {
3640
+ return this.getOne(
3641
+ this.sitePath(siteName, "newsletter", `campaigns/${idOrSlug}`)
3642
+ );
3643
+ }
3644
+ };
3645
+
3646
+ // src/v2/client/contacts-client.ts
3647
+ var ContactsV2Client = class extends BaseV2Client {
3648
+ async submit(siteName, data) {
3649
+ return this.post(this.sitePath(siteName, "contacts"), data);
3650
+ }
3651
+ async list(siteName, params) {
3652
+ return this.getList(this.sitePath(siteName, "contacts"), params);
3653
+ }
3654
+ async get(siteName, id) {
3655
+ return this.getOne(this.sitePath(siteName, "contacts", id));
3656
+ }
3657
+ };
3658
+
3659
+ // src/v2/client/organizations-client.ts
3660
+ var OrganizationsV2Client = class extends BaseV2Client {
3661
+ async list() {
3662
+ return this.getList("/organizations");
3663
+ }
3664
+ async get(id) {
3665
+ return this.getOne(`/organizations/${id}`);
3666
+ }
3667
+ };
3668
+
3669
+ // src/v2/client/sites-client.ts
3670
+ var SitesV2Client = class extends BaseV2Client {
3671
+ async list(params) {
3672
+ return this.getList("/sites", params);
3673
+ }
3674
+ async get(name) {
3675
+ return this.getOne(`/sites/${encodeURIComponent(name)}`);
3676
+ }
3677
+ };
3678
+
3679
+ // src/v2/client/api-keys-client.ts
3680
+ var ApiKeysV2Client = class extends BaseV2Client {
3681
+ async list(params) {
3682
+ return this.getList("/api-keys", params);
3683
+ }
3684
+ async get(id) {
3685
+ return this.getOne(`/api-keys/${id}`);
3686
+ }
3687
+ async del(id) {
3688
+ return this.deleteOne(`/api-keys/${id}`);
3689
+ }
3690
+ };
3691
+
3692
+ // src/v2/client/webhooks-client.ts
3693
+ var WebhooksV2Client = class extends BaseV2Client {
3694
+ async list(siteName, params) {
3695
+ return this.getList(this.sitePath(siteName, "webhooks"), params);
3696
+ }
3697
+ async get(siteName, id) {
3698
+ return this.getOne(this.sitePath(siteName, "webhooks", id));
3699
+ }
3700
+ async create(siteName, data) {
3701
+ return this.post(this.sitePath(siteName, "webhooks"), data);
3702
+ }
3703
+ async update(siteName, id, data) {
3704
+ return this.patchOne(this.sitePath(siteName, "webhooks", id), data);
3705
+ }
3706
+ async del(siteName, id) {
3707
+ return this.deleteOne(this.sitePath(siteName, "webhooks", id));
3708
+ }
3709
+ };
3710
+
3711
+ // src/v2/index.ts
3712
+ var PerspectApiV2Client = class {
3713
+ http;
3714
+ content;
3715
+ products;
3716
+ categories;
3717
+ collections;
3718
+ orders;
3719
+ siteUsers;
3720
+ newsletter;
3721
+ contacts;
3722
+ organizations;
3723
+ sites;
3724
+ apiKeys;
3725
+ webhooks;
3726
+ constructor(config) {
3727
+ const baseUrl = config.baseUrl.replace(/\/+$/, "");
3728
+ const v2BaseUrl = baseUrl.endsWith("/api/v2") ? baseUrl : `${baseUrl}/api/v2`;
3729
+ this.http = new HttpClient({ ...config, baseUrl: v2BaseUrl });
3730
+ const basePath = "";
3731
+ this.content = new ContentV2Client(this.http, basePath);
3732
+ this.products = new ProductsV2Client(this.http, basePath);
3733
+ this.categories = new CategoriesV2Client(this.http, basePath);
3734
+ this.collections = new CollectionsV2Client(this.http, basePath);
3735
+ this.orders = new OrdersV2Client(this.http, basePath);
3736
+ this.siteUsers = new SiteUsersV2Client(this.http, basePath);
3737
+ this.newsletter = new NewsletterV2Client(this.http, basePath);
3738
+ this.contacts = new ContactsV2Client(this.http, basePath);
3739
+ this.organizations = new OrganizationsV2Client(this.http, basePath);
3740
+ this.sites = new SitesV2Client(this.http, basePath);
3741
+ this.apiKeys = new ApiKeysV2Client(this.http, basePath);
3742
+ this.webhooks = new WebhooksV2Client(this.http, basePath);
3743
+ }
3744
+ /** Update the JWT token for authenticated requests. */
3745
+ setAuth(jwt) {
3746
+ this.http.setAuth(jwt);
3747
+ }
3748
+ /** Update the API key. */
3749
+ setApiKey(apiKey) {
3750
+ this.http.setApiKey(apiKey);
3751
+ }
3752
+ /** Clear authentication. */
3753
+ clearAuth() {
3754
+ this.http.clearAuth();
3755
+ }
3756
+ };
3757
+ function createPerspectApiV2Client(config) {
3758
+ return new PerspectApiV2Client(config);
3759
+ }
3760
+
3322
3761
  // src/utils/image-transform.ts
3323
3762
  var DEFAULT_IMAGE_SIZES = {
3324
3763
  thumbnail: {
@@ -3944,6 +4383,8 @@ async function createCheckoutSession(options) {
3944
4383
  NoopCacheAdapter,
3945
4384
  OrganizationsClient,
3946
4385
  PerspectApiClient,
4386
+ PerspectApiV2Client,
4387
+ PerspectV2Error,
3947
4388
  ProductsClient,
3948
4389
  SiteUsersClient,
3949
4390
  SitesClient,
@@ -3952,6 +4393,7 @@ async function createCheckoutSession(options) {
3952
4393
  createApiError,
3953
4394
  createCheckoutSession,
3954
4395
  createPerspectApiClient,
4396
+ createPerspectApiV2Client,
3955
4397
  generateResponsiveImageHtml,
3956
4398
  generateResponsiveUrls,
3957
4399
  generateSizesAttribute,