@protobase/workflow-plugins 0.68.4 → 0.68.6

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.
package/dist/app.d.ts CHANGED
@@ -7,7 +7,7 @@ export declare const appPlugin: {
7
7
  readonly [x: string]: import("@protobase/workflows/plugin").StepType;
8
8
  };
9
9
  readonly triggers: {
10
- readonly [x: string]: import("@protobase/workflows/plugin").ReadyTrigger;
10
+ readonly [x: string]: import("@protobase/workflows/plugin").ReadyTrigger | import("@protobase/workflows/plugin").PollingTrigger | import("@protobase/workflows/plugin").WebhookTrigger;
11
11
  };
12
12
  };
13
13
  //#endregion
package/dist/app.js CHANGED
@@ -3,6 +3,8 @@ import { mailSteps } from "./mail.js";
3
3
  import { pieceSteps } from "./piece.js";
4
4
  import { recordTriggers } from "./record-trigger.js";
5
5
  import { recordsSteps } from "./records.js";
6
+ import { scheduleTriggers } from "./schedule.js";
7
+ import { webhookTriggers } from "./webhook.js";
6
8
  import { definePlugin } from "@protobase/workflows/plugin";
7
9
  //#region src/app.ts
8
10
  /** The built-in types, `app:<key>`: one plugin like any other, on the public interface alone. */
@@ -15,7 +17,11 @@ const appPlugin = definePlugin({
15
17
  ...mailSteps,
16
18
  ...pieceSteps
17
19
  },
18
- triggers: { ...recordTriggers }
20
+ triggers: {
21
+ ...recordTriggers,
22
+ ...scheduleTriggers,
23
+ ...webhookTriggers
24
+ }
19
25
  });
20
26
  //#endregion
21
27
  export { appPlugin };
@@ -0,0 +1,85 @@
1
+ //#region src/schedule.ts
2
+ const fieldBounds = [
3
+ [0, 59],
4
+ [0, 23],
5
+ [1, 31],
6
+ [1, 12],
7
+ [0, 7]
8
+ ];
9
+ const cronItem = /^(?:\*|(\d+)(?:-(\d+))?)(?:\/(\d+))?$/;
10
+ const cronField = (field, [min, max]) => field.split(",").every((item) => {
11
+ const match = cronItem.exec(item);
12
+ if (!match) return false;
13
+ const [, from, to, step] = match;
14
+ if (step !== void 0 && Number(step) === 0) return false;
15
+ if (from === void 0) return true;
16
+ const last = to === void 0 ? Number(from) : Number(to);
17
+ return Number(from) >= min && last <= max && Number(from) <= last;
18
+ });
19
+ const knownZone = (zone) => {
20
+ try {
21
+ new Intl.DateTimeFormat("en", { timeZone: zone });
22
+ return true;
23
+ } catch (error) {
24
+ if (!(error instanceof RangeError)) throw error;
25
+ return false;
26
+ }
27
+ };
28
+ /** What the schema cannot check of a schedule: its five cron fields and its IANA time zone. */
29
+ const scheduleProblems = (input) => {
30
+ const problems = [];
31
+ const fields = String(input.cron).trim().split(/\s+/);
32
+ if (fields.length !== 5 || !fields.every((field, index) => cronField(field, fieldBounds[index]))) problems.push({
33
+ field: "cron",
34
+ message: "must be a cron of 5 fields, each within its range, such as 0 6 * * *"
35
+ });
36
+ const zone = input.timezone;
37
+ if (typeof zone === "string" && !knownZone(zone)) problems.push({
38
+ field: "timezone",
39
+ message: `"${zone}" is not an IANA time zone, such as Europe/Amsterdam`
40
+ });
41
+ return problems;
42
+ };
43
+ /** `app:schedule`: one run at each time of a cron, in a time zone, with the time as the run's input. */
44
+ const scheduleTriggers = { schedule: {
45
+ kind: "polling",
46
+ label: "Schedule",
47
+ description: "Starts one run at each time of a cron schedule, such as 0 6 * * 1-5 in Europe/Amsterdam.",
48
+ input: {
49
+ type: "object",
50
+ additionalProperties: false,
51
+ required: ["cron"],
52
+ properties: {
53
+ cron: {
54
+ type: "string",
55
+ minLength: 1,
56
+ description: "five cron fields: minute, hour, day of month, month, day of week"
57
+ },
58
+ timezone: {
59
+ type: "string",
60
+ minLength: 1,
61
+ description: "an IANA time zone, such as Europe/Amsterdam; default UTC"
62
+ }
63
+ }
64
+ },
65
+ output: {
66
+ type: "object",
67
+ description: "{ scheduled_at }: the time this run was scheduled for"
68
+ },
69
+ check: scheduleProblems,
70
+ every: (input) => ({
71
+ cron: input.cron,
72
+ ...typeof input.timezone === "string" && { timezone: input.timezone }
73
+ }),
74
+ limit: 1,
75
+ start: async () => null,
76
+ poll: async ({ due }) => ({
77
+ items: [{
78
+ key: `schedule:${due.toISOString()}`,
79
+ data: { scheduled_at: due.toISOString() }
80
+ }],
81
+ cursor: due.toISOString()
82
+ })
83
+ } };
84
+ //#endregion
85
+ export { scheduleTriggers };
@@ -0,0 +1,27 @@
1
+ import { webhookInputCheck, webhookInputProperties } from "@protobase/workflows/plugin";
2
+ //#region src/webhook.ts
3
+ /**
4
+ * `app:webhook`: one run per call of the workflow's webhook URL that proves it comes from the sender, with the call as
5
+ * the run's input. A sender's `webhook-id`, or the configured idempotency header, starts one run per value.
6
+ */
7
+ const webhookTriggers = { webhook: {
8
+ kind: "webhook",
9
+ label: "Webhook",
10
+ description: "Starts one run for each call of the workflow's webhook URL that carries its secret; new_webhook_url issues the URL.",
11
+ input: {
12
+ type: "object",
13
+ additionalProperties: false,
14
+ properties: webhookInputProperties
15
+ },
16
+ output: {
17
+ type: "object",
18
+ description: "{ method, query, headers, body, received_at }: the call without its credentials; a body that makes it over 1 MiB as JSON is a file of the run"
19
+ },
20
+ check: webhookInputCheck,
21
+ receive: async ({ event, idempotencyKey }) => ({ items: [{
22
+ ...idempotencyKey !== void 0 && { key: idempotencyKey },
23
+ data: event
24
+ }] })
25
+ } };
26
+ //#endregion
27
+ export { webhookTriggers };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@protobase/workflow-plugins",
3
- "version": "0.68.4",
3
+ "version": "0.68.6",
4
4
  "description": "The built-in workflow trigger and step types, written on the public plugin interface like any other plugin.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -29,6 +29,6 @@
29
29
  "access": "public"
30
30
  },
31
31
  "dependencies": {
32
- "@protobase/workflows": "0.68.4"
32
+ "@protobase/workflows": "0.68.6"
33
33
  }
34
34
  }