Skip to content

Commit c1575f6

Browse files
committed
Add webhook API functions for insight dashboard
1 parent 9479d25 commit c1575f6

File tree

1 file changed

+200
-0
lines changed

1 file changed

+200
-0
lines changed
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
"use server";
2+
3+
import { getAuthToken } from "app/(app)/api/lib/getAuthToken";
4+
import { THIRDWEB_INSIGHT_API_DOMAIN } from "constants/urls";
5+
6+
export interface WebhookResponse {
7+
id: string;
8+
name: string;
9+
team_id: string;
10+
project_id: string;
11+
webhook_url: string;
12+
webhook_secret: string;
13+
filters: WebhookFilters;
14+
suspended_at: string | null;
15+
suspended_reason: string | null;
16+
disabled: boolean;
17+
created_at: string;
18+
updated_at: string | null;
19+
}
20+
21+
export interface WebhookFilters {
22+
"v1.events"?: {
23+
chain_ids?: string[];
24+
addresses?: string[];
25+
signatures?: Array<{
26+
sig_hash: string;
27+
abi?: string;
28+
params?: Record<string, unknown>;
29+
}>;
30+
};
31+
"v1.transactions"?: {
32+
chain_ids?: string[];
33+
from_addresses?: string[];
34+
to_addresses?: string[];
35+
signatures?: Array<{
36+
sig_hash: string;
37+
abi?: string;
38+
params?: Record<string, unknown>;
39+
}>;
40+
};
41+
}
42+
43+
export interface CreateWebhookPayload {
44+
name: string;
45+
webhook_url: string;
46+
filters: WebhookFilters;
47+
}
48+
49+
interface WebhooksListResponse {
50+
data: WebhookResponse[];
51+
error?: string;
52+
}
53+
54+
interface WebhookSingleResponse {
55+
data: WebhookResponse | null;
56+
error?: string;
57+
}
58+
59+
interface TestWebhookPayload {
60+
webhook_url: string;
61+
type?: "event" | "transaction";
62+
}
63+
64+
interface TestWebhookResponse {
65+
success: boolean;
66+
error?: string;
67+
}
68+
69+
export async function createWebhook(
70+
payload: CreateWebhookPayload,
71+
clientId: string,
72+
): Promise<WebhookSingleResponse> {
73+
try {
74+
const authToken = await getAuthToken();
75+
const response = await fetch(`${THIRDWEB_INSIGHT_API_DOMAIN}/v1/webhooks`, {
76+
method: "POST",
77+
headers: {
78+
"Content-Type": "application/json",
79+
"x-client-id": clientId,
80+
Authorization: `Bearer ${authToken}`,
81+
},
82+
body: JSON.stringify(payload),
83+
});
84+
85+
if (!response.ok) {
86+
const errorText = await response.text();
87+
return {
88+
data: null,
89+
error: `Failed to create webhook: ${errorText}`,
90+
};
91+
}
92+
93+
return (await response.json()) as WebhookSingleResponse;
94+
} catch (error) {
95+
return {
96+
data: null,
97+
error: `Network or parsing error: ${error instanceof Error ? error.message : "Unknown error"}`,
98+
};
99+
}
100+
}
101+
102+
export async function getWebhooks(
103+
clientId: string,
104+
): Promise<WebhooksListResponse> {
105+
try {
106+
const authToken = await getAuthToken();
107+
const response = await fetch(`${THIRDWEB_INSIGHT_API_DOMAIN}/v1/webhooks`, {
108+
method: "GET",
109+
headers: {
110+
"x-client-id": clientId,
111+
Authorization: `Bearer ${authToken}`,
112+
},
113+
});
114+
115+
if (!response.ok) {
116+
const errorText = await response.text();
117+
return {
118+
data: [],
119+
error: `Failed to get webhooks: ${errorText}`,
120+
};
121+
}
122+
123+
return (await response.json()) as WebhooksListResponse;
124+
} catch (error) {
125+
return {
126+
data: [],
127+
error: `Network or parsing error: ${error instanceof Error ? error.message : "Unknown error"}`,
128+
};
129+
}
130+
}
131+
132+
export async function deleteWebhook(
133+
webhookId: string,
134+
clientId: string,
135+
): Promise<WebhookSingleResponse> {
136+
try {
137+
const authToken = await getAuthToken();
138+
const response = await fetch(
139+
`${THIRDWEB_INSIGHT_API_DOMAIN}/v1/webhooks/${encodeURIComponent(webhookId)}`,
140+
{
141+
method: "DELETE",
142+
headers: {
143+
"x-client-id": clientId,
144+
Authorization: `Bearer ${authToken}`,
145+
},
146+
},
147+
);
148+
149+
if (!response.ok) {
150+
const errorText = await response.text();
151+
return {
152+
data: null,
153+
error: `Failed to delete webhook: ${errorText}`,
154+
};
155+
}
156+
157+
return (await response.json()) as WebhookSingleResponse;
158+
} catch (error) {
159+
return {
160+
data: null,
161+
error: `Network or parsing error: ${error instanceof Error ? error.message : "Unknown error"}`,
162+
};
163+
}
164+
}
165+
166+
export async function testWebhook(
167+
payload: TestWebhookPayload,
168+
clientId: string,
169+
): Promise<TestWebhookResponse> {
170+
try {
171+
const authToken = await getAuthToken();
172+
const response = await fetch(
173+
`${THIRDWEB_INSIGHT_API_DOMAIN}/v1/webhooks/test`,
174+
{
175+
method: "POST",
176+
headers: {
177+
"Content-Type": "application/json",
178+
"x-client-id": clientId,
179+
Authorization: `Bearer ${authToken}`,
180+
},
181+
body: JSON.stringify(payload),
182+
},
183+
);
184+
185+
if (!response.ok) {
186+
const errorText = await response.text();
187+
return {
188+
success: false,
189+
error: `Failed to test webhook: ${errorText}`,
190+
};
191+
}
192+
193+
return (await response.json()) as TestWebhookResponse;
194+
} catch (error) {
195+
return {
196+
success: false,
197+
error: `Network or parsing error: ${error instanceof Error ? error.message : "Unknown error"}`,
198+
};
199+
}
200+
}

0 commit comments

Comments
 (0)