import {z} from 'zod' export type GetApiProfilePublicIdChallengesOptions = {baseUrl?: string | undefined, BearerProfile?: string | undefined, fetch?: typeof fetch | undefined}; export const enrollmentInformation = z.object({team: z.string().min(1).optional(), team_public_id: z.string().min(1).optional(), team_size: z.number().int().optional(), leaderboard_rank: z.number().int().optional(), leaderboard_score: z.number().optional()}); export const profileChallengeList = z.object({url: z.string().optional(), public_id: z.string().optional(), title: z.string().min(1), slogan: z.string().optional(), description: z.string().optional(), start: z.string(), end: z.string().nullable().optional(), type: z.enum(["INDIVIDUAL", "GROUP", "TEAM"]), goal: z.enum(["DAILY", "TOTAL", "WEEKLY", "MONTHLY"]), goal_value: z.number().int().gte(-2147483648).lte(2147483647).nullable().optional(), goal_type: z.enum(["POINTS", "DISTANCE", "CALORIES", "DURATION", "STEPS"]), visibility: z.enum(["PUBLIC", "PRIVATE"]), num_members: z.string().optional(), leaderboard_rank: z.number().int().optional(), leaderboard_score: z.number().optional(), is_participating: z.boolean().optional(), enrollment: enrollmentInformation.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 }; /** * This docstring is used by Django Rest Framework * * GET /profile/{public_id}/challenges/ */ export const getApiProfilePublicIdChallenges = async (options?: GetApiProfilePublicIdChallengesOptions) => { const {baseUrl = "https://api.wefitter.com/api/v1", fetch: fetchFn = fetch, BearerProfile} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (BearerProfile !== undefined) headers.set("Authorization", BearerProfile) const url = `${baseUrl.replace(/\/+$/, '')}/profile/${encodeURIComponent(String(args.public_id))}/challenges/${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(profileChallengeList, await res.json()) };