import {z} from 'zod' export type IProviderEnum = 'openai' | 'elevenlabs' | 'azure_speech' | 'cartesia' | 'playht'; export type IProviderCreate = {org_id?: (string | null) | undefined, name: string | null, provider_key: string | null, provider_name?: (IProviderEnum | null) | undefined, attributes?: {[key: string]: unknown} | undefined, status?: (boolean | null) | undefined}; export type CreateApiApiV1ProviderArgs = {body: IProviderCreate}; export type CreateApiApiV1ProviderOptions = {baseUrl: string, YourAPIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const iProviderEnum = z.enum(["openai", "elevenlabs", "azure_speech", "cartesia", "playht"]); export const iProviderCreate = z.object({org_id: z.string().nullable().optional(), name: z.string().nullable(), provider_key: z.string().nullable(), provider_name: iProviderEnum.nullable().optional(), attributes: z.record(z.string(), z.unknown()).optional(), status: z.boolean().nullable().optional()}); export const iPostResponseBaseIProviderCreate = z.object({message: z.string().nullable().optional(), meta: z.union([z.record(z.string(), z.unknown()), z.unknown()]).nullable().optional(), data: iProviderCreate.nullable().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 }; /** * Create Provider * * POST /api/v1/provider * * Also accepts auth: HTTPBearer */ export const createApiApiV1Provider = async (args: CreateApiApiV1ProviderArgs, options: CreateApiApiV1ProviderOptions) => { const {baseUrl, fetch: fetchFn = fetch, YourAPIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (YourAPIKey !== undefined) query.set("api_key", YourAPIKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/provider${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(iPostResponseBaseIProviderCreate, await res.json()) };