Repository navigation
Expand file tree
/
Copy pathwebhooks.ts
More file actions
118 lines (116 loc) · 3.32 KB
/
Copy pathwebhooks.ts
File metadata and controls
118 lines (116 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import {
createWebhookEndpoint,
deleteWebhookEndpoint,
listWebhookDeliveries,
listWebhookEndpoints,
updateWebhookEndpoint,
} from "@core/modules/automation/webhooks.js";
import type { DB } from "@db/index.js";
import type { WebhookDB } from "@db/webhook-database.js";
import { Type } from "@sinclair/typebox";
import type { FastifyInstance, FastifyRequest } from "fastify";
import type { Actor } from "@core/modules/identity/passwords.js";
import {
resolveWebhookAddress,
type WebhookResolve,
} from "../services/webhooks/http.js";
const eventName = Type.String({ minLength: 1, maxLength: 80 });
const headerField = Type.Object(
{
name: Type.String({ minLength: 1, maxLength: 80 }),
value: Type.String({ maxLength: 4000 }),
},
{ additionalProperties: false },
);
const headersField = Type.Optional(Type.Array(headerField, { maxItems: 30 }));
const bodyFields = {
name: Type.Optional(Type.String({ minLength: 1, maxLength: 80 })),
url: Type.Optional(Type.String({ minLength: 8, maxLength: 2000 })),
events: Type.Optional(Type.Array(eventName, { maxItems: 40 })),
headers: headersField,
enabled: Type.Optional(Type.Boolean()),
};
export function registerWebhooks(
api: FastifyInstance,
db: DB,
hooks: WebhookDB,
admin: (request: FastifyRequest) => Actor,
resolve?: WebhookResolve,
) {
const params = Type.Object({ id: Type.String({ format: "uuid" }) });
async function callbackUrl(value: string) {
await resolveWebhookAddress(value, resolve);
return value;
}
api.get("/api/v1/admin/webhooks", async (request) => {
admin(request);
return listWebhookEndpoints(hooks);
});
api.post<{
Body: {
name: string;
url: string;
events: string[];
headers?: { name: string; value: string }[];
};
}>(
"/api/v1/admin/webhooks",
{
schema: {
body: Type.Object(
{
name: Type.String({ minLength: 1, maxLength: 80 }),
url: Type.String({ minLength: 8, maxLength: 2000 }),
events: Type.Array(eventName, { minItems: 1, maxItems: 40 }),
headers: headersField,
},
{ additionalProperties: false },
),
},
},
async (request) => {
admin(request);
await callbackUrl(request.body.url);
return createWebhookEndpoint(db, hooks, request.body);
},
);
api.patch<{
Params: { id: string };
Body: {
name?: string;
url?: string;
events?: string[];
headers?: { name: string; value: string }[];
enabled?: boolean;
};
}>(
"/api/v1/admin/webhooks/:id",
{
schema: {
params,
body: Type.Object(bodyFields, { additionalProperties: false }),
},
},
async (request) => {
admin(request);
if (request.body.url) await callbackUrl(request.body.url);
return updateWebhookEndpoint(hooks, request.params.id, request.body);
},
);
api.delete<{ Params: { id: string } }>(
"/api/v1/admin/webhooks/:id",
{ schema: { params } },
async (request) => {
admin(request);
return deleteWebhookEndpoint(hooks, request.params.id);
},
);
api.get<{ Params: { id: string } }>(
"/api/v1/admin/webhooks/:id/deliveries",
{ schema: { params } },
async (request) => {
admin(request);
return listWebhookDeliveries(hooks, request.params.id);
},
);
}