|
| 1 | +"use server"; |
| 2 | + |
| 3 | +import { THIRDWEB_INSIGHT_API_DOMAIN } from "constants/urls"; |
| 4 | + |
| 5 | +export interface WebhookResponse { |
| 6 | + id: string; |
| 7 | + name: string; |
| 8 | + team_id: string; |
| 9 | + project_id: string; |
| 10 | + webhook_url: string; |
| 11 | + webhook_secret: string; |
| 12 | + filters: WebhookFilters; |
| 13 | + suspended_at: string | null; |
| 14 | + suspended_reason: string | null; |
| 15 | + disabled: boolean; |
| 16 | + created_at: string; |
| 17 | + updated_at: string | null; |
| 18 | +} |
| 19 | + |
| 20 | +export interface WebhookFilters { |
| 21 | + "v1.events"?: { |
| 22 | + chain_ids?: string[]; |
| 23 | + addresses?: string[]; |
| 24 | + signatures?: Array<{ |
| 25 | + sig_hash: string; |
| 26 | + abi?: string; |
| 27 | + params?: Record<string, unknown>; |
| 28 | + }>; |
| 29 | + }; |
| 30 | + "v1.transactions"?: { |
| 31 | + chain_ids?: string[]; |
| 32 | + from_addresses?: string[]; |
| 33 | + to_addresses?: string[]; |
| 34 | + signatures?: Array<{ |
| 35 | + sig_hash: string; |
| 36 | + abi?: string; |
| 37 | + params?: string[]; |
| 38 | + }>; |
| 39 | + }; |
| 40 | +} |
| 41 | + |
| 42 | +interface CreateWebhookPayload { |
| 43 | + webhook_url: string; |
| 44 | + filters: WebhookFilters; |
| 45 | +} |
| 46 | + |
| 47 | +interface WebhooksListResponse { |
| 48 | + data: WebhookResponse[]; |
| 49 | +} |
| 50 | + |
| 51 | +interface WebhookSingleResponse { |
| 52 | + data: WebhookResponse; |
| 53 | +} |
| 54 | + |
| 55 | +interface TestWebhookPayload { |
| 56 | + webhook_url: string; |
| 57 | + type?: "event" | "transaction"; |
| 58 | +} |
| 59 | + |
| 60 | +interface TestWebhookResponse { |
| 61 | + success: boolean; |
| 62 | +} |
| 63 | + |
| 64 | +export async function createWebhook( |
| 65 | + payload: CreateWebhookPayload, |
| 66 | + clientId: string, |
| 67 | +): Promise<WebhookSingleResponse> { |
| 68 | + const response = await fetch(`${THIRDWEB_INSIGHT_API_DOMAIN}/v1/webhooks`, { |
| 69 | + method: "POST", |
| 70 | + headers: { |
| 71 | + "Content-Type": "application/json", |
| 72 | + "x-client-id": clientId, |
| 73 | + }, |
| 74 | + body: JSON.stringify(payload), |
| 75 | + }); |
| 76 | + |
| 77 | + if (!response.ok) { |
| 78 | + const errorText = await response.text(); |
| 79 | + throw new Error(`Failed to create webhook: ${errorText}`); |
| 80 | + } |
| 81 | + |
| 82 | + return await response.json(); |
| 83 | +} |
| 84 | + |
| 85 | +export async function getWebhooks( |
| 86 | + clientId: string, |
| 87 | +): Promise<WebhooksListResponse> { |
| 88 | + const response = await fetch(`${THIRDWEB_INSIGHT_API_DOMAIN}/v1/webhooks`, { |
| 89 | + method: "GET", |
| 90 | + headers: { |
| 91 | + "x-client-id": clientId, |
| 92 | + }, |
| 93 | + }); |
| 94 | + |
| 95 | + if (!response.ok) { |
| 96 | + const errorText = await response.text(); |
| 97 | + throw new Error(`Failed to get webhooks: ${errorText}`); |
| 98 | + } |
| 99 | + |
| 100 | + return await response.json(); |
| 101 | +} |
| 102 | + |
| 103 | +export async function deleteWebhook( |
| 104 | + webhookId: string, |
| 105 | + clientId: string, |
| 106 | +): Promise<WebhookSingleResponse> { |
| 107 | + const response = await fetch( |
| 108 | + `${THIRDWEB_INSIGHT_API_DOMAIN}/v1/webhooks/${webhookId}`, |
| 109 | + { |
| 110 | + method: "DELETE", |
| 111 | + headers: { |
| 112 | + "x-client-id": clientId, |
| 113 | + }, |
| 114 | + }, |
| 115 | + ); |
| 116 | + |
| 117 | + if (!response.ok) { |
| 118 | + const errorText = await response.text(); |
| 119 | + throw new Error(`Failed to delete webhook: ${errorText}`); |
| 120 | + } |
| 121 | + |
| 122 | + return await response.json(); |
| 123 | +} |
| 124 | + |
| 125 | +export async function testWebhook( |
| 126 | + payload: TestWebhookPayload, |
| 127 | + clientId: string, |
| 128 | +): Promise<TestWebhookResponse> { |
| 129 | + const response = await fetch( |
| 130 | + `${THIRDWEB_INSIGHT_API_DOMAIN}/v1/webhooks/test`, |
| 131 | + { |
| 132 | + method: "POST", |
| 133 | + headers: { |
| 134 | + "Content-Type": "application/json", |
| 135 | + "x-client-id": clientId, |
| 136 | + }, |
| 137 | + body: JSON.stringify(payload), |
| 138 | + }, |
| 139 | + ); |
| 140 | + |
| 141 | + if (!response.ok) { |
| 142 | + const errorText = await response.text(); |
| 143 | + throw new Error(`Failed to test webhook: ${errorText}`); |
| 144 | + } |
| 145 | + |
| 146 | + return await response.json(); |
| 147 | +} |
0 commit comments