import {z} from 'zod' export type UpdateApiSessionsChannelResetArgs = {channel: string}; export type UpdateApiSessionsChannelResetOptions = {baseUrl?: string | undefined, JWTBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const followerLatest = z.object({name: z.string()}); export const count = z.object({count: z.number().int()}); export const amount = z.object({amount: z.number().int()}); export const subscriberLatest = z.object({name: z.string(), amount: z.number().int(), tier: z.string(), message: z.string(), sender: z.string().nullable(), gifted: z.string().nullable()}); export const subscriberNewLatest = z.object({name: z.string(), amount: z.number().int(), message: z.string()}); export const subscriberResubLatest = z.object({name: z.string(), amount: z.number().int(), message: z.string()}); export const subscriberGiftedLatest = z.object({name: z.string(), amount: z.number().int(), message: z.string()}); export const topUserAmount = z.object({name: z.string(), amount: z.number().int(), message: z.string().optional()}); export const amountRecent = z.object({name: z.string(), amount: z.number().int(), createdAt: z.string().optional()}); export const cheerLatest = z.object({name: z.string(), amount: z.number().int(), message: z.string()}); export const tipLatest = z.object({name: z.string(), amount: z.number().int(), message: z.string()}); export const followerRecent = z.object({name: z.string(), createdAt: z.string()}); export const subscriberRecent = z.object({name: z.string(), tier: z.string(), amount: z.number().int(), createdAt: z.string()}); export const sessionData = z.object({"follower-latest": followerLatest, "follower-session": count, "follower-week": count, "follower-month": count, "follower-goal": amount, "follower-total": count, "subscriber-latest": subscriberLatest, "subscriber-new-latest": subscriberNewLatest, "subscriber-resub-latest": subscriberResubLatest, "subscriber-gifted-latest": subscriberGiftedLatest, "subscriber-session": count, "subscriber-new-session": count, "subscriber-resub-session": count, "subscriber-gifted-session": count, "subscriber-week": count, "subscriber-month": count, "subscriber-goal": amount, "subscriber-total": count, "subscriber-points": amount, "subscriber-alltime-gifter": topUserAmount, "host-latest": amountRecent, "raid-latest": amountRecent, "cheer-session": amount, "cheer-week": amount, "cheer-month": amount, "cheer-total": amount, "cheer-count": count, "cheer-goal": amount, "cheer-latest": cheerLatest, "cheer-session-top-donation": topUserAmount, "cheer-weekly-top-donation": topUserAmount, "cheer-monthly-top-donation": topUserAmount, "cheer-alltime-top-donation": topUserAmount, "cheer-session-top-donator": topUserAmount, "cheer-weekly-top-donator": topUserAmount, "cheer-monthly-top-donator": topUserAmount, "cheer-alltime-top-donator": topUserAmount, "tip-latest": tipLatest, "tip-session-top-donation": topUserAmount, "tip-weekly-top-donation": topUserAmount, "tip-monthly-top-donation": topUserAmount, "tip-alltime-top-donation": topUserAmount, "tip-session-top-donator": topUserAmount, "tip-weekly-top-donator": topUserAmount, "tip-monthly-top-donator": topUserAmount, "tip-alltime-top-donator": topUserAmount, "tip-session": amount, "tip-week": amount, "tip-month": amount, "tip-total": amount, "tip-count": count, "tip-goal": amount, "merch-goal-orders": amount, "merch-goal-items": amount, "merch-goal-total": amount, "follower-recent": z.array(followerRecent), "subscriber-recent": z.array(subscriberRecent), "host-recent": z.array(amountRecent), "raid-recent": z.array(amountRecent), "cheer-recent": z.array(amountRecent), "tip-recent": z.array(amountRecent), "merch-recent": z.array(amountRecent)}); export const sessionSettings = z.object({autoReset: z.boolean(), calendar: z.boolean()}); export const channelReset = z.object({data: sessionData, settings: sessionSettings, _id: z.string(), provider: z.string(), lastReset: z.string(), channel: z.string(), createdAt: z.string(), updatedAt: z.string(), __v: z.number().int()}); 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 }; /** * /:channel/reset * * PUT /sessions/{channel}/reset * * Also accepts auth: OAuth2, ApiKeyBearer */ export const updateApiSessionsChannelReset = async (args: UpdateApiSessionsChannelResetArgs, options?: UpdateApiSessionsChannelResetOptions) => { const {baseUrl = "https://api.streamelements.com/kappa/v2", fetch: fetchFn = fetch, JWTBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json; charset=utf-8") if (JWTBearer !== undefined) headers.set('authorization', `Bearer ${JWTBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/sessions/${encodeURIComponent(String(args.channel))}/reset${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(channelReset, await res.json()) };