import type { OAuthUserAgent } from "@atcute/oauth-browser-client"; import { mintServiceAuth, serviceDidForHost } from "$lib/auth/agent"; import { mainSchema as redeemSchema } from "$lib/api/lexicons/types/org/tangled/temp/server/redeemInviteCode"; import { serviceUrlFor, toResponseError } from "$lib/api/_request"; export const REDEEM_NSID = redeemSchema.nsid; export const redeemInviteCode = async ( agent: OAuthUserAgent, knot: string, code: string, fetch: typeof globalThis.fetch = globalThis.fetch ): Promise => { const token = await mintServiceAuth(agent, { aud: serviceDidForHost(knot), lxm: REDEEM_NSID }); const response = await fetch(new URL(`/xrpc/${REDEEM_NSID}`, `${serviceUrlFor(knot)}/`), { method: "POST", headers: { "content-type": "application/json", accept: "application/json", authorization: `Bearer ${token}` }, body: JSON.stringify({ code }) }); if (!response.ok) throw await toResponseError(response); };