|
| 1 | +"use server"; |
| 2 | +import { getAuthToken } from "app/api/lib/getAuthToken"; |
| 3 | + |
| 4 | +const UB_BASE_URL = process.env.NEXT_PUBLIC_THIRDWEB_BRIDGE_HOST; |
| 5 | + |
| 6 | +type Webhook = { |
| 7 | + url: string; |
| 8 | + label: string; |
| 9 | + active: boolean; |
| 10 | + createdAt: string; |
| 11 | + id: string; |
| 12 | + secret: string; |
| 13 | + version?: number; // TODO (UB) make this mandatory after migration |
| 14 | +}; |
| 15 | + |
| 16 | +export async function getWebhooks(props: { |
| 17 | + clientId: string; |
| 18 | +}) { |
| 19 | + const authToken = await getAuthToken(); |
| 20 | + const res = await fetch(`${UB_BASE_URL}/v1/developer/webhooks`, { |
| 21 | + method: "GET", |
| 22 | + headers: { |
| 23 | + "Content-Type": "application/json", |
| 24 | + "x-client-id-override": props.clientId, |
| 25 | + Authorization: `Bearer ${authToken}`, |
| 26 | + }, |
| 27 | + }); |
| 28 | + |
| 29 | + if (!res.ok) { |
| 30 | + const text = await res.text(); |
| 31 | + throw new Error(text); |
| 32 | + } |
| 33 | + |
| 34 | + const json = await res.json(); |
| 35 | + return json.data as Array<Webhook>; |
| 36 | +} |
| 37 | + |
| 38 | +export async function createWebhook(props: { |
| 39 | + clientId: string; |
| 40 | + version?: number; |
| 41 | + url: string; |
| 42 | + label: string; |
| 43 | + secret?: string; |
| 44 | +}) { |
| 45 | + const authToken = await getAuthToken(); |
| 46 | + const res = await fetch(`${UB_BASE_URL}/v1/developer/webhooks`, { |
| 47 | + method: "POST", |
| 48 | + body: JSON.stringify({ |
| 49 | + url: props.url, |
| 50 | + label: props.label, |
| 51 | + version: props.version, |
| 52 | + secret: props.secret, |
| 53 | + }), |
| 54 | + headers: { |
| 55 | + "Content-Type": "application/json", |
| 56 | + "x-client-id-override": props.clientId, |
| 57 | + Authorization: `Bearer ${authToken}`, |
| 58 | + }, |
| 59 | + }); |
| 60 | + |
| 61 | + if (!res.ok) { |
| 62 | + const text = await res.text(); |
| 63 | + throw new Error(text); |
| 64 | + } |
| 65 | + |
| 66 | + return; |
| 67 | +} |
| 68 | + |
| 69 | +export async function deleteWebhook(props: { |
| 70 | + clientId: string; |
| 71 | + webhookId: string; |
| 72 | +}) { |
| 73 | + const authToken = await getAuthToken(); |
| 74 | + const res = await fetch( |
| 75 | + `${UB_BASE_URL}/v1/developer/webhooks/${props.webhookId}`, |
| 76 | + { |
| 77 | + method: "DELETE", |
| 78 | + headers: { |
| 79 | + "Content-Type": "application/json", |
| 80 | + "x-client-id-override": props.clientId, |
| 81 | + Authorization: `Bearer ${authToken}`, |
| 82 | + }, |
| 83 | + }, |
| 84 | + ); |
| 85 | + |
| 86 | + if (!res.ok) { |
| 87 | + const text = await res.text(); |
| 88 | + throw new Error(text); |
| 89 | + } |
| 90 | + |
| 91 | + return; |
| 92 | +} |
| 93 | + |
| 94 | +export type Fee = { |
| 95 | + feeRecipient: string; |
| 96 | + feeBps: number; |
| 97 | + createdAt: string; |
| 98 | + updatedAt: string; |
| 99 | +}; |
| 100 | + |
| 101 | +export async function getFees(props: { |
| 102 | + clientId: string; |
| 103 | +}) { |
| 104 | + const authToken = await getAuthToken(); |
| 105 | + const res = await fetch(`${UB_BASE_URL}/v1/developer/fees`, { |
| 106 | + method: "GET", |
| 107 | + headers: { |
| 108 | + "Content-Type": "application/json", |
| 109 | + "x-client-id-override": props.clientId, |
| 110 | + Authorization: `Bearer ${authToken}`, |
| 111 | + }, |
| 112 | + }); |
| 113 | + |
| 114 | + if (!res.ok) { |
| 115 | + const text = await res.text(); |
| 116 | + throw new Error(text); |
| 117 | + } |
| 118 | + |
| 119 | + const json = await res.json(); |
| 120 | + return json.data as Fee; |
| 121 | +} |
| 122 | + |
| 123 | +export async function updateFee(props: { |
| 124 | + clientId: string; |
| 125 | + feeRecipient: string; |
| 126 | + feeBps: number; |
| 127 | +}) { |
| 128 | + const authToken = await getAuthToken(); |
| 129 | + const res = await fetch(`${UB_BASE_URL}/v1/developer/fees`, { |
| 130 | + method: "PUT", |
| 131 | + headers: { |
| 132 | + "Content-Type": "application/json", |
| 133 | + "x-client-id-override": props.clientId, |
| 134 | + Authorization: `Bearer ${authToken}`, |
| 135 | + }, |
| 136 | + body: JSON.stringify({ |
| 137 | + feeRecipient: props.feeRecipient, |
| 138 | + feeBps: props.feeBps, |
| 139 | + }), |
| 140 | + }); |
| 141 | + |
| 142 | + if (!res.ok) { |
| 143 | + const text = await res.text(); |
| 144 | + throw new Error(text); |
| 145 | + } |
| 146 | + |
| 147 | + return; |
| 148 | +} |
0 commit comments