@voxgig/apidef 8.28.1 → 8.30.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 (49) hide show
  1. package/README.md +18 -0
  2. package/dist/builder/entity/entity.js +1 -1
  3. package/dist/builder/entity/entity.js.map +1 -1
  4. package/dist/builder/entity/info.js +1 -1
  5. package/dist/builder/entity/info.js.map +1 -1
  6. package/dist/guide/graphql01.js +6 -3
  7. package/dist/guide/graphql01.js.map +1 -1
  8. package/dist/guide/guide.js +2 -10
  9. package/dist/guide/guide.js.map +1 -1
  10. package/dist/guide/heuristic01.js +87 -18
  11. package/dist/guide/heuristic01.js.map +1 -1
  12. package/dist/model.d.ts +1 -0
  13. package/dist/parse.d.ts +2 -1
  14. package/dist/parse.js +65 -18
  15. package/dist/parse.js.map +1 -1
  16. package/dist/transform/entity.js +3 -3
  17. package/dist/transform/entity.js.map +1 -1
  18. package/dist/transform/field.d.ts +2 -1
  19. package/dist/transform/field.js +33 -21
  20. package/dist/transform/field.js.map +1 -1
  21. package/dist/transform/flowstep.js +21 -1
  22. package/dist/transform/flowstep.js.map +1 -1
  23. package/dist/transform/operation.js +13 -24
  24. package/dist/transform/operation.js.map +1 -1
  25. package/dist/transform/top.js +8 -0
  26. package/dist/transform/top.js.map +1 -1
  27. package/dist/tsconfig.tsbuildinfo +1 -1
  28. package/dist/utility.d.ts +10 -2
  29. package/dist/utility.js +177 -28
  30. package/dist/utility.js.map +1 -1
  31. package/dist/yamlalias.d.ts +3 -0
  32. package/dist/yamlalias.js +269 -0
  33. package/dist/yamlalias.js.map +1 -0
  34. package/model/apidef.aontu +1 -0
  35. package/package.json +1 -1
  36. package/src/builder/entity/entity.ts +1 -1
  37. package/src/builder/entity/info.ts +1 -1
  38. package/src/guide/graphql01.ts +9 -3
  39. package/src/guide/guide.ts +2 -10
  40. package/src/guide/heuristic01.ts +101 -20
  41. package/src/model.ts +1 -0
  42. package/src/parse.ts +68 -26
  43. package/src/transform/entity.ts +5 -5
  44. package/src/transform/field.ts +39 -21
  45. package/src/transform/flowstep.ts +24 -1
  46. package/src/transform/operation.ts +15 -27
  47. package/src/transform/top.ts +9 -0
  48. package/src/utility.ts +204 -28
  49. package/src/yamlalias.ts +295 -0
package/dist/utility.js CHANGED
@@ -42,6 +42,10 @@ exports.paramName = paramName;
42
42
  exports.humanTitle = humanTitle;
43
43
  exports.foldName = foldName;
44
44
  exports.prefixLeadingDigit = prefixLeadingDigit;
45
+ exports.guardPrototypeName = guardPrototypeName;
46
+ exports.guideParts = guideParts;
47
+ exports.guidePath = guidePath;
48
+ exports.stripOperationVerb = stripOperationVerb;
45
49
  exports.debugpath = debugpath;
46
50
  exports.debugpathOn = debugpathOn;
47
51
  exports.findPathsWithPrefix = findPathsWithPrefix;
@@ -56,10 +60,13 @@ exports.itemEnvelopeKey = itemEnvelopeKey;
56
60
  exports.itemEnvelopeTransform = itemEnvelopeTransform;
57
61
  exports.itemEnvelopeOf = itemEnvelopeOf;
58
62
  exports.envelopeProp = envelopeProp;
63
+ exports.recordLists = recordLists;
64
+ exports.singleEnvelope = singleEnvelope;
59
65
  exports.envelopeItemRef = envelopeItemRef;
60
66
  exports.composedEnvelopeProp = composedEnvelopeProp;
61
67
  exports.pageRecords = pageRecords;
62
68
  exports.answersPage = answersPage;
69
+ exports.answersRecordMap = answersRecordMap;
63
70
  exports.propIsList = propIsList;
64
71
  exports.mergedProperties = mergedProperties;
65
72
  exports.closedBodyTransform = closedBodyTransform;
@@ -78,10 +85,6 @@ const sortedKeys = (obj) => Object.keys(obj ?? {}).sort();
78
85
  exports.sortedKeys = sortedKeys;
79
86
  const sortedEntries = (obj) => Object.entries(obj ?? {}).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0);
80
87
  exports.sortedEntries = sortedEntries;
81
- // Pre-compiled regex patterns for formatJsonSrc to avoid recompilation per call.
82
- const RE_JSON_KEY = /"([a-zA-Z_][a-zA-Z_0-9]*)": /g;
83
- const RE_JSON_TRAILING_BRACE = /},/g;
84
- const RE_JSON_COMMENT = /\n(\s*)([a-zA-Z_][a-zA-Z_0-9]*)_COMMENT:\s*"(.*)",/g;
85
88
  const RE_BARE_KEY = /^[A-Za-z_][_A-Za-z0-9]*$/;
86
89
  const isBareKey = (k) => RE_BARE_KEY.test(k);
87
90
  const quoteKey = (k) => (isBareKey(k) ? k : JSON.stringify(k));
@@ -155,11 +158,38 @@ function loadFile(path, what, fs, log) {
155
158
  throw err;
156
159
  }
157
160
  }
161
+ // Unquotes bare keys and breaks the line after `},`, only outside strings, so
162
+ // no string value is rewritten.
158
163
  function formatJsonSrc(jsonsrc) {
159
- return jsonsrc
160
- .replace(RE_JSON_KEY, '$1: ')
161
- .replace(RE_JSON_TRAILING_BRACE, '}\n')
162
- .replace(RE_JSON_COMMENT, '\n\n$1# $2 $3');
164
+ let out = '';
165
+ let i = 0;
166
+ while (i < jsonsrc.length) {
167
+ const ch = jsonsrc[i];
168
+ if ('"' === ch) {
169
+ let end = i + 1;
170
+ while (end < jsonsrc.length && '"' !== jsonsrc[end]) {
171
+ end += '\\' === jsonsrc[end] ? 2 : 1;
172
+ }
173
+ const body = jsonsrc.substring(i + 1, end);
174
+ if (': ' === jsonsrc.substring(end + 1, end + 3) && isBareKey(body)) {
175
+ out += body + ': ';
176
+ i = end + 3;
177
+ }
178
+ else {
179
+ out += jsonsrc.substring(i, end + 1);
180
+ i = end + 1;
181
+ }
182
+ }
183
+ else if ('}' === ch && ',' === jsonsrc[i + 1]) {
184
+ out += '}\n';
185
+ i += 2;
186
+ }
187
+ else {
188
+ out += ch;
189
+ i++;
190
+ }
191
+ }
192
+ return out;
163
193
  }
164
194
  const IRREGULARS = Object.assign(Object.create(null), {
165
195
  'aircraft': 'aircraft',
@@ -659,6 +689,7 @@ function formatJSONIC(val, opts) {
659
689
  const useColor = opts?.color ?? false;
660
690
  const maxlines = opts?.maxlines ?? Number.MAX_VALUE;
661
691
  const exclude = opts?.exclude ?? [];
692
+ const comments = opts?.comments ?? true;
662
693
  // ANSI color codes
663
694
  const colors = {
664
695
  reset: '\x1b[0m',
@@ -676,13 +707,7 @@ function formatJSONIC(val, opts) {
676
707
  return c('null', 'null');
677
708
  const t = typeof v;
678
709
  switch (t) {
679
- case 'string': return c('string', !v.includes('\n') ? JSON.stringify(v) :
680
- '`' + JSON.stringify(v)
681
- .substring(1)
682
- .replace(/\\n/g, '\n')
683
- .replace(/\\"/g, '"')
684
- .replace(/`/g, '\\`')
685
- .replace(/"$/, '`'));
710
+ case 'string': return c('string', !v.includes('\n') ? JSON.stringify(v) : backtickString(v));
686
711
  case 'number': return c('number', Number.isFinite(v) ? String(v) : 'null');
687
712
  case 'boolean': return c('boolean', v ? 'true' : 'false');
688
713
  case 'bigint': return c('string', JSON.stringify(v.toString()));
@@ -693,7 +718,7 @@ function formatJSONIC(val, opts) {
693
718
  default: return JSON.stringify(v);
694
719
  }
695
720
  };
696
- const renderComment = (c) => {
721
+ const commentText = (c) => {
697
722
  if (c == null)
698
723
  return null;
699
724
  if (Array.isArray(c) && c.every(x => typeof x === 'string'))
@@ -707,9 +732,33 @@ function formatJSONIC(val, opts) {
707
732
  return String(c);
708
733
  }
709
734
  };
710
- return renderJSONIC(val, hsepd, showd, useColor, maxlines, exclude, c, renderPrimitive, renderComment, false);
735
+ // A comment runs to the end of its line, so a line break in it would start
736
+ // source.
737
+ const renderComment = (c) => {
738
+ const text = comments ? commentText(c) : null;
739
+ return null == text ? null : text.replace(RE_LINE_BREAKS, ' ');
740
+ };
741
+ return renderJSONIC(val, hsepd, showd, useColor, maxlines, exclude, c, renderPrimitive, renderComment, comments, false);
742
+ }
743
+ const RE_LINE_BREAKS = /[\r\n\u2028\u2029]+/g;
744
+ // Its JSON escapes, read in order so an escaped backslash never starts
745
+ // another, with a newline and a quote written as themselves.
746
+ function backtickString(s) {
747
+ const json = JSON.stringify(s);
748
+ let out = '`';
749
+ for (let i = 1; i < json.length - 1; i++) {
750
+ const ch = json[i];
751
+ if ('\\' === ch) {
752
+ const esc = json[++i];
753
+ out += 'n' === esc ? '\n' : '"' === esc ? '"' : '\\' + esc;
754
+ }
755
+ else {
756
+ out += '`' === ch ? '\\`' : ch;
757
+ }
758
+ }
759
+ return out + '`';
711
760
  }
712
- function renderJSONIC(val, hsepd, showd, useColor, maxlines, exclude, c, renderPrimitive, renderComment, decircularized) {
761
+ function renderJSONIC(val, hsepd, showd, useColor, maxlines, exclude, c, renderPrimitive, renderComment, comments, decircularized) {
713
762
  const space = ' ';
714
763
  const seen = new WeakSet();
715
764
  let stack = new Array(32);
@@ -760,7 +809,7 @@ function renderJSONIC(val, hsepd, showd, useColor, maxlines, exclude, c, renderP
760
809
  flat = undefined;
761
810
  }
762
811
  if (undefined !== flat) {
763
- return renderJSONIC(flat, hsepd, showd, useColor, maxlines, exclude, c, renderPrimitive, renderComment, true);
812
+ return renderJSONIC(flat, hsepd, showd, useColor, maxlines, exclude, c, renderPrimitive, renderComment, comments, true);
764
813
  }
765
814
  }
766
815
  // decircular did not resolve it, so render a marker rather than
@@ -800,7 +849,7 @@ function renderJSONIC(val, hsepd, showd, useColor, maxlines, exclude, c, renderP
800
849
  if (v instanceof Error) {
801
850
  keys.unshift('name', 'message', 'stack');
802
851
  }
803
- const printableKeys = keys.filter(k => !k.endsWith('_COMMENT') &&
852
+ const printableKeys = keys.filter(k => !(comments && k.endsWith('_COMMENT')) &&
804
853
  (showd || !k.endsWith('$')));
805
854
  if (printableKeys.length === 0) {
806
855
  lines.push(`${linePrefix}${c('bracket', '{')}${commentSuffix}`);
@@ -969,6 +1018,42 @@ function foldName(name) {
969
1018
  return String((0, jostraca_1.camelify)(name)).toLowerCase();
970
1019
  }
971
1020
  const FIRST_LETTER_RE = /[a-zA-Z]/;
1021
+ // Formats a route segment may name beside its resource, or alone.
1022
+ const FORMAT_EXTENSIONS = new Set([
1023
+ 'json', 'xml', 'csv', 'tsv', 'yaml', 'yml', 'txt', 'html', 'htm', 'rss',
1024
+ 'atom', 'jsonl', 'ndjson', 'geojson',
1025
+ ]);
1026
+ // A route segment as the guide reads it: {id}.json is {id}, repos.json and
1027
+ // data.{format} are repos and data, and a format alone (json) names nothing.
1028
+ function guideSegment(seg) {
1029
+ const held = seg.match(/^(\{[^}]+\})\.(?:[A-Za-z0-9]+|\{[^}]+\})$/);
1030
+ if (null != held)
1031
+ return held[1];
1032
+ const named = seg.match(/^([^{}.]+)\.([A-Za-z0-9]+|\{[^}]+\})$/);
1033
+ if (null != named && ('{' === named[2][0] || FORMAT_EXTENSIONS.has(named[2].toLowerCase()))) {
1034
+ return named[1];
1035
+ }
1036
+ return FORMAT_EXTENSIONS.has(seg.toLowerCase()) ? null : seg;
1037
+ }
1038
+ // The guide's parts of a route; a route of formats alone keeps them.
1039
+ function guideParts(path) {
1040
+ const parts = path.split('/').filter((p) => '' !== p);
1041
+ const kept = parts.map(guideSegment).filter((p) => null != p);
1042
+ return 0 === kept.length ? parts : kept;
1043
+ }
1044
+ function guidePath(path) {
1045
+ return '/' + guideParts(path).join('/');
1046
+ }
1047
+ // aontu cannot unify a spread with a key Object.prototype owns, so no entity
1048
+ // is given one of these names. Listed, not read, so both ports agree.
1049
+ const PROTOTYPE_NAMES = new Set([
1050
+ 'constructor', '__defineGetter__', '__defineSetter__', 'hasOwnProperty',
1051
+ '__lookupGetter__', '__lookupSetter__', 'isPrototypeOf',
1052
+ 'propertyIsEnumerable', 'toString', 'valueOf', '__proto__', 'toLocaleString',
1053
+ ]);
1054
+ function guardPrototypeName(s) {
1055
+ return PROTOTYPE_NAMES.has(s) ? s + '_item' : s;
1056
+ }
972
1057
  function prefixLeadingDigit(s) {
973
1058
  if (null == s || '' === s)
974
1059
  return s;
@@ -1089,7 +1174,7 @@ function ensureMinEntityName(name, existing) {
1089
1174
  }
1090
1175
  padded = truncated || parts[0].substring(0, MAX_ENTITY_NAME_LEN);
1091
1176
  }
1092
- padded = prefixLeadingDigit(padded);
1177
+ padded = guardPrototypeName(prefixLeadingDigit(padded));
1093
1178
  if (padded.length < MIN_ENTITY_NAME_LEN) {
1094
1179
  const padding = 'nt'.substring(0, MIN_ENTITY_NAME_LEN - padded.length);
1095
1180
  padded = padded + padding;
@@ -1111,12 +1196,29 @@ function ensureMinEntityName(name, existing) {
1111
1196
  }
1112
1197
  // Unconditional suffixes: framework noise, always stripped.
1113
1198
  const CMP_SUFFIXES = ['_rest_controller', '_controller', '_response', '_request'];
1114
- const CMP_GUARDED_SUFFIXES = ['_create_response', '_update_response', '_page_response', '_page'];
1199
+ // Longest first: the first suffix a name ends with is the only one tried.
1200
+ const CMP_GUARDED_SUFFIXES = [
1201
+ '_create_response', '_update_response', '_page_response',
1202
+ '_response_collection', '_response_single', '_response_dto',
1203
+ '_collection', '_single', '_dto', '_page',
1204
+ ];
1115
1205
  const CMP_RESULT_VERBS = [
1116
1206
  'list', 'create', 'show', 'update', 'delete', 'remove',
1117
- 'get', 'edit', 'resolve', 'rotate', 'search',
1207
+ 'get', 'edit', 'resolve', 'rotate', 'search', 'add',
1118
1208
  ];
1119
1209
  const CMP_PREFIXES = ['get_', 'post_', 'put_', 'delete_', 'patch_'];
1210
+ const OPERATION_VERBS = [
1211
+ ...CMP_RESULT_VERBS, 'post', 'put', 'patch', 'fetch', 'find', 'retrieve', 'read', 'add',
1212
+ ];
1213
+ // An operationId leads with its action, so get_service_status names
1214
+ // service_status. The verb stays when what follows is too short to name one.
1215
+ function stripOperationVerb(opid) {
1216
+ const cut = opid.indexOf('_');
1217
+ if (cut < 0)
1218
+ return opid;
1219
+ const rest = opid.substring(cut + 1);
1220
+ return OPERATION_VERBS.includes(opid.substring(0, cut)) && 3 <= rest.length ? rest : opid;
1221
+ }
1120
1222
  // A guide node (entity, path or op) is active unless guide.aontu explicitly
1121
1223
  // says otherwise. `active` has always been declared in the guide model and
1122
1224
  // documented as the escape hatch for a misclassified entity; this is the
@@ -1239,7 +1341,7 @@ function cleanComponentName(name, isKnownCmp) {
1239
1341
  // (CallRouteAllowedCaller), so the namespace is tried singular too.
1240
1342
  const cands = after
1241
1343
  ? [canonize(depluralize(after)), canonize(depluralize(before + '_' + after)),
1242
- canonize(depluralize(before) + '_' + depluralize(after))]
1344
+ canonize(depluralize(before) + '_' + depluralize(after)), canonize(depluralize(before))]
1243
1345
  : [canonize(depluralize(before))];
1244
1346
  for (const cand of cands) {
1245
1347
  if (cand.length >= 3 && knownWithVersion(cand, version)) {
@@ -1272,6 +1374,14 @@ function cleanComponentName(name, isKnownCmp) {
1272
1374
  }
1273
1375
  }
1274
1376
  }
1377
+ // A list wrapper names its records: list_asset is asset, when asset is known.
1378
+ if (null != isKnownCmp && cleaned.startsWith('list_')) {
1379
+ const remainder = canonize(cleaned.substring('list_'.length));
1380
+ if (remainder.length >= 3 && knownWithVersion(remainder, version)) {
1381
+ cleaned = remainder;
1382
+ stripped = true;
1383
+ }
1384
+ }
1275
1385
  // Nothing stripped it: restore the version suffix removed above.
1276
1386
  if (!stripped && cleaned === preVersion.replace(/_v\d+$/, '') && null != version) {
1277
1387
  cleaned = preVersion;
@@ -1525,7 +1635,34 @@ function itemEnvelopeOf(res) {
1525
1635
  const m = 'string' === typeof merge ? merge.match(/^`\.([^.`$]+)`$/) : null;
1526
1636
  return null == m ? null : m[1];
1527
1637
  }
1528
- function envelopeProp(resprops, opname) {
1638
+ // A list over an object reads its records, never the object: its one envelope,
1639
+ // else its one list of records beside other data, else the first of several.
1640
+ function envelopeProp(resprops, opname, example) {
1641
+ const key = singleEnvelope(resprops, opname);
1642
+ return null == key && 'list' === opname ? listedRecords(resprops, example) : key;
1643
+ }
1644
+ function recordLists(resprops) {
1645
+ return (0, struct_1.keysof)(resprops).filter((k) => isRecordList(resprops[k]));
1646
+ }
1647
+ // Lists of one item schema are categories of one record: the one the example
1648
+ // fills is read, as the example is counted. Otherwise the first by name is,
1649
+ // past one holding errors or messages.
1650
+ function listedRecords(resprops, example) {
1651
+ const lists = recordLists(resprops);
1652
+ if (lists.length < 2) {
1653
+ return lists[0] ?? null;
1654
+ }
1655
+ const refs = new Set(lists.map((k) => resprops[k]?.items?.['x-ref']));
1656
+ if (1 === refs.size && null != [...refs][0]) {
1657
+ const filled = lists.filter((k) => Array.isArray(example?.[k]) && 0 < example[k].length);
1658
+ if (1 === filled.length) {
1659
+ return filled[0];
1660
+ }
1661
+ }
1662
+ const status = (k) => isEnvelopeStatusProp(k) || isEnvelopeStatusProp(k.replace(/s$/i, ''));
1663
+ return lists.find((k) => !isPageMetaProp(k) && !status(k)) ?? lists[0];
1664
+ }
1665
+ function singleEnvelope(resprops, opname) {
1529
1666
  const keys = (0, struct_1.keysof)(resprops);
1530
1667
  if (0 === keys.length) {
1531
1668
  return null;
@@ -1624,9 +1761,10 @@ const PAGE_META_PROPS = new Set([
1624
1761
  function isPageMetaProp(name) {
1625
1762
  return PAGE_META_PROPS.has(name.toLowerCase().replace(/[_-]/g, ''));
1626
1763
  }
1627
- // The page metadata a single item may carry too. Paging beside an object makes
1628
- // it a page, such as GitHub's attestations by digest beside their page info.
1629
- const ITEM_META_PROPS = new Set(['meta', 'metadata', 'links']);
1764
+ // The metadata a single item may carry, such as the `info` beside a Disney
1765
+ // character. Paging beside an object makes it a page, such as GitHub's
1766
+ // attestations by digest beside their page info.
1767
+ const ITEM_META_PROPS = new Set(['meta', 'metadata', 'links', 'info']);
1630
1768
  // A list under a metadata name is records, as it is on a page.
1631
1769
  function isItemMetaProp(resprops, name) {
1632
1770
  return ITEM_META_PROPS.has(name.toLowerCase().replace(/[_-]/g, '')) &&
@@ -1729,6 +1867,17 @@ function pageRecords(schema) {
1729
1867
  function answersPage(schema) {
1730
1868
  return null != pageRecords(schema);
1731
1869
  }
1870
+ // An object whose entries are records under keys of the API's choosing, with
1871
+ // no properties of its own, such as FXMacroData's catalogue by indicator slug.
1872
+ function answersRecordMap(schema) {
1873
+ if (null == schema || 'object' !== typeof schema || Array.isArray(schema) ||
1874
+ (null != schema.type && 'object' !== schema.type) ||
1875
+ 0 < (0, struct_1.keysof)(mergedProperties(schema)).length) {
1876
+ return false;
1877
+ }
1878
+ const entry = schema.additionalProperties;
1879
+ return true !== propIsList(entry) && isObjectSchema(entry);
1880
+ }
1732
1881
  // The properties of a schema and its allOf members; the first declaration of
1733
1882
  // a name wins. Read-only, since parsed nodes are shared between references.
1734
1883
  function mergedProperties(schema) {