import {z} from 'zod' export type GetApiApiChartsTwitchUsersArgs = {date?: string | undefined, since?: string | undefined, limit?: number | undefined, duration: 'daily' | 'weekly' | 'monthly', type?: ('followers' | 'viewer_hours') | undefined, latest?: boolean | undefined}; export type GetApiApiChartsTwitchUsersOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiChartsTwitchUsersResponse = z.object({obj: z.object({length: z.number().int().nullable().optional(), data: z.array(z.object({rank: z.number().int().nullable().optional(), pre_rank: z.number().int().nullable().optional(), peak_rank: z.number().int().nullable().optional(), peak_date: z.union([z.string(), z.string()]).nullable().optional(), added_at: z.union([z.string(), z.string()]).nullable().optional(), velocity: z.number().nullable().optional(), time_on_chart: z.union([z.string(), z.number()]).nullable().optional(), twitch_user: z.union([z.number().int().gte(0).lte(2147483647), z.string()]).nullable().optional(), followers: z.union([z.number(), z.string()]).nullable().optional(), weekly_followers: z.union([z.number(), z.string()]).nullable().optional(), monthly_followers: z.union([z.number(), z.string()]).nullable().optional(), count: z.union([z.number(), z.string()]).nullable().optional(), weekly_count: z.union([z.number(), z.string()]).nullable().optional(), monthly_count: z.union([z.number(), z.string()]).nullable().optional(), name: z.string().nullable().optional(), image_url: z.string().nullable().optional(), link: z.string().nullable().optional(), cm_artist: z.number().int().gte(0).lte(2147483647).nullable().optional(), rankStats: z.array(z.object({rank: z.number().int().nullable().optional(), followers: z.union([z.number(), z.string()]).nullable().optional(), timestp: z.union([z.string(), z.string()]).nullable().optional()})).nullable().optional()})).nullable().optional()}).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 }; /** * Twitch User Charts * * GET /api/charts/twitch/users */ export const getApiApiChartsTwitchUsers = async (args: GetApiApiChartsTwitchUsersArgs, options?: GetApiApiChartsTwitchUsersOptions) => { const {baseUrl = "https://api.chartmetric.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.date != null) query.append("date", String(args.date)) if (args.since != null) query.append("since", String(args.since)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.duration != null) query.append("duration", String(args.duration)) if (args.type != null) query.append("type", String(args.type)) if (args.latest != null) query.append("latest", String(args.latest)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/charts/twitch/users${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(getApiApiChartsTwitchUsersResponse, await res.json()) };