perspectapi-ts-sdk 4.1.0 → 5.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/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,446 @@ 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
+ /**
3369
+ * Extract v2 payload from HttpClient response.
3370
+ *
3371
+ * The shared HttpClient wraps responses in a v1-style { success, data } envelope.
3372
+ * v2 API responses don't have a `success` field — the HttpClient already throws
3373
+ * on HTTP errors, so if we reach this point the request succeeded.
3374
+ *
3375
+ * The HttpClient may return the v2 payload nested under `data` (when the response
3376
+ * JSON contains a `data` key, which v2 list responses do) or as a direct wrap.
3377
+ * We unwrap accordingly.
3378
+ */
3379
+ extractData(response) {
3380
+ const payload = response.data;
3381
+ if (payload && typeof payload === "object" && "error" in payload) {
3382
+ const errObj = payload.error;
3383
+ if (errObj && typeof errObj === "object" && "type" in errObj) {
3384
+ throw this.toError(response);
3385
+ }
3386
+ }
3387
+ return payload;
3388
+ }
3389
+ /** GET a single resource. */
3390
+ async getOne(path, params) {
3391
+ const response = await this.http.get(path, this.toParams(params));
3392
+ return this.extractData(response);
3393
+ }
3394
+ /** GET a list of resources with cursor pagination. */
3395
+ async getList(path, params) {
3396
+ const response = await this.http.get(path, this.toParams(params));
3397
+ return this.extractData(response);
3398
+ }
3399
+ /** POST to create a resource. */
3400
+ async post(path, body) {
3401
+ const response = await this.http.post(path, body);
3402
+ return this.extractData(response);
3403
+ }
3404
+ /** PATCH to update a resource. */
3405
+ async patchOne(path, body) {
3406
+ const response = await this.http.patch(path, body);
3407
+ return this.extractData(response);
3408
+ }
3409
+ /** DELETE a resource. */
3410
+ async deleteOne(path) {
3411
+ const response = await this.http.delete(path);
3412
+ return this.extractData(response);
3413
+ }
3414
+ /**
3415
+ * Auto-paginating async generator.
3416
+ * Yields every item across all pages.
3417
+ *
3418
+ * Usage:
3419
+ * for await (const item of client.listAutoPaginate(path, params)) { ... }
3420
+ */
3421
+ async *listAutoPaginate(path, params) {
3422
+ let startingAfter;
3423
+ let hasMore = true;
3424
+ while (hasMore) {
3425
+ const queryParams = { ...params };
3426
+ if (startingAfter) queryParams.starting_after = startingAfter;
3427
+ const page = await this.getList(path, queryParams);
3428
+ for (const item of page.data) {
3429
+ yield item;
3430
+ }
3431
+ hasMore = page.has_more;
3432
+ if (page.data.length > 0) {
3433
+ startingAfter = page.data[page.data.length - 1].id;
3434
+ } else {
3435
+ hasMore = false;
3436
+ }
3437
+ }
3438
+ }
3439
+ toError(response) {
3440
+ const data = response.data;
3441
+ const errorObj = data?.error ?? response.error;
3442
+ if (errorObj && typeof errorObj === "object" && "type" in errorObj) {
3443
+ return new PerspectV2Error(errorObj, 400);
3444
+ }
3445
+ return new PerspectV2Error(
3446
+ { type: "api_error", code: "unknown", message: response.message ?? "Unknown error" },
3447
+ 500
3448
+ );
3449
+ }
3450
+ };
3451
+
3452
+ // src/v2/client/content-client.ts
3453
+ var ContentV2Client = class extends BaseV2Client {
3454
+ async list(siteName, params) {
3455
+ return this.getList(this.sitePath(siteName, "content"), params);
3456
+ }
3457
+ async *listAutoPaginated(siteName, params) {
3458
+ yield* this.listAutoPaginate(this.sitePath(siteName, "content"), params);
3459
+ }
3460
+ async get(siteName, idOrSlug) {
3461
+ return this.getOne(this.sitePath(siteName, "content", idOrSlug));
3462
+ }
3463
+ async create(siteName, data) {
3464
+ return this.post(this.sitePath(siteName, "content"), data);
3465
+ }
3466
+ async update(siteName, id, data) {
3467
+ return this.patchOne(this.sitePath(siteName, "content", id), data);
3468
+ }
3469
+ async del(siteName, id) {
3470
+ return this.deleteOne(this.sitePath(siteName, "content", id));
3471
+ }
3472
+ async publish(siteName, id) {
3473
+ return this.post(this.sitePath(siteName, "content", `${id}/publish`));
3474
+ }
3475
+ async unpublish(siteName, id) {
3476
+ return this.post(this.sitePath(siteName, "content", `${id}/unpublish`));
3477
+ }
3478
+ };
3479
+
3480
+ // src/v2/client/products-client.ts
3481
+ var ProductsV2Client = class extends BaseV2Client {
3482
+ async list(siteName, params) {
3483
+ return this.getList(this.sitePath(siteName, "products"), params);
3484
+ }
3485
+ async *listAutoPaginated(siteName, params) {
3486
+ yield* this.listAutoPaginate(this.sitePath(siteName, "products"), params);
3487
+ }
3488
+ async get(siteName, idOrSlug) {
3489
+ return this.getOne(this.sitePath(siteName, "products", idOrSlug));
3490
+ }
3491
+ async create(siteName, data) {
3492
+ return this.post(this.sitePath(siteName, "products"), data);
3493
+ }
3494
+ async update(siteName, id, data) {
3495
+ return this.patchOne(this.sitePath(siteName, "products", id), data);
3496
+ }
3497
+ async del(siteName, id) {
3498
+ return this.deleteOne(this.sitePath(siteName, "products", id));
3499
+ }
3500
+ };
3501
+
3502
+ // src/v2/client/categories-client.ts
3503
+ var CategoriesV2Client = class extends BaseV2Client {
3504
+ async list(siteName, params) {
3505
+ return this.getList(this.sitePath(siteName, "categories"), params);
3506
+ }
3507
+ async get(siteName, id) {
3508
+ return this.getOne(this.sitePath(siteName, "categories", id));
3509
+ }
3510
+ async create(siteName, data) {
3511
+ return this.post(this.sitePath(siteName, "categories"), data);
3512
+ }
3513
+ async update(siteName, id, data) {
3514
+ return this.patchOne(this.sitePath(siteName, "categories", id), data);
3515
+ }
3516
+ async del(siteName, id) {
3517
+ return this.deleteOne(this.sitePath(siteName, "categories", id));
3518
+ }
3519
+ };
3520
+
3521
+ // src/v2/client/collections-client.ts
3522
+ var CollectionsV2Client = class extends BaseV2Client {
3523
+ async list(siteName, params) {
3524
+ return this.getList(this.sitePath(siteName, "collections"), params);
3525
+ }
3526
+ async getCurrent(siteName) {
3527
+ const result = await this.getOne(
3528
+ this.sitePath(siteName, "collections", "current")
3529
+ );
3530
+ return result ?? null;
3531
+ }
3532
+ async get(siteName, id) {
3533
+ return this.getOne(this.sitePath(siteName, "collections", id));
3534
+ }
3535
+ async create(siteName, data) {
3536
+ return this.post(this.sitePath(siteName, "collections"), data);
3537
+ }
3538
+ async update(siteName, id, data) {
3539
+ return this.patchOne(this.sitePath(siteName, "collections", id), data);
3540
+ }
3541
+ async del(siteName, id) {
3542
+ return this.deleteOne(this.sitePath(siteName, "collections", id));
3543
+ }
3544
+ // --- Items ---
3545
+ async listItems(siteName, collectionId) {
3546
+ return this.getList(
3547
+ this.sitePath(siteName, "collections", `${collectionId}/items`)
3548
+ );
3549
+ }
3550
+ async addItem(siteName, collectionId, data) {
3551
+ return this.post(
3552
+ this.sitePath(siteName, "collections", `${collectionId}/items`),
3553
+ data
3554
+ );
3555
+ }
3556
+ async removeItem(siteName, collectionId, itemId) {
3557
+ return this.deleteOne(
3558
+ this.sitePath(siteName, "collections", `${collectionId}/items/${itemId}`)
3559
+ );
3560
+ }
3561
+ };
3562
+
3563
+ // src/v2/client/orders-client.ts
3564
+ var OrdersV2Client = class extends BaseV2Client {
3565
+ async list(siteName, params) {
3566
+ return this.getList(this.sitePath(siteName, "orders"), params);
3567
+ }
3568
+ async *listAutoPaginated(siteName, params) {
3569
+ yield* this.listAutoPaginate(this.sitePath(siteName, "orders"), params);
3570
+ }
3571
+ async get(siteName, id) {
3572
+ return this.getOne(this.sitePath(siteName, "orders", id));
3573
+ }
3574
+ };
3575
+
3576
+ // src/v2/client/site-users-client.ts
3577
+ var SiteUsersV2Client = class extends BaseV2Client {
3578
+ // --- OTP Auth ---
3579
+ async requestOtp(siteName, data) {
3580
+ return this.post(this.sitePath(siteName, "users", "request-otp"), data);
3581
+ }
3582
+ async verifyOtp(siteName, data) {
3583
+ return this.post(this.sitePath(siteName, "users", "verify-otp"), data);
3584
+ }
3585
+ // --- Admin ---
3586
+ async list(siteName, params) {
3587
+ return this.getList(this.sitePath(siteName, "users"), params);
3588
+ }
3589
+ async *listAutoPaginated(siteName, params) {
3590
+ yield* this.listAutoPaginate(this.sitePath(siteName, "users"), params);
3591
+ }
3592
+ async get(siteName, id) {
3593
+ return this.getOne(this.sitePath(siteName, "users", id));
3594
+ }
3595
+ async update(siteName, id, data) {
3596
+ return this.patchOne(this.sitePath(siteName, "users", id), data);
3597
+ }
3598
+ };
3599
+
3600
+ // src/v2/client/newsletter-client.ts
3601
+ var NewsletterV2Client = class extends BaseV2Client {
3602
+ // --- Subscribe / Unsubscribe ---
3603
+ async subscribe(siteName, data) {
3604
+ return this.post(
3605
+ this.sitePath(siteName, "newsletter", "subscribe"),
3606
+ data
3607
+ );
3608
+ }
3609
+ async confirm(siteName, token) {
3610
+ return this.getOne(
3611
+ this.sitePath(siteName, "newsletter", `confirm/${token}`)
3612
+ );
3613
+ }
3614
+ async unsubscribe(siteName, data) {
3615
+ return this.post(
3616
+ this.sitePath(siteName, "newsletter", "unsubscribe"),
3617
+ data
3618
+ );
3619
+ }
3620
+ // --- Subscriptions (admin) ---
3621
+ async listSubscriptions(siteName, params) {
3622
+ return this.getList(
3623
+ this.sitePath(siteName, "newsletter", "subscriptions"),
3624
+ params
3625
+ );
3626
+ }
3627
+ async getSubscription(siteName, id) {
3628
+ return this.getOne(
3629
+ this.sitePath(siteName, "newsletter", `subscriptions/${id}`)
3630
+ );
3631
+ }
3632
+ // --- Lists ---
3633
+ async listLists(siteName) {
3634
+ return this.getList(
3635
+ this.sitePath(siteName, "newsletter", "lists")
3636
+ );
3637
+ }
3638
+ // --- Campaigns ---
3639
+ async listCampaigns(siteName, params) {
3640
+ return this.getList(
3641
+ this.sitePath(siteName, "newsletter", "campaigns"),
3642
+ params
3643
+ );
3644
+ }
3645
+ async getCampaign(siteName, idOrSlug) {
3646
+ return this.getOne(
3647
+ this.sitePath(siteName, "newsletter", `campaigns/${idOrSlug}`)
3648
+ );
3649
+ }
3650
+ };
3651
+
3652
+ // src/v2/client/contacts-client.ts
3653
+ var ContactsV2Client = class extends BaseV2Client {
3654
+ async submit(siteName, data) {
3655
+ return this.post(this.sitePath(siteName, "contacts"), data);
3656
+ }
3657
+ async list(siteName, params) {
3658
+ return this.getList(this.sitePath(siteName, "contacts"), params);
3659
+ }
3660
+ async get(siteName, id) {
3661
+ return this.getOne(this.sitePath(siteName, "contacts", id));
3662
+ }
3663
+ };
3664
+
3665
+ // src/v2/client/organizations-client.ts
3666
+ var OrganizationsV2Client = class extends BaseV2Client {
3667
+ async list() {
3668
+ return this.getList("/organizations");
3669
+ }
3670
+ async get(id) {
3671
+ return this.getOne(`/organizations/${id}`);
3672
+ }
3673
+ };
3674
+
3675
+ // src/v2/client/sites-client.ts
3676
+ var SitesV2Client = class extends BaseV2Client {
3677
+ async list(params) {
3678
+ return this.getList("/sites", params);
3679
+ }
3680
+ async get(name) {
3681
+ return this.getOne(`/sites/${encodeURIComponent(name)}`);
3682
+ }
3683
+ };
3684
+
3685
+ // src/v2/client/api-keys-client.ts
3686
+ var ApiKeysV2Client = class extends BaseV2Client {
3687
+ async list(params) {
3688
+ return this.getList("/api-keys", params);
3689
+ }
3690
+ async get(id) {
3691
+ return this.getOne(`/api-keys/${id}`);
3692
+ }
3693
+ async del(id) {
3694
+ return this.deleteOne(`/api-keys/${id}`);
3695
+ }
3696
+ };
3697
+
3698
+ // src/v2/client/webhooks-client.ts
3699
+ var WebhooksV2Client = class extends BaseV2Client {
3700
+ async list(siteName, params) {
3701
+ return this.getList(this.sitePath(siteName, "webhooks"), params);
3702
+ }
3703
+ async get(siteName, id) {
3704
+ return this.getOne(this.sitePath(siteName, "webhooks", id));
3705
+ }
3706
+ async create(siteName, data) {
3707
+ return this.post(this.sitePath(siteName, "webhooks"), data);
3708
+ }
3709
+ async update(siteName, id, data) {
3710
+ return this.patchOne(this.sitePath(siteName, "webhooks", id), data);
3711
+ }
3712
+ async del(siteName, id) {
3713
+ return this.deleteOne(this.sitePath(siteName, "webhooks", id));
3714
+ }
3715
+ };
3716
+
3717
+ // src/v2/index.ts
3718
+ var PerspectApiV2Client = class {
3719
+ http;
3720
+ content;
3721
+ products;
3722
+ categories;
3723
+ collections;
3724
+ orders;
3725
+ siteUsers;
3726
+ newsletter;
3727
+ contacts;
3728
+ organizations;
3729
+ sites;
3730
+ apiKeys;
3731
+ webhooks;
3732
+ constructor(config) {
3733
+ const baseUrl = config.baseUrl.replace(/\/+$/, "");
3734
+ const v2BaseUrl = baseUrl.endsWith("/api/v2") ? baseUrl : `${baseUrl}/api/v2`;
3735
+ this.http = new HttpClient({ ...config, baseUrl: v2BaseUrl });
3736
+ const basePath = "";
3737
+ this.content = new ContentV2Client(this.http, basePath);
3738
+ this.products = new ProductsV2Client(this.http, basePath);
3739
+ this.categories = new CategoriesV2Client(this.http, basePath);
3740
+ this.collections = new CollectionsV2Client(this.http, basePath);
3741
+ this.orders = new OrdersV2Client(this.http, basePath);
3742
+ this.siteUsers = new SiteUsersV2Client(this.http, basePath);
3743
+ this.newsletter = new NewsletterV2Client(this.http, basePath);
3744
+ this.contacts = new ContactsV2Client(this.http, basePath);
3745
+ this.organizations = new OrganizationsV2Client(this.http, basePath);
3746
+ this.sites = new SitesV2Client(this.http, basePath);
3747
+ this.apiKeys = new ApiKeysV2Client(this.http, basePath);
3748
+ this.webhooks = new WebhooksV2Client(this.http, basePath);
3749
+ }
3750
+ /** Update the JWT token for authenticated requests. */
3751
+ setAuth(jwt) {
3752
+ this.http.setAuth(jwt);
3753
+ }
3754
+ /** Update the API key. */
3755
+ setApiKey(apiKey) {
3756
+ this.http.setApiKey(apiKey);
3757
+ }
3758
+ /** Clear authentication. */
3759
+ clearAuth() {
3760
+ this.http.clearAuth();
3761
+ }
3762
+ };
3763
+ function createPerspectApiV2Client(config) {
3764
+ return new PerspectApiV2Client(config);
3765
+ }
3766
+
3322
3767
  // src/utils/image-transform.ts
3323
3768
  var DEFAULT_IMAGE_SIZES = {
3324
3769
  thumbnail: {
@@ -3944,6 +4389,8 @@ async function createCheckoutSession(options) {
3944
4389
  NoopCacheAdapter,
3945
4390
  OrganizationsClient,
3946
4391
  PerspectApiClient,
4392
+ PerspectApiV2Client,
4393
+ PerspectV2Error,
3947
4394
  ProductsClient,
3948
4395
  SiteUsersClient,
3949
4396
  SitesClient,
@@ -3952,6 +4399,7 @@ async function createCheckoutSession(options) {
3952
4399
  createApiError,
3953
4400
  createCheckoutSession,
3954
4401
  createPerspectApiClient,
4402
+ createPerspectApiV2Client,
3955
4403
  generateResponsiveImageHtml,
3956
4404
  generateResponsiveUrls,
3957
4405
  generateSizesAttribute,