import { getNamespaceAndKey } from "@applicaster/zapp-react-native-utils/appUtils/contextKeysManager/utils"; import { localStorage } from "@applicaster/zapp-react-native-bridge/ZappStorage/LocalStorage"; import { sessionStorage } from "@applicaster/zapp-react-native-bridge/ZappStorage/SessionStorage"; import { looksLikeSkipExpression, parseSkipExpression, evaluateSkipExpression, } from "./skipExpression"; import { log_error, log_info } from "./logger"; type ParseKey = { key: string; namespace?: string }; export function parseKeyEntries(input: string): ParseKey[] { return input.split(",").flatMap((item) => { const trimmed = item.trim(); return trimmed ? getNamespaceAndKey(trimmed) : []; }); } export const getKeyToSkipHook = async (key: string, namespace?: string) => { const value = await sessionStorage.getItem(key, namespace); if (value) { return value; } return await localStorage.getItem(key, namespace); }; /** * Decides whether the hook should be skipped, from the `skip_hook_storage_key` * rule. The value is either a comma-separated key list — skip when ANY of them * exists — or, packed into the same string, a JSON boolean expression over * storage keys. See `skipExpression.ts` for the expression format. */ export const shouldSkipHook = async ( skipHookIfKeysExist?: string ): Promise => { if (!skipHookIfKeysExist?.trim()) { log_info("shouldSkipHook: No skipping condition provided"); return false; } if (looksLikeSkipExpression(skipHookIfKeysExist)) { return await evaluateSkipExpression( parseSkipExpression(skipHookIfKeysExist), getKeyToSkipHook ); } const keyEntries = parseKeyEntries(skipHookIfKeysExist); if (keyEntries.length === 0) { log_info("shouldSkipHook: No valid keys provided"); return false; } for (const entry of keyEntries) { try { const value = await getKeyToSkipHook(entry.key, entry.namespace); if (value) { log_info( `shouldSkipHook: Hook will be skipped due to: ${ entry.namespace ?? "" } ${entry.key}. Finishing hook flow` ); return true; } } catch (error) { log_error( `shouldSkipHook: Error: ${error.message} checking key: ${ entry.namespace ?? "" } ${entry.key}`, { error } ); } } log_info( // eslint-disable-next-line max-len "shouldSkipHook: No skipping condition met, none of the provided keys found in storage, proceeding with hook" ); return false; };