@kahitsan/plugin-sdk 0.6.0 → 0.7.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.
@@ -185,11 +185,123 @@ async function runFlow(flow, startId, ctx) {
185
185
  current = target;
186
186
  }
187
187
  }
188
+
189
+ // src/flow/resource-flows.ts
190
+ function crudFlow(opts) {
191
+ return {
192
+ id: opts.id,
193
+ title: opts.title,
194
+ nodes: [
195
+ {
196
+ id: "submit",
197
+ kind: "trigger",
198
+ label: opts.triggerLabel,
199
+ out: [{ id: "o", to: "commit" }]
200
+ },
201
+ {
202
+ id: "commit",
203
+ kind: "commit",
204
+ label: opts.commitLabel,
205
+ detail: opts.commitDetail,
206
+ request: opts.request,
207
+ out: [{ id: "o", to: "cond" }]
208
+ },
209
+ {
210
+ id: "cond",
211
+ kind: "condition",
212
+ label: opts.conditionLabel,
213
+ when: (ctx) => ctx.state.commit ? "yes" : "no",
214
+ out: [
215
+ { id: "yes", to: "refresh", label: "yes" },
216
+ { id: "no", to: "error", label: "no" }
217
+ ]
218
+ },
219
+ {
220
+ id: "error",
221
+ kind: "effect",
222
+ label: opts.errorLabel,
223
+ effect: "toast",
224
+ out: [{ id: "o", to: "done_err" }]
225
+ },
226
+ {
227
+ id: "refresh",
228
+ kind: "effect",
229
+ label: opts.refreshLabel,
230
+ effect: "refresh",
231
+ out: [{ id: "o", to: "done" }]
232
+ },
233
+ { id: "done", kind: "terminal", label: opts.doneLabel },
234
+ { id: "done_err", kind: "terminal", label: opts.failedLabel }
235
+ ]
236
+ };
237
+ }
238
+ function resourceFlows(cfg) {
239
+ const noun = cfg.noun;
240
+ const lower = noun.toLowerCase();
241
+ const createFlow = crudFlow({
242
+ id: `${cfg.id}.create`,
243
+ title: `Add ${noun}`,
244
+ triggerLabel: `Create ${noun}`,
245
+ commitLabel: `Create ${lower}`,
246
+ commitDetail: `POST ${cfg.basePath}`,
247
+ request: (ctx) => ({ url: cfg.basePath, method: "POST", body: ctx.state.body }),
248
+ conditionLabel: "Server accepted?",
249
+ errorLabel: "Show server error in form",
250
+ refreshLabel: "Close modal + reset form + refresh list",
251
+ doneLabel: `${noun} added`,
252
+ failedLabel: "Create failed"
253
+ });
254
+ const editFlow = crudFlow({
255
+ id: `${cfg.id}.edit`,
256
+ title: `Edit ${noun}`,
257
+ triggerLabel: "Save Changes",
258
+ commitLabel: `Update ${lower}`,
259
+ commitDetail: `PUT ${cfg.basePath}/:id`,
260
+ request: (ctx) => ({
261
+ url: `${cfg.basePath}/${ctx.state.id}`,
262
+ method: "PUT",
263
+ body: ctx.state.body
264
+ }),
265
+ conditionLabel: "Server accepted?",
266
+ errorLabel: "Show server error in form",
267
+ refreshLabel: "Update modal row + exit edit + refresh list",
268
+ doneLabel: `${noun} updated`,
269
+ failedLabel: "Update failed"
270
+ });
271
+ const archiveFlow = crudFlow({
272
+ id: `${cfg.id}.archive`,
273
+ title: `Archive ${noun}`,
274
+ triggerLabel: `Archive button (active ${lower})`,
275
+ commitLabel: `Soft-delete ${lower}`,
276
+ commitDetail: `DELETE ${cfg.basePath}/:id`,
277
+ request: (ctx) => ({ url: `${cfg.basePath}/${ctx.state.id}`, method: "DELETE" }),
278
+ conditionLabel: "Archived?",
279
+ errorLabel: "Show archive error in modal",
280
+ refreshLabel: "Refresh list + close modal",
281
+ doneLabel: `${noun} archived`,
282
+ failedLabel: "Archive failed"
283
+ });
284
+ const restoreFlow = crudFlow({
285
+ id: `${cfg.id}.restore`,
286
+ title: `Restore ${noun}`,
287
+ triggerLabel: `Restore button (archived ${lower})`,
288
+ commitLabel: `Restore ${lower}`,
289
+ commitDetail: `PATCH ${cfg.basePath}/:id/restore`,
290
+ request: (ctx) => ({ url: `${cfg.basePath}/${ctx.state.id}/restore`, method: "PATCH" }),
291
+ conditionLabel: "Restored?",
292
+ errorLabel: "Show restore error in modal",
293
+ refreshLabel: "Update modal row + refresh list",
294
+ doneLabel: `${noun} restored`,
295
+ failedLabel: "Restore failed"
296
+ });
297
+ return { createFlow, editFlow, archiveFlow, restoreFlow, all: [createFlow, editFlow, archiveFlow, restoreFlow] };
298
+ }
188
299
  export {
189
300
  FlowSteps,
190
301
  buildFlow,
191
302
  defineFlow,
192
303
  edge,
193
304
  node,
305
+ resourceFlows,
194
306
  runFlow
195
307
  };