@descope/web-component 3.66.1 → 3.67.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. package/dist/cjs/descope-wc/BaseDescopeWc.js +1 -1
  2. package/dist/cjs/descope-wc/BaseDescopeWc.js.map +1 -1
  3. package/dist/cjs/descope-wc/DescopeWc.js +1 -1
  4. package/dist/cjs/descope-wc/DescopeWc.js.map +1 -1
  5. package/dist/cjs/helpers/realtime-conditions/applier.js +2 -0
  6. package/dist/cjs/helpers/realtime-conditions/applier.js.map +1 -0
  7. package/dist/cjs/helpers/realtime-conditions/config.js +2 -0
  8. package/dist/cjs/helpers/realtime-conditions/config.js.map +1 -0
  9. package/dist/cjs/helpers/realtime-conditions/evaluator.js +2 -0
  10. package/dist/cjs/helpers/realtime-conditions/evaluator.js.map +1 -0
  11. package/dist/cjs/helpers/templates.js +1 -1
  12. package/dist/cjs/helpers/templates.js.map +1 -1
  13. package/dist/cjs/mixins/componentConditionsMixin.js +2 -0
  14. package/dist/cjs/mixins/componentConditionsMixin.js.map +1 -0
  15. package/dist/cjs/types.js.map +1 -1
  16. package/dist/esm/descope-wc/BaseDescopeWc.js +1 -1
  17. package/dist/esm/descope-wc/BaseDescopeWc.js.map +1 -1
  18. package/dist/esm/descope-wc/DescopeWc.js +1 -1
  19. package/dist/esm/descope-wc/DescopeWc.js.map +1 -1
  20. package/dist/esm/helpers/realtime-conditions/applier.js +2 -0
  21. package/dist/esm/helpers/realtime-conditions/applier.js.map +1 -0
  22. package/dist/esm/helpers/realtime-conditions/config.js +2 -0
  23. package/dist/esm/helpers/realtime-conditions/config.js.map +1 -0
  24. package/dist/esm/helpers/realtime-conditions/evaluator.js +2 -0
  25. package/dist/esm/helpers/realtime-conditions/evaluator.js.map +1 -0
  26. package/dist/esm/helpers/templates.js +1 -1
  27. package/dist/esm/helpers/templates.js.map +1 -1
  28. package/dist/esm/mixins/componentConditionsMixin.js +2 -0
  29. package/dist/esm/mixins/componentConditionsMixin.js.map +1 -0
  30. package/dist/esm/types.js.map +1 -1
  31. package/dist/index.d.ts +517 -69
  32. package/dist/index.js +2 -2
  33. package/package.json +4 -4
@@ -0,0 +1,2 @@
1
+ const n=["input","change"],t=50;export{t as REALTIME_CONDITION_DEBOUNCE_MS,n as REALTIME_CONDITION_EVENTS};
2
+ //# sourceMappingURL=config.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.js","sources":["../../../../src/lib/helpers/realtime-conditions/config.ts"],"sourcesContent":["/**\n * Single source of truth for the events the real-time conditions mixin\n * subscribes to. We listen to both `input` and `change` because some custom\n * elements (notably descope-checkbox) dispatch `input` with the stale value\n * before their internal state updates; `change` fires with the new value.\n * The handler is idempotent, so receiving both events for one user action is\n * harmless. Swapping events is a one-line change.\n */\nexport const REALTIME_CONDITION_EVENTS = ['input', 'change'] as const;\n\n/** Debounce window applied before re-evaluation runs. */\nexport const REALTIME_CONDITION_DEBOUNCE_MS = 50;\n"],"names":["REALTIME_CONDITION_EVENTS","REALTIME_CONDITION_DEBOUNCE_MS"],"mappings":"MAQaA,EAA4B,CAAC,QAAS,UAGtCC,EAAiC"}
@@ -0,0 +1,2 @@
1
+ function n(n){if("number"==typeof n&&Number.isFinite(n))return{value:n,ok:!0};if("boolean"==typeof n)return{value:n?1:0,ok:!0};if("string"==typeof n){if(""===n.trim())return{value:0,ok:!1};const r=Number(n);if(Number.isFinite(r))return{value:r,ok:!0}}return{value:0,ok:!1}}function r(n){return null==n?"":"string"==typeof n?n:"boolean"==typeof n?n?"true":"false":"number"==typeof n?Number.isFinite(n)?String(n):"":String(n)}function t(n){if("boolean"==typeof n)return{value:n,ok:!0};if("string"==typeof n){if("true"===n)return{value:!0,ok:!0};if("false"===n)return{value:!1,ok:!0}}return"number"==typeof n?{value:0!==n,ok:!0}:{value:!1,ok:!1}}function e(n){return Array.isArray(n)?n:"string"==typeof n?n.split(","):null}function o(n){return null==n||("string"==typeof n?""===n:Array.isArray(n)?0===n.length:"object"==typeof n&&0===Object.keys(n).length)}const u=new Map;const l={equal:(n,t)=>n===t||r(n)===r(t),"not-equal":(n,t)=>n!==t&&r(n)!==r(t),contains:(n,t)=>Array.isArray(n)?n.includes(t):"string"==typeof n&&n.includes(r(t)),"greater-than":(r,t)=>{const e=n(r),o=n(t);return e.ok&&o.ok&&e.value>o.value},"greater-than-or-equal":(r,t)=>{const e=n(r),o=n(t);return e.ok&&o.ok&&e.value>=o.value},"less-than":(r,t)=>{const e=n(r),o=n(t);return e.ok&&o.ok&&e.value<o.value},"less-than-or-equal":(r,t)=>{const e=n(r),o=n(t);return e.ok&&o.ok&&e.value<=o.value},empty:n=>o(n),"not-empty":n=>!o(n),"is-true":n=>{const r=t(n);return r.ok&&r.value},"is-false":n=>{const r=t(n);return r.ok&&!r.value},in:(n,r)=>{const t=e(r);return null!==t&&t.includes(n)},"not-in":(n,r)=>{const t=e(r);return null!==t&&!t.includes(n)},matches:(n,t)=>{if("string"!=typeof n)return!1;const e=function(n){var r;if(u.has(n))return null!==(r=u.get(n))&&void 0!==r?r:null;let t;try{t=new RegExp(n)}catch(n){t=null}return u.set(n,t),t}(r(t));return null!==e&&e.test(n)}};function i(n,r){var t;if(n)return"form"===n.kind&&n.form?r[n.form]:"list"===n.kind?(null!==(t=n.items)&&void 0!==t?t:[]).map((n=>i(n,r))):n.value}function a(n,r){const t=l[n.operator];if(!t)return!1;return t(i(n.target,r),i(n.predicate,r))}function s(n,r){var t;return(null!==(t=n.rules)&&void 0!==t?t:[]).some((n=>function(n,r){var t;const e=null!==(t=n.atomicConditions)&&void 0!==t?t:[];return!!e.length&&(n.logicalOr?e.some((n=>a(n,r))):e.every((n=>a(n,r))))}(n,r)))}function f(n,r){const t={};return(null!=n?n:[]).forEach((n=>{var e;s(n,r)&&(null!==(e=n.componentIds)&&void 0!==e?e:[]).forEach((r=>{t[r]=n.action}))})),t}function c(n){const r=new Set;return(null!=n?n:[]).forEach((n=>{var t;(null!==(t=n.componentIds)&&void 0!==t?t:[]).forEach((n=>r.add(n)))})),r}function v(n){const r=new Set,t=n=>{var e;n&&("form"===n.kind&&n.form?r.add(n.form):"list"===n.kind&&(null!==(e=n.items)&&void 0!==e?e:[]).forEach(t))};return(null!=n?n:[]).forEach((n=>{var r;return(null!==(r=n.rules)&&void 0!==r?r:[]).forEach((n=>{var r;return(null!==(r=n.atomicConditions)&&void 0!==r?r:[]).forEach((n=>{t(n.target),t(n.predicate)}))}))})),r}export{v as collectReferencedFormKeys,c as collectTouchedComponentIds,f as evaluateAll,s as evaluateCondition,o as isEmpty,t as toBoolean,n as toFloat,e as toSlice,r as toString};
2
+ //# sourceMappingURL=evaluator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"evaluator.js","sources":["../../../../src/lib/helpers/realtime-conditions/evaluator.ts"],"sourcesContent":["import type {\n RealtimeAtomicCondition,\n RealtimeComponentsCondition,\n RealtimeOperand,\n RealtimeOperator,\n RealtimeRule,\n} from '../../types';\n\n/**\n * Snapshot of on-screen form values keyed by their full context key\n * (e.g. `form.phone`). The mixin maintains this snapshot as the user types.\n */\nexport type FormSnapshot = Record<string, unknown>;\n\n/* ------------------------------------------------------------------ */\n/* type-conversion helpers — mirror the Go cutils.AnyTo* semantics */\n/* ------------------------------------------------------------------ */\n\nexport function toFloat(v: unknown): { value: number; ok: boolean } {\n if (typeof v === 'number' && Number.isFinite(v)) {\n return { value: v, ok: true };\n }\n if (typeof v === 'boolean') {\n return { value: v ? 1 : 0, ok: true };\n }\n if (typeof v === 'string') {\n if (v.trim() === '') return { value: 0, ok: false };\n const n = Number(v);\n if (Number.isFinite(n)) return { value: n, ok: true };\n }\n return { value: 0, ok: false };\n}\n\nexport function toString(v: unknown): string {\n if (v === null || v === undefined) return '';\n if (typeof v === 'string') return v;\n if (typeof v === 'boolean') return v ? 'true' : 'false';\n if (typeof v === 'number') return Number.isFinite(v) ? String(v) : '';\n return String(v);\n}\n\nexport function toBoolean(v: unknown): { value: boolean; ok: boolean } {\n if (typeof v === 'boolean') return { value: v, ok: true };\n if (typeof v === 'string') {\n if (v === 'true') return { value: true, ok: true };\n if (v === 'false') return { value: false, ok: true };\n }\n if (typeof v === 'number') {\n return { value: v !== 0, ok: true };\n }\n return { value: false, ok: false };\n}\n\nexport function toSlice(v: unknown): unknown[] | null {\n if (Array.isArray(v)) return v;\n if (typeof v === 'string') return v.split(',');\n return null;\n}\n\nexport function isEmpty(v: unknown): boolean {\n if (v === null || v === undefined) return true;\n if (typeof v === 'string') return v === '';\n if (Array.isArray(v)) return v.length === 0;\n if (typeof v === 'object') return Object.keys(v as object).length === 0;\n return false;\n}\n\n/* ------------------------------------------------------------------ */\n/* operator implementations */\n/* ------------------------------------------------------------------ */\n\ntype OperatorFn = (target: unknown, predicate: unknown) => boolean;\n\n// Compiled regex cache for `matches`. Patterns come from the server (trusted),\n// and the same condition is re-evaluated on every input event, so caching by\n// pattern source avoids redundant RegExp construction on each keystroke.\nconst matchesRegexCache = new Map<string, RegExp | null>();\nfunction compileMatchesPattern(source: string): RegExp | null {\n if (matchesRegexCache.has(source)) {\n return matchesRegexCache.get(source) ?? null;\n }\n let compiled: RegExp | null;\n try {\n compiled = new RegExp(source);\n } catch {\n compiled = null;\n }\n matchesRegexCache.set(source, compiled);\n return compiled;\n}\n\nconst operators: Record<RealtimeOperator, OperatorFn> = {\n equal: (target, predicate) =>\n target === predicate || toString(target) === toString(predicate),\n 'not-equal': (target, predicate) =>\n target !== predicate && toString(target) !== toString(predicate),\n contains: (target, predicate) => {\n if (Array.isArray(target)) return target.includes(predicate);\n if (typeof target === 'string') return target.includes(toString(predicate));\n return false;\n },\n 'greater-than': (target, predicate) => {\n const targetNum = toFloat(target);\n const predicateNum = toFloat(predicate);\n return (\n targetNum.ok && predicateNum.ok && targetNum.value > predicateNum.value\n );\n },\n 'greater-than-or-equal': (target, predicate) => {\n const targetNum = toFloat(target);\n const predicateNum = toFloat(predicate);\n return (\n targetNum.ok && predicateNum.ok && targetNum.value >= predicateNum.value\n );\n },\n 'less-than': (target, predicate) => {\n const targetNum = toFloat(target);\n const predicateNum = toFloat(predicate);\n return (\n targetNum.ok && predicateNum.ok && targetNum.value < predicateNum.value\n );\n },\n 'less-than-or-equal': (target, predicate) => {\n const targetNum = toFloat(target);\n const predicateNum = toFloat(predicate);\n return (\n targetNum.ok && predicateNum.ok && targetNum.value <= predicateNum.value\n );\n },\n empty: (target) => isEmpty(target),\n 'not-empty': (target) => !isEmpty(target),\n 'is-true': (target) => {\n const asBool = toBoolean(target);\n return asBool.ok && asBool.value;\n },\n 'is-false': (target) => {\n const asBool = toBoolean(target);\n return asBool.ok && !asBool.value;\n },\n in: (target, predicate) => {\n const slice = toSlice(predicate);\n return slice !== null && slice.includes(target);\n },\n // Mirrors `in`: bad input → false, not true.\n 'not-in': (target, predicate) => {\n const slice = toSlice(predicate);\n return slice !== null && !slice.includes(target);\n },\n matches: (target, predicate) => {\n if (typeof target !== 'string') return false;\n const regex = compileMatchesPattern(toString(predicate));\n return regex !== null && regex.test(target);\n },\n};\n\n/* ------------------------------------------------------------------ */\n/* atomic & rule evaluation */\n/* ------------------------------------------------------------------ */\n\nfunction resolveOperand(\n operand: RealtimeOperand | undefined,\n snapshot: FormSnapshot,\n): unknown {\n if (!operand) return undefined;\n if (operand.kind === 'form' && operand.form) {\n return snapshot[operand.form];\n }\n if (operand.kind === 'list') {\n // Each item may itself be a form placeholder or a pre-resolved literal,\n // so resolve recursively. The server emits this shape when an\n // `in` / `not-in` / `contains` predicate is an array containing a\n // `{{form.X}}` reference to an on-screen key.\n return (operand.items ?? []).map((item) => resolveOperand(item, snapshot));\n }\n return operand.value;\n}\n\nfunction evaluateAtomic(\n atom: RealtimeAtomicCondition,\n snapshot: FormSnapshot,\n): boolean {\n const fn = operators[atom.operator as RealtimeOperator];\n if (!fn) return false;\n\n const target = resolveOperand(atom.target, snapshot);\n const predicate = resolveOperand(atom.predicate, snapshot);\n return fn(target, predicate);\n}\n\nfunction evaluateRule(rule: RealtimeRule, snapshot: FormSnapshot): boolean {\n const atomics = rule.atomicConditions ?? [];\n if (!atomics.length) return false;\n if (rule.logicalOr) {\n return atomics.some((a) => evaluateAtomic(a, snapshot));\n }\n return atomics.every((a) => evaluateAtomic(a, snapshot));\n}\n\n/** Evaluates a single condition group: returns true if any of its rules fire. */\nexport function evaluateCondition(\n condition: RealtimeComponentsCondition,\n snapshot: FormSnapshot,\n): boolean {\n return (condition.rules ?? []).some((r) => evaluateRule(r, snapshot));\n}\n\n/**\n * Evaluates all condition groups and returns, for each targeted component, the\n * action of the LAST firing group. Components where no group fires are absent\n * from the result.\n *\n * \"Last match wins\" mirrors the BE evaluator: when two groups target the same\n * component with different actions, the BE assigns\n * `componentsState[id] = cc.Then.Action` unconditionally in declaration order,\n * so a later CC's action overwrites an earlier one. The SDK matches this so\n * the two layers can't disagree on which action a component ends up with.\n */\nexport function evaluateAll(\n conditions: RealtimeComponentsCondition[] | undefined,\n snapshot: FormSnapshot,\n): Record<string, string> {\n const result: Record<string, string> = {};\n (conditions ?? []).forEach((c) => {\n if (!evaluateCondition(c, snapshot)) return;\n (c.componentIds ?? []).forEach((id) => {\n result[id] = c.action;\n });\n });\n return result;\n}\n\n/**\n * Returns the set of component IDs targeted by any condition group. The\n * applier uses this to know which components belong to the realtime layer.\n */\nexport function collectTouchedComponentIds(\n conditions: RealtimeComponentsCondition[] | undefined,\n): Set<string> {\n const out = new Set<string>();\n (conditions ?? []).forEach((c) => {\n (c.componentIds ?? []).forEach((id) => out.add(id));\n });\n return out;\n}\n\n/**\n * Returns every form key (e.g. `form.phone`) referenced by any operand in\n * any rule. The mixin uses this on mount to know which DOM inputs to read so\n * it can seed the snapshot from rendered defaults that the server can't see.\n */\nexport function collectReferencedFormKeys(\n conditions: RealtimeComponentsCondition[] | undefined,\n): Set<string> {\n const out = new Set<string>();\n const walkOperand = (operand: RealtimeOperand | undefined) => {\n if (!operand) return;\n if (operand.kind === 'form' && operand.form) {\n out.add(operand.form);\n } else if (operand.kind === 'list') {\n (operand.items ?? []).forEach(walkOperand);\n }\n };\n (conditions ?? []).forEach((c) =>\n (c.rules ?? []).forEach((r) =>\n (r.atomicConditions ?? []).forEach((a) => {\n walkOperand(a.target);\n walkOperand(a.predicate);\n }),\n ),\n );\n return out;\n}\n"],"names":["toFloat","v","Number","isFinite","value","ok","trim","n","toString","String","toBoolean","toSlice","Array","isArray","split","isEmpty","length","Object","keys","matchesRegexCache","Map","operators","equal","target","predicate","contains","includes","targetNum","predicateNum","empty","asBool","in","slice","matches","regex","source","has","_a","get","compiled","RegExp","_b","set","compileMatchesPattern","test","resolveOperand","operand","snapshot","kind","form","items","map","item","evaluateAtomic","atom","fn","operator","evaluateCondition","condition","rules","some","r","rule","atomics","atomicConditions","logicalOr","a","every","evaluateRule","evaluateAll","conditions","result","forEach","c","componentIds","id","action","collectTouchedComponentIds","out","Set","add","collectReferencedFormKeys","walkOperand"],"mappings":"AAkBM,SAAUA,EAAQC,GACtB,GAAiB,iBAANA,GAAkBC,OAAOC,SAASF,GAC3C,MAAO,CAAEG,MAAOH,EAAGI,IAAI,GAEzB,GAAiB,kBAANJ,EACT,MAAO,CAAEG,MAAOH,EAAI,EAAI,EAAGI,IAAI,GAEjC,GAAiB,iBAANJ,EAAgB,CACzB,GAAiB,KAAbA,EAAEK,OAAe,MAAO,CAAEF,MAAO,EAAGC,IAAI,GAC5C,MAAME,EAAIL,OAAOD,GACjB,GAAIC,OAAOC,SAASI,GAAI,MAAO,CAAEH,MAAOG,EAAGF,IAAI,EAChD,CACD,MAAO,CAAED,MAAO,EAAGC,IAAI,EACzB,CAEM,SAAUG,EAASP,GACvB,OAAIA,QAAsC,GACzB,iBAANA,EAAuBA,EACjB,kBAANA,EAAwBA,EAAI,OAAS,QAC/B,iBAANA,EAAuBC,OAAOC,SAASF,GAAKQ,OAAOR,GAAK,GAC5DQ,OAAOR,EAChB,CAEM,SAAUS,EAAUT,GACxB,GAAiB,kBAANA,EAAiB,MAAO,CAAEG,MAAOH,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,EAAAA,EAAA,IAAImC,QAAQU,GAC/B,EAUH,OARCZ,QAAAA,EAAc,IAAIE,SAASC,UAC1B,OAAY,QAAXpC,EAAAoC,EAAEd,aAAS,IAAAtB,EAAAA,EAAA,IAAImC,SAASX,UACvB,OAAuB,QAAtBxB,EAAAwB,EAAEG,wBAAoB,IAAA3B,EAAAA,EAAA,IAAImC,SAASN,IAClCgB,EAAYhB,EAAE3C,QACd2D,EAAYhB,EAAE1C,UAAU,GACxB,GACH,IAEIsD,CACT"}
@@ -1,2 +1,2 @@
1
- import{__rest as e}from"tslib";import{escapeMarkdown as t}from"@descope/escape-markdown";import{ELEMENT_TYPE_ATTRIBUTE as o,HAS_DYNAMIC_VALUES_ATTR_NAME as r,DESCOPE_ATTRIBUTE_EXCLUDE_FIELD as c}from"../constants/index.js";import{shouldHandleMarkdown as n}from"./helpers.js";const s=["disabled"],l=(e,t,r="")=>{e.querySelectorAll(`[${o}="${t}"]`).forEach((e=>{e.textContent=r,e.classList[r?"remove":"add"]("hide")}))},i=(e,t)=>{Object.entries(t||{}).forEach((([t,o])=>{Array.from(e.querySelectorAll(`*[name="${t}"]:not([${c}])`)).forEach((e=>{e.value=o}))}))},a=(e,t)=>t.split(".").reduce(((e,t)=>(null==e?void 0:e[t])||""),e),d=(e,o,r)=>e.replace(/{{(.+?)}}/g,((e,c)=>r?t(a(o,c)):a(o,c))),u=(e,t,r)=>{e.querySelectorAll(`[${o}="${t}"]`).forEach((e=>{e.setAttribute("href",r)}))},f=(e,t,o,r)=>{o&&Object.keys(o).forEach((c=>{if(!t.querySelector(c))return void r.debug(`Skipping css vars for component "${c}"`,`Got css vars for component ${c} but Could not find it on next page`);const n=customElements.get(c);n?Object.keys(o[c]).forEach((t=>{var s;const l=o[c],i=null===(s=null==n?void 0:n.cssVarList)||void 0===s?void 0:s[t];if(!i)return void r.info(`Could not find css variable name for ${t} in ${c}`,"Check if the css variable is defined in the component");const a=l[t];e.style.setProperty(i,a)})):r.debug(`Could not find component class for ${c}`,"Check if the component is registered")}))},p=(e,t,o)=>{var r,c;const n=null===(r=customElements.get(t))||void 0===r?void 0:r.cssVarList.url;o&&n&&(null===(c=null==e?void 0:e.style)||void 0===c||c.setProperty(n,`url(data:image/jpg;base64,${o})`))},b=(t,o,c,l)=>{var i,a;u(t,"totp-link",null===(i=null==o?void 0:o.totp)||void 0===i?void 0:i.provisionUrl),u(t,"notp-link",null===(a=null==o?void 0:o.notp)||void 0===a?void 0:a.redirectUrl),((e,t)=>{e.querySelectorAll("descope-text,descope-link,descope-enriched-text,descope-code-snippet").forEach((e=>{const o=n(e.localName);e.textContent=d(e.textContent,t,o);const r=e.getAttribute("href");r&&e.setAttribute("href",d(r,t))}))})(t,o),((t,o,r)=>{if(!o)return;const{componentsDynamicAttrs:c}=o,n=e(o,["componentsDynamicAttrs"]),s=Object.keys(n).reduce(((e,t)=>(e[`[name=${t}]`]=n[t],e)),{});c&&Object.keys(c).forEach((e=>{const t=c[e];if(t){const{attributes:o}=t;o&&Object.keys(o).length&&(s[e]=o)}})),Object.keys(s).forEach((e=>{t.querySelectorAll(e).forEach((t=>{const o=s[e];Object.keys(o).forEach((e=>{let c=o[e];if("string"!=typeof c)try{c=JSON.stringify(c)}catch(t){r.error(`Could not stringify value "${c}" for "${e}"`,t.message),c=""}t.setAttribute(e,c)}))}))}))})(t,null==o?void 0:o.componentsConfig,l),((e,t)=>{e.querySelectorAll(`[${r}]`).forEach((e=>{Array.from(e.attributes).forEach((e=>{e.value=d(e.value,t)}))}))})(t,o),((e,t)=>{Object.entries(t).forEach((([t,o])=>{e.querySelectorAll(`[name="${t}"]`).forEach((e=>{Object.entries(o).forEach((([t,o])=>{s.includes(t)&&e.setAttribute(t,o)}))}))}))})(t,c),((e,t={},o)=>{Object.entries(t).forEach((([t,r])=>{e.querySelectorAll(`[id="${CSS.escape(t)}"]`).forEach((e=>{switch(r){case"disable":e.setAttribute("disabled","true");break;case"hide":e.classList.add("hidden");break;case"read-only":e.setAttribute("readonly","true");break;default:null==o||o.error(`Unknown component state "${r}" for component with id "${t}"`,'Valid states are "disable", "hide", and "read-only"')}}))}))})(t,null==o?void 0:o.componentsState,l)},h=(e,t)=>{i(e,null==t?void 0:t.inputs),i(e,null==t?void 0:t.form)},m=(e,t)=>{p(e,"descope-totp-image",t)},y=(e,t)=>{p(e,"descope-notp-image",t)},v=(e,t)=>{Object.entries({"default-code":"autoDetect","default-country":"autoDetect"}).forEach((([o,r])=>{Array.from(e.querySelectorAll(`[${o}="${r}"]`)).forEach((e=>{e.setAttribute(o,t||r)}))}))},A=(e,t)=>{let o=t;if(t)try{const[e]=Intl.getCanonicalLocales(t);e&&(o=e)}catch(e){}Object.entries({lang:"autoDetect"}).forEach((([t,r])=>{Array.from(e.querySelectorAll(`[${t}="${r}"]`)).forEach((e=>{e.setAttribute(t,o||r)}))}))},E=e=>{e.querySelectorAll(`descope-button[${o}="biometrics"]`).forEach((e=>e.setAttribute("disabled","true")))};export{E as disableWebauthnButtons,l as replaceElementMessage,v as setComponentsAutoDetectByGeo,A as setComponentsAutoDetectByLocale,f as setCssVars,y as setNOTPVariable,m as setTOTPVariable,h as updateScreenFromScreenState,b as updateTemplateFromScreenState};
1
+ import{__rest as e}from"tslib";import{escapeMarkdown as t}from"@descope/escape-markdown";import{ELEMENT_TYPE_ATTRIBUTE as o,HAS_DYNAMIC_VALUES_ATTR_NAME as r,DESCOPE_ATTRIBUTE_EXCLUDE_FIELD as c}from"../constants/index.js";import{shouldHandleMarkdown as n}from"./helpers.js";const s=["disabled"],l=(e,t,r="")=>{e.querySelectorAll(`[${o}="${t}"]`).forEach((e=>{e.textContent=r,e.classList[r?"remove":"add"]("hide")}))},i=(e,t)=>{Object.entries(t||{}).forEach((([t,o])=>{Array.from(e.querySelectorAll(`*[name="${t}"]:not([${c}])`)).forEach((e=>{e.value=o}))}))},a=(e,t)=>t.split(".").reduce(((e,t)=>(null==e?void 0:e[t])||""),e),u=(e,o,r)=>e.replace(/{{(.+?)}}/g,((e,c)=>r?t(a(o,c)):a(o,c))),d=(e,t,r)=>{e.querySelectorAll(`[${o}="${t}"]`).forEach((e=>{e.setAttribute("href",r)}))},f=(e,t,o,r)=>{o&&Object.keys(o).forEach((c=>{if(!t.querySelector(c))return void r.debug(`Skipping css vars for component "${c}"`,`Got css vars for component ${c} but Could not find it on next page`);const n=customElements.get(c);n?Object.keys(o[c]).forEach((t=>{var s;const l=o[c],i=null===(s=null==n?void 0:n.cssVarList)||void 0===s?void 0:s[t];if(!i)return void r.info(`Could not find css variable name for ${t} in ${c}`,"Check if the css variable is defined in the component");const a=l[t];e.style.setProperty(i,a)})):r.debug(`Could not find component class for ${c}`,"Check if the component is registered")}))},p=(e,t,o)=>{var r,c;const n=null===(r=customElements.get(t))||void 0===r?void 0:r.cssVarList.url;o&&n&&(null===(c=null==e?void 0:e.style)||void 0===c||c.setProperty(n,`url(data:image/jpg;base64,${o})`))},m=(t,o,c,l)=>{var i,a;d(t,"totp-link",null===(i=null==o?void 0:o.totp)||void 0===i?void 0:i.provisionUrl),d(t,"notp-link",null===(a=null==o?void 0:o.notp)||void 0===a?void 0:a.redirectUrl),((e,t)=>{e.querySelectorAll("descope-text,descope-link,descope-enriched-text,descope-code-snippet").forEach((e=>{const o=n(e.localName);e.textContent=u(e.textContent,t,o);const r=e.getAttribute("href");r&&e.setAttribute("href",u(r,t))}))})(t,o),((t,o,r)=>{if(!o)return;const{componentsDynamicAttrs:c}=o,n=e(o,["componentsDynamicAttrs"]),s=Object.keys(n).reduce(((e,t)=>(e[`[name=${t}]`]=n[t],e)),{});c&&Object.keys(c).forEach((e=>{const t=c[e];if(t){const{attributes:o}=t;o&&Object.keys(o).length&&(s[e]=o)}})),Object.keys(s).forEach((e=>{t.querySelectorAll(e).forEach((t=>{const o=s[e];Object.keys(o).forEach((e=>{let c=o[e];if("string"!=typeof c)try{c=JSON.stringify(c)}catch(t){r.error(`Could not stringify value "${c}" for "${e}"`,t.message),c=""}t.setAttribute(e,c)}))}))}))})(t,null==o?void 0:o.componentsConfig,l),((e,t)=>{e.querySelectorAll(`[${r}]`).forEach((e=>{Array.from(e.attributes).forEach((e=>{e.value=u(e.value,t)}))}))})(t,o),((e,t)=>{Object.entries(t).forEach((([t,o])=>{e.querySelectorAll(`[name="${t}"]`).forEach((e=>{Object.entries(o).forEach((([t,o])=>{s.includes(t)&&e.setAttribute(t,o)}))}))}))})(t,c)},h=(e,t)=>{i(e,null==t?void 0:t.inputs),i(e,null==t?void 0:t.form)},y=(e,t)=>{p(e,"descope-totp-image",t)},b=(e,t)=>{p(e,"descope-notp-image",t)},v=(e,t)=>{Object.entries({"default-code":"autoDetect","default-country":"autoDetect"}).forEach((([o,r])=>{Array.from(e.querySelectorAll(`[${o}="${r}"]`)).forEach((e=>{e.setAttribute(o,t||r)}))}))},A=(e,t)=>{let o=t;if(t)try{const[e]=Intl.getCanonicalLocales(t);e&&(o=e)}catch(e){}Object.entries({lang:"autoDetect"}).forEach((([t,r])=>{Array.from(e.querySelectorAll(`[${t}="${r}"]`)).forEach((e=>{e.setAttribute(t,o||r)}))}))},E=e=>{e.querySelectorAll(`descope-button[${o}="biometrics"]`).forEach((e=>e.setAttribute("disabled","true")))};export{E as disableWebauthnButtons,l as replaceElementMessage,v as setComponentsAutoDetectByGeo,A as setComponentsAutoDetectByLocale,f as setCssVars,b as setNOTPVariable,y as setTOTPVariable,h as updateScreenFromScreenState,m as updateTemplateFromScreenState};
2
2
  //# sourceMappingURL=templates.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"templates.js","sources":["../../../src/lib/helpers/templates.ts"],"sourcesContent":["import { escapeMarkdown } from '@descope/escape-markdown';\nimport {\n ELEMENT_TYPE_ATTRIBUTE,\n DESCOPE_ATTRIBUTE_EXCLUDE_FIELD,\n HAS_DYNAMIC_VALUES_ATTR_NAME,\n} from '../constants';\nimport { ComponentsConfig, CssVars, ScreenState } from '../types';\nimport { shouldHandleMarkdown } from './helpers';\n\nconst ALLOWED_INPUT_CONFIG_ATTRS = ['disabled'];\n\nexport const replaceElementMessage = (\n baseEle: HTMLElement,\n eleType: string,\n message = '',\n) => {\n const eleList = baseEle.querySelectorAll(\n `[${ELEMENT_TYPE_ATTRIBUTE}=\"${eleType}\"]`,\n );\n eleList.forEach((ele: HTMLElement) => {\n // eslint-disable-next-line no-param-reassign\n ele.textContent = message;\n ele.classList[message ? 'remove' : 'add']('hide');\n });\n};\n\n/**\n * Replace the 'value' attribute of screen inputs with screen state's inputs.\n * For example: if base element contains '<input name=\"key1\" ...>' and screen input is in form of { key1: 'val1' },\n * it will add 'val1' as the input value\n */\nconst replaceElementInputs = (\n baseEle: HTMLElement,\n screenInputs: Record<string, string>,\n) => {\n Object.entries(screenInputs || {}).forEach(([name, value]) => {\n const inputEls = Array.from(\n baseEle.querySelectorAll(\n `*[name=\"${name}\"]:not([${DESCOPE_ATTRIBUTE_EXCLUDE_FIELD}])`,\n ),\n ) as HTMLInputElement[];\n inputEls.forEach((inputEle) => {\n // eslint-disable-next-line no-param-reassign\n inputEle.value = value;\n });\n });\n};\n\n/**\n * Get object nested path.\n * Examples:\n * - getByPath({ { a { b: 'rob' } }, 'a.b') => 'hey rob'\n * - getByPath({}, 'a.b') => ''\n */\nconst getByPath = (obj: Record<string, any>, path: string) =>\n path.split('.').reduce((prev, next) => prev?.[next] || '', obj);\n\n/**\n * Apply template language on text, based on screen state.\n * Examples:\n * - 'hey {{a.b}}', { a { b: 'rob' }} => 'hey rob'\n * - 'hey {{not.exists}}', {} => 'hey '\n */\nconst applyTemplates = (\n text: string,\n screenState?: Record<string, any>,\n handleMarkdown?: boolean,\n): string =>\n text.replace(/{{(.+?)}}/g, (_, match) =>\n handleMarkdown\n ? escapeMarkdown(getByPath(screenState, match))\n : getByPath(screenState, match),\n );\n\n/**\n * Replace the templates of content of inner text/link elements with screen state data\n */\nconst replaceElementTemplates = (\n baseEle: DocumentFragment,\n screenState?: Record<string, any>,\n) => {\n const eleList = baseEle.querySelectorAll(\n 'descope-text,descope-link,descope-enriched-text,descope-code-snippet',\n );\n eleList.forEach((inEle: HTMLElement) => {\n const handleMarkdown = shouldHandleMarkdown(inEle.localName);\n // eslint-disable-next-line no-param-reassign\n inEle.textContent = applyTemplates(\n inEle.textContent,\n screenState,\n handleMarkdown,\n );\n const href = inEle.getAttribute('href');\n if (href) {\n inEle.setAttribute('href', applyTemplates(href, screenState));\n }\n });\n};\n\nconst replaceTemplateDynamicAttrValues = (\n baseEle: DocumentFragment,\n screenState?: Record<string, any>,\n) => {\n const eleList = baseEle.querySelectorAll(`[${HAS_DYNAMIC_VALUES_ATTR_NAME}]`);\n eleList.forEach((ele: HTMLElement) => {\n Array.from(ele.attributes).forEach((attr) => {\n // eslint-disable-next-line no-param-reassign\n attr.value = applyTemplates(attr.value, screenState);\n });\n });\n};\n\nconst replaceHrefByDataType = (\n baseEle: DocumentFragment,\n dataType: string,\n provisionUrl?: string,\n) => {\n const eleList = baseEle.querySelectorAll(\n `[${ELEMENT_TYPE_ATTRIBUTE}=\"${dataType}\"]`,\n );\n eleList.forEach((ele: HTMLLinkElement) => {\n // eslint-disable-next-line no-param-reassign\n ele.setAttribute('href', provisionUrl);\n });\n};\n\nconst setFormConfigValues = (\n baseEle: DocumentFragment,\n formData: Record<string, string>,\n) => {\n Object.entries(formData).forEach(([name, config]) => {\n const eles = baseEle.querySelectorAll(`[name=\"${name}\"]`);\n\n eles.forEach((ele) => {\n Object.entries(config).forEach(([attrName, attrValue]) => {\n if (ALLOWED_INPUT_CONFIG_ATTRS.includes(attrName)) {\n ele.setAttribute(attrName, attrValue);\n }\n });\n });\n });\n};\n\nexport const setCssVars = (\n rootEle: HTMLElement,\n nextPageTemplate: DocumentFragment,\n cssVars: CssVars,\n logger: {\n error: (message: string, description: string) => void;\n info: (message: string, description: string) => void;\n debug: (message: string, description: string) => void;\n },\n) => {\n if (!cssVars) {\n return;\n }\n\n Object.keys(cssVars).forEach((componentName) => {\n if (!nextPageTemplate.querySelector(componentName)) {\n logger.debug(\n `Skipping css vars for component \"${componentName}\"`,\n `Got css vars for component ${componentName} but Could not find it on next page`,\n );\n\n return;\n }\n const componentClass:\n | (CustomElementConstructor & { cssVarList: CssVars })\n | undefined = customElements.get(componentName) as any;\n\n if (!componentClass) {\n logger.debug(\n `Could not find component class for ${componentName}`,\n 'Check if the component is registered',\n );\n\n return;\n }\n\n Object.keys(cssVars[componentName]).forEach((cssVarKey) => {\n const componentCssVars = cssVars[componentName];\n const varName = componentClass?.cssVarList?.[cssVarKey];\n\n if (!varName) {\n logger.info(\n `Could not find css variable name for ${cssVarKey} in ${componentName}`,\n 'Check if the css variable is defined in the component',\n );\n return;\n }\n\n const value = componentCssVars[cssVarKey];\n\n rootEle.style.setProperty(varName, value);\n });\n });\n};\n\nconst setElementConfig = (\n baseEle: DocumentFragment,\n componentsConfig: ComponentsConfig,\n logger?: { error: (message: string, description: string) => void },\n) => {\n if (!componentsConfig) {\n return;\n }\n const { componentsDynamicAttrs, ...rest } = componentsConfig;\n\n const configMap = Object.keys(rest).reduce((acc, componentName) => {\n acc[`[name=${componentName}]`] = rest[componentName];\n return acc;\n }, {});\n\n if (componentsDynamicAttrs) {\n Object.keys(componentsDynamicAttrs).forEach((componentSelector) => {\n const componentDynamicAttrs = componentsDynamicAttrs[componentSelector];\n if (componentDynamicAttrs) {\n const { attributes } = componentDynamicAttrs;\n if (attributes && Object.keys(attributes).length) {\n configMap[componentSelector] = attributes;\n }\n }\n });\n }\n\n // collect components that needs configuration from DOM\n Object.keys(configMap).forEach((componentsSelector) => {\n baseEle.querySelectorAll(componentsSelector).forEach((comp) => {\n const config = configMap[componentsSelector];\n\n Object.keys(config).forEach((attr) => {\n let value = config[attr];\n\n if (typeof value !== 'string') {\n try {\n value = JSON.stringify(value);\n } catch (e) {\n logger.error(\n `Could not stringify value \"${value}\" for \"${attr}\"`,\n e.message,\n );\n value = '';\n }\n }\n\n comp.setAttribute(attr, value);\n });\n });\n });\n};\n\nconst setImageVariable = (\n rootEle: HTMLElement,\n name: string,\n image?: string,\n) => {\n const imageVarName = (\n customElements.get(name) as CustomElementConstructor & {\n cssVarList: Record<string, string>;\n }\n )?.cssVarList.url;\n\n if (image && imageVarName) {\n rootEle?.style?.setProperty(\n imageVarName,\n `url(data:image/jpg;base64,${image})`,\n );\n }\n};\n\nconst applyComponentsState = (\n baseEle: DocumentFragment,\n componentsState: Record<string, string> = {},\n logger?: { error: (message: string, description: string) => void },\n) => {\n Object.entries(componentsState).forEach(([componentId, state]) => {\n const componentEls = baseEle.querySelectorAll(\n `[id=\"${CSS.escape(componentId)}\"]`,\n );\n componentEls.forEach((compEl) => {\n switch (state) {\n case 'disable':\n compEl.setAttribute('disabled', 'true');\n break;\n case 'hide':\n compEl.classList.add('hidden');\n break;\n case 'read-only':\n compEl.setAttribute('readonly', 'true');\n break;\n default:\n logger?.error(\n `Unknown component state \"${state}\" for component with id \"${componentId}\"`,\n 'Valid states are \"disable\", \"hide\", and \"read-only\"',\n );\n break;\n }\n });\n });\n};\n\n/**\n * Update a screen template based on the screen state\n * - Show/hide error messages\n * - Replace element templates ({{...}} syntax) with screen state object\n */\nexport const updateTemplateFromScreenState = (\n baseEle: DocumentFragment,\n screenState?: ScreenState,\n flowInputs?: Record<string, string>,\n logger?: { error: (message: string, description: string) => void },\n) => {\n replaceHrefByDataType(baseEle, 'totp-link', screenState?.totp?.provisionUrl);\n replaceHrefByDataType(baseEle, 'notp-link', screenState?.notp?.redirectUrl);\n replaceElementTemplates(baseEle, screenState);\n setElementConfig(baseEle, screenState?.componentsConfig, logger);\n replaceTemplateDynamicAttrValues(baseEle, screenState);\n setFormConfigValues(baseEle, flowInputs);\n applyComponentsState(baseEle, screenState?.componentsState, logger);\n};\n\n/**\n * Update a screen based on a screen state\n * - Replace values of element inputs with screen state's inputs\n */\nexport const updateScreenFromScreenState = (\n baseEle: HTMLElement,\n screenState?: ScreenState,\n) => {\n replaceElementInputs(baseEle, screenState?.inputs);\n replaceElementInputs(baseEle, screenState?.form);\n};\n\nexport const setTOTPVariable = (rootEle: HTMLElement, image?: string) => {\n setImageVariable(rootEle, 'descope-totp-image', image);\n};\n\nexport const setNOTPVariable = (rootEle: HTMLElement, image?: string) => {\n setImageVariable(rootEle, 'descope-notp-image', image);\n};\n\nexport const setComponentsAutoDetectByGeo = (\n fragment: DocumentFragment,\n countryCodeIso2?: string, // e.g. 'US', 'IL', 'FR'\n) => {\n const config = {\n // phone\n 'default-code': 'autoDetect',\n // country-subdivision-city\n 'default-country': 'autoDetect',\n };\n Object.entries(config).forEach(([key, value]) => {\n Array.from(fragment.querySelectorAll(`[${key}=\"${value}\"]`)).forEach(\n (ele) => {\n ele.setAttribute(key, countryCodeIso2 || value);\n },\n );\n });\n};\n\nexport const setComponentsAutoDetectByLocale = (\n fragment: DocumentFragment,\n locale?: string, // e.g. 'en-US', 'fr-FR'\n) => {\n const config = {\n // country-subdivision-city\n lang: 'autoDetect',\n };\n\n let canonicalLocale = locale;\n if (locale) {\n try {\n const [canonical] = Intl.getCanonicalLocales(locale);\n if (canonical) {\n canonicalLocale = canonical;\n }\n } catch {\n // locale is not valid, keep original value\n }\n }\n\n Object.entries(config).forEach(([key, value]) => {\n Array.from(fragment.querySelectorAll(`[${key}=\"${value}\"]`)).forEach(\n (ele) => {\n ele.setAttribute(key, canonicalLocale || value);\n },\n );\n });\n};\n\nexport const disableWebauthnButtons = (fragment: DocumentFragment) => {\n const webauthnButtons = fragment.querySelectorAll(\n `descope-button[${ELEMENT_TYPE_ATTRIBUTE}=\"biometrics\"]`,\n );\n webauthnButtons.forEach((button) => button.setAttribute('disabled', 'true'));\n};\n\nexport const getDescopeUiComponentsList = (clone: DocumentFragment) => [\n ...Array.from(clone.querySelectorAll('*')).reduce<Set<string>>(\n (acc, el: HTMLElement) =>\n el.tagName.startsWith('DESCOPE-')\n ? acc.add(el.tagName.toLocaleLowerCase())\n : acc,\n new Set(),\n ),\n];\n"],"names":["ALLOWED_INPUT_CONFIG_ATTRS","replaceElementMessage","baseEle","eleType","message","querySelectorAll","ELEMENT_TYPE_ATTRIBUTE","forEach","ele","textContent","classList","replaceElementInputs","screenInputs","Object","entries","name","value","Array","from","DESCOPE_ATTRIBUTE_EXCLUDE_FIELD","inputEle","getByPath","obj","path","split","reduce","prev","next","applyTemplates","text","screenState","handleMarkdown","replace","_","match","escapeMarkdown","replaceHrefByDataType","dataType","provisionUrl","setAttribute","setCssVars","rootEle","nextPageTemplate","cssVars","logger","keys","componentName","querySelector","debug","componentClass","customElements","get","cssVarKey","componentCssVars","varName","_a","cssVarList","info","style","setProperty","setImageVariable","image","imageVarName","url","_b","updateTemplateFromScreenState","flowInputs","totp","notp","redirectUrl","inEle","shouldHandleMarkdown","localName","href","getAttribute","replaceElementTemplates","componentsConfig","componentsDynamicAttrs","rest","__rest","configMap","acc","componentSelector","componentDynamicAttrs","attributes","length","componentsSelector","comp","config","attr","JSON","stringify","e","error","setElementConfig","HAS_DYNAMIC_VALUES_ATTR_NAME","replaceTemplateDynamicAttrValues","formData","attrName","attrValue","includes","setFormConfigValues","componentsState","componentId","state","CSS","escape","compEl","add","applyComponentsState","updateScreenFromScreenState","inputs","form","setTOTPVariable","setNOTPVariable","setComponentsAutoDetectByGeo","fragment","countryCodeIso2","key","setComponentsAutoDetectByLocale","locale","canonicalLocale","canonical","Intl","getCanonicalLocales","lang","disableWebauthnButtons","button"],"mappings":"mRASA,MAAMA,EAA6B,CAAC,YAEvBC,EAAwB,CACnCC,EACAC,EACAC,EAAU,MAEMF,EAAQG,iBACtB,IAAIC,MAA2BH,OAEzBI,SAASC,IAEfA,EAAIC,YAAcL,EAClBI,EAAIE,UAAUN,EAAU,SAAW,OAAO,OAAO,GACjD,EAQEO,EAAuB,CAC3BT,EACAU,KAEAC,OAAOC,QAAQF,GAAgB,CAAE,GAAEL,SAAQ,EAAEQ,EAAMC,MAChCC,MAAMC,KACrBhB,EAAQG,iBACN,WAAWU,YAAeI,QAGrBZ,SAASa,IAEhBA,EAASJ,MAAQA,CAAK,GACtB,GACF,EASEK,EAAY,CAACC,EA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AsBoB,KACpDD,EAAiBnB,EAAS,qBAAsBoB,EAAM,EAG3CqD,EAAkB,CAACzE,EAAsBoB,KACpDD,EAAiBnB,EAAS,qBAAsBoB,EAAM,EAG3CsD,EAA+B,CAC1CC,EACAC,KAQAxG,OAAOC,QANQ,CAEb,eAAgB,aAEhB,kBAAmB,eAEEP,SAAQ,EAAE+G,EAAKtG,MACpCC,MAAMC,KAAKkG,EAAS/G,iBAAiB,IAAIiH,MAAQtG,QAAYT,SAC1DC,IACCA,EAAI+B,aAAa+E,EAAKD,GAAmBrG,EAAM,GAElD,GACD,EAGSuG,EAAkC,CAC7CH,EACAI,KAOA,IAAIC,EAAkBD,EACtB,GAAIA,EACF,IACE,MAAOE,GAAaC,KAAKC,oBAAoBJ,GACzCE,IACFD,EAAkBC,EAErB,CAAC,MAAAnE,GAED,CAGH1C,OAAOC,QAjBQ,CAEb+G,KAAM,eAeetH,SAAQ,EAAE+G,EAAKtG,MACpCC,MAAMC,KAAKkG,EAAS/G,iBAAiB,IAAIiH,MAAQtG,QAAYT,SAC1DC,IACCA,EAAI+B,aAAa+E,EAAKG,GAAmBzG,EAAM,GAElD,GACD,EAGS8G,EAA0BV,IACbA,EAAS/G,iBAC/B,kBAAkBC,mBAEJC,SAASwH,GAAWA,EAAOxF,aAAa,WAAY,SAAQ"}
1
+ {"version":3,"file":"templates.js","sources":["../../../src/lib/helpers/templates.ts"],"sourcesContent":["import { escapeMarkdown } from '@descope/escape-markdown';\nimport {\n ELEMENT_TYPE_ATTRIBUTE,\n DESCOPE_ATTRIBUTE_EXCLUDE_FIELD,\n HAS_DYNAMIC_VALUES_ATTR_NAME,\n} from '../constants';\nimport { ComponentsConfig, CssVars, ScreenState } from '../types';\nimport { shouldHandleMarkdown } from './helpers';\n\nconst ALLOWED_INPUT_CONFIG_ATTRS = ['disabled'];\n\nexport const replaceElementMessage = (\n baseEle: HTMLElement,\n eleType: string,\n message = '',\n) => {\n const eleList = baseEle.querySelectorAll(\n `[${ELEMENT_TYPE_ATTRIBUTE}=\"${eleType}\"]`,\n );\n eleList.forEach((ele: HTMLElement) => {\n // eslint-disable-next-line no-param-reassign\n ele.textContent = message;\n ele.classList[message ? 'remove' : 'add']('hide');\n });\n};\n\n/**\n * Replace the 'value' attribute of screen inputs with screen state's inputs.\n * For example: if base element contains '<input name=\"key1\" ...>' and screen input is in form of { key1: 'val1' },\n * it will add 'val1' as the input value\n */\nconst replaceElementInputs = (\n baseEle: HTMLElement,\n screenInputs: Record<string, string>,\n) => {\n Object.entries(screenInputs || {}).forEach(([name, value]) => {\n const inputEls = Array.from(\n baseEle.querySelectorAll(\n `*[name=\"${name}\"]:not([${DESCOPE_ATTRIBUTE_EXCLUDE_FIELD}])`,\n ),\n ) as HTMLInputElement[];\n inputEls.forEach((inputEle) => {\n // eslint-disable-next-line no-param-reassign\n inputEle.value = value;\n });\n });\n};\n\n/**\n * Get object nested path.\n * Examples:\n * - getByPath({ { a { b: 'rob' } }, 'a.b') => 'hey rob'\n * - getByPath({}, 'a.b') => ''\n */\nconst getByPath = (obj: Record<string, any>, path: string) =>\n path.split('.').reduce((prev, next) => prev?.[next] || '', obj);\n\n/**\n * Apply template language on text, based on screen state.\n * Examples:\n * - 'hey {{a.b}}', { a { b: 'rob' }} => 'hey rob'\n * - 'hey {{not.exists}}', {} => 'hey '\n */\nconst applyTemplates = (\n text: string,\n screenState?: Record<string, any>,\n handleMarkdown?: boolean,\n): string =>\n text.replace(/{{(.+?)}}/g, (_, match) =>\n handleMarkdown\n ? escapeMarkdown(getByPath(screenState, match))\n : getByPath(screenState, match),\n );\n\n/**\n * Replace the templates of content of inner text/link elements with screen state data\n */\nconst replaceElementTemplates = (\n baseEle: DocumentFragment,\n screenState?: Record<string, any>,\n) => {\n const eleList = baseEle.querySelectorAll(\n 'descope-text,descope-link,descope-enriched-text,descope-code-snippet',\n );\n eleList.forEach((inEle: HTMLElement) => {\n const handleMarkdown = shouldHandleMarkdown(inEle.localName);\n // eslint-disable-next-line no-param-reassign\n inEle.textContent = applyTemplates(\n inEle.textContent,\n screenState,\n handleMarkdown,\n );\n const href = inEle.getAttribute('href');\n if (href) {\n inEle.setAttribute('href', applyTemplates(href, screenState));\n }\n });\n};\n\nconst replaceTemplateDynamicAttrValues = (\n baseEle: DocumentFragment,\n screenState?: Record<string, any>,\n) => {\n const eleList = baseEle.querySelectorAll(`[${HAS_DYNAMIC_VALUES_ATTR_NAME}]`);\n eleList.forEach((ele: HTMLElement) => {\n Array.from(ele.attributes).forEach((attr) => {\n // eslint-disable-next-line no-param-reassign\n attr.value = applyTemplates(attr.value, screenState);\n });\n });\n};\n\nconst replaceHrefByDataType = (\n baseEle: DocumentFragment,\n dataType: string,\n provisionUrl?: string,\n) => {\n const eleList = baseEle.querySelectorAll(\n `[${ELEMENT_TYPE_ATTRIBUTE}=\"${dataType}\"]`,\n );\n eleList.forEach((ele: HTMLLinkElement) => {\n // eslint-disable-next-line no-param-reassign\n ele.setAttribute('href', provisionUrl);\n });\n};\n\nconst setFormConfigValues = (\n baseEle: DocumentFragment,\n formData: Record<string, string>,\n) => {\n Object.entries(formData).forEach(([name, config]) => {\n const eles = baseEle.querySelectorAll(`[name=\"${name}\"]`);\n\n eles.forEach((ele) => {\n Object.entries(config).forEach(([attrName, attrValue]) => {\n if (ALLOWED_INPUT_CONFIG_ATTRS.includes(attrName)) {\n ele.setAttribute(attrName, attrValue);\n }\n });\n });\n });\n};\n\nexport const setCssVars = (\n rootEle: HTMLElement,\n nextPageTemplate: DocumentFragment,\n cssVars: CssVars,\n logger: {\n error: (message: string, description: string) => void;\n info: (message: string, description: string) => void;\n debug: (message: string, description: string) => void;\n },\n) => {\n if (!cssVars) {\n return;\n }\n\n Object.keys(cssVars).forEach((componentName) => {\n if (!nextPageTemplate.querySelector(componentName)) {\n logger.debug(\n `Skipping css vars for component \"${componentName}\"`,\n `Got css vars for component ${componentName} but Could not find it on next page`,\n );\n\n return;\n }\n const componentClass:\n | (CustomElementConstructor & { cssVarList: CssVars })\n | undefined = customElements.get(componentName) as any;\n\n if (!componentClass) {\n logger.debug(\n `Could not find component class for ${componentName}`,\n 'Check if the component is registered',\n );\n\n return;\n }\n\n Object.keys(cssVars[componentName]).forEach((cssVarKey) => {\n const componentCssVars = cssVars[componentName];\n const varName = componentClass?.cssVarList?.[cssVarKey];\n\n if (!varName) {\n logger.info(\n `Could not find css variable name for ${cssVarKey} in ${componentName}`,\n 'Check if the css variable is defined in the component',\n );\n return;\n }\n\n const value = componentCssVars[cssVarKey];\n\n rootEle.style.setProperty(varName, value);\n });\n });\n};\n\nconst setElementConfig = (\n baseEle: DocumentFragment,\n componentsConfig: ComponentsConfig,\n logger?: { error: (message: string, description: string) => void },\n) => {\n if (!componentsConfig) {\n return;\n }\n const { componentsDynamicAttrs, ...rest } = componentsConfig;\n\n const configMap = Object.keys(rest).reduce((acc, componentName) => {\n acc[`[name=${componentName}]`] = rest[componentName];\n return acc;\n }, {});\n\n if (componentsDynamicAttrs) {\n Object.keys(componentsDynamicAttrs).forEach((componentSelector) => {\n const componentDynamicAttrs = componentsDynamicAttrs[componentSelector];\n if (componentDynamicAttrs) {\n const { attributes } = componentDynamicAttrs;\n if (attributes && Object.keys(attributes).length) {\n configMap[componentSelector] = attributes;\n }\n }\n });\n }\n\n // collect components that needs configuration from DOM\n Object.keys(configMap).forEach((componentsSelector) => {\n baseEle.querySelectorAll(componentsSelector).forEach((comp) => {\n const config = configMap[componentsSelector];\n\n Object.keys(config).forEach((attr) => {\n let value = config[attr];\n\n if (typeof value !== 'string') {\n try {\n value = JSON.stringify(value);\n } catch (e) {\n logger.error(\n `Could not stringify value \"${value}\" for \"${attr}\"`,\n e.message,\n );\n value = '';\n }\n }\n\n comp.setAttribute(attr, value);\n });\n });\n });\n};\n\nconst setImageVariable = (\n rootEle: HTMLElement,\n name: string,\n image?: string,\n) => {\n const imageVarName = (\n customElements.get(name) as CustomElementConstructor & {\n cssVarList: Record<string, string>;\n }\n )?.cssVarList.url;\n\n if (image && imageVarName) {\n rootEle?.style?.setProperty(\n imageVarName,\n `url(data:image/jpg;base64,${image})`,\n );\n }\n};\n\n/**\n * Update a screen template based on the screen state\n * - Show/hide error messages\n * - Replace element templates ({{...}} syntax) with screen state object\n *\n * Note: applying `screenState.componentsState` (the server-applied baseline\n * hide/disable/read-only map) is owned by the `componentConditionsMixin`. The\n * host calls `this.applyComponentsState(...)` after this function returns and\n * before the fragment is mounted.\n */\nexport const updateTemplateFromScreenState = (\n baseEle: DocumentFragment,\n screenState?: ScreenState,\n flowInputs?: Record<string, string>,\n logger?: { error: (message: string, description: string) => void },\n) => {\n replaceHrefByDataType(baseEle, 'totp-link', screenState?.totp?.provisionUrl);\n replaceHrefByDataType(baseEle, 'notp-link', screenState?.notp?.redirectUrl);\n replaceElementTemplates(baseEle, screenState);\n setElementConfig(baseEle, screenState?.componentsConfig, logger);\n replaceTemplateDynamicAttrValues(baseEle, screenState);\n setFormConfigValues(baseEle, flowInputs);\n};\n\n/**\n * Update a screen based on a screen state\n * - Replace values of element inputs with screen state's inputs\n */\nexport const updateScreenFromScreenState = (\n baseEle: HTMLElement,\n screenState?: ScreenState,\n) => {\n replaceElementInputs(baseEle, screenState?.inputs);\n replaceElementInputs(baseEle, screenState?.form);\n};\n\nexport const setTOTPVariable = (rootEle: HTMLElement, image?: string) => {\n setImageVariable(rootEle, 'descope-totp-image', image);\n};\n\nexport const setNOTPVariable = (rootEle: HTMLElement, image?: string) => {\n setImageVariable(rootEle, 'descope-notp-image', image);\n};\n\nexport const setComponentsAutoDetectByGeo = (\n fragment: DocumentFragment,\n countryCodeIso2?: string, // e.g. 'US', 'IL', 'FR'\n) => {\n const config = {\n // phone\n 'default-code': 'autoDetect',\n // country-subdivision-city\n 'default-country': 'autoDetect',\n };\n Object.entries(config).forEach(([key, value]) => {\n Array.from(fragment.querySelectorAll(`[${key}=\"${value}\"]`)).forEach(\n (ele) => {\n ele.setAttribute(key, countryCodeIso2 || value);\n },\n );\n });\n};\n\nexport const setComponentsAutoDetectByLocale = (\n fragment: DocumentFragment,\n locale?: string, // e.g. 'en-US', 'fr-FR'\n) => {\n const config = {\n // country-subdivision-city\n lang: 'autoDetect',\n };\n\n let canonicalLocale = locale;\n if (locale) {\n try {\n const [canonical] = Intl.getCanonicalLocales(locale);\n if (canonical) {\n canonicalLocale = canonical;\n }\n } catch {\n // locale is not valid, keep original value\n }\n }\n\n Object.entries(config).forEach(([key, value]) => {\n Array.from(fragment.querySelectorAll(`[${key}=\"${value}\"]`)).forEach(\n (ele) => {\n ele.setAttribute(key, canonicalLocale || value);\n },\n );\n });\n};\n\nexport const disableWebauthnButtons = (fragment: DocumentFragment) => {\n const webauthnButtons = fragment.querySelectorAll(\n `descope-button[${ELEMENT_TYPE_ATTRIBUTE}=\"biometrics\"]`,\n );\n webauthnButtons.forEach((button) => button.setAttribute('disabled', 'true'));\n};\n\nexport const getDescopeUiComponentsList = (clone: DocumentFragment) => [\n ...Array.from(clone.querySelectorAll('*')).reduce<Set<string>>(\n (acc, el: HTMLElement) =>\n el.tagName.startsWith('DESCOPE-')\n ? acc.add(el.tagName.toLocaleLowerCase())\n : acc,\n new Set(),\n ),\n];\n"],"names":["ALLOWED_INPUT_CONFIG_ATTRS","replaceElementMessage","baseEle","eleType","message","querySelectorAll","ELEMENT_TYPE_ATTRIBUTE","forEach","ele","textContent","classList","replaceElementInputs","screenInputs","Object","entries","name","value","Array","from","DESCOPE_ATTRIBUTE_EXCLUDE_FIELD","inputEle","getByPath","obj","path","split","reduce","prev","next","applyTemplates","text","screenState","handleMarkdown","replace","_","match","escapeMarkdown","replaceHrefByDataType","dataType","provisionUrl","setAttribute","setCssVars","rootEle","nextPageTemplate","cssVars","logger","keys","componentName","querySelector","debug","componentClass","customElements","get","cssVarKey","componentCssVars","varName","_a","cssVarList","info","style","setProperty","setImageVariable","image","imageVarName","url","_b","updateTemplateFromScreenState","flowInputs","totp","notp","redirectUrl","inEle","shouldHandleMarkdown","localName","href","getAttribute","replaceElementTemplates","componentsConfig","componentsDynamicAttrs","rest","__rest","configMap","acc","componentSelector","componentDynamicAttrs","attributes","length","componentsSelector","comp","config","attr","JSON","stringify","e","error","setElementConfig","HAS_DYNAMIC_VALUES_ATTR_NAME","replaceTemplateDynamicAttrValues","formData","attrName","attrValue","includes","setFormConfigValues","updateScreenFromScreenState","inputs","form","setTOTPVariable","setNOTPVariable","setComponentsAutoDetectByGeo","fragment","countryCodeIso2","key","setComponentsAutoDetectByLocale","locale","canonicalLocale","canonical","Intl","getCanonicalLocales","lang","disableWebauthnButtons","button"],"mappings":"mRASA,MAAMA,EAA6B,CAAC,YAEvBC,EAAwB,CACnCC,EACAC,EACAC,EAAU,MAEMF,EAAQG,iBACtB,IAAIC,MAA2BH,OAEzBI,SAASC,IAEfA,EAAIC,YAAcL,EAClBI,EAAIE,UAAUN,EAAU,SAAW,OAAO,OAAO,GACjD,EAQEO,EAAuB,CAC3BT,EACAU,KAEAC,OAAOC,QAAQF,GAAgB,CAAE,GAAEL,SAAQ,EAAEQ,EAAMC,MAChCC,MAAMC,KACrBhB,EAAQG,iBACN,WAAWU,YAAeI,QAGrBZ,SAASa,IAEhBA,EAASJ,MAAQA,CAAK,GACtB,GACF,EASEK,EAAY,CAACC,EAA0BC,IAC3CA,EAAKC,MAAM,KAAKC,QAAO,CAACC,EAAMC,KAASD,aAAI,EAAJA,EAAOC,KAAS,IAAIL,GAQvDM,EAAiB,CACrBC,EACAC,EACAC,IAEAF,EAAKG,QAAQ,cAAc,CAACC,EAAGC,IAC7BH,EACII,EAAed,EAAUS,EAAaI,IACtCb,EAAUS,EAAaI,KAyCzBE,EAAwB,CAC5BlC,EACAmC,EACAC,KAEgBpC,EAAQG,iBACtB,IAAIC,MAA2B+B,OAEzB9B,SAASC,IAEfA,EAAI+B,aAAa,OAAQD,EAAa,GACtC,EAoBSE,EAAa,CACxBC,EACAC,EACAC,EACAC,KAMKD,GAIL9B,OAAOgC,KAAKF,GAASpC,SAASuC,IAC5B,IAAKJ,EAAiBK,cAAcD,GAMl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0E,GAAQjF,SAAQ,EAAE2F,EAAUC,MACrCnG,EAA2BoG,SAASF,IACtC1F,EAAI+B,aAAa2D,EAAUC,EAC5B,GACD,GACF,GACF,EAuJFE,CAAoBnG,EAASgE,EAAW,EAO7BoC,EAA8B,CACzCpG,EACA4B,KAEAnB,EAAqBT,EAAS4B,aAAW,EAAXA,EAAayE,QAC3C5F,EAAqBT,EAAS4B,aAAW,EAAXA,EAAa0E,KAAK,EAGrCC,EAAkB,CAAChE,EAAsBoB,KACpDD,EAAiBnB,EAAS,qBAAsBoB,EAAM,EAG3C6C,EAAkB,CAACjE,EAAsBoB,KACpDD,EAAiBnB,EAAS,qBAAsBoB,EAAM,EAG3C8C,EAA+B,CAC1CC,EACAC,KAQAhG,OAAOC,QANQ,CAEb,eAAgB,aAEhB,kBAAmB,eAEEP,SAAQ,EAAEuG,EAAK9F,MACpCC,MAAMC,KAAK0F,EAASvG,iBAAiB,IAAIyG,MAAQ9F,QAAYT,SAC1DC,IACCA,EAAI+B,aAAauE,EAAKD,GAAmB7F,EAAM,GAElD,GACD,EAGS+F,EAAkC,CAC7CH,EACAI,KAOA,IAAIC,EAAkBD,EACtB,GAAIA,EACF,IACE,MAAOE,GAAaC,KAAKC,oBAAoBJ,GACzCE,IACFD,EAAkBC,EAErB,CAAC,MAAA3D,GAED,CAGH1C,OAAOC,QAjBQ,CAEbuG,KAAM,eAee9G,SAAQ,EAAEuG,EAAK9F,MACpCC,MAAMC,KAAK0F,EAASvG,iBAAiB,IAAIyG,MAAQ9F,QAAYT,SAC1DC,IACCA,EAAI+B,aAAauE,EAAKG,GAAmBjG,EAAM,GAElD,GACD,EAGSsG,EAA0BV,IACbA,EAASvG,iBAC/B,kBAAkBC,mBAEJC,SAASgH,GAAWA,EAAOhF,aAAa,WAAY,SAAQ"}
@@ -0,0 +1,2 @@
1
+ import{__classPrivateFieldGet as e,__classPrivateFieldSet as t}from"tslib";import{createSingletonMixin as n,compose as o}from"@descope/sdk-helpers";import{loggerMixin as i}from"@descope/sdk-mixins";import{REALTIME_CONDITION_EVENTS as s,REALTIME_CONDITION_DEBOUNCE_MS as r}from"../helpers/realtime-conditions/config.js";import{collectReferencedFormKeys as l,collectTouchedComponentIds as c,evaluateAll as u}from"../helpers/realtime-conditions/evaluator.js";import{COMPONENT_ACTIONS as a,apply as d,escapeSelector as h}from"../helpers/realtime-conditions/applier.js";import{DESCOPE_ATTRIBUTE_EXCLUDE_FIELD as m}from"../constants/index.js";const p="component-conditions:";function f(e){return e instanceof HTMLInputElement?"checkbox"===e.type?e.checked:e.value:"checked"in e?e.checked:"value"in e?e.value:void 0}function b(e,t){const n=Object.keys(e),o=Object.keys(t);return n.length===o.length&&n.every((n=>e[n]===t[n]))}function v(){return{conditions:[],touchedIds:new Set,snapshot:{},applied:{},serverBaseline:{},debounceTimer:null,paused:!1,unsubscribePauseListener:null,inputHandler:()=>{},root:null}}function g(e,t,n){const o={};return n.forEach((n=>{e[n]?o[n]=e[n]:t[n]&&(o[n]=t[n])})),o}const $=n((n=>{var $,k,y,T,j,E;const O=o(i)(n);return E=class extends O{constructor(){super(...arguments),$.add(this),k.set(this,v())}applyComponentsState(e,t){t&&(Object.entries(t).forEach((([e,t])=>{a.includes(t)||this.logger.error(`Unknown component action "${t}" for component with id "${e}"`,`Valid actions are ${a.map((e=>`"${e}"`)).join(", ")}`)})),d(e,{},t))}initRealtimeConditions(n,o){var i,r,a,m;e(this,$,"m",j).call(this);const v=null!==(i=null==o?void 0:o.realtimeComponentsConditions)&&void 0!==i?i:[];if(!v.length)return void this.logger.debug(`${p} no real-time rules to apply on this screen`);this.logger.info(`${p} found ${v.length} real-time rule(s) for this screen`);const T={};Object.entries(null!==(r=null==o?void 0:o.form)&&void 0!==r?r:{}).forEach((([e,t])=>{T[`form.${e}`]=t})),l(v).forEach((e=>{const t=function(e,t){const n=t.startsWith("form.")?t.slice(5):t;return e.querySelector(`[name="${h(t)}"]`)||e.querySelector(`[name="${h(n)}"]`)}(n,e);if(!t)return;const o=f(t);void 0!==o&&(T[e]=o)}));const E=c(v),O={},S={};Object.entries(null!==(a=null==o?void 0:o.componentsState)&&void 0!==a?a:{}).forEach((([e,t])=>{E.has(e)&&(S[e]=t)})),Object.entries(null!==(m=null==o?void 0:o.serverOnlyComponentsState)&&void 0!==m?m:{}).forEach((([e,t])=>{E.has(e)&&(O[e]=t)}));const L={conditions:v,touchedIds:E,snapshot:T,applied:S,serverBaseline:O,debounceTimer:null,paused:!1,unsubscribePauseListener:null,inputHandler:()=>{},root:n},C=g(u(v,T),O,E);b(S,C)||(L.applied=d(n,S,C)),L.inputHandler=t=>e(this,$,"m",y).call(this,t),s.forEach((e=>{n.addEventListener(e,L.inputHandler,!0)})),this.logger.debug(`${p} listening for "${s.join('" and "')}" events on form fields; ${Object.keys(S).length} component(s) start hidden / disabled / read-only by these rules`);const w=this.nextRequestStatus;if(null==w?void 0:w.subscribe){const e=({isLoading:e})=>{L.paused=e,e&&L.debounceTimer&&(clearTimeout(L.debounceTimer),L.debounceTimer=null)},t=w.subscribe(e);L.unsubscribePauseListener=()=>{var e;null!=t&&(null===(e=w.unsubscribe)||void 0===e||e.call(w,t))}}t(this,k,L,"f")}disconnectedCallback(){var t;null===(t=super.disconnectedCallback)||void 0===t||t.call(this),e(this,$,"m",j).call(this)}},k=new WeakMap,$=new WeakSet,y=function(t){var n,o;const i=e(this,k,"f");if(!i.root||i.paused)return;const s=t.target;if(!s)return;if(null===(n=s.hasAttribute)||void 0===n?void 0:n.call(s,m))return;const l=null===(o=s.getAttribute)||void 0===o?void 0:o.call(s,"name");if(!l)return;const c=function(e){return e.startsWith("form.")?e:`form.${e}`}(l);i.snapshot[c]=f(s),i.debounceTimer&&clearTimeout(i.debounceTimer),i.debounceTimer=setTimeout((()=>{i.debounceTimer=null,e(this,$,"m",T).call(this)}),r)},T=function(){const t=e(this,k,"f");if(!t.root)return;let n;try{n=g(u(t.conditions,t.snapshot),t.serverBaseline,t.touchedIds)}catch(e){return void this.logger.error(`${p} failed to evaluate real-time rules — keeping the previous state`,null==e?void 0:e.message)}const o=t.applied;b(o,n)||(this.logger.debug(`${p} form changed — updating components: was ${JSON.stringify(o)}, now ${JSON.stringify(n)}`),t.applied=d(t.root,o,n))},j=function(){const n=e(this,k,"f");n.debounceTimer&&clearTimeout(n.debounceTimer),n.unsubscribePauseListener&&n.unsubscribePauseListener(),n.root&&(Object.keys(n.applied).length>0&&n.root.isConnected&&d(n.root,n.applied,{}),s.forEach((e=>{n.root.removeEventListener(e,n.inputHandler,!0)}))),t(this,k,v(),"f")},E}));export{$ as componentConditionsMixin};
2
+ //# sourceMappingURL=componentConditionsMixin.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"componentConditionsMixin.js","sources":["../../../src/lib/mixins/componentConditionsMixin.ts"],"sourcesContent":["/* eslint-disable import/prefer-default-export, no-param-reassign */\nimport { compose, createSingletonMixin } from '@descope/sdk-helpers';\nimport { loggerMixin } from '@descope/sdk-mixins';\nimport {\n REALTIME_CONDITION_DEBOUNCE_MS,\n REALTIME_CONDITION_EVENTS,\n} from '../helpers/realtime-conditions/config';\nimport {\n collectReferencedFormKeys,\n collectTouchedComponentIds,\n evaluateAll,\n FormSnapshot,\n} from '../helpers/realtime-conditions/evaluator';\nimport {\n apply,\n COMPONENT_ACTIONS,\n escapeSelector,\n} from '../helpers/realtime-conditions/applier';\nimport { DESCOPE_ATTRIBUTE_EXCLUDE_FIELD } from '../constants';\nimport type { RealtimeComponentsCondition, ScreenState } from '../types';\n\nconst LOG_PREFIX = 'component-conditions:';\n\n// What the mixin needs from its host: a way to subscribe to loading state\n// so input handling can pause while the next request is in flight. Defined\n// as an interface (not imported) to avoid a circular dependency on BaseDescopeWc.\nexport interface PausableHost {\n nextRequestStatus?: {\n // subscribe returns a token; pass that token to unsubscribe — not the handler.\n subscribe: (cb: (v: { isLoading: boolean }) => void) => string;\n unsubscribe?: (token: string) => void;\n };\n}\n\nexport interface RealtimeRuntime {\n conditions: RealtimeComponentsCondition[];\n // IDs of components that any rule can change\n touchedIds: Set<string>;\n // latest form values on screen\n snapshot: FormSnapshot;\n // what's currently applied on screen for touched components.\n // Compared against on each re-evaluation to know what to change.\n applied: Record<string, string>;\n // What the server told us to apply for these components. Stays fixed for\n // the screen's lifetime so we can fall back to it when a real-time rule\n // stops matching. Without this, a server-set action could disappear when\n // a real-time rule briefly fires on the same component.\n serverBaseline: Record<string, string>;\n debounceTimer: ReturnType<typeof setTimeout> | null;\n // true while paused (during submit, to avoid stale updates)\n paused: boolean;\n unsubscribePauseListener: (() => void) | null;\n // the listener we attach to form events (kept so we can detach)\n inputHandler: (e: Event) => void;\n // the screen root we work against\n root: HTMLElement | null;\n}\n\n// Some inputs already have `name=\"form.x\"`, others use `name=\"x\"`. Always\n// return the `form.x` form so it lines up with what rules reference.\nfunction toFormKey(name: string): string {\n return name.startsWith('form.') ? name : `form.${name}`;\n}\n\n// Try the full `form.x` first (descope components), then `x` (plain inputs).\nfunction findInputForFormKey(\n root: ParentNode,\n formKey: string,\n): Element | null {\n const bare = formKey.startsWith('form.')\n ? formKey.slice('form.'.length)\n : formKey;\n return (\n root.querySelector(`[name=\"${escapeSelector(formKey)}\"]`) ||\n root.querySelector(`[name=\"${escapeSelector(bare)}\"]`)\n );\n}\n\n// Read the current value off a form input. Reads JS properties (not HTML\n// attributes) so we pick up both the component's default and any value\n// carried over from earlier screens.\n//\n// Native <input type=checkbox> needs special handling: `.value` is always\n// \"on\", only `.checked` reflects the real state. Descope boolean components\n// (descope-checkbox, descope-switch-toggle) expose `.checked` on their class.\nfunction readInputValue(el: Element): unknown {\n if (el instanceof HTMLInputElement) {\n return el.type === 'checkbox' ? el.checked : el.value;\n }\n if ('checked' in el) {\n return (el as unknown as { checked: boolean }).checked;\n }\n if ('value' in el) {\n return (el as unknown as { value: unknown }).value;\n }\n return undefined;\n}\n\nfunction shallowEqualStringMap(\n a: Record<string, string>,\n b: Record<string, string>,\n): boolean {\n const keysA = Object.keys(a);\n const keysB = Object.keys(b);\n if (keysA.length !== keysB.length) return false;\n return keysA.every((k) => a[k] === b[k]);\n}\n\nfunction emptyRuntime(): RealtimeRuntime {\n return {\n conditions: [],\n touchedIds: new Set(),\n snapshot: {},\n applied: {},\n serverBaseline: {},\n debounceTimer: null,\n paused: false,\n unsubscribePauseListener: null,\n inputHandler: () => {},\n root: null,\n };\n}\n\n// For each touched component, pick the real-time action if a rule is firing\n// for it, otherwise fall back to the server's action. Components that no rule\n// touches are not in the result — those were already painted by\n// applyComponentsState.\nfunction effectiveActions(\n realtimeVerdict: Record<string, string>,\n serverBaseline: Record<string, string>,\n touchedIds: Set<string>,\n): Record<string, string> {\n const out: Record<string, string> = {};\n touchedIds.forEach((id) => {\n if (realtimeVerdict[id]) {\n out[id] = realtimeVerdict[id];\n } else if (serverBaseline[id]) {\n out[id] = serverBaseline[id];\n }\n });\n return out;\n}\n\nexport const componentConditionsMixin = createSingletonMixin(\n <T extends CustomElementConstructor>(superclass: T) => {\n // Pull in loggerMixin so `this.logger` is available no matter where\n // this mixin sits in the chain.\n const BaseClass = compose(loggerMixin)(superclass);\n\n return class ComponentConditionsMixin extends BaseClass {\n // One runtime per <descope-wc> element.\n #rtRuntime: RealtimeRuntime = emptyRuntime();\n\n /**\n * Apply the server's hide/disable/read-only actions to the DOM.\n * Logs unknown actions here (runs once per screen) but stays quiet on\n * the keystroke path to avoid log spam.\n */\n applyComponentsState(\n root: ParentNode,\n componentsState: Record<string, string> | undefined,\n ): void {\n if (!componentsState) return;\n Object.entries(componentsState).forEach(([id, action]) => {\n if (!(COMPONENT_ACTIONS as readonly string[]).includes(action)) {\n this.logger.error(\n `Unknown component action \"${action}\" for component with id \"${id}\"`,\n `Valid actions are ${COMPONENT_ACTIONS.map((a) => `\"${a}\"`).join(\n ', ',\n )}`,\n );\n }\n });\n apply(root, {}, componentsState);\n }\n\n /**\n * Set up real-time conditions for the current screen. Safe to call\n * twice — a second call rebuilds from screenState.\n *\n * Must run AFTER `updateScreenFromScreenState`, so the DOM reads below\n * see both server-shipped values and any component defaults from the\n * HTML.\n */\n initRealtimeConditions(\n rootElement: HTMLElement,\n screenState: ScreenState | undefined,\n ): void {\n this.#teardownRealtimeRuntime();\n\n const conditions = screenState?.realtimeComponentsConditions ?? [];\n if (!conditions.length) {\n this.logger.debug(\n `${LOG_PREFIX} no real-time rules to apply on this screen`,\n );\n return;\n }\n this.logger.info(\n `${LOG_PREFIX} found ${conditions.length} real-time rule(s) for this screen`,\n );\n\n // screenState.form uses \"phone\"; rules reference \"form.phone\".\n // Add the prefix on the way in.\n const initialSnapshot: FormSnapshot = {};\n Object.entries(screenState?.form ?? {}).forEach(([k, v]) => {\n initialSnapshot[`form.${k}`] = v;\n });\n\n // For every key the rules reference, read the current DOM value.\n // Component defaults (e.g. `checked=\"true\"` on a descope-checkbox)\n // live only in the HTML and don't make it into screenState.form, so\n // without this read the first evaluation would miss them.\n collectReferencedFormKeys(conditions).forEach((formKey) => {\n const el = findInputForFormKey(rootElement, formKey);\n if (!el) return;\n const v = readInputValue(el);\n if (v !== undefined) initialSnapshot[formKey] = v;\n });\n\n const touchedIds = collectTouchedComponentIds(conditions);\n\n // What to fall back to when a real-time rule stops matching for a\n // touched component — the actions set by server-only rules (rules\n // with no client-side conditions).\n const serverBaseline: Record<string, string> = {};\n // What's already painted on the DOM (touched components only), so\n // the first real-time evaluation can diff against the right state.\n const initialApplied: Record<string, string> = {};\n\n Object.entries(screenState?.componentsState ?? {}).forEach(\n ([id, action]) => {\n if (!touchedIds.has(id)) return;\n initialApplied[id] = action;\n },\n );\n\n Object.entries(screenState?.serverOnlyComponentsState ?? {}).forEach(\n ([id, action]) => {\n if (touchedIds.has(id)) {\n serverBaseline[id] = action;\n }\n },\n );\n\n const runtime: RealtimeRuntime = {\n conditions,\n touchedIds,\n snapshot: initialSnapshot,\n applied: initialApplied,\n serverBaseline,\n debounceTimer: null,\n paused: false,\n unsubscribePauseListener: null,\n inputHandler: () => {},\n root: rootElement,\n };\n\n // Re-run the rules now that we've read the real DOM values. The\n // server evaluated without seeing component defaults, so its\n // `componentsState` can disagree with what the user actually sees.\n // Apply the diff once before listening for input.\n const reconciled = effectiveActions(\n evaluateAll(conditions, initialSnapshot),\n serverBaseline,\n touchedIds,\n );\n if (!shallowEqualStringMap(initialApplied, reconciled)) {\n runtime.applied = apply(rootElement, initialApplied, reconciled);\n }\n\n runtime.inputHandler = (e: Event) =>\n this.#handleRealtimeInput(e as InputEvent);\n\n REALTIME_CONDITION_EVENTS.forEach((ev) => {\n rootElement.addEventListener(ev, runtime.inputHandler, true);\n });\n this.logger.debug(\n `${LOG_PREFIX} listening for \"${REALTIME_CONDITION_EVENTS.join(\n '\" and \"',\n )}\" events on form fields; ${\n Object.keys(initialApplied).length\n } component(s) start hidden / disabled / read-only by these rules`,\n );\n\n // Pause while a /next request is in flight. Without this, a debounced\n // evaluation could run between submit and the next screen render and\n // change components that are about to be replaced — causing flicker,\n // or hiding something the next screen needs visible.\n const pauseState = (this as unknown as PausableHost).nextRequestStatus;\n if (pauseState?.subscribe) {\n const handler = ({ isLoading }: { isLoading: boolean }) => {\n runtime.paused = isLoading;\n if (isLoading && runtime.debounceTimer) {\n clearTimeout(runtime.debounceTimer);\n runtime.debounceTimer = null;\n }\n };\n // subscribe returns a token; unsubscribe needs that token, not the\n // handler. Without storing it, handlers leak across screens and\n // keep old runtimes (and their DOM roots) alive.\n const token = pauseState.subscribe(handler);\n runtime.unsubscribePauseListener = () => {\n if (token !== undefined && token !== null) {\n pauseState.unsubscribe?.(token);\n }\n };\n }\n\n this.#rtRuntime = runtime;\n }\n\n #handleRealtimeInput(e: InputEvent): void {\n const runtime = this.#rtRuntime;\n if (!runtime.root || runtime.paused) return;\n\n const target = e.target as Element | null;\n if (!target) return;\n if (target.hasAttribute?.(DESCOPE_ATTRIBUTE_EXCLUDE_FIELD)) return;\n\n const name = target.getAttribute?.('name');\n if (!name) return;\n\n // Update the snapshot only — evaluation is debounced. Use readInputValue\n // so checkboxes read the same way as at mount; otherwise `target.value`\n // would be the string \"on\" for native checkboxes and break is-true rules.\n const key = toFormKey(name);\n runtime.snapshot[key] = readInputValue(target);\n\n if (runtime.debounceTimer) {\n clearTimeout(runtime.debounceTimer);\n }\n runtime.debounceTimer = setTimeout(() => {\n runtime.debounceTimer = null;\n this.#reEvaluateRealtime();\n }, REALTIME_CONDITION_DEBOUNCE_MS);\n }\n\n #reEvaluateRealtime(): void {\n const runtime = this.#rtRuntime;\n if (!runtime.root) return;\n\n let next: Record<string, string>;\n try {\n next = effectiveActions(\n evaluateAll(runtime.conditions, runtime.snapshot),\n runtime.serverBaseline,\n runtime.touchedIds,\n );\n } catch (e) {\n this.logger.error(\n `${LOG_PREFIX} failed to evaluate real-time rules — keeping the previous state`,\n (e as Error)?.message,\n );\n return;\n }\n\n const prevApplied = runtime.applied;\n if (shallowEqualStringMap(prevApplied, next)) return;\n\n this.logger.debug(\n `${LOG_PREFIX} form changed — updating components: was ${JSON.stringify(\n prevApplied,\n )}, now ${JSON.stringify(next)}`,\n );\n runtime.applied = apply(runtime.root, prevApplied, next);\n }\n\n // Clean up listeners and timers when the host leaves the DOM. Reaches\n // us only if BaseDescopeWc calls `super.disconnectedCallback?.()`;\n // otherwise teardown happens lazily on the next initRealtimeConditions.\n disconnectedCallback() {\n // disconnectedCallback isn't on HTMLElement in the lib types, but it\n // exists at runtime — forward into super in case the chain uses it.\n // @ts-expect-error custom-element lifecycle method missing from lib types\n super.disconnectedCallback?.();\n this.#teardownRealtimeRuntime();\n }\n\n #teardownRealtimeRuntime(): void {\n const r = this.#rtRuntime;\n if (r.debounceTimer) {\n clearTimeout(r.debounceTimer);\n }\n if (r.unsubscribePauseListener) {\n r.unsubscribePauseListener();\n }\n if (r.root) {\n // Undo anything we applied so the next screen starts clean. Skip\n // if the root is already gone (replaced by the next screen) —\n // nothing left to undo.\n if (Object.keys(r.applied).length > 0 && r.root.isConnected) {\n apply(r.root, r.applied, {});\n }\n REALTIME_CONDITION_EVENTS.forEach((ev) => {\n r.root!.removeEventListener(ev, r.inputHandler, true);\n });\n }\n this.#rtRuntime = emptyRuntime();\n }\n };\n },\n);\n"],"names":["LOG_PREFIX","readInputValue","el","HTMLInputElement","type","checked","value","shallowEqualStringMap","a","b","keysA","Object","keys","keysB","length","every","k","emptyRuntime","conditions","touchedIds","Set","snapshot","applied","serverBaseline","debounceTimer","paused","unsubscribePauseListener","inputHandler","root","effectiveActions","realtimeVerdict","out","forEach","id","componentConditionsMixin","createSingletonMixin","superclass","BaseClass","compose","loggerMixin","_a","constructor","_ComponentConditionsMixin_rtRuntime","set","this","applyComponentsState","componentsState","entries","action","COMPONENT_ACTIONS","includes","logger","error","map","join","apply","initRealtimeConditions","rootElement","screenState","__classPrivateFieldGet","_ComponentConditionsMixin_instances","_ComponentConditionsMixin_teardownRealtimeRuntime","call","_b","realtimeComponentsConditions","debug","info","initialSnapshot","_c","form","v","collectReferencedFormKeys","formKey","bare","startsWith","slice","querySelector","escapeSelector","findInputForFormKey","undefined","collectTouchedComponentIds","initialApplied","_d","has","_e","serverOnlyComponentsState","runtime","reconciled","evaluateAll","e","REALTIME_CONDITION_EVENTS","ev","addEventListener","pauseState","nextRequestStatus","subscribe","handler","isLoading","clearTimeout","token","unsubscribe","__classPrivateFieldSet","disconnectedCallback","super","target","hasAttribute","DESCOPE_ATTRIBUTE_EXCLUDE_FIELD","name","getAttribute","key","toFormKey","setTimeout","_ComponentConditionsMixin_reEvaluateRealtime","REALTIME_CONDITION_DEBOUNCE_MS","next","message","prevApplied","JSON","stringify","r","isConnected","removeEventListener"],"mappings":"6nBAqBA,MAAMA,EAAa,wBAgEnB,SAASC,EAAeC,GACtB,OAAIA,aAAcC,iBACG,aAAZD,EAAGE,KAAsBF,EAAGG,QAAUH,EAAGI,MAE9C,YAAaJ,EACPA,EAAuCG,QAE7C,UAAWH,EACLA,EAAqCI,WAD/C,CAIF,CAEA,SAASC,EACPC,EACAC,GAEA,MAAMC,EAAQC,OAAOC,KAAKJ,GACpBK,EAAQF,OAAOC,K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wF,GAAO5G,EAAeuG,GAEnClB,EAAQ9D,eACV0E,aAAaZ,EAAQ9D,eAEvB8D,EAAQ9D,cAAgBuF,YAAW,KACjCzB,EAAQ9D,cAAgB,KACxBmC,EAAAf,KAAIgB,EAAA,IAAAoD,GAAJlD,KAAAlB,KAA0B,GACzBqE,EACJ,eAGC,MAAM3B,EAAU3B,EAAAf,YAChB,IAAK0C,EAAQ1D,KAAM,OAEnB,IAAIsF,EACJ,IACEA,EAAOrF,EACL2D,EAAYF,EAAQpE,WAAYoE,EAAQjE,UACxCiE,EAAQ/D,eACR+D,EAAQnE,WAEX,CAAC,MAAOsE,GAKP,YAJA7C,KAAKO,OAAOC,MACV,GAAGpD,oEACFyF,aAAC,EAADA,EAAa0B,QAGjB,CAED,MAAMC,EAAc9B,EAAQhE,QACxBf,EAAsB6G,EAAaF,KAEvCtE,KAAKO,OAAOc,MACV,GAAGjE,6CAAsDqH,KAAKC,UAC5DF,WACQC,KAAKC,UAAUJ,MAE3B5B,EAAQhE,QAAUiC,EAAM+B,EAAQ1D,KAAMwF,EAAaF,GACpD,eAcC,MAAMK,EAAI5D,EAAAf,YACN2E,EAAE/F,eACJ0E,aAAaqB,EAAE/F,eAEb+F,EAAE7F,0BACJ6F,EAAE7F,2BAEA6F,EAAE3F,OAIAjB,OAAOC,KAAK2G,EAAEjG,SAASR,OAAS,GAAKyG,EAAE3F,KAAK4F,aAC9CjE,EAAMgE,EAAE3F,KAAM2F,EAAEjG,QAAS,CAAE,GAE7BoE,EAA0B1D,SAAS2D,IACjC4B,EAAE3F,KAAM6F,oBAAoB9B,EAAI4B,EAAE5F,cAAc,EAAK,KAGzD0E,EAAAzD,KAAIF,EAAczB,QACnB,EACDuB,CAAA"}
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","sources":["../../src/lib/types.ts"],"sourcesContent":["/* istanbul ignore file */\n\nimport type { JWTResponse } from '@descope/web-js-sdk';\nimport { createSdk } from '@descope/web-js-sdk';\n\nexport type SdkConfig = Parameters<typeof createSdk>[0];\nexport type Sdk = ReturnType<typeof createSdk>;\n\nexport type SdkFlowNext = Sdk['flow']['next'];\n\nexport type ComponentsDynamicAttrs = {\n attributes: Record<string, any>;\n};\n\nexport type ComponentsConfig = Record<string, any> & {\n componentsDynamicAttrs?: Record<string, ComponentsDynamicAttrs>;\n};\nexport type CssVars = Record<string, any>;\n\ntype KeepArgsByIndex<F, Indices extends readonly number[]> = F extends (\n ...args: infer A\n) => infer R\n ? (...args: PickArgsByIndex<A, Indices>) => R\n : never;\n\ntype PickArgsByIndex<\n All extends readonly any[],\n Indices extends readonly number[],\n> = {\n [K in keyof Indices]: Indices[K] extends keyof All ? All[Indices[K]] : never;\n};\n\ntype Project = {\n name: string;\n};\n\nexport enum Direction {\n backward = 'backward',\n forward = 'forward',\n}\n\nexport type LastAuthState = NonNullable<\n NextFnReturnPromiseValue['data']['lastAuth']\n> & {\n loginId?: string;\n name?: string;\n lastUsedPerScreen?: Record<string, string>;\n};\n\nexport interface ScreenState {\n errorText?: string;\n errorType?: string;\n componentsConfig?: ComponentsConfig;\n cssVars?: CssVars;\n form?: Record<string, string>;\n inputs?: Record<string, string>; // Backward compatibility\n lastAuth?: LastAuthState;\n project?: Project;\n totp?: { image?: string; provisionUrl?: string };\n notp?: { image?: string; redirectUrl?: string };\n selfProvisionDomains?: unknown;\n user?: unknown;\n sso?: unknown;\n dynamicSelects?: unknown;\n keysInUse?: unknown;\n genericForm?: unknown;\n linkId?: unknown;\n sentTo?: unknown;\n clientScripts?: ClientScript[];\n // map of component IDs to their state\n componentsState?: Record<string, string>;\n}\n\nexport type SSOQueryParams = {\n oidcIdpStateId?: string;\n samlIdpStateId?: string;\n wsfedIdpStateId?: string;\n samlIdpUsername?: string;\n descopeIdpInitiated?: boolean;\n ssoAppId?: string;\n customAppId?: string;\n thirdPartyAppId: string;\n thirdPartyAppStateId?: string;\n applicationScopes?: string;\n} & OIDCOptions;\n\nexport type OIDCOptions = {\n oidcLoginHint?: string;\n oidcPrompt?: string;\n oidcErrorRedirectUri?: string;\n oidcResource?: string;\n};\n\nexport type Locale = {\n locale: string;\n fallback: string;\n};\n\nexport type FlowState = {\n flowId: string;\n projectId: string;\n baseUrl: string;\n tenant: string;\n stepId: string;\n stepName: string;\n executionId: string;\n action: string;\n redirectTo: string;\n redirectIsPopup: boolean;\n openInNewTabUrl?: string;\n redirectUrl: string;\n screenId: string;\n screenState: ScreenState;\n token: string;\n code: string;\n isPopup: boolean;\n exchangeError: string;\n webauthnTransactionId: string;\n webauthnOptions: string;\n redirectAuthCodeChallenge: string;\n redirectAuthCallbackUrl: string;\n redirectAuthBackupCallbackUri: string;\n redirectAuthInitiator: string;\n deferredRedirect: boolean;\n deferredPolling: boolean;\n locale: string;\n samlIdpResponseUrl: string;\n samlIdpResponseSamlResponse: string;\n samlIdpResponseRelayState: string;\n wsFedIdpResponseUrl: string;\n wsFedIdpResponseWresult: string;\n wsFedIdpResponseWctx: string;\n nativeResponseType: string;\n nativePayload: Record<string, any>;\n reqTimestamp: number;\n} & SSOQueryParams;\n\nexport type StepState = {\n screenState: ScreenState;\n screenId: string;\n stepName: string;\n htmlFilename: string;\n htmlLocaleFilename: string;\n next: NextFn;\n direction: Direction | undefined;\n samlIdpUsername: string;\n action?: string;\n locale?: string;\n} & OIDCOptions;\n\nexport type CustomScreenState = Omit<\n ScreenState,\n 'cssVars' | 'componentsConfig' | 'inputs'\n> & {\n error?: {\n text: ScreenState['errorText'];\n type: ScreenState['errorType'];\n };\n action?: string;\n inboundAppApproveScopes?: {\n desc: string;\n id: string;\n required: boolean;\n }[];\n};\n\nexport type DebugState = {\n isDebug: boolean;\n};\n\nexport interface ScriptElement extends HTMLDivElement {\n moduleRes?: ScriptModule;\n}\n\nexport type ScriptModule = {\n /**\n * Unique identifier of the module.\n */\n id: string;\n /**\n * Notifies the module that it should start any profiling or monitoring.\n */\n start?: () => void;\n /**\n * Notifies the module that it should stop any profiling or monitoring.\n */\n stop?: () => void;\n /**\n * Presents the user with any required interaction to get a refreshed token or state,\n * e.g., a challenge or captcha.\n *\n * Modules should return a value of true if the presentation completed successfully,\n * false if it was cancelled by the user, and throw an error in case of failure.\n *\n * This is called before form submission (via a next call) after a button click.\n */\n present?: () => Promise<boolean>;\n /**\n * Refreshes any tokens or state that might be needed before form submission.\n *\n * Modules should throw an error in case of failure.\n */\n refresh?: () => Promise<void>;\n};\n\nexport type ClientScript = {\n id: string;\n initArgs: Record<string, any>;\n resultKey?: string;\n};\n\nexport type NextFn = KeepArgsByIndex<SdkFlowNext, [2, 5]> & {\n isCustomScreen?: boolean;\n};\nexport type NextFnReturnPromiseValue = Awaited<ReturnType<NextFn>>;\n\nexport type DebuggerMessage = {\n title: string;\n description?: string;\n};\n\nexport type FlowStateUpdateFn = (state: FlowState) => void;\n\ntype Operator =\n | 'equal'\n | 'not-equal'\n | 'contains'\n | 'greater-than'\n | 'greater-than-or-equal'\n | 'less-than'\n | 'less-than-or-equal'\n | 'empty'\n | 'not-empty'\n | 'is-true'\n | 'is-false'\n | 'in'\n | 'not-in'\n | 'in-range'\n | 'not-in-range'\n | 'devised-by';\n\nexport interface ClientConditionResult {\n screenId: string;\n screenName: string;\n clientScripts?: ClientScript[];\n componentsConfig?: ComponentsConfig;\n interactionId: string;\n}\n\nexport interface ClientCondition {\n operator: Operator;\n key: string;\n predicate?: string | number;\n met: ClientConditionResult;\n unmet?: ClientConditionResult;\n}\n\nexport type AutoFocusOptions = true | false | 'skipFirstScreen';\n\nexport type ThemeOptions = 'light' | 'dark' | 'os';\n\nexport type Key =\n | 'lastAuth.loginId'\n | 'idpInitiated'\n | 'externalToken'\n | 'abTestingKey';\n\ntype CheckFunction = (ctx: Context, predicate?: string | number) => boolean;\n\nexport type ConditionsMap = {\n [key in Key]: {\n [operator in Operator]?: CheckFunction;\n };\n};\n\nexport interface Context {\n loginId?: string;\n code?: string;\n token?: string;\n abTestingKey?: number;\n lastAuth?: LastAuthState;\n}\n\nexport type DescopeUI = Record<string, () => Promise<void>> & {\n componentsThemeManager: Record<string, any>;\n};\n\ntype Font = {\n family: string[];\n label: string;\n url?: string;\n};\n\ntype ThemeTemplate = {\n fonts: {\n font1: Font;\n font2: Font;\n };\n};\n\ntype ThemeColor = {\n main: string;\n dark: string;\n light: string;\n highlight: string;\n contrast: string;\n};\n\nexport type OverrideTheme = {\n globals?: {\n colors?: {\n primary?: ThemeColor;\n secondary?: ThemeColor;\n };\n };\n};\n\nexport type OverrideThemes = {\n dark?: OverrideTheme;\n light?: OverrideTheme;\n};\n\nexport type FlowConfig = {\n startScreenId?: string;\n startScreenName?: string;\n version: number;\n targetLocales?: string[];\n conditions?: ClientCondition[];\n condition?: ClientCondition;\n fingerprintEnabled?: boolean;\n fingerprintKey?: string;\n sdkScripts?: [\n {\n id: string;\n initArgs: Record<string, any>;\n resultKey?: string;\n },\n ];\n clientScripts?: ClientScript[];\n componentsConfig?: ComponentsConfig;\n};\n\nexport interface ProjectConfiguration {\n componentsVersion: string;\n cssTemplate: {\n dark: ThemeTemplate;\n light: ThemeTemplate;\n };\n flows: {\n [key: string]: FlowConfig; // dynamic key names for flows\n };\n}\n\nexport type FlowStatus = 'loading' | 'error' | 'success' | 'ready' | 'initial';\n\nexport type CustomStorage = {\n getItem: (key: string) => string | null;\n setItem: (key: string, value: string) => void;\n removeItem: (key: string) => void;\n};\n\nexport type FlowJWTResponse = JWTResponse & {\n flowOutput?: Record<string, any>;\n};\n"],"names":["Direction"],"mappings":"IAoCYA,GAAZ,SAAYA,GACVA,EAAA,SAAA,WACAA,EAAA,QAAA,SACD,CAHD,CAAYA,IAAAA,EAGX,CAAA"}
1
+ {"version":3,"file":"types.js","sources":["../../src/lib/types.ts"],"sourcesContent":["/* istanbul ignore file */\n\nimport type { JWTResponse } from '@descope/web-js-sdk';\nimport { createSdk } from '@descope/web-js-sdk';\n\nexport type SdkConfig = Parameters<typeof createSdk>[0];\nexport type Sdk = ReturnType<typeof createSdk>;\n\nexport type SdkFlowNext = Sdk['flow']['next'];\n\nexport type ComponentsDynamicAttrs = {\n attributes: Record<string, any>;\n};\n\nexport type ComponentsConfig = Record<string, any> & {\n componentsDynamicAttrs?: Record<string, ComponentsDynamicAttrs>;\n};\nexport type CssVars = Record<string, any>;\n\ntype KeepArgsByIndex<F, Indices extends readonly number[]> = F extends (\n ...args: infer A\n) => infer R\n ? (...args: PickArgsByIndex<A, Indices>) => R\n : never;\n\ntype PickArgsByIndex<\n All extends readonly any[],\n Indices extends readonly number[],\n> = {\n [K in keyof Indices]: Indices[K] extends keyof All ? All[Indices[K]] : never;\n};\n\ntype Project = {\n name: string;\n};\n\nexport enum Direction {\n backward = 'backward',\n forward = 'forward',\n}\n\nexport type LastAuthState = NonNullable<\n NextFnReturnPromiseValue['data']['lastAuth']\n> & {\n loginId?: string;\n name?: string;\n lastUsedPerScreen?: Record<string, string>;\n};\n\nexport type RealtimeOperandKind = 'value' | 'form' | 'list';\n\n// Operators the SDK is allowed to evaluate locally. The server's\n// clientSupportedRealtimeOperators must stay in sync — anything outside this\n// list is pre-evaluated server-side and shipped as a value operand.\n//\n// is-email / is-phone are intentionally absent: the server validates them\n// with dedicated libraries (contact.IsValid*); the client has no equivalent,\n// so the server keeps these CCs server-only.\nexport type RealtimeOperator =\n | 'equal'\n | 'not-equal'\n | 'contains'\n | 'greater-than'\n | 'greater-than-or-equal'\n | 'less-than'\n | 'less-than-or-equal'\n | 'empty'\n | 'not-empty'\n | 'is-true'\n | 'is-false'\n | 'in'\n | 'not-in'\n | 'matches';\n\nexport interface RealtimeOperand {\n kind: RealtimeOperandKind;\n // Form is the context key the client looks up from the live form snapshot\n // (e.g. \"form.phone\"). Set only when kind === 'form'.\n form?: string;\n // Items is the operand list when kind === 'list'. Each element is a nested\n // operand — either a form placeholder or a resolved literal. Used when the\n // server detects an `in` / `not-in` / `contains` predicate whose array\n // contains `{{form.X}}` references; the client resolves them at eval time.\n items?: RealtimeOperand[];\n // Pre-resolved literal. Set only when kind === 'value', and may legitimately\n // be false / 0 / \"\" — do not treat absence as \"no value\" without checking\n // kind first.\n value?: unknown;\n}\n\nexport interface RealtimeAtomicCondition {\n operator: RealtimeOperator;\n target?: RealtimeOperand;\n predicate?: RealtimeOperand;\n}\n\nexport interface RealtimeRule {\n logicalOr?: boolean;\n atomicConditions: RealtimeAtomicCondition[];\n}\n\nexport interface RealtimeComponentsCondition {\n id?: string;\n componentIds: string[];\n action: string;\n rules: RealtimeRule[];\n}\n\nexport interface ScreenState {\n errorText?: string;\n errorType?: string;\n componentsConfig?: ComponentsConfig;\n cssVars?: CssVars;\n form?: Record<string, string>;\n inputs?: Record<string, string>; // Backward compatibility\n lastAuth?: LastAuthState;\n project?: Project;\n totp?: { image?: string; provisionUrl?: string };\n notp?: { image?: string; redirectUrl?: string };\n selfProvisionDomains?: unknown;\n user?: unknown;\n sso?: unknown;\n dynamicSelects?: unknown;\n keysInUse?: unknown;\n genericForm?: unknown;\n linkId?: unknown;\n sentTo?: unknown;\n clientScripts?: ClientScript[];\n // map of component IDs to their state — the FULL last-wins verdict over\n // all CCs (server-only + client-eligible) the BE evaluated at screen-init.\n // Used by `applyComponentsState` for the first DOM paint.\n componentsState?: Record<string, string>;\n // Subset of `componentsState` contributed by SERVER-ONLY CCs — those the\n // client cannot re-evaluate locally (operators on the server-only\n // allow-list like `is-email`, or rules referencing context the client\n // doesn't have). Parallels `componentsState` in structure but excludes\n // contributions from client-eligible CCs that also ship in\n // `realtimeComponentsConditions`.\n //\n // The realtime layer uses this as the fallback action to restore when a\n // realtime CC stops firing on a touched component — without it the SDK\n // can't tell whether the action in `componentsState` came from a\n // server-only CC (must persist) or from a realtime CC also re-shipped\n // (must clear).\n //\n // Absent on old backends; new SDKs fall back to a legacy heuristic that\n // infers the same information from `componentsState`, so the old-BE /\n // new-SDK combination still works correctly.\n serverOnlyComponentsState?: Record<string, string>;\n // Client-evaluable visibility conditions, populated only by new backends.\n // Absent on old backends; new SDKs ignore when absent.\n realtimeComponentsConditions?: RealtimeComponentsCondition[];\n}\n\nexport type SSOQueryParams = {\n oidcIdpStateId?: string;\n samlIdpStateId?: string;\n wsfedIdpStateId?: string;\n samlIdpUsername?: string;\n descopeIdpInitiated?: boolean;\n ssoAppId?: string;\n customAppId?: string;\n thirdPartyAppId: string;\n thirdPartyAppStateId?: string;\n applicationScopes?: string;\n} & OIDCOptions;\n\nexport type OIDCOptions = {\n oidcLoginHint?: string;\n oidcPrompt?: string;\n oidcErrorRedirectUri?: string;\n oidcResource?: string;\n};\n\nexport type Locale = {\n locale: string;\n fallback: string;\n};\n\nexport type FlowState = {\n flowId: string;\n projectId: string;\n baseUrl: string;\n tenant: string;\n stepId: string;\n stepName: string;\n executionId: string;\n action: string;\n redirectTo: string;\n redirectIsPopup: boolean;\n openInNewTabUrl?: string;\n redirectUrl: string;\n screenId: string;\n screenState: ScreenState;\n token: string;\n code: string;\n isPopup: boolean;\n exchangeError: string;\n webauthnTransactionId: string;\n webauthnOptions: string;\n redirectAuthCodeChallenge: string;\n redirectAuthCallbackUrl: string;\n redirectAuthBackupCallbackUri: string;\n redirectAuthInitiator: string;\n deferredRedirect: boolean;\n deferredPolling: boolean;\n locale: string;\n samlIdpResponseUrl: string;\n samlIdpResponseSamlResponse: string;\n samlIdpResponseRelayState: string;\n wsFedIdpResponseUrl: string;\n wsFedIdpResponseWresult: string;\n wsFedIdpResponseWctx: string;\n nativeResponseType: string;\n nativePayload: Record<string, any>;\n reqTimestamp: number;\n} & SSOQueryParams;\n\nexport type StepState = {\n screenState: ScreenState;\n screenId: string;\n stepName: string;\n htmlFilename: string;\n htmlLocaleFilename: string;\n next: NextFn;\n direction: Direction | undefined;\n samlIdpUsername: string;\n action?: string;\n locale?: string;\n} & OIDCOptions;\n\nexport type CustomScreenState = Omit<\n ScreenState,\n 'cssVars' | 'componentsConfig' | 'inputs'\n> & {\n error?: {\n text: ScreenState['errorText'];\n type: ScreenState['errorType'];\n };\n action?: string;\n inboundAppApproveScopes?: {\n desc: string;\n id: string;\n required: boolean;\n }[];\n};\n\nexport type DebugState = {\n isDebug: boolean;\n};\n\nexport interface ScriptElement extends HTMLDivElement {\n moduleRes?: ScriptModule;\n}\n\nexport type ScriptModule = {\n /**\n * Unique identifier of the module.\n */\n id: string;\n /**\n * Notifies the module that it should start any profiling or monitoring.\n */\n start?: () => void;\n /**\n * Notifies the module that it should stop any profiling or monitoring.\n */\n stop?: () => void;\n /**\n * Presents the user with any required interaction to get a refreshed token or state,\n * e.g., a challenge or captcha.\n *\n * Modules should return a value of true if the presentation completed successfully,\n * false if it was cancelled by the user, and throw an error in case of failure.\n *\n * This is called before form submission (via a next call) after a button click.\n */\n present?: () => Promise<boolean>;\n /**\n * Refreshes any tokens or state that might be needed before form submission.\n *\n * Modules should throw an error in case of failure.\n */\n refresh?: () => Promise<void>;\n};\n\nexport type ClientScript = {\n id: string;\n initArgs: Record<string, any>;\n resultKey?: string;\n};\n\nexport type NextFn = KeepArgsByIndex<SdkFlowNext, [2, 5]> & {\n isCustomScreen?: boolean;\n};\nexport type NextFnReturnPromiseValue = Awaited<ReturnType<NextFn>>;\n\nexport type DebuggerMessage = {\n title: string;\n description?: string;\n};\n\nexport type FlowStateUpdateFn = (state: FlowState) => void;\n\ntype Operator =\n | 'equal'\n | 'not-equal'\n | 'contains'\n | 'greater-than'\n | 'greater-than-or-equal'\n | 'less-than'\n | 'less-than-or-equal'\n | 'empty'\n | 'not-empty'\n | 'is-true'\n | 'is-false'\n | 'in'\n | 'not-in'\n | 'in-range'\n | 'not-in-range'\n | 'devised-by';\n\nexport interface ClientConditionResult {\n screenId: string;\n screenName: string;\n clientScripts?: ClientScript[];\n componentsConfig?: ComponentsConfig;\n interactionId: string;\n}\n\nexport interface ClientCondition {\n operator: Operator;\n key: string;\n predicate?: string | number;\n met: ClientConditionResult;\n unmet?: ClientConditionResult;\n}\n\nexport type AutoFocusOptions = true | false | 'skipFirstScreen';\n\nexport type ThemeOptions = 'light' | 'dark' | 'os';\n\nexport type Key =\n | 'lastAuth.loginId'\n | 'idpInitiated'\n | 'externalToken'\n | 'abTestingKey';\n\ntype CheckFunction = (ctx: Context, predicate?: string | number) => boolean;\n\nexport type ConditionsMap = {\n [key in Key]: {\n [operator in Operator]?: CheckFunction;\n };\n};\n\nexport interface Context {\n loginId?: string;\n code?: string;\n token?: string;\n abTestingKey?: number;\n lastAuth?: LastAuthState;\n}\n\nexport type DescopeUI = Record<string, () => Promise<void>> & {\n componentsThemeManager: Record<string, any>;\n};\n\ntype Font = {\n family: string[];\n label: string;\n url?: string;\n};\n\ntype ThemeTemplate = {\n fonts: {\n font1: Font;\n font2: Font;\n };\n};\n\ntype ThemeColor = {\n main: string;\n dark: string;\n light: string;\n highlight: string;\n contrast: string;\n};\n\nexport type OverrideTheme = {\n globals?: {\n colors?: {\n primary?: ThemeColor;\n secondary?: ThemeColor;\n };\n };\n};\n\nexport type OverrideThemes = {\n dark?: OverrideTheme;\n light?: OverrideTheme;\n};\n\nexport type FlowConfig = {\n startScreenId?: string;\n startScreenName?: string;\n version: number;\n targetLocales?: string[];\n conditions?: ClientCondition[];\n condition?: ClientCondition;\n fingerprintEnabled?: boolean;\n fingerprintKey?: string;\n sdkScripts?: [\n {\n id: string;\n initArgs: Record<string, any>;\n resultKey?: string;\n },\n ];\n clientScripts?: ClientScript[];\n componentsConfig?: ComponentsConfig;\n};\n\nexport interface ProjectConfiguration {\n componentsVersion: string;\n cssTemplate: {\n dark: ThemeTemplate;\n light: ThemeTemplate;\n };\n flows: {\n [key: string]: FlowConfig; // dynamic key names for flows\n };\n}\n\nexport type FlowStatus = 'loading' | 'error' | 'success' | 'ready' | 'initial';\n\nexport type CustomStorage = {\n getItem: (key: string) => string | null;\n setItem: (key: string, value: string) => void;\n removeItem: (key: string) => void;\n};\n\nexport type FlowJWTResponse = JWTResponse & {\n flowOutput?: Record<string, any>;\n};\n"],"names":["Direction"],"mappings":"IAoCYA,GAAZ,SAAYA,GACVA,EAAA,SAAA,WACAA,EAAA,QAAA,SACD,CAHD,CAAYA,IAAAA,EAGX,CAAA"}