@voxgig/apidef 8.17.2 → 8.18.1

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.
package/src/utility.ts CHANGED
@@ -1637,19 +1637,39 @@ function envelopeProp(resprops: any, opname: string): string | null {
1637
1637
  return null
1638
1638
  }
1639
1639
 
1640
+ // Beside an `id` or other data, one object is the record's own, such as
1641
+ // Lob's link and its metadata. A single item sits beside status or paging.
1642
+ if (!islist && keys.some((k: string) => k !== key &&
1643
+ !isEnvelopeStatusProp(k) && !isEnvelopePagingProp(k))) {
1644
+ return null
1645
+ }
1646
+
1640
1647
  return key
1641
1648
  }
1642
1649
 
1643
1650
 
1651
+ // What a single-item envelope may hold beside its item, compared as the
1652
+ // paging names are.
1653
+ const ENVELOPE_STATUS_PROPS = new Set([
1654
+ 'success', 'status', 'ok', 'message', 'code', 'error', 'errorcode',
1655
+ 'errormessage', 'requestid', 'timestamp', 'took', 'version', 'apiversion',
1656
+ ])
1657
+
1658
+
1659
+ function isEnvelopeStatusProp(name: string): boolean {
1660
+ return ENVELOPE_STATUS_PROPS.has(name.toLowerCase().replace(/[_-]/g, ''))
1661
+ }
1662
+
1663
+
1644
1664
  // What a page may hold beside its records, compared without case, `_` or
1645
1665
  // `-`. Any other property is data, which makes the component a record.
1646
1666
  const ENVELOPE_PAGING_PROPS = new Set([
1647
1667
  'count', 'total', 'totalcount', 'totalhits', 'totalitems', 'totalpages',
1648
1668
  'totalresults', 'page', 'pages', 'pagecount', 'pagenumber', 'pagesize',
1649
1669
  'perpage', 'limit', 'offset', 'cursor', 'next', 'nextcursor', 'nextpage',
1650
- 'nextpagetoken', 'nexttoken', 'previous', 'prev', 'previouscursor',
1651
- 'prevcursor', 'previouspage', 'prevpage', 'hasmore', 'hasnext',
1652
- 'hasprevious', 'object', 'url',
1670
+ 'nextpagetoken', 'nexttoken', 'nexturl', 'previous', 'prev',
1671
+ 'previouscursor', 'prevcursor', 'previouspage', 'prevpage', 'previousurl',
1672
+ 'prevurl', 'hasmore', 'hasnext', 'hasprevious', 'object', 'url',
1653
1673
  ])
1654
1674
 
1655
1675
 
@@ -1658,32 +1678,93 @@ function isEnvelopePagingProp(name: string): boolean {
1658
1678
  }
1659
1679
 
1660
1680
 
1681
+ // Paging, or a count that describes the page, such as the number of Lob's QR
1682
+ // codes on the page that were scanned at least once.
1683
+ function isEnvelopePageProp(name: string): boolean {
1684
+ return isEnvelopePagingProp(name) || name.toLowerCase().replace(/[_-]/g, '').endsWith('count')
1685
+ }
1686
+
1687
+
1661
1688
  // The component a response envelope carries: the resolved reference of the
1662
- // record envelopeProp unwraps to. Narrower than envelopeProp, since a record
1663
- // with one structured property passes that test too: an envelope has no `id`,
1664
- // a page holds nothing beside its records but paging, and a single-item
1665
- // envelope holds nothing beside the item.
1689
+ // record strictEnvelopeProp unwraps to.
1666
1690
  function envelopeItemRef(schema: any, opname: string): string | null {
1667
1691
  const props = schema?.properties
1692
+ const key = strictEnvelopeProp(props, opname)
1693
+ if (null == key) {
1694
+ return null
1695
+ }
1696
+
1697
+ const prop = props[key]
1698
+ const item = propIsList(prop) ? prop.items : prop
1699
+ if (!isRecordSchema(item)) {
1700
+ return null
1701
+ }
1702
+
1703
+ const xref = item['x-ref']
1704
+ return 'string' === typeof xref && '' !== xref ? xref : null
1705
+ }
1706
+
1707
+
1708
+ // Narrower than envelopeProp, since a record with one structured property
1709
+ // passes that test too: an envelope has no `id`, a page holds nothing beside
1710
+ // its records but paging and counts, and a single-item envelope holds nothing
1711
+ // beside the item.
1712
+ function strictEnvelopeProp(props: any, opname: string): string | null {
1668
1713
  const key = envelopeProp(props, opname)
1669
1714
  if (null == key || null != props.id) {
1670
1715
  return null
1671
1716
  }
1672
1717
 
1673
- const prop = props[key]
1674
- const islist = propIsList(prop)
1718
+ const islist = propIsList(props[key])
1675
1719
  const rest = keysof(props).filter((k: string) => k !== key)
1676
- if (islist ? !rest.every(isEnvelopePagingProp) : 0 < rest.length) {
1720
+ if (islist ? !rest.every(isEnvelopePageProp) : 0 < rest.length) {
1677
1721
  return null
1678
1722
  }
1679
1723
 
1680
- const item = islist ? prop.items : prop
1681
- if (!isRecordSchema(item)) {
1724
+ return key
1725
+ }
1726
+
1727
+
1728
+ // The envelope of a schema composed with allOf, such as Lob's list,
1729
+ // allOf[list, {data: [...]}]. Only the strict rules apply: most composed
1730
+ // schemas are records, and a record's one object is its data.
1731
+ function composedEnvelopeProp(schema: any, opname: string): string | null {
1732
+ if (null == schema || 'object' !== typeof schema || !Array.isArray(schema.allOf)) {
1682
1733
  return null
1683
1734
  }
1735
+ return strictEnvelopeProp(mergedProperties(schema), opname)
1736
+ }
1684
1737
 
1685
- const xref = item['x-ref']
1686
- return 'string' === typeof xref && '' !== xref ? xref : null
1738
+
1739
+ // The properties of a schema and its allOf members; the first declaration of
1740
+ // a name wins. Read-only, since parsed nodes are shared between references.
1741
+ function mergedProperties(schema: any): Record<string, any> | undefined {
1742
+ let out: Record<string, any> | undefined
1743
+ const seen = new Set<any>()
1744
+
1745
+ const visit = (node: any) => {
1746
+ if (null == node || 'object' !== typeof node || seen.has(node)) {
1747
+ return
1748
+ }
1749
+ seen.add(node)
1750
+
1751
+ const props = node.properties
1752
+ if (null != props && 'object' === typeof props && !Array.isArray(props)) {
1753
+ for (const key of Object.keys(props)) {
1754
+ out = out ?? Object.create(null)
1755
+ if (!(key in out!)) {
1756
+ out![key] = props[key]
1757
+ }
1758
+ }
1759
+ }
1760
+
1761
+ if (Array.isArray(node.allOf)) {
1762
+ node.allOf.forEach(visit)
1763
+ }
1764
+ }
1765
+
1766
+ visit(schema)
1767
+ return out
1687
1768
  }
1688
1769
 
1689
1770
 
@@ -1857,6 +1938,8 @@ export {
1857
1938
  isEntityWrapperProp,
1858
1939
  envelopeProp,
1859
1940
  envelopeItemRef,
1941
+ composedEnvelopeProp,
1942
+ mergedProperties,
1860
1943
  closedBodyTransform,
1861
1944
  untaggedUnionBranches,
1862
1945
  scanUntaggedUnion,