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