import {z} from 'zod' export type GetApiApiConfigOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const featureFlags = z.object({cpp_paywall: z.boolean()}); export const configResponse = z.object({feature_flags: featureFlags, onboard_hard_paywall_rate: z.number(), onboarding_treatment_allocation: z.number(), minimum_app_version: z.string(), returning_user_absent_paywall_allocation: z.number(), trial_end_reminder_enabled: z.boolean(), usage_limit_warning_treatment_allocation: z.number(), log_meta_sdk_events: z.boolean(), log_tiktok_sdk_events: z.boolean(), newcomer_banner_treatment_allocation: z.number(), tts_treatment_allocation: z.number(), tts_exp_legacy_cutoff_timestamp: z.number().int(), low_usage_country_codes: z.array(z.string()), low_usage_currency_codes: z.array(z.string()), low_usage_language_codes: z.array(z.string()), inapp_ads_admob_allocation: z.number(), inapp_ads_house_allocation: z.number(), paid_token_exp_legacy_cutoff_timestamp: z.number().int(), paid_token_exp_treatment_allocation: z.number(), tiered_sub_exp_cutoff_timestamp: z.number().int(), tiered_sub_exp_treatment_allocation: z.number(), quiz_exp_legacy_cutoff_timestamp: z.number().int(), quiz_exp_treatment_allocation: z.number(), learning_plans_nav_exp_legacy_cutoff_timestamp: z.number().int(), learning_plans_nav_exp_treatment_allocation: z.number(), cold_boot_route_exp_legacy_cutoff_timestamp: z.number().int(), cold_boot_route_exp_treatment_allocation: z.number(), learning_plans_card_exp_legacy_cutoff_timestamp: z.number().int(), learning_plans_card_exp_treatment_allocation: z.number(), quiz_card_exp_legacy_cutoff_timestamp: z.number().int(), quiz_card_exp_treatment_allocation: z.number(), commit_to_church_exp_legacy_cutoff_timestamp: z.number().int(), commit_to_church_exp_treatment_allocation: z.number(), session_end_card_exp_legacy_cutoff_timestamp: z.number().int(), session_end_card_exp_treatment_allocation: z.number(), name_moderation_exp_legacy_cutoff_timestamp: z.number().int(), name_moderation_exp_treatment_allocation: z.number(), guided_tour_exp_treatment_1_allocation: z.number(), guided_tour_exp_treatment_2_allocation: z.number(), compaction_interval: z.number().int(), chat_streaming_allocation: z.number(), web_apple_pay_readiness: z.boolean()}); 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 Config * * GET /api/config */ export const getApiApiConfig = async (options: GetApiApiConfigOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/config${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(configResponse, await res.json()) };