import {z} from 'zod' export type CreateApiV1BusinessesArgs = {'Idempotency-Key'?: string | undefined, body: {book_id: string, name: string, industry?: string | undefined, website_url?: string | undefined, tax_id?: string | undefined, additional_info?: string | undefined, custom_fields?: Record | undefined, email_addresses?: (Array<{email?: string | undefined, email_type?: ('personal' | 'business' | 'school' | 'other' | 'inactive') | undefined, is_primary?: boolean | undefined}>) | undefined, phone_numbers?: (Array<{phone_number?: string | undefined, phone_type?: ('mobile' | 'work' | 'home' | 'fax' | 'other') | undefined, is_primary?: boolean | undefined, extension?: string | undefined}>) | undefined, addresses?: (Array<{line1: string, line2?: string | undefined, city?: string | undefined, state?: string | undefined, zip?: string | undefined, country_code?: string | undefined, address_type?: ('home' | 'work' | 'mailing' | 'other') | undefined, is_primary?: boolean | undefined}>) | undefined, tag_ids?: Array | undefined}}; export type CreateApiV1BusinessesOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const email = z.object({email: z.string(), email_type: z.enum(["personal", "business", "school", "other", "inactive"]), is_primary: z.boolean(), id: z.string().optional()}); export const phoneNumber = z.object({phone_number: z.string(), extension: z.string().nullable().optional(), phone_type: z.enum(["mobile", "work", "home", "fax", "other"]), is_primary: z.boolean(), id: z.string().optional()}); export const address = z.object({line1: z.string().nullable().optional(), line2: z.string().nullable().optional(), city: z.string().nullable().optional(), state: z.string().nullable().optional(), zip: z.string().nullable().optional(), country_code: z.string().nullable().optional(), address_type: z.enum(["home", "work", "mailing", "other"]), is_primary: z.boolean(), id: z.string().optional()}); export const business = z.object({custom_fields: z.object({}).optional(), id: z.string(), book_id: z.string(), name: z.string(), industry: z.string().nullable().optional(), website_url: z.string().nullable().optional(), tax_id: z.string().nullable().optional(), additional_info: z.string().nullable().optional(), created_at: z.string(), updated_at: z.string(), email_addresses: z.array(email).optional(), phone_numbers: z.array(phoneNumber).optional(), addresses: z.array(address).optional(), tags: z.array(z.object({id: z.string().optional(), name: 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 }; /** * Create a business * * POST /v1/businesses */ export const createApiV1Businesses = async (args: CreateApiV1BusinessesArgs, options?: CreateApiV1BusinessesOptions) => { const {baseUrl = "https://api.slant.app", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/businesses${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(business, await res.json()) };