import {z} from 'zod' export type CreateApiV2510BetaAgenticLogicSourcesValidateArgs = {body: {definition: {kind: 'bestSellers', args: {timeWindow?: ('24h' | '7d' | '30d' | '90d') | undefined, metric?: ('units' | 'revenue') | undefined, scope?: ({collectionIds?: Array | undefined, tags?: Array | undefined}) | undefined, limit: number, excludeOutOfStock?: boolean | undefined}} | {kind: 'newArrivals', args: {timeWindow?: ('24h' | '7d' | '30d' | '90d') | undefined, dateField?: ('created' | 'published') | undefined, scope?: ({collectionIds?: Array | undefined, tags?: Array | undefined}) | undefined, limit: number, excludeOutOfStock?: boolean | undefined}} | {kind: 'inStock', args: {minQuantity?: number | undefined, scope?: ({collectionIds?: Array | undefined, tags?: Array | undefined}) | undefined, orderBy?: ({field: 'bestSelling' | 'price' | 'createdAt' | 'title' | 'relevance' | 'inventoryTotal', direction?: ('asc' | 'desc') | undefined}) | undefined, limit: number}} | {kind: 'collectionProducts', args: {orderBy?: ({field: 'bestSelling' | 'price' | 'createdAt' | 'title' | 'relevance', direction?: ('asc' | 'desc') | undefined}) | undefined, limit: number, include?: ({collectionIds?: (boolean | {first: number}) | undefined, metafields?: Array<{namespace: string, key: string}> | undefined}) | undefined, collectionId: string}} | {kind: 'storefrontGraphql', args: {gql: string, variables: {[key: string]: unknown}}} | {kind: 'adminGraphql', args: {gql: string, variables: {[key: string]: unknown}}} | {kind: 'script', args: {workspace: {entry?: 'source.ts' | undefined, files: {[key: string]: string}}, outputSchema?: {[key: string]: unknown} | undefined}}}}; export type CreateApiV2510BetaAgenticLogicSourcesValidateOptions = {baseUrl?: string | undefined, intelligemsAccessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV2510BetaAgenticLogicSourcesValidateResponse = z.union([z.object({valid: z.literal(true), warnings: z.array(z.string())}), z.object({valid: z.literal(false), errors: z.array(z.string()), warnings: z.array(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 }; /** * Validate a Source definition * * POST /v25-10-beta/agentic-logic/sources/validate */ export const createApiV2510BetaAgenticLogicSourcesValidate = async (args: CreateApiV2510BetaAgenticLogicSourcesValidateArgs, options?: CreateApiV2510BetaAgenticLogicSourcesValidateOptions) => { const {baseUrl = "https://api.intelligems.io", fetch: fetchFn = fetch, intelligemsAccessToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (intelligemsAccessToken !== undefined) headers.set("intelligems-access-token", intelligemsAccessToken) const url = `${baseUrl.replace(/\/+$/, '')}/v25-10-beta/agentic-logic/sources/validate${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(createApiV2510BetaAgenticLogicSourcesValidateResponse, await res.json()) };