UNPKG

@open-condo/miniapp-utils

Version:

A set of helper functions / components / hooks used to build new condo apps fast

333 lines (320 loc) 10.6 kB
// src/helpers/analytics/instance.ts import { Analytics as DefaultAnalytics } from "analytics"; // src/helpers/analytics/middlewares.ts function _addGroupingProperties(data) { const { instance } = data; for (const groupName of instance.groups) { const groupKey = Analytics.getGroupKey(groupName); const groupValue = instance.storage.getItem(groupKey); if (typeof groupValue === "string") { const groupAttrName = `groups.${groupName}`; data.payload.properties[groupAttrName] = groupValue; } } return data; } var GroupingMiddlewarePlugin = { name: "analytics-plugin-grouping", track: _addGroupingProperties, page: _addGroupingProperties }; // src/helpers/analytics/instance.ts var Analytics = class _Analytics { constructor(config) { this._groups = /* @__PURE__ */ new Set(); this._analytics = DefaultAnalytics({ ...config, plugins: [ GroupingMiddlewarePlugin, ...config.plugins || [] ] }); this._analytics.groups = this._groups; } /** * Tracks type-safe business events. Recommended to use in most cases in app's codebase. * To add an event, modify "Events" generic. */ async track(eventName, eventData) { await this._analytics.track(eventName, eventData); } /** * Tracks untyped analytics events, used mainly for external sources (bridge / ui-kit / messages, etc.) * @deprecated It's not recommended to use this in your business logic, consider using typed "track" instead */ async trackUntyped(eventName, eventData) { await this._analytics.track(eventName, eventData); } /** * Tracks page changing in SPAs */ async pageView(data) { await this._analytics.page(data); } /** * Identifies user in analytics provider. * To specify all possible shape of user's data, modify "UserData" generic * * NOTE: Analytics plugins don't have a fixed behavior on how to handle consecutive identify calls. * Some of them affect only subsequent events, others affect all user events. * Therefore, it is not recommended to put cohort-specific data (organization, address, language, etc.) here. * Instead, use something like "group" method if your plugins supports it. */ async identify(userId, userData) { await this._analytics.identify(userId, userData); } /** * Resets analytics providers */ async reset() { for (const groupName of this._groups) { const groupKey = _Analytics.getGroupKey(groupName); this._analytics.storage.removeItem(groupKey); } this._groups.clear(); await this._analytics.reset(); } static getGroupKey(groupName) { return ["analytics", "groups", groupName].join(":"); } /** * Associates the user with a group, adding the attributes `groups.${groupName} = groupId` * to all subsequent analytic queries for the user * @example * analytics.setGroup('organization', organizationId) */ setGroup(groupName, groupId) { const groupKey = _Analytics.getGroupKey(groupName); this._groups.add(groupName); this._analytics.storage.setItem(groupKey, groupId); } /** * Removes the current user from the group, stripping the “groups.${groupName}” * attribute from all subsequent eventualities * @example * deleteOrganization() * .then(() => analytics.removeGroup('organization')) */ removeGroup(groupName) { const groupKey = _Analytics.getGroupKey(groupName); this._analytics.storage.removeItem(groupKey); this._groups.delete(groupName); } }; // src/helpers/apollo.ts import { parse as parseCookieString, serialize as serializeCookie } from "cookie"; import { setCookie as setCookie2, getCookies } from "cookies-next"; // src/helpers/sender.ts import { getCookie, setCookie } from "cookies-next"; // src/helpers/uuid.ts import { randomBytes } from "crypto"; function generateUUIDv4() { let randomValues; if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") { return crypto.randomUUID(); } else if (typeof window !== "undefined" && window.crypto && window.crypto.getRandomValues) { randomValues = new Uint8Array(16); window.crypto.getRandomValues(randomValues); } else { randomValues = randomBytes(16); } randomValues[6] = randomValues[6] & 15 | 64; randomValues[8] = randomValues[8] & 63 | 128; return [...randomValues].map((value, index) => { const hex = value.toString(16).padStart(2, "0"); if (index === 4 || index === 6 || index === 8 || index === 10) { return `-${hex}`; } return hex; }).join(""); } // src/helpers/sender.ts var FINGERPRINT_ID_COOKIE_NAME = "fingerprint"; var FINGERPRINT_ID_LENGTH = 12; function makeId(length) { const croppedLength = Math.min(length, 32); return generateUUIDv4().replaceAll("-", "").substring(0, croppedLength); } function generateFingerprint() { return makeId(FINGERPRINT_ID_LENGTH); } function getClientSideFingerprint() { let fingerprint = getCookie(FINGERPRINT_ID_COOKIE_NAME); if (!fingerprint) { fingerprint = generateFingerprint(); setCookie(FINGERPRINT_ID_COOKIE_NAME, fingerprint); } return fingerprint; } function getClientSideSenderInfo() { return { dv: 1, fingerprint: getClientSideFingerprint() }; } // src/helpers/apollo.ts var SSR_DEFAULT_FINGERPRINT = "webAppSSR"; var COOKIE_HEADER_NAME = "cookie"; var REMOTE_APP_HEADER_NAME = "x-remote-app"; var REMOTE_VERSION_HEADER_NAME = "x-remote-version"; var REMOTE_CLIENT_HEADER_NANE = "x-remote-client"; var REMOTE_ENV_HEADER_NAME = "x-remote-env"; var TARGET_HEADER_NAME = "x-target"; var START_REQUEST_ID_HEADER_NAME = "x-start-request-id"; var PARENT_REQUEST_ID_HEADER_NAME = "x-parent-request-id"; function generateRequestId() { return `BR${generateUUIDv4().replaceAll("-", "")}`; } function getTracingMiddleware(options) { return function(operation, forward) { operation.setContext((previousContext) => { const { headers: previousHeaders } = previousContext; const reqId = generateRequestId(); const headers = { ...previousHeaders, [REMOTE_APP_HEADER_NAME]: options.serviceUrl, [REMOTE_VERSION_HEADER_NAME]: options.codeVersion, [PARENT_REQUEST_ID_HEADER_NAME]: reqId, [START_REQUEST_ID_HEADER_NAME]: reqId }; if (options.target) { headers[TARGET_HEADER_NAME] = options.target; } headers[REMOTE_ENV_HEADER_NAME] = typeof document === "undefined" ? "SSR" : "CSR"; if (typeof document !== "undefined" && document.cookie) { headers[REMOTE_CLIENT_HEADER_NANE] = getClientSideFingerprint(); } else if (headers[COOKIE_HEADER_NAME]) { const ssrCookies = parseCookieString(headers[COOKIE_HEADER_NAME]); headers[REMOTE_CLIENT_HEADER_NANE] = ssrCookies[FINGERPRINT_ID_COOKIE_NAME] || SSR_DEFAULT_FINGERPRINT; } return { ...previousContext, headers }; }); return forward(operation); }; } function prepareSSRContext(req, res) { if (!req) { return { headers: {} }; } const requestCookies = getCookies({ req, res }); if (!requestCookies[FINGERPRINT_ID_COOKIE_NAME]) { const fingerprint = generateFingerprint(); requestCookies[FINGERPRINT_ID_COOKIE_NAME] = fingerprint; setCookie2(FINGERPRINT_ID_COOKIE_NAME, fingerprint, { req, res }); } const cookieHeader = Object.entries(requestCookies).map(([name, value]) => value ? serializeCookie(name, value) : null).filter(Boolean).join(";"); return { headers: { cookie: cookieHeader } }; } // src/helpers/collections.ts function nonNull(val) { return val !== null && val !== void 0; } // src/helpers/cookies.ts import { getCookie as getCookie2 } from "cookies-next"; import { createContext, useContext } from "react"; var SSR_COOKIES_DEFAULT_PROP_NAME = "__SSR_COOKIES__"; var SSRCookiesHelper = class { constructor(allowedCookies, propName) { this.allowedCookies = allowedCookies; this.propName = propName || SSR_COOKIES_DEFAULT_PROP_NAME; this.defaultValues = Object.fromEntries(allowedCookies.map((key) => [key, null])); this.context = createContext(this.defaultValues); this.extractSSRCookies = this.extractSSRCookies.bind(this); } getContext() { return this.context; } generateUseSSRCookiesExtractorHook() { const defaultValues = this.defaultValues; const propName = this.propName; return function useSSRCookiesExtractor(pageProps) { return pageProps[propName] || defaultValues; }; } generateUseSSRCookiesHook() { const context = this.context; return function useSSRCookies() { return useContext(context); }; } extractSSRCookies(req, res, pageParams) { return { ...pageParams, props: { ...pageParams.props, [this.propName]: Object.fromEntries( Object.keys(this.defaultValues).map((key) => [ key, getCookie2(key, { req, res }) || null ]) ) } }; } }; // src/helpers/environment.ts function isSSR() { return typeof window === "undefined"; } function isDebug() { return process.env.NODE_ENV === "development"; } // src/helpers/posthog.ts var POSTHOG_CLOUD_HOST_BASE = "i.posthog.com"; var POSTHOG_CLOUD_HOST_MATCHER = new RegExp(`^(\\w+)\\.${POSTHOG_CLOUD_HOST_BASE.replaceAll(".", "\\.")}$`); function getPosthogEndpoint(posthogDomain, requestedPath) { const posthogURL = new URL(posthogDomain); const cloudMatch = posthogURL.host.match(POSTHOG_CLOUD_HOST_MATCHER); if (cloudMatch && cloudMatch.length > 1) { const region = cloudMatch[1]; if (requestedPath.length && requestedPath[0] === "static") { posthogURL.host = `${region}-assets.${POSTHOG_CLOUD_HOST_BASE}`; posthogURL.pathname = requestedPath.slice(1).join("/"); return posthogURL.toString(); } } posthogURL.pathname = requestedPath.join("/"); return posthogURL.toString(); } // src/hooks/useEffectOnce.ts import { useEffect } from "react"; function useEffectOnce(cb) { useEffect(cb, []); } // src/hooks/usePrevious.ts import { useEffect as useEffect2, useRef } from "react"; function usePrevious(state) { const ref = useRef(); useEffect2(() => { ref.current = state; }); return ref.current; } export { Analytics, FINGERPRINT_ID_COOKIE_NAME, FINGERPRINT_ID_LENGTH, SSRCookiesHelper, generateFingerprint, generateUUIDv4, getClientSideFingerprint, getClientSideSenderInfo, getPosthogEndpoint, getTracingMiddleware, isDebug, isSSR, nonNull, prepareSSRContext, useEffectOnce, usePrevious }; //# sourceMappingURL=index.mjs.map