@voxgig/apidef 8.24.0 → 8.25.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.
@@ -22,7 +22,7 @@ import type {
22
22
  ModelEntity,
23
23
  } from '../model'
24
24
 
25
- import { depluralize, guideActive, paramName } from '../utility'
25
+ import { cmpRefName, depluralize, guideActive, paramName, READ_METHODS } from '../utility'
26
26
 
27
27
  import { byCodePoint } from '../refcount'
28
28
 
@@ -160,6 +160,43 @@ function mergeCollectionPaths(
160
160
  true === distinct?.(pathStr, methodsOf(entities[ename].path[pathStr]),
161
161
  owner.route, methodsOf(entities[owner.ename]?.path?.[owner.route]))
162
162
 
163
+ const namedByRecord = (ename: string, pathStr: string) => {
164
+ const ref = recordRef?.(pathStr, methodsOf(entities[ename].path[pathStr]), true)
165
+ return null != ref && ename === cmpRefName(ref)
166
+ }
167
+
168
+ // An owner that reads nothing, holds only routes beneath the collection,
169
+ // and extends the name a share's own record gives, takes that name.
170
+ const absorbed: Record<string, string> = {}
171
+ for (const pathStr of Object.keys(owned).sort(byCodePoint)) {
172
+ const owner = owned[pathStr]
173
+ if (null == owner || null != absorbed[owner.ename] ||
174
+ Object.values(absorbed).includes(owner.ename)) continue
175
+ const ename = shares[pathStr].slice().sort(byCodePoint).find((share: string) =>
176
+ owner.ename.startsWith(share + '_') && namedByRecord(share, pathStr) &&
177
+ !apart(share, pathStr, owner) &&
178
+ onlyBeneath(entities[owner.ename], collectionRoot(pathStr) as string) &&
179
+ !Object.values(entities[owner.ename].path).some((pd: any) =>
180
+ methodsOf(pd).some((method: string) => READ_METHODS.includes(method))) &&
181
+ false !== entities[owner.ename].active && false !== entities[share].active)
182
+ if (null != ename && null == absorbed[ename]) {
183
+ absorbed[owner.ename] = ename
184
+ }
185
+ }
186
+ for (const [from, to] of Object.entries(absorbed)) {
187
+ for (const pathStr of Object.keys(entities[from].path)) {
188
+ movePath(entities[from], entities[to], pathStr)
189
+ }
190
+ delete entities[from]
191
+ emptied.push(from)
192
+ log?.debug?.({ point: 'merge-collection-absorb', from, to })
193
+ }
194
+ for (const [pathStr, owner] of Object.entries(owned)) {
195
+ if (null != owner && null != absorbed[owner.ename]) {
196
+ owned[pathStr] = { ...owner, ename: absorbed[owner.ename] }
197
+ }
198
+ }
199
+
163
200
  // Second pass: for each entity with a "/X" path, if X has an owner
164
201
  // elsewhere, move the path there.
165
202
  for (const [ename, entity] of Object.entries(entities)) {
@@ -176,40 +213,7 @@ function mergeCollectionPaths(
176
213
  for (const [pathStr, owner] of pathsToMove) {
177
214
  const targetEntity = entities[owner.ename]
178
215
  if (targetEntity == null) continue
179
- targetEntity.path = targetEntity.path ?? {}
180
- const srcPath = entity.path[pathStr]
181
- const tgtPath = targetEntity.path[pathStr]
182
- if (tgtPath == null) {
183
- targetEntity.path[pathStr] = srcPath
184
- }
185
- else {
186
- if (srcPath?.op) {
187
- tgtPath.op = tgtPath.op ?? {}
188
- for (const opname of Object.keys(srcPath.op)) {
189
- if (tgtPath.op[opname] == null) {
190
- tgtPath.op[opname] = srcPath.op[opname]
191
- }
192
- }
193
- }
194
- if (srcPath?.action) {
195
- tgtPath.action = tgtPath.action ?? {}
196
- for (const aname of Object.keys(srcPath.action)) {
197
- if (tgtPath.action[aname] == null) {
198
- tgtPath.action[aname] = srcPath.action[aname]
199
- }
200
- }
201
- }
202
- if (srcPath?.rename?.param) {
203
- tgtPath.rename = tgtPath.rename ?? {}
204
- tgtPath.rename.param = tgtPath.rename.param ?? {}
205
- for (const p of Object.keys(srcPath.rename.param)) {
206
- if (tgtPath.rename.param[p] == null) {
207
- tgtPath.rename.param[p] = srcPath.rename.param[p]
208
- }
209
- }
210
- }
211
- }
212
- delete entity.path[pathStr]
216
+ movePath(entity, targetEntity, pathStr)
213
217
  log?.debug?.({
214
218
  point: 'merge-collection-path',
215
219
  path: pathStr,
@@ -234,6 +238,51 @@ function mergeCollectionPaths(
234
238
 
235
239
 
236
240
 
241
+ // Move one path onto the target entity, keeping what the target already has.
242
+ function movePath(entity: any, targetEntity: any, pathStr: string) {
243
+ targetEntity.path = targetEntity.path ?? {}
244
+ const srcPath = entity.path[pathStr]
245
+ const tgtPath = targetEntity.path[pathStr]
246
+ if (tgtPath == null) {
247
+ targetEntity.path[pathStr] = srcPath
248
+ }
249
+ else {
250
+ if (srcPath?.op) {
251
+ tgtPath.op = tgtPath.op ?? {}
252
+ for (const opname of Object.keys(srcPath.op)) {
253
+ if (tgtPath.op[opname] == null) {
254
+ tgtPath.op[opname] = srcPath.op[opname]
255
+ }
256
+ }
257
+ }
258
+ if (srcPath?.action) {
259
+ tgtPath.action = tgtPath.action ?? {}
260
+ for (const aname of Object.keys(srcPath.action)) {
261
+ if (tgtPath.action[aname] == null) {
262
+ tgtPath.action[aname] = srcPath.action[aname]
263
+ }
264
+ }
265
+ }
266
+ if (srcPath?.rename?.param) {
267
+ tgtPath.rename = tgtPath.rename ?? {}
268
+ tgtPath.rename.param = tgtPath.rename.param ?? {}
269
+ for (const p of Object.keys(srcPath.rename.param)) {
270
+ if (tgtPath.rename.param[p] == null) {
271
+ tgtPath.rename.param[p] = srcPath.rename.param[p]
272
+ }
273
+ }
274
+ }
275
+ }
276
+ delete entity.path[pathStr]
277
+ }
278
+
279
+
280
+ function onlyBeneath(entity: any, root: string): boolean {
281
+ return Object.keys(entity?.path ?? {}).every((pathStr: string) =>
282
+ root === pathStr.replace(/\/+$/, '') || pathStr.startsWith(root + '/'))
283
+ }
284
+
285
+
237
286
  // A path of literals only, less any trailing slash, is a collection path.
238
287
  function collectionRoot(pathStr: string): string | null {
239
288
  const key = pathStr.replace(/\/+$/, '')
@@ -5,13 +5,13 @@ import { each, getx } from 'jostraca'
5
5
  import type { TransformResult, Transform } from '../transform'
6
6
 
7
7
  import {
8
- validator, canonizeField, inferFieldType, normalizeFieldName, envelopeProp,
9
- composedEnvelopeProp, mergedProperties, canonizeCmpName, requestWrapperOf,
8
+ validator, fieldName, inferFieldType, envelopeProp,
9
+ composedEnvelopeProp, mergedProperties, canonizeCmpName,
10
10
  scanUntaggedUnion, firstSentence, humanTitle, collapseScalarAllOf,
11
11
  itemEnvelopeOf,
12
12
  } from '../utility'
13
13
 
14
- import { arrayCarrier, guideMedia, sameType, selectedRequestSchema } from './body'
14
+ import { arrayCarrier, guideMedia, requestFieldSchema, sameType } from './body'
15
15
 
16
16
  import { KIT } from '../types'
17
17
 
@@ -592,11 +592,7 @@ function routeFieldNames(
592
592
 
593
593
 
594
594
  function modelField(fielddef: SchemaDef): ModelField {
595
- const fieldname = (fielddef as any).key$ as string
596
- // Field names are WIRE identifiers — see canonizeField. Using the
597
- // entity-name canonizer here renamed modelType -> model_type and
598
- // items -> item, so the SDK read keys the server never sends.
599
- const name = canonizeField(normalizeFieldName(fieldname))
595
+ const name = fieldName((fielddef as any).key$ as string)
600
596
  const mfield: ModelField = {
601
597
  n: name,
602
598
  h: humanTitle(name),
@@ -800,14 +796,9 @@ function findFieldDefs(
800
796
  // A QUERY (RFC 10008) body is a filter, not the entity shape, so a QUERY
801
797
  // op's fields come from its response alone; an action's body is the
802
798
  // verb's arguments, never the record.
803
- const reqschema = selectedRequestSchema(def, mpoint.m, mpoint.o, media) ?? getx(requestBody, 'schema')
799
+ const reqschema = requestFieldSchema(def, mpoint, media)
804
800
  if ((requestBody || null != reqschema) && 'query' !== method && !isAction) {
805
- // A body that sends the record under one key holds its fields there.
806
- const reqkey = requestWrapperOf(mpoint.t?.req)
807
- fieldSets = [
808
- fieldSets,
809
- null == reqkey ? reqschema : mergedProperties(reqschema)?.[reqkey] ?? reqschema,
810
- ]
801
+ fieldSets = [fieldSets, reqschema]
811
802
  }
812
803
 
813
804
 
package/src/utility.ts CHANGED
@@ -1054,6 +1054,13 @@ function canonizeField(s: string) {
1054
1054
  }
1055
1055
 
1056
1056
 
1057
+ // Field names are WIRE identifiers: the entity-name canonizer would rename
1058
+ // modelType to model_type and items to item, keys the server never sends.
1059
+ function fieldName(prop: string): string {
1060
+ return canonizeField(normalizeFieldName(prop))
1061
+ }
1062
+
1063
+
1057
1064
  function stripSchemaNamespace(name: string): string {
1058
1065
  if (null == name || !name.includes('.')) return name
1059
1066
  const segs = name.split('.')
@@ -1076,6 +1083,18 @@ function canonizeCmpName(orig: string): string {
1076
1083
  }
1077
1084
 
1078
1085
 
1086
+ const READ_METHODS = ['GET', 'QUERY', 'HEAD', 'OPTIONS']
1087
+
1088
+
1089
+ const CMP_REF_RE = /\/(components\/schemas|definitions)\/(.+)$/
1090
+
1091
+ // The canonical name of the component a reference points to.
1092
+ function cmpRefName(xref: string): string {
1093
+ const m = xref.match(CMP_REF_RE)
1094
+ return null == m ? xref : canonizeCmpName(m[2])
1095
+ }
1096
+
1097
+
1079
1098
  const FIRST_LETTER_RE = /[a-zA-Z]/
1080
1099
 
1081
1100
 
@@ -2153,7 +2172,11 @@ export {
2153
2172
  CANON_ONE,
2154
2173
  canonize,
2155
2174
  canonizeField,
2175
+ fieldName,
2156
2176
  canonizeCmpName,
2177
+ CMP_REF_RE,
2178
+ cmpRefName,
2179
+ READ_METHODS,
2157
2180
  stripSchemaNamespace,
2158
2181
  sanitizeSlug,
2159
2182
  slugToPascalCase,