@voxgig/apidef 8.32.0 → 8.32.2

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.
@@ -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