@voxgig/apidef 8.32.1 → 8.32.3

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.
@@ -59,7 +59,7 @@ const fieldTransform: Transform = async function(
59
59
  for (const fielddef of findFieldDefs(ment, mop, mpoint, def, media)) {
60
60
  const opfield = modelField(fielddef)
61
61
  ;(wires[opfield.n] = wires[opfield.n] ?? new Set()).add(String((fielddef as any).key$))
62
- addField(fields, mop, opfield, carrier === opfield.n)
62
+ addField(fields, mop, opfield, carrier === opfield.n, true === (fielddef as any).ans$)
63
63
  }
64
64
  }
65
65
  }
@@ -761,12 +761,18 @@ function modelField(fielddef: SchemaDef): ModelField {
761
761
  }
762
762
 
763
763
 
764
- function addField(fields: Record<string, ModelField>, mop: ModelOp, field: ModelField, carrier: boolean) {
764
+ function addField(
765
+ fields: Record<string, ModelField>,
766
+ mop: ModelOp,
767
+ field: ModelField,
768
+ carrier: boolean,
769
+ answer: boolean = false,
770
+ ) {
765
771
  if (!Object.prototype.hasOwnProperty.call(fields, field.n)) {
766
772
  fields[field.n] = field
767
773
  }
768
774
  else {
769
- mergeField(mop, fields[field.n], field, carrier)
775
+ mergeField(mop, fields[field.n], field, carrier, answer)
770
776
  }
771
777
  }
772
778
 
@@ -925,17 +931,25 @@ function findFieldDefs(
925
931
  // verb's arguments, never the record.
926
932
  const reqschema = requestFieldSchema(def, mpoint, media)
927
933
  if ((requestBody || null != reqschema) && 'query' !== method && !isAction) {
928
- fieldSets = [fieldSets, reqschema]
934
+ // What the call sends is required where the request says so. The
935
+ // response's `required` describes the answer, so a property the request
936
+ // also declares is marked to leave the op's requiredness alone.
937
+ const sent = composedFields(reqschema)
938
+ const declared = new Set(sent.map((fd: any) => fd.key$))
939
+ fielddefs.push(
940
+ ...composedFields(fieldSets).map((fd: any) => declared.has(fd.key$) ? { ...fd, ans$: true } : fd),
941
+ ...sent)
929
942
  }
930
943
 
944
+ else {
945
+ if (fieldSets && (Array.isArray(fieldSets.allOf) || fieldSets.properties)) {
946
+ fieldSets = [fieldSets]
947
+ }
931
948
 
932
- if (fieldSets && (Array.isArray(fieldSets.allOf) || fieldSets.properties)) {
933
- fieldSets = [fieldSets]
949
+ each(fieldSets, (fieldSet: any) => {
950
+ fielddefs.push(...composedFields(fieldSet))
951
+ })
934
952
  }
935
-
936
- each(fieldSets, (fieldSet: any) => {
937
- fielddefs.push(...composedFields(fieldSet))
938
- })
939
953
  }
940
954
 
941
955
  // Fallback: infer fields from example response data when no schema properties found
@@ -1178,14 +1192,17 @@ function inferTypeFromValue(value: any): string {
1178
1192
 
1179
1193
 
1180
1194
  // A carrier keeps its array type for its operation where it meets a field of
1181
- // another type.
1195
+ // another type. An answer's declaration of a field the request also declares
1196
+ // says nothing of the operation.
1182
1197
  function mergeField(
1183
1198
  mop: ModelOp,
1184
1199
  existingField: ModelField,
1185
1200
  newField: ModelField,
1186
1201
  carrier: boolean = false,
1202
+ answer: boolean = false,
1187
1203
  ) {
1188
- if (newField.r !== existingField.r || (carrier && !sameType(newField.t, existingField.t))) {
1204
+ if (!answer &&
1205
+ (newField.r !== existingField.r || (carrier && !sameType(newField.t, existingField.t)))) {
1189
1206
  existingField.op[mop.name] = {
1190
1207
  req: newField.r,
1191
1208
  type: newField.t,
@@ -77,6 +77,7 @@ const operationTransform: Transform = async function(
77
77
  resolveUpdate(opm, on)
78
78
  resolveRemove(opm, on)
79
79
  resolvePatch(opm, on)
80
+ resolveActionCreate(ctx, opm, on)
80
81
 
81
82
  // After patch has joined update, so each operation's routes are final.
82
83
  for (const mop of Object.values(opm)) {
@@ -186,11 +187,56 @@ function resolvePatch(opm: ModelOpMap, on: OpEntity): undefined | ModelOp {
186
187
  }
187
188
 
188
189
 
190
+ // A create made only of actions on the record creates nothing: its points ride
191
+ // along into the entity's own update, as update's do when a PATCH is promoted.
192
+ function resolveActionCreate(ctx: any, opm: ModelOpMap, on: OpEntity) {
193
+ if (null == opm.create || !onlyActionPaths(on.gent, 'create')) {
194
+ return
195
+ }
196
+
197
+ const opm$ = (on.gent as any).opm$
198
+ const paths: PathDesc[] = opm$.create.paths
199
+ const updatePaths: PathDesc[] = [
200
+ ...(opm$.update?.paths ?? []),
201
+ ...(null == opm.patch ? (opm$.patch?.paths ?? []) : []),
202
+ ]
203
+
204
+ if (null != opm.update && updatePaths.some((p) => !hasAction(p)) &&
205
+ paths.every(addressesRecord)) {
206
+ opm.update.points.push(...opm.create.points)
207
+ opm.create = undefined
208
+ return
209
+ }
210
+
211
+ const verbs = [...new Set(paths.flatMap((p) => Object.keys(p.action ?? {})))].sort()
212
+ ctx.warn?.({
213
+ note: `Create on entity=${on.gent.name} has no route of its own, only the action ` +
214
+ `${verbs.join(', ')}, so a plain create sends it. Set the route's op in the ` +
215
+ `guide if it should not.`,
216
+ entity: on.gent.name,
217
+ op: 'create',
218
+ })
219
+ }
220
+
221
+
222
+ // A custom method's `{id}:verb` keeps its placeholder in a literal, so the
223
+ // rename is what names the record there.
224
+ function addressesRecord(p: PathDesc): boolean {
225
+ return Object.values(p.rename?.param ?? {})
226
+ .some((to: any) => 'id' === ('string' === typeof to ? to : to?.target)) ||
227
+ p.segments.some((seg) => 'id' === seg.var)
228
+ }
229
+
230
+
231
+ function hasAction(p: PathDesc): boolean {
232
+ return 0 < Object.keys(p.action ?? {}).length
233
+ }
234
+
235
+
189
236
  // True when every path collected under the op carries a guide action.
190
237
  function onlyActionPaths(gent: GuideEntity, opname: OpName): boolean {
191
238
  const paths: PathDesc[] = (gent as any).opm$?.[opname]?.paths ?? []
192
- return 0 < paths.length &&
193
- paths.every((p: PathDesc) => 0 < Object.keys(p.action ?? {}).length)
239
+ return 0 < paths.length && paths.every(hasAction)
194
240
  }
195
241
 
196
242