import {z} from 'zod' export type DirectReferrer = {sourceType?: 'direct' | undefined}; export type TypedValue = {strValue?: (string | null) | undefined, numValue?: (number | string | null) | undefined, boolValue?: (boolean | null) | undefined, arrValue?: (Array | null) | undefined}; export type TVAParameter = {key: string, value: TypedValue}; export type LinkAnnotation = {title: string, summary?: (string | null) | undefined, imageUrl?: (string | null) | undefined}; export type TemplatedViewAddress = {viewName: string, uri: string, parameters?: Array | undefined, annotation?: (LinkAnnotation | null) | undefined}; export type HomepageReferrer = {sourceType?: 'homepage' | undefined, tva: TemplatedViewAddress}; export type PropertyHomepageReferrer = {sourceType?: 'property_homepage' | undefined}; export type PropertyDeepLinkReferrer = {sourceType?: 'property_deep_link' | undefined}; export type SearchFormReferrer = {sourceType?: 'search_form' | undefined, tva?: (TemplatedViewAddress | null) | undefined}; export type SearchResultsReferrer = {sourceType?: 'search_results' | undefined, tva: TemplatedViewAddress}; export type ListingSummary = {title?: (string | null) | undefined, priceDisplay?: (string | null) | undefined, address?: (string | null) | undefined, suburb?: (string | null) | undefined, propertyType?: (string | null) | undefined, bedrooms?: (number | null) | undefined, bathrooms?: (number | null) | undefined, parking?: (number | null) | undefined, floorArea?: (string | null) | undefined, landArea?: (string | null) | undefined, tva?: (TemplatedViewAddress | null) | undefined}; export type ListingReferrer = {sourceType?: 'listing' | undefined, listingId: string, listingSummary?: (ListingSummary | null) | undefined}; export type PromptStartersBody = {referrer?: (DirectReferrer | HomepageReferrer | PropertyHomepageReferrer | PropertyDeepLinkReferrer | SearchFormReferrer | SearchResultsReferrer | ListingReferrer | null) | undefined}; export type CreateApiApiV1beta1AssistantTrademePromptStartersArgs = {max_prompts?: number | undefined, authorization?: (string | null) | undefined, body: PromptStartersBody}; export type CreateApiApiV1beta1AssistantTrademePromptStartersOptions = {baseUrl: string, AnvilBearerToken?: string | undefined, fetch?: typeof fetch | undefined}; export const promptStarter = z.object({symbol: z.string(), text: z.string()}); export const promptStartersResponse = z.object({prompts: z.array(promptStarter)}); 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 prompt starters * * POST /api/v1beta1/assistant/trademe/prompt-starters */ export const createApiApiV1beta1AssistantTrademePromptStarters = async (args: CreateApiApiV1beta1AssistantTrademePromptStartersArgs, options: CreateApiApiV1beta1AssistantTrademePromptStartersOptions) => { const {baseUrl, fetch: fetchFn = fetch, AnvilBearerToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.max_prompts != null) query.append("max_prompts", String(args.max_prompts)) if (args.authorization != null) headers.set("authorization", String(args.authorization)) if (AnvilBearerToken !== undefined) headers.set('authorization', `Bearer ${AnvilBearerToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1beta1/assistant/trademe/prompt-starters${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(promptStartersResponse, await res.json()) };