import {z} from 'zod' export type CreateApiContactsOrganizationsArgs = {body: {name: string, email?: string | undefined, phones?: Array<{title: string, phone: string, notify: boolean, has_viber: boolean, has_whatsapp: boolean}> | undefined, notes?: string | undefined, address?: string | undefined, billing_address?: ({country: string, state?: string | undefined, city?: string | undefined, address_line_1: string, address_line_2?: string | undefined, postal_code?: string | undefined, comment?: string | undefined}) | undefined, supplier?: boolean | undefined, manager_id?: number | undefined, ad_campaign_id?: number | undefined, discount_code?: string | undefined, custom_fields?: unknown | undefined, tags?: Array | undefined, tax_identification_number?: string | undefined, business_registration_number?: string | undefined}}; export type CreateApiContactsOrganizationsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiContactsOrganizationsResponse = z.object({}); 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 }; /** * Create Organization * * POST /contacts/organizations */ export const createApiContactsOrganizations = async (args: CreateApiContactsOrganizationsArgs, options?: CreateApiContactsOrganizationsOptions) => { const {baseUrl = "https://api.roapp.io/v2", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) headers.set('authorization', `Bearer ${apiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/contacts/organizations${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(createApiContactsOrganizationsResponse, await res.json()) };