@voxgig/apidef 8.22.0 → 8.23.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.
- package/dist/apidef.d.ts +1 -1
- package/dist/apidef.js +2 -0
- package/dist/apidef.js.map +1 -1
- package/dist/guide/heuristic01.d.ts +10 -1
- package/dist/guide/heuristic01.js +249 -39
- package/dist/guide/heuristic01.js.map +1 -1
- package/dist/model.d.ts +18 -1
- package/dist/transform/args.js +27 -9
- package/dist/transform/args.js.map +1 -1
- package/dist/transform/body.d.ts +6 -0
- package/dist/transform/body.js +300 -0
- package/dist/transform/body.js.map +1 -0
- package/dist/transform/entity.d.ts +1 -1
- package/dist/transform/entity.js +13 -4
- package/dist/transform/entity.js.map +1 -1
- package/dist/transform/select.js +34 -7
- package/dist/transform/select.js.map +1 -1
- package/dist/transform/top.d.ts +1 -1
- package/dist/transform/top.js +77 -8
- package/dist/transform/top.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/types.d.ts +6 -0
- package/dist/types.js.map +1 -1
- package/dist/utility.d.ts +3 -1
- package/dist/utility.js +17 -0
- package/dist/utility.js.map +1 -1
- package/model/apidef.aontu +29 -6
- package/model/guide.aontu +8 -0
- package/package.json +2 -2
- package/src/apidef.ts +5 -0
- package/src/guide/heuristic01.ts +320 -36
- package/src/model.ts +27 -0
- package/src/transform/args.ts +35 -10
- package/src/transform/body.ts +422 -0
- package/src/transform/entity.ts +20 -4
- package/src/transform/select.ts +36 -7
- package/src/transform/top.ts +95 -12
- package/src/types.ts +6 -0
- package/src/utility.ts +21 -0
package/src/guide/heuristic01.ts
CHANGED
|
@@ -138,6 +138,7 @@ async function heuristic01(ctx: ApiDefContext): Promise<Guide> {
|
|
|
138
138
|
{ select: selectCmpXrefs, apply: MeasureRef },
|
|
139
139
|
{ select: selectAllMethods, apply: MeasureEnvelope },
|
|
140
140
|
MeasureEnvelopeItems,
|
|
141
|
+
MeasureAnswered,
|
|
141
142
|
{ select: selectAllMethods, apply: MeasureSharing },
|
|
142
143
|
MeasureShared,
|
|
143
144
|
{
|
|
@@ -164,7 +165,14 @@ async function heuristic01(ctx: ApiDefContext): Promise<Guide> {
|
|
|
164
165
|
|
|
165
166
|
guide.metrics.count.entity -= mergeCollectionPaths(guide, ctx.log,
|
|
166
167
|
(pathStr: string, methods: string[], collection: boolean) =>
|
|
167
|
-
pathRecordRef(ctx.def, pathStr, methods, collection)
|
|
168
|
+
pathRecordRef(ctx.def, pathStr, methods, collection),
|
|
169
|
+
(sharePath: string, shareMethods: string[], itemPath: string, itemMethods: string[]) =>
|
|
170
|
+
distinctShare(ctx.def, sharePath, shareMethods, itemPath, itemMethods)).length
|
|
171
|
+
|
|
172
|
+
// After the merge, which can move a path onto another entity.
|
|
173
|
+
for (const entity of Object.values(guide.entity) as GuideEntity[]) {
|
|
174
|
+
nameEntityParams(entity)
|
|
175
|
+
}
|
|
168
176
|
|
|
169
177
|
const metrics = guide.metrics
|
|
170
178
|
|
|
@@ -229,6 +237,7 @@ function Prepare(spec: TaskSpec) {
|
|
|
229
237
|
listEnvelope: {},
|
|
230
238
|
envelopePaths: {},
|
|
231
239
|
sharing: { routes: [], records: {}, yields: {} },
|
|
240
|
+
recordResources: {},
|
|
232
241
|
entity: {
|
|
233
242
|
count: {
|
|
234
243
|
seen: 0,
|
|
@@ -378,6 +387,13 @@ function MeasureEnvelopeItems(spec: TaskSpec) {
|
|
|
378
387
|
}
|
|
379
388
|
|
|
380
389
|
|
|
390
|
+
// The records some operation answers with in a 200 or 201, read through the
|
|
391
|
+
// envelopes MeasureEnvelopeItems settles.
|
|
392
|
+
function MeasureAnswered(spec: TaskSpec) {
|
|
393
|
+
spec.data.work.answered = answeredRefs(spec.data.def, spec.data.work.envelope)
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
|
|
381
397
|
function isPageAndItsItem(work: any, xrefs: string[]): boolean {
|
|
382
398
|
const pages = xrefs.filter((xref) => work.listEnvelope[xref])
|
|
383
399
|
const items = xrefs.filter((xref) => !work.listEnvelope[xref])
|
|
@@ -400,13 +416,20 @@ function MeasureSharing(spec: TaskSpec) {
|
|
|
400
416
|
const op = methodOpname(mdesc, matchEntityPath(parts), []) ?? ''
|
|
401
417
|
const sharing = work.sharing
|
|
402
418
|
|
|
403
|
-
const
|
|
419
|
+
const resource = pathResource(parts, mdesc.method)
|
|
420
|
+
|
|
421
|
+
const xrefs = findPotentialSchemaRefs(
|
|
422
|
+
mdesc.path, mdesc.method, mdesc.responses, work.envelope, work.answered, [])
|
|
404
423
|
for (const xref of xrefs) {
|
|
405
424
|
const m = xref.match(/\/(components\/schemas|definitions)\/(.+)$/)
|
|
406
425
|
if (null != m) {
|
|
407
426
|
const cmp = canonizeCmpName(m[2])
|
|
427
|
+
const record = declaresId(refSchema(spec.data.def, xref))
|
|
408
428
|
sharing.routes.push({ cmp, method: mdesc.method, path: mdesc.path, op })
|
|
409
|
-
sharing.records[cmp] = true === sharing.records[cmp] ||
|
|
429
|
+
sharing.records[cmp] = true === sharing.records[cmp] || record
|
|
430
|
+
if (record && null != resource) {
|
|
431
|
+
work.recordResources[resource] = true
|
|
432
|
+
}
|
|
410
433
|
}
|
|
411
434
|
}
|
|
412
435
|
}
|
|
@@ -492,7 +515,7 @@ function ResolveEntityComponent(spec: TaskSpec) {
|
|
|
492
515
|
let responses = methodDef.responses
|
|
493
516
|
|
|
494
517
|
let origxrefs: any[] = findPotentialSchemaRefs(
|
|
495
|
-
pathStr, methodName, responses, work.envelope, why_cmp).map(val => ({
|
|
518
|
+
pathStr, methodName, responses, work.envelope, work.answered, why_cmp).map(val => ({
|
|
496
519
|
val
|
|
497
520
|
}))
|
|
498
521
|
|
|
@@ -702,6 +725,12 @@ function ResolveEntityName(spec: TaskSpec) {
|
|
|
702
725
|
|
|
703
726
|
entname = resplitFromCmp(entname, ment.cmp as string, why_path)
|
|
704
727
|
|
|
728
|
+
const collectionEntity = itemOfCollection(data, mdesc, parts)
|
|
729
|
+
if (null != collectionEntity) {
|
|
730
|
+
why_path.push('collection-record=' + collectionEntity)
|
|
731
|
+
entname = collectionEntity
|
|
732
|
+
}
|
|
733
|
+
|
|
705
734
|
// Keep the pre-truncation name so a truncated-name collision can tell a
|
|
706
735
|
// re-encounter of the SAME origin (merge) from a genuinely different one
|
|
707
736
|
// (numeric suffix) — see ensureMinEntityName.
|
|
@@ -738,6 +767,15 @@ function ResolveEntityName(spec: TaskSpec) {
|
|
|
738
767
|
work.pathowner[pathStr] = work.pathowner[pathStr] ?? {}
|
|
739
768
|
work.pathowner[pathStr][methodName] = entname
|
|
740
769
|
|
|
770
|
+
// The entity a path's own record names, where the record carries the name
|
|
771
|
+
// the path's last segment gives.
|
|
772
|
+
const last = parts.filter((part: string) => !isParam(part)).pop()
|
|
773
|
+
work.recordowner = work.recordowner ?? {}
|
|
774
|
+
if (null == work.recordowner[pathStr] && isSchemaRef(ment.ref) &&
|
|
775
|
+
entname === ment.cmp && null != last && entname === canonize(last)) {
|
|
776
|
+
work.recordowner[pathStr] = entname
|
|
777
|
+
}
|
|
778
|
+
|
|
741
779
|
// Same guard, same reason: the formatting is the cost, not the call.
|
|
742
780
|
if (debugpathOn()) {
|
|
743
781
|
debugpath(pathStr, methodName, 'RESOLVE-ENTITY-NAME',
|
|
@@ -776,7 +814,7 @@ function RenameParams(spec: TaskSpec) {
|
|
|
776
814
|
rename: pathDesc.rename.param = (pathDesc.rename.param ?? {}),
|
|
777
815
|
why: pathDesc.why_rename.why_param = (pathDesc.why_rename.why_param ?? {}),
|
|
778
816
|
}
|
|
779
|
-
const parts = pathdesc.parts
|
|
817
|
+
const parts = verbParts(pathdesc.parts)
|
|
780
818
|
|
|
781
819
|
const applySnakeCaseRename = () => {
|
|
782
820
|
for (const part of parts) {
|
|
@@ -871,7 +909,7 @@ function RenameParams(spec: TaskSpec) {
|
|
|
871
909
|
why.push('id-parent-ent')
|
|
872
910
|
}
|
|
873
911
|
|
|
874
|
-
else {
|
|
912
|
+
else if (!isNameParam(oldParam)) {
|
|
875
913
|
updateParamRename(
|
|
876
914
|
ctx, data, pathStr, methodName, paramRenameCapture, oldParam,
|
|
877
915
|
parentName + '_id', 'parent:' + parentName)
|
|
@@ -939,7 +977,7 @@ function RenameParams(spec: TaskSpec) {
|
|
|
939
977
|
why.push('default')
|
|
940
978
|
|
|
941
979
|
let newParamName = parentName + '_id'
|
|
942
|
-
if (newParamName != oldParam) {
|
|
980
|
+
if (newParamName != oldParam && !isNameParam(oldParam)) {
|
|
943
981
|
updateParamRename(
|
|
944
982
|
ctx, data, pathStr, methodName, paramRenameCapture, oldParam,
|
|
945
983
|
newParamName, 'not-primary')
|
|
@@ -1002,7 +1040,7 @@ function FindActions(spec: TaskSpec) {
|
|
|
1002
1040
|
pathdesc.action = (pathdesc.action ?? {})
|
|
1003
1041
|
pathdesc.why_action = (pathdesc.why_action ?? {})
|
|
1004
1042
|
|
|
1005
|
-
const parts = spec.data.work.pathmap[pathStr].parts
|
|
1043
|
+
const parts = verbParts(spec.data.work.pathmap[pathStr].parts)
|
|
1006
1044
|
|
|
1007
1045
|
const fourthLastPart = parts[parts.length - 4]
|
|
1008
1046
|
const fourthLastPartCanon = canonize(fourthLastPart)
|
|
@@ -1016,8 +1054,10 @@ function FindActions(spec: TaskSpec) {
|
|
|
1016
1054
|
const cmp = ment.cmp
|
|
1017
1055
|
|
|
1018
1056
|
if (null != ment.verb_on_parent) {
|
|
1019
|
-
|
|
1020
|
-
|
|
1057
|
+
if (isVerb(lastPart)) {
|
|
1058
|
+
pathdesc.action[lastPartCanon] = pathdesc.action[lastPartCanon] ?? {
|
|
1059
|
+
why_action: ['ent', entdesc.name, 'verb-on-parent', lastPart, methodName],
|
|
1060
|
+
}
|
|
1021
1061
|
}
|
|
1022
1062
|
}
|
|
1023
1063
|
|
|
@@ -1027,7 +1067,7 @@ function FindActions(spec: TaskSpec) {
|
|
|
1027
1067
|
|| secondLastPartCanon === ment.origcmp
|
|
1028
1068
|
|| secondLastPartCanon === entname
|
|
1029
1069
|
) {
|
|
1030
|
-
if (
|
|
1070
|
+
if (isVerb(lastPart)) {
|
|
1031
1071
|
updateAction(methodName, lastPart, lastPartCanon, entdesc, pathdesc, 'no-param')
|
|
1032
1072
|
}
|
|
1033
1073
|
}
|
|
@@ -1038,7 +1078,7 @@ function FindActions(spec: TaskSpec) {
|
|
|
1038
1078
|
|| thirdLastPartCanon === ment.origcmp
|
|
1039
1079
|
|| thirdLastPartCanon === entname
|
|
1040
1080
|
) {
|
|
1041
|
-
if (isParam(secondLastPart) &&
|
|
1081
|
+
if (isParam(secondLastPart) && isVerb(lastPart)) {
|
|
1042
1082
|
updateAction(methodName, lastPart, lastPartCanon, entdesc, pathdesc,
|
|
1043
1083
|
'ent-param-2nd-last')
|
|
1044
1084
|
}
|
|
@@ -1050,7 +1090,7 @@ function FindActions(spec: TaskSpec) {
|
|
|
1050
1090
|
|| fourthLastPartCanon === ment.origcmp
|
|
1051
1091
|
|| fourthLastPartCanon === entname
|
|
1052
1092
|
) {
|
|
1053
|
-
if (isParam(thirdLastPart) &&
|
|
1093
|
+
if (isParam(thirdLastPart) && isVerb(secondLastPart) && isVerb(lastPart)) {
|
|
1054
1094
|
const oldActionName = secondLastPart + '/' + lastPart
|
|
1055
1095
|
const actionName = secondLastPartCanon + '_' + lastPartCanon
|
|
1056
1096
|
updateAction(methodName, oldActionName, actionName, entdesc, pathdesc,
|
|
@@ -1423,6 +1463,46 @@ function verbOnParent(
|
|
|
1423
1463
|
}
|
|
1424
1464
|
|
|
1425
1465
|
|
|
1466
|
+
// The entity of an item route's collection, for a method on the item route
|
|
1467
|
+
// named by its tag alone, when the route answers nothing and the tag names
|
|
1468
|
+
// another resource with a record of its own, which the item's operations
|
|
1469
|
+
// would join: the collection's record then names the item too, as GitHub's
|
|
1470
|
+
// repository invitations do beside its repositories.
|
|
1471
|
+
function itemOfCollection(
|
|
1472
|
+
data: { def: any, work: any },
|
|
1473
|
+
mdesc: any,
|
|
1474
|
+
parts: string[],
|
|
1475
|
+
): string | null {
|
|
1476
|
+
const ment = mdesc.MethodEntity
|
|
1477
|
+
if ('tag' !== ment.ref || null != ment.rescmp ||
|
|
1478
|
+
true !== data.work.recordResources[ment.cmp]) {
|
|
1479
|
+
return null
|
|
1480
|
+
}
|
|
1481
|
+
|
|
1482
|
+
const lits = parts.slice(0, parts.findIndex(isParam))
|
|
1483
|
+
if (0 === lits.length || !parts.slice(lits.length).every(isParam)) {
|
|
1484
|
+
return null
|
|
1485
|
+
}
|
|
1486
|
+
|
|
1487
|
+
const answers = Object.entries(data.def?.paths?.[mdesc.path] ?? {})
|
|
1488
|
+
.some(([method, mdef]: [string, any]) =>
|
|
1489
|
+
null != METHOD_CONSIDER_ORDER[method.toUpperCase()] &&
|
|
1490
|
+
null != getResponseSchema(successResponse(mdef?.responses)))
|
|
1491
|
+
if (answers) {
|
|
1492
|
+
return null
|
|
1493
|
+
}
|
|
1494
|
+
|
|
1495
|
+
const collection = '/' + lits.join('/')
|
|
1496
|
+
return data.work.recordowner?.[collection] ??
|
|
1497
|
+
data.work.recordowner?.[collection + '/'] ?? null
|
|
1498
|
+
}
|
|
1499
|
+
|
|
1500
|
+
|
|
1501
|
+
function isSchemaRef(ref: string | undefined): boolean {
|
|
1502
|
+
return null != ref && '' !== ref && 'tag' !== ref
|
|
1503
|
+
}
|
|
1504
|
+
|
|
1505
|
+
|
|
1426
1506
|
function isOrigCmp(data: any, name: string) {
|
|
1427
1507
|
return null != data.guide.metrics.count.origcmprefs[name]
|
|
1428
1508
|
}
|
|
@@ -1547,6 +1627,61 @@ function successSchemas(responses: any): any[] {
|
|
|
1547
1627
|
}
|
|
1548
1628
|
|
|
1549
1629
|
|
|
1630
|
+
// A read answered only by an Accepted response is named from it when another
|
|
1631
|
+
// operation answers with the same record in a 200 or 201, which shows the
|
|
1632
|
+
// record is a resource rather than the work queued.
|
|
1633
|
+
function namingSchemas(
|
|
1634
|
+
method: string,
|
|
1635
|
+
responses: any,
|
|
1636
|
+
answered: Record<string, boolean>,
|
|
1637
|
+
envelope: Record<string, string>,
|
|
1638
|
+
): any[] {
|
|
1639
|
+
const schemas = successSchemas(responses)
|
|
1640
|
+
if (0 === schemas.length && READ_METHODS.includes(method)) {
|
|
1641
|
+
const accepted = getResponseSchema(responses?.[202])
|
|
1642
|
+
if (true === answered[namingRef(accepted, envelope) ?? '']) {
|
|
1643
|
+
schemas.push(accepted)
|
|
1644
|
+
}
|
|
1645
|
+
}
|
|
1646
|
+
return schemas
|
|
1647
|
+
}
|
|
1648
|
+
|
|
1649
|
+
|
|
1650
|
+
function answeredRefs(
|
|
1651
|
+
def: any,
|
|
1652
|
+
envelope: Record<string, string>,
|
|
1653
|
+
): Record<string, boolean> {
|
|
1654
|
+
const answered: Record<string, boolean> = {}
|
|
1655
|
+
for (const pathdef of Object.values(def?.paths ?? {}) as any[]) {
|
|
1656
|
+
for (const [method, mdef] of Object.entries(pathdef ?? {}) as [string, any][]) {
|
|
1657
|
+
if (null == METHOD_CONSIDER_ORDER[method.toUpperCase()] ||
|
|
1658
|
+
null == mdef || 'object' !== typeof mdef) {
|
|
1659
|
+
continue
|
|
1660
|
+
}
|
|
1661
|
+
for (const schema of successSchemas(mdef.responses)) {
|
|
1662
|
+
const ref = namingRef(schema, envelope)
|
|
1663
|
+
if (null != ref) {
|
|
1664
|
+
answered[ref] = true
|
|
1665
|
+
}
|
|
1666
|
+
}
|
|
1667
|
+
}
|
|
1668
|
+
}
|
|
1669
|
+
return answered
|
|
1670
|
+
}
|
|
1671
|
+
|
|
1672
|
+
|
|
1673
|
+
// The component a response schema names through: the record an envelope
|
|
1674
|
+
// carries, else the schema's own component, else its array items'.
|
|
1675
|
+
function namingRef(schema: any, envelope: Record<string, string>): string | undefined {
|
|
1676
|
+
const xref = schema?.['x-ref']
|
|
1677
|
+
if (null != xref) {
|
|
1678
|
+
const itemref = envelope[xref]
|
|
1679
|
+
return null == itemref || '' === itemref ? xref : itemref
|
|
1680
|
+
}
|
|
1681
|
+
return 'array' === schema?.type ? schema.items?.['x-ref'] : undefined
|
|
1682
|
+
}
|
|
1683
|
+
|
|
1684
|
+
|
|
1550
1685
|
function getResponseSchema(response: any) {
|
|
1551
1686
|
return response?.content?.['application/json']?.schema ??
|
|
1552
1687
|
response?.schema
|
|
@@ -1560,11 +1695,12 @@ function pathRecordRef(
|
|
|
1560
1695
|
pathStr: string,
|
|
1561
1696
|
methods: string[],
|
|
1562
1697
|
collection: boolean,
|
|
1698
|
+
accepted: boolean = true,
|
|
1563
1699
|
): string | null {
|
|
1564
1700
|
const rank = (method: string) => METHOD_CONSIDER_ORDER[method] ?? Number.MAX_SAFE_INTEGER
|
|
1565
1701
|
const ordered = [...methods].sort((a, b) => rank(a) - rank(b) || byCodePoint(a, b))
|
|
1566
1702
|
for (const method of ordered) {
|
|
1567
|
-
const ref = routeRecordRef(def, pathStr, method, collection)
|
|
1703
|
+
const ref = routeRecordRef(def, pathStr, method, collection, accepted)
|
|
1568
1704
|
if (null != ref) {
|
|
1569
1705
|
return ref
|
|
1570
1706
|
}
|
|
@@ -1577,15 +1713,17 @@ function pathRecordRef(
|
|
|
1577
1713
|
// items', or the one its envelope carries, such as the job summary in Mux's
|
|
1578
1714
|
// `{ data }`. Only a collection reads a page, so beneath the collection a
|
|
1579
1715
|
// team that holds nothing but its members is a team. Null when the answer
|
|
1580
|
-
// names no component.
|
|
1716
|
+
// names no component, or is Accepted and `accepted` excludes it.
|
|
1581
1717
|
function routeRecordRef(
|
|
1582
1718
|
def: any,
|
|
1583
1719
|
pathStr: string,
|
|
1584
1720
|
method: string,
|
|
1585
1721
|
collection: boolean,
|
|
1722
|
+
accepted: boolean = true,
|
|
1586
1723
|
): string | null {
|
|
1587
|
-
const
|
|
1588
|
-
|
|
1724
|
+
const responses = def?.paths?.[pathStr]?.[method.toLowerCase()]?.responses
|
|
1725
|
+
const schema = getResponseSchema(accepted ? successResponse(responses) :
|
|
1726
|
+
responses?.[200] ?? responses?.[201])
|
|
1589
1727
|
if (null == schema || 'object' !== typeof schema) {
|
|
1590
1728
|
return null
|
|
1591
1729
|
}
|
|
@@ -1972,14 +2110,39 @@ function refSchema(def: any, xref: string): any {
|
|
|
1972
2110
|
}
|
|
1973
2111
|
|
|
1974
2112
|
|
|
1975
|
-
//
|
|
1976
|
-
//
|
|
1977
|
-
|
|
1978
|
-
|
|
2113
|
+
// Whether a share's record is apart from an item's: both declare an `id`, and
|
|
2114
|
+
// the share has no more than half of its properties in common with the item,
|
|
2115
|
+
// since a collection's list is routinely a summary of its item's record.
|
|
2116
|
+
function distinctRecord(share: any, item: any): boolean {
|
|
2117
|
+
if (!declaresId(share) || !declaresId(item)) {
|
|
1979
2118
|
return false
|
|
1980
2119
|
}
|
|
1981
|
-
const
|
|
1982
|
-
|
|
2120
|
+
const own = Object.keys(mergedProperties(share) ?? {})
|
|
2121
|
+
const theirs = mergedProperties(item) ?? {}
|
|
2122
|
+
const common = own.filter((name) => Object.prototype.hasOwnProperty.call(theirs, name))
|
|
2123
|
+
return common.length * 2 <= own.length
|
|
2124
|
+
}
|
|
2125
|
+
|
|
2126
|
+
|
|
2127
|
+
// Whether a collection share answers with a record apart from the one its item
|
|
2128
|
+
// route answers with, both read from a 200 or 201: an Accepted body may
|
|
2129
|
+
// describe the queued work rather than a resource.
|
|
2130
|
+
function distinctShare(
|
|
2131
|
+
def: any,
|
|
2132
|
+
sharePath: string,
|
|
2133
|
+
shareMethods: string[],
|
|
2134
|
+
itemPath: string,
|
|
2135
|
+
itemMethods: string[],
|
|
2136
|
+
): boolean {
|
|
2137
|
+
const share = pathRecordRef(def, sharePath, shareMethods, true, false)
|
|
2138
|
+
const item = pathRecordRef(def, itemPath, itemMethods, false, false)
|
|
2139
|
+
return null != share && null != item && share !== item &&
|
|
2140
|
+
distinctRecord(refSchema(def, share), refSchema(def, item))
|
|
2141
|
+
}
|
|
2142
|
+
|
|
2143
|
+
|
|
2144
|
+
function declaresId(schema: any): boolean {
|
|
2145
|
+
return null != mergedProperties(schema)?.id
|
|
1983
2146
|
}
|
|
1984
2147
|
|
|
1985
2148
|
|
|
@@ -2177,6 +2340,123 @@ function isParam(partStr: string) {
|
|
|
2177
2340
|
}
|
|
2178
2341
|
|
|
2179
2342
|
|
|
2343
|
+
// A segment holding a placeholder, whole or beside other text, names no verb.
|
|
2344
|
+
function isVerb(partStr: string): boolean {
|
|
2345
|
+
return !String(partStr ?? '').includes('{')
|
|
2346
|
+
}
|
|
2347
|
+
|
|
2348
|
+
|
|
2349
|
+
// A custom method (`/schedules:count`, `/users/{id}:activate`) reads as a
|
|
2350
|
+
// trailing verb segment after what it acts on.
|
|
2351
|
+
function verbParts(parts: string[]): string[] {
|
|
2352
|
+
const m = (parts[parts.length - 1] ?? '').match(/^(.+):([A-Za-z][\w-]*)$/)
|
|
2353
|
+
return null == m ? parts : [...parts.slice(0, -1), m[1], m[2]]
|
|
2354
|
+
}
|
|
2355
|
+
|
|
2356
|
+
|
|
2357
|
+
// A parameter whose own name says it holds a name, such as `project_name`,
|
|
2358
|
+
// which a derived `<parent>_id` would misdescribe.
|
|
2359
|
+
function isNameParam(param: string): boolean {
|
|
2360
|
+
return /(^|_)name$/.test(snakify(normalizeFieldName(param)))
|
|
2361
|
+
}
|
|
2362
|
+
|
|
2363
|
+
|
|
2364
|
+
function nameEntityParams(entity: GuideEntity) {
|
|
2365
|
+
const given: Record<string, Record<string, string>> = {}
|
|
2366
|
+
for (const [pathStr, guidePath] of Object.entries(entity.path ?? {})) {
|
|
2367
|
+
given[pathStr] = {}
|
|
2368
|
+
for (const [param, rename] of Object.entries(guidePath.rename?.param ?? {})) {
|
|
2369
|
+
given[pathStr][param] = rename.target
|
|
2370
|
+
}
|
|
2371
|
+
}
|
|
2372
|
+
|
|
2373
|
+
const named = entityParamNames(given)
|
|
2374
|
+
|
|
2375
|
+
for (const [pathStr, guidePath] of Object.entries(entity.path ?? {})) {
|
|
2376
|
+
guidePath.rename = guidePath.rename ?? { param: {} }
|
|
2377
|
+
const renames = guidePath.rename.param = guidePath.rename.param ?? {}
|
|
2378
|
+
for (const param of Object.keys(renames)) {
|
|
2379
|
+
if (null == named[pathStr][param]) {
|
|
2380
|
+
delete renames[param]
|
|
2381
|
+
}
|
|
2382
|
+
}
|
|
2383
|
+
for (const [param, target] of Object.entries(named[pathStr])) {
|
|
2384
|
+
if (target !== renames[param]?.target) {
|
|
2385
|
+
renames[param] = { target, why_rename: ['entity-param'] }
|
|
2386
|
+
}
|
|
2387
|
+
}
|
|
2388
|
+
}
|
|
2389
|
+
}
|
|
2390
|
+
|
|
2391
|
+
|
|
2392
|
+
// An entity's paths with the renames of their parameters, given back with one
|
|
2393
|
+
// name for each parameter at the same place in several paths: its own name,
|
|
2394
|
+
// snake-cased as the paths would, unless some path makes it the entity's `id`.
|
|
2395
|
+
function entityParamNames(
|
|
2396
|
+
paths: Record<string, Record<string, string>>
|
|
2397
|
+
): Record<string, Record<string, string>> {
|
|
2398
|
+
const out: Record<string, Record<string, string>> = {}
|
|
2399
|
+
const places: Record<string, { pathStr: string, param: string }[]> = {}
|
|
2400
|
+
|
|
2401
|
+
for (const pathStr of Object.keys(paths).sort(byCodePoint)) {
|
|
2402
|
+
out[pathStr] = { ...paths[pathStr] }
|
|
2403
|
+
const parts = verbParts(pathStr.split('/').filter((p) => '' !== p))
|
|
2404
|
+
parts.forEach((part, partI) => {
|
|
2405
|
+
if (isParam(part)) {
|
|
2406
|
+
const place = parts.slice(0, partI + 1).join('/')
|
|
2407
|
+
;(places[place] = places[place] ?? []).push({ pathStr, param: part.slice(1, -1) })
|
|
2408
|
+
}
|
|
2409
|
+
})
|
|
2410
|
+
}
|
|
2411
|
+
|
|
2412
|
+
for (const place of Object.keys(places).sort(byCodePoint)) {
|
|
2413
|
+
const uses = places[place]
|
|
2414
|
+
const names = uses.map((use) => out[use.pathStr][use.param] ?? use.param)
|
|
2415
|
+
const param = uses[0].param
|
|
2416
|
+
const snake = depluralize(snakify(normalizeFieldName(param)))
|
|
2417
|
+
const name = names.includes('id') ? null :
|
|
2418
|
+
names.includes(snake) ? snake :
|
|
2419
|
+
names.includes(param) ? param : null
|
|
2420
|
+
|
|
2421
|
+
if (null == name || names.every((n) => n === name)) {
|
|
2422
|
+
continue
|
|
2423
|
+
}
|
|
2424
|
+
|
|
2425
|
+
for (const use of uses) {
|
|
2426
|
+
const renames = out[use.pathStr]
|
|
2427
|
+
if (paramNameTaken(use.pathStr, renames, use.param, name)) {
|
|
2428
|
+
continue
|
|
2429
|
+
}
|
|
2430
|
+
if (name === use.param) {
|
|
2431
|
+
delete renames[use.param]
|
|
2432
|
+
}
|
|
2433
|
+
else {
|
|
2434
|
+
renames[use.param] = name
|
|
2435
|
+
}
|
|
2436
|
+
}
|
|
2437
|
+
}
|
|
2438
|
+
|
|
2439
|
+
return out
|
|
2440
|
+
}
|
|
2441
|
+
|
|
2442
|
+
|
|
2443
|
+
// As updateParamRename judges a clash: by what the path's other parameters
|
|
2444
|
+
// end up called.
|
|
2445
|
+
function paramNameTaken(
|
|
2446
|
+
pathStr: string,
|
|
2447
|
+
renames: Record<string, string>,
|
|
2448
|
+
param: string,
|
|
2449
|
+
name: string,
|
|
2450
|
+
): boolean {
|
|
2451
|
+
return Object.keys(renames).some((other) =>
|
|
2452
|
+
other !== param && renames[other] === name) ||
|
|
2453
|
+
(pathStr.match(/\{([^}]+)\}/g) || [])
|
|
2454
|
+
.map((seg) => seg.slice(1, -1))
|
|
2455
|
+
.some((other) => other !== param && null == renames[other] &&
|
|
2456
|
+
canonize(other) === name)
|
|
2457
|
+
}
|
|
2458
|
+
|
|
2459
|
+
|
|
2180
2460
|
|
|
2181
2461
|
|
|
2182
2462
|
function findcmps(
|
|
@@ -2238,24 +2518,19 @@ function findPotentialSchemaRefs(
|
|
|
2238
2518
|
methodName: string,
|
|
2239
2519
|
responses: any,
|
|
2240
2520
|
envelope: Record<string, string>,
|
|
2521
|
+
answered: Record<string, boolean>,
|
|
2241
2522
|
why: string[],
|
|
2242
2523
|
) {
|
|
2243
2524
|
const xrefs: string[] = []
|
|
2244
|
-
for (const schema of
|
|
2245
|
-
|
|
2246
|
-
|
|
2247
|
-
|
|
2248
|
-
|
|
2249
|
-
if ('' !==
|
|
2525
|
+
for (const schema of namingSchemas(methodName, responses, answered, envelope)) {
|
|
2526
|
+
// An envelope component names its wrapping, not the entity: the
|
|
2527
|
+
// component it carries takes its place.
|
|
2528
|
+
const ref = namingRef(schema, envelope)
|
|
2529
|
+
if (null != ref) {
|
|
2530
|
+
if (null != schema['x-ref'] && ref !== schema['x-ref']) {
|
|
2250
2531
|
why.push('envelope=' + cmpRefName(schema['x-ref']))
|
|
2251
|
-
xrefs.push(itemref)
|
|
2252
2532
|
}
|
|
2253
|
-
|
|
2254
|
-
xrefs.push(schema['x-ref'])
|
|
2255
|
-
}
|
|
2256
|
-
}
|
|
2257
|
-
else if ('array' === schema.type && null != schema.items?.['x-ref']) {
|
|
2258
|
-
xrefs.push(schema.items?.['x-ref'])
|
|
2533
|
+
xrefs.push(ref)
|
|
2259
2534
|
}
|
|
2260
2535
|
}
|
|
2261
2536
|
|
|
@@ -2285,7 +2560,16 @@ function hasMethod(def: any, pathStr: string, methodName: string) {
|
|
|
2285
2560
|
|
|
2286
2561
|
|
|
2287
2562
|
export {
|
|
2563
|
+
answeredRefs,
|
|
2564
|
+
distinctRecord,
|
|
2565
|
+
distinctShare,
|
|
2288
2566
|
heuristic01,
|
|
2567
|
+
namingRef,
|
|
2568
|
+
namingSchemas,
|
|
2289
2569
|
pathResource,
|
|
2290
2570
|
sharedRoutes,
|
|
2571
|
+
entityParamNames,
|
|
2572
|
+
isNameParam,
|
|
2573
|
+
isVerb,
|
|
2574
|
+
verbParts,
|
|
2291
2575
|
}
|
package/src/model.ts
CHANGED
|
@@ -129,6 +129,28 @@ type ModelPathSegment = {
|
|
|
129
129
|
}
|
|
130
130
|
|
|
131
131
|
|
|
132
|
+
type BodyKind = 'json' | 'raw' | 'multipart' | 'form'
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
type ModelBodyField = {
|
|
136
|
+
name: string
|
|
137
|
+
binary?: boolean
|
|
138
|
+
list?: boolean
|
|
139
|
+
join?: string
|
|
140
|
+
media?: string
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
// One media type of a request body or a success response.
|
|
145
|
+
type ModelBody = {
|
|
146
|
+
kind: BodyKind
|
|
147
|
+
media: string
|
|
148
|
+
binary?: boolean
|
|
149
|
+
fields?: ModelBodyField[]
|
|
150
|
+
alternatives?: ModelBody[]
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
|
|
132
154
|
type ModelPoint = {
|
|
133
155
|
co?: { version: 2, id: string, source: string }
|
|
134
156
|
li?: boolean | Record<string, any>
|
|
@@ -157,6 +179,8 @@ type ModelPoint = {
|
|
|
157
179
|
exist: string[]
|
|
158
180
|
$action?: string
|
|
159
181
|
}
|
|
182
|
+
rb?: ModelBody
|
|
183
|
+
rs?: ModelBody
|
|
160
184
|
}
|
|
161
185
|
|
|
162
186
|
|
|
@@ -250,6 +274,9 @@ export type {
|
|
|
250
274
|
ModelArg,
|
|
251
275
|
ModelPoint,
|
|
252
276
|
ModelPathSegment,
|
|
277
|
+
BodyKind,
|
|
278
|
+
ModelBodyField,
|
|
279
|
+
ModelBody,
|
|
253
280
|
ModelOp,
|
|
254
281
|
ModelEntity,
|
|
255
282
|
ModelEntityFlow,
|
package/src/transform/args.ts
CHANGED
|
@@ -4,7 +4,14 @@ import { each, snakify } from 'jostraca'
|
|
|
4
4
|
|
|
5
5
|
import type { TransformResult, Transform } from '../transform'
|
|
6
6
|
|
|
7
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
depluralize,
|
|
9
|
+
canonizeParam,
|
|
10
|
+
inferFieldType,
|
|
11
|
+
normalizeFieldName,
|
|
12
|
+
paramName,
|
|
13
|
+
validator,
|
|
14
|
+
} from '../utility'
|
|
8
15
|
|
|
9
16
|
|
|
10
17
|
import { KIT } from '../types'
|
|
@@ -113,6 +120,7 @@ function resolveArgs(
|
|
|
113
120
|
ment: ModelEntity, mop: ModelOp, mpoint: ModelPoint, argdefs: ParameterDef[]
|
|
114
121
|
) {
|
|
115
122
|
const touchedKeys = new Set<string>()
|
|
123
|
+
const placeholders = [...String(mpoint.o ?? '').matchAll(/\{([^}]+)\}/g)].map((m) => m[1])
|
|
116
124
|
|
|
117
125
|
each(argdefs, (argdef: ParameterDef) => {
|
|
118
126
|
const specName = normalizeFieldName(argdef.name)
|
|
@@ -132,12 +140,30 @@ function resolveArgs(
|
|
|
132
140
|
return
|
|
133
141
|
}
|
|
134
142
|
|
|
135
|
-
|
|
143
|
+
let kind = ARG_KIND[argdef.in] ?? 'query'
|
|
144
|
+
let placed = false
|
|
145
|
+
const where: unknown = argdef.in
|
|
146
|
+
if ('string' !== typeof where || '' === where) {
|
|
147
|
+
placed = placeholders.includes(argdef.name)
|
|
148
|
+
kind = placed ? 'param' : 'query'
|
|
149
|
+
ctx?.warn?.({
|
|
150
|
+
note: `Parameter ${argdef.name} on entity=${ment.name} op=${mop.name}` +
|
|
151
|
+
` path=${mpoint.o} has no \`in\`` +
|
|
152
|
+
(placed ? `; it names the path placeholder {${argdef.name}}, so it is taken as` +
|
|
153
|
+
' a path parameter.' : ', so it is taken as a query parameter.') +
|
|
154
|
+
' A parameter needs an `in`.',
|
|
155
|
+
entity: ment.name,
|
|
156
|
+
path: mpoint.o,
|
|
157
|
+
op: mop.name,
|
|
158
|
+
param: argdef.name,
|
|
159
|
+
})
|
|
160
|
+
}
|
|
136
161
|
// Rename map can be keyed by either the spec original (camelCase) or by
|
|
137
162
|
// the snakified form depending on which path went through heuristic01.
|
|
138
163
|
// Try both before falling through to `orig`.
|
|
139
164
|
const renameMap = mpoint.r[kind]
|
|
140
|
-
const name =
|
|
165
|
+
const name = placed ? paramName(argdef.name, mpoint.r.param) :
|
|
166
|
+
(renameMap?.[specName] ?? renameMap?.[orig] ?? orig)
|
|
141
167
|
// The name the definition gives, which the SDK sends on the wire. The
|
|
142
168
|
// model name beside it is only what a caller writes.
|
|
143
169
|
const marg: ModelArg = {
|
|
@@ -145,7 +171,7 @@ function resolveArgs(
|
|
|
145
171
|
or: String(argdef.name),
|
|
146
172
|
t: inferFieldType(name, validator(argdef.schema?.type)),
|
|
147
173
|
k: kind,
|
|
148
|
-
r: !!argdef.required
|
|
174
|
+
r: placed || !!argdef.required
|
|
149
175
|
}
|
|
150
176
|
|
|
151
177
|
const example = resolveArgExample(argdef)
|
|
@@ -167,12 +193,11 @@ function resolveArgs(
|
|
|
167
193
|
// DELETE /call/{id}, still takes a value: it gets a required string
|
|
168
194
|
// argument under its own name, and a warning.
|
|
169
195
|
if ('graphql' !== mpoint.k) {
|
|
170
|
-
const
|
|
171
|
-
|
|
172
|
-
for (const
|
|
173
|
-
const orig =
|
|
174
|
-
const
|
|
175
|
-
const name = renameMap?.[wire] ?? renameMap?.[normalizeFieldName(wire)] ?? renameMap?.[orig] ?? orig
|
|
196
|
+
const declared = new Set((mpoint.g.params ?? [])
|
|
197
|
+
.map((arg: ModelArg) => canonizeParam(String(arg.or))))
|
|
198
|
+
for (const wire of placeholders) {
|
|
199
|
+
const orig = canonizeParam(wire)
|
|
200
|
+
const name = paramName(wire, mpoint.r.param)
|
|
176
201
|
// A declared parameter the placeholder is renamed to already fills it.
|
|
177
202
|
if ('' === orig || declared.has(orig) ||
|
|
178
203
|
(mpoint.g.params ?? []).some((arg: ModelArg) => arg.n === name)) continue
|