import {z} from 'zod' export type TeamProfile = {name: string, public_id?: string | undefined, url?: string | undefined, avatar?: (string | null) | undefined}; export type ProfileListWithEnrolledTeam = {url?: string | undefined, public_id: string, name?: string | undefined, gender?: ('M' | 'F' | 'X') | undefined, team?: TeamProfile | undefined, reference: string, is_active?: boolean | undefined}; export type ChallengeDetail = {url?: string | undefined, public_id?: string | undefined, title: string, slogan?: string | undefined, description?: string | undefined, start: string, end?: (string | null) | undefined, type: 'INDIVIDUAL' | 'GROUP' | 'TEAM', goal: 'DAILY' | 'TOTAL' | 'WEEKLY' | 'MONTHLY', goal_value?: (number | null) | undefined, goal_type: 'POINTS' | 'DISTANCE' | 'CALORIES' | 'DURATION' | 'STEPS', visibility: 'PUBLIC' | 'PRIVATE', num_members?: string | undefined, calculation_method: 'SUM' | 'AVERAGE', goal_progress?: number | undefined, enrollment_method: 'AUTOMATIC' | 'MANUAL', default_enrollment_time?: ('CHALLENGE_START' | 'ENROLLMENT_TIME') | undefined, members?: Array | undefined}; export type PatchApiChallengePublicIdArgs = {body: ChallengeDetail}; export type PatchApiChallengePublicIdOptions = {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 }; /** * Partial edit challenge * * PATCH /challenge/{public_id}/ */ export const patchApiChallengePublicId = async (args: PatchApiChallengePublicIdArgs, options?: PatchApiChallengePublicIdOptions) => { const {baseUrl = "https://api.wefitter.com/api/v1", fetch: fetchFn = fetch, BearerAdmin} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAdmin !== undefined) headers.set("Authorization", BearerAdmin) const url = `${baseUrl.replace(/\/+$/, '')}/challenge/${encodeURIComponent(String(args.public_id))}/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(challengeDetail, await res.json()) };