@voxgig/apidef 8.27.0 → 8.28.1

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,9 +124,18 @@ const fieldTransform: Transform = async function(
122
124
  const singleKey = (composite as any).single
123
125
  delete (composite as any).single
124
126
 
125
- const single = null == composite.parts ? singleIdentity(ment, gent?.id, def) : {}
127
+ const single = null == composite.parts ? singleIdentity(ment, gent?.id, def, wires) : {}
126
128
  const keyed = null != single.field
127
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
+
128
139
  if (null == idField && null != singleKey && null == composite.parts && !keyed) {
129
140
  // The guide disabled composite; the terminal parameter is the key, and
130
141
  // the entity needs the field to carry it for the same reason the
@@ -244,6 +255,32 @@ function identityParams(ment: ModelEntity): string[] {
244
255
  }
245
256
 
246
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[] {
247
284
  const out: any[] = []
248
285
  const seen = new Set<any>()
249
286
 
@@ -274,29 +311,25 @@ function responseCandidates(ment: ModelEntity, def: any): any[] {
274
311
 
275
312
  const add = (schema: any, opname: string) => {
276
313
  for (const props of propsOf(schema)) {
277
- out.push(props)
278
-
279
314
  // One level in, but ONLY through the envelope property.
280
315
  const envelope = envelopeProp(props, opname)
281
- if (null == envelope) {
282
- continue
283
- }
284
- const inner = resolveRef(props[envelope], def)
285
- if (null == inner) {
286
- continue
316
+ const inner = null == envelope ? null : resolveRef(props[envelope], def)
317
+
318
+ if (whole || null == inner) {
319
+ out.push(props)
287
320
  }
288
- for (const innerProps of propsOf(inner)) {
289
- out.push(innerProps)
321
+ if (null != inner) {
322
+ out.push(...propsOf(inner))
290
323
  }
291
324
  }
292
325
  }
293
326
 
294
- for (const opname of ['load', 'list', 'update', 'create']) {
327
+ for (const opname of opnames) {
295
328
  const mop = (ment as any).op?.[opname]
296
329
 
297
330
  for (const mpoint of (mop?.points || [])) {
298
331
  // An action point's response is not the entity.
299
- if (null != mpoint?.q?.['$action']) {
332
+ if (null != mpoint?.q?.['$action'] || !keep(mpoint)) {
300
333
  continue
301
334
  }
302
335
 
@@ -551,11 +584,13 @@ function compositeId(
551
584
 
552
585
 
553
586
  // Where a single-part identity lives in the record. A field name becomes the
554
- // descriptor's `field`; a dotted path cannot, so it stays a `from`.
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.
555
589
  function singleIdentity(
556
590
  ment: ModelEntity,
557
591
  gid?: any,
558
592
  def?: any,
593
+ wires?: Record<string, Set<string>>,
559
594
  ): { field?: string, from?: Record<string, string> } {
560
595
  const say = null != gid?.from && 'object' === typeof gid.from ? gid.from.id : undefined
561
596
  if (null != say && '' !== String(say)) {
@@ -565,16 +600,35 @@ function singleIdentity(
565
600
 
566
601
  const parts = identityParams(ment)
567
602
  const wire = itemWireNames(ment)
568
- if (1 !== parts.length || 'id' !== parts[0] || 1 !== wire.length ||
569
- 'id' === wire[0] || '' === wire[0]) {
603
+ if (1 !== parts.length || 'id' !== parts[0] || 1 !== wire.length || '' === wire[0]) {
570
604
  return {}
571
605
  }
572
606
 
573
- const name = fieldName(wire[0])
574
- const returned = responseCandidates(ment, def ?? {})
575
- .some((props: any) => scalarSchema(resolveRef(props[wire[0]], def)))
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
+
576
627
 
577
- return returned && identityScalar((ment.fields as any)?.[name]) ? { field: name } : {}
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'])]
578
632
  }
579
633
 
580
634
 
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,