import {z} from 'zod' export type CreateUserProfileDto = {name: string, username?: Record | undefined, type: number, companyName?: Record | undefined, jobTitle?: Record | undefined, birthday?: Record | undefined, phone?: Record | undefined, timezone?: Record | undefined, referralCode?: Record | undefined, onboardingCompleted?: boolean | undefined}; export type CreateApiUsersMeProfileArgs = {body: CreateUserProfileDto}; export type CreateApiUsersMeProfileOptions = {baseUrl: string, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const user = z.object({id: z.number(), privyId: z.string(), email: z.object({}).nullable(), status: z.union([z.literal(1), z.literal(2), z.literal(3)]), lastLoginCountry: z.object({}).nullable(), lastLoginCountryAt: z.object({}).nullable(), created_at: z.string(), updated_at: z.string(), userProfile: z.lazy(() => userProfile.nullable())}); export const referralCode = z.object({id: z.number(), wallet: z.string(), code: z.string(), codeNormalized: z.string(), createdAt: z.string()}); export const userProfile: z.ZodType = z.object({id: z.number(), name: z.string(), username: z.object({}).nullable(), type: z.union([z.literal(1), z.literal(2)]), companyName: z.object({}).nullable(), jobTitle: z.object({}).nullable(), birthday: z.object({}).nullable(), phone: z.object({}).nullable(), timezone: z.object({}).nullable(), avatar: z.object({}).nullable(), userId: z.number(), user: user, referralCode: referralCode.nullable(), onboardingCompleted: z.boolean(), currency: z.object({}).nullable(), inputMode: z.object({}).nullable(), language: z.object({}).nullable(), sessionTimeout: z.object({}).nullable(), appLockEnabled: z.boolean(), created_at: z.string(), updated_at: z.string()}); 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 }; /** * Create the current user profile and mark account setup completed * * POST /users/me/profile */ export const createApiUsersMeProfile = async (args: CreateApiUsersMeProfileArgs, options: CreateApiUsersMeProfileOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/users/me/profile${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(userProfile, await res.json()) };