@voxgig/apidef 8.15.2 → 8.17.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 (55) hide show
  1. package/dist/apidef.js +15 -0
  2. package/dist/apidef.js.map +1 -1
  3. package/dist/builder/entity/entity.js +8 -5
  4. package/dist/builder/entity/entity.js.map +1 -1
  5. package/dist/builder/entity/info.js +1 -1
  6. package/dist/builder/entity/info.js.map +1 -1
  7. package/dist/builder/flow.js +3 -3
  8. package/dist/builder/flow.js.map +1 -1
  9. package/dist/cli.js +8 -10
  10. package/dist/cli.js.map +1 -1
  11. package/dist/desc.d.ts +2 -0
  12. package/dist/guide/guide.d.ts +14 -1
  13. package/dist/guide/guide.js +106 -62
  14. package/dist/guide/guide.js.map +1 -1
  15. package/dist/guide/heuristic01.d.ts +9 -1
  16. package/dist/guide/heuristic01.js +290 -71
  17. package/dist/guide/heuristic01.js.map +1 -1
  18. package/dist/parse.d.ts +4 -1
  19. package/dist/parse.js +183 -133
  20. package/dist/parse.js.map +1 -1
  21. package/dist/refcount.d.ts +6 -0
  22. package/dist/refcount.js +215 -0
  23. package/dist/refcount.js.map +1 -0
  24. package/dist/transform/entity.d.ts +1 -1
  25. package/dist/transform/entity.js +12 -3
  26. package/dist/transform/entity.js.map +1 -1
  27. package/dist/transform/field.js +1 -1
  28. package/dist/transform/field.js.map +1 -1
  29. package/dist/transform/operation.js +1 -1
  30. package/dist/transform/operation.js.map +1 -1
  31. package/dist/transform/top.js +1 -1
  32. package/dist/transform/top.js.map +1 -1
  33. package/dist/tsconfig.tsbuildinfo +1 -1
  34. package/dist/utility.d.ts +3 -1
  35. package/dist/utility.js +85 -6
  36. package/dist/utility.js.map +1 -1
  37. package/model/{guide.aon → guide.aontu} +1 -1
  38. package/package.json +3 -3
  39. package/src/apidef.ts +20 -1
  40. package/src/builder/entity/entity.ts +8 -5
  41. package/src/builder/entity/info.ts +1 -1
  42. package/src/builder/flow.ts +3 -3
  43. package/src/cli.ts +8 -10
  44. package/src/desc.ts +3 -0
  45. package/src/guide/guide.ts +136 -71
  46. package/src/guide/heuristic01.ts +358 -90
  47. package/src/parse.ts +221 -126
  48. package/src/refcount.ts +244 -0
  49. package/src/transform/entity.ts +16 -5
  50. package/src/transform/field.ts +1 -1
  51. package/src/transform/operation.ts +1 -1
  52. package/src/transform/top.ts +1 -1
  53. package/src/types.ts +2 -2
  54. package/src/utility.ts +102 -6
  55. /package/model/{apidef.aon → apidef.aontu} +0 -0
