UNPKG

@questlabs/core

Version:
214 lines (213 loc) 8.65 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Social = exports.Abstract = exports.Core = void 0; const config_1 = require("./utils/config"); const Core = __importStar(require("./modules/core")); exports.Core = Core; const Abstract = __importStar(require("./modules/abstract")); exports.Abstract = Abstract; const Social = __importStar(require("./modules/social")); exports.Social = Social; const helperFunctions_1 = require("./utils/helperFunctions"); const axios_1 = __importDefault(require("axios")); const constants_1 = require("./utils/constants"); // import * as Sentry from "@sentry/node"; // import { ProfilingIntegration } from "@sentry/profiling-node"; class QuestSdk { constructor({ apiKey, apiSecret, entityAuthenticationToken, stagingEnvironment = false, userAgent, entityId, platform, }) { this.apiKey = apiKey; this.apiSecret = apiSecret; this.userAgent = userAgent || constants_1.USER_AGENT.BROWSER; this.entityAuthenticationToken = entityAuthenticationToken; this.entityId = entityId; this.stagingEnvironment = stagingEnvironment; this.baseUrl = stagingEnvironment ? config_1.STAGING_URL : config_1.PRODUCTION_URL; // Sentry.init({ // dsn: SENTRY_PROJECT_ID, // integrations: [new ProfilingIntegration()], // // Performance Monitoring // tracesSampleRate: 1.0, // // Set sampling rate for profiling - this is relative to tracesSampleRate // profilesSampleRate: 1.0, // ...(platform && { environment: platform }), // }); } makeRequest({ url, method, headers, queryParams, body, timeout }) { var _a, _b; return __awaiter(this, void 0, void 0, function* () { try { (0, helperFunctions_1.validateParams)({ apiKey: this.apiKey }); const response = yield (0, axios_1.default)({ method, url: this.baseUrl + url, params: queryParams, headers: Object.assign(Object.assign(Object.assign(Object.assign({ apiKey: this.apiKey }, (typeof this.userId == "string" && { userId: this.userId })), (typeof this.token === "string" && { token: this.token })), { entityId: this.entityId, ["User-Agent"]: this.userAgent, ["entity-authentication-token"]: this.entityAuthenticationToken }), headers), data: body, timeout: timeout || undefined, }); return response.data; } catch (error) { this.captureSentryException(error); if (axios_1.default.isAxiosError(error) && error.code === 'ECONNABORTED') { throw new Error('timeout'); } if ((_a = error === null || error === void 0 ? void 0 : error.response) === null || _a === void 0 ? void 0 : _a.data) { return (_b = error === null || error === void 0 ? void 0 : error.response) === null || _b === void 0 ? void 0 : _b.data; } throw error; } }); } setEntityId({ entityId }) { this.entityId = entityId; } getEntityId() { return this.entityId; } setUser({ userId, token }) { this.userId = userId; this.token = token; } getUser() { return { userId: this.userId, token: this.token, }; } setEnvironment({ stagingEnvironment }) { this.stagingEnvironment = stagingEnvironment; this.baseUrl = stagingEnvironment ? config_1.STAGING_URL : config_1.PRODUCTION_URL; } getEnvironment() { return { stagingEnvironment: this.stagingEnvironment }; } isStagingEnvironment() { return this.stagingEnvironment; } setUserAgent({ userAgent }) { this.userAgent = userAgent; } getUserAgent() { return this.userAgent; } setEntityAuthenticationToken({ token }) { this.entityAuthenticationToken = token; } getEntityAuthenticationToken() { return this.entityAuthenticationToken; } setApiKeys({ apiKey, apiSecret }) { this.apiKey = apiKey; this.apiSecret = apiSecret; } getApiKeys() { return { apiKey: this.apiKey, apiSecret: this.apiSecret, }; } getBaseUrl() { return this.baseUrl; } captureSentryException(error) { // Sentry.captureException(error); } getDistinctId() { if (typeof localStorage !== 'undefined') { return localStorage.getItem('distinctId') || this.generateDistinctId(); } else { return this.generateDistinctId(); } } generateDistinctId() { const id = 'sdk-' + Math.random().toString(36).substring(2, 12); if (typeof localStorage !== 'undefined') { localStorage.setItem('distinctId', id); } return id; } getUrlInformation() { if (typeof window !== 'undefined') { return { href: window.location.href, host: window.location.host, hostname: window.location.hostname, origin: window.location.origin, pathname: window.location.pathname, }; } else return; } getUserInfo() { return __awaiter(this, void 0, void 0, function* () { try { // const response = await axios.get('https://api.ipify.org?format=json'); let data; if (typeof window === 'undefined') { const response = yield axios_1.default.get("https://ipinfo.io/json"); data = response.data; } else { const response = yield fetch("https://ipinfo.io/json"); data = yield response.json(); } // sample response data: // { // ip: '117.209.59.18', // city: 'Vellore', // region: 'Tamil Nadu', // country: 'IN', // loc: '12.9184,79.1325', // org: 'AS9829 National Internet Backbone', // postal: '614620', // timezone: 'Asia/Kolkata', // readme: 'https://ipinfo.io/missingauth' // } return data; } catch (error) { console.error('Error fetching IP address:', error); return null; } }); } } exports.default = QuestSdk;