import {z} from 'zod' export type SignupGoal = {kind?: ('improve_skill' | 'lose_weight' | 'increase_strength' | 'build_muscles') | undefined, start_date?: string | undefined, end_date?: (string | null) | undefined, technique_guide_id?: (number | null) | undefined, current_weight?: (number | null) | undefined, planned_weight?: (number | null) | undefined, end_weight?: (number | null) | undefined, exercise_id?: (number | null) | undefined, muscle?: (string | null) | undefined}; export type SignupUser = {email: string, username: string, name: string, password: string, gender?: string | undefined, level?: string | undefined, height?: number | undefined, height_unit?: string | undefined, weight?: number | undefined, goal_weight?: number | undefined, weight_unit?: string | undefined, goal_weight_unit?: string | undefined, max_pushups?: number | undefined, max_pullups?: number | undefined, max_dips?: number | undefined, max_squats?: number | undefined, device_id?: string | undefined, main_goals?: Array | undefined, goal?: SignupGoal | undefined}; export type SignupRequest = {user: SignupUser}; export type CreateApiV1AuthSignupArgs = {body: SignupRequest}; export type CreateApiV1AuthSignupOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1AuthSignupResponse = z.object({data: z.object({}).optional(), meta: z.object({token: z.string().optional()}).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 }; /** * Register a new user * * POST /v1/auth/signup */ export const createApiV1AuthSignup = async (args: CreateApiV1AuthSignupArgs, options?: CreateApiV1AuthSignupOptions) => { const {baseUrl = "http://localhost:3000", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/auth/signup${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(createApiV1AuthSignupResponse, await res.json()) };