@waffo/waffo-node 3.1.0 → 3.2.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.mjs CHANGED
@@ -348,6 +348,10 @@ var ApiResponse = class _ApiResponse {
348
348
  getData() {
349
349
  return this.data;
350
350
  }
351
+ /** Returns the same JSON data with business string literals opened to future values. */
352
+ getOpenData() {
353
+ return this.data;
354
+ }
351
355
  /**
352
356
  * Gets the error message.
353
357
  *
@@ -455,7 +459,7 @@ var HEADER_SDK_VERSION = "x-sdk-version";
455
459
  var HEADER_CONTENT_TYPE = "content-type";
456
460
  var CONTENT_TYPE_JSON = "application/json";
457
461
  var API_VERSION = "1.0.0";
458
- var SDK_VERSION = "waffo-node/3.1.0";
462
+ var SDK_VERSION = "waffo-node/3.2.0";
459
463
  var WaffoHttpClient = class {
460
464
  baseUrl;
461
465
  apiKey;
@@ -646,6 +650,7 @@ var WebhookHandler = class {
646
650
  subscriptionPaymentHandler;
647
651
  subscriptionPeriodChangedHandler;
648
652
  subscriptionChangeHandler;
653
+ customEventHandlers = /* @__PURE__ */ new Map();
649
654
  constructor(config) {
650
655
  this.waffoPublicKey = config.waffoPublicKey;
651
656
  this.privateKey = config.privateKey;
@@ -694,6 +699,20 @@ var WebhookHandler = class {
694
699
  this.subscriptionChangeHandler = handler;
695
700
  return this;
696
701
  }
702
+ /** Registers an exact custom event name before receiving webhooks. Later registration replaces the same name. */
703
+ onEvent(eventType, handler) {
704
+ if (typeof eventType !== "string" || !eventType.trim()) {
705
+ throw new WaffoError(WaffoErrorCode.UNEXPECTED, "Custom event name must not be blank");
706
+ }
707
+ if (typeof handler !== "function") {
708
+ throw new WaffoError(WaffoErrorCode.UNEXPECTED, "Custom event handler must be a function");
709
+ }
710
+ if (Object.values(WebhookEventType).some((known) => known === eventType)) {
711
+ throw new WaffoError(WaffoErrorCode.UNEXPECTED, `Use the dedicated handler for built-in event: ${eventType}`);
712
+ }
713
+ this.customEventHandlers.set(eventType, handler);
714
+ return this;
715
+ }
697
716
  /**
698
717
  * Processes a webhook notification.
699
718
  *
@@ -713,6 +732,9 @@ var WebhookHandler = class {
713
732
  return this.createFailedResult("Invalid signature");
714
733
  }
715
734
  const parsed = JSON.parse(body);
735
+ if (!parsed || typeof parsed.eventType !== "string") {
736
+ return this.createFailedResult("Unknown event type");
737
+ }
716
738
  const eventType = parsed.eventType;
717
739
  switch (eventType) {
718
740
  case "PAYMENT_NOTIFICATION" /* PAYMENT_NOTIFICATION */:
@@ -742,9 +764,15 @@ var WebhookHandler = class {
742
764
  await this.subscriptionChangeHandler(parsed);
743
765
  }
744
766
  break;
745
- default:
746
- this.logger?.warn(`Unknown webhook event type: ${eventType}`);
747
- return this.createFailedResult(`Unknown event type: ${eventType}`);
767
+ default: {
768
+ const handler = this.customEventHandlers.get(eventType);
769
+ if (!handler) {
770
+ this.logger?.warn(`Unknown webhook event type: ${eventType}`);
771
+ return this.createFailedResult(`Unknown event type: ${eventType}`);
772
+ }
773
+ await handler(parsed);
774
+ break;
775
+ }
748
776
  }
749
777
  return this.createSuccessResult();
750
778
  } catch (error) {
@@ -937,14 +965,6 @@ var SubscriptionResource = class _SubscriptionResource extends BaseResource {
937
965
  static PATH_UPDATE = "/update";
938
966
  static PATH_CHANGE = "/change";
939
967
  static PATH_CHANGE_INQUIRY = "/change/inquiry";
940
- /**
941
- * Creates a new subscription.
942
- *
943
- * @param params - Subscription creation parameters
944
- * @param options - Optional request options
945
- * @returns API response with subscription data
946
- * @throws WaffoUnknownStatusError for network errors
947
- */
948
968
  async create(params, options) {
949
969
  if (!params.requestedAt) {
950
970
  params.requestedAt = getCurrentTimestamp();
@@ -1026,47 +1046,6 @@ var SubscriptionResource = class _SubscriptionResource extends BaseResource {
1026
1046
  null
1027
1047
  );
1028
1048
  }
1029
- /**
1030
- * Changes (upgrades/downgrades) an existing subscription.
1031
- *
1032
- * This method allows changing an active subscription to a new plan with different
1033
- * pricing, billing period, or other terms. The remaining amount from the original
1034
- * subscription can be applied to the new subscription.
1035
- *
1036
- * @param params - Subscription change parameters
1037
- * @param options - Optional request options
1038
- * @returns API response with change result
1039
- * @throws WaffoUnknownStatusError for network errors
1040
- *
1041
- * @example
1042
- * ```typescript
1043
- * const response = await waffo.subscription().change({
1044
- * subscriptionRequest: 'new-request-id',
1045
- * originSubscriptionRequest: 'original-request-id',
1046
- * remainingAmount: '50.00',
1047
- * currency: 'HKD',
1048
- * requestedAt: new Date().toISOString(),
1049
- * notifyUrl: 'https://example.com/webhook',
1050
- * productInfoList: [{
1051
- * description: 'Premium Monthly',
1052
- * periodType: 'MONTH',
1053
- * periodInterval: '1',
1054
- * amount: '199.00',
1055
- * }],
1056
- * userInfo: { userId: 'user-123', userEmail: 'user@example.com' },
1057
- * goodsInfo: { goodsId: 'premium', goodsName: 'Premium Plan' },
1058
- * paymentInfo: { productName: 'SUBSCRIPTION' },
1059
- * });
1060
- *
1061
- * if (response.isSuccess()) {
1062
- * const data = response.getData();
1063
- * if (data?.subscriptionChangeStatus === 'AUTHORIZATION_REQUIRED') {
1064
- * // Redirect user to authorization URL
1065
- * const webUrl = JSON.parse(data.subscriptionAction || '{}').webUrl;
1066
- * }
1067
- * }
1068
- * ```
1069
- */
1070
1049
  async change(params, options) {
1071
1050
  if (!params.requestedAt) {
1072
1051
  params.requestedAt = getCurrentTimestamp();