@voxgig/apidef 8.32.4 → 8.33.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.
@@ -1,18 +1,66 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.VERSION_TAG_RE = exports.READ_VERBS = exports.LITERAL_LAST_SHAPES = void 0;
4
+ exports.methodOf = methodOf;
5
+ exports.singletonPartOf = singletonPartOf;
6
+ exports.readOfItem = readOfItem;
7
+ exports.keyOwner = keyOwner;
3
8
  exports.answeredRefs = answeredRefs;
9
+ exports.collectionOwner = collectionOwner;
10
+ exports.listedRefs = listedRefs;
11
+ exports.pathAnswersPage = pathAnswersPage;
12
+ exports.pathAnswersRecordMap = pathAnswersRecordMap;
13
+ exports.randomRoute = randomRoute;
14
+ exports.selectorRoute = selectorRoute;
15
+ exports.namedRecord = namedRecord;
16
+ exports.verbAfterNoun = verbAfterNoun;
17
+ exports.warnListBody = warnListBody;
4
18
  exports.arrayBodyField = arrayBodyField;
19
+ exports.commandBodyName = commandBodyName;
20
+ exports.commandTwin = commandTwin;
21
+ exports.itemAbove = itemAbove;
22
+ exports.pathQualified = pathQualified;
23
+ exports.readsRecord = readsRecord;
24
+ exports.recordOf = recordOf;
25
+ exports.responsePayload = responsePayload;
26
+ exports.cmpSchemaOf = cmpSchemaOf;
27
+ exports.tagHolds = tagHolds;
28
+ exports.wrapsLiteral = wrapsLiteral;
29
+ exports.isListResponse = isListResponse;
5
30
  exports.distinctRecord = distinctRecord;
6
31
  exports.distinctShare = distinctShare;
7
32
  exports.heuristic01 = heuristic01;
33
+ exports.keyParamOf = keyParamOf;
34
+ exports.matchEntityPath = matchEntityPath;
35
+ exports.pathNoun = pathNoun;
8
36
  exports.namingRef = namingRef;
9
37
  exports.namingSchemas = namingSchemas;
38
+ exports.pathRecordRef = pathRecordRef;
10
39
  exports.pathResource = pathResource;
11
40
  exports.sharedRoutes = sharedRoutes;
12
41
  exports.entityParamNames = entityParamNames;
13
42
  exports.isNameParam = isNameParam;
14
43
  exports.isVerb = isVerb;
15
44
  exports.verbParts = verbParts;
45
+ exports.verbOfItem = verbOfItem;
46
+ exports.shortLiteral = shortLiteral;
47
+ exports.normalizeParams = normalizeParams;
48
+ exports.cleanCmp = cleanCmp;
49
+ exports.commandSource = commandSource;
50
+ exports.relationshipOf = relationshipOf;
51
+ exports.singletonOf = singletonOf;
52
+ exports.subWriteOfItem = subWriteOfItem;
53
+ exports.variantOfCollection = variantOfCollection;
54
+ exports.keyNounOf = keyNounOf;
55
+ exports.asyncResultOf = asyncResultOf;
56
+ exports.nounless = nounless;
57
+ exports.genericCmp = genericCmp;
58
+ exports.actionWord = actionWord;
59
+ exports.keyRunBefore = keyRunBefore;
60
+ exports.isVerbWord = isVerbWord;
61
+ exports.keyedBy = keyedBy;
62
+ exports.postReadVerb = postReadVerb;
63
+ exports.componentReadOwner = componentReadOwner;
16
64
  const ordu_1 = require("ordu");
17
65
  const jostraca_1 = require("jostraca");
18
66
  const struct_1 = require("@voxgig/struct");
@@ -42,9 +90,49 @@ const METHOD_IDOP = {
42
90
  PATCH: 'patch',
43
91
  HEAD: 'head',
44
92
  OPTIONS: 'OPTIONS',
93
+ TRACE: 'TRACE',
45
94
  };
46
95
  // Tried in order: the first shape a path matches decides how its entity is named.
47
96
  const ENTITY_PATH_SHAPES = ['t/p/t/', 't/p/', 'p/t/', 't/', 't/p/p'];
97
+ // A trailing literal that is one of these is a verb on the noun before it.
98
+ const VERB_WORDS = new Set([
99
+ 'send', 'resend', 'get', 'fetch', 'list', 'create', 'add', 'update', 'edit', 'delete',
100
+ 'remove', 'search', 'find', 'query', 'lookup', 'check', 'validate', 'verify', 'submit',
101
+ 'cancel', 'approve', 'reject', 'publish', 'unpublish', 'archive', 'unarchive', 'restore',
102
+ 'run', 'execute', 'start', 'stop', 'pause', 'resume', 'retry', 'refresh', 'reset', 'sync',
103
+ 'import', 'export', 'upload', 'download', 'login', 'logout', 'register', 'subscribe',
104
+ 'unsubscribe', 'activate', 'deactivate', 'enable', 'disable', 'lock', 'unlock', 'move',
105
+ 'copy', 'rename', 'merge', 'split', 'assign', 'unassign', 'attach', 'detach', 'invite',
106
+ 'accept', 'decline', 'notify', 'ping', 'count', 'exists', 'preview', 'render', 'generate',
107
+ 'calculate', 'compute', 'convert', 'process', 'trigger', 'dispatch', 'apply', 'revoke',
108
+ 'grant', 'save', 'lock', 'test', 'inject', 'analyze', 'autocomplete', 'bypass', 'contains',
109
+ 'finish', 'reorder', 'shuffle',
110
+ ]);
111
+ // A literal that names an identifier keys a resource rather than naming one.
112
+ const ID_LITERAL_RE = /(^|_)ids?$/;
113
+ // A component named for one of these, alone or as the bare member of a family
114
+ // of results, names no entity.
115
+ const GENERIC_CMP_WORDS = ['default', 'empty', 'ok', 'response', 'result', 'success', 'void'];
116
+ // A version tag groups routes by release, not by resource.
117
+ const VERSION_TAG_RE = /^v\d+([a-z]+\d*)?$/;
118
+ exports.VERSION_TAG_RE = VERSION_TAG_RE;
119
+ // Literals that name no resource of their own.
120
+ const GENERIC_WORDS = ['history', 'list', 'result'];
121
+ // A key that identifies a resource rather than naming an item.
122
+ const ID_KEY_RE = /_(id|key|uuid)$/;
123
+ // The words that open an operationId's tail after its object.
124
+ const OPID_TAIL_WORDS = ['via', 'by', 'with'];
125
+ // A literal opening with one of these reads, whatever the method.
126
+ const READ_VERBS = ['list', 'search', 'get', 'find', 'query'];
127
+ exports.READ_VERBS = READ_VERBS;
128
+ // The shapes whose naming literal ends the path.
129
+ const LITERAL_LAST_SHAPES = ['t/p/t/', 't/', 'p/t/'];
130
+ exports.LITERAL_LAST_SHAPES = LITERAL_LAST_SHAPES;
131
+ // Stripped from a command's body component to name the command.
132
+ const COMMAND_SUFFIXES = [
133
+ '_request', '_input', '_payload', '_body', '_params', '_parameters', '_command', '_dto',
134
+ ];
135
+ const SCALAR_TYPES = new Set(['string', 'number', 'integer', 'boolean']);
48
136
  // The matched part that gives each shape its name.
49
137
  const PATH_NAME_INDEX = {
50
138
  't/p/t/': 2, 't/p/': 0, 'p/t/': 1, 't/': 0, 't/p/p': 0,
@@ -58,6 +146,7 @@ const METHOD_CONSIDER_ORDER = {
58
146
  'DELETE': 500,
59
147
  'HEAD': 600,
60
148
  'OPTIONS': 700,
149
+ 'TRACE': 800,
61
150
  };
62
151
  async function heuristic01(ctx) {
63
152
  const analysis = new ordu_1.Ordu({ select: { sort: true } }).add([
@@ -97,10 +186,12 @@ async function heuristic01(ctx) {
97
186
  const guide = result.data.guide;
98
187
  guide.metrics.count.entity -= (0, entity_1.mergeCollectionPaths)(guide, ctx.log, (pathStr, methods, collection) => pathRecordRef(ctx.def, pathStr, methods, collection), (sharePath, shareMethods, itemPath, itemMethods) => distinctShare(ctx.def, sharePath, shareMethods, itemPath, itemMethods)).length;
99
188
  guide.metrics.count.entity -= (0, entity_1.mergeFoldedEntities)(guide, ctx.log).length;
189
+ guide.metrics.count.entity -= (0, entity_1.mergeCommandEntities)(guide, ctx.log).length;
100
190
  // After the merge, which can move a path onto another entity.
101
191
  for (const entity of Object.values(guide.entity)) {
102
192
  nameEntityParams(entity);
103
193
  }
194
+ (0, entity_1.separateSharedPoints)(guide, ctx.def, ctx.log);
104
195
  const metrics = guide.metrics;
105
196
  const entities = Object.values(guide.entity);
106
197
  const entityCount = entities.length;
@@ -151,6 +242,8 @@ function Prepare(spec) {
151
242
  def: spec.ctx.def,
152
243
  guide,
153
244
  work: {
245
+ cmpNamespace: (0, utility_1.cmpNamespace)(spec.ctx.def),
246
+ wordBreaks: (0, utility_1.wordBreaks)(spec.ctx.def),
154
247
  pathmap: named(),
155
248
  entmap: named(),
156
249
  envelope: named(),
@@ -205,7 +298,7 @@ function MeasureMethod(spec) {
205
298
  metrics.count.tag++;
206
299
  metrics.found.tag[tag] = {
207
300
  name: tag,
208
- canon: (0, utility_2.canonize)(tag),
301
+ canon: (0, utility_1.literalName)(tag),
209
302
  };
210
303
  }
211
304
  }
@@ -270,7 +363,8 @@ function MeasureEnvelope(spec) {
270
363
  const work = spec.data.work;
271
364
  const mdesc = spec.node.val;
272
365
  const opname = methodOpname(mdesc, matchEntityPath(work.pathmap[mdesc.path].parts), []);
273
- const unwrapref = getResponseSchema(successResponse(mdesc.responses))?.['x-ref'];
366
+ const unwrapref = null != singletonOf(spec.data, mdesc.path) ? null :
367
+ getResponseSchema(successResponse(mdesc.responses))?.['x-ref'];
274
368
  for (const schema of successSchemas(mdesc.responses)) {
275
369
  const xref = schema['x-ref'];
276
370
  if (null != xref) {
@@ -386,11 +480,15 @@ function selectAllMethods(_source, spec) {
386
480
  });
387
481
  }
388
482
  }
483
+ // Parents resolve before their children whatever the keys are called, so
484
+ // the paths compare with their parameters normalised first.
389
485
  caught.methods.sort((a, b) => {
390
- if (a.path < b.path) {
486
+ const an = normalizeParams(a.path);
487
+ const bn = normalizeParams(b.path);
488
+ if (an < bn || (an === bn && a.path < b.path)) {
391
489
  return -1;
392
490
  }
393
- else if (a.path > b.path) {
491
+ else if (an > bn || (an === bn && a.path > b.path)) {
394
492
  return 1;
395
493
  }
396
494
  else if (METHOD_CONSIDER_ORDER[a.method] < METHOD_CONSIDER_ORDER[b.method]) {
@@ -418,7 +516,7 @@ function MeasureTagItems(spec) {
418
516
  const items = work.tagItems[cmp] = work.tagItems[cmp] ?? {};
419
517
  items[collection] = items[collection] ?? {};
420
518
  items[collection][parts.filter(isParam)
421
- .map((part) => (0, utility_2.canonizeParam)(part.slice(1, -1))).sort().join(',')] = true;
519
+ .map((part) => (0, utility_2.canonizeParam)((0, utility_1.paramOf)(part))).sort().join(',')] = true;
422
520
  }
423
521
  }
424
522
  // The name each route takes by its path and component, and its stored form,
@@ -436,7 +534,7 @@ function MeasureClaims(spec) {
436
534
  const ment = { ...(mdesc.MethodEntity ?? makeMethodEntityDesc({})) };
437
535
  const name = pathEntityName(data, { ...mdesc, MethodEntity: ment }, parts, matchEntityPath(parts), work.claimowner, []);
438
536
  if (null != name) {
439
- const claim = (0, utility_2.resplitFromCmp)(name, ment.cmp, []);
537
+ const claim = (0, utility_2.resplitFromCmp)(name, ment.cmp, [], work.wordBreaks);
440
538
  const stored = (0, utility_2.ensureMinEntityName)(claim, {});
441
539
  for (const key of [claim, stored]) {
442
540
  const claims = work.claims[key] = work.claims[key] ?? {};
@@ -482,9 +580,11 @@ function ResolveEntityComponent(spec) {
482
580
  // Guarded wrapper-suffix stripping folds e.g. BeneficiaryPageResponse
483
581
  // into beneficiary — but only when the remainder is itself a schema
484
582
  // measured by MeasureRef (keys are canonizeCmpName, pre-clean).
485
- xref.cmp = (0, utility_2.cleanComponentName)(xref.cmp, (n) => null != metrics.count.origcmprefs[n]);
583
+ xref.cmp = cleanCmp(spec.data, xref.cmp);
486
584
  return xref;
487
- });
585
+ })
586
+ .flatMap((xref) => unwrapCmp(metrics, xref, why_cmp))
587
+ .filter((xref) => !genericCmp(xref.cmp, metrics.count.origcmprefs, cmpSchemaOf(spec.data.def, xref.origcmp)));
488
588
  let goodxrefs = cleanxrefs
489
589
  .filter(xref => {
490
590
  if (cleanxrefs.length <= 1
@@ -600,7 +700,8 @@ function ResolveEntityName(spec) {
600
700
  const cmp = ment.cmp;
601
701
  let entname = shaped;
602
702
  if (null != owner && (null == shaped ||
603
- (0, utility_2.resplitFromCmp)(owner.entname, cmp, []) !== (0, utility_2.resplitFromCmp)(shaped, cmp, []))) {
703
+ (0, utility_2.resplitFromCmp)(owner.entname, cmp, [], work.wordBreaks) !==
704
+ (0, utility_2.resplitFromCmp)(shaped, cmp, [], work.wordBreaks))) {
604
705
  ment.verb_on_parent = verb_on_parent;
605
706
  ment.same_path = owner.entname;
606
707
  why_path.push('same-path-tag=' + owner.entname + '/' + owner.method);
@@ -617,7 +718,7 @@ function ResolveEntityName(spec) {
617
718
  work.entity.count.unresolved++;
618
719
  entname = 'entity' + work.entity.count.unresolved;
619
720
  }
620
- entname = (0, utility_2.resplitFromCmp)(entname, cmp, why_path);
721
+ entname = (0, utility_2.resplitFromCmp)(entname, cmp, why_path, work.wordBreaks);
621
722
  if (null != item) {
622
723
  why_path.push(item.why + '=' + item.name);
623
724
  entname = item.name;
@@ -664,14 +765,16 @@ function ResolveEntityName(spec) {
664
765
  }
665
766
  // A method named only from its tag, as a DELETE answering 204 is, joins the
666
767
  // entity a read of the same path resolved to when that read carries the same
667
- // tag: the tag then groups the path's routes and names no other resource. A
668
- // tag the read does not carry, Admin on the delete of a team, keeps its name.
768
+ // tag: the tag then groups the path's routes and names no other resource.
669
769
  function samePathOwner(mdesc, work) {
670
770
  const ment = mdesc.MethodEntity;
671
771
  const owner = work.pathowner[mdesc.path];
672
- if (null == owner || 'tag' !== ment?.ref) {
772
+ if (null == owner) {
673
773
  return null;
674
774
  }
775
+ if ('tag' !== ment?.ref) {
776
+ return null == ment?.cmp ? componentReadOwner(mdesc.path, owner, work.methods.map((m) => ({ path: m.path, method: m.method, cmp: m.MethodEntity }))) : null;
777
+ }
675
778
  const tags = mdesc.tags ?? [];
676
779
  for (const method of ['GET', 'QUERY', 'PUT', 'PATCH']) {
677
780
  const entname = owner[method];
@@ -682,6 +785,25 @@ function samePathOwner(mdesc, work) {
682
785
  }
683
786
  return null;
684
787
  }
788
+ // A method that answers no component of its own joins the entity a read of
789
+ // its path takes from the component that read answers.
790
+ function componentReadOwner(pathStr, owner, routes) {
791
+ for (const method of ['GET', 'QUERY']) {
792
+ const entname = owner[method];
793
+ const read = routes.find((r) => r.path === pathStr && r.method === method)?.cmp;
794
+ if (null != entname && null != read && 'tag' !== read.ref && entname === read.cmp) {
795
+ return { entname, method };
796
+ }
797
+ }
798
+ return null;
799
+ }
800
+ // A component's name cleaned of its affixes and the definition's namespace;
801
+ // a measured component counts as known with or without the namespace.
802
+ function cleanCmp(data, name) {
803
+ const refs = data.guide.metrics.count.origcmprefs;
804
+ const ns = data.work.cmpNamespace ?? null;
805
+ return (0, utility_2.cleanComponentName)(name, (n) => null != refs[n] || (null != ns && null != refs[ns + '_' + n]), ns);
806
+ }
685
807
  // A method named only from its tag, on a verb route beneath an item, joins the
686
808
  // entity that owns the item's collection, so its verb becomes that entity's
687
809
  // action. The verb is a singular word or a pair of them, behind an optional
@@ -706,8 +828,126 @@ function collectionOwner(mdesc, parts, work) {
706
828
  return owner === (0, utility_2.canonize)(parts[first - 1]) ? owner : null;
707
829
  }
708
830
  // The name a method's path shape and component give it, or null where neither
709
- // does. A verb on a parent takes the name pathowner holds for the parent.
831
+ // does; a read that views an item joins it, and a POST that reads or commands
832
+ // is named for what it reads or sends.
710
833
  function pathEntityName(data, mdesc, parts, pm, pathowner, why) {
834
+ const ment = mdesc.MethodEntity;
835
+ const joined = methodOf(mdesc.path) ?? singletonPartOf(data, parts, pathowner) ??
836
+ readOfItem(data, mdesc, parts, pathowner);
837
+ if (null != joined) {
838
+ ment.verb_on_parent = parts[parts.length - 1] ?? joined.action;
839
+ ment.view_on_parent = 'method-name' !== joined.why && utility_2.READ_METHODS.includes(mdesc.method);
840
+ ment.view_action = joined.action;
841
+ why.push(joined.why + '=' + joined.parent);
842
+ return joined.parent;
843
+ }
844
+ const link = relationshipOf(data, parts, pathowner);
845
+ if (null != link) {
846
+ const ment = mdesc.MethodEntity;
847
+ ment.verb_on_parent = parts[parts.length - 1];
848
+ ment.view_on_parent = utility_2.READ_METHODS.includes(mdesc.method);
849
+ ment.view_action = link.action;
850
+ why.push('relationship=' + link.parent);
851
+ return link.parent;
852
+ }
853
+ const single = singletonOf(data, mdesc.path);
854
+ if (null != single) {
855
+ const name = cleanCmp(data, (0, utility_2.canonizeCmpName)((0, utility_2.cmpRefName)(single)));
856
+ mdesc.MethodEntity.singleton = true;
857
+ why.push('singleton=' + name);
858
+ return name;
859
+ }
860
+ const verb = verbOfItem(data, mdesc, parts, pathowner);
861
+ if (null != verb) {
862
+ const ment = mdesc.MethodEntity;
863
+ ment.verb_on_parent = parts[parts.length - 1];
864
+ ment.view_on_parent = utility_2.READ_METHODS.includes(mdesc.method);
865
+ ment.view_action = verb.action;
866
+ why.push('verb-of-item=' + verb.parent);
867
+ return verb.parent;
868
+ }
869
+ const view = viewOfItem(data, mdesc, parts, pathowner);
870
+ if (null != view) {
871
+ const ment = mdesc.MethodEntity;
872
+ ment.verb_on_parent = parts[parts.length - 1];
873
+ ment.view_on_parent = true;
874
+ ment.view_action = view.action;
875
+ why.push('view-of-item=' + view.parent);
876
+ return view.parent;
877
+ }
878
+ const write = subWriteOfItem(data, mdesc, parts, pathowner);
879
+ if (null != write) {
880
+ const ment = mdesc.MethodEntity;
881
+ ment.verb_on_parent = parts[parts.length - 1];
882
+ ment.view_action = write.action;
883
+ why.push('sub-write=' + write.parent);
884
+ return write.parent;
885
+ }
886
+ const variant = variantOfCollection(data, mdesc, parts, pathowner);
887
+ if (null != variant) {
888
+ const ment = mdesc.MethodEntity;
889
+ ment.verb_on_parent = parts[parts.length - 1];
890
+ ment.view_action = variant.action;
891
+ why.push('variant=' + variant.parent);
892
+ return variant.parent;
893
+ }
894
+ const keyed = keyNounOf(data, mdesc, parts);
895
+ if (null != keyed) {
896
+ const ment = mdesc.MethodEntity;
897
+ ment.verb_on_parent = parts[parts.length - 1];
898
+ ment.view_on_parent = utility_2.READ_METHODS.includes(mdesc.method);
899
+ ment.view_action = keyed.action;
900
+ why.push('key-noun=' + keyed.parent);
901
+ return keyed.parent;
902
+ }
903
+ const queued = asyncResultOf(data, mdesc, parts);
904
+ if (null != queued) {
905
+ const ment = mdesc.MethodEntity;
906
+ ment.verb_on_parent = parts[parts.length - 1];
907
+ ment.view_action = queued.action;
908
+ why.push('async-result=' + queued.parent);
909
+ return queued.parent;
910
+ }
911
+ // A short literal names nothing: the component the route answers, else
912
+ // the route's key, else what a route with no literal is named by, else the
913
+ // padded literal.
914
+ if (null != pm && shortLiteral(pm)) {
915
+ why.push('short-literal');
916
+ const answered = answeredCmp(mdesc);
917
+ if (null != answered) {
918
+ why.push('answers=' + answered);
919
+ return answered;
920
+ }
921
+ const key = keyParamOf(data.def, mdesc.path, data.work);
922
+ const noun = null == key || /version/i.test(key) ? '' : (0, utility_1.paramNoun)(key);
923
+ if (3 <= noun.length) {
924
+ why.push('infer-key=' + key);
925
+ return noun;
926
+ }
927
+ return inferEntityName(mdesc, parts.filter(isParam), why) ??
928
+ shapeEntityName(data, mdesc, parts, pm, pathowner, why);
929
+ }
930
+ // A read whose literal is a lone verb or a generic word is named for what
931
+ // it answers; a write stays a command.
932
+ if (null != pm && utility_2.READ_METHODS.includes(mdesc.method) && nounless(pm, parts)) {
933
+ const answered = answeredCmp(mdesc);
934
+ if (null != answered) {
935
+ why.push('nounless;answers=' + answered);
936
+ return answered;
937
+ }
938
+ }
939
+ const shaped = shapeEntityName(data, mdesc, parts, pm, pathowner, why);
940
+ if (null == pm || null == shaped) {
941
+ return shaped;
942
+ }
943
+ const read = postRead(mdesc, pm, parts, why);
944
+ if (null != read) {
945
+ return '' === read ? shaped : read;
946
+ }
947
+ return commandName(data, mdesc, pm, parts, why) ?? commandTwin(data, mdesc, pm, parts, why) ?? shaped;
948
+ }
949
+ // A verb on a parent takes the name pathowner holds for the parent.
950
+ function shapeEntityName(data, mdesc, parts, pm, pathowner, why) {
711
951
  if ('t/p/t/' === pm?.expr) {
712
952
  return entityPathMatch_tpte(data, pm, mdesc, pathowner, why);
713
953
  }
@@ -715,7 +955,7 @@ function pathEntityName(data, mdesc, parts, pm, pathowner, why) {
715
955
  return entityPathMatch_tpe(data, pm, mdesc, why);
716
956
  }
717
957
  else if ('p/t/' === pm?.expr) {
718
- return entityPathMatch_pte(data, pm, mdesc, why);
958
+ return entityPathMatch_pte(data, pm, mdesc, pathowner, why);
719
959
  }
720
960
  else if ('t/' === pm?.expr) {
721
961
  return entityPathMatch_te(data, pm, mdesc, why);
@@ -730,7 +970,7 @@ function pathEntityName(data, mdesc, parts, pm, pathowner, why) {
730
970
  function markRecordOwner(recordowner, pathStr, parts, ment, entname) {
731
971
  const last = parts.filter((part) => !isParam(part)).pop();
732
972
  if (null == recordowner[pathStr] && isSchemaRef(ment.ref) &&
733
- entname === ment.cmp && null != last && entname === (0, utility_2.canonize)(last)) {
973
+ entname === ment.cmp && null != last && entname === (0, utility_1.literalName)(last)) {
734
974
  recordowner[pathStr] = entname;
735
975
  }
736
976
  }
@@ -757,10 +997,9 @@ function RenameParams(spec) {
757
997
  const parts = verbParts(pathdesc.parts);
758
998
  const applySnakeCaseRename = () => {
759
999
  for (const part of parts) {
760
- const m = part.match(/^\{(.+)\}$/);
761
- if (!m)
1000
+ if (!isParam(part))
762
1001
  continue;
763
- const placeholder = m[1];
1002
+ const placeholder = (0, utility_1.paramOf)(part);
764
1003
  const snake = (0, utility_2.depluralize)((0, jostraca_2.snakify)((0, utility_2.normalizeFieldName)(placeholder)));
765
1004
  if (snake !== placeholder && paramRenameCapture.rename[placeholder] === undefined) {
766
1005
  paramRenameCapture.why[placeholder] = (paramRenameCapture.why[placeholder] ?? []);
@@ -786,15 +1025,15 @@ function RenameParams(spec) {
786
1025
  for (let partI = 0; partI < parts.length; partI++) {
787
1026
  let partStr = parts[partI];
788
1027
  if (isParam(partStr)) {
789
- origParams.push(partStr.replace(/[\}\{\*]/g, ''));
1028
+ origParams.push((0, utility_1.paramOf)(partStr).replace(/\*/g, ''));
790
1029
  const why = [];
791
- const oldParam = partStr.substring(1, partStr.length - 1);
1030
+ const oldParam = (0, utility_1.paramOf)(partStr);
792
1031
  paramRenameCapture.why[oldParam] = (paramRenameCapture.why[oldParam] ?? []);
793
1032
  const lastPart = partI === parts.length - 1;
794
1033
  const secondLastPart = partI === parts.length - 2;
795
1034
  const notLastPart = partI < parts.length - 1;
796
1035
  const hasParent = 0 < partI && !isParam(parts[partI - 1]);
797
- const parentName = hasParent ? (0, utility_2.canonize)(parts[partI - 1]) : null;
1036
+ const parentName = hasParent ? (0, utility_1.literalName)(parts[partI - 1]) : null;
798
1037
  const not_exact_id = 'id' !== oldParam;
799
1038
  const probably_an_id = oldParam.endsWith('id')
800
1039
  || oldParam.endsWith('Id')
@@ -856,7 +1095,8 @@ function RenameParams(spec) {
856
1095
  && !filtered
857
1096
  && (!hasParent
858
1097
  || (parentName === entdesc.name
859
- || entdesc.name.endsWith('_' + parentName)))) {
1098
+ || entdesc.name.endsWith('_' + parentName)
1099
+ || verbAfterNoun(parts, partI, entdesc.name)))) {
860
1100
  updateParamRename(ctx, data, pathStr, methodName, paramRenameCapture, oldParam, 'id', 'end-id;' + methodName + ';parent=' + hasParent + '/' + parentName);
861
1101
  why.push('end-id');
862
1102
  }
@@ -871,7 +1111,7 @@ function RenameParams(spec) {
871
1111
  why.push('second-last');
872
1112
  if ('id' !== oldParam
873
1113
  // && fixEntName(partStr) === entdesc.name
874
- && (0, utility_2.canonize)(partStr) === entdesc.name) {
1114
+ && (0, utility_1.literalName)(partStr) === entdesc.name) {
875
1115
  updateParamRename(ctx, data, pathStr, methodName, paramRenameCapture, oldParam, 'id', 'end-action');
876
1116
  why.push('end-action');
877
1117
  updateAction(methodName, oldParam, parts[partI + 1], entdesc, pathDesc, 'end-action');
@@ -937,21 +1177,26 @@ function FindActions(spec) {
937
1177
  }
938
1178
  const parts = verbParts(spec.data.work.pathmap[pathStr].parts);
939
1179
  const fourthLastPart = parts[parts.length - 4];
940
- const fourthLastPartCanon = (0, utility_2.canonize)(fourthLastPart);
1180
+ const fourthLastPartCanon = (0, utility_1.literalName)(fourthLastPart);
941
1181
  const thirdLastPart = parts[parts.length - 3];
942
- const thirdLastPartCanon = (0, utility_2.canonize)(thirdLastPart);
1182
+ const thirdLastPartCanon = (0, utility_1.literalName)(thirdLastPart);
943
1183
  const secondLastPart = parts[parts.length - 2];
944
- const secondLastPartCanon = (0, utility_2.canonize)(secondLastPart);
1184
+ const secondLastPartCanon = (0, utility_1.literalName)(secondLastPart);
945
1185
  const lastPart = parts[parts.length - 1];
946
- const lastPartCanon = (0, utility_2.canonize)(lastPart);
1186
+ const lastPartCanon = actionWord(lastPart);
947
1187
  const cmp = ment.cmp;
948
1188
  if (null != ment.verb_on_parent) {
949
- if (isVerb(lastPart)) {
950
- pathdesc.action[lastPartCanon] = pathdesc.action[lastPartCanon] ?? {
1189
+ if (isVerb(lastPart) || null != ment.view_action) {
1190
+ const name = ment.view_action ?? lastPartCanon;
1191
+ pathdesc.action[name] = pathdesc.action[name] ?? {
951
1192
  why_action: ['ent', entdesc.name, 'verb-on-parent', lastPart, methodName],
952
1193
  };
953
1194
  }
954
1195
  }
1196
+ // A route its record names ends in its collection's literal, not an action.
1197
+ else if (true === ment.record_named) {
1198
+ // The collection_action below still applies.
1199
+ }
955
1200
  // /api/foo/bar where foo is the entity and bar is the action, no id param
956
1201
  else if (secondLastPartCanon === cmp
957
1202
  || secondLastPartCanon === ment.origcmp
@@ -979,6 +1224,22 @@ function FindActions(spec) {
979
1224
  secondLastPartCanon + '_' + lastPartCanon;
980
1225
  updateAction(methodName, oldActionName, actionName, entdesc, pathdesc, 'ent-param-3rd-last');
981
1226
  }
1227
+ // /api/foo/{parent}/{param}/action
1228
+ else if (isParam(thirdLastPart) && isParam(secondLastPart) && isVerb(lastPart)) {
1229
+ updateAction(methodName, lastPart, lastPartCanon, entdesc, pathdesc, 'ent-keys-2nd-last');
1230
+ }
1231
+ }
1232
+ // /api/foo/{a}/{b}/{c}/action
1233
+ else if (isVerb(lastPart) && keyRunBefore(parts, (part) => {
1234
+ const canon = (0, utility_1.literalName)(part);
1235
+ return canon === cmp || canon === ment.origcmp || canon === entname;
1236
+ })) {
1237
+ updateAction(methodName, lastPart, lastPartCanon, entdesc, pathdesc, 'ent-keys-before-last');
1238
+ }
1239
+ if (null != ment.collection_action) {
1240
+ pathdesc.action[ment.collection_action] = pathdesc.action[ment.collection_action] ?? {
1241
+ why_action: ['ent', entdesc.name, 'collection', ment.collection_action, methodName],
1242
+ };
982
1243
  }
983
1244
  (0, utility_2.debugpath)(pathStr, methodName, 'FIND-ACTIONS', cmp, parts, pathdesc.action, pathdesc.why_action);
984
1245
  // return pathdesc.action
@@ -1000,8 +1261,17 @@ function ResolveOperation(spec) {
1000
1261
  return;
1001
1262
  }
1002
1263
  if ('load' === standard_opname) {
1003
- const islist = isListResponse(mdesc, ment.pm, pathStr, why_op, work.listed);
1264
+ const islist = true !== ment.view_on_parent && true !== ment.singleton &&
1265
+ isListResponse(mdesc, ment.pm, pathStr, why_op, work.listed);
1004
1266
  opname = islist ? 'list' : opname;
1267
+ if (true === ment.view_on_parent) {
1268
+ why_op.push('view');
1269
+ }
1270
+ }
1271
+ else if ('create' === standard_opname &&
1272
+ (true === ment.post_read || null != postReadVerb(mdesc, ment.pm, parts))) {
1273
+ opname = 'list';
1274
+ why_op.push('post-read');
1005
1275
  }
1006
1276
  else {
1007
1277
  why_op.push('not-load');
@@ -1143,6 +1413,10 @@ function ResolveTransform(spec) {
1143
1413
  transform.res = (0, utility_1.itemEnvelopeTransform)(key);
1144
1414
  }
1145
1415
  }
1416
+ // A singleton is read whole.
1417
+ if (true === ment.singleton) {
1418
+ transform.res = undefined;
1419
+ }
1146
1420
  if ('list' === opname) {
1147
1421
  warnListBody(spec.ctx, methodName + ' ' + pathStr, transform.res, partprops);
1148
1422
  // The page a list reads its records from keeps its metadata beside them.
@@ -1242,14 +1516,89 @@ function BuildEntity(spec) {
1242
1516
  }
1243
1517
  entityMap[entdesc.name] = guideEntity;
1244
1518
  }
1519
+ function genericCmp(name, refs, schema) {
1520
+ if (GENERIC_CMP_WORDS.includes(name) || ID_LITERAL_RE.test(name) ||
1521
+ ('object' === name && 0 === Object.keys((0, utility_2.mergedProperties)(schema) ?? {}).length)) {
1522
+ return true;
1523
+ }
1524
+ const at = name.lastIndexOf('_');
1525
+ const word = name.slice(at + 1);
1526
+ if (at < 1 || !GENERIC_CMP_WORDS.includes(word)) {
1527
+ return false;
1528
+ }
1529
+ const prefix = name.slice(0, at + 1);
1530
+ return Object.keys(refs).some((ref) => ref !== name && ref.startsWith(prefix) &&
1531
+ ref.endsWith('_' + word) && prefix.length + word.length < ref.length);
1532
+ }
1533
+ // The schema a component's original name declares.
1534
+ function cmpSchemaOf(def, origcmp) {
1535
+ return null == origcmp ? undefined :
1536
+ (def?.components?.schemas?.[origcmp] ?? def?.definitions?.[origcmp]);
1537
+ }
1538
+ // An action keeps a verb word's own spelling (`contains`); any other literal
1539
+ // gives its canonical name.
1540
+ function actionWord(part) {
1541
+ const lit = (0, jostraca_2.snakify)(String(part ?? '').replace(/\.?\{[^}]*\}/g, ''));
1542
+ return VERB_WORDS.has(lit) ? lit : (0, utility_1.literalName)(part);
1543
+ }
1544
+ // A run of keys between the entity's literal and a trailing verb, as
1545
+ // `/shops/{owner}/{shop}/{order}/cancel` acts on shop.
1546
+ function keyRunBefore(parts, isEnt) {
1547
+ let at = parts.length - 2;
1548
+ while (0 <= at && isParam(parts[at])) {
1549
+ at--;
1550
+ }
1551
+ return 3 <= parts.length - 2 - at && 0 <= at && isEnt(parts[at]);
1552
+ }
1553
+ // A verb word as a path literal spells it: singular, or in the third person
1554
+ // (`contains`).
1555
+ function isVerbWord(part) {
1556
+ const lit = (0, jostraca_2.snakify)(part);
1557
+ return VERB_WORDS.has(lit) || (VERB_WORDS.has((0, utility_1.literalName)(part)) && (0, utility_2.depluralize)(lit) === lit);
1558
+ }
1559
+ // The parameter after a literal keys it when it is named for the literal, as
1560
+ // `{stop_id}` keys `stop`, which is then a noun whatever verb it spells.
1561
+ function keyedBy(next, name) {
1562
+ return null != next && isParam(next) && (0, utility_1.paramNoun)((0, utility_1.paramOf)(next)) === name;
1563
+ }
1564
+ // The noun the shape's literal gives: the literal before a custom method's
1565
+ // colon, or the noun before the literal when it is a verb, as `/message/send`
1566
+ // names message.
1567
+ function pathNoun(pm, parts, why) {
1568
+ const at = pm.index + PATH_NAME_INDEX[pm.expr];
1569
+ const custom = parts[at].match(CUSTOM_METHOD_RE);
1570
+ if (null != custom && !isParam(custom[1])) {
1571
+ why.push('noun-before-method=' + custom[1]);
1572
+ return (0, utility_1.literalName)(custom[1]);
1573
+ }
1574
+ const name = (0, utility_1.literalName)(parts[at]);
1575
+ const lit = (0, jostraca_2.snakify)(parts[at]);
1576
+ if (null != custom || (isVerbWord(parts[at]) && !keyedBy(parts[at + 1], name))) {
1577
+ for (let i = at - 1; 0 <= i; i--) {
1578
+ if (!isParam(parts[i])) {
1579
+ why.push('noun-before-verb=' + parts[i]);
1580
+ return (0, utility_1.literalName)(parts[i]);
1581
+ }
1582
+ }
1583
+ }
1584
+ return name;
1585
+ }
1245
1586
  function entityPathMatch_tpte(data, pm, mdesc, pathowner, why) {
1246
1587
  const ment = mdesc.MethodEntity;
1247
1588
  const pathNameIndex = PATH_NAME_INDEX['t/p/t/'];
1589
+ const parts = data.work.pathmap[mdesc.path].parts;
1248
1590
  why.push('path=t/p/t/');
1249
1591
  const origPathName = pm[pathNameIndex];
1250
- let entname = (0, utility_2.canonize)(origPathName);
1592
+ let entname = pathNoun(pm, parts, why);
1593
+ const verbish = entname !== (0, utility_1.literalName)(origPathName);
1251
1594
  let ecm = undefined;
1252
- if (null != ment.cmp) {
1595
+ // A verb's response is its result: the resource before the verb names.
1596
+ if (verbish && 'tag' !== ment.ref) {
1597
+ entname = itemAbove(data, parts, lastKey(parts), pathowner) ?? entname;
1598
+ ment.verb_on_parent = (0, struct_1.getelem)(pm, -1);
1599
+ why.push('verb-noun=' + entname);
1600
+ }
1601
+ else if (null != ment.cmp) {
1253
1602
  const parent = verbOnParent(data, pm, mdesc, pathowner);
1254
1603
  if (null != parent) {
1255
1604
  entname = parent;
@@ -1269,27 +1618,27 @@ function entityPathMatch_tpte(data, pm, mdesc, pathowner, why) {
1269
1618
  why.push('prob-ent=' + ecm.orig);
1270
1619
  }
1271
1620
  else if (endsWithCmp(data, pm)) {
1272
- entname = (0, utility_2.canonize)((0, struct_1.getelem)(pm, -1));
1621
+ entname = (0, utility_1.literalName)((0, struct_1.getelem)(pm, -1));
1273
1622
  why.push('prob-ent-last=' + ecm.orig);
1274
1623
  }
1275
1624
  else if (0 < (0, utility_2.findPathsWithPrefix)(data, pm.path, { strict: true })) {
1276
- entname = (0, utility_2.canonize)((0, struct_1.getelem)(pm, -1));
1625
+ entname = (0, utility_1.literalName)((0, struct_1.getelem)(pm, -1));
1277
1626
  why.push('prob-ent-prefix=' + ecm.orig);
1278
1627
  }
1279
1628
  else {
1280
- entname = (0, utility_2.canonize)((0, struct_1.getelem)(pm, -3)) + '_' + entname;
1629
+ entname = (0, utility_1.literalName)((0, struct_1.getelem)(pm, -3)) + '_' + entname;
1281
1630
  why.push('prob-ent-part');
1282
1631
  }
1283
1632
  }
1284
1633
  // Probably an entity action suffix
1285
1634
  else {
1286
1635
  why.push('prob-ent-act');
1287
- entname = (0, utility_2.canonize)((0, struct_1.getelem)(pm, -3));
1636
+ entname = (0, utility_1.literalName)((0, struct_1.getelem)(pm, -3));
1288
1637
  }
1289
1638
  return entname;
1290
1639
  }
1291
1640
  function endsWithCmp(data, pm) {
1292
- const last = (0, utility_2.canonize)((0, struct_1.getelem)(pm, -1));
1641
+ const last = (0, utility_1.literalName)((0, struct_1.getelem)(pm, -1));
1293
1642
  return isOrigCmp(data, last);
1294
1643
  }
1295
1644
  function verbOnParent(data, pm, mdesc, pathowner) {
@@ -1309,38 +1658,626 @@ function verbOnParent(data, pm, mdesc, pathowner) {
1309
1658
  if ('' === verb || cmp === verb || cmp.endsWith('_' + verb)) {
1310
1659
  return null;
1311
1660
  }
1312
- const defpaths = data.def?.paths ?? {};
1313
- // Paths compare with parameters normalised: the item path may spell its
1314
- // key `{id}` where the verb path spells it `{thing_number}`.
1661
+ const itemPath = leafItemRoute(data, pm);
1662
+ return null == itemPath ? null : itemOwner(pm, itemPath, pathowner);
1663
+ }
1664
+ // The item route a verb path hangs off, or null when a path extends the
1665
+ // verb. Paths compare with parameters normalised: the item path may spell
1666
+ // its key `{id}` where the verb path spells it `{thing_number}`.
1667
+ function leafItemRoute(data, pm) {
1315
1668
  const normalize = (p) => p.replace(/\{[^}]+\}/g, '{}');
1316
1669
  const itemNorm = normalize(pm.path.replace(/\/[^/]+$/, ''));
1317
1670
  const prefix = normalize(pm.path) + '/';
1318
- let itemPath = undefined;
1319
- for (const p of Object.keys(defpaths)) {
1671
+ let itemPath = null;
1672
+ for (const p of Object.keys(data.def?.paths ?? {})) {
1320
1673
  const pn = normalize(p);
1321
1674
  if (pn === itemNorm) {
1322
1675
  itemPath = p;
1323
1676
  }
1324
- // A leaf: no path continues past the verb. Compared on a segment
1325
- // boundary, so `/merge` is not "extended" by `/merge-async`.
1677
+ // On a segment boundary, so `/merge` is not extended by `/merge-async`.
1326
1678
  else if (pn.startsWith(prefix)) {
1327
1679
  return null;
1328
1680
  }
1329
1681
  }
1330
- if (null == itemPath) {
1331
- return null;
1332
- }
1333
- // Methods resolve in path order, so the item path's owners are already
1334
- // known. The parent is the entity a READ of the item returns: a PUT on
1335
- // the item answering with a one-off acknowledgement is named after that
1336
- // and must not claim the verb. Fall back to any owner, then the literal.
1682
+ return itemPath;
1683
+ }
1684
+ // Methods resolve in path order, so the item path's owners are already
1685
+ // known. The parent is the entity a READ of the item returns: a PUT on the
1686
+ // item answering with a one-off acknowledgement is named after that and must
1687
+ // not claim the verb. Fall back to any owner, then the literal.
1688
+ function itemOwner(pm, itemPath, pathowner) {
1337
1689
  const owners = pathowner[itemPath] ?? {};
1338
1690
  const parent = owners.GET ?? owners.QUERY ??
1339
1691
  Object.values(owners).sort()[0];
1340
- if (null != parent) {
1341
- return parent;
1692
+ return parent ?? (0, utility_1.literalName)((0, struct_1.getelem)(pm, -3));
1693
+ }
1694
+ // A read whose literals after the item key are singular and whose response
1695
+ // declares no record of its own, or is an array of scalars, is a view of
1696
+ // the item: the nearest resource above takes it as a load action.
1697
+ function viewOfItem(data, mdesc, parts, pathowner) {
1698
+ if (!utility_2.READ_METHODS.includes(mdesc.method)) {
1699
+ return null;
1700
+ }
1701
+ let key = -1;
1702
+ for (let i = 0; i < parts.length; i++) {
1703
+ if (isParam(parts[i])) {
1704
+ key = i;
1705
+ }
1706
+ }
1707
+ if (key < 0 || parts.length - 1 === key || /version/i.test(parts[key])) {
1708
+ return null;
1709
+ }
1710
+ const tail = parts.slice(key + 1);
1711
+ const singular = tail.every((part) => {
1712
+ const lit = (0, jostraca_2.snakify)(part);
1713
+ return '' !== lit && (0, utility_2.depluralize)(lit) === lit;
1714
+ });
1715
+ const schema = getResponseSchema(successResponse(mdesc.responses));
1716
+ const payload = responsePayload(schema);
1717
+ if ((!payload.scalars &&
1718
+ (!singular || payload.record || namedRecord(schema, parts[parts.length - 1]))) ||
1719
+ 0 < (0, utility_2.findPathsWithPrefix)(data, mdesc.path, { strict: true })) {
1720
+ return null;
1721
+ }
1722
+ const parent = itemAbove(data, parts, key, pathowner);
1723
+ return null == parent ? null : { parent, action: tail.map(utility_1.literalName).join('_') };
1724
+ }
1725
+ // A response holding a record under the name its route ends in answers that
1726
+ // record, as `/users/{id}/user-role` answers its `user_role`.
1727
+ function namedRecord(schema, last) {
1728
+ const props = (0, utility_2.mergedProperties)(schema);
1729
+ if (null == props || null == last || isParam(last)) {
1730
+ return false;
1731
+ }
1732
+ const key = Object.keys(props).find((k) => (0, utility_2.canonize)(k) === (0, utility_2.canonize)(last));
1733
+ return null != key && responsePayload(props[key]).record;
1734
+ }
1735
+ // A key after a verb word that follows the entity's own noun keys the entity,
1736
+ // as `/members/lookup/{member_id}` keys member.
1737
+ function verbAfterNoun(parts, at, entname) {
1738
+ const verb = parts[at - 1];
1739
+ if (at < 2 || null == verb || isParam(verb) || !VERB_WORDS.has((0, utility_1.literalName)(verb)) ||
1740
+ (0, utility_2.depluralize)((0, jostraca_2.snakify)(verb)) !== (0, jostraca_2.snakify)(verb)) {
1741
+ return false;
1742
+ }
1743
+ for (let i = at - 2; 0 <= i; i--) {
1744
+ if (!isParam(parts[i])) {
1745
+ return (0, utility_1.literalName)(parts[i]) === entname;
1746
+ }
1747
+ }
1748
+ return false;
1749
+ }
1750
+ // A `/relationships/<name>` route links the resource before it: that
1751
+ // resource's entity, with the relationship as the action, or null.
1752
+ function relationshipOf(data, parts, pathowner) {
1753
+ const at = parts.length - 2;
1754
+ if (at < 1 || 'relationship' !== (0, utility_1.literalName)(parts[at]) || isParam(parts[at + 1]) ||
1755
+ !isParam(parts[at - 1])) {
1756
+ return null;
1757
+ }
1758
+ const parent = itemAbove(data, parts, at - 1, pathowner);
1759
+ return null == parent ? null : { parent, action: verbActionName([parts[at + 1]]) };
1760
+ }
1761
+ // A read of a singular literal followed by an identifier key named for
1762
+ // another noun, answering no record or page of its own, views that noun's item: the
1763
+ // noun, with the path's literals as the action, or null. A name key names an
1764
+ // item of the literal itself.
1765
+ function keyNounOf(data, mdesc, parts) {
1766
+ const key = parts.length - 1;
1767
+ const keyName = isParam(parts[key]) ? (0, utility_1.paramOf)(parts[key]) : null;
1768
+ const lit = key < 1 || isParam(parts[key - 1]) ? '' : (0, jostraca_2.snakify)(parts[key - 1]);
1769
+ if (!utility_2.READ_METHODS.includes(mdesc.method) || null == keyName || '' === lit ||
1770
+ (0, utility_2.depluralize)(lit) !== lit || /version/i.test(keyName) ||
1771
+ !ID_KEY_RE.test((0, utility_2.canonizeParam)(keyName)) ||
1772
+ null != pathNamed(data, '/' + parts.slice(0, key).join('/'))) {
1773
+ return null;
1774
+ }
1775
+ const noun = (0, utility_1.paramNoun)(keyName);
1776
+ const schema = getResponseSchema(successResponse(mdesc.responses));
1777
+ if (noun.length < 3 || (0, utility_1.literalName)(parts[key - 1]).split('_').includes(noun) ||
1778
+ responsePayload(schema).record ||
1779
+ ((0, utility_1.answersPage)(schema) && true !== data.work.listed?.[schema['x-ref']])) {
1780
+ return null;
1781
+ }
1782
+ let from = key - 1;
1783
+ while (0 < from && !isParam(parts[from - 1])) {
1784
+ from--;
1785
+ }
1786
+ const literals = parts.slice(from, key).filter((part) => !/^v\d+$/i.test(part));
1787
+ return { parent: noun, action: literals.map(utility_1.literalName).join('_') };
1788
+ }
1789
+ // A POST answered by an empty Accepted response creates what the route that
1790
+ // reads its results answers with: `/report/render` beside
1791
+ // `/report/renderResults/{resultId}`. That record's name, with the noun the
1792
+ // verb acts on as the action, or null.
1793
+ function asyncResultOf(data, mdesc, parts) {
1794
+ const responses = mdesc.responses;
1795
+ const last = parts[parts.length - 1];
1796
+ if ('POST' !== mdesc.method || null == responses?.['202'] || null != responses?.['200'] ||
1797
+ null != responses?.['201'] || null != getResponseSchema(responses['202']) ||
1798
+ null == last || isParam(last)) {
1799
+ return null;
1800
+ }
1801
+ const verb = (0, utility_1.literalName)(last);
1802
+ const prefix = '/' + parts.slice(0, -1).join('/');
1803
+ for (const p of (0, utility_2.sortedKeys)(data.def?.paths ?? {})) {
1804
+ const rest = (0, utility_2.guideParts)(p).slice(parts.length - 1);
1805
+ if (!((0, utility_2.guidePath)(p) + '/').startsWith(prefix + '/') || (0, utility_2.guideParts)(p).length !== parts.length + 1 ||
1806
+ !isParam(rest[1]) || !(0, utility_1.literalName)(rest[0]).startsWith(verb + '_')) {
1807
+ continue;
1808
+ }
1809
+ const read = getResponseSchema(successResponse(data.def.paths[p]?.get?.responses));
1810
+ const xref = read?.['x-ref'];
1811
+ if ('string' === typeof xref) {
1812
+ const before = parts[parts.length - 2];
1813
+ const noun = null == before ? verb : isParam(before) ?
1814
+ (0, utility_1.literalName)(parts[parts.length - 3] ?? '') : (0, utility_1.literalName)(before);
1815
+ return { parent: cleanCmp(data, (0, utility_2.cmpRefName)(xref)), action: '' === noun ? verb : noun };
1816
+ }
1817
+ }
1818
+ return null;
1819
+ }
1820
+ // A sub-path under an item key that a read answers with a component and a
1821
+ // write sends or answers with the same one is a resource of its own: the
1822
+ // component's reference, or null.
1823
+ // A remote procedure written into the query string, as `?method=a.photos.getInfo`,
1824
+ // names the object before its verb, and the verb is the action.
1825
+ function methodOf(pathStr) {
1826
+ const m = pathStr.match(/[?&]method=([A-Za-z0-9_]+(?:\.[A-Za-z0-9_]+)+)/);
1827
+ const names = null == m ? [] : m[1].split('.');
1828
+ const parent = 2 <= names.length ? (0, utility_1.literalName)(names[names.length - 2]) : '';
1829
+ const action = 2 <= names.length ? (0, jostraca_2.snakify)(names[names.length - 1]) : '';
1830
+ return '' === parent || '' === action ? null : { parent, action, why: 'method-name' };
1831
+ }
1832
+ // A sub-path of a keyless singleton, read, written and deleted with no schema
1833
+ // and no key beneath it, is an action of that singleton, plural or not.
1834
+ function singletonPartOf(data, parts, pathowner) {
1835
+ if (parts.length < 2 || parts.some((part) => isParam(part))) {
1836
+ return null;
1837
+ }
1838
+ const pathStr = pathNamed(data, '/' + parts.join('/'));
1839
+ const parentPath = pathNamed(data, '/' + parts.slice(0, -1).join('/'));
1840
+ const pathdef = null == pathStr ? null : data.def.paths[pathStr];
1841
+ const parentdef = null == parentPath ? null : data.def.paths[parentPath];
1842
+ if (null == pathdef || null == parentdef?.get || null == pathdef.get || null == pathdef.delete ||
1843
+ !['put', 'patch', 'post'].some((m) => null != pathdef[m]) ||
1844
+ ['get', 'put', 'patch', 'post', 'delete'].some((m) => null != getResponseSchema(successResponse(pathdef[m]?.responses))) ||
1845
+ 'array' === getResponseSchema(successResponse(parentdef.get.responses))?.type ||
1846
+ 0 < (0, utility_2.findPathsWithPrefix)(data, pathStr, { strict: true })) {
1847
+ return null;
1848
+ }
1849
+ const parent = pathowner[parentPath]?.GET;
1850
+ const action = (0, jostraca_2.snakify)(parts[parts.length - 1]).replace(/[^a-zA-Z0-9_]/g, '');
1851
+ return null == parent || '' === action ? null : { parent, action, why: 'singleton-part' };
1852
+ }
1853
+ const ITEM_READ_WORDS = ['detail', 'info'];
1854
+ const BINARY_MEDIA_RE = /^(application\/(octet-stream|pdf|zip|gzip)|image\/|audio\/|video\/)/i;
1855
+ // A read beneath an item key that reads the item itself: its `details` or
1856
+ // `info`, spelled as written, or a download whose response is binary, named
1857
+ // as a view is.
1858
+ function readOfItem(data, mdesc, parts, pathowner) {
1859
+ if ('GET' !== mdesc.method || 0 === parts.length || isParam(parts[parts.length - 1]) &&
1860
+ !binaryResponse(data.def?.paths?.[mdesc.path]?.get?.responses)) {
1861
+ return null;
1862
+ }
1863
+ const lastLit = parts.map((part) => !isParam(part)).lastIndexOf(true);
1864
+ const key = parts.slice(0, lastLit).map((part) => isParam(part)).lastIndexOf(true);
1865
+ if (key < 0 || lastLit < 0) {
1866
+ return null;
1867
+ }
1868
+ const literals = parts.slice(key + 1).filter((part) => !isParam(part));
1869
+ const detail = parts.length - 1 === lastLit && key === lastLit - 1 &&
1870
+ ITEM_READ_WORDS.includes((0, utility_1.literalName)(parts[lastLit]));
1871
+ if (!detail && !binaryResponse(data.def?.paths?.[mdesc.path]?.get?.responses)) {
1872
+ return null;
1873
+ }
1874
+ const parent = itemAbove(data, parts, key, pathowner);
1875
+ const action = detail ? (0, jostraca_2.snakify)(parts[lastLit]).replace(/[^a-zA-Z0-9_]/g, '') :
1876
+ literals.map(utility_1.literalName).filter((word) => '' !== word).join('_');
1877
+ return null == parent || ID_LITERAL_RE.test(parent) || '' === action ? null :
1878
+ { parent, action, why: detail ? 'detail-of-item' : 'download-of-item' };
1879
+ }
1880
+ // Every success response declares content, and all of it binary.
1881
+ function binaryResponse(responses) {
1882
+ const ok = [responses?.['200'], responses?.['201'], responses?.['206'], (0, utility_1.rangeResponse)(responses)]
1883
+ .filter((response) => null != response?.content);
1884
+ return 0 < ok.length && ok.every((response) => {
1885
+ const media = Object.keys(response.content);
1886
+ return 0 < media.length && media.every((mt) => BINARY_MEDIA_RE.test(mt) ||
1887
+ ('*/*' === mt && 'binary' === response.content[mt]?.schema?.format));
1888
+ });
1889
+ }
1890
+ function singletonOf(data, pathStr) {
1891
+ const work = data.work;
1892
+ work.singletons = work.singletons ?? Object.create(null);
1893
+ if (pathStr in work.singletons) {
1894
+ return work.singletons[pathStr];
1895
+ }
1896
+ let found = null;
1897
+ const parts = (0, utility_2.guideParts)(pathStr);
1898
+ const pathdef = data.def?.paths?.[pathStr];
1899
+ const key = lastKey(parts);
1900
+ if (0 <= key && key < parts.length - 1 && !/version/i.test(parts[key]) && null != pathdef &&
1901
+ 0 === (0, utility_2.findPathsWithPrefix)(data, pathStr, { strict: true })) {
1902
+ const read = getResponseSchema(successResponse((pathdef.get ?? pathdef.query)?.responses));
1903
+ const ref = 'array' === read?.type ? null : read?.['x-ref'];
1904
+ if ('string' === typeof ref && ['put', 'patch', 'post'].some((m) => null != pathdef[m] && (ref === getResponseSchema(successResponse(pathdef[m].responses))?.['x-ref'] ||
1905
+ ref === (0, body_1.selectedRequestSchema)(data.def, m.toUpperCase(), pathStr)?.['x-ref']))) {
1906
+ found = ref;
1907
+ }
1908
+ }
1909
+ work.singletons[pathStr] = found;
1910
+ return found;
1911
+ }
1912
+ // A write to a singular literal right after an item key, on a path nothing
1913
+ // reads or extends, whose body declares no id, updates the item: the item's
1914
+ // entity and the literal as the action, or null.
1915
+ function subWriteOfItem(data, mdesc, parts, pathowner) {
1916
+ const pathdef = data.def?.paths?.[mdesc.path];
1917
+ const last = parts[parts.length - 1];
1918
+ const lit = null == last || isParam(last) ? '' : (0, jostraca_2.snakify)(last);
1919
+ const key = parts.length - 2;
1920
+ if (!['PUT', 'PATCH'].includes(mdesc.method) || '' === lit || (0, utility_2.depluralize)(lit) !== lit ||
1921
+ key < 0 || !isParam(parts[key]) || /version/i.test(parts[key]) ||
1922
+ null != pathdef?.get || null != pathdef?.query ||
1923
+ 0 < (0, utility_2.findPathsWithPrefix)(data, mdesc.path, { strict: true })) {
1924
+ return null;
1925
+ }
1926
+ const body = (0, body_1.selectedRequestSchema)(data.def, mdesc.method, mdesc.path) ?? mdesc.requestBody?.schema;
1927
+ if (null == body || responsePayload(body).record) {
1928
+ return null;
1929
+ }
1930
+ const parent = itemAbove(data, parts, key, pathowner);
1931
+ return null == parent ? null : { parent, action: (0, utility_1.literalName)(last) };
1932
+ }
1933
+ // The entity of the item route a view hangs off, the entity a read of it
1934
+ // returns, else the noun before the key.
1935
+ function itemAbove(data, parts, key, pathowner) {
1936
+ return keyOwner(data, parts, key, pathowner) ??
1937
+ (0 < key && !isParam(parts[key - 1]) ? (0, utility_1.literalName)(parts[key - 1]) : null);
1938
+ }
1939
+ // The entity holding the item route that ends at a key.
1940
+ function keyOwner(data, parts, key, pathowner) {
1941
+ const itemPath = key < 0 ? null : pathNamed(data, '/' + parts.slice(0, key + 1).join('/'));
1942
+ const owners = null == itemPath ? {} : (pathowner[itemPath] ?? {});
1943
+ return owners.GET ?? owners.QUERY ?? Object.values(owners).sort()[0] ?? null;
1944
+ }
1945
+ // The index of the last parameter of a path, or -1.
1946
+ function lastKey(parts) {
1947
+ let key = -1;
1948
+ parts.forEach((part, i) => {
1949
+ if (isParam(part)) {
1950
+ key = i;
1951
+ }
1952
+ });
1953
+ return key;
1954
+ }
1955
+ // A literal opening with a verb word is a verb on the nearest resource up
1956
+ // the path: a collection of its own between the key and the verb, else the
1957
+ // key's resource. The action is named for the literals after that resource.
1958
+ // A bare verb right after the key is left to the shape rules.
1959
+ function verbOfItem(data, mdesc, parts, pathowner) {
1960
+ const last = parts[parts.length - 1];
1961
+ if (null == last || isParam(last)) {
1962
+ return null;
1963
+ }
1964
+ const words = (0, utility_2.canonize)(last).split('_');
1965
+ const key = lastKey(parts);
1966
+ const bare = 1 === words.length;
1967
+ if (!VERB_WORDS.has(words[0]) || (key < 0 && bare) || (0 <= key && /version/i.test(parts[key])) ||
1968
+ (bare && (key === parts.length - 2 || (0, utility_2.depluralize)((0, jostraca_2.snakify)(last)) !== (0, jostraca_2.snakify)(last))) ||
1969
+ 0 < (0, utility_2.findPathsWithPrefix)(data, mdesc.path, { strict: true })) {
1970
+ return null;
1971
+ }
1972
+ const keyParent = itemAbove(data, parts, key, pathowner);
1973
+ for (let i = parts.length - 2; key < i; i--) {
1974
+ const lit = (0, jostraca_2.snakify)(parts[i]);
1975
+ if ((0, utility_2.depluralize)(lit) === lit) {
1976
+ continue;
1977
+ }
1978
+ const route = pathNamed(data, '/' + parts.slice(0, i + 1).join('/'));
1979
+ const owners = null == route ? {} : (pathowner[route] ?? {});
1980
+ const owner = owners.GET ?? owners.QUERY ?? Object.values(owners).sort()[0];
1981
+ if (null != owner && owner !== keyParent) {
1982
+ return { parent: owner, action: verbActionName(parts.slice(i + 1)) };
1983
+ }
1984
+ }
1985
+ return null == keyParent ? null : { parent: keyParent, action: verbActionName(parts.slice(key + 1)) };
1986
+ }
1987
+ // The verb keeps its own spelling, plural and all; the literals before it
1988
+ // are path nouns, and an `actions` element only gathers the verb after it.
1989
+ function verbActionName(literals) {
1990
+ const at = literals.length - 2;
1991
+ const kept = 0 <= at && 'action' === (0, utility_1.literalName)(literals[at]) ?
1992
+ [...literals.slice(0, at), literals[at + 1]] : literals;
1993
+ const last = kept.length - 1;
1994
+ return kept
1995
+ .map((lit, i) => i < last ? (0, utility_1.literalName)(lit) :
1996
+ (0, jostraca_2.snakify)(lit.replace(/\.?\{[^}]*\}/g, '')).replace(/[^a-zA-Z0-9_]/g, ''))
1997
+ .join('_');
1998
+ }
1999
+ // A literal too short for a word names nothing.
2000
+ function shortLiteral(pm) {
2001
+ return (0, utility_2.canonize)(pm[PATH_NAME_INDEX[pm.expr]]).replace(/_/g, '').length < 3;
2002
+ }
2003
+ // The naming literal is a generic word, or a verb word with no literal
2004
+ // before it in the path.
2005
+ function nounless(pm, parts) {
2006
+ const at = pm.index + PATH_NAME_INDEX[pm.expr];
2007
+ const name = (0, utility_1.literalName)(parts[at]);
2008
+ return GENERIC_WORDS.includes(name) ||
2009
+ (VERB_WORDS.has(name) && parts.slice(0, at).every((part) => isParam(part)));
2010
+ }
2011
+ // The component a route answers with, cleaned, when no tag stands for it.
2012
+ function answeredCmp(mdesc) {
2013
+ const ment = mdesc.MethodEntity;
2014
+ const cmp = 'tag' === ment?.ref ? null : ment?.cmp;
2015
+ return 'string' === typeof cmp && 3 <= cmp.replace(/_/g, '').length ? cmp : null;
2016
+ }
2017
+ // The object an operationId names after its leading verb, less a tail that
2018
+ // opens with via, by or with: getting an access token via a provider names
2019
+ // access_token.
2020
+ function opidObject(opid) {
2021
+ const words = 'string' === typeof opid ? (0, utility_2.canonize)(opid).split('_').filter((w) => '' !== w) : [];
2022
+ if (words.length < 2 || !VERB_WORDS.has(words[0])) {
2023
+ return null;
1342
2024
  }
1343
- return (0, utility_2.canonize)((0, struct_1.getelem)(pm, -3));
2025
+ const tail = words.findIndex((word, i) => 1 < i && OPID_TAIL_WORDS.includes(word));
2026
+ const name = (0, utility_2.canonize)(words.slice(1, tail < 0 ? words.length : tail).join('_'));
2027
+ return name.replace(/_/g, '').length < 3 ? null : name;
2028
+ }
2029
+ // The path of the definition that is the given path with its parameters
2030
+ // normalised, or null.
2031
+ function pathNamed(data, pathStr) {
2032
+ const work = data.work;
2033
+ if (null == work.normPaths) {
2034
+ work.normPaths = Object.create(null);
2035
+ for (const p of Object.keys(data.def?.paths ?? {})) {
2036
+ work.normPaths[normalizeParams(p)] = work.normPaths[normalizeParams(p)] ?? p;
2037
+ }
2038
+ }
2039
+ return work.normPaths[normalizeParams(pathStr)] ?? null;
2040
+ }
2041
+ // What a response carries past its envelopes: whether it reaches an array of
2042
+ // scalars, and whether the record it reaches declares an id, with that record.
2043
+ function responsePayload(schema) {
2044
+ let node = schema;
2045
+ let array = false;
2046
+ for (let depth = 0; null != node && 'object' === typeof node && depth < 8; depth++) {
2047
+ if ('array' === node.type) {
2048
+ array = true;
2049
+ node = node.items;
2050
+ continue;
2051
+ }
2052
+ const props = (0, utility_2.mergedProperties)(node);
2053
+ if (null == props) {
2054
+ break;
2055
+ }
2056
+ if (null != props.id) {
2057
+ return { scalars: false, record: true, node };
2058
+ }
2059
+ const env = (0, utility_1.strictEnvelopeProp)(props, 'list') ?? (0, utility_1.strictEnvelopeProp)(props, 'load');
2060
+ if (null == env) {
2061
+ break;
2062
+ }
2063
+ node = props[env];
2064
+ }
2065
+ const scalar = null != node && 'object' === typeof node &&
2066
+ SCALAR_TYPES.has(node.type) && null == (0, utility_2.mergedProperties)(node);
2067
+ return { scalars: array && scalar, record: false, node };
2068
+ }
2069
+ // The record component the item route of a path answers with, the path's own
2070
+ // when it ends with a parameter, else the item route beneath it: null for none.
2071
+ function recordOf(data, pathStr) {
2072
+ const work = data.work;
2073
+ work.itemRecords = work.itemRecords ?? Object.create(null);
2074
+ if (pathStr in work.itemRecords) {
2075
+ return work.itemRecords[pathStr];
2076
+ }
2077
+ const parts = (0, utility_2.guideParts)(pathStr);
2078
+ const itemPath = isParam(parts[parts.length - 1]) ? pathStr :
2079
+ pathNamed(data, pathStr + '/{}');
2080
+ let found = null;
2081
+ const orig = null == itemPath ? null : itemRecordRef(data, itemPath);
2082
+ if (null != orig) {
2083
+ const cmp = cleanCmp(data, (0, utility_2.canonizeCmpName)(orig)).replace(/_v\d+$/, '');
2084
+ found = { cmp, orig, shared: 1 < (itemRecordRoutes(data)[orig]?.size ?? 0) };
2085
+ }
2086
+ work.itemRecords[pathStr] = found;
2087
+ return found;
2088
+ }
2089
+ // The component an item route's read answers with when it declares an id.
2090
+ function itemRecordRef(data, itemPath) {
2091
+ const read = data.def?.paths?.[itemPath]?.get;
2092
+ const schema = getResponseSchema(successResponse(read?.responses));
2093
+ const payload = responsePayload(schema);
2094
+ const xref = (payload.record ? payload.node :
2095
+ recordNode(schema, (0, utility_1.paramOf)((0, utility_2.guideParts)(itemPath).slice(-1)[0] ?? '')))?.['x-ref'];
2096
+ const m = 'string' === typeof xref ? xref.match(/\/(components\/schemas|definitions)\/(.+)$/) : null;
2097
+ return null == m ? null : m[2];
2098
+ }
2099
+ // The record an item's read answers with: an object declaring an `id` or the
2100
+ // field its route's key names, read through a one-record envelope.
2101
+ function recordNode(schema, keyName) {
2102
+ const key = null == keyName ? null : (0, utility_2.canonizeParam)(keyName);
2103
+ let node = schema;
2104
+ for (let depth = 0; null != node && 'object' === typeof node && depth < 8; depth++) {
2105
+ const props = (0, utility_2.mergedProperties)(node);
2106
+ if (null == props) {
2107
+ return null;
2108
+ }
2109
+ if (null != props.id || (null != key && Object.keys(props).some((p) => (0, utility_2.canonizeParam)(p) === key))) {
2110
+ return node;
2111
+ }
2112
+ const env = (0, utility_1.envelopeProp)(props, 'load');
2113
+ if (null == env) {
2114
+ return null;
2115
+ }
2116
+ node = props[env];
2117
+ }
2118
+ return null;
2119
+ }
2120
+ // A read on a literal right after a collection that answers the collection's
2121
+ // own record is a variant of its list: the collection's entity and the
2122
+ // literal as the action, or null.
2123
+ function variantOfCollection(data, mdesc, parts, pathowner) {
2124
+ const last = parts[parts.length - 1];
2125
+ if (!utility_2.READ_METHODS.includes(mdesc.method) || parts.length < 2 || null == last ||
2126
+ isParam(last) || isParam(parts[parts.length - 2])) {
2127
+ return null;
2128
+ }
2129
+ const collection = pathNamed(data, '/' + parts.slice(0, -1).join('/'));
2130
+ const record = null == collection ? null : recordOf(data, collection);
2131
+ if (null == record) {
2132
+ return null;
2133
+ }
2134
+ let node = getResponseSchema(successResponse(mdesc.responses));
2135
+ const props = 'array' === node?.type ? null : (0, utility_2.mergedProperties)(node);
2136
+ const env = null == props ? null : (0, utility_1.envelopeProp)(props, 'list');
2137
+ node = null == env || null == props ? node : props[env];
2138
+ const xref = 'array' === node?.type ? node.items?.['x-ref'] : null;
2139
+ if ('string' !== typeof xref || xref.match(utility_2.CMP_REF_RE)?.[2] !== record.orig) {
2140
+ return null;
2141
+ }
2142
+ const owners = pathowner[collection] ?? {};
2143
+ const parent = owners.GET ?? owners.QUERY ?? record.cmp;
2144
+ return { parent, action: (0, utility_1.literalName)(last) };
2145
+ }
2146
+ // The collections whose item routes answer each record, by the item path
2147
+ // with its parameters normalised: a record shared by several names them all,
2148
+ // and each collection's literal becomes its action.
2149
+ function itemRecordRoutes(data) {
2150
+ const work = data.work;
2151
+ if (null == work.itemRecordRoutes) {
2152
+ work.itemRecordRoutes = Object.create(null);
2153
+ for (const p of Object.keys(data.def?.paths ?? {})) {
2154
+ const parts = (0, utility_2.guideParts)(p);
2155
+ const orig = 0 < parts.length && isParam(parts[parts.length - 1]) ? itemRecordRef(data, p) : null;
2156
+ if (null != orig) {
2157
+ (work.itemRecordRoutes[orig] = work.itemRecordRoutes[orig] ?? new Set()).add(normalizeParams(p));
2158
+ }
2159
+ }
2160
+ }
2161
+ return work.itemRecordRoutes;
2162
+ }
2163
+ // A record named for a literal of the path and the naming literal, as
2164
+ // `call_route_allowed_caller` under `/call_routes/{id}/allowed_callers`, is
2165
+ // qualified rather than wrapped.
2166
+ function pathQualified(parts, cmp, lit) {
2167
+ if (!cmp.endsWith('_' + lit)) {
2168
+ return false;
2169
+ }
2170
+ const prefix = cmp.slice(0, -(lit.length + 1));
2171
+ return parts.some((part) => !isParam(part) && (0, utility_1.literalName)(part) === prefix);
2172
+ }
2173
+ // Whether a component's name holds the literal's words in a row, compared
2174
+ // singular: such a name wraps the literal and the literal keeps naming.
2175
+ function wrapsLiteral(cmp, lit) {
2176
+ const cw = cmp.split('_').map((w) => (0, utility_2.depluralize)(w));
2177
+ const lw = lit.split('_').map((w) => (0, utility_2.depluralize)(w));
2178
+ for (let i = 0; i + lw.length <= cw.length; i++) {
2179
+ if (lw.every((w, j) => w === cw[i + j])) {
2180
+ return true;
2181
+ }
2182
+ }
2183
+ return false;
2184
+ }
2185
+ // A POST addressed to a singular literal that names no component, with no
2186
+ // item route above it to be a verb on or beneath it to be a collection, is a
2187
+ // command: its body component names it.
2188
+ function commandName(data, mdesc, pm, parts, why) {
2189
+ const ment = mdesc.MethodEntity;
2190
+ if ('POST' !== mdesc.method || null != ment.verb_on_parent ||
2191
+ !LITERAL_LAST_SHAPES.includes(pm.expr)) {
2192
+ return null;
2193
+ }
2194
+ const last = (0, struct_1.getelem)(pm, -1);
2195
+ const lit = (0, jostraca_2.snakify)(last);
2196
+ if ('' === lit || (0, utility_2.depluralize)(lit) !== lit || pathNoun(pm, parts, []) !== (0, utility_1.literalName)(last) ||
2197
+ isOrigCmp(data, (0, utility_1.literalName)(last)) || null != keyParamOf(data.def, mdesc.path, data.work) ||
2198
+ ('t/p/t/' === pm.expr && null != leafItemRoute(data, pm)) || readsRecord(data, mdesc.path)) {
2199
+ return null;
2200
+ }
2201
+ const source = commandSource(data, mdesc);
2202
+ if (null != source) {
2203
+ why.push(('body' === source.from ? 'command=' : 'command-opid=') + source.name);
2204
+ }
2205
+ return source?.name ?? null;
2206
+ }
2207
+ // What names a command: its body component, else, when no response component
2208
+ // names it either, the object of its operationId.
2209
+ function commandSource(data, mdesc) {
2210
+ const body = commandBodyName(data, mdesc);
2211
+ if (null != body) {
2212
+ return { name: body, from: 'body' };
2213
+ }
2214
+ const responded = getResponseSchema(successResponse(mdesc.responses));
2215
+ const object = null == namingRef(responded, data.work.envelope) ? opidObject(mdesc.operationId) : null;
2216
+ return null == object ? null : { name: object, from: 'opid' };
2217
+ }
2218
+ // A path whose read answers with a record is a resource, not a command.
2219
+ function readsRecord(data, pathStr) {
2220
+ const mdef = data.def?.paths?.[pathStr]?.get;
2221
+ return null != mdef && responsePayload(getResponseSchema(successResponse(mdef.responses))).record;
2222
+ }
2223
+ // The name a command's body component gives under any media type, the first
2224
+ // member of a oneOf, its affixes stripped. A body that is no object, such as
2225
+ // an upload, names nothing.
2226
+ function commandBodyName(data, mdesc) {
2227
+ const body = (0, body_1.selectedRequestSchema)(data.def, mdesc.method, mdesc.path) ?? mdesc.requestBody?.schema;
2228
+ const member = Array.isArray(body?.oneOf) ? body.oneOf[0] :
2229
+ Array.isArray(body?.anyOf) ? body.anyOf[0] : body;
2230
+ const xref = member?.['x-ref'];
2231
+ if ('string' !== typeof xref || null == (0, utility_2.mergedProperties)(member)) {
2232
+ return null;
2233
+ }
2234
+ let name = cleanCmp(data, (0, utility_2.canonizeCmpName)((0, utility_2.cmpRefName)(xref)));
2235
+ for (;;) {
2236
+ const suffix = COMMAND_SUFFIXES.find((sfx) => name.endsWith(sfx) && sfx.length < name.length);
2237
+ if (null == suffix) {
2238
+ break;
2239
+ }
2240
+ name = name.slice(0, -suffix.length);
2241
+ }
2242
+ return name.replace(/_/g, '').length < 3 ? null : name;
2243
+ }
2244
+ // A read on a command's path is the command's load.
2245
+ function commandTwin(data, mdesc, pm, parts, why) {
2246
+ const post = data.def?.paths?.[mdesc.path]?.post;
2247
+ if (!utility_2.READ_METHODS.includes(mdesc.method) || null == post || 'object' !== typeof post) {
2248
+ return null;
2249
+ }
2250
+ const twin = { method: 'POST', path: mdesc.path, responses: post.responses, MethodEntity: {} };
2251
+ const name = commandName(data, twin, pm, parts, []);
2252
+ if (null != name) {
2253
+ why.push('command-twin=' + name);
2254
+ }
2255
+ return name;
2256
+ }
2257
+ // The read verb a POST's last literal opens with, where it answers a collection.
2258
+ function postReadVerb(mdesc, pm, parts) {
2259
+ if ('POST' !== mdesc.method) {
2260
+ return null;
2261
+ }
2262
+ const verbs = verbParts(parts);
2263
+ const last = verbs[verbs.length - 1];
2264
+ const lit = (0, jostraca_2.snakify)(last);
2265
+ const verb = READ_VERBS.find((v) => lit === v || lit.startsWith(v + '_'));
2266
+ return null == verb || isParam(last) || !isListResponse(mdesc, pm, mdesc.path, []) ? null : verb;
2267
+ }
2268
+ // A POST whose literal opens with a read verb and answers with a collection
2269
+ // lists the noun the literal names, the body its filter; '' keeps the shape's
2270
+ // name where the literal is the verb alone.
2271
+ function postRead(mdesc, pm, parts, why) {
2272
+ const verb = postReadVerb(mdesc, pm, parts);
2273
+ if (null == verb) {
2274
+ return null;
2275
+ }
2276
+ const lit = (0, jostraca_2.snakify)(verbParts(parts)[verbParts(parts).length - 1]);
2277
+ mdesc.MethodEntity.post_read = true;
2278
+ why.push('post-read=' + verb);
2279
+ const noun = lit.slice(verb.length + 1);
2280
+ return '' === noun ? '' : (0, utility_1.literalName)(noun);
1344
2281
  }
1345
2282
  // The collection of an item route whose method its tag alone names, when the
1346
2283
  // route answers nothing.
@@ -1392,7 +2329,7 @@ function itemOfCollection(data, mdesc, parts) {
1392
2329
  const work = data.work;
1393
2330
  const collection = naming.collection;
1394
2331
  const cmp = mdesc.MethodEntity.cmp;
1395
- const segment = (0, utility_2.canonize)(collection.substring(collection.lastIndexOf('/') + 1));
2332
+ const segment = (0, utility_1.literalName)(collection.substring(collection.lastIndexOf('/') + 1));
1396
2333
  const own = segment === cmp;
1397
2334
  const tagged = own ? segment : cmp + '_' + segment;
1398
2335
  const tagOwn = own ? (path) => tagNamesRoute(data, path, cmp) : undefined;
@@ -1469,15 +2406,16 @@ function isOrigCmp(data, name) {
1469
2406
  return null != data.guide.metrics.count.origcmprefs[name];
1470
2407
  }
1471
2408
  function entityOccursInPath(parts, entname) {
1472
- return parts.some(p => p[0] !== '{' && (0, utility_2.canonize)(p.toLowerCase()) === entname);
2409
+ return parts.some(p => p[0] !== '{' && (0, utility_1.literalName)(p.toLowerCase()) === entname);
1473
2410
  }
1474
2411
  function entityPathMatch_tpe(data, pm, mdesc, why) {
1475
2412
  const ment = mdesc.MethodEntity;
1476
- const pathNameIndex = PATH_NAME_INDEX['t/p/'];
1477
2413
  why.push('path=t/p/');
1478
- const origPathName = pm[pathNameIndex];
1479
- let entname = (0, utility_2.canonize)(origPathName);
1480
- if (null != ment.cmp || probableEntityMethod(data, mdesc, pm, why)) {
2414
+ let entname = pathNoun(pm, data.work.pathmap[mdesc.path].parts, why);
2415
+ if (entname !== (0, utility_1.literalName)(pm[PATH_NAME_INDEX[pm.expr]]) && 'tag' !== ment.ref) {
2416
+ why.push('verb-noun=' + entname);
2417
+ }
2418
+ else if (null != ment.cmp || probableEntityMethod(data, mdesc, pm, why)) {
1481
2419
  let ecm = entityCmpMatch(data, entname, mdesc, why);
1482
2420
  entname = ecm.name;
1483
2421
  }
@@ -1486,13 +2424,21 @@ function entityPathMatch_tpe(data, pm, mdesc, why) {
1486
2424
  }
1487
2425
  return entname;
1488
2426
  }
1489
- function entityPathMatch_pte(data, pm, mdesc, why) {
2427
+ function entityPathMatch_pte(data, pm, mdesc, pathowner, why) {
1490
2428
  const ment = mdesc.MethodEntity;
1491
- const pathNameIndex = PATH_NAME_INDEX['p/t/'];
1492
2429
  why.push('path=p/t/');
1493
- const origPathName = pm[pathNameIndex];
1494
- let entname = (0, utility_2.canonize)(origPathName);
1495
- if (null != ment.cmp || probableEntityMethod(data, mdesc, pm, why)) {
2430
+ const parts = data.work.pathmap[mdesc.path].parts;
2431
+ let entname = pathNoun(pm, parts, why);
2432
+ if (entname !== (0, utility_1.literalName)(pm[PATH_NAME_INDEX[pm.expr]]) && 'tag' !== ment.ref) {
2433
+ // A verb after a run of keys acts on the item they address.
2434
+ const owner = keyOwner(data, parts, lastKey(parts), pathowner);
2435
+ if (null != owner) {
2436
+ entname = owner;
2437
+ ment.verb_on_parent = (0, struct_1.getelem)(pm, -1);
2438
+ }
2439
+ why.push('verb-noun=' + entname);
2440
+ }
2441
+ else if (null != ment.cmp || probableEntityMethod(data, mdesc, pm, why)) {
1496
2442
  let ecm = entityCmpMatch(data, entname, mdesc, why);
1497
2443
  entname = ecm.name;
1498
2444
  }
@@ -1503,11 +2449,12 @@ function entityPathMatch_pte(data, pm, mdesc, why) {
1503
2449
  }
1504
2450
  function entityPathMatch_te(data, pm, mdesc, why) {
1505
2451
  const ment = mdesc.MethodEntity;
1506
- const pathNameIndex = PATH_NAME_INDEX['t/'];
1507
2452
  why.push('path=t/');
1508
- const origPathName = pm[pathNameIndex];
1509
- let entname = (0, utility_2.canonize)(origPathName);
1510
- if (null != ment.cmp || probableEntityMethod(data, mdesc, pm, why)) {
2453
+ let entname = pathNoun(pm, data.work.pathmap[mdesc.path].parts, why);
2454
+ if (entname !== (0, utility_1.literalName)(pm[PATH_NAME_INDEX[pm.expr]]) && 'tag' !== ment.ref) {
2455
+ why.push('verb-noun=' + entname);
2456
+ }
2457
+ else if (null != ment.cmp || probableEntityMethod(data, mdesc, pm, why)) {
1511
2458
  let ecm = entityCmpMatch(data, entname, mdesc, why);
1512
2459
  entname = ecm.name;
1513
2460
  }
@@ -1518,11 +2465,12 @@ function entityPathMatch_te(data, pm, mdesc, why) {
1518
2465
  }
1519
2466
  function entityPathMatch_tpp(data, pm, mdesc, why) {
1520
2467
  const ment = mdesc.MethodEntity;
1521
- const pathNameIndex = PATH_NAME_INDEX['t/p/p'];
1522
2468
  why.push('path=t/p/p');
1523
- const origPathName = pm[pathNameIndex];
1524
- let entname = (0, utility_2.canonize)(origPathName);
1525
- if (null != ment.cmp || probableEntityMethod(data, mdesc, pm, why)) {
2469
+ let entname = pathNoun(pm, data.work.pathmap[mdesc.path].parts, why);
2470
+ if (entname !== (0, utility_1.literalName)(pm[PATH_NAME_INDEX[pm.expr]]) && 'tag' !== ment.ref) {
2471
+ why.push('verb-noun=' + entname);
2472
+ }
2473
+ else if (null != ment.cmp || probableEntityMethod(data, mdesc, pm, why)) {
1526
2474
  let ecm = entityCmpMatch(data, entname, mdesc, why);
1527
2475
  entname = ecm.name;
1528
2476
  }
@@ -1538,14 +2486,14 @@ function getRequestBodySchema(def, mdesc) {
1538
2486
  // The response an operation's result is read from, down to an Accepted
1539
2487
  // response when nothing else answers, whose body may be the job it queued.
1540
2488
  function successResponse(responses) {
1541
- return responses?.[200] ?? responses?.[201] ?? responses?.[202];
2489
+ return responses?.[200] ?? responses?.[201] ?? responses?.[202] ?? (0, utility_1.rangeResponse)(responses);
1542
2490
  }
1543
2491
  // The response schemas an operation answers with when it succeeds, in the
1544
2492
  // order they are tried. An Accepted response names nothing, since its body
1545
2493
  // may describe the work rather than the resource.
1546
2494
  function successSchemas(responses) {
1547
- return ['200', '201']
1548
- .map((rescode) => getResponseSchema(responses?.[rescode]))
2495
+ return [responses?.['200'], responses?.['201'], (0, utility_1.rangeResponse)(responses)]
2496
+ .map((response) => getResponseSchema(response))
1549
2497
  .filter((schema) => null != schema);
1550
2498
  }
1551
2499
  // A read answered only by an Accepted response is named from it when another
@@ -1608,8 +2556,7 @@ function namingRef(schema, envelope) {
1608
2556
  return 'array' === schema?.type ? schema.items?.['x-ref'] : undefined;
1609
2557
  }
1610
2558
  function getResponseSchema(response) {
1611
- return response?.content?.['application/json']?.schema ??
1612
- response?.schema;
2559
+ return (0, utility_1.responseSchema)(response);
1613
2560
  }
1614
2561
  // The record a path answers with, read from its methods in the order they are
1615
2562
  // considered, so a read decides before a write.
@@ -1632,7 +2579,7 @@ function pathRecordRef(def, pathStr, methods, collection, accepted = true) {
1632
2579
  function routeRecordRef(def, pathStr, method, collection, accepted = true) {
1633
2580
  const responses = def?.paths?.[pathStr]?.[method.toLowerCase()]?.responses;
1634
2581
  const schema = getResponseSchema(accepted ? successResponse(responses) :
1635
- responses?.[200] ?? responses?.[201]);
2582
+ responses?.[200] ?? responses?.[201] ?? (0, utility_1.rangeResponse)(responses));
1636
2583
  if (null == schema || 'object' !== typeof schema) {
1637
2584
  return null;
1638
2585
  }
@@ -1652,6 +2599,19 @@ function routeRecordRef(def, pathStr, method, collection, accepted = true) {
1652
2599
  return xref ?? null;
1653
2600
  }
1654
2601
  function inferEntityName(mdesc, parts, why) {
2602
+ // A route with no literal is named for the component it answers, else
2603
+ // for its first parameter's noun, a version aside.
2604
+ const answered = answeredCmp(mdesc);
2605
+ if (null != answered) {
2606
+ why.push('infer-answers=' + answered);
2607
+ return answered;
2608
+ }
2609
+ const keys = parts.filter((part) => isParam(part) && !/version/i.test(part));
2610
+ const noun = 0 < keys.length ? (0, utility_1.paramNoun)((0, utility_1.paramOf)(keys[0])) : '';
2611
+ if (3 <= noun.length) {
2612
+ why.push('infer-param');
2613
+ return noun;
2614
+ }
1655
2615
  if (mdesc.operationId) {
1656
2616
  const opid = (0, utility_2.stripOperationVerb)((0, utility_2.canonize)(mdesc.operationId));
1657
2617
  if (opid.length >= 3) {
@@ -1660,7 +2620,8 @@ function inferEntityName(mdesc, parts, why) {
1660
2620
  }
1661
2621
  }
1662
2622
  // Try response schema title
1663
- const response = mdesc.responses?.[200] ?? mdesc.responses?.[201];
2623
+ const response = mdesc.responses?.[200] ?? mdesc.responses?.[201] ??
2624
+ (0, utility_1.rangeResponse)(mdesc.responses);
1664
2625
  const resSchema = getResponseSchema(response);
1665
2626
  if (resSchema?.title) {
1666
2627
  const title = (0, utility_2.canonize)(resSchema.title);
@@ -1672,18 +2633,25 @@ function inferEntityName(mdesc, parts, why) {
1672
2633
  // Try last non-param path segment
1673
2634
  for (let i = parts.length - 1; i >= 0; i--) {
1674
2635
  if (!isParam(parts[i])) {
1675
- const seg = (0, utility_2.canonize)(parts[i]);
2636
+ const seg = (0, utility_1.literalName)(parts[i]);
1676
2637
  if (seg.length >= 3) {
1677
2638
  why.push('infer-path-seg');
1678
2639
  return seg;
1679
2640
  }
1680
2641
  }
1681
2642
  }
2643
+ // A tag names only a route with no usable literal.
2644
+ const tag = mdesc.MethodEntity?.cmp;
2645
+ if ('tag' === mdesc.MethodEntity?.ref && 'string' === typeof tag && 3 <= tag.length) {
2646
+ why.push('infer-tag');
2647
+ return tag;
2648
+ }
1682
2649
  return null;
1683
2650
  }
1684
2651
  function probableEntityMethod(data, mdesc, pm, why) {
1685
2652
  const reqSchema = getRequestBodySchema(data.def, mdesc);
1686
- const response = mdesc.responses?.['201'] || mdesc.responses?.['200'];
2653
+ const response = mdesc.responses?.['201'] || mdesc.responses?.['200'] ||
2654
+ (0, utility_1.rangeResponse)(mdesc.responses);
1687
2655
  const resSchema = getResponseSchema(response);
1688
2656
  const noResponse = null == resSchema && null != mdesc.responses?.['204'];
1689
2657
  let prob_why = '';
@@ -1738,19 +2706,62 @@ function entityCmpMatch(data, entname, mdesc, why) {
1738
2706
  // names the path's resource, qualified by its parent and versioned.
1739
2707
  const unversioned = (ment.cmp ?? '').replace(/_v\d+$/, '');
1740
2708
  const qualifies = unversioned !== ment.cmp && unversioned.endsWith('_' + entname);
2709
+ const record = recordOf(data, mdesc.path);
2710
+ const parts = data.work.pathmap[mdesc.path].parts;
2711
+ const recordWraps = null != record && record.cmp !== entname &&
2712
+ wrapsLiteral(record.cmp, entname) && !pathQualified(parts, record.cmp, entname);
2713
+ const cmpWraps = null != ment.cmp && 'tag' !== ment.ref && ment.cmp !== entname &&
2714
+ wrapsLiteral(ment.cmp, entname) && !pathQualified(parts, ment.cmp, entname);
2715
+ // The key parameter names the component, whatever the literal beside it:
2716
+ // `/s/{shelf}` answering with a Shelf.
1741
2717
  if (null != ment.cmp
2718
+ && entname != ment.cmp
2719
+ && 'tag' !== ment.ref
2720
+ && keyParamNames(data, mdesc, ment.cmp)) {
2721
+ why.push('cmp-names-key');
2722
+ out.name = ment.cmp;
2723
+ out.orig = ment.origcmp;
2724
+ out.cmpish = true;
2725
+ out.pathish = false;
2726
+ }
2727
+ // The record an item route answers names the item and its collection,
2728
+ // whatever its frequency, unless the record's name wraps the literal's.
2729
+ else if (null != record
2730
+ && record.cmp !== entname
2731
+ && !recordWraps) {
2732
+ why.push('cmp-record=' + record.orig);
2733
+ out.name = record.cmp;
2734
+ out.orig = record.orig;
2735
+ out.cmpish = true;
2736
+ out.pathish = false;
2737
+ ment.record_named = true;
2738
+ if (record.shared) {
2739
+ ment.collection_action = entname;
2740
+ why.push('collection-action=' + entname);
2741
+ }
2742
+ }
2743
+ else if (null != ment.cmp
1742
2744
  && entname != ment.cmp
1743
2745
  && !ment.cmp.startsWith(entname)) {
1744
2746
  if (cmpInfrequent && cmpShared) {
1745
2747
  why.push('cmp-shared');
1746
2748
  }
1747
- if (qualifies) {
2749
+ // A tag that is not the path's own noun groups routes; the noun names them.
2750
+ if ('tag' === ment.ref && !ID_LITERAL_RE.test(entname) &&
2751
+ !tagHolds(data, data.work.pathmap[mdesc.path].parts, mdesc.path, ment.cmp)) {
2752
+ why.push('path-over-tag');
2753
+ }
2754
+ else if (qualifies) {
1748
2755
  why.push('cmp-qualifies-path');
1749
2756
  out.name = unversioned;
1750
2757
  out.orig = ment.origcmp;
1751
2758
  out.cmpish = true;
1752
2759
  out.pathish = false;
1753
2760
  }
2761
+ // A component that wraps the literal's words is the literal's own shape.
2762
+ else if (recordWraps || cmpWraps) {
2763
+ why.push('path-over-wrapper=' + (recordWraps ? record.orig : ment.origcmp));
2764
+ }
1754
2765
  else if (cmpInfrequent && !cmpShared) {
1755
2766
  why.push('cmp-primary');
1756
2767
  out.name = ment.cmp;
@@ -1772,8 +2783,11 @@ function entityCmpMatch(data, entname, mdesc, why) {
1772
2783
  }
1773
2784
  }
1774
2785
  else if ('DELETE' === mdesc.method
1775
- && null == ment.cmp) {
1776
- let cmps = findcmps(data, mdesc.path, ['responses'], { uniq: true });
2786
+ && null == ment.cmp
2787
+ && !recordWraps) {
2788
+ let cmps = findcmps(data, mdesc.path, ['responses'], { uniq: true })
2789
+ .flatMap((found) => unwrapCmp(data.guide.metrics, found, why))
2790
+ .filter((found) => !genericCmp(found.cmp, data.guide.metrics.count.origcmprefs, cmpSchemaOf(data.def, found.origcmp)));
1777
2791
  if (1 === cmps.length) {
1778
2792
  out.name = cmps[0].cmp;
1779
2793
  out.orig = cmps[0].origcmp;
@@ -1791,6 +2805,105 @@ function entityCmpMatch(data, entname, mdesc, why) {
1791
2805
  (0, utility_2.debugpath)(mdesc.path, mdesc.method, 'ENTITY-CMP-NAME', mdesc.path, mdesc.method, entname + '->', out, why, ment, IS_ENTCMP_METHOD_RATE, IS_ENTCMP_PATH_RATE);
1792
2806
  return out;
1793
2807
  }
2808
+ // A tag keeps naming a route whose noun it is not when it names a literal
2809
+ // before the naming literal and the route is a leaf, or ends with a key
2810
+ // whose literal is singular: a selector or a view of the tag's resource.
2811
+ function tagHolds(data, parts, pathStr, tag) {
2812
+ const pm = matchEntityPath(parts);
2813
+ if (null == pm || VERSION_TAG_RE.test(tag)) {
2814
+ return false;
2815
+ }
2816
+ const at = pm.index + PATH_NAME_INDEX[pm.expr];
2817
+ const tagAt = parts.slice(0, at).findIndex((part) => !isParam(part) && (0, utility_1.literalName)(part) === tag);
2818
+ if (tagAt < 0) {
2819
+ return false;
2820
+ }
2821
+ const lit = (0, jostraca_2.snakify)(parts[at]);
2822
+ const singular = '' !== lit && (0, utility_2.depluralize)(lit) === lit;
2823
+ if (isParam(parts[parts.length - 1])) {
2824
+ return singular;
2825
+ }
2826
+ if (0 < (0, utility_2.findPathsWithPrefix)(data, pathStr, { strict: true })) {
2827
+ return false;
2828
+ }
2829
+ // Beneath a key a leaf views the keyed resource, unless it is a singular
2830
+ // sub-read answering an id of its own rather than projecting the literal;
2831
+ // without one it is a selector, or what the tag's verb acts over. A key
2832
+ // after another literal keys that literal's resource, not the tag's.
2833
+ const keyAt = parts.slice(0, at).map((part) => isParam(part)).lastIndexOf(true);
2834
+ if (0 <= keyAt) {
2835
+ if (parts.slice(tagAt + 1, keyAt).some((part) => !isParam(part))) {
2836
+ return false;
2837
+ }
2838
+ const schema = getResponseSchema(successResponse(data.def?.paths?.[pathStr]?.get?.responses));
2839
+ const name = (0, utility_1.literalName)(parts[at]);
2840
+ const projects = Object.keys((0, utility_2.mergedProperties)(schema) ?? {}).some((prop) => (0, utility_2.canonize)(prop) === name || (0, utility_2.canonize)(prop).startsWith(name + '_'));
2841
+ return !(singular && 'array' !== schema?.type && !projects && responsePayload(schema).record);
2842
+ }
2843
+ return singular || VERB_WORDS.has((0, utility_1.literalName)(parts[tagAt]));
2844
+ }
2845
+ // The key parameter names the component, as a bucket's key does beside the
2846
+ // literal b, and a collection through the item route beneath it.
2847
+ function keyParamNames(data, mdesc, cmp) {
2848
+ const key = keyParamOf(data.def, mdesc.path, data.work);
2849
+ return null != key && (0, utility_2.canonizeParam)(key) === cmp;
2850
+ }
2851
+ // The name of the parameter that keys a route's records: its own trailing
2852
+ // parameter, or the one the item route directly beneath a collection takes.
2853
+ function keyParamOf(def, pathStr, work) {
2854
+ const parts = (0, utility_2.guideParts)(pathStr);
2855
+ const pm = matchEntityPath(parts);
2856
+ if (null == pm) {
2857
+ return null;
2858
+ }
2859
+ const last = parts[parts.length - 1];
2860
+ if ((pm.expr.endsWith('p/') || 't/p/p' === pm.expr) && isParam(last)) {
2861
+ return (0, utility_1.paramOf)(last);
2862
+ }
2863
+ if (isParam(last)) {
2864
+ return null;
2865
+ }
2866
+ work.childKeys = work.childKeys ?? childKeyIndex(def);
2867
+ return work.childKeys[normalizeParams(pathStr)] ?? null;
2868
+ }
2869
+ function normalizeParams(pathStr) {
2870
+ return (0, utility_2.guidePath)(pathStr).replace(/\{[^}]+\}/g, '{}');
2871
+ }
2872
+ // The parameter each path takes directly beneath it, keyed by the path with
2873
+ // its parameters normalised.
2874
+ function childKeyIndex(def) {
2875
+ const index = Object.create(null);
2876
+ for (const p of Object.keys(def?.paths ?? {}).sort()) {
2877
+ const parts = (0, utility_2.guideParts)(p);
2878
+ const last = parts[parts.length - 1];
2879
+ if (1 < parts.length && isParam(last)) {
2880
+ const parent = normalizeParams('/' + parts.slice(0, -1).join('/'));
2881
+ index[parent] = index[parent] ?? (0, utility_1.paramOf)(last);
2882
+ }
2883
+ }
2884
+ return index;
2885
+ }
2886
+ // A wrapper names the component it wraps, or nothing: the candidate is
2887
+ // replaced or dropped. The replacement keeps the candidate's other fields.
2888
+ function unwrapCmp(metrics, xref, why) {
2889
+ const inner = (0, utility_2.wrapperOf)(xref.cmp, Object.keys(metrics.found.cmp).sort());
2890
+ if (null == inner) {
2891
+ return [xref];
2892
+ }
2893
+ why.push('wrapper=' + xref.cmp + ('' === inner ? '' : '>' + inner));
2894
+ if ('' === inner) {
2895
+ return [];
2896
+ }
2897
+ const orig = metrics.found.cmp[inner]?.orig ?? inner;
2898
+ const out = { ...xref, cmp: inner, origcmp: orig };
2899
+ if (null != xref.val) {
2900
+ out.val = xref.val.replace(/[^/]+$/, orig);
2901
+ }
2902
+ if (null != xref.origcmpref) {
2903
+ out.origcmpref = inner;
2904
+ }
2905
+ return [out];
2906
+ }
1794
2907
  function cmpOccursInPath(data, cmpname) {
1795
2908
  if (null == data.work.potentialCmpsFromPaths) {
1796
2909
  data.work.potentialCmpsFromPaths = Object.create(null);
@@ -1798,7 +2911,7 @@ function cmpOccursInPath(data, cmpname) {
1798
2911
  const parts = data.work.pathmap[pathstr].parts;
1799
2912
  parts
1800
2913
  .filter(p => !p.startsWith('{'))
1801
- .map(p => data.work.potentialCmpsFromPaths[(0, utility_2.canonize)(p)] = true);
2914
+ .map(p => data.work.potentialCmpsFromPaths[(0, utility_1.literalName)(p)] = true);
1802
2915
  });
1803
2916
  }
1804
2917
  return null != data.work.potentialCmpsFromPaths[cmpname];
@@ -1824,7 +2937,7 @@ function pathResource(parts, method) {
1824
2937
  if (!utility_2.READ_METHODS.includes(method) && !isParam(last) && isSingular(last)) {
1825
2938
  return null;
1826
2939
  }
1827
- return (0, utility_2.canonize)(pm[PATH_NAME_INDEX[pm.expr]]);
2940
+ return (0, utility_1.literalName)(pm[PATH_NAME_INDEX[pm.expr]]);
1828
2941
  }
1829
2942
  // The routes that keep their own path's name for the component they answer
1830
2943
  // with, each keyed `cmp METHOD path`: those of a component that several
@@ -2020,12 +3133,10 @@ function isListResponse(mdesc, pm, pathStr, why, listed) {
2020
3133
  }
2021
3134
  if (!islist && !endParamBare) {
2022
3135
  const properties = resolveSchemaProperties(schema);
2023
- (0, jostraca_1.each)(properties, (prop) => {
2024
- if (prop.type === 'array') {
2025
- why.push('array-prop:' + prop.key$);
2026
- islist = true;
2027
- }
2028
- });
3136
+ for (const key of (0, utility_1.listingProps)(properties, pathStr)) {
3137
+ why.push('array-prop:' + key);
3138
+ islist = true;
3139
+ }
2029
3140
  }
2030
3141
  if (!islist) {
2031
3142
  why.push(endParamBare ? 'end-param' : 'not-list');
@@ -2053,7 +3164,7 @@ function resolveSchemaProperties(schema) {
2053
3164
  function updateAction(methodName, oldParam, actionName, entityDesc, pathdesc, why) {
2054
3165
  if (
2055
3166
  // Entity not already encoding action.
2056
- !entityDesc.name.endsWith((0, utility_2.canonize)(actionName))
3167
+ !entityDesc.name.endsWith((0, utility_1.literalName)(actionName))
2057
3168
  && null == pathdesc.action[actionName]) {
2058
3169
  pathdesc.action[actionName] = {
2059
3170
  // kind: '`$BOOLEAN`',
@@ -2078,7 +3189,7 @@ function updateParamRename(ctx, data, path, method, paramRenameCapture, oldParam
2078
3189
  // The clash is with what the path's OTHER parameters end up called: their
2079
3190
  // rename if they have one, their canonical name if not.
2080
3191
  const otherParams = (path.match(/\{([^}]+)\}/g) || [])
2081
- .map((seg) => seg.slice(1, -1))
3192
+ .map((seg) => (0, utility_1.paramOf)(seg))
2082
3193
  .filter((name) => name !== oldParamName);
2083
3194
  const takenBy = Object.keys(paramRenameCapture.rename)
2084
3195
  .find((other) => other !== oldParamName &&
@@ -2120,21 +3231,29 @@ function updateParamRename(ctx, data, path, method, paramRenameCapture, oldParam
2120
3231
  }
2121
3232
  }
2122
3233
  function isParam(partStr) {
2123
- return '{' === partStr[0] && '}' === partStr[partStr.length - 1];
3234
+ if (null == partStr) {
3235
+ return false;
3236
+ }
3237
+ const part = partStr.replace(/^\./, '').replace(/\}\.[A-Za-z0-9]+$/, '}');
3238
+ return '{' === part[0] && '}' === part[part.length - 1];
2124
3239
  }
2125
3240
  // A literal that names one thing rather than a collection.
2126
3241
  function isSingular(part) {
2127
3242
  const lit = (0, jostraca_2.snakify)(part);
2128
3243
  return '' !== lit && (0, utility_2.depluralize)(lit) === lit;
2129
3244
  }
2130
- // A segment holding a placeholder, whole or beside other text, names no verb.
3245
+ // A segment holding a placeholder names no verb, unless the placeholder only
3246
+ // trails a literal, as a format does in `retract{format}`.
2131
3247
  function isVerb(partStr) {
2132
- return !String(partStr ?? '').includes('{');
3248
+ const part = String(partStr ?? '');
3249
+ const lit = part.replace(/(\.?\{[^}]*\})+$/, '');
3250
+ return !part.includes('{') || ('' !== lit && !lit.includes('{'));
2133
3251
  }
3252
+ const CUSTOM_METHOD_RE = /^(.+):([A-Za-z][\w-]*)$/;
2134
3253
  // A custom method (`/schedules:count`, `/users/{id}:activate`) reads as a
2135
3254
  // trailing verb segment after what it acts on.
2136
3255
  function verbParts(parts) {
2137
- const m = (parts[parts.length - 1] ?? '').match(/^(.+):([A-Za-z][\w-]*)$/);
3256
+ const m = (parts[parts.length - 1] ?? '').match(CUSTOM_METHOD_RE);
2138
3257
  return null == m ? parts : [...parts.slice(0, -1), m[1], m[2]];
2139
3258
  }
2140
3259
  // A parameter whose own name says it holds a name, such as `project_name`,
@@ -2178,7 +3297,7 @@ function entityParamNames(paths) {
2178
3297
  parts.forEach((part, partI) => {
2179
3298
  if (isParam(part)) {
2180
3299
  const place = parts.slice(0, partI + 1).join('/');
2181
- (places[place] = places[place] ?? []).push({ pathStr, param: part.slice(1, -1) });
3300
+ (places[place] = places[place] ?? []).push({ pathStr, param: (0, utility_1.paramOf)(part) });
2182
3301
  }
2183
3302
  });
2184
3303
  }
@@ -2213,7 +3332,7 @@ function entityParamNames(paths) {
2213
3332
  function paramNameTaken(pathStr, renames, param, name) {
2214
3333
  return Object.keys(renames).some((other) => other !== param && renames[other] === name) ||
2215
3334
  (pathStr.match(/\{([^}]+)\}/g) || [])
2216
- .map((seg) => seg.slice(1, -1))
3335
+ .map((seg) => (0, utility_1.paramOf)(seg))
2217
3336
  .some((other) => other !== param && null == renames[other] &&
2218
3337
  (0, utility_2.canonize)(other) === name);
2219
3338
  }