import {z} from 'zod' export type GetApiApiV1ContentIdChannelsArgs = {Authorization: string, page?: number | undefined, per_page?: number | undefined}; export type GetApiApiV1ContentIdChannelsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const whitelistedChannel = z.object({id: z.string().optional(), type: z.string().optional(), attributes: z.object({channel_id: z.string().optional(), title: z.string().optional(), avatar_url: z.string().optional()}).optional()}); export const contentIdStatus = z.enum(["active", "inactive", "expired", "quota_reached"]); export const contentIdLicenseMeta = z.object({code: z.string().optional(), status: contentIdStatus.optional(), expiration_date: z.string().nullable().optional()}); export const getApiApiV1ContentIdChannelsResponse = z.object({data: z.array(whitelistedChannel).optional(), meta: z.object({license: contentIdLicenseMeta.nullable().optional(), obj_count: z.number().int().optional(), page_count: z.number().int().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 }; /** * List whitelisted YouTube channels * * GET /api/v1/content_id/channels */ export const getApiApiV1ContentIdChannels = async (args: GetApiApiV1ContentIdChannelsArgs, options?: GetApiApiV1ContentIdChannelsOptions) => { const {baseUrl = "https://api.melod.ie", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/content_id/channels${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(getApiApiV1ContentIdChannelsResponse, await res.json()) };