@neetru/sdk 1.0.0 → 1.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/dist/auth.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { N as NeetruClientConfig, a as NeetruClient } from './types-PKUaFtBY.cjs';
1
+ import { N as NeetruClientConfig, a as NeetruClient } from './types-BA53dd8S.cjs';
2
2
 
3
3
  /**
4
4
  * Cria uma instância imutável do cliente Neetru SDK.
package/dist/auth.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { N as NeetruClientConfig, a as NeetruClient } from './types-PKUaFtBY.js';
1
+ import { N as NeetruClientConfig, a as NeetruClient } from './types-BA53dd8S.js';
2
2
 
3
3
  /**
4
4
  * Cria uma instância imutável do cliente Neetru SDK.
package/dist/auth.mjs CHANGED
@@ -690,6 +690,181 @@ function createDbNamespace(config) {
690
690
  };
691
691
  }
692
692
 
693
+ // src/checkout.ts
694
+ function parseStartResponse(raw) {
695
+ if (!raw || typeof raw !== "object") {
696
+ throw new NeetruError("invalid_response", "checkout.start response is not an object");
697
+ }
698
+ const r = raw;
699
+ if (typeof r.intentId !== "string" || !r.intentId) {
700
+ throw new NeetruError("invalid_response", "checkout.start response missing intentId");
701
+ }
702
+ if (typeof r.redirectUrl !== "string" || !r.redirectUrl) {
703
+ throw new NeetruError("invalid_response", "checkout.start response missing redirectUrl");
704
+ }
705
+ return {
706
+ intentId: r.intentId,
707
+ redirectUrl: r.redirectUrl,
708
+ status: r.status ?? "pending",
709
+ expiresAt: typeof r.expiresAt === "string" ? r.expiresAt : (/* @__PURE__ */ new Date()).toISOString(),
710
+ requiresKyc: r.requiresKyc === true
711
+ };
712
+ }
713
+ function parseGetResponse(raw) {
714
+ if (!raw || typeof raw !== "object") {
715
+ throw new NeetruError("invalid_response", "checkout.get response is not an object");
716
+ }
717
+ const r = raw;
718
+ const intent = r.intent;
719
+ if (!intent || typeof intent !== "object") {
720
+ throw new NeetruError("invalid_response", "checkout.get response missing intent");
721
+ }
722
+ if (typeof intent.intentId !== "string") {
723
+ throw new NeetruError("invalid_response", "checkout.get response missing intentId");
724
+ }
725
+ return {
726
+ intentId: intent.intentId,
727
+ uid: intent.uid ?? "",
728
+ targetTenantId: intent.targetTenantId ?? "",
729
+ targetTenantType: intent.targetTenantType ?? "pf",
730
+ productId: intent.productId ?? "",
731
+ planId: intent.planId ?? "",
732
+ callbackUrl: intent.callbackUrl ?? "",
733
+ status: intent.status ?? "pending",
734
+ stripeSessionId: intent.stripeSessionId ?? null,
735
+ expiresAt: intent.expiresAt ?? (/* @__PURE__ */ new Date()).toISOString(),
736
+ isStale: r.isStale === true
737
+ };
738
+ }
739
+ function inBrowser() {
740
+ try {
741
+ return typeof globalThis !== "undefined" && typeof globalThis.window !== "undefined" && typeof globalThis.location !== "undefined" && typeof globalThis.location.assign === "function";
742
+ } catch {
743
+ return false;
744
+ }
745
+ }
746
+ function performRedirect(url) {
747
+ try {
748
+ globalThis.location.assign(url);
749
+ } catch {
750
+ }
751
+ }
752
+ function createHttpCheckoutNamespace(config) {
753
+ return {
754
+ async start(input) {
755
+ if (!input?.productId) {
756
+ throw new NeetruError("validation_failed", "checkout.start: productId is required");
757
+ }
758
+ if (!input?.planId) {
759
+ throw new NeetruError("validation_failed", "checkout.start: planId is required");
760
+ }
761
+ if (!input?.callbackUrl) {
762
+ throw new NeetruError("validation_failed", "checkout.start: callbackUrl is required");
763
+ }
764
+ const body = {
765
+ productId: input.productId,
766
+ planId: input.planId,
767
+ callbackUrl: input.callbackUrl
768
+ };
769
+ if (input.tenantType) body.targetTenantType = input.tenantType;
770
+ if (input.tenantId) body.targetTenantId = input.tenantId;
771
+ const raw = await httpRequest(config, {
772
+ method: "POST",
773
+ path: "/api/v1/checkout/intents",
774
+ body,
775
+ requireAuth: true
776
+ });
777
+ const result = parseStartResponse(raw);
778
+ const shouldRedirect = input.autoRedirect !== false;
779
+ if (shouldRedirect && inBrowser()) {
780
+ performRedirect(result.redirectUrl);
781
+ }
782
+ return result;
783
+ },
784
+ async get(intentId) {
785
+ if (!intentId || typeof intentId !== "string") {
786
+ throw new NeetruError("validation_failed", "checkout.get: intentId is required");
787
+ }
788
+ const raw = await httpRequest(config, {
789
+ method: "GET",
790
+ path: `/api/v1/checkout/intents/${encodeURIComponent(intentId)}`,
791
+ requireAuth: true
792
+ });
793
+ return parseGetResponse(raw);
794
+ },
795
+ async cancel(intentId) {
796
+ if (!intentId || typeof intentId !== "string") {
797
+ throw new NeetruError("validation_failed", "checkout.cancel: intentId is required");
798
+ }
799
+ const raw = await httpRequest(config, {
800
+ method: "DELETE",
801
+ path: `/api/v1/checkout/intents/${encodeURIComponent(intentId)}`,
802
+ requireAuth: true
803
+ });
804
+ return {
805
+ ok: true,
806
+ alreadyCancelled: raw?.alreadyCancelled === true
807
+ };
808
+ }
809
+ };
810
+ }
811
+ var MockCheckout = class {
812
+ intents = /* @__PURE__ */ new Map();
813
+ async start(input) {
814
+ if (!input?.productId) {
815
+ throw new NeetruError("validation_failed", "checkout.start: productId is required");
816
+ }
817
+ if (!input?.planId) {
818
+ throw new NeetruError("validation_failed", "checkout.start: planId is required");
819
+ }
820
+ if (!input?.callbackUrl) {
821
+ throw new NeetruError("validation_failed", "checkout.start: callbackUrl is required");
822
+ }
823
+ const intentId = `chk_mock_${Math.random().toString(36).slice(2, 10)}`;
824
+ const expiresAt = new Date(Date.now() + 15 * 60 * 1e3).toISOString();
825
+ const redirectUrl = `https://localhost:9003/portal/checkout/${intentId}`;
826
+ this.intents.set(intentId, {
827
+ intentId,
828
+ uid: "dev-fixture-uid",
829
+ targetTenantId: input.tenantId ?? "dev-fixture-uid",
830
+ targetTenantType: input.tenantType ?? "pf",
831
+ productId: input.productId,
832
+ planId: input.planId,
833
+ callbackUrl: input.callbackUrl,
834
+ status: "pending",
835
+ stripeSessionId: null,
836
+ expiresAt
837
+ });
838
+ return {
839
+ intentId,
840
+ redirectUrl,
841
+ status: "pending",
842
+ expiresAt,
843
+ requiresKyc: false
844
+ };
845
+ }
846
+ async get(intentId) {
847
+ const found = this.intents.get(intentId);
848
+ if (!found) {
849
+ throw new NeetruError("not_found", `Mock intent ${intentId} not found`);
850
+ }
851
+ return { ...found };
852
+ }
853
+ async cancel(intentId) {
854
+ const found = this.intents.get(intentId);
855
+ if (!found) {
856
+ throw new NeetruError("not_found", `Mock intent ${intentId} not found`);
857
+ }
858
+ const alreadyCancelled = found.status === "cancelled";
859
+ this.intents.set(intentId, { ...found, status: "cancelled" });
860
+ return { ok: true, alreadyCancelled };
861
+ }
862
+ };
863
+ function createCheckoutNamespace(config) {
864
+ if (config.env === "dev") return new MockCheckout();
865
+ return createHttpCheckoutNamespace(config);
866
+ }
867
+
693
868
  // src/mocks.ts
694
869
  var DEV_FIXTURE_USER = Object.freeze({
695
870
  uid: "dev-fixture-uid-0001",
@@ -1125,7 +1300,8 @@ function createNeetruClient(config = {}) {
1125
1300
  telemetry: createTelemetryNamespace(resolved),
1126
1301
  usage,
1127
1302
  support,
1128
- db
1303
+ db,
1304
+ checkout: createCheckoutNamespace(resolved)
1129
1305
  });
1130
1306
  return client;
1131
1307
  }