All files / src/features/profile/api profile.api.ts

100% Statements 4/4
100% Branches 0/0
100% Functions 3/3
100% Lines 4/4

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33                      7x 4x                         4x         2x    
import { httpClient } from '@/lib/http-client'
import { userSchema, type User } from '@/features/auth/api/auth.schemas'
 
export interface UpdateProfilePayload {
  avatarUrl?: string | null
  name?: string
  password?: string
  currentPassword?: string
}
 
export async function updateProfile(payload: UpdateProfilePayload): Promise<User> {
  const response = await httpClient.patch<unknown>('/users/me', payload)
  return userSchema.parse(response)
}
 
/**
 * The two ways to confirm deleting an account, and the API takes exactly one.
 *
 * `code` exists because an account created by signing in without a password has
 * no usable password at all — its hash is random bytes — so the password field
 * would lock it out of its own deletion. See `cygnus-api` #36.
 */
export type DeleteAccountProof = { currentPassword: string } | { code: string }
 
export async function deleteAccount(proof: DeleteAccountProof): Promise<void> {
  await httpClient.delete<void>('/users/me', { body: proof })
}
 
/** Mails a 6-digit code to the address on the account; spendable only on the deletion. */
export async function requestDeletionCode(): Promise<void> {
  await httpClient.post<unknown>('/users/me/deletion-code', {})
}