import {z} from 'zod' export type GetApiV1CompanyMappingsSearchArgs = {query: string, product_type?: ('income' | 'employment' | 'deposit_switch' | 'pll') | undefined}; export type GetApiV1CompanyMappingsSearchOptions = {baseUrl?: string | undefined, ClientID?: string | undefined, AccessKey?: string | undefined, fetch?: typeof fetch | undefined}; export const companyMappingSearchResponse = z.object({company_mapping_id: z.string().max(64), name: z.string().max(128), domain: z.string().max(128).nullable(), logo_url: z.string().nullable().optional(), success_rate: z.enum(["low", "high", "unsupported", "null"]).nullable(), confidence_level: z.string(), sso: z.object({name: z.string().max(128)}).nullable().optional()}); export const getApiV1CompanyMappingsSearchResponse = z.array(companyMappingSearchResponse); 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 }; /** * Search companies with autocomplete * * GET /v1/company-mappings-search/ */ export const getApiV1CompanyMappingsSearch = async (args: GetApiV1CompanyMappingsSearchArgs, options?: GetApiV1CompanyMappingsSearchOptions) => { const {baseUrl = "https://prod.truv.com", fetch: fetchFn = fetch, ClientID, AccessKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.query != null) query.append("query", String(args.query)) if (args.product_type != null) query.append("product_type", String(args.product_type)) if (ClientID !== undefined) headers.set("X-Access-Client-Id", ClientID) if (AccessKey !== undefined) headers.set("X-Access-Secret", AccessKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/company-mappings-search/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiV1CompanyMappingsSearchResponse, await res.json()) };