@@ -34,10 +34,6 @@ const entityTransform: Transform = async function(
34
34
 
35
35
  let msg = ''
36
36
 
37
- if (true !== ctx.def?.graphql) {
38
- mergeCollectionPaths(guide, ctx.log)
39
- }
40
-
41
37
  each(guide.entity, (guideEntity: GuideEntity, entname: string) => {
42
38
  if (!guideActive(guideEntity)) {
43
39
  ctx.log.debug({ point: 'guide-entity', note: entname, active: false })
@@ -86,8 +82,11 @@ function filterEntityAncestors(entities: Record<string, any>) {
86
82
  // Move "/X" paths onto the entity that owns "/X/{id}" or "/X/{id}/sub".
87
83
  // Only acts when the path "/X" sits on a different entity than the
88
84
  // per-instance paths — leaves correctly-classified APIs alone.
89
- function mergeCollectionPaths(guide: any, log?: any) {
85
+ // Returns the entities the moves emptied, which are removed.
86
+ // Guide stage only: on the unified guide it would override guide.aontu.
87
+ function mergeCollectionPaths(guide: any, log?: any): string[] {
90
88
  const entities = guide.entity as Record<string, any>
89
+ const emptied: string[] = []
91
90
 
92
91
  const rootOwners: Record<string, { ename: string, depth: number }> = {}
93
92
 
@@ -169,7 +168,19 @@ function mergeCollectionPaths(guide: any, log?: any) {
169
168
  to: owner.ename,
170
169
  })
171
170
  }
171
+
172
+ if (0 < pathsToMove.length && 0 === Object.keys(entity.path).length) {
173
+ emptied.push(ename)
174
+ }
172
175
  }
176
+
177
+ // With no path left it names nothing guide.aontu could switch back on.
178
+ for (const ename of emptied) {
179
+ delete entities[ename]
180
+ log?.debug?.({ point: 'merge-collection-drop', entity: ename })
181
+ }
182
+
183
+ return emptied
173
184
  }
174
185
 
175
186
 
@@ -509,7 +509,7 @@ function compositeId(
509
509
  return { single: singleKeyOf(ment, identityParams(ment)) } as any
510
510
  }
511
511
 
512
- // `from` STATED IN guide.aon WINS PER PART, so a spec can correct one
512
+ // `from` STATED IN guide.aontu WINS PER PART, so a spec can correct one
513
513
  // mapping without restating the others — which matters because the
514
514
  // heuristic gets most of them right and the odd one wrong.
515
515
  const withFrom = (parts: string[], usesep: string) => {
@@ -27,7 +27,7 @@ import type {
27
27
 
28
28
  // The op names the transform resolves. Anything else under a guide path's
29
29
  // `op` map is dropped, and an unknown name (a verb such as `merge`, or a
30
- // typo) is dropped WITH A WARNING: guide.aon is the only correction surface
30
+ // typo) is dropped WITH A WARNING: guide.aontu is the only correction surface
31
31
  // (ADR-002), so a correction that vanishes silently defeats it. A non-CRUD
32
32
  // verb is declared as `action: <verb>: {}` beside a CRUD op on the same path.
33
33
  const RESOLVED_OPS = ['load', 'list', 'create', 'update', 'remove', 'patch']
@@ -40,7 +40,7 @@ const topTransform = async function(
40
40
  // Guarantee at least one sentence of API description. Many specs (e.g. the
41
41
  // readme.io-hosted Bluefin APIs) ship a placeholder `info.description` of
42
42
  // "." — letterless, useless prose. When the description is empty or has no
43
- // letters, synthesise a sentence from the title so the api-info.aon (and
43
+ // letters, synthesise a sentence from the title so the api-info.aontu (and
44
44
  // the docs generated from it) never carry an empty/degenerate description.
45
45
  kit.info.description = ensureDescription(kit.info)
46
46
 
package/src/types.ts CHANGED
@@ -268,10 +268,10 @@ type GuideEntity = {
268
268
  name: string
269
269
  orig: string
270
270
  // `false` drops the entity downstream (transform/entity.ts). Emitted by
271
- // the heuristic for an access-token exchange, and editable in guide.aon —
271
+ // the heuristic for an access-token exchange, and editable in guide.aontu —
272
272
  // which is the ONLY correction surface (ADR-002).
273
273
  active?: boolean
274
- // Why the heuristic deactivated it, so guide.aon reads as a record of a
274
+ // Why the heuristic deactivated it, so guide.aontu reads as a record of a
275
275
  // decision rather than an unexplained `active: false`.
276
276
  why_inactive?: string
277
277
  field?: Record<string, GuidePath>
package/src/utility.ts CHANGED
@@ -66,6 +66,32 @@ function writeFileSyncWarn(warn: Warner, fs: any, path: string, text: string) {
66
66
  }
67
67
 
68
68
 
69
+ // Before `.aon` was retired apidef wrote each of its files under that
70
+ // extension, so the `.aon` twin of a file it writes as `.aontu` is its own.
71
+ function removeLegacyAon(fs: any, log: any, file: string): boolean {
72
+ const legacy = file.replace(/\.aontu$/, '.aon')
73
+ if (legacy === file || !fs.existsSync(legacy)) {
74
+ return false
75
+ }
76
+
77
+ try {
78
+ fs.unlinkSync(legacy)
79
+ log?.info?.({
80
+ point: 'legacy-aon', file: legacy,
81
+ note: 'removed ' + relativizePath(legacy) + ', now written as .aontu',
82
+ })
83
+ return true
84
+ }
85
+ catch (err: any) {
86
+ log?.warn?.({
87
+ point: 'legacy-aon-failed', file: legacy, err,
88
+ note: 'could not remove ' + relativizePath(legacy) + ': ' + err?.message,
89
+ })
90
+ return false
91
+ }
92
+ }
93
+
94
+
69
95
 
70
96
  function getdlog(
71
97
  tagin?: string,
@@ -159,6 +185,7 @@ const IRREGULARS: Record<string, string> = Object.assign(Object.create(null), {
159
185
  'premises': 'premise',
160
186
  'promises': 'promise',
161
187
  'psyches': 'psyche',
188
+ 'purchases': 'purchase',
162
189
  'purses': 'purse',
163
190
  'releases': 'release',
164
191
  'roses': 'rose',
@@ -177,6 +204,15 @@ const IRREGULARS: Record<string, string> = Object.assign(Object.create(null), {
177
204
  })
178
205
 
179
206
 
207
+ // The stems whose -ves plural comes from -fe (knives) or -f (wolves). The
208
+ // -fe stems match the whole word, since olives is not the plural of olife.
209
+ const FE_PLURAL_STEMS = ['kni', 'li', 'wi']
210
+ const F_PLURAL_STEMS = [
211
+ 'cal', 'dwar', 'el', 'hal', 'hoo', 'lea', 'loa', 'scar', 'shea', 'thie',
212
+ 'whar', 'wol',
213
+ ]
214
+
215
+
180
216
  // Sorted longest-first so the most specific IRREGULARS suffix wins.
181
217
  // Without this, 'women' would be shadowed by 'men' (3 < 5) under
182
218
  // insertion-order iteration. Both happen to round-trip correctly
@@ -274,16 +310,21 @@ function depluralize(word: string): string {
274
310
  }
275
311
  }
276
312
 
277
- // -ves -> -f or -fe (wolves -> wolf, knives -> knife)
313
+ // -ves -> -f or -fe only for the words that take it (wolves -> wolf,
314
+ // knives -> knife); every other -ves plural drops the -s alone
315
+ // (objectives -> objective).
278
316
  if (lower.endsWith('ves')) {
279
317
  const stem = word.slice(0, -3)
318
+ const lstem = stem.toLowerCase()
280
319
  const dropped = word.slice(-3)
281
320
  const isUpper = dropped === dropped.toUpperCase()
282
- // Check if it should be -fe (like knife, wife, life)
283
- if (['kni', 'wi', 'li'].includes(stem.toLowerCase())) {
321
+ if (FE_PLURAL_STEMS.includes(lstem)) {
284
322
  return stem + (isUpper ? 'FE' : 'fe')
285
323
  }
286
- return stem + (isUpper ? 'F' : 'f')
324
+ if (F_PLURAL_STEMS.some((fstem) => lstem.endsWith(fstem))) {
325
+ return stem + (isUpper ? 'F' : 'f')
326
+ }
327
+ return word.slice(0, -1)
287
328
  }
288
329
 
289
330
  // -oes -> -o (potatoes -> potato)
@@ -1141,7 +1182,7 @@ const CMP_RESULT_VERBS = [
1141
1182
 
1142
1183
  const CMP_PREFIXES = ['get_', 'post_', 'put_', 'delete_', 'patch_']
1143
1184
 
1144
- // A guide node (entity, path or op) is active unless guide.aon explicitly
1185
+ // A guide node (entity, path or op) is active unless guide.aontu explicitly
1145
1186
  // says otherwise. `active` has always been declared in the guide model and
1146
1187
  // documented as the escape hatch for a misclassified entity; this is the
1147
1188
  // single place that decides what it means, so entity/flow/operation transforms
@@ -1224,12 +1265,13 @@ function requestBodySchema(requestBody: any): any {
1224
1265
  }
1225
1266
 
1226
1267
 
1268
+ // Sorted, not definition order, which the Go parser does not keep.
1227
1269
  function schemaProps(schema: any): string[] {
1228
1270
  const props = schema?.properties
1229
1271
  if (null == props || 'object' !== typeof props) {
1230
1272
  return []
1231
1273
  }
1232
- return Object.keys(props)
1274
+ return sortedKeys(props)
1233
1275
  }
1234
1276
 
1235
1277
 
@@ -1595,6 +1637,58 @@ function envelopeProp(resprops: any, opname: string): string | null {
1595
1637
  }
1596
1638
 
1597
1639
 
1640
+ // What a page may hold beside its records, compared without case, `_` or
1641
+ // `-`. Any other property is data, which makes the component a record.
1642
+ const ENVELOPE_PAGING_PROPS = new Set([
1643
+ 'count', 'total', 'totalcount', 'totalhits', 'totalitems', 'totalpages',
1644
+ 'totalresults', 'page', 'pages', 'pagecount', 'pagenumber', 'pagesize',
1645
+ 'perpage', 'limit', 'offset', 'cursor', 'next', 'nextcursor', 'nextpage',
1646
+ 'nextpagetoken', 'nexttoken', 'previous', 'prev', 'previouscursor',
1647
+ 'prevcursor', 'previouspage', 'prevpage', 'hasmore', 'hasnext',
1648
+ 'hasprevious', 'object', 'url',
1649
+ ])
1650
+
1651
+
1652
+ function isEnvelopePagingProp(name: string): boolean {
1653
+ return ENVELOPE_PAGING_PROPS.has(name.toLowerCase().replace(/[_-]/g, ''))
1654
+ }
1655
+
1656
+
1657
+ // The component a response envelope carries: the resolved reference of the
1658
+ // record envelopeProp unwraps to. Narrower than envelopeProp, since a record
1659
+ // with one structured property passes that test too: an envelope has no `id`,
1660
+ // a page holds nothing beside its records but paging, and a single-item
1661
+ // envelope holds nothing beside the item.
1662
+ function envelopeItemRef(schema: any, opname: string): string | null {
1663
+ const props = schema?.properties
1664
+ const key = envelopeProp(props, opname)
1665
+ if (null == key || null != props.id) {
1666
+ return null
1667
+ }
1668
+
1669
+ const prop = props[key]
1670
+ const islist = propIsList(prop)
1671
+ const rest = keysof(props).filter((k: string) => k !== key)
1672
+ if (islist ? !rest.every(isEnvelopePagingProp) : 0 < rest.length) {
1673
+ return null
1674
+ }
1675
+
1676
+ const item = islist ? prop.items : prop
1677
+ if (!isRecordSchema(item)) {
1678
+ return null
1679
+ }
1680
+
1681
+ const xref = item['x-ref']
1682
+ return 'string' === typeof xref && '' !== xref ? xref : null
1683
+ }
1684
+
1685
+
1686
+ function isRecordSchema(schema: any): boolean {
1687
+ return null != schema && 'object' === typeof schema && (
1688
+ null != schema.properties || null != schema.allOf || 'object' === schema.type)
1689
+ }
1690
+
1691
+
1598
1692
  function untaggedUnionBranches(schema: any): number {
1599
1693
  if (null == schema || 'object' !== typeof schema) {
1600
1694
  return 0
@@ -1750,6 +1844,7 @@ export {
1750
1844
  debugpathOn,
1751
1845
  findPathsWithPrefix,
1752
1846
  writeFileSyncWarn,
1847
+ removeLegacyAon,
1753
1848
  warnOnError,
1754
1849
  relativizePath,
1755
1850
  getModelPath,
@@ -1757,6 +1852,7 @@ export {
1757
1852
  sortedEntries,
1758
1853
  isEntityWrapperProp,
1759
1854
  envelopeProp,
1855
+ envelopeItemRef,
1760
1856
  closedBodyTransform,
1761
1857
  untaggedUnionBranches,
1762
1858
  scanUntaggedUnion,
File without changes