import {z} from 'zod' export type IIntentCreate = {name?: (string | null) | undefined, description?: (string | null) | undefined, attributes?: {[key: string]: unknown} | undefined, is_active?: (boolean | null) | undefined, intent_type?: (string | null) | undefined}; export type CreateApiApiV1IntentArgs = {body: IIntentCreate}; export type CreateApiApiV1IntentOptions = {baseUrl: string, YourAPIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const iIntentRead = z.object({name: z.string().nullable().optional(), description: z.string().nullable().optional(), attributes: z.record(z.string(), z.unknown()).optional(), is_active: z.boolean().nullable().optional(), id: z.string(), intent_type: z.string()}); export const iPostResponseBaseIIntentRead = z.object({message: z.string().nullable().optional(), meta: z.union([z.record(z.string(), z.unknown()), z.unknown()]).nullable().optional(), data: iIntentRead.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 Intent * * POST /api/v1/intent * * Also accepts auth: HTTPBearer */ export const createApiApiV1Intent = async (args: CreateApiApiV1IntentArgs, options: CreateApiApiV1IntentOptions) => { 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/intent${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(iPostResponseBaseIIntentRead, await res.json()) };