import {z} from 'zod' export type GetApiLivestreamsArgs = {exclude_meetings?: boolean | undefined, per_page?: number | undefined, page_no?: number | undefined, status?: ('LIVE' | 'IDLE' | 'ERRORED' | 'INVOKED') | undefined, start_time?: string | undefined, end_time?: string | undefined, sort_order?: ('ASC' | 'DSC') | undefined}; export type GetApiLivestreamsOptions = {baseUrl?: string | undefined, Authorization?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const getApiLivestreamsResponse = z.object({success: z.boolean().optional(), data: z.object({status: z.enum(["LIVE", "IDLE", "ERRORED", "INVOKED"]).optional(), id: z.string().optional(), name: z.string().optional(), ingest_server: z.string().optional(), stream_key: z.string().optional(), playback_url: z.string().optional(), meeting_id: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), disabled: z.string().optional(), paging: z.object({total_count: z.number().int().optional(), start_offset: z.number().int().optional(), end_offset: z.number().int().optional()}).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 }; /** * Fetch all livestreams * * GET /livestreams */ export const getApiLivestreams = async (args: GetApiLivestreamsArgs, options?: GetApiLivestreamsOptions) => { const {baseUrl = "https://api.dyte.io/v2", fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.exclude_meetings != null) query.append("exclude_meetings", String(args.exclude_meetings)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.page_no != null) query.append("page_no", String(args.page_no)) if (args.status != null) query.append("status", String(args.status)) if (args.start_time != null) query.append("start_time", String(args.start_time)) if (args.end_time != null) query.append("end_time", String(args.end_time)) if (args.sort_order != null) query.append("sort_order", String(args.sort_order)) if (Authorization !== undefined) headers.set('authorization', `Basic ${btoa(`${Authorization.username}:${Authorization.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/livestreams${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(getApiLivestreamsResponse, await res.json()) };