import {z} from 'zod' export type GetApiApiV2ContactsRecomsDataSourceIdArgs = {dataSourceId: number, contactId?: number | undefined, email?: string | undefined, phone?: string | undefined, externalCustomerId?: string | undefined, webId?: string | undefined, products?: string | undefined, category?: string | undefined}; export type GetApiApiV2ContactsRecomsDataSourceIdOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const contactRecom = z.object({id: z.string().optional(), name: z.string().optional(), url: z.string().optional(), price: z.number().optional(), descr: z.string().optional(), brand: z.string().optional(), category: z.array(z.string()).optional(), isInStock: z.number().int().optional()}); export const getApiApiV2ContactsRecomsDataSourceIdResponse = z.array(contactRecom); 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 }; /** * Contact recommendations based on web tracking * * GET /api/v2/contacts/recoms/{dataSourceId} */ export const getApiApiV2ContactsRecomsDataSourceId = async (args: GetApiApiV2ContactsRecomsDataSourceIdArgs, options?: GetApiApiV2ContactsRecomsDataSourceIdOptions) => { const {baseUrl = "https://api.reteno.com", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json; charset=UTF-8") if (args.contactId != null) query.append("contactId", String(args.contactId)) if (args.email != null) query.append("email", String(args.email)) if (args.phone != null) query.append("phone", String(args.phone)) if (args.externalCustomerId != null) query.append("externalCustomerId", String(args.externalCustomerId)) if (args.webId != null) query.append("webId", String(args.webId)) if (args.products != null) query.append("products", String(args.products)) if (args.category != null) query.append("category", String(args.category)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/contacts/recoms/${encodeURIComponent(String(args.dataSourceId))}${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(getApiApiV2ContactsRecomsDataSourceIdResponse, await res.json()) };