UNPKG

@swan-admin/swan-ai-measurements

Version:
112 lines (111 loc) 6.96 kB
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_instances, _Auth_socketRef, _Auth_accessKey, _Auth_urlType, _Auth_token, _Auth_getHeaders; 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"; import { URLType } from "./enum.js"; class Auth { constructor(accessKey, urlType = URLType.PROD, token) { _Auth_instances.add(this); _Auth_socketRef.set(this, void 0); _Auth_accessKey.set(this, void 0); _Auth_urlType.set(this, void 0); _Auth_token.set(this, void 0); __classPrivateFieldSet(this, _Auth_accessKey, accessKey, "f"); __classPrivateFieldSet(this, _Auth_urlType, urlType, "f"); __classPrivateFieldSet(this, _Auth_token, token, "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, urlType: __classPrivateFieldGet(this, _Auth_urlType, "f") })}${API_ENDPOINTS.REGISTER_USER}`, body, { headers: __classPrivateFieldGet(this, _Auth_instances, "m", _Auth_getHeaders).call(this), }); } verifyToken(token) { if (!checkParameters(token)) { throw new Error(REQUIRED_MESSAGE); } return axios.post(`${getUrl({ urlName: APP_AUTH_BASE_URL, urlType: __classPrivateFieldGet(this, _Auth_urlType, "f") })}${API_ENDPOINTS.VERIFY_USER}`, null, { params: { token }, headers: __classPrivateFieldGet(this, _Auth_instances, "m", _Auth_getHeaders).call(this), }); } 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, urlType: __classPrivateFieldGet(this, _Auth_urlType, "f") })}${API_ENDPOINTS.ADD_USER}`, { scan_id: scanId, email, name, offsetMarketingConsent, attributes: JSON.stringify({ height, gender }) }, { headers: __classPrivateFieldGet(this, _Auth_instances, "m", _Auth_getHeaders).call(this) }); } userProfile() { return axios.get(`${getUrl({ urlName: APP_AUTH_BASE_URL, urlType: __classPrivateFieldGet(this, _Auth_urlType, "f") })}${API_ENDPOINTS.USER_PROFILE}`, { headers: __classPrivateFieldGet(this, _Auth_instances, "m", _Auth_getHeaders).call(this), }); } userExists() { return axios.get(`${getUrl({ urlName: APP_AUTH_BASE_URL, urlType: __classPrivateFieldGet(this, _Auth_urlType, "f") })}${API_ENDPOINTS.USER_EXISTS}`, { headers: __classPrivateFieldGet(this, _Auth_instances, "m", _Auth_getHeaders).call(this), }); } getUserDetail(email) { if (!checkParameters(email)) { throw new Error(REQUIRED_MESSAGE); } return axios.get(`${getUrl({ urlName: APP_AUTH_BASE_URL, urlType: __classPrivateFieldGet(this, _Auth_urlType, "f") })}${API_ENDPOINTS.GET_USER_DETAIL}/${email}`, { headers: __classPrivateFieldGet(this, _Auth_instances, "m", _Auth_getHeaders).call(this), }); } 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, urlType: __classPrivateFieldGet(this, _Auth_urlType, "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_urlType = new WeakMap(), _Auth_token = new WeakMap(), _Auth_instances = new WeakSet(), _Auth_getHeaders = function _Auth_getHeaders() { return Object.assign(Object.assign({}, (__classPrivateFieldGet(this, _Auth_accessKey, "f") ? { "X-Api-Key": __classPrivateFieldGet(this, _Auth_accessKey, "f") } : {})), (__classPrivateFieldGet(this, _Auth_token, "f") ? { Authorization: `Bearer ${__classPrivateFieldGet(this, _Auth_token, "f")}` } : {})); }; export default Auth;