@swan-admin/swan-ai-measurements
Version:
provides ai measurement suggestion
96 lines (95 loc) • 5.7 kB
JavaScript
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _Auth_socketRef, _Auth_accessKey, _Auth_stagingUrl;
import axios from "axios";
import { API_ENDPOINTS, APP_AUTH_BASE_URL, APP_BASE_WEBSOCKET_URL, REQUIRED_MESSAGE } from "./constants.js";
import { checkParameters, getUrl } from "./utils.js";
class Auth {
constructor(accessKey, stagingUrl = false) {
_Auth_socketRef.set(this, void 0);
_Auth_accessKey.set(this, void 0);
_Auth_stagingUrl.set(this, void 0);
__classPrivateFieldSet(this, _Auth_accessKey, accessKey, "f");
__classPrivateFieldSet(this, _Auth_stagingUrl, stagingUrl, "f");
}
registerUser({ email, appVerifyUrl, gender, height, username }) {
if (!checkParameters(email, appVerifyUrl)) {
throw new Error(REQUIRED_MESSAGE);
}
let body = { username, email, appVerifyUrl };
if (gender && height) {
body = Object.assign(Object.assign({}, body), { attributes: { gender, height } });
}
return axios.post(`${getUrl({ urlName: APP_AUTH_BASE_URL, stagingUrl: __classPrivateFieldGet(this, _Auth_stagingUrl, "f") })}${API_ENDPOINTS.REGISTER_USER}`, body, {
headers: { "X-Api-Key": __classPrivateFieldGet(this, _Auth_accessKey, "f") },
});
}
verifyToken(token) {
if (!checkParameters(token)) {
throw new Error(REQUIRED_MESSAGE);
}
return axios.post(`${getUrl({ urlName: APP_AUTH_BASE_URL, stagingUrl: __classPrivateFieldGet(this, _Auth_stagingUrl, "f") })}${API_ENDPOINTS.VERIFY_USER}`, null, {
params: { token },
headers: { "X-Api-Key": __classPrivateFieldGet(this, _Auth_accessKey, "f") },
});
}
addUser({ scanId, email, name, height, gender, offsetMarketingConsent }) {
if (!checkParameters(scanId, email, height, gender)) {
throw new Error(REQUIRED_MESSAGE);
}
return axios.post(`${getUrl({ urlName: APP_AUTH_BASE_URL, stagingUrl: __classPrivateFieldGet(this, _Auth_stagingUrl, "f") })}${API_ENDPOINTS.ADD_USER}`, { scan_id: scanId, email, name, offsetMarketingConsent, attributes: JSON.stringify({ height, gender }) }, { headers: { "X-Api-Key": __classPrivateFieldGet(this, _Auth_accessKey, "f") } });
}
getUserDetail(email) {
if (!checkParameters(email)) {
throw new Error(REQUIRED_MESSAGE);
}
return axios.get(`${getUrl({ urlName: APP_AUTH_BASE_URL, stagingUrl: __classPrivateFieldGet(this, _Auth_stagingUrl, "f") })}${API_ENDPOINTS.GET_USER_DETAIL}/${email}`, {
headers: { "X-Api-Key": __classPrivateFieldGet(this, _Auth_accessKey, "f") },
});
}
handleAuthSocket({ email, scanId, onError, onSuccess, onClose, onOpen }) {
if (!checkParameters(email, scanId)) {
throw new Error(REQUIRED_MESSAGE);
}
if (__classPrivateFieldGet(this, _Auth_socketRef, "f"))
__classPrivateFieldGet(this, _Auth_socketRef, "f").close();
__classPrivateFieldSet(this, _Auth_socketRef, new WebSocket(`${getUrl({ urlName: APP_BASE_WEBSOCKET_URL, stagingUrl: __classPrivateFieldGet(this, _Auth_stagingUrl, "f") })}${API_ENDPOINTS.AUTH}`), "f");
const detailObj = { email, scanId };
if (__classPrivateFieldGet(this, _Auth_socketRef, "f")) {
__classPrivateFieldGet(this, _Auth_socketRef, "f").onopen = () => {
var _a;
(_a = __classPrivateFieldGet(this, _Auth_socketRef, "f")) === null || _a === void 0 ? void 0 : _a.send(JSON.stringify(detailObj));
onOpen === null || onOpen === void 0 ? void 0 : onOpen();
};
__classPrivateFieldGet(this, _Auth_socketRef, "f").onmessage = (event) => {
let data;
try {
data = JSON.parse(event.data);
}
catch (error) {
console.log(data, error, "noy correct format for data");
return;
}
data = JSON.parse(event.data);
onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(data);
};
__classPrivateFieldGet(this, _Auth_socketRef, "f").onclose = () => {
onClose === null || onClose === void 0 ? void 0 : onClose();
};
__classPrivateFieldGet(this, _Auth_socketRef, "f").onerror = (event) => {
onError === null || onError === void 0 ? void 0 : onError(event);
};
}
}
}
_Auth_socketRef = new WeakMap(), _Auth_accessKey = new WeakMap(), _Auth_stagingUrl = new WeakMap();
export default Auth;