@voxgig/apidef 8.29.0 → 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 (43) 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 +72 -15
  11. package/dist/guide/heuristic01.js.map +1 -1
  12. package/dist/parse.d.ts +2 -1
  13. package/dist/parse.js +65 -18
  14. package/dist/parse.js.map +1 -1
  15. package/dist/transform/entity.js +3 -3
  16. package/dist/transform/entity.js.map +1 -1
  17. package/dist/transform/field.d.ts +2 -1
  18. package/dist/transform/field.js +10 -1
  19. package/dist/transform/field.js.map +1 -1
  20. package/dist/transform/operation.js +13 -24
  21. package/dist/transform/operation.js.map +1 -1
  22. package/dist/transform/top.js +8 -0
  23. package/dist/transform/top.js.map +1 -1
  24. package/dist/tsconfig.tsbuildinfo +1 -1
  25. package/dist/utility.d.ts +9 -2
  26. package/dist/utility.js +161 -25
  27. package/dist/utility.js.map +1 -1
  28. package/dist/yamlalias.d.ts +3 -0
  29. package/dist/yamlalias.js +269 -0
  30. package/dist/yamlalias.js.map +1 -0
  31. package/package.json +1 -1
  32. package/src/builder/entity/entity.ts +1 -1
  33. package/src/builder/entity/info.ts +1 -1
  34. package/src/guide/graphql01.ts +9 -3
  35. package/src/guide/guide.ts +2 -10
  36. package/src/guide/heuristic01.ts +81 -16
  37. package/src/parse.ts +68 -26
  38. package/src/transform/entity.ts +5 -5
  39. package/src/transform/field.ts +12 -1
  40. package/src/transform/operation.ts +15 -27
  41. package/src/transform/top.ts +9 -0
  42. package/src/utility.ts +186 -25
  43. 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,6 +60,8 @@ 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;
@@ -79,10 +85,6 @@ const sortedKeys = (obj) => Object.keys(obj ?? {}).sort();
79
85
  exports.sortedKeys = sortedKeys;
80
86
  const sortedEntries = (obj) => Object.entries(obj ?? {}).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0);
81
87
  exports.sortedEntries = sortedEntries;
82
- // Pre-compiled regex patterns for formatJsonSrc to avoid recompilation per call.
83
- const RE_JSON_KEY = /"([a-zA-Z_][a-zA-Z_0-9]*)": /g;
84
- const RE_JSON_TRAILING_BRACE = /},/g;
85
- const RE_JSON_COMMENT = /\n(\s*)([a-zA-Z_][a-zA-Z_0-9]*)_COMMENT:\s*"(.*)",/g;
86
88
  const RE_BARE_KEY = /^[A-Za-z_][_A-Za-z0-9]*$/;
87
89
  const isBareKey = (k) => RE_BARE_KEY.test(k);
88
90
  const quoteKey = (k) => (isBareKey(k) ? k : JSON.stringify(k));
@@ -156,11 +158,38 @@ function loadFile(path, what, fs, log) {
156
158
  throw err;
157
159
  }
158
160
  }
161
+ // Unquotes bare keys and breaks the line after `},`, only outside strings, so
162
+ // no string value is rewritten.
159
163
  function formatJsonSrc(jsonsrc) {
160
- return jsonsrc
161
- .replace(RE_JSON_KEY, '$1: ')
162
- .replace(RE_JSON_TRAILING_BRACE, '}\n')
163
- .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;
164
193
  }
165
194
  const IRREGULARS = Object.assign(Object.create(null), {
166
195
  'aircraft': 'aircraft',
@@ -660,6 +689,7 @@ function formatJSONIC(val, opts) {
660
689
  const useColor = opts?.color ?? false;
661
690
  const maxlines = opts?.maxlines ?? Number.MAX_VALUE;
662
691
  const exclude = opts?.exclude ?? [];
692
+ const comments = opts?.comments ?? true;
663
693
  // ANSI color codes
664
694
  const colors = {
665
695
  reset: '\x1b[0m',
@@ -677,13 +707,7 @@ function formatJSONIC(val, opts) {
677
707
  return c('null', 'null');
678
708
  const t = typeof v;
679
709
  switch (t) {
680
- case 'string': return c('string', !v.includes('\n') ? JSON.stringify(v) :
681
- '`' + JSON.stringify(v)
682
- .substring(1)
683
- .replace(/\\n/g, '\n')
684
- .replace(/\\"/g, '"')
685
- .replace(/`/g, '\\`')
686
- .replace(/"$/, '`'));
710
+ case 'string': return c('string', !v.includes('\n') ? JSON.stringify(v) : backtickString(v));
687
711
  case 'number': return c('number', Number.isFinite(v) ? String(v) : 'null');
688
712
  case 'boolean': return c('boolean', v ? 'true' : 'false');
689
713
  case 'bigint': return c('string', JSON.stringify(v.toString()));
@@ -694,7 +718,7 @@ function formatJSONIC(val, opts) {
694
718
  default: return JSON.stringify(v);
695
719
  }
696
720
  };
697
- const renderComment = (c) => {
721
+ const commentText = (c) => {
698
722
  if (c == null)
699
723
  return null;
700
724
  if (Array.isArray(c) && c.every(x => typeof x === 'string'))
@@ -708,9 +732,33 @@ function formatJSONIC(val, opts) {
708
732
  return String(c);
709
733
  }
710
734
  };
711
- 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 + '`';
712
760
  }
713
- 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) {
714
762
  const space = ' ';
715
763
  const seen = new WeakSet();
716
764
  let stack = new Array(32);
@@ -761,7 +809,7 @@ function renderJSONIC(val, hsepd, showd, useColor, maxlines, exclude, c, renderP
761
809
  flat = undefined;
762
810
  }
763
811
  if (undefined !== flat) {
764
- 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);
765
813
  }
766
814
  }
767
815
  // decircular did not resolve it, so render a marker rather than
@@ -801,7 +849,7 @@ function renderJSONIC(val, hsepd, showd, useColor, maxlines, exclude, c, renderP
801
849
  if (v instanceof Error) {
802
850
  keys.unshift('name', 'message', 'stack');
803
851
  }
804
- const printableKeys = keys.filter(k => !k.endsWith('_COMMENT') &&
852
+ const printableKeys = keys.filter(k => !(comments && k.endsWith('_COMMENT')) &&
805
853
  (showd || !k.endsWith('$')));
806
854
  if (printableKeys.length === 0) {
807
855
  lines.push(`${linePrefix}${c('bracket', '{')}${commentSuffix}`);
@@ -970,6 +1018,42 @@ function foldName(name) {
970
1018
  return String((0, jostraca_1.camelify)(name)).toLowerCase();
971
1019
  }
972
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
+ }
973
1057
  function prefixLeadingDigit(s) {
974
1058
  if (null == s || '' === s)
975
1059
  return s;
@@ -1090,7 +1174,7 @@ function ensureMinEntityName(name, existing) {
1090
1174
  }
1091
1175
  padded = truncated || parts[0].substring(0, MAX_ENTITY_NAME_LEN);
1092
1176
  }
1093
- padded = prefixLeadingDigit(padded);
1177
+ padded = guardPrototypeName(prefixLeadingDigit(padded));
1094
1178
  if (padded.length < MIN_ENTITY_NAME_LEN) {
1095
1179
  const padding = 'nt'.substring(0, MIN_ENTITY_NAME_LEN - padded.length);
1096
1180
  padded = padded + padding;
@@ -1112,12 +1196,29 @@ function ensureMinEntityName(name, existing) {
1112
1196
  }
1113
1197
  // Unconditional suffixes: framework noise, always stripped.
1114
1198
  const CMP_SUFFIXES = ['_rest_controller', '_controller', '_response', '_request'];
1115
- 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
+ ];
1116
1205
  const CMP_RESULT_VERBS = [
1117
1206
  'list', 'create', 'show', 'update', 'delete', 'remove',
1118
- 'get', 'edit', 'resolve', 'rotate', 'search',
1207
+ 'get', 'edit', 'resolve', 'rotate', 'search', 'add',
1119
1208
  ];
1120
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
+ }
1121
1222
  // A guide node (entity, path or op) is active unless guide.aontu explicitly
1122
1223
  // says otherwise. `active` has always been declared in the guide model and
1123
1224
  // documented as the escape hatch for a misclassified entity; this is the
@@ -1240,7 +1341,7 @@ function cleanComponentName(name, isKnownCmp) {
1240
1341
  // (CallRouteAllowedCaller), so the namespace is tried singular too.
1241
1342
  const cands = after
1242
1343
  ? [canonize(depluralize(after)), canonize(depluralize(before + '_' + after)),
1243
- canonize(depluralize(before) + '_' + depluralize(after))]
1344
+ canonize(depluralize(before) + '_' + depluralize(after)), canonize(depluralize(before))]
1244
1345
  : [canonize(depluralize(before))];
1245
1346
  for (const cand of cands) {
1246
1347
  if (cand.length >= 3 && knownWithVersion(cand, version)) {
@@ -1273,6 +1374,14 @@ function cleanComponentName(name, isKnownCmp) {
1273
1374
  }
1274
1375
  }
1275
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
+ }
1276
1385
  // Nothing stripped it: restore the version suffix removed above.
1277
1386
  if (!stripped && cleaned === preVersion.replace(/_v\d+$/, '') && null != version) {
1278
1387
  cleaned = preVersion;
@@ -1526,7 +1635,34 @@ function itemEnvelopeOf(res) {
1526
1635
  const m = 'string' === typeof merge ? merge.match(/^`\.([^.`$]+)`$/) : null;
1527
1636
  return null == m ? null : m[1];
1528
1637
  }
1529
- 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) {
1530
1666
  const keys = (0, struct_1.keysof)(resprops);
1531
1667
  if (0 === keys.length) {
1532
1668
  return null;