import {z} from 'zod' export type IdentitiesBusinessIdentityRequestParamEntityType = 'LLC' | 'CORPORATION' | 'NON_PROFIT' | 'PARTNERSHIP' | 'TRUST'; export type IdentitiesBusinessIdentityRequestParamAddressesItem = {address_line1?: string | undefined, city?: string | undefined, state?: string | undefined, postal_code?: string | undefined}; export type CreateApiV1IdentitiesAutocompleteArgs = {body: {name: string, entity_type?: IdentitiesBusinessIdentityRequestParamEntityType | undefined, addresses?: Array | undefined}}; export type CreateApiV1IdentitiesAutocompleteOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const businessIdentityNamesItemType = z.enum(["legal", "dba"]); export const businessIdentityNamesItem = z.object({name: z.string().optional(), type: businessIdentityNamesItemType.optional()}); export const businessIdentityAddressesItem = 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 businessIdentityPeopleItem = z.object({name: z.string().optional(), titles: z.array(z.string()).optional()}); export const businessIdentityEntityType = z.enum(["LLC", "CORPORATION", "NON_PROFIT", "PARTNERSHIP", "TRUST"]); export const businessIdentity = z.object({id: z.string().optional(), names: z.array(businessIdentityNamesItem).optional(), addresses: z.array(businessIdentityAddressesItem).optional(), people: z.array(businessIdentityPeopleItem).optional(), entity_type: businessIdentityEntityType.nullable().optional()}); export const businessIdentityList = z.object({data: z.array(businessIdentity).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 }; /** * Autocomplete business identities * * POST /v1/identities/autocomplete */ export const createApiV1IdentitiesAutocomplete = async (args: CreateApiV1IdentitiesAutocompleteArgs, options?: CreateApiV1IdentitiesAutocompleteOptions) => { 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/identities/autocomplete${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(businessIdentityList, await res.json()) };