import {z} from 'zod' export type GetApiV2TemplatesArgs = {limit?: number | undefined, offset?: number | undefined, source?: (Array<'workspace' | 'synthesia'>) | undefined}; export type GetApiV2TemplatesOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const ctaSettings = z.object({label: z.string(), url: z.string()}); export const templateResponse = z.object({callbackId: z.string().optional(), createdAt: z.number().int().optional(), ctaSettings: ctaSettings.optional(), description: z.string().optional(), templateId: z.string(), templateData: z.object({}), test: z.boolean().optional(), title: z.string().optional(), visibility: z.enum(["private", "public"]).optional(), brandKitId: z.string().nullable().optional(), download: z.string().optional(), duration: z.string().optional(), id: z.string().optional(), lastUpdatedAt: z.number().int().optional(), variables: z.array(z.object({})).optional()}); export const templateListResponse = z.object({templates: z.array(templateResponse).optional(), nextOffset: z.number().int().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 templates * * GET /v2/templates */ export const getApiV2Templates = async (args: GetApiV2TemplatesArgs, options?: GetApiV2TemplatesOptions) => { const {baseUrl = "https://api.synthesia.io", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) for (const item of args.source ?? []) query.append("source", String(item)) if (apiKey !== undefined) headers.set("Authorization", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/templates${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(templateListResponse, await res.json()) };