@merit-systems/echo-react-sdk 1.0.1 → 1.0.2
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/echo-react-sdk.umd.js +1 -1
- package/dist/echo-react-sdk.umd.js.map +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +46 -28
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -3,7 +3,7 @@ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { en
|
|
|
3
3
|
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
4
4
|
var _a;
|
|
5
5
|
import { jsx, jsxs, Fragment } from "react/jsx-runtime";
|
|
6
|
-
import React, { createContext, useState,
|
|
6
|
+
import React, { useEffect, createContext, useState, useCallback, useContext, useRef } from "react";
|
|
7
7
|
class InvalidTokenError extends Error {
|
|
8
8
|
}
|
|
9
9
|
InvalidTokenError.prototype.name = "InvalidTokenError";
|
|
@@ -7102,6 +7102,18 @@ var EchoClient = class {
|
|
|
7102
7102
|
const response = await this.getSupportedModels();
|
|
7103
7103
|
return response.models_by_provider;
|
|
7104
7104
|
}
|
|
7105
|
+
/**
|
|
7106
|
+
* Register a referral code for the authenticated user
|
|
7107
|
+
* @param echoAppId The Echo app ID to register the referral code for
|
|
7108
|
+
* @param code The referral code to register
|
|
7109
|
+
*/
|
|
7110
|
+
async registerReferralCode(echoAppId, code) {
|
|
7111
|
+
const request = { echoAppId, code };
|
|
7112
|
+
const response = await this.http.post("/api/v1/user/referral", request).catch((error) => {
|
|
7113
|
+
throw this.handleError(error, "Failed to register referral code");
|
|
7114
|
+
});
|
|
7115
|
+
return response.data;
|
|
7116
|
+
}
|
|
7105
7117
|
handleError(error, message) {
|
|
7106
7118
|
var _a2, _b;
|
|
7107
7119
|
if ((_b = (_a2 = error.response) == null ? void 0 : _a2.data) == null ? void 0 : _b.error) {
|
|
@@ -7113,6 +7125,35 @@ var EchoClient = class {
|
|
|
7113
7125
|
return new Error(message);
|
|
7114
7126
|
}
|
|
7115
7127
|
};
|
|
7128
|
+
function useRegisterReferralCode({
|
|
7129
|
+
appId,
|
|
7130
|
+
client,
|
|
7131
|
+
onSuccess,
|
|
7132
|
+
onError
|
|
7133
|
+
}) {
|
|
7134
|
+
useEffect(() => {
|
|
7135
|
+
const registerReferralCode = async () => {
|
|
7136
|
+
if (typeof window === "undefined") return;
|
|
7137
|
+
const urlParams = new URLSearchParams(window.location.search);
|
|
7138
|
+
const referralCode = urlParams.get("referralCode");
|
|
7139
|
+
if (!referralCode) return;
|
|
7140
|
+
const result = await client.registerReferralCode(appId, referralCode);
|
|
7141
|
+
if (!result) return;
|
|
7142
|
+
urlParams.delete("referralCode");
|
|
7143
|
+
window.history.replaceState(
|
|
7144
|
+
{},
|
|
7145
|
+
"",
|
|
7146
|
+
`${window.location.pathname}${urlParams.toString() ? `?${urlParams.toString()}` : ""}`
|
|
7147
|
+
);
|
|
7148
|
+
if (result.success) {
|
|
7149
|
+
onSuccess == null ? void 0 : onSuccess();
|
|
7150
|
+
} else {
|
|
7151
|
+
onError == null ? void 0 : onError(result.message);
|
|
7152
|
+
}
|
|
7153
|
+
};
|
|
7154
|
+
registerReferralCode();
|
|
7155
|
+
}, [appId, client, onSuccess, onError]);
|
|
7156
|
+
}
|
|
7116
7157
|
const EchoContext = createContext(
|
|
7117
7158
|
void 0
|
|
7118
7159
|
);
|
|
@@ -7341,6 +7382,10 @@ function EchoProvider({ config, children }) {
|
|
|
7341
7382
|
};
|
|
7342
7383
|
initializeAuth();
|
|
7343
7384
|
}, [isClient, userManager, loadUserData]);
|
|
7385
|
+
useRegisterReferralCode({
|
|
7386
|
+
appId: config.appId,
|
|
7387
|
+
client: createClientWithToken(token || "")
|
|
7388
|
+
});
|
|
7344
7389
|
useEffect(() => {
|
|
7345
7390
|
if (!userManager) {
|
|
7346
7391
|
return;
|
|
@@ -7348,36 +7393,9 @@ function EchoProvider({ config, children }) {
|
|
|
7348
7393
|
const handleUserLoaded = (oidcUser) => {
|
|
7349
7394
|
loadUserData(oidcUser);
|
|
7350
7395
|
};
|
|
7351
|
-
const handleUserUnloaded = () => {
|
|
7352
|
-
console.log("User unloaded, clearing state");
|
|
7353
|
-
setUser(null);
|
|
7354
|
-
setBalance(null);
|
|
7355
|
-
setToken(null);
|
|
7356
|
-
};
|
|
7357
|
-
const handleAccessTokenExpiring = () => {
|
|
7358
|
-
console.log("Access token expiring, refreshing");
|
|
7359
|
-
userManager.signinSilent();
|
|
7360
|
-
};
|
|
7361
|
-
const handleAccessTokenExpired = async () => {
|
|
7362
|
-
console.log("Access token expired, reauthenticating");
|
|
7363
|
-
await userManager.signinSilent();
|
|
7364
|
-
};
|
|
7365
|
-
const handleSilentRenewError = async (err) => {
|
|
7366
|
-
console.error("Silent renew failed:", err);
|
|
7367
|
-
await clearAuth();
|
|
7368
|
-
setError("Session renewal failed. Please sign in again.");
|
|
7369
|
-
};
|
|
7370
7396
|
userManager.events.addUserLoaded(handleUserLoaded);
|
|
7371
|
-
userManager.events.addUserUnloaded(handleUserUnloaded);
|
|
7372
|
-
userManager.events.addAccessTokenExpiring(handleAccessTokenExpiring);
|
|
7373
|
-
userManager.events.addAccessTokenExpired(handleAccessTokenExpired);
|
|
7374
|
-
userManager.events.addSilentRenewError(handleSilentRenewError);
|
|
7375
7397
|
return () => {
|
|
7376
7398
|
userManager.events.removeUserLoaded(handleUserLoaded);
|
|
7377
|
-
userManager.events.removeUserUnloaded(handleUserUnloaded);
|
|
7378
|
-
userManager.events.removeAccessTokenExpiring(handleAccessTokenExpiring);
|
|
7379
|
-
userManager.events.removeAccessTokenExpired(handleAccessTokenExpired);
|
|
7380
|
-
userManager.events.removeSilentRenewError(handleSilentRenewError);
|
|
7381
7399
|
};
|
|
7382
7400
|
}, [userManager, loadUserData, clearAuth]);
|
|
7383
7401
|
const contextValue = {
|