import {z} from 'zod' export type GetApiApiDailyQuestsArgs = {'x-user-id'?: (string | null) | undefined}; export type GetApiApiDailyQuestsOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const dailyQuestResponse = z.object({daily_prayer_completed: z.boolean(), daily_gratitude_completed: z.boolean(), daily_verse_interacted: z.boolean(), daily_devotional_completed: z.boolean(), daily_prayer_wall_completed: z.boolean(), daily_prompt_completed: z.boolean(), discord_joined: z.boolean(), scene_generator_completed: z.boolean(), widget_tutorial_completed: z.boolean(), prayer_reminder_completed: z.boolean(), share_creed_completed: z.boolean(), customize_completed: z.boolean(), sponsor_completed: z.boolean(), add_church_completed: z.boolean().optional(), start_friend_streak_completed: z.boolean().optional(), faith_lock_completed: z.boolean().optional(), artist_of_the_week_completed: z.boolean().optional(), commit_to_mass_completed: z.boolean().optional(), learning_plan_started_completed: z.boolean().optional(), learning_plan_completed: z.boolean().optional(), total_quests_completed: z.number().int().optional(), claimed: z.record(z.string(), z.boolean()).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 Daily Quests * * GET /api/daily-quests */ export const getApiApiDailyQuests = async (args: GetApiApiDailyQuestsArgs, options: GetApiApiDailyQuestsOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['x-user-id'] != null) headers.set("x-user-id", String(args['x-user-id'])) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/daily-quests${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(dailyQuestResponse, await res.json()) };