import {z} from 'zod' export type GetApiApiInternalDesktopRewardsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const getApiApiInternalDesktopRewardsResponse = z.object({viewer: z.object({cloudConnected: z.boolean(), activeModelId: z.string().nullable(), activeModelProviderId: z.string().nullable(), usingManagedModel: z.boolean()}), progress: z.object({claimedCount: z.number().int().gte(0), totalCount: z.number().int().gte(0), earnedCredits: z.number().gte(0), availableCredits: z.number().gte(0).optional()}), tasks: z.array(z.object({id: z.enum(["daily_checkin", "github_star", "x_share", "reddit", "mobile_share", "lingying", "facebook", "whatsapp"]), group: z.enum(["daily", "opensource", "social"]), icon: z.string(), reward: z.number().gt(0), shareMode: z.enum(["link", "tweet", "image"]), repeatMode: z.enum(["once", "daily", "weekly"]), requiresScreenshot: z.boolean(), actionUrl: z.string().nullable().optional(), isClaimed: z.boolean(), lastClaimedAt: z.string().nullable(), claimCount: z.number().int().gte(0)})), cloudBalance: z.object({totalBalance: z.number().int().gte(0), totalRecharged: z.number().int().gte(0), totalConsumed: z.number().int().gte(0), giftedBalance: z.number().int().gte(0).optional(), planBalance: z.number().int().gte(0).optional()}).nullable().optional(), autoFallbackTriggered: 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 /api/internal/desktop/rewards */ export const getApiApiInternalDesktopRewards = async (options: GetApiApiInternalDesktopRewardsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/internal/desktop/rewards${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(getApiApiInternalDesktopRewardsResponse, await res.json()) };