import {z} from 'zod' export type CreateApiV1PrefillBusinessesArgs = {body: {names?: Array | undefined, addresses?: Array | undefined, tin?: string | undefined, website_url?: string | undefined, autocomplete_result_id?: string | undefined}}; export type CreateApiV1PrefillBusinessesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const prefillBusinessNamesItemType = z.enum(["legal", "dba"]); export const prefillBusinessNamesItem = z.object({name: z.string().optional(), type: prefillBusinessNamesItemType.nullable().optional()}); export const prefillBusinessAddressesItem = z.object({full_address: z.string().optional(), address_line1: z.string().optional(), address_line2: z.string().nullable().optional(), city: z.string().optional(), state: z.string().optional(), postal_code: z.string().optional(), labels: z.array(z.string()).optional()}); export const prefillBusinessTin = z.object({last_four: z.string().optional(), tin_type: z.string().optional()}); export const prefillBusinessFormation = z.object({entity_type: z.string().nullable().optional()}); export const prefillBusinessIndustryClassificationsItem = z.object({name: z.string().optional(), score: z.number().optional(), sector: z.string().nullable().optional(), category: z.string().nullable().optional(), high_risk: z.boolean().optional(), mcc_codes: z.array(z.string()).nullable().optional(), sic_codes: z.array(z.string()).nullable().optional(), naics_codes: z.array(z.string()).nullable().optional(), prohibited_labels: z.array(z.string()).optional(), classification_system: z.string().optional()}); export const prefillBusinessPeopleItem = z.object({name: z.string().optional(), titles: z.array(z.string()).optional()}); export const prefillBusinessWebsite = z.object({url: z.string().optional()}); export const prefillBusiness = z.object({object: z.string().optional(), names: z.array(prefillBusinessNamesItem).optional(), addresses: z.array(prefillBusinessAddressesItem).optional(), tin: prefillBusinessTin.nullable().optional(), formation: prefillBusinessFormation.optional(), industry_classifications: z.array(prefillBusinessIndustryClassificationsItem).nullable().optional(), people: z.array(prefillBusinessPeopleItem).optional(), website: prefillBusinessWebsite.nullable().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 }; /** * Prefill business information * * POST /v1/prefill/businesses */ export const createApiV1PrefillBusinesses = async (args: CreateApiV1PrefillBusinessesArgs, options?: CreateApiV1PrefillBusinessesOptions) => { const {baseUrl = "https://api.middesk.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/prefill/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(prefillBusiness, await res.json()) };