import {z} from 'zod' export type InstitutionFieldSelection = 'name' | 'state' | 'city' | 'county' | 'street' | 'population' | 'label' | 'website' | 'description' | 'latitude' | 'longitude'; export type CustomFieldValue = {crm_field_name: string, value: string}; export type PushInstitutionRequest = {fields?: (Array | null) | undefined, create_mapping?: boolean | undefined, owner_id?: (string | null) | undefined, custom_fields?: (Array | null) | undefined}; export type CreateApiApiV3CrmIntegrationIdInstitutionsInstitutionIdCompanyArgs = {integration_id: string, institution_id: string, body: PushInstitutionRequest}; export type CreateApiApiV3CrmIntegrationIdInstitutionsInstitutionIdCompanyOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const pushInstitutionResponse = z.object({company_id: z.string(), institution_id: z.string(), properties_pushed: z.record(z.string(), z.unknown()), mapping_created: z.boolean()}); 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 }; /** * Push Institution to CRM * * POST /api/v3/crm/{integration_id}/institutions/{institution_id}/company */ export const createApiApiV3CrmIntegrationIdInstitutionsInstitutionIdCompany = async (args: CreateApiApiV3CrmIntegrationIdInstitutionsInstitutionIdCompanyArgs, options: CreateApiApiV3CrmIntegrationIdInstitutionsInstitutionIdCompanyOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v3/crm/${encodeURIComponent(String(args.integration_id))}/institutions/${encodeURIComponent(String(args.institution_id))}/company${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(pushInstitutionResponse, await res.json()) };