@neetru/sdk 1.0.0 → 1.1.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/CHANGELOG.md +32 -1
- package/dist/auth.cjs +184 -2
- package/dist/auth.cjs.map +1 -1
- package/dist/auth.d.cts +1 -1
- package/dist/auth.d.ts +1 -1
- package/dist/auth.mjs +184 -2
- package/dist/auth.mjs.map +1 -1
- package/dist/catalog.cjs +4 -0
- package/dist/catalog.cjs.map +1 -1
- package/dist/catalog.d.cts +1 -1
- package/dist/catalog.d.ts +1 -1
- package/dist/catalog.mjs +4 -0
- package/dist/catalog.mjs.map +1 -1
- package/dist/checkout.cjs +279 -0
- package/dist/checkout.cjs.map +1 -0
- package/dist/checkout.d.cts +1 -0
- package/dist/checkout.d.ts +1 -0
- package/dist/checkout.mjs +276 -0
- package/dist/checkout.mjs.map +1 -0
- package/dist/db.cjs +4 -0
- package/dist/db.cjs.map +1 -1
- package/dist/db.d.cts +1 -1
- package/dist/db.d.ts +1 -1
- package/dist/db.mjs +4 -0
- package/dist/db.mjs.map +1 -1
- package/dist/entitlements.cjs +4 -0
- package/dist/entitlements.cjs.map +1 -1
- package/dist/entitlements.d.cts +1 -1
- package/dist/entitlements.d.ts +1 -1
- package/dist/entitlements.mjs +4 -0
- package/dist/entitlements.mjs.map +1 -1
- package/dist/index.cjs +187 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.mjs +186 -4
- package/dist/index.mjs.map +1 -1
- package/dist/mocks.d.cts +1 -1
- package/dist/mocks.d.ts +1 -1
- package/dist/react.cjs +158 -0
- package/dist/react.cjs.map +1 -0
- package/dist/react.d.cts +112 -0
- package/dist/react.d.ts +112 -0
- package/dist/react.mjs +134 -0
- package/dist/react.mjs.map +1 -0
- package/dist/support.cjs +4 -0
- package/dist/support.cjs.map +1 -1
- package/dist/support.d.cts +1 -1
- package/dist/support.d.ts +1 -1
- package/dist/support.mjs +4 -0
- package/dist/support.mjs.map +1 -1
- package/dist/telemetry.cjs +4 -0
- package/dist/telemetry.cjs.map +1 -1
- package/dist/telemetry.d.cts +1 -1
- package/dist/telemetry.d.ts +1 -1
- package/dist/telemetry.mjs +4 -0
- package/dist/telemetry.mjs.map +1 -1
- package/dist/{types-PKUaFtBY.d.cts → types-BA53dd8S.d.cts} +83 -1
- package/dist/{types-PKUaFtBY.d.ts → types-BA53dd8S.d.ts} +83 -1
- package/dist/usage.cjs +4 -0
- package/dist/usage.cjs.map +1 -1
- package/dist/usage.d.cts +1 -1
- package/dist/usage.d.ts +1 -1
- package/dist/usage.mjs +4 -0
- package/dist/usage.mjs.map +1 -1
- package/package.json +27 -5
package/dist/auth.d.cts
CHANGED
package/dist/auth.d.ts
CHANGED
package/dist/auth.mjs
CHANGED
|
@@ -68,10 +68,14 @@ async function httpRequest(config, opts) {
|
|
|
68
68
|
headers["content-type"] = "application/json";
|
|
69
69
|
init.body = JSON.stringify(opts.body);
|
|
70
70
|
}
|
|
71
|
+
init.signal = AbortSignal.timeout(3e4);
|
|
71
72
|
let res;
|
|
72
73
|
try {
|
|
73
74
|
res = await config.fetch(url, init);
|
|
74
75
|
} catch (err) {
|
|
76
|
+
if (err instanceof DOMException && err.name === "TimeoutError") {
|
|
77
|
+
throw new NeetruError("network_error", "Network error: timeout after 30s");
|
|
78
|
+
}
|
|
75
79
|
const message = err instanceof Error ? err.message : "fetch failed";
|
|
76
80
|
throw new NeetruError("network_error", `Network error: ${message}`);
|
|
77
81
|
}
|
|
@@ -690,6 +694,181 @@ function createDbNamespace(config) {
|
|
|
690
694
|
};
|
|
691
695
|
}
|
|
692
696
|
|
|
697
|
+
// src/checkout.ts
|
|
698
|
+
function parseStartResponse(raw) {
|
|
699
|
+
if (!raw || typeof raw !== "object") {
|
|
700
|
+
throw new NeetruError("invalid_response", "checkout.start response is not an object");
|
|
701
|
+
}
|
|
702
|
+
const r = raw;
|
|
703
|
+
if (typeof r.intentId !== "string" || !r.intentId) {
|
|
704
|
+
throw new NeetruError("invalid_response", "checkout.start response missing intentId");
|
|
705
|
+
}
|
|
706
|
+
if (typeof r.redirectUrl !== "string" || !r.redirectUrl) {
|
|
707
|
+
throw new NeetruError("invalid_response", "checkout.start response missing redirectUrl");
|
|
708
|
+
}
|
|
709
|
+
return {
|
|
710
|
+
intentId: r.intentId,
|
|
711
|
+
redirectUrl: r.redirectUrl,
|
|
712
|
+
status: r.status ?? "pending",
|
|
713
|
+
expiresAt: typeof r.expiresAt === "string" ? r.expiresAt : (/* @__PURE__ */ new Date()).toISOString(),
|
|
714
|
+
requiresKyc: r.requiresKyc === true
|
|
715
|
+
};
|
|
716
|
+
}
|
|
717
|
+
function parseGetResponse(raw) {
|
|
718
|
+
if (!raw || typeof raw !== "object") {
|
|
719
|
+
throw new NeetruError("invalid_response", "checkout.get response is not an object");
|
|
720
|
+
}
|
|
721
|
+
const r = raw;
|
|
722
|
+
const intent = r.intent;
|
|
723
|
+
if (!intent || typeof intent !== "object") {
|
|
724
|
+
throw new NeetruError("invalid_response", "checkout.get response missing intent");
|
|
725
|
+
}
|
|
726
|
+
if (typeof intent.intentId !== "string") {
|
|
727
|
+
throw new NeetruError("invalid_response", "checkout.get response missing intentId");
|
|
728
|
+
}
|
|
729
|
+
return {
|
|
730
|
+
intentId: intent.intentId,
|
|
731
|
+
uid: intent.uid ?? "",
|
|
732
|
+
targetTenantId: intent.targetTenantId ?? "",
|
|
733
|
+
targetTenantType: intent.targetTenantType ?? "pf",
|
|
734
|
+
productId: intent.productId ?? "",
|
|
735
|
+
planId: intent.planId ?? "",
|
|
736
|
+
callbackUrl: intent.callbackUrl ?? "",
|
|
737
|
+
status: intent.status ?? "pending",
|
|
738
|
+
stripeSessionId: intent.stripeSessionId ?? null,
|
|
739
|
+
expiresAt: intent.expiresAt ?? (/* @__PURE__ */ new Date()).toISOString(),
|
|
740
|
+
isStale: r.isStale === true
|
|
741
|
+
};
|
|
742
|
+
}
|
|
743
|
+
function inBrowser() {
|
|
744
|
+
try {
|
|
745
|
+
return typeof globalThis !== "undefined" && typeof globalThis.window !== "undefined" && typeof globalThis.location !== "undefined" && typeof globalThis.location.assign === "function";
|
|
746
|
+
} catch {
|
|
747
|
+
return false;
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
function performRedirect(url) {
|
|
751
|
+
try {
|
|
752
|
+
globalThis.location.assign(url);
|
|
753
|
+
} catch {
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
function createHttpCheckoutNamespace(config) {
|
|
757
|
+
return {
|
|
758
|
+
async start(input) {
|
|
759
|
+
if (!input?.productId) {
|
|
760
|
+
throw new NeetruError("validation_failed", "checkout.start: productId is required");
|
|
761
|
+
}
|
|
762
|
+
if (!input?.planId) {
|
|
763
|
+
throw new NeetruError("validation_failed", "checkout.start: planId is required");
|
|
764
|
+
}
|
|
765
|
+
if (!input?.callbackUrl) {
|
|
766
|
+
throw new NeetruError("validation_failed", "checkout.start: callbackUrl is required");
|
|
767
|
+
}
|
|
768
|
+
const body = {
|
|
769
|
+
productId: input.productId,
|
|
770
|
+
planId: input.planId,
|
|
771
|
+
callbackUrl: input.callbackUrl
|
|
772
|
+
};
|
|
773
|
+
if (input.tenantType) body.targetTenantType = input.tenantType;
|
|
774
|
+
if (input.tenantId) body.targetTenantId = input.tenantId;
|
|
775
|
+
const raw = await httpRequest(config, {
|
|
776
|
+
method: "POST",
|
|
777
|
+
path: "/api/v1/checkout/intents",
|
|
778
|
+
body,
|
|
779
|
+
requireAuth: true
|
|
780
|
+
});
|
|
781
|
+
const result = parseStartResponse(raw);
|
|
782
|
+
const shouldRedirect = input.autoRedirect !== false;
|
|
783
|
+
if (shouldRedirect && inBrowser()) {
|
|
784
|
+
performRedirect(result.redirectUrl);
|
|
785
|
+
}
|
|
786
|
+
return result;
|
|
787
|
+
},
|
|
788
|
+
async get(intentId) {
|
|
789
|
+
if (!intentId || typeof intentId !== "string") {
|
|
790
|
+
throw new NeetruError("validation_failed", "checkout.get: intentId is required");
|
|
791
|
+
}
|
|
792
|
+
const raw = await httpRequest(config, {
|
|
793
|
+
method: "GET",
|
|
794
|
+
path: `/api/v1/checkout/intents/${encodeURIComponent(intentId)}`,
|
|
795
|
+
requireAuth: true
|
|
796
|
+
});
|
|
797
|
+
return parseGetResponse(raw);
|
|
798
|
+
},
|
|
799
|
+
async cancel(intentId) {
|
|
800
|
+
if (!intentId || typeof intentId !== "string") {
|
|
801
|
+
throw new NeetruError("validation_failed", "checkout.cancel: intentId is required");
|
|
802
|
+
}
|
|
803
|
+
const raw = await httpRequest(config, {
|
|
804
|
+
method: "DELETE",
|
|
805
|
+
path: `/api/v1/checkout/intents/${encodeURIComponent(intentId)}`,
|
|
806
|
+
requireAuth: true
|
|
807
|
+
});
|
|
808
|
+
return {
|
|
809
|
+
ok: true,
|
|
810
|
+
alreadyCancelled: raw?.alreadyCancelled === true
|
|
811
|
+
};
|
|
812
|
+
}
|
|
813
|
+
};
|
|
814
|
+
}
|
|
815
|
+
var MockCheckout = class {
|
|
816
|
+
intents = /* @__PURE__ */ new Map();
|
|
817
|
+
async start(input) {
|
|
818
|
+
if (!input?.productId) {
|
|
819
|
+
throw new NeetruError("validation_failed", "checkout.start: productId is required");
|
|
820
|
+
}
|
|
821
|
+
if (!input?.planId) {
|
|
822
|
+
throw new NeetruError("validation_failed", "checkout.start: planId is required");
|
|
823
|
+
}
|
|
824
|
+
if (!input?.callbackUrl) {
|
|
825
|
+
throw new NeetruError("validation_failed", "checkout.start: callbackUrl is required");
|
|
826
|
+
}
|
|
827
|
+
const intentId = `chk_mock_${Math.random().toString(36).slice(2, 10)}`;
|
|
828
|
+
const expiresAt = new Date(Date.now() + 15 * 60 * 1e3).toISOString();
|
|
829
|
+
const redirectUrl = `https://localhost:9003/portal/checkout/${intentId}`;
|
|
830
|
+
this.intents.set(intentId, {
|
|
831
|
+
intentId,
|
|
832
|
+
uid: "dev-fixture-uid",
|
|
833
|
+
targetTenantId: input.tenantId ?? "dev-fixture-uid",
|
|
834
|
+
targetTenantType: input.tenantType ?? "pf",
|
|
835
|
+
productId: input.productId,
|
|
836
|
+
planId: input.planId,
|
|
837
|
+
callbackUrl: input.callbackUrl,
|
|
838
|
+
status: "pending",
|
|
839
|
+
stripeSessionId: null,
|
|
840
|
+
expiresAt
|
|
841
|
+
});
|
|
842
|
+
return {
|
|
843
|
+
intentId,
|
|
844
|
+
redirectUrl,
|
|
845
|
+
status: "pending",
|
|
846
|
+
expiresAt,
|
|
847
|
+
requiresKyc: false
|
|
848
|
+
};
|
|
849
|
+
}
|
|
850
|
+
async get(intentId) {
|
|
851
|
+
const found = this.intents.get(intentId);
|
|
852
|
+
if (!found) {
|
|
853
|
+
throw new NeetruError("not_found", `Mock intent ${intentId} not found`);
|
|
854
|
+
}
|
|
855
|
+
return { ...found };
|
|
856
|
+
}
|
|
857
|
+
async cancel(intentId) {
|
|
858
|
+
const found = this.intents.get(intentId);
|
|
859
|
+
if (!found) {
|
|
860
|
+
throw new NeetruError("not_found", `Mock intent ${intentId} not found`);
|
|
861
|
+
}
|
|
862
|
+
const alreadyCancelled = found.status === "cancelled";
|
|
863
|
+
this.intents.set(intentId, { ...found, status: "cancelled" });
|
|
864
|
+
return { ok: true, alreadyCancelled };
|
|
865
|
+
}
|
|
866
|
+
};
|
|
867
|
+
function createCheckoutNamespace(config) {
|
|
868
|
+
if (config.env === "dev") return new MockCheckout();
|
|
869
|
+
return createHttpCheckoutNamespace(config);
|
|
870
|
+
}
|
|
871
|
+
|
|
693
872
|
// src/mocks.ts
|
|
694
873
|
var DEV_FIXTURE_USER = Object.freeze({
|
|
695
874
|
uid: "dev-fixture-uid-0001",
|
|
@@ -1047,7 +1226,9 @@ function createOidcAuthNamespace(config) {
|
|
|
1047
1226
|
const redirectUri = options?.redirectUri ?? globalThis.location.origin;
|
|
1048
1227
|
const scope = options?.scope ?? "openid profile email";
|
|
1049
1228
|
const state = options?.postLoginRedirect ?? globalThis.location.href;
|
|
1050
|
-
const
|
|
1229
|
+
const overrideAuthUrl = typeof globalThis.NEETRU_AUTH_URL === "string" ? globalThis.NEETRU_AUTH_URL : null;
|
|
1230
|
+
const idpOrigin = overrideAuthUrl ?? config.baseUrl.replace(/^https?:\/\/api\./, "https://auth.");
|
|
1231
|
+
const url = new URL("/api/v1/oauth/authorize", idpOrigin);
|
|
1051
1232
|
url.searchParams.set("response_type", "code");
|
|
1052
1233
|
url.searchParams.set("redirect_uri", redirectUri);
|
|
1053
1234
|
url.searchParams.set("scope", scope);
|
|
@@ -1125,7 +1306,8 @@ function createNeetruClient(config = {}) {
|
|
|
1125
1306
|
telemetry: createTelemetryNamespace(resolved),
|
|
1126
1307
|
usage,
|
|
1127
1308
|
support,
|
|
1128
|
-
db
|
|
1309
|
+
db,
|
|
1310
|
+
checkout: createCheckoutNamespace(resolved)
|
|
1129
1311
|
});
|
|
1130
1312
|
return client;
|
|
1131
1313
|
}
|