@voxgig/apidef 8.26.0 → 8.28.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.
@@ -45,6 +45,7 @@ const fieldTransform: Transform = async function(
45
45
 
46
46
  each(kit.entity, (ment: ModelEntity, entname: string) => {
47
47
  const fields = ment.fields
48
+ const wires: Record<string, Set<string>> = Object.create(null)
48
49
 
49
50
  for (let opname of opFieldPrecedence) {
50
51
  const mop = ment.op[opname]
@@ -53,10 +54,11 @@ const fieldTransform: Transform = async function(
53
54
 
54
55
  for (let mpoint of mpoints) {
55
56
  const media = guideMedia(guide, entname, opname, mpoint).body
56
- const opfields = resolveOpFields(ment, mop, mpoint, def, media)
57
57
  const carrier = carrierOf(mpoint, def, media)?.name
58
58
 
59
- for (let opfield of opfields) {
59
+ for (const fielddef of findFieldDefs(ment, mop, mpoint, def, media)) {
60
+ const opfield = modelField(fielddef)
61
+ ;(wires[opfield.n] = wires[opfield.n] ?? new Set()).add(String((fielddef as any).key$))
60
62
  addField(fields, mop, opfield, carrier === opfield.n)
61
63
  }
62
64
  }
@@ -122,7 +124,19 @@ const fieldTransform: Transform = async function(
122
124
  const singleKey = (composite as any).single
123
125
  delete (composite as any).single
124
126
 
125
- if (null == idField && null != singleKey && null == composite.parts) {
127
+ const single = null == composite.parts ? singleIdentity(ment, gent?.id, def, wires) : {}
128
+ const keyed = null != single.field
129
+
130
+ // `_id` keeps the underscore a field name drops, unless a property shares it.
131
+ const keywire = single.field as string
132
+ const keyname = keyed ? fieldName(keywire) : undefined
133
+ if (null != keyname && keyname !== keywire && null != fields[keyname] &&
134
+ [...(wires[keyname] ?? [])].every((w: string) => w === keywire)) {
135
+ fields[keywire] = { ...fields[keyname], n: keywire }
136
+ delete fields[keyname]
137
+ }
138
+
139
+ if (null == idField && null != singleKey && null == composite.parts && !keyed) {
126
140
  // The guide disabled composite; the terminal parameter is the key, and
127
141
  // the entity needs the field to carry it for the same reason the
128
142
  // composite branch above does.
@@ -134,8 +148,8 @@ const fieldTransform: Transform = async function(
134
148
  } as ModelField
135
149
  }
136
150
 
137
- if (idField || null != composite.parts || null != singleKey) {
138
- ment.id = { name: 'id', field: 'id', ...composite }
151
+ if (idField || null != composite.parts || null != singleKey || keyed) {
152
+ ment.id = { name: 'id', field: 'id', ...composite, ...single }
139
153
  }
140
154
  else if (addressedById(ment)) {
141
155
  fields.id = {
@@ -145,7 +159,7 @@ const fieldTransform: Transform = async function(
145
159
  r: false,
146
160
  } as ModelField
147
161
 
148
- ment.id = { name: 'id', field: 'id', ...composite }
162
+ ment.id = { name: 'id', field: 'id', ...composite, ...single }
149
163
  }
150
164
 
151
165
  ment.fields = Object.fromEntries(Object.keys(fields).sort().map(n => {
@@ -241,6 +255,32 @@ function identityParams(ment: ModelEntity): string[] {
241
255
  }
242
256
 
243
257
  function responseCandidates(ment: ModelEntity, def: any): any[] {
258
+ return answeredProps(ment, def, ['load', 'list', 'update', 'create'], () => true, true)
259
+ }
260
+
261
+
262
+ // The records the routes addressing one record by `id` answer with, through
263
+ // their envelopes, else every record the entity answers with.
264
+ function itemRecords(ment: ModelEntity, def: any): any[] {
265
+ const keyed = (pt: any) => 'id' === trailingVars(pt).slice(-1)[0]
266
+ for (const opnames of [['load'], ['update', 'patch']]) {
267
+ const records = answeredProps(ment, def, opnames, keyed, false)
268
+ if (0 < records.length) {
269
+ return records
270
+ }
271
+ }
272
+ return responseCandidates(ment, def)
273
+ }
274
+
275
+
276
+ // What the ops' points answer with; `whole` keeps an envelope beside its item.
277
+ function answeredProps(
278
+ ment: ModelEntity,
279
+ def: any,
280
+ opnames: string[],
281
+ keep: (pt: any) => boolean,
282
+ whole: boolean,
283
+ ): any[] {
244
284
  const out: any[] = []
245
285
  const seen = new Set<any>()
246
286
 
@@ -271,29 +311,25 @@ function responseCandidates(ment: ModelEntity, def: any): any[] {
271
311
 
272
312
  const add = (schema: any, opname: string) => {
273
313
  for (const props of propsOf(schema)) {
274
- out.push(props)
275
-
276
314
  // One level in, but ONLY through the envelope property.
277
315
  const envelope = envelopeProp(props, opname)
278
- if (null == envelope) {
279
- continue
280
- }
281
- const inner = resolveRef(props[envelope], def)
282
- if (null == inner) {
283
- continue
316
+ const inner = null == envelope ? null : resolveRef(props[envelope], def)
317
+
318
+ if (whole || null == inner) {
319
+ out.push(props)
284
320
  }
285
- for (const innerProps of propsOf(inner)) {
286
- out.push(innerProps)
321
+ if (null != inner) {
322
+ out.push(...propsOf(inner))
287
323
  }
288
324
  }
289
325
  }
290
326
 
291
- for (const opname of ['load', 'list', 'update', 'create']) {
327
+ for (const opname of opnames) {
292
328
  const mop = (ment as any).op?.[opname]
293
329
 
294
330
  for (const mpoint of (mop?.points || [])) {
295
331
  // An action point's response is not the entity.
296
- if (null != mpoint?.q?.['$action']) {
332
+ if (null != mpoint?.q?.['$action'] || !keep(mpoint)) {
297
333
  continue
298
334
  }
299
335
 
@@ -418,9 +454,7 @@ function resolvePart(
418
454
  }
419
455
 
420
456
  const prop = (name: string) => resolveRef(props[name], def)
421
- const scalar = (p: any) =>
422
- null != p && 'object' !== String(p.type) && 'array' !== String(p.type) &&
423
- null == p.properties && null == p.items
457
+ const scalar = scalarSchema
424
458
 
425
459
  for (const name of aliases) {
426
460
  if (scalar(prop(name))) {
@@ -549,6 +583,96 @@ function compositeId(
549
583
  }
550
584
 
551
585
 
586
+ // Where a single-part identity lives in the record. A field name becomes the
587
+ // descriptor's `field`; a dotted path cannot, so it stays a `from`. A field
588
+ // counts only at the top of the records `itemRecords` reads.
589
+ function singleIdentity(
590
+ ment: ModelEntity,
591
+ gid?: any,
592
+ def?: any,
593
+ wires?: Record<string, Set<string>>,
594
+ ): { field?: string, from?: Record<string, string> } {
595
+ const say = null != gid?.from && 'object' === typeof gid.from ? gid.from.id : undefined
596
+ if (null != say && '' !== String(say)) {
597
+ const where = String(say)
598
+ return where.includes('.') ? { from: { id: where } } : { field: where }
599
+ }
600
+
601
+ const parts = identityParams(ment)
602
+ const wire = itemWireNames(ment)
603
+ if (1 !== parts.length || 'id' !== parts[0] || 1 !== wire.length || '' === wire[0]) {
604
+ return {}
605
+ }
606
+
607
+ const records = itemRecords(ment, def ?? {})
608
+ const carries = (key: string) => identityScalar((ment.fields as any)?.[fieldName(key)]) &&
609
+ records.some((props: any) => scalarSchema(resolveRef(props[key], def)))
610
+
611
+ if ('id' !== wire[0] && carries(wire[0])) {
612
+ return { field: fieldName(wire[0]) }
613
+ }
614
+
615
+ // A record holding no `id` is keyed by the one identifier it declares, when
616
+ // the path's own parameter is an identifier too.
617
+ const declared = carries('id') || !IDENTIFIER_PARAM.test(wire[0]) ? [] :
618
+ identityKeys(ment.name).filter((key: string) => carries(key) &&
619
+ [...(wires?.[fieldName(key)] ?? [key])].every((w: string) => w === key))
620
+
621
+ return 1 === declared.length ? { field: declared[0] } : {}
622
+ }
623
+
624
+
625
+ const IDENTIFIER_PARAM = /(^|[_-])[iI][dD]$|[a-z0-9](Id|ID)$/
626
+
627
+
628
+ // The wire names a record conventionally keeps its own identifier under.
629
+ function identityKeys(entname: string): string[] {
630
+ const camel = entname.replace(/_([a-z0-9])/g, (_m: string, c: string) => c.toUpperCase())
631
+ return [...new Set(['_id', entname + '_id', camel + 'Id'])]
632
+ }
633
+
634
+
635
+ // The wire names of the `id` parameter on the routes that address one record
636
+ // by it, with '' for a route that takes other parameters too.
637
+ function itemWireNames(ment: ModelEntity): string[] {
638
+ const names = new Set<string>()
639
+
640
+ for (const opname of ID_OPS) {
641
+ for (const pt of ((ment as any).op?.[opname]?.points || [])) {
642
+ if (null != pt?.q?.['$action']) {
643
+ continue
644
+ }
645
+ const run = trailingVars(pt)
646
+ if ('id' !== run[run.length - 1]) {
647
+ continue
648
+ }
649
+ const vars = ((pt.s || []) as any[]).filter((s: any) => null != s?.var)
650
+ if (1 !== vars.length) {
651
+ names.add('')
652
+ continue
653
+ }
654
+ const param = (pt.g?.params || []).find((p: any) => 'id' === p?.n)
655
+ const renamed = Object.keys(pt.r?.param || {}).find((k: string) => 'id' === pt.r.param[k])
656
+ names.add(String(param?.or ?? renamed ?? 'id'))
657
+ }
658
+ }
659
+
660
+ return [...names]
661
+ }
662
+
663
+
664
+ function scalarSchema(p: any): boolean {
665
+ return null != p && 'object' !== String(p.type) && 'array' !== String(p.type) &&
666
+ null == p.properties && null == p.items
667
+ }
668
+
669
+
670
+ function identityScalar(f: any): boolean {
671
+ const t = String(f?.t || '').toUpperCase()
672
+ return /STRING|INTEGER|NUMBER/.test(t) && !/OBJECT|ARRAY|MAP|LIST/.test(t)
673
+ }
674
+
675
+
552
676
  // True when any of the entity's own operation points declares an `id`
553
677
  // parameter — i.e. the API addresses this entity by id, whether or not its
554
678
  // response schema declares an id field.
@@ -1073,4 +1197,5 @@ export {
1073
1197
  inferFieldsFromExamples,
1074
1198
  inferTypeFromValue,
1075
1199
  routeFieldNames,
1200
+ singleIdentity,
1076
1201
  }
package/src/utility.ts CHANGED
@@ -2048,6 +2048,36 @@ function composedEnvelopeProp(schema: any, opname: string): string | null {
2048
2048
  }
2049
2049
 
2050
2050
 
2051
+ // The list of records a page response holds: the response itself when it is
2052
+ // a list of records, else the one list of them a list unwraps, beside nothing
2053
+ // but status, paging and the page's own metadata. An `id` beside the list
2054
+ // makes the response a record that holds one, and answers no page.
2055
+ function pageRecords(schema: any): any {
2056
+ if (null == schema || 'object' !== typeof schema) {
2057
+ return null
2058
+ }
2059
+ if (true === propIsList(schema)) {
2060
+ return isRecordList(schema) ? schema : null
2061
+ }
2062
+
2063
+ const own = schema.properties
2064
+ const key = (null != own ? envelopeProp(own, 'list') : null) ??
2065
+ composedEnvelopeProp(schema, 'list')
2066
+ const props = mergedProperties(schema)
2067
+ if (null == key || null == props || null != props.id || !isRecordList(props[key])) {
2068
+ return null
2069
+ }
2070
+
2071
+ return keysof(props).every((k: string) =>
2072
+ k === key || isEnvelopePageProp(k) || isEnvelopeStatusProp(k)) ? props[key] : null
2073
+ }
2074
+
2075
+
2076
+ function answersPage(schema: any): boolean {
2077
+ return null != pageRecords(schema)
2078
+ }
2079
+
2080
+
2051
2081
  // The properties of a schema and its allOf members; the first declaration of
2052
2082
  // a name wins. Read-only, since parsed nodes are shared between references.
2053
2083
  function mergedProperties(schema: any): Record<string, any> | undefined {
@@ -2292,6 +2322,8 @@ export {
2292
2322
  envelopeProp,
2293
2323
  envelopeItemRef,
2294
2324
  composedEnvelopeProp,
2325
+ pageRecords,
2326
+ answersPage,
2295
2327
  propIsList,
2296
2328
  mergedProperties,
2297
2329
  closedBodyTransform,