import {z} from 'zod' export type UpdateUserParams = {$schema?: string | undefined, company?: (string | null) | undefined, country?: (string | null) | undefined, first_name?: (string | null) | undefined, is_alerts_enabled?: (boolean | null) | undefined, is_subscribed_to_changelog?: (boolean | null) | undefined, is_subscribed_to_marketing_emails?: (boolean | null) | undefined, is_subscribed_to_newsletter?: (boolean | null) | undefined, last_name?: (string | null) | undefined, password?: (string | null) | undefined, promo_code?: (string | null) | undefined}; export type UpdateApiUserArgs = {body: UpdateUserParams}; export type UpdateApiUserOptions = {baseUrl?: string | undefined, AccessTokenAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const user = z.object({company: z.string(), country: z.string(), created_at: z.string(), email: z.string(), first_name: z.string(), is_alerts_enabled: z.boolean(), is_confirmed: z.boolean(), is_invitation_claimed: z.boolean().optional(), is_subscribed_to_changelog: z.boolean(), last_name: z.string(), profile_photo_url: z.string(), promo_code: z.string()}); export const updateUserResponse = z.object({$schema: z.string().optional(), user: user}); 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 user profile * * PUT /user * * Also accepts auth: APIKeyAuth */ export const updateApiUser = async (args: UpdateApiUserArgs, options?: UpdateApiUserOptions) => { const {baseUrl = "https://api.tower.dev/v1", fetch: fetchFn = fetch, AccessTokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (AccessTokenAuth !== undefined) headers.set('authorization', `Bearer ${AccessTokenAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/user${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(updateUserResponse, await res.json()) };