import {z} from 'zod' export type GetApiV1AccountClipsArgs = {page?: number | undefined, page_size?: number | undefined, language?: (string | null) | undefined, sort_by?: ('timestamp' | 'text_length' | 'voice') | undefined, sort_order?: ('asc' | 'desc') | undefined, debug?: boolean | undefined, 'x-api-key'?: string | undefined, 'lmnt-auth0'?: (string | null) | undefined}; export type GetApiV1AccountClipsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const clip = z.object({url: z.string().min(1).max(2083), voice: z.string(), timestamp: z.number().int(), text: z.string().nullable().optional(), speed: z.number().gte(0.25).lte(2).nullable().optional(), language: z.string().nullable().optional(), debug: z.boolean().optional()}); export const clipsResponse = z.object({clips: z.array(clip), page: z.number().int().gt(0), page_size: z.number().int().gt(0), total_clips: z.number().int().gte(0), total_pages: z.number().int().gte(0)}); 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 Clips Handler * * GET /v1/account/clips */ export const getApiV1AccountClips = async (args: GetApiV1AccountClipsArgs, options: GetApiV1AccountClipsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() const cookies: string[] = [] headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.language != null) query.append("language", String(args.language)) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (args.sort_order != null) query.append("sort_order", String(args.sort_order)) if (args.debug != null) query.append("debug", String(args.debug)) if (args['x-api-key'] != null) headers.set("x-api-key", String(args['x-api-key'])) if (args['lmnt-auth0'] != null) cookies.push("lmnt-auth0=" + encodeURIComponent(String(args['lmnt-auth0']))) if (cookies.length > 0) headers.set('cookie', cookies.join('; ')) const url = `${baseUrl.replace(/\/+$/, '')}/v1/account/clips${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(clipsResponse, await res.json()) };