Something went wrong. Try again.
Monorepo for Tangled tangled.org
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869import { authedGet, authedPost, type DeliberiContext } from "$lib/api/deliberi";import { buildUrl } from "$lib/api/_request";
export interface ListEmailItem { address: string; verified: boolean; primary: boolean; createdAt: string;}
const LIST_EMAILS = "org.tangled.temp.account.listEmails";const ADD_EMAIL = "org.tangled.temp.account.addEmail";const VERIFY_EMAIL = "org.tangled.temp.account.verifyEmail";const DELETE_EMAIL = "org.tangled.temp.account.deleteEmail";const SET_PRIMARY_EMAIL = "org.tangled.temp.account.setPrimaryEmail";
export const listEmails = (ctx: DeliberiContext): Promise<{ emails: ListEmailItem[] }> => authedGet(ctx, LIST_EMAILS);
export const addEmail = (ctx: DeliberiContext, email: string): Promise<void> => authedPost(ctx, ADD_EMAIL, { email }).then(() => undefined);
export class VerificationError extends Error { constructor( message: string, readonly kind: "invalid" | "unavailable" ) { super(message); }}
export const verifyEmailByToken = async ( serviceUrl: string, token: string, fetchFn: typeof globalThis.fetch = fetch): Promise<{ email: string }> => { const response = await fetchFn(buildUrl(serviceUrl, VERIFY_EMAIL), { method: "POST", headers: { "content-type": "application/json", accept: "application/json" }, body: JSON.stringify({ token }) }); if (response.status === 400) { throw new VerificationError( "The verification link is invalid or has already been used.", "invalid" ); } if (!response.ok) { throw new VerificationError( `Could not reach the verification service (${response.status}).`, "unavailable" ); } const text = await response.text(); return text ? JSON.parse(text) : { email: "" };};
export const deleteEmail = (ctx: DeliberiContext, email: string): Promise<void> => authedPost(ctx, DELETE_EMAIL, { email }).then(() => undefined);
export const setPrimaryEmail = (ctx: DeliberiContext, email: string): Promise<void> => authedPost(ctx, SET_PRIMARY_EMAIL, { email }).then(() => undefined);
export const markPrimary = (rows: ListEmailItem[], address: string): ListEmailItem[] => rows.map((row) => ({ ...row, primary: row.address === address }));
export const removeEmailRow = (rows: ListEmailItem[], address: string): ListEmailItem[] => rows.filter((row) => row.address !== address);