Open History to see the last commit
1import {
2 MAX_WEBHOOK_DELIVERY_AGE_MINUTES,
3 MAX_WEBHOOK_FILTERS,
4 ScheduledTaskWebhookFilter,
5 type ScheduledTaskWebhookFilterOp,
6} from "@t3tools/contracts";
7import * as Option from "effect/Option";
8import * as Schema from "effect/Schema";
9import { mFallback, msg } from "gt-i18n";
10import type { M } from "gt-i18n";
11
12export const DEFAULT_WEBHOOK_PROMPT = "Handle this webhook:\n{{body}}";
13
14/** Blank means "no limit"; undefined means the input is not a valid limit, which blocks saving. */
15export function parseMaxDeliveryAge(value: string): number | null | undefined {
16 if (value.trim() === "") return null;
17 const minutes = Number(value.trim());
18 return Number.isInteger(minutes) && minutes > 0 && minutes <= MAX_WEBHOOK_DELIVERY_AGE_MINUTES
19 ? minutes
20 : undefined;
21}
22
23/**
24 * One editable filter row. `in` takes a comma-separated list, which is
25 * lossless because list values can't contain commas; `exists` ignores the value.
26 */
27export interface WebhookFilterDraft {
28 /** Stable row identity for the form; never sent to the server. */
29 readonly id: string;
30 readonly path: string;
31 readonly op: ScheduledTaskWebhookFilterOp;
32 readonly value: string;
33}
34
35export const WEBHOOK_FILTER_OPS: ReadonlyArray<{
36 readonly op: ScheduledTaskWebhookFilterOp;
37 readonly label: string;
38}> = [
39 { op: "equals", label: msg("equals") },
40 { op: "notEquals", label: msg("does not equal") },
41 { op: "in", label: msg("is one of") },
42 { op: "startsWith", label: msg("starts with") },
43 { op: "exists", label: msg("exists") },
44];
45
46let lastWebhookFilterDraftId = 0;
47const nextWebhookFilterDraftId = () => `filter-${++lastWebhookFilterDraftId}`;
48
49export function emptyWebhookFilterDraft(): WebhookFilterDraft {
50 return { id: nextWebhookFilterDraftId(), path: "", op: "equals", value: "" };
51}
52
53/** By id, so a removed row's identity never passes to the row after it. */
54export function removeWebhookFilterDraft(
55 drafts: ReadonlyArray<WebhookFilterDraft>,
56 id: string,
57): ReadonlyArray<WebhookFilterDraft> {
58 return drafts.filter((draft) => draft.id !== id);
59}
60
61export function webhookFilterDrafts(
62 filters: ReadonlyArray<ScheduledTaskWebhookFilter> | undefined,
63): ReadonlyArray<WebhookFilterDraft> {
64 return (filters ?? []).map((filter) => ({
65 id: nextWebhookFilterDraftId(),
66 path: filter.path,
67 op: filter.op,
68 value:
69 filter.op === "exists" ? "" : filter.op === "in" ? filter.value.join(", ") : filter.value,
70 }));
71}
72
73const decodeFilter = Schema.decodeUnknownOption(ScheduledTaskWebhookFilter);
74
75/**
76 * Checks each row with the server's own schema, so a draft that parses here
77 * also saves. The error names the first row that would not.
78 */
79export function parseWebhookFilterDrafts(
80 drafts: ReadonlyArray<WebhookFilterDraft>,
81 resolve: M = mFallback,
82): { readonly filters: ReadonlyArray<ScheduledTaskWebhookFilter> } | { readonly error: string } {
83 if (drafts.length > MAX_WEBHOOK_FILTERS) {
84 return { error: resolve(msg`Use at most ${{ max: MAX_WEBHOOK_FILTERS }} filters.`) };
85 }
86 const filters: ScheduledTaskWebhookFilter[] = [];
87 for (const [index, draft] of drafts.entries()) {
88 const path = draft.path.trim();
89 const filter = decodeFilter(
90 draft.op === "exists"
91 ? { path, op: draft.op }
92 : draft.op === "in"
93 ? {
94 path,
95 op: draft.op,
96 value: draft.value
97 .split(",")
98 .map((item) => item.trim())
99 .filter((item) => item !== ""),
100 }
101 : { path, op: draft.op, value: draft.value },
102 );
103 if (Option.isNone(filter)) {
104 return {
105 error:
106 draft.op === "exists"
107 ? resolve(
108 msg`Filter ${{ number: index + 1 }} needs a path under body, headers or query.`,
109 )
110 : draft.op === "in"
111 ? resolve(
112 msg`Filter ${{ number: index + 1 }} needs a path under body, headers or query, and at least one value.`,
113 )
114 : resolve(
115 msg`Filter ${{ number: index + 1 }} needs a path under body, headers or query, and a value.`,
116 ),
117 };
118 }
119 filters.push(filter.value);
120 }
121 return { filters };
122}
123
124/**
125 * Whether a form's filter rows differ from the ones it opened with. A save
126 * leaves `filters` out unless they did, so editing only the title can't
127 * overwrite filters another device changed in the meantime.
128 */
129export function webhookFilterDraftsChanged(
130 saved: ReadonlyArray<WebhookFilterDraft>,
131 current: ReadonlyArray<WebhookFilterDraft>,
132): boolean {
133 return (
134 saved.length !== current.length ||
135 saved.some(
136 (filter, index) =>
137 filter.path !== current[index]?.path ||
138 filter.op !== current[index]?.op ||
139 (filter.op !== "exists" && filter.value !== current[index]?.value),
140 )
141 );
142}