@voxgig/apidef 8.25.2 → 8.25.4
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/desc.d.ts +3 -0
- package/dist/guide/heuristic01.js +141 -37
- package/dist/guide/heuristic01.js.map +1 -1
- package/dist/transform/args.js +4 -8
- package/dist/transform/args.js.map +1 -1
- package/dist/transform/body.d.ts +1 -2
- package/dist/transform/body.js +19 -14
- package/dist/transform/body.js.map +1 -1
- package/dist/transform/field.js +13 -1
- package/dist/transform/field.js.map +1 -1
- package/dist/transform/flowstep.js +5 -5
- package/dist/transform/flowstep.js.map +1 -1
- package/dist/transform/graphql.d.ts +2 -1
- package/dist/transform/graphql.js +13 -9
- package/dist/transform/graphql.js.map +1 -1
- package/dist/transform/top.js +34 -1
- package/dist/transform/top.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/utility.d.ts +3 -1
- package/dist/utility.js +31 -5
- package/dist/utility.js.map +1 -1
- package/package.json +1 -1
- package/src/desc.ts +4 -0
- package/src/guide/heuristic01.ts +163 -38
- package/src/transform/args.ts +5 -8
- package/src/transform/body.ts +24 -17
- package/src/transform/field.ts +18 -3
- package/src/transform/flowstep.ts +5 -5
- package/src/transform/graphql.ts +19 -9
- package/src/transform/top.ts +41 -1
- package/src/utility.ts +37 -5
package/src/guide/heuristic01.ts
CHANGED
|
@@ -13,7 +13,7 @@ import {
|
|
|
13
13
|
itemEnvelopeTransform,
|
|
14
14
|
} from '../utility'
|
|
15
15
|
|
|
16
|
-
import {
|
|
16
|
+
import { requestSchema } from '../transform/body'
|
|
17
17
|
|
|
18
18
|
|
|
19
19
|
import {
|
|
@@ -139,7 +139,7 @@ async function heuristic01(ctx: ApiDefContext): Promise<Guide> {
|
|
|
139
139
|
{
|
|
140
140
|
select: 'def.paths', apply: [
|
|
141
141
|
MeasurePath,
|
|
142
|
-
{ select:
|
|
142
|
+
{ select: selectPathItemMembers, apply: MeasureMethod },
|
|
143
143
|
PreparePath
|
|
144
144
|
]
|
|
145
145
|
},
|
|
@@ -161,7 +161,7 @@ async function heuristic01(ctx: ApiDefContext): Promise<Guide> {
|
|
|
161
161
|
// ShowNode,
|
|
162
162
|
]
|
|
163
163
|
},
|
|
164
|
-
{ select: 'work.entmap', apply: BuildEntity }
|
|
164
|
+
{ select: 'work.entmap', apply: [VersionActions, BuildEntity] }
|
|
165
165
|
])
|
|
166
166
|
|
|
167
167
|
const result = analysis.execSync(ctx, {})
|
|
@@ -216,9 +216,12 @@ function ShowNode(spec: TaskSpec) {
|
|
|
216
216
|
|
|
217
217
|
|
|
218
218
|
function Prepare(spec: TaskSpec) {
|
|
219
|
+
// Keyed by names from the definition, so no key may find an Object.prototype member.
|
|
220
|
+
const named = () => Object.create(null)
|
|
221
|
+
|
|
219
222
|
const guide: Guide = {
|
|
220
223
|
control: {},
|
|
221
|
-
entity:
|
|
224
|
+
entity: named(),
|
|
222
225
|
metrics: {
|
|
223
226
|
count: {
|
|
224
227
|
path: 0,
|
|
@@ -227,11 +230,11 @@ function Prepare(spec: TaskSpec) {
|
|
|
227
230
|
tag: 0,
|
|
228
231
|
cmp: 0,
|
|
229
232
|
entity: 0,
|
|
230
|
-
origcmprefs:
|
|
233
|
+
origcmprefs: named(),
|
|
231
234
|
},
|
|
232
235
|
found: {
|
|
233
|
-
tag:
|
|
234
|
-
cmp:
|
|
236
|
+
tag: named(),
|
|
237
|
+
cmp: named(),
|
|
235
238
|
}
|
|
236
239
|
},
|
|
237
240
|
}
|
|
@@ -240,19 +243,19 @@ function Prepare(spec: TaskSpec) {
|
|
|
240
243
|
def: spec.ctx.def,
|
|
241
244
|
guide,
|
|
242
245
|
work: {
|
|
243
|
-
pathmap:
|
|
244
|
-
entmap:
|
|
245
|
-
envelope:
|
|
246
|
-
listEnvelope:
|
|
247
|
-
envelopePaths:
|
|
248
|
-
sharing: { routes: [], records:
|
|
249
|
-
recordResources:
|
|
250
|
-
tagItems:
|
|
251
|
-
claims:
|
|
252
|
-
claimowner:
|
|
253
|
-
claimrecord:
|
|
254
|
-
pathowner:
|
|
255
|
-
recordowner:
|
|
246
|
+
pathmap: named(),
|
|
247
|
+
entmap: named(),
|
|
248
|
+
envelope: named(),
|
|
249
|
+
listEnvelope: named(),
|
|
250
|
+
envelopePaths: named(),
|
|
251
|
+
sharing: { routes: [], records: named(), yields: named() },
|
|
252
|
+
recordResources: named(),
|
|
253
|
+
tagItems: named(),
|
|
254
|
+
claims: named(),
|
|
255
|
+
claimowner: named(),
|
|
256
|
+
claimrecord: named(),
|
|
257
|
+
pathowner: named(),
|
|
258
|
+
recordowner: named(),
|
|
256
259
|
entity: {
|
|
257
260
|
count: {
|
|
258
261
|
seen: 0,
|
|
@@ -270,6 +273,10 @@ function MeasurePath(spec: TaskSpec) {
|
|
|
270
273
|
const metrics = guide.metrics
|
|
271
274
|
const pathdef = spec.node.val
|
|
272
275
|
|
|
276
|
+
if (!isPathItem(spec.node.key, pathdef)) {
|
|
277
|
+
return
|
|
278
|
+
}
|
|
279
|
+
|
|
273
280
|
metrics.count.path++
|
|
274
281
|
|
|
275
282
|
metrics.count.method += (
|
|
@@ -314,6 +321,10 @@ function PreparePath(spec: TaskSpec) {
|
|
|
314
321
|
const pathstr = spec.node.key
|
|
315
322
|
const pathdef = spec.node.val
|
|
316
323
|
|
|
324
|
+
if (!isPathItem(pathstr, pathdef)) {
|
|
325
|
+
return
|
|
326
|
+
}
|
|
327
|
+
|
|
317
328
|
const pathdesc = {
|
|
318
329
|
path: pathstr,
|
|
319
330
|
def: pathdef,
|
|
@@ -325,6 +336,19 @@ function PreparePath(spec: TaskSpec) {
|
|
|
325
336
|
}
|
|
326
337
|
|
|
327
338
|
|
|
339
|
+
// A vendor extension beside the paths is no path, as apidef reads no `x-` key.
|
|
340
|
+
function isPathItem(key: string, pathdef: any): boolean {
|
|
341
|
+
return !String(key).startsWith('x-') &&
|
|
342
|
+
null != pathdef && 'object' === typeof pathdef && !Array.isArray(pathdef)
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
function selectPathItemMembers(pathdef: any, spec: TaskSpec) {
|
|
347
|
+
return isPathItem(spec.node.key, pathdef) ?
|
|
348
|
+
sortedKeys(pathdef).map((key: string) => pathdef[key]) : []
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
|
|
328
352
|
function selectCmpXrefs(_source: any, spec: TaskSpec) {
|
|
329
353
|
const counts = countRefs(spec.ctx.def)
|
|
330
354
|
return Object.keys(counts)
|
|
@@ -388,7 +412,7 @@ function MeasureEnvelope(spec: TaskSpec) {
|
|
|
388
412
|
function MeasureEnvelopeItems(spec: TaskSpec) {
|
|
389
413
|
const work = spec.data.work
|
|
390
414
|
const envelope: Record<string, string> = work.envelope
|
|
391
|
-
const carriers: Record<string, string[]> =
|
|
415
|
+
const carriers: Record<string, string[]> = Object.create(null)
|
|
392
416
|
for (const xref of Object.keys(envelope)) {
|
|
393
417
|
if ('' !== envelope[xref]) {
|
|
394
418
|
(carriers[envelope[xref]] = carriers[envelope[xref]] ?? []).push(xref)
|
|
@@ -471,6 +495,9 @@ function selectAllMethods(_source: any, spec: TaskSpec): MethodDesc[] {
|
|
|
471
495
|
let caught: any = { methods: [] }
|
|
472
496
|
|
|
473
497
|
for (const [path, pdef] of sortedEntries(ctx.def.paths)) {
|
|
498
|
+
if (!isPathItem(path, pdef)) {
|
|
499
|
+
continue
|
|
500
|
+
}
|
|
474
501
|
for (const [m, mdef] of sortedEntries(pdef)) {
|
|
475
502
|
const method = m.toUpperCase()
|
|
476
503
|
|
|
@@ -492,6 +519,7 @@ function selectAllMethods(_source: any, spec: TaskSpec): MethodDesc[] {
|
|
|
492
519
|
// Carried so authExchangeOp can see a per-operation `security: []`.
|
|
493
520
|
// Everything downstream ignores it.
|
|
494
521
|
security: mdef.security,
|
|
522
|
+
deprecated: true === mdef.deprecated,
|
|
495
523
|
})
|
|
496
524
|
}
|
|
497
525
|
}
|
|
@@ -761,7 +789,8 @@ function ResolveEntityName(spec: TaskSpec) {
|
|
|
761
789
|
|
|
762
790
|
const pm = matchEntityPath(parts)
|
|
763
791
|
|
|
764
|
-
let entname =
|
|
792
|
+
let entname = samePathOwner(mdesc, work, why_path)
|
|
793
|
+
?? pathEntityName(data, mdesc, parts, pm, work.pathowner, why_path)
|
|
765
794
|
if (null == entname) {
|
|
766
795
|
work.entity.count.unresolved++
|
|
767
796
|
entname = 'entity' + work.entity.count.unresolved
|
|
@@ -824,6 +853,34 @@ function ResolveEntityName(spec: TaskSpec) {
|
|
|
824
853
|
}
|
|
825
854
|
|
|
826
855
|
|
|
856
|
+
// A method named only from its tag, as a DELETE answering 204 is, joins the
|
|
857
|
+
// entity a read of the same path resolved to when that read carries the same
|
|
858
|
+
// tag: the tag then groups the path's routes and names no other resource. A
|
|
859
|
+
// tag the read does not carry, Admin on the delete of a team, keeps its name.
|
|
860
|
+
function samePathOwner(
|
|
861
|
+
mdesc: MethodDesc,
|
|
862
|
+
work: any,
|
|
863
|
+
why: string[],
|
|
864
|
+
): string | null {
|
|
865
|
+
const ment = mdesc.MethodEntity
|
|
866
|
+
const owner = work.pathowner[mdesc.path]
|
|
867
|
+
if (null == owner || 'tag' !== ment?.ref) {
|
|
868
|
+
return null
|
|
869
|
+
}
|
|
870
|
+
const tags: string[] = mdesc.tags ?? []
|
|
871
|
+
for (const method of ['GET', 'QUERY', 'PUT', 'PATCH']) {
|
|
872
|
+
const entname = owner[method]
|
|
873
|
+
const read = work.methods.find((m: MethodDesc) =>
|
|
874
|
+
m.path === mdesc.path && m.method === method)
|
|
875
|
+
if (null != entname && (read?.tags ?? []).some((tag: string) => tags.includes(tag))) {
|
|
876
|
+
why.push('same-path-tag=' + entname + '/' + method)
|
|
877
|
+
return entname
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
return null
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
|
|
827
884
|
// The name a method's path shape and component give it, or null where neither
|
|
828
885
|
// does. A verb on a parent takes the name pathowner holds for the parent.
|
|
829
886
|
function pathEntityName(
|
|
@@ -1242,7 +1299,11 @@ function ResolveOperation(spec: TaskSpec) {
|
|
|
1242
1299
|
entdesc.authexchange_ops = (entdesc.authexchange_ops ?? 0) + 1
|
|
1243
1300
|
}
|
|
1244
1301
|
|
|
1245
|
-
const
|
|
1302
|
+
const pathdesc = entdesc.path[pathStr]
|
|
1303
|
+
pathdesc.ops = (pathdesc.ops ?? 0) + 1
|
|
1304
|
+
pathdesc.deprecated_ops = (pathdesc.deprecated_ops ?? 0) + (true === mdesc.deprecated ? 1 : 0)
|
|
1305
|
+
|
|
1306
|
+
const op = pathdesc.op
|
|
1246
1307
|
|
|
1247
1308
|
const opdef = {
|
|
1248
1309
|
method: methodName,
|
|
@@ -1347,7 +1408,7 @@ function ResolveTransform(spec: TaskSpec) {
|
|
|
1347
1408
|
}
|
|
1348
1409
|
}
|
|
1349
1410
|
|
|
1350
|
-
const reqschema = getRequestBodySchema(mdesc
|
|
1411
|
+
const reqschema = getRequestBodySchema(spec.data.def, mdesc)
|
|
1351
1412
|
debugpath(pathStr, methodName, 'TRANSFORM-REQ', keysof(reqschema?.properties))
|
|
1352
1413
|
transform.req = bodyRequestTransform(reqschema, [entdesc.origname, entdesc.name])
|
|
1353
1414
|
|
|
@@ -1358,6 +1419,58 @@ function ResolveTransform(spec: TaskSpec) {
|
|
|
1358
1419
|
|
|
1359
1420
|
|
|
1360
1421
|
|
|
1422
|
+
// An API that keeps older versions of a route beside the current one gives
|
|
1423
|
+
// every version the same selector, so only the first would ever be reached.
|
|
1424
|
+
// The highest version with an operation the spec does not deprecate is the
|
|
1425
|
+
// route; each other version becomes an action named for it (v1, v2), or
|
|
1426
|
+
// suffixes the action its path already has, so it stays reachable.
|
|
1427
|
+
function VersionActions(spec: TaskSpec) {
|
|
1428
|
+
const entdesc: EntityDesc = spec.node.val
|
|
1429
|
+
|
|
1430
|
+
const groups: Record<string, { path: string, version: number }[]> = {}
|
|
1431
|
+
for (const pathStr of sortedKeys(entdesc.path)) {
|
|
1432
|
+
const vm = pathStr.match(/\/v(\d+)(?=\/|$)/)
|
|
1433
|
+
if (null == vm) {
|
|
1434
|
+
continue
|
|
1435
|
+
}
|
|
1436
|
+
const key = pathStr.replace(/\/v\d+(?=\/|$)/, '/v')
|
|
1437
|
+
groups[key] = groups[key] ?? []
|
|
1438
|
+
groups[key].push({ path: pathStr, version: Number(vm[1]) })
|
|
1439
|
+
}
|
|
1440
|
+
|
|
1441
|
+
for (const members of Object.values(groups)) {
|
|
1442
|
+
if (members.length < 2) {
|
|
1443
|
+
continue
|
|
1444
|
+
}
|
|
1445
|
+
const live = members.filter((m) => {
|
|
1446
|
+
const pathdesc = entdesc.path[m.path]
|
|
1447
|
+
return (pathdesc.deprecated_ops ?? 0) < (pathdesc.ops ?? 0)
|
|
1448
|
+
})
|
|
1449
|
+
const route = (0 < live.length ? live : members)
|
|
1450
|
+
.reduce((a, b) => b.version > a.version ? b : a)
|
|
1451
|
+
|
|
1452
|
+
for (const m of members) {
|
|
1453
|
+
if (m === route) {
|
|
1454
|
+
continue
|
|
1455
|
+
}
|
|
1456
|
+
const pathdesc = entdesc.path[m.path]
|
|
1457
|
+
const vname = 'v' + m.version
|
|
1458
|
+
const why = ['ent', entdesc.name, 'version', vname, 'behind', route.path]
|
|
1459
|
+
const actions = pathdesc.action ?? {}
|
|
1460
|
+
pathdesc.action = {}
|
|
1461
|
+
for (const actname of sortedKeys(actions)) {
|
|
1462
|
+
pathdesc.action[actname + '_' + vname] = {
|
|
1463
|
+
why_action: [...actions[actname].why_action, ...why],
|
|
1464
|
+
}
|
|
1465
|
+
}
|
|
1466
|
+
if (0 === sortedKeys(actions).length) {
|
|
1467
|
+
pathdesc.action[vname] = { why_action: why }
|
|
1468
|
+
}
|
|
1469
|
+
}
|
|
1470
|
+
}
|
|
1471
|
+
}
|
|
1472
|
+
|
|
1473
|
+
|
|
1361
1474
|
function BuildEntity(spec: TaskSpec) {
|
|
1362
1475
|
const entdesc = spec.node.val
|
|
1363
1476
|
|
|
@@ -1691,7 +1804,7 @@ function outsideCollection(path: string, collection: string): boolean {
|
|
|
1691
1804
|
// Two of a tag's collections whose item routes take the same parameters, or
|
|
1692
1805
|
// that are both read, would share a selector on the tag's entity.
|
|
1693
1806
|
function tagCollides(def: any, items: Record<string, Record<string, boolean>>): boolean {
|
|
1694
|
-
const seen: Record<string, boolean> =
|
|
1807
|
+
const seen: Record<string, boolean> = Object.create(null)
|
|
1695
1808
|
let reads = 0
|
|
1696
1809
|
for (const [collection, params] of Object.entries(items)) {
|
|
1697
1810
|
for (const key of Object.keys(params)) {
|
|
@@ -1821,8 +1934,9 @@ function entityPathMatch_tpp(
|
|
|
1821
1934
|
}
|
|
1822
1935
|
|
|
1823
1936
|
|
|
1824
|
-
|
|
1825
|
-
|
|
1937
|
+
// The JSON body a method sends, a Swagger 2 body parameter included.
|
|
1938
|
+
function getRequestBodySchema(def: any, mdesc: any) {
|
|
1939
|
+
return requestSchema(def, mdesc.method, mdesc.path) ?? mdesc.requestBody?.schema
|
|
1826
1940
|
}
|
|
1827
1941
|
|
|
1828
1942
|
// The response an operation's result is read from, down to an Accepted
|
|
@@ -1866,7 +1980,7 @@ function answeredRefs(
|
|
|
1866
1980
|
def: any,
|
|
1867
1981
|
envelope: Record<string, string>,
|
|
1868
1982
|
): Record<string, boolean> {
|
|
1869
|
-
const answered: Record<string, boolean> =
|
|
1983
|
+
const answered: Record<string, boolean> = Object.create(null)
|
|
1870
1984
|
for (const pathdef of Object.values(def?.paths ?? {}) as any[]) {
|
|
1871
1985
|
for (const [method, mdef] of Object.entries(pathdef ?? {}) as [string, any][]) {
|
|
1872
1986
|
if (null == METHOD_CONSIDER_ORDER[method.toUpperCase()] ||
|
|
@@ -2006,8 +2120,7 @@ function probableEntityMethod(
|
|
|
2006
2120
|
pm: PathMatch,
|
|
2007
2121
|
why: string[]
|
|
2008
2122
|
) {
|
|
2009
|
-
const
|
|
2010
|
-
const reqSchema = getRequestBodySchema(request)
|
|
2123
|
+
const reqSchema = getRequestBodySchema(data.def, mdesc)
|
|
2011
2124
|
|
|
2012
2125
|
const response = mdesc.responses?.['201'] || mdesc.responses?.['200']
|
|
2013
2126
|
const resSchema = getResponseSchema(response)
|
|
@@ -2096,6 +2209,11 @@ function entityCmpMatch(
|
|
|
2096
2209
|
const cmpShared =
|
|
2097
2210
|
true === data.work.sharing.yields[ment.origcmpref + ' ' + mdesc.method + ' ' + mdesc.path]
|
|
2098
2211
|
|
|
2212
|
+
// CallRouteOptionV2 answering /call_routes/{id}/options/{id}: the record
|
|
2213
|
+
// names the path's resource, qualified by its parent and versioned.
|
|
2214
|
+
const unversioned = (ment.cmp ?? '').replace(/_v\d+$/, '')
|
|
2215
|
+
const qualifies = unversioned !== ment.cmp && unversioned.endsWith('_' + entname)
|
|
2216
|
+
|
|
2099
2217
|
if (
|
|
2100
2218
|
null != ment.cmp
|
|
2101
2219
|
&& entname != ment.cmp
|
|
@@ -2105,7 +2223,14 @@ function entityCmpMatch(
|
|
|
2105
2223
|
why.push('cmp-shared')
|
|
2106
2224
|
}
|
|
2107
2225
|
|
|
2108
|
-
if (
|
|
2226
|
+
if (qualifies) {
|
|
2227
|
+
why.push('cmp-qualifies-path')
|
|
2228
|
+
out.name = unversioned
|
|
2229
|
+
out.orig = ment.origcmp
|
|
2230
|
+
out.cmpish = true
|
|
2231
|
+
out.pathish = false
|
|
2232
|
+
}
|
|
2233
|
+
else if (cmpInfrequent && !cmpShared) {
|
|
2109
2234
|
why.push('cmp-primary')
|
|
2110
2235
|
out.name = ment.cmp
|
|
2111
2236
|
out.orig = ment.origcmp
|
|
@@ -2159,7 +2284,7 @@ function entityCmpMatch(
|
|
|
2159
2284
|
|
|
2160
2285
|
function cmpOccursInPath(data: { def: any, work: any }, cmpname: string): boolean {
|
|
2161
2286
|
if (null == data.work.potentialCmpsFromPaths) {
|
|
2162
|
-
data.work.potentialCmpsFromPaths =
|
|
2287
|
+
data.work.potentialCmpsFromPaths = Object.create(null)
|
|
2163
2288
|
each(data.def.paths, (_pathdef: PathDef, pathstr: string) => {
|
|
2164
2289
|
const parts: string[] = data.work.pathmap[pathstr].parts
|
|
2165
2290
|
|
|
@@ -2217,7 +2342,7 @@ type SharingEntry = SharingRoute & { parts: string[], resource: string }
|
|
|
2217
2342
|
// with, each keyed `cmp METHOD path`: those of a component that several
|
|
2218
2343
|
// resources share, where a record (one that declares an `id`) is never shared.
|
|
2219
2344
|
function sharedRoutes(routes: SharingRoute[], records: string[]): string[] {
|
|
2220
|
-
const bycmp: Record<string, SharingEntry[]> =
|
|
2345
|
+
const bycmp: Record<string, SharingEntry[]> = Object.create(null)
|
|
2221
2346
|
for (const route of routes) {
|
|
2222
2347
|
const parts = route.path.split('/').filter((p) => '' !== p)
|
|
2223
2348
|
const resource = pathResource(parts, route.method)
|
|
@@ -2234,15 +2359,15 @@ function sharedRoutes(routes: SharingRoute[], records: string[]): string[] {
|
|
|
2234
2359
|
if (new Set(counted.map((entry) => group[entry.resource])).size < 2) {
|
|
2235
2360
|
continue
|
|
2236
2361
|
}
|
|
2237
|
-
const members: Record<string, number> =
|
|
2362
|
+
const members: Record<string, number> = Object.create(null)
|
|
2238
2363
|
for (const name of new Set(counted.map((entry) => entry.resource))) {
|
|
2239
2364
|
members[group[name]] = (members[group[name]] ?? 0) + 1
|
|
2240
2365
|
}
|
|
2241
2366
|
taking.push(...counted.filter((entry) => 1 === members[group[entry.resource]]))
|
|
2242
2367
|
}
|
|
2243
2368
|
|
|
2244
|
-
const cmps: Record<string, Set<string>> =
|
|
2245
|
-
const routesOf: Record<string, Set<string>> =
|
|
2369
|
+
const cmps: Record<string, Set<string>> = Object.create(null)
|
|
2370
|
+
const routesOf: Record<string, Set<string>> = Object.create(null)
|
|
2246
2371
|
for (const entry of taking) {
|
|
2247
2372
|
(cmps[entry.resource] = cmps[entry.resource] ?? new Set()).add(entry.cmp)
|
|
2248
2373
|
const key = entry.resource + ' ' + entry.op + ' ' + paramNames(entry.parts)
|
|
@@ -2278,13 +2403,13 @@ function isSharingView(entry: SharingEntry, names: Set<string>): boolean {
|
|
|
2278
2403
|
// Item routes under the same parent keyed by the same parameters address the
|
|
2279
2404
|
// same records, so the resources they name are one: each maps to its group.
|
|
2280
2405
|
function aliasGroups(entries: SharingEntry[]): Record<string, string> {
|
|
2281
|
-
const group: Record<string, string> =
|
|
2406
|
+
const group: Record<string, string> = Object.create(null)
|
|
2282
2407
|
const find = (name: string): string => group[name] === name ? name : find(group[name])
|
|
2283
2408
|
for (const entry of entries) {
|
|
2284
2409
|
group[entry.resource] = entry.resource
|
|
2285
2410
|
}
|
|
2286
2411
|
|
|
2287
|
-
const first: Record<string, string> =
|
|
2412
|
+
const first: Record<string, string> = Object.create(null)
|
|
2288
2413
|
for (const entry of entries) {
|
|
2289
2414
|
if (!isParam(entry.parts[entry.parts.length - 1])) {
|
|
2290
2415
|
continue
|
package/src/transform/args.ts
CHANGED
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
depluralize,
|
|
9
9
|
canonizeParam,
|
|
10
10
|
inferFieldType,
|
|
11
|
+
isFileType,
|
|
11
12
|
normalizeFieldName,
|
|
12
13
|
paramName,
|
|
13
14
|
validator,
|
|
@@ -135,8 +136,8 @@ function resolveArgs(
|
|
|
135
136
|
const placeholders = [...String(mpoint.o ?? '').matchAll(/\{([^}]+)\}/g)].map((m) => m[1])
|
|
136
137
|
|
|
137
138
|
each(argdefs, (argdef: ParameterDef) => {
|
|
138
|
-
// A Swagger body parameter is the request body, which the body step reads.
|
|
139
|
-
if ('body' === (argdef as any).in) {
|
|
139
|
+
// A Swagger body or formData parameter is the request body, which the body step reads.
|
|
140
|
+
if ('body' === (argdef as any).in || 'formData' === (argdef as any).in) {
|
|
140
141
|
return
|
|
141
142
|
}
|
|
142
143
|
|
|
@@ -245,16 +246,12 @@ function resolveArgs(
|
|
|
245
246
|
|
|
246
247
|
|
|
247
248
|
// Type facts sit on a Swagger 2 parameter itself, and under `schema` in
|
|
248
|
-
// OpenAPI 3.
|
|
249
|
+
// OpenAPI 3.
|
|
249
250
|
function paramSchema(argdef: any): any {
|
|
250
251
|
if (null != argdef?.schema) {
|
|
251
252
|
return argdef.schema
|
|
252
253
|
}
|
|
253
|
-
|
|
254
|
-
return undefined
|
|
255
|
-
}
|
|
256
|
-
// Swagger 2's file type is a binary string in OpenAPI 3.
|
|
257
|
-
return 'file' === argdef?.type ? { ...argdef, type: 'string', format: 'binary' } : argdef
|
|
254
|
+
return isFileType(argdef?.type) ? { ...argdef, type: 'string', format: 'binary' } : argdef
|
|
258
255
|
}
|
|
259
256
|
|
|
260
257
|
|
package/src/transform/body.ts
CHANGED
|
@@ -5,7 +5,7 @@ import type { TransformResult, Transform } from '../transform'
|
|
|
5
5
|
import { KIT } from '../types'
|
|
6
6
|
|
|
7
7
|
import {
|
|
8
|
-
fieldName, guideActive, mergedProperties, requestWrapperOf, sortedKeys,
|
|
8
|
+
fieldName, guideActive, isFileType, mergedProperties, requestWrapperOf, sortedKeys,
|
|
9
9
|
} from '../utility'
|
|
10
10
|
|
|
11
11
|
import type {
|
|
@@ -236,7 +236,7 @@ function swaggerOffers(def: any, pathdef: any, opdef: any): Offer[] {
|
|
|
236
236
|
.filter((media) => null != textOf(media))
|
|
237
237
|
const consumes = 0 < declared.length ? declared : [
|
|
238
238
|
null != body ? JSON_MEDIA :
|
|
239
|
-
form.some(
|
|
239
|
+
form.some(sendsFile) ? MULTIPART_MEDIA : FORM_MEDIA
|
|
240
240
|
]
|
|
241
241
|
|
|
242
242
|
return consumes.map((media: string) => ({
|
|
@@ -247,6 +247,11 @@ function swaggerOffers(def: any, pathdef: any, opdef: any): Offer[] {
|
|
|
247
247
|
}
|
|
248
248
|
|
|
249
249
|
|
|
250
|
+
function sendsFile(param: any): boolean {
|
|
251
|
+
return isFileType(param.type) || (hasType(param, 'array') && isFileType(param.items?.type))
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
|
|
250
255
|
// An operation's parameter replaces the path's of the same location and name.
|
|
251
256
|
function swaggerParams(pathdef: any, opdef: any): any[] {
|
|
252
257
|
const key = (param: any) => (textOf(param.in) ?? '') + '\u0000' + (textOf(param.name) ?? '')
|
|
@@ -333,7 +338,7 @@ function bodyField(name: string, prop: any, encoding: any, arrays: string): Mode
|
|
|
333
338
|
field.binary = true
|
|
334
339
|
}
|
|
335
340
|
if (list) {
|
|
336
|
-
const join = arrayJoin(prop, encoding, arrays)
|
|
341
|
+
const join = field.binary ? undefined : arrayJoin(prop, encoding, arrays)
|
|
337
342
|
if (null == join) {
|
|
338
343
|
field.list = true
|
|
339
344
|
}
|
|
@@ -384,7 +389,7 @@ function rawBinary(type: string, schema: any): boolean {
|
|
|
384
389
|
|
|
385
390
|
function binarySchema(schema: any): boolean {
|
|
386
391
|
return isMap(schema) && !encodedText(schema) &&
|
|
387
|
-
('binary' === schema.format ||
|
|
392
|
+
('binary' === schema.format || isFileType(schema.type) || null != schema.contentMediaType)
|
|
388
393
|
}
|
|
389
394
|
|
|
390
395
|
|
|
@@ -454,11 +459,6 @@ function jsonSchema(offers: Offer[], media?: string): any {
|
|
|
454
459
|
}
|
|
455
460
|
|
|
456
461
|
|
|
457
|
-
function jsonRequestSchema(opdef: any): any {
|
|
458
|
-
return isMap(opdef) ? jsonSchema(openapiOffers(opdef)) : undefined
|
|
459
|
-
}
|
|
460
|
-
|
|
461
|
-
|
|
462
462
|
// The JSON schema a point's request body is sent with, under the media type
|
|
463
463
|
// the guide names, else the one the body step prefers.
|
|
464
464
|
function requestSchema(def: any, method: string, path: string, media?: string): any {
|
|
@@ -467,11 +467,11 @@ function requestSchema(def: any, method: string, path: string, media?: string):
|
|
|
467
467
|
|
|
468
468
|
|
|
469
469
|
// The schema of the request body offered under the media type the guide
|
|
470
|
-
// names, of
|
|
470
|
+
// names, else of the one the body step prefers, of any kind.
|
|
471
471
|
function selectedRequestSchema(def: any, method: string, path: string, media?: string): any {
|
|
472
|
-
const
|
|
473
|
-
const named = null == textOf(media) ? undefined : chooseOffer(
|
|
474
|
-
return
|
|
472
|
+
const ranked = rankOffers(requestOffers(def, method, path) ?? [])
|
|
473
|
+
const named = null == textOf(media) ? undefined : chooseOffer(ranked, media)
|
|
474
|
+
return (named ?? chooseOffer(ranked))?.offer.schema
|
|
475
475
|
}
|
|
476
476
|
|
|
477
477
|
|
|
@@ -504,14 +504,22 @@ function bodyFields(def: any, mpoint: ModelPoint, media?: string): string[] | fa
|
|
|
504
504
|
|
|
505
505
|
|
|
506
506
|
// A body that may be one of several objects declares the properties of each.
|
|
507
|
-
|
|
507
|
+
// One the body marks readOnly anywhere is never sent, so it is left out.
|
|
508
|
+
function declaredProperties(schema: any): string[] {
|
|
509
|
+
const declared = propertyDeclarations(schema, new Set())
|
|
510
|
+
const readOnly = new Set(declared.filter(([, prop]) => true === prop?.readOnly).map(([name]) => name))
|
|
511
|
+
return declared.map(([name]) => name).filter((name) => !readOnly.has(name))
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
|
|
515
|
+
function propertyDeclarations(schema: any, seen: Set<any>): [string, any][] {
|
|
508
516
|
if (!isMap(schema) || seen.has(schema)) {
|
|
509
517
|
return []
|
|
510
518
|
}
|
|
511
519
|
seen.add(schema)
|
|
512
|
-
return (isMap(schema.properties) ? Object.
|
|
520
|
+
return (isMap(schema.properties) ? Object.entries(schema.properties) as [string, any][] : [])
|
|
513
521
|
.concat(['allOf', 'oneOf', 'anyOf'].flatMap((key) =>
|
|
514
|
-
listOf(schema[key]).flatMap((member: any) =>
|
|
522
|
+
listOf(schema[key]).flatMap((member: any) => propertyDeclarations(member, seen))))
|
|
515
523
|
}
|
|
516
524
|
|
|
517
525
|
|
|
@@ -674,7 +682,6 @@ export {
|
|
|
674
682
|
guideMedia,
|
|
675
683
|
requestBody,
|
|
676
684
|
responseBody,
|
|
677
|
-
jsonRequestSchema,
|
|
678
685
|
requestSchema,
|
|
679
686
|
arrayRequestSchema,
|
|
680
687
|
arrayCarrier,
|
package/src/transform/field.ts
CHANGED
|
@@ -8,7 +8,7 @@ import {
|
|
|
8
8
|
validator, fieldName, inferFieldType, envelopeProp,
|
|
9
9
|
composedEnvelopeProp, mergedProperties, canonizeCmpName,
|
|
10
10
|
scanUntaggedUnion, firstSentence, humanTitle, collapseScalarAllOf,
|
|
11
|
-
itemEnvelopeOf,
|
|
11
|
+
itemEnvelopeOf, isFileType, nonNullType,
|
|
12
12
|
} from '../utility'
|
|
13
13
|
|
|
14
14
|
import { arrayCarrier, guideMedia, requestFieldSchema, sameType } from './body'
|
|
@@ -621,6 +621,9 @@ function modelField(fielddef: SchemaDef): ModelField {
|
|
|
621
621
|
if ('string' === typeof ffmt && '' !== ffmt.trim()) {
|
|
622
622
|
mfield.fo = ffmt.trim()
|
|
623
623
|
}
|
|
624
|
+
else if (isFileType(fielddef.type)) {
|
|
625
|
+
mfield.fo = 'binary'
|
|
626
|
+
}
|
|
624
627
|
|
|
625
628
|
// Record an untagged union under this field. The field is already typed
|
|
626
629
|
// openly ($ANY/$ARRAY/$OBJECT) because there is nothing to narrow it to;
|
|
@@ -1043,16 +1046,28 @@ function mergeField(
|
|
|
1043
1046
|
existingField.sh = newField.sh
|
|
1044
1047
|
}
|
|
1045
1048
|
|
|
1046
|
-
for (const flag of ['ro', 'wo', 'de'
|
|
1049
|
+
for (const flag of ['ro', 'wo', 'de'] as const) {
|
|
1047
1050
|
if (null == existingField[flag] && null != newField[flag]) {
|
|
1048
|
-
|
|
1051
|
+
existingField[flag] = newField[flag]
|
|
1049
1052
|
}
|
|
1050
1053
|
}
|
|
1051
1054
|
|
|
1055
|
+
// A format describes its own type, so another type's format would mislabel the field.
|
|
1056
|
+
if (null == existingField.fo && null != newField.fo &&
|
|
1057
|
+
sameType(ownType(existingField.t), ownType(newField.t))) {
|
|
1058
|
+
existingField.fo = newField.fo
|
|
1059
|
+
}
|
|
1060
|
+
|
|
1052
1061
|
return existingField
|
|
1053
1062
|
}
|
|
1054
1063
|
|
|
1055
1064
|
|
|
1065
|
+
// The type beside a nullable union's `$NULL`, else the type itself.
|
|
1066
|
+
function ownType(type: any): any {
|
|
1067
|
+
return nonNullType(type) ?? type
|
|
1068
|
+
}
|
|
1069
|
+
|
|
1070
|
+
|
|
1056
1071
|
export {
|
|
1057
1072
|
fieldTransform,
|
|
1058
1073
|
inferFieldsFromExamples,
|
|
@@ -324,13 +324,13 @@ function firstTextField(ent: ModelEntity, op?: ModelOp) {
|
|
|
324
324
|
const fields = Object.keys(ent.fields).sort().map(n => ent.fields[n])
|
|
325
325
|
for (let fI = 0; fI < fields.length; fI++) {
|
|
326
326
|
const field = fields[fI]
|
|
327
|
-
// NOT A readOnly FIELD. The flow writes this one and then
|
|
328
|
-
// mark comes back,
|
|
329
|
-
//
|
|
327
|
+
// NOT A readOnly FIELD, NOR A FILE. The flow writes this one and then
|
|
328
|
+
// asserts the mark comes back, which a field the client may not send, or
|
|
329
|
+
// one holding bytes, never does. Fields are sorted by name, so the choice
|
|
330
330
|
// is alphabetical accident: solar's planet, once its spec declared the
|
|
331
331
|
// server-assigned `forbidReason`, marked that instead of `kind`.
|
|
332
|
-
if ('`$STRING`' === field.t && 'id' !== field.n &&
|
|
333
|
-
|
|
332
|
+
if ('`$STRING`' === field.t && 'id' !== field.n && true !== field.ro &&
|
|
333
|
+
'binary' !== field.fo && true !== paramNames[field.n]) {
|
|
334
334
|
return field
|
|
335
335
|
}
|
|
336
336
|
}
|
package/src/transform/graphql.ts
CHANGED
|
@@ -19,6 +19,8 @@ import type {
|
|
|
19
19
|
|
|
20
20
|
import { deriveRetShape } from '../guide/graphql01'
|
|
21
21
|
|
|
22
|
+
import { paramName } from '../utility'
|
|
23
|
+
|
|
22
24
|
|
|
23
25
|
const REL_STUB = '{ id }'
|
|
24
26
|
|
|
@@ -92,24 +94,31 @@ function payloadScalarFields(typeName: string, def: any): string[] {
|
|
|
92
94
|
}
|
|
93
95
|
|
|
94
96
|
|
|
95
|
-
function buildVars(
|
|
97
|
+
function buildVars(
|
|
98
|
+
fielddef: any,
|
|
99
|
+
def: any,
|
|
100
|
+
optype: string,
|
|
101
|
+
renames?: Record<string, any>,
|
|
102
|
+
): ModelGraphqlVar[] {
|
|
96
103
|
const args = fielddef?.args ?? []
|
|
97
104
|
|
|
98
|
-
// The whole-request-body binding is the SINGLE-entity-input convention
|
|
99
|
-
// (issueCreate(input: IssueCreateInput!)). A
|
|
100
|
-
//
|
|
101
|
-
//
|
|
102
|
-
//
|
|
105
|
+
// The whole-request-body binding is the SINGLE-entity-input convention of a
|
|
106
|
+
// mutation (issueCreate(input: IssueCreateInput!)). A query's input object
|
|
107
|
+
// is a filter beside its other arguments, and a field taking several input
|
|
108
|
+
// objects — items(filter: Filter, orderBy: OrderBy) — binds each from its
|
|
109
|
+
// own argument, or they all receive the same value.
|
|
103
110
|
const inputs = args.filter((a: any) => {
|
|
104
111
|
const atype = def.types?.[a.type]
|
|
105
112
|
return null != atype && 'INPUT_OBJECT' === atype.kind
|
|
106
113
|
})
|
|
107
|
-
const soleInput = 1 === inputs.length ? inputs[0].name : undefined
|
|
114
|
+
const soleInput = 'mutation' === optype && 1 === inputs.length ? inputs[0].name : undefined
|
|
108
115
|
|
|
109
116
|
return args.map((arg: any) => {
|
|
117
|
+
// A scalar argument is read from the parameter the args step names for it.
|
|
110
118
|
const v: any = {
|
|
111
119
|
name: arg.name,
|
|
112
|
-
from: arg.name === soleInput ? '' :
|
|
120
|
+
from: arg.name === soleInput ? '' :
|
|
121
|
+
inputs.includes(arg) ? arg.name : paramName(arg.name, renames),
|
|
113
122
|
gqltype: arg.gqltype,
|
|
114
123
|
}
|
|
115
124
|
if (undefined !== arg.deflt) {
|
|
@@ -196,7 +205,7 @@ const graphqlTransform: Transform = async function(
|
|
|
196
205
|
const fragName = pascal(entname) + 'Fields'
|
|
197
206
|
const fragSpread = 0 < fragFields.length ? '{ ...' + fragName + ' }' : '{ id }'
|
|
198
207
|
|
|
199
|
-
const vars = buildVars(fielddef, def)
|
|
208
|
+
const vars = buildVars(fielddef, def, optype, mpoint.r?.param)
|
|
200
209
|
|
|
201
210
|
let selection = fragSpread
|
|
202
211
|
let respath = 'body.data.' + rootfield
|
|
@@ -272,4 +281,5 @@ export {
|
|
|
272
281
|
graphqlTransform,
|
|
273
282
|
selectionFields,
|
|
274
283
|
renderDoc,
|
|
284
|
+
buildVars,
|
|
275
285
|
}
|