@suqo/sdk 1.0.0 → 2.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/README.md +2 -1
- package/dist/index.cjs +367 -36
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +544 -93
- package/dist/index.d.ts +544 -93
- package/dist/index.js +367 -37
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -24,6 +24,8 @@ var KycRequiredError = class extends SuqoError {
|
|
|
24
24
|
if (options.kycStatus !== void 0) this.kycStatus = options.kycStatus;
|
|
25
25
|
}
|
|
26
26
|
};
|
|
27
|
+
var PermissionDeniedError = class extends SuqoError {
|
|
28
|
+
};
|
|
27
29
|
var ValidationError = class extends SuqoError {
|
|
28
30
|
constructor(message, options = {}) {
|
|
29
31
|
super(message, options);
|
|
@@ -48,7 +50,20 @@ function isRecord(value) {
|
|
|
48
50
|
return typeof value === "object" && value !== null;
|
|
49
51
|
}
|
|
50
52
|
|
|
53
|
+
// src/utils/assertPathId.ts
|
|
54
|
+
function assertPathId(id, method, reserved = []) {
|
|
55
|
+
if (!id || id === "." || id === "..") {
|
|
56
|
+
throw new SuqoConfigError(`${method}() requires a non-empty id other than "." or ".."`);
|
|
57
|
+
}
|
|
58
|
+
if (reserved.includes(id)) {
|
|
59
|
+
throw new SuqoConfigError(`${method}() was given "${id}", which is a route name, not an id`);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
51
63
|
// src/errors/mapHttpError.ts
|
|
64
|
+
function isMessageList(body) {
|
|
65
|
+
return Array.isArray(body) && body.length > 0 && body.every((item) => typeof item === "string");
|
|
66
|
+
}
|
|
52
67
|
function isDetailShaped(body) {
|
|
53
68
|
return isRecord(body) && typeof body.detail === "string" && body.detail.length > 0;
|
|
54
69
|
}
|
|
@@ -83,14 +98,19 @@ function mapHttpError(input) {
|
|
|
83
98
|
case 401:
|
|
84
99
|
return new AuthenticationError(messageFrom(body, fallbackMessage), base);
|
|
85
100
|
case 403: {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
101
|
+
if (isRecord(body) && typeof body.status_code === "string") {
|
|
102
|
+
const message = messageFrom(body, "KYC verification needed to perform this action.");
|
|
103
|
+
return new KycRequiredError(message, { ...base, kycStatus: body.status_code });
|
|
104
|
+
}
|
|
105
|
+
return new PermissionDeniedError(messageFrom(body, "You do not have permission to perform this action."), base);
|
|
89
106
|
}
|
|
90
107
|
case 400: {
|
|
91
108
|
if (isDetailShaped(body)) {
|
|
92
109
|
return new ValidationError(body.detail, base);
|
|
93
110
|
}
|
|
111
|
+
if (isMessageList(body)) {
|
|
112
|
+
return new ValidationError(body.join(" "), { ...base, fieldErrors: {} });
|
|
113
|
+
}
|
|
94
114
|
return new ValidationError("Validation failed.", { ...base, fieldErrors: fieldErrorsFrom(body) });
|
|
95
115
|
}
|
|
96
116
|
case 404:
|
|
@@ -210,6 +230,12 @@ function parseRetryAfterMs(headerValue, now = Date.now()) {
|
|
|
210
230
|
const delta = Math.max(dateMs - now, 0);
|
|
211
231
|
return Math.min(delta, MAX_RETRY_AFTER_MS);
|
|
212
232
|
}
|
|
233
|
+
function parseRetryAfterDetailMs(body) {
|
|
234
|
+
if (!isRecord(body) || typeof body.detail !== "string") return void 0;
|
|
235
|
+
const match = /available in (\d+(?:\.\d+)?) seconds?/i.exec(body.detail);
|
|
236
|
+
if (!match) return void 0;
|
|
237
|
+
return Math.min(Number(match[1]) * 1e3, MAX_RETRY_AFTER_MS);
|
|
238
|
+
}
|
|
213
239
|
|
|
214
240
|
// src/http/signals.ts
|
|
215
241
|
function combineSignals(signals) {
|
|
@@ -274,8 +300,8 @@ var HttpClient = class {
|
|
|
274
300
|
* send, not just `unknown`.
|
|
275
301
|
*/
|
|
276
302
|
async request(options) {
|
|
277
|
-
if (options.method === "GET" && options.body !== void 0) {
|
|
278
|
-
throw new TypeError(
|
|
303
|
+
if ((options.method === "GET" || options.method === "DELETE") && options.body !== void 0) {
|
|
304
|
+
throw new TypeError(`HttpClient.request: a ${options.method} request cannot carry a body.`);
|
|
279
305
|
}
|
|
280
306
|
let url;
|
|
281
307
|
try {
|
|
@@ -300,7 +326,7 @@ var HttpClient = class {
|
|
|
300
326
|
return await parseJsonBody(response);
|
|
301
327
|
}
|
|
302
328
|
const body = await parseJsonBody(response);
|
|
303
|
-
const retryAfter = parseRetryAfterMs(response.headers.get("Retry-After"));
|
|
329
|
+
const retryAfter = parseRetryAfterMs(response.headers.get("Retry-After")) ?? (response.status === 429 ? parseRetryAfterDetailMs(body) : void 0);
|
|
304
330
|
if (!isLastAttempt && retryable && isRetryableFailure({ networkError: false, status: response.status })) {
|
|
305
331
|
await this.#sleepOrAbort(retryAfter ?? backoffDelayMs(attempt - 1), options.signal, timeoutMs);
|
|
306
332
|
continue;
|
|
@@ -358,7 +384,7 @@ var HttpClient = class {
|
|
|
358
384
|
if (isWrite) {
|
|
359
385
|
headers["Content-Type"] = "application/json";
|
|
360
386
|
}
|
|
361
|
-
const body =
|
|
387
|
+
const body = "body" in options ? options.body : void 0;
|
|
362
388
|
const { signal, cleanup } = combineSignals([AbortSignal.timeout(timeoutMs), options.signal]);
|
|
363
389
|
try {
|
|
364
390
|
const init = {
|
|
@@ -428,14 +454,23 @@ function deserializeBillingPeriod(wire) {
|
|
|
428
454
|
isCurrent: wire.is_current,
|
|
429
455
|
isLimited: wire.is_limited,
|
|
430
456
|
isArchived: wire.is_archived,
|
|
431
|
-
offers: wire.offers
|
|
457
|
+
offers: (wire.offers ?? []).map(deserializeOffer)
|
|
458
|
+
};
|
|
459
|
+
}
|
|
460
|
+
function deserializeOffer(wire) {
|
|
461
|
+
return {
|
|
462
|
+
id: wire.id,
|
|
463
|
+
discountAmount: wire.discount_amount,
|
|
464
|
+
startsAt: wire.starts_at ?? null,
|
|
465
|
+
validUntil: wire.valid_until ?? null,
|
|
466
|
+
isActive: wire.is_active
|
|
432
467
|
};
|
|
433
468
|
}
|
|
434
469
|
function deserializePlan(wire) {
|
|
435
470
|
return {
|
|
436
471
|
planId: wire.plan_id,
|
|
437
472
|
planName: wire.plan_name,
|
|
438
|
-
description: wire.description,
|
|
473
|
+
description: wire.description ?? null,
|
|
439
474
|
// `openapi.yaml`'s Product schema has no `required` list at all, so `billing_periods` being
|
|
440
475
|
// omitted entirely (not just an empty array) is spec-legal — `?? []` degrades to an empty
|
|
441
476
|
// list instead of `.map` throwing on `undefined` (found in review, reproduced).
|
|
@@ -445,25 +480,25 @@ function deserializePlan(wire) {
|
|
|
445
480
|
function deserializeProductVat(wire) {
|
|
446
481
|
return {
|
|
447
482
|
isVatActive: wire.is_vat_active,
|
|
448
|
-
vatType: wire.vat_type,
|
|
449
|
-
vatPercentage: wire.vat_percentage
|
|
483
|
+
vatType: wire.vat_type ?? null,
|
|
484
|
+
vatPercentage: wire.vat_percentage ?? null
|
|
450
485
|
};
|
|
451
486
|
}
|
|
452
487
|
function deserializeProduct(wire) {
|
|
453
488
|
return {
|
|
454
489
|
productId: wire.product_id,
|
|
455
490
|
name: wire.name,
|
|
456
|
-
description: wire.description,
|
|
491
|
+
description: wire.description ?? null,
|
|
457
492
|
type: wire.type,
|
|
458
493
|
isActive: wire.is_active,
|
|
459
|
-
termsAndConditions: wire.terms_and_conditions,
|
|
460
|
-
featuresAndBenefits: wire.features_and_benefits,
|
|
494
|
+
termsAndConditions: wire.terms_and_conditions ?? null,
|
|
495
|
+
featuresAndBenefits: wire.features_and_benefits ?? null,
|
|
461
496
|
// `== null` (not `=== null`) is deliberate — it catches both an explicit `null` and an
|
|
462
497
|
// entirely omitted `vat` key. `openapi.yaml` has no `required` list for Product, so omitting
|
|
463
498
|
// `vat` is spec-legal, and the old `=== null` check crashed on `deserializeProductVat(undefined)`
|
|
464
499
|
// when that happened (found in review, reproduced).
|
|
465
500
|
vat: wire.vat == null ? null : deserializeProductVat(wire.vat),
|
|
466
|
-
productImage: wire.product_image,
|
|
501
|
+
productImage: (wire.product_image ?? []).map((img) => ({ image: img.image, imageOrder: img.image_order })),
|
|
467
502
|
// Same reasoning as `billingPeriods` above — `plan` being omitted entirely is spec-legal.
|
|
468
503
|
plan: (wire.plan ?? []).map(deserializePlan),
|
|
469
504
|
totalSubscribers: wire.total_subscribers,
|
|
@@ -571,18 +606,26 @@ function deserializeSubscriptionCustomer(wire) {
|
|
|
571
606
|
}
|
|
572
607
|
return customer;
|
|
573
608
|
}
|
|
609
|
+
function deserializeCheckoutSessionResponse(wire) {
|
|
610
|
+
return {
|
|
611
|
+
publicId: wire.public_id,
|
|
612
|
+
expiresAt: wire.expires_at,
|
|
613
|
+
checkoutUrl: wire.checkout_url
|
|
614
|
+
};
|
|
615
|
+
}
|
|
574
616
|
|
|
575
617
|
// src/resources/subscriptions.ts
|
|
576
618
|
var SUBSCRIPTIONS_PATH = "/api/v1/subscriptions";
|
|
619
|
+
var RENEW_PATH = "/api/v1/subscription/renew";
|
|
577
620
|
function deserializeSubscriptionProduct(wire) {
|
|
578
621
|
return {
|
|
579
|
-
productId: wire.product_id,
|
|
580
|
-
name: wire.name,
|
|
581
|
-
planName: wire.plan_name,
|
|
582
|
-
pbpId: wire.pbp_id,
|
|
583
|
-
label: wire.label,
|
|
584
|
-
price: wire.price,
|
|
585
|
-
currency: wire.currency
|
|
622
|
+
productId: wire.product_id ?? null,
|
|
623
|
+
name: wire.name ?? null,
|
|
624
|
+
planName: wire.plan_name ?? null,
|
|
625
|
+
pbpId: wire.pbp_id ?? null,
|
|
626
|
+
label: wire.label ?? null,
|
|
627
|
+
price: wire.price ?? null,
|
|
628
|
+
currency: wire.currency ?? null
|
|
586
629
|
};
|
|
587
630
|
}
|
|
588
631
|
function deserializeSubscription(wire) {
|
|
@@ -654,13 +697,28 @@ var SubscriptionsResource = class {
|
|
|
654
697
|
});
|
|
655
698
|
}
|
|
656
699
|
/**
|
|
700
|
+
* Retrieves one of your own subscriptions by id. Built from Swagger's schema; not yet checked
|
|
701
|
+
* against a captured response.
|
|
702
|
+
*/
|
|
703
|
+
async retrieve(id) {
|
|
704
|
+
assertPathId(id, "subscriptions.retrieve");
|
|
705
|
+
const wire = await this.#http.request({
|
|
706
|
+
method: "GET",
|
|
707
|
+
path: `${SUBSCRIPTIONS_PATH}/${encodeURIComponent(id)}`
|
|
708
|
+
});
|
|
709
|
+
return deserializeSubscription(wire);
|
|
710
|
+
}
|
|
711
|
+
/**
|
|
712
|
+
* **Legacy** — prefer `checkoutSessions.create()`, which takes up to ten items in one payment
|
|
713
|
+
* and doesn't need a full customer object. Still supported; existing integrations keep working.
|
|
714
|
+
*
|
|
657
715
|
* Creates a subscription in `pending_checkout` state and returns a checkout URL for the buyer
|
|
658
716
|
* to pay (SDK-SPEC.md §5). Redirect the buyer to `checkoutUrl`; the return is **not** proof of
|
|
659
717
|
* payment — learn the real outcome via the `checkout.succeeded`/`checkout.failed` webhooks.
|
|
660
718
|
*
|
|
661
|
-
*
|
|
662
|
-
*
|
|
663
|
-
*
|
|
719
|
+
* `customer.email` identifies the buyer. A buyer may hold only one live recurring subscription
|
|
720
|
+
* per product: a second one on any billing period of the same product throws `ValidationError`.
|
|
721
|
+
* To restart an inactive subscription, use `renew()` rather than creating a new one.
|
|
664
722
|
*
|
|
665
723
|
* Not auto-retryable — writes never retry until idempotency ships (SDK-SPEC.md §8, §12).
|
|
666
724
|
*/
|
|
@@ -680,11 +738,14 @@ var SubscriptionsResource = class {
|
|
|
680
738
|
/**
|
|
681
739
|
* Schedules cancellation for the end of the current billing period — **not** immediate
|
|
682
740
|
* (confirmed with backend 2026-08-17; immediate cancellation is a future capability). Status
|
|
683
|
-
* moves to `pending_cancellation`; the subscription stays active until the period ends.
|
|
741
|
+
* moves to `pending_cancellation`; the subscription stays active until the period ends. Calling
|
|
742
|
+
* it on a subscription already cancelled or pending cancellation returns it unchanged, so a
|
|
743
|
+
* retry by the caller is safe.
|
|
684
744
|
*
|
|
685
745
|
* Not auto-retryable — writes never retry until idempotency ships (SDK-SPEC.md §8, §12).
|
|
686
746
|
*/
|
|
687
747
|
async cancel(id) {
|
|
748
|
+
assertPathId(id, "subscriptions.cancel");
|
|
688
749
|
return this.#http.request({
|
|
689
750
|
method: "POST",
|
|
690
751
|
path: `${SUBSCRIPTIONS_PATH}/${encodeURIComponent(id)}/cancel`
|
|
@@ -707,18 +768,37 @@ var SubscriptionsResource = class {
|
|
|
707
768
|
});
|
|
708
769
|
}
|
|
709
770
|
/**
|
|
710
|
-
*
|
|
711
|
-
*
|
|
712
|
-
*
|
|
771
|
+
* Undoes a scheduled cancellation and returns the subscription to `active`. An already-active
|
|
772
|
+
* subscription is returned unchanged. Once a subscription is `cancelled` there is nothing to
|
|
773
|
+
* resume — use `renew()`.
|
|
713
774
|
*
|
|
714
775
|
* Not auto-retryable — writes never retry until idempotency ships (SDK-SPEC.md §8, §12).
|
|
715
776
|
*/
|
|
716
777
|
async resume(id) {
|
|
778
|
+
assertPathId(id, "subscriptions.resume");
|
|
717
779
|
return this.#http.request({
|
|
718
780
|
method: "POST",
|
|
719
781
|
path: `${SUBSCRIPTIONS_PATH}/${encodeURIComponent(id)}/resume`
|
|
720
782
|
});
|
|
721
783
|
}
|
|
784
|
+
/**
|
|
785
|
+
* Opens a checkout session for the next payment on one of your own subscriptions, reusing its
|
|
786
|
+
* customer and billing period (or partner-priced item) rather than resubmitting them — how an
|
|
787
|
+
* inactive subscription is restarted. Send the buyer to the returned `checkoutUrl`, and learn
|
|
788
|
+
* the outcome from the `checkout.*` webhooks, matched on `publicId`.
|
|
789
|
+
*
|
|
790
|
+
* A cancelled or archived subscription, or a paid one-time purchase, can't be renewed: that
|
|
791
|
+
* throws `ValidationError`. Not auto-retryable — writes never retry until idempotency ships
|
|
792
|
+
* (SDK-SPEC.md §8, §12).
|
|
793
|
+
*/
|
|
794
|
+
async renew(subscriptionId) {
|
|
795
|
+
const wire = await this.#http.request({
|
|
796
|
+
method: "POST",
|
|
797
|
+
path: RENEW_PATH,
|
|
798
|
+
body: { subscription_id: subscriptionId }
|
|
799
|
+
});
|
|
800
|
+
return deserializeCheckoutSessionResponse(wire);
|
|
801
|
+
}
|
|
722
802
|
};
|
|
723
803
|
|
|
724
804
|
// src/resources/customers.ts
|
|
@@ -726,12 +806,22 @@ var CUSTOMERS_PATH = "/api/v1/customers";
|
|
|
726
806
|
function deserializeCustomer(wire) {
|
|
727
807
|
return {
|
|
728
808
|
id: wire.id,
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
809
|
+
// `?? null` on all four nullable fields (not a direct pass-through, found in review): catches
|
|
810
|
+
// both an explicit `null` and an entirely omitted key — see the WireCustomer comment above.
|
|
811
|
+
buyerPhone: wire.buyer_phone ?? null,
|
|
812
|
+
buyerEmail: wire.buyer_email ?? null,
|
|
813
|
+
fullName: wire.full_name ?? null,
|
|
814
|
+
address: wire.address ?? null,
|
|
732
815
|
createdAt: wire.created_at
|
|
733
816
|
};
|
|
734
817
|
}
|
|
818
|
+
function serializeCustomerFields(params) {
|
|
819
|
+
const wire = {};
|
|
820
|
+
if (params.fullName !== void 0) wire.full_name = params.fullName;
|
|
821
|
+
if (params.email !== void 0) wire.email = params.email;
|
|
822
|
+
if (params.address !== void 0) wire.address = params.address;
|
|
823
|
+
return wire;
|
|
824
|
+
}
|
|
735
825
|
var CustomersResource = class {
|
|
736
826
|
#http;
|
|
737
827
|
constructor(http) {
|
|
@@ -758,21 +848,260 @@ var CustomersResource = class {
|
|
|
758
848
|
});
|
|
759
849
|
}
|
|
760
850
|
/**
|
|
761
|
-
* Retrieves a single customer by id
|
|
762
|
-
*
|
|
851
|
+
* Retrieves a single customer by id — an opaque prefixed string (e.g. `"cus_1ce18d624"`), like
|
|
852
|
+
* `pbp_...` on billing periods, not an integer and not a UUID (Bug #42 corrected the previously
|
|
853
|
+
* wrong `number` type/doc claim here).
|
|
763
854
|
*/
|
|
764
855
|
async retrieve(id) {
|
|
856
|
+
assertPathId(id, "customers.retrieve");
|
|
765
857
|
const wire = await this.#http.request({
|
|
766
858
|
method: "GET",
|
|
767
859
|
path: `${CUSTOMERS_PATH}/${encodeURIComponent(id)}`
|
|
768
860
|
});
|
|
769
861
|
return deserializeCustomer(wire);
|
|
770
862
|
}
|
|
863
|
+
/**
|
|
864
|
+
* Records a customer on the seller's account without opening a subscription. `email` identifies
|
|
865
|
+
* the buyer and is required; `phone` is optional. The name and address are the seller's own copy
|
|
866
|
+
* and aren't shared with other sellers. Pass the returned `id` to `checkoutSessions.create()` as
|
|
867
|
+
* `customerId` to send the buyer straight to payment.
|
|
868
|
+
*
|
|
869
|
+
* An email or phone the seller already holds updates that existing customer instead of creating
|
|
870
|
+
* a second one (the API answers `200` rather than `201`; both resolve to the `Customer` here).
|
|
871
|
+
* That makes this call safe for the caller to retry — but, like every write, the SDK never
|
|
872
|
+
* retries it automatically (SDK-SPEC.md §8, §12).
|
|
873
|
+
*/
|
|
874
|
+
async create(params) {
|
|
875
|
+
const wire = await this.#http.request({
|
|
876
|
+
method: "POST",
|
|
877
|
+
path: CUSTOMERS_PATH,
|
|
878
|
+
body: {
|
|
879
|
+
...serializeCustomerFields(params),
|
|
880
|
+
...params.phone !== void 0 ? { phone: params.phone } : {}
|
|
881
|
+
}
|
|
882
|
+
});
|
|
883
|
+
return deserializeCustomer(wire);
|
|
884
|
+
}
|
|
885
|
+
/**
|
|
886
|
+
* Updates the seller's copy of a customer's name, email or address. Send only the fields you're
|
|
887
|
+
* changing; `""` clears a field. The phone can't be changed.
|
|
888
|
+
*
|
|
889
|
+
* Not auto-retryable — writes never retry until idempotency ships (SDK-SPEC.md §8, §12).
|
|
890
|
+
*/
|
|
891
|
+
async update(id, params) {
|
|
892
|
+
assertPathId(id, "customers.update");
|
|
893
|
+
const wire = await this.#http.request({
|
|
894
|
+
method: "PATCH",
|
|
895
|
+
path: `${CUSTOMERS_PATH}/${encodeURIComponent(id)}`,
|
|
896
|
+
body: serializeCustomerFields(params)
|
|
897
|
+
});
|
|
898
|
+
return deserializeCustomer(wire);
|
|
899
|
+
}
|
|
900
|
+
};
|
|
901
|
+
|
|
902
|
+
// src/resources/checkoutSessions.ts
|
|
903
|
+
var CHECKOUT_SESSIONS_PATH = "/api/v1/checkout-sessions";
|
|
904
|
+
function deserializeLineItem(wire) {
|
|
905
|
+
return {
|
|
906
|
+
name: wire.name,
|
|
907
|
+
amount: wire.amount,
|
|
908
|
+
pbpId: wire.pbp_id ?? null,
|
|
909
|
+
currency: wire.currency,
|
|
910
|
+
vatType: wire.vat_type ?? null,
|
|
911
|
+
vatPercentage: wire.vat_percentage ?? null,
|
|
912
|
+
discountAmount: wire.discount_amount ?? null,
|
|
913
|
+
intervalType: wire.interval_type,
|
|
914
|
+
intervalCount: wire.interval_count,
|
|
915
|
+
displayLabel: wire.display_label,
|
|
916
|
+
currentPeriodEnd: wire.current_period_end ?? null,
|
|
917
|
+
extendsTo: wire.extends_to ?? null,
|
|
918
|
+
nextPaymentDue: wire.next_payment_due ?? null,
|
|
919
|
+
// Each kind carries one of these and not the other, so they stay absent rather than null.
|
|
920
|
+
...wire.image_urls !== void 0 ? { imageUrls: wire.image_urls } : {},
|
|
921
|
+
...wire.plan_name !== void 0 ? { planName: wire.plan_name } : {}
|
|
922
|
+
};
|
|
923
|
+
}
|
|
924
|
+
function deserializeCheckoutSession(wire) {
|
|
925
|
+
return {
|
|
926
|
+
publicId: wire.public_id,
|
|
927
|
+
lineItems: (wire.line_items ?? []).map(deserializeLineItem),
|
|
928
|
+
customerId: wire.customer_id ?? null,
|
|
929
|
+
sellerDetails: {
|
|
930
|
+
id: wire.seller_details.id,
|
|
931
|
+
phone: wire.seller_details.phone,
|
|
932
|
+
businessLogo: wire.seller_details.business_logo ?? null,
|
|
933
|
+
businessName: wire.seller_details.business_name
|
|
934
|
+
},
|
|
935
|
+
returnUrl: wire.return_url,
|
|
936
|
+
expiresAt: wire.expires_at,
|
|
937
|
+
completedAt: wire.completed_at ?? null,
|
|
938
|
+
isExpired: wire.is_expired,
|
|
939
|
+
sessionType: wire.session_type ?? null
|
|
940
|
+
};
|
|
941
|
+
}
|
|
942
|
+
function serializeItem(item) {
|
|
943
|
+
if (item.pbpId !== void 0) return { pbp_id: item.pbpId };
|
|
944
|
+
return {
|
|
945
|
+
name: item.name,
|
|
946
|
+
amount: item.amount,
|
|
947
|
+
interval_type: item.intervalType,
|
|
948
|
+
interval_count: item.intervalCount
|
|
949
|
+
};
|
|
950
|
+
}
|
|
951
|
+
var CheckoutSessionsResource = class {
|
|
952
|
+
#http;
|
|
953
|
+
constructor(http) {
|
|
954
|
+
this.#http = http;
|
|
955
|
+
}
|
|
956
|
+
/**
|
|
957
|
+
* Opens a checkout session for one to ten items and returns a URL for the buyer to pay. Prices
|
|
958
|
+
* are snapshotted now, so a plan edited later can't change what the buyer was shown. Redirect
|
|
959
|
+
* the buyer to `checkoutUrl`; the return to `returnUrl` is **not** proof of payment — learn the
|
|
960
|
+
* outcome from `checkout.succeeded`/`checkout.failed`, matched on `publicId` (`session_id`).
|
|
961
|
+
*
|
|
962
|
+
* The API's one rate-limited endpoint: 20 sessions a minute per account. Past that it throws
|
|
963
|
+
* `RateLimitError` with `retryAfter` set. Never retried automatically — writes don't retry until
|
|
964
|
+
* idempotency ships (SDK-SPEC.md §8, §12) — so open a session when the buyer commits to paying,
|
|
965
|
+
* not on page load.
|
|
966
|
+
*/
|
|
967
|
+
async create(params) {
|
|
968
|
+
const wire = await this.#http.request({
|
|
969
|
+
method: "POST",
|
|
970
|
+
path: CHECKOUT_SESSIONS_PATH,
|
|
971
|
+
body: {
|
|
972
|
+
items: params.items.map(serializeItem),
|
|
973
|
+
return_url: params.returnUrl,
|
|
974
|
+
...params.customerId !== void 0 ? { customer_id: params.customerId } : {}
|
|
975
|
+
}
|
|
976
|
+
});
|
|
977
|
+
return deserializeCheckoutSessionResponse(wire);
|
|
978
|
+
}
|
|
979
|
+
/**
|
|
980
|
+
* Reads one of your own open sessions: its line items, customer, seller and expiry. Once paid,
|
|
981
|
+
* expired, unknown or someone else's, it throws `NotFoundError`.
|
|
982
|
+
*/
|
|
983
|
+
async retrieve(id) {
|
|
984
|
+
assertPathId(id, "checkoutSessions.retrieve");
|
|
985
|
+
const wire = await this.#http.request({
|
|
986
|
+
method: "GET",
|
|
987
|
+
path: `${CHECKOUT_SESSIONS_PATH}/${encodeURIComponent(id)}`
|
|
988
|
+
});
|
|
989
|
+
return deserializeCheckoutSession(wire);
|
|
990
|
+
}
|
|
771
991
|
};
|
|
992
|
+
var WEBHOOKS_PATH = "/api/v1/webhooks";
|
|
993
|
+
var RESERVED_SEGMENTS = ["secret"];
|
|
994
|
+
function deserializeWebhook(wire) {
|
|
995
|
+
return {
|
|
996
|
+
id: wire.id,
|
|
997
|
+
event: wire.event,
|
|
998
|
+
endpointUrl: wire.endpoint_url,
|
|
999
|
+
isActive: wire.is_active,
|
|
1000
|
+
createdAt: wire.created_at
|
|
1001
|
+
};
|
|
1002
|
+
}
|
|
1003
|
+
function serializeWebhookWrite(params) {
|
|
1004
|
+
const wire = {};
|
|
1005
|
+
if (params.event !== void 0) wire.event = params.event;
|
|
1006
|
+
if (params.endpointUrl !== void 0) wire.endpoint_url = params.endpointUrl;
|
|
1007
|
+
if (params.isActive !== void 0) wire.is_active = params.isActive;
|
|
1008
|
+
return wire;
|
|
1009
|
+
}
|
|
772
1010
|
var DEFAULT_TOLERANCE_SEC = 300;
|
|
773
1011
|
var SIGNATURE_PREFIX = "sha256=";
|
|
774
1012
|
var HEX_DIGEST_RE = /^[0-9a-f]{64}$/i;
|
|
775
1013
|
var WebhooksResource = class {
|
|
1014
|
+
#http;
|
|
1015
|
+
constructor(http) {
|
|
1016
|
+
this.#http = http;
|
|
1017
|
+
}
|
|
1018
|
+
/** Every webhook registered by your account. Not paginated — the API returns them all. */
|
|
1019
|
+
async list() {
|
|
1020
|
+
const wire = await this.#http.request({ method: "GET", path: WEBHOOKS_PATH });
|
|
1021
|
+
return wire.map(deserializeWebhook);
|
|
1022
|
+
}
|
|
1023
|
+
/**
|
|
1024
|
+
* Subscribes an `https` endpoint to one event. An account holds at most one webhook per event,
|
|
1025
|
+
* and the endpoint must resolve only to public addresses — either failing throws
|
|
1026
|
+
* `ValidationError`. Not auto-retryable (SDK-SPEC.md §8, §12).
|
|
1027
|
+
*/
|
|
1028
|
+
async create(params) {
|
|
1029
|
+
const wire = await this.#http.request({
|
|
1030
|
+
method: "POST",
|
|
1031
|
+
path: WEBHOOKS_PATH,
|
|
1032
|
+
body: serializeWebhookWrite(params)
|
|
1033
|
+
});
|
|
1034
|
+
return deserializeWebhook(wire);
|
|
1035
|
+
}
|
|
1036
|
+
/** Retrieves one of your own webhooks by id (`whk_…`). */
|
|
1037
|
+
async retrieve(id) {
|
|
1038
|
+
assertPathId(id, "webhooks.retrieve", RESERVED_SEGMENTS);
|
|
1039
|
+
const wire = await this.#http.request({
|
|
1040
|
+
method: "GET",
|
|
1041
|
+
path: `${WEBHOOKS_PATH}/${encodeURIComponent(id)}`
|
|
1042
|
+
});
|
|
1043
|
+
return deserializeWebhook(wire);
|
|
1044
|
+
}
|
|
1045
|
+
/**
|
|
1046
|
+
* Overwrites a webhook's event, endpoint and active flag (`PUT`). `event` and `endpointUrl` are
|
|
1047
|
+
* both required; to change one field on its own, use {@link WebhooksResource.update}. Not
|
|
1048
|
+
* auto-retryable (SDK-SPEC.md §8, §12).
|
|
1049
|
+
*/
|
|
1050
|
+
async replace(id, params) {
|
|
1051
|
+
assertPathId(id, "webhooks.replace", RESERVED_SEGMENTS);
|
|
1052
|
+
const wire = await this.#http.request({
|
|
1053
|
+
method: "PUT",
|
|
1054
|
+
path: `${WEBHOOKS_PATH}/${encodeURIComponent(id)}`,
|
|
1055
|
+
body: serializeWebhookWrite(params)
|
|
1056
|
+
});
|
|
1057
|
+
return deserializeWebhook(wire);
|
|
1058
|
+
}
|
|
1059
|
+
/**
|
|
1060
|
+
* Changes only the fields you send (`PATCH`) — e.g. `{ isActive: false }` pauses deliveries
|
|
1061
|
+
* without deleting the webhook. Not auto-retryable (SDK-SPEC.md §8, §12).
|
|
1062
|
+
*/
|
|
1063
|
+
async update(id, params) {
|
|
1064
|
+
assertPathId(id, "webhooks.update", RESERVED_SEGMENTS);
|
|
1065
|
+
const wire = await this.#http.request({
|
|
1066
|
+
method: "PATCH",
|
|
1067
|
+
path: `${WEBHOOKS_PATH}/${encodeURIComponent(id)}`,
|
|
1068
|
+
body: serializeWebhookWrite(params)
|
|
1069
|
+
});
|
|
1070
|
+
return deserializeWebhook(wire);
|
|
1071
|
+
}
|
|
1072
|
+
/** Removes a webhook. Past delivery records are kept for debugging. Not auto-retryable (SDK-SPEC.md §8, §12). */
|
|
1073
|
+
async delete(id) {
|
|
1074
|
+
assertPathId(id, "webhooks.delete", RESERVED_SEGMENTS);
|
|
1075
|
+
await this.#http.request({
|
|
1076
|
+
method: "DELETE",
|
|
1077
|
+
path: `${WEBHOOKS_PATH}/${encodeURIComponent(id)}`
|
|
1078
|
+
});
|
|
1079
|
+
}
|
|
1080
|
+
/**
|
|
1081
|
+
* Returns the account's signing secret — the `secret` to pass to {@link WebhooksResource.verify}.
|
|
1082
|
+
* The API creates it on the first call; it can't be rotated through the API. Treat it like a
|
|
1083
|
+
* password: never log it.
|
|
1084
|
+
*/
|
|
1085
|
+
async retrieveSecret() {
|
|
1086
|
+
const wire = await this.#http.request({
|
|
1087
|
+
method: "GET",
|
|
1088
|
+
path: `${WEBHOOKS_PATH}/secret`
|
|
1089
|
+
});
|
|
1090
|
+
return { signingSecret: wire.signing_secret };
|
|
1091
|
+
}
|
|
1092
|
+
/**
|
|
1093
|
+
* Queues a signed test delivery to the webhook's endpoint so you can check your receiver. It
|
|
1094
|
+
* resolves once the job is queued; the delivery itself arrives asynchronously. Note a test
|
|
1095
|
+
* delivery's body differs from a real event's (fields under `data`) — use it to check
|
|
1096
|
+
* `verify()`, not your payload parsing. Not auto-retryable (SDK-SPEC.md §8, §12).
|
|
1097
|
+
*/
|
|
1098
|
+
async testDelivery(id) {
|
|
1099
|
+
assertPathId(id, "webhooks.testDelivery", RESERVED_SEGMENTS);
|
|
1100
|
+
await this.#http.request({
|
|
1101
|
+
method: "POST",
|
|
1102
|
+
path: `${WEBHOOKS_PATH}/${encodeURIComponent(id)}/test-delivery`
|
|
1103
|
+
});
|
|
1104
|
+
}
|
|
776
1105
|
/**
|
|
777
1106
|
* Verifies an inbound webhook delivery's signature. Returns `false` for any failure mode —
|
|
778
1107
|
* malformed signature, expired timestamp, or a genuine mismatch — never throws, so a caller
|
|
@@ -822,7 +1151,8 @@ var SuqoClient = class {
|
|
|
822
1151
|
this.products = new ProductsResource(http);
|
|
823
1152
|
this.subscriptions = new SubscriptionsResource(http);
|
|
824
1153
|
this.customers = new CustomersResource(http);
|
|
825
|
-
this.
|
|
1154
|
+
this.checkoutSessions = new CheckoutSessionsResource(http);
|
|
1155
|
+
this.webhooks = new WebhooksResource(http);
|
|
826
1156
|
}
|
|
827
1157
|
/** `"sandbox" | "live"` — inferred from the key prefix, never selected explicitly (SDK-SPEC.md §2). */
|
|
828
1158
|
get environment() {
|
|
@@ -851,8 +1181,8 @@ var SuqoClient = class {
|
|
|
851
1181
|
};
|
|
852
1182
|
|
|
853
1183
|
// src/index.ts
|
|
854
|
-
var VERSION = "
|
|
1184
|
+
var VERSION = "2.0.0";
|
|
855
1185
|
|
|
856
|
-
export { AuthenticationError, KycRequiredError, NetworkError, NotFoundError, RateLimitError, ServerError, SubscriptionStatus, SuqoClient, SuqoConfigError, SuqoError, VERSION, ValidationError, bridgeAutoPaging, deserializePage, listAll, mapHttpError, toPageQuery };
|
|
1186
|
+
export { AuthenticationError, KycRequiredError, NetworkError, NotFoundError, PermissionDeniedError, RateLimitError, ServerError, SubscriptionStatus, SuqoClient, SuqoConfigError, SuqoError, VERSION, ValidationError, bridgeAutoPaging, deserializePage, listAll, mapHttpError, toPageQuery };
|
|
857
1187
|
//# sourceMappingURL=index.js.map
|
|
858
1188
|
//# sourceMappingURL=index.js.map
|