import {z} from 'zod' export type GetApiChallengePublicIdMembersOptions = {baseUrl?: string | undefined, BearerAdmin?: string | undefined, fetch?: typeof fetch | undefined}; export const teamProfile = z.object({name: z.string().min(1).max(100), public_id: z.string().optional(), url: z.string().optional(), avatar: z.string().min(1).nullable().optional()}); export const profileListWithEnrolledTeam = z.object({url: z.string().optional(), public_id: z.string(), name: z.string().min(1).optional(), gender: z.enum(["M", "F", "X"]).optional(), team: teamProfile.optional(), reference: z.string().min(1).max(256), is_active: z.boolean().optional()}); export const challengeDetail = 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(), calculation_method: z.enum(["SUM", "AVERAGE"]), goal_progress: z.number().optional(), enrollment_method: z.enum(["AUTOMATIC", "MANUAL"]), default_enrollment_time: z.enum(["CHALLENGE_START", "ENROLLMENT_TIME"]).optional(), members: z.array(profileListWithEnrolledTeam).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 members * * GET /challenge/{public_id}/members/ * * Also accepts auth: BearerProfile */ export const getApiChallengePublicIdMembers = async (options?: GetApiChallengePublicIdMembersOptions) => { const {baseUrl = "https://api.wefitter.com/api/v1", fetch: fetchFn = fetch, BearerAdmin} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (BearerAdmin !== undefined) headers.set("Authorization", BearerAdmin) const url = `${baseUrl.replace(/\/+$/, '')}/challenge/${encodeURIComponent(String(args.public_id))}/members/${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(challengeDetail, await res.json()) };