import {z} from 'zod' export type CreateApiApiUtilConvertSegmentRuleArgs = {body: {name: string, description: string, parent_segment_id?: string | undefined, simplifiedIonRule: {title: string, summary: string, conditions: Array<{attribute_column?: string | undefined, operator?: ('=' | '!=' | '>' | '>=' | '<' | '<=' | 'IS NULL' | 'IS NOT NULL' | 'IN' | 'BETWEEN' | 'REGEX' | 'NOT REGEX' | null) | undefined, right_value?: (string | number | boolean | Array | {[key: string]: unknown}) | undefined, reason?: (string | null) | undefined, left_value?: (string | null) | undefined, logic?: ('ALL' | 'ANY' | null) | undefined, conditions?: (Array<{left_value: string, operator: '=' | '!=' | '>' | '>=' | '<' | '<=' | 'IS NULL' | 'IS NOT NULL' | 'IN' | 'BETWEEN' | 'REGEX' | 'NOT REGEX', right_value: number | string, reason: string}> | null) | undefined}>, logic?: string | undefined, baseSegmentIds?: ({logic: 'ALL' | 'ANY', segment_ids?: Array | undefined, segmentIds: Array}) | undefined} | {title: string, summary: string, conditions?: (Array<{attribute_column?: string | undefined, operator?: ('=' | '!=' | '>' | '>=' | '<' | '<=' | 'IS NULL' | 'IS NOT NULL' | 'IN' | 'BETWEEN' | 'REGEX' | 'NOT REGEX' | null) | undefined, right_value?: (string | number | boolean | Array | {[key: string]: unknown}) | undefined, reason?: (string | null) | undefined, left_value?: (string | null) | undefined, logic?: ('ALL' | 'ANY' | null) | undefined, conditions?: (Array<{left_value: string, operator: '=' | '!=' | '>' | '>=' | '<' | '<=' | 'IS NULL' | 'IS NOT NULL' | 'IN' | 'BETWEEN' | 'REGEX' | 'NOT REGEX', right_value: number | string, reason: string}> | null) | undefined}>) | undefined, logic?: string | undefined, baseSegmentIds: {logic: 'ALL' | 'ANY', segment_ids?: Array | undefined, segmentIds: Array}}}}; export type CreateApiApiUtilConvertSegmentRuleOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiUtilConvertSegmentRuleResponse = z.object({name: z.string(), description: z.string(), fullIonRule: z.object({conditions: z.array(z.object({conditions: z.array(z.object({arrayMatching: z.unknown(), exclude: z.boolean(), leftValue: z.object({name: z.string()}).nullable().optional(), operator: z.object({type: z.string(), not: z.boolean(), rightValue: z.union([z.number(), z.number().int()])}).nullable().optional(), type: z.string(), id: z.string().nullable().optional()})), type: z.string()})), type: z.string()})}); 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 }; /** * convert_segment_rule * * POST /api/util/convert_segment_rule */ export const createApiApiUtilConvertSegmentRule = async (args: CreateApiApiUtilConvertSegmentRuleArgs, options?: CreateApiApiUtilConvertSegmentRuleOptions) => { const {baseUrl = "https://llm-api.treasuredata.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set('authorization', `TD1 ${ApiKeyAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/util/convert_segment_rule${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(createApiApiUtilConvertSegmentRuleResponse, await res.json()) };