diff --git a/src/api/client.ts b/src/api/client.ts index 1153e5d..753a7cd 100644 --- a/src/api/client.ts +++ b/src/api/client.ts @@ -15,9 +15,27 @@ export const api = { setToken(null); localStorage.removeItem("token"); window.location.href = "/"; - return; } - return response.json(); + return response; + }, + post: async (url: string, body: any) => { + const response = await fetch(`${FAUCET_API_URL}/${url}`, { + method: "POST", + body: JSON.stringify(body), + headers: { + Authorization: `Bearer ${token()}`, + "Content-Type": "application/json", + }, + }); + + if (response.status === 401) { + // Handle unauthorized + setToken(null); + localStorage.removeItem("token"); + window.location.href = "/"; + } + + return response; }, }; \ No newline at end of file diff --git a/src/components/Faucet.tsx b/src/components/Faucet.tsx index 9ebef41..7a10018 100644 --- a/src/components/Faucet.tsx +++ b/src/components/Faucet.tsx @@ -1,6 +1,7 @@ import { Match, Switch, createSignal } from "solid-js"; import { createRouteAction, useSearchParams } from "solid-start"; -import { token } from "~/stores/auth"; +import {setToken, token} from "~/stores/auth"; +import {api} from "~/api/client"; const FAUCET_API_URL = import.meta.env.VITE_FAUCET_API; @@ -64,13 +65,9 @@ export function Faucet() { // Strip surrounding quotation marks if they exist toAddress = toAddress.replace(/^"|"$/g, '').trim(); - const res = await fetch(`${FAUCET_API_URL}/api/onchain`, { - method: "POST", - body: JSON.stringify({ sats: howMuchSats, address: toAddress }), - headers: { - "Content-Type": "application/json", - Authorization: `Bearer ${token()}`, - }, + const res = await api.post("api/onchain", { + sats: howMuchSats, + address: toAddress, }); if (!res.ok) { diff --git a/src/components/LnChannel.tsx b/src/components/LnChannel.tsx index bedbc04..dbb32c8 100644 --- a/src/components/LnChannel.tsx +++ b/src/components/LnChannel.tsx @@ -1,6 +1,7 @@ import { createSignal, Match, Switch } from "solid-js"; import { createRouteAction } from "solid-start"; -import { token } from "~/stores/auth"; +import {setToken, token} from "~/stores/auth"; +import {api} from "~/api/client"; const FAUCET_API_URL = import.meta.env.VITE_FAUCET_API; @@ -90,18 +91,11 @@ export function LnChannel() { } const pushAmount = Math.round(capacity / 100) * pushPercentage; - const res = await fetch(`${FAUCET_API_URL}/api/channel`, { - method: "POST", - body: JSON.stringify({ + const res = await api.post("api/channel", { push_amount: pushAmount, capacity, pubkey, host, - }), - headers: { - "Content-Type": "application/json", - "Authorization": `Bearer ${token()}`, - }, }); if (!res.ok) { diff --git a/src/components/LnFaucet.tsx b/src/components/LnFaucet.tsx index 18b04be..a664504 100644 --- a/src/components/LnFaucet.tsx +++ b/src/components/LnFaucet.tsx @@ -1,6 +1,7 @@ -import { token } from "~/stores/auth"; +import {setToken, token} from "~/stores/auth"; import { Match, Switch } from "solid-js"; import { createRouteAction } from "solid-start"; +import {api} from "~/api/client"; const FAUCET_API_URL = import.meta.env.VITE_FAUCET_API; @@ -67,13 +68,8 @@ export function LnFaucet() { } // const result = await payInvoice(bolt11); - const res = await fetch(`${FAUCET_API_URL}/api/lightning`, { - method: "POST", - body: JSON.stringify({ bolt11 }), - headers: { - "Content-Type": "application/json", - "Authorization": `Bearer ${token()}`, - }, + const res = await api.post("api/lightning", { + bolt11 }); if (!res.ok) { diff --git a/src/components/NWC.tsx b/src/components/NWC.tsx index d55008a..d35c100 100644 --- a/src/components/NWC.tsx +++ b/src/components/NWC.tsx @@ -1,8 +1,8 @@ import { Match, Switch, createSignal } from "solid-js"; import { createRouteAction } from "solid-start"; -import { token } from "~/stores/auth"; import NDK, { NDKEvent, NDKPrivateKeySigner } from "@nostr-dev-kit/ndk"; +import {api} from "~/api/client"; const FAUCET_API_URL = import.meta.env.VITE_FAUCET_API; @@ -94,7 +94,7 @@ async function publishMultiInvoiceRequest(invoices: string[], nwc: NWCInfo) { event.content = JSON.stringify({ method: "multi_pay_invoice", params: { - invoices: invoicesParam, + invoices: invoicesParam, }, }); event.tags = [["p", nwc.npubHex]]; @@ -107,13 +107,8 @@ async function publishMultiInvoiceRequest(invoices: string[], nwc: NWCInfo) { async function fetchBolt11(): Promise<{ bolt11: string }> { - const res = await fetch(`${FAUCET_API_URL}/api/bolt11`, { - method: "POST", - body: JSON.stringify({ amount_sats: 21 }), - headers: { - "Content-Type": "application/json", - "Authorization": `Bearer ${token()}`, - }, + const res = await api.post("api/bolt11", { + amount_sats: 21 }); if (!res.ok) { @@ -148,7 +143,7 @@ function Zapper(props: { nwc: NWCInfo }) { const bolt11 = (await fetchBolt11()).bolt11; invoices.push(bolt11); } - await publishMultiInvoiceRequest(invoices, props.nwc); + await publishMultiInvoiceRequest(invoices, props.nwc); } catch (e) { console.error(e); } @@ -195,8 +190,8 @@ function Zapper(props: { nwc: NWCInfo }) { let keysendParams = []; for (let i = 0; i < 3; i++) { let keysendParam = { - id: String(Math.floor(Math.random() * 10000000)), - pubkey: "02465ed5be53d04fde66c9418ff14a5f2267723810176c9212b722e542dc1afb1b", + id: String(Math.floor(Math.random() * 10000000)), + pubkey: "02465ed5be53d04fde66c9418ff14a5f2267723810176c9212b722e542dc1afb1b", amount: 42 * 1000 } keysendParams.push(keysendParam) @@ -219,7 +214,7 @@ function Zapper(props: { nwc: NWCInfo }) { setLoading(false); } - + return (