import {z} from 'zod' export type Rule = {field: string, op: string, value: unknown, unknown?: ('exclude' | 'review' | null) | undefined}; export type PreferredRule = {field: string, op: string, value: unknown, weight: number}; export type Cutoffs = {pursue: number, monitor: number}; export type DiscoveryDraft = {markets: Array, must?: Array | undefined, preferred?: Array | undefined, verdict_cutoffs: Cutoffs}; export type PromoteRequest = {parcel_key: string, buy_box: string, query_hash: string, draft?: (DiscoveryDraft | null) | undefined}; export type CreateApiV2PipelineArgs = {authorization?: (string | null) | undefined, 'X-Tenant'?: (string | null) | undefined, body: PromoteRequest}; export type CreateApiV2PipelineOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const promoteResponse = z.object({property_id: z.string(), pipeline_item_id: z.string(), buy_box_match_id: z.string(), diligence_job_id: z.string(), status: 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 }; /** * Promote To Pipeline * * POST /v2/pipeline */ export const createApiV2Pipeline = async (args: CreateApiV2PipelineArgs, options: CreateApiV2PipelineOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.authorization != null) headers.set("authorization", String(args.authorization)) if (args['X-Tenant'] != null) headers.set("X-Tenant", String(args['X-Tenant'])) const url = `${baseUrl.replace(/\/+$/, '')}/v2/pipeline${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(promoteResponse, await res.json()) };