import {z} from 'zod' export type GetApiApiV1ExpertsProfileOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const expertProfile = z.object({id: z.string(), email: z.string(), skills: z.array(z.string()), rating: z.number(), total_answers: z.number().int(), credits_earned: z.number().int(), status: z.string(), worldid_nullifier: z.string().nullable().optional(), worldid_verified: z.boolean().optional(), stripe_account_id: z.string().nullable().optional(), stripe_onboarding_complete: z.boolean().optional(), hourly_rate: z.number().int().optional(), work_schedule: z.record(z.string(), z.unknown()).nullable().optional(), schedule_paused: z.boolean().optional(), name: z.string().nullable().optional(), profile_picture_url: z.string().nullable().optional(), linkedin_url: 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 }; /** * Get Expert Profile * * GET /api/v1/experts/profile */ export const getApiApiV1ExpertsProfile = async (options: GetApiApiV1ExpertsProfileOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/experts/profile${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(expertProfile, await res.json()) };