import {z} from 'zod' export type PatchApiMeProfileArgs = {body: {first_name?: string | undefined, last_name?: string | undefined, display_name?: string | undefined, description?: string | undefined, pronunciation?: string | undefined, pronouns?: string | undefined, location?: string | undefined, job_title?: string | undefined, company?: string | undefined, cell_phone?: string | undefined, contact_email?: string | undefined, hidden_contact_info?: boolean | undefined}}; export type PatchApiMeProfileOptions = {baseUrl?: string | undefined, oauth?: string | undefined, fetch?: typeof fetch | undefined}; export const verifiedAccount = z.object({service_type: z.string(), service_label: z.string(), service_icon: z.string(), url: z.string(), is_hidden: z.boolean()}); export const language = z.object({code: z.string(), name: z.string(), is_primary: z.boolean(), order: z.number().int()}); export const link = z.object({label: z.string(), url: z.string()}); export const interest = z.object({id: z.number().int(), name: z.string()}); export const cryptoWalletAddress = z.object({label: z.string(), address: z.string()}); export const galleryImage = z.object({url: z.string(), alt_text: z.string().optional()}); export const profile = z.object({user_id: z.number().int().optional(), user_login: z.string().optional(), hash: z.string(), display_name: z.string(), profile_url: z.string(), avatar_url: z.string(), avatar_alt_text: z.string(), location: z.string(), description: z.string(), job_title: z.string(), company: z.string(), verified_accounts: z.array(verifiedAccount), pronunciation: z.string(), pronouns: z.string(), timezone: z.string().optional(), languages: z.array(language).optional(), first_name: z.string().optional(), last_name: z.string().optional(), is_organization: z.boolean().optional(), header_image: z.string().optional(), background_color: z.string().optional(), links: z.array(link).optional(), interests: z.array(interest).optional(), payments: z.object({links: z.array(link), crypto_wallets: z.array(cryptoWalletAddress)}).optional(), contact_info: z.object({home_phone: z.string().optional(), work_phone: z.string().optional(), cell_phone: z.string().optional(), email: z.string().optional(), contact_form: z.string().optional(), calendar: z.string().optional()}).optional(), gallery: z.array(galleryImage).optional(), number_verified_accounts: z.number().int().optional(), last_profile_edit: z.string().nullable().optional(), registration_date: z.string().nullable().optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Update profile information for the authenticated user * * PATCH /me/profile */ export const patchApiMeProfile = async (args: PatchApiMeProfileArgs, options?: PatchApiMeProfileOptions) => { const {baseUrl = "https://api.gravatar.com/v3", fetch: fetchFn = fetch, oauth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (oauth !== undefined) headers.set('authorization', `Bearer ${oauth}`) const url = `${baseUrl.replace(/\/+$/, '')}/me/profile${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(profile, await res.json()) };