import {z} from 'zod' export type CreateApiDealsArgs = {body: {pipeline_id: number, stage_id: number, title: string, frozen_at?: string | undefined, reference?: string | undefined, temperature?: number | undefined, probability?: number | undefined, owner_id?: number | undefined, person_id?: number | undefined, company_id?: number | undefined, lost_reason_id?: number | undefined, origin_id?: number | undefined, started_in_stage_id?: number | undefined, created_at?: string | undefined, closed_at?: string | undefined, reason_close?: string | undefined, status?: number | undefined, deleted?: boolean | undefined, freezed?: boolean | undefined, value?: number | undefined, value_mrr?: number | undefined, order?: number | undefined, probably_closed_at?: string | undefined, last_contact_at?: string | undefined, custom_fields?: (Array<{id?: number | undefined, value?: string | undefined}>) | undefined, tags?: (Array<{id?: number | undefined}>) | undefined}}; export type CreateApiDealsOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiDealsResponse = z.object({success: z.boolean().optional(), message: z.string().optional(), data: z.object({id: z.number().int().optional(), account_id: z.number().int().optional(), hash: z.string().optional(), rdstation_reference: z.string().optional(), data_congelamento: z.string().optional(), type_reference: z.number().int().optional(), reference: z.string().optional(), temperature: z.number().int().optional(), probability: z.number().int().optional(), pipeline_id: z.number().int().optional(), owner_id: z.number().int().optional(), stage_id: z.number().int().optional(), person_id: z.number().int().optional(), company_id: z.number().int().optional(), lost_reason_id: z.number().int().optional(), origin_id: z.number().int().optional(), started_in_stage_id: z.number().int().optional(), created_at: z.string().optional(), title: z.string().optional(), description: z.string().optional(), observation: z.string().optional(), status: z.number().int().optional(), closed_at: z.string().optional(), reason_close: z.string().optional(), deleted: z.number().int().optional(), freezed: z.number().int().optional(), value: z.number().int().optional(), order: z.number().int().optional(), updated_at: z.string().optional(), last_stage_updated_at: z.string().optional(), value_mrr: z.number().int().optional(), probably_closed_at: z.string().optional(), last_contact_at: z.string().optional(), stage_changed_at: z.string().optional()}).optional()}); 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 }; /** * Adicionar oportunidade * * POST /deals */ export const createApiDeals = async (args: CreateApiDealsArgs, options?: CreateApiDealsOptions) => { const {baseUrl = "https://api.pipe.run/v1", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (sec0 !== undefined) headers.set("token", sec0) const url = `${baseUrl.replace(/\/+$/, '')}/deals${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(createApiDealsResponse, await res.json()) };