import {z} from 'zod' export type CreateApiCompaniesArgs = {body: {name: string, city_id?: number | undefined, manager_id?: number | undefined, segment_id?: number | undefined, cnae_id?: number | undefined, cnaes?: (Array<{id?: string | undefined}>) | undefined, cnpj?: string | undefined, website?: string | undefined, email_nf?: string | undefined, logo?: string | undefined, observation?: string | undefined, address_postal_code?: string | undefined, address?: string | undefined, address_number?: string | undefined, address_complement?: string | undefined, company_name?: string | undefined, ie?: string | undefined, district?: string | undefined, country?: string | undefined, facebook?: string | undefined, linkedin?: string | undefined, customer_at?: string | undefined, foundation_at?: string | undefined, company_type?: number | undefined, company_situation?: number | undefined, status_touch?: string | undefined, nps_score?: number | undefined, legal_nature?: string | undefined, social_capital?: number | undefined, is_brand?: boolean | undefined, is_supplier?: boolean | undefined, is_client?: boolean | undefined, is_carrier?: boolean | undefined, is_franchise?: boolean | undefined, is_channel?: boolean | undefined, is_distributor?: boolean | undefined, is_manufacturer?: boolean | undefined, is_partner?: boolean | undefined, hash?: string | undefined, city?: Record | undefined, external_code?: string | undefined, status?: boolean | undefined, company_status?: boolean | undefined, foreign_contact?: boolean | undefined, size?: string | undefined, contactEmails?: Array | undefined, contactPhones?: Array | undefined, customFields?: (Array<{id?: number | undefined, value?: string | undefined}>) | undefined, tags?: (Array<{id?: number | undefined}>) | undefined, personsRelated?: (Array<{id?: number | undefined, job_title?: string | undefined, is_main_company?: boolean | undefined, started_working_at?: string | undefined}>) | undefined}}; export type CreateApiCompaniesOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiCompaniesResponse = z.object({success: z.boolean().optional(), message: z.string().optional(), data: z.array(z.object({id: z.number().int().optional(), account_id: z.number().int().optional(), cnae_id: z.number().int().optional(), city_id: z.number().int().optional(), owner_id: z.number().int().optional(), manager_id: z.number().int().optional(), segment_id: z.number().int().optional(), name: z.string().optional(), cnpj: z.string().optional(), website: z.string().optional(), email_nf: z.string().optional(), logo: z.string().optional(), hash: z.string().optional(), observation: z.string().optional(), address_postal_code: z.string().optional(), address: z.string().optional(), address_number: z.string().optional(), address_complement: z.string().optional(), company_name: z.string().optional(), ie: z.string().optional(), district: z.string().optional(), country: z.string().optional(), facebook: z.string().optional(), linkedin: z.string().optional(), city: z.object({id: z.number().int().optional(), uf: z.string().optional(), name: z.string().optional()}).optional(), cep: z.string().optional(), customer_at: z.string().optional(), foundation_at: z.string().optional(), created_at: z.string().optional(), company_type: z.string().optional(), company_status: z.number().int().optional(), company_situation: z.number().int().optional(), status_touch: z.number().int().optional(), nps_score: z.number().int().optional(), social_capital: z.number().int().optional(), is_brand: z.boolean().optional(), is_supplier: z.boolean().optional(), is_client: z.boolean().optional(), is_carrier: z.boolean().optional(), is_franchise: z.boolean().optional(), lat: z.number().int().optional(), lng: z.number().int().optional(), status: z.number().int().optional(), external_code: z.number().int().optional(), foreign_contact: z.number().int().optional(), size: z.number().int().optional(), contact_emails: z.object({id: z.number().int().optional(), account_id: z.number().int().optional(), company_id: z.number().int().optional(), person_id: z.number().int().optional(), is_main: z.number().int().optional(), type: z.string().optional(), email: z.string().optional()}).optional(), contact_phones: z.array(z.object({})).optional(), custom_fields: z.array(z.object({})).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 empresa * * POST /companies */ export const createApiCompanies = async (args: CreateApiCompaniesArgs, options?: CreateApiCompaniesOptions) => { 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(/\/+$/, '')}/companies${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(createApiCompaniesResponse, await res.json()) };