Extract {domain, methodStr, operationName, paramsCddl} from a command/event leaf def.
(def)
| 508 | |
| 509 | /** Extract {domain, methodStr, operationName, paramsCddl} from a command/event leaf def. */ |
| 510 | function parseLeafDef(def) { |
| 511 | const flatProps = (def.Properties ?? []).flatMap((p) => (Array.isArray(p) ? p : [p])) |
| 512 | |
| 513 | const methodProp = flatProps.find((p) => p.Name === 'method') |
| 514 | const paramsProp = flatProps.find((p) => p.Name === 'params') |
| 515 | if (!methodProp || !paramsProp) return null |
| 516 | |
| 517 | const methodLiteral = Array.isArray(methodProp.Type) ? methodProp.Type : [methodProp.Type] |
| 518 | if (methodLiteral[0]?.Type !== 'literal') return null |
| 519 | |
| 520 | const methodStr = methodLiteral[0].Value // e.g. "browser.createUserContext" |
| 521 | const dotIdx = methodStr.indexOf('.') |
| 522 | if (dotIdx === -1) return null |
| 523 | |
| 524 | const domainRaw = methodStr.slice(0, dotIdx) |
| 525 | const operationName = methodStr.slice(dotIdx + 1) |
| 526 | const domain = METHOD_DOMAIN_MAP[domainRaw] ?? 'common' |
| 527 | |
| 528 | const paramsTypeEntries = Array.isArray(paramsProp.Type) ? paramsProp.Type : [paramsProp.Type] |
| 529 | let paramsCddl = null |
| 530 | if (paramsTypeEntries[0]?.Type === 'group' && paramsTypeEntries[0]?.Value) { |
| 531 | paramsCddl = paramsTypeEntries[0].Value |
| 532 | } |
| 533 | |
| 534 | return { domain, methodStr, operationName, paramsCddl } |
| 535 | } |
| 536 | |
| 537 | /** |
| 538 | * Collect all leaf command/event names from every XxxCommand / XxxEvent |
no test coverage detected