Skip to content

Commit 7fe7b9f

Browse files
committed
Add webhook API functions for insight dashboard
1 parent 655df03 commit 7fe7b9f

File tree

1 file changed

+201
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)