import {z} from 'zod' export type GetApiPosfinancingRestV2MortgageOfficersArgs = {state: string, categoryCode?: ('Solar_TPO' | 'HVAC_LEASE' | 'BATTERY_LEASE' | 'HELOC') | undefined}; export type GetApiPosfinancingRestV2MortgageOfficersOptions = {baseUrl?: string | undefined, jwt?: string | undefined, fetch?: typeof fetch | undefined}; export const sUBMISSIONAddress = z.object({line1: z.string(), line2: z.string().optional(), city: z.string(), state: z.string(), zip: z.string(), country: z.string().optional()}); export const sUBMISSIONMortgageOfficerDto = z.object({name: z.string(), licenseNumber: z.string()}); export const schemaObject3 = z.object({payload: z.object({state: z.string(), legalEntityName: z.string(), legalEntityLocation: sUBMISSIONAddress, officer: sUBMISSIONMortgageOfficerDto})}); 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 }; /** * Fetches the mortgage officer configuration for a state from application submission configuration. When categoryCode is omitted, the officer assigned to the state is returned. * * GET /posfinancing/rest/v2/mortgage-officers */ export const getApiPosfinancingRestV2MortgageOfficers = async (args: GetApiPosfinancingRestV2MortgageOfficersArgs, options?: GetApiPosfinancingRestV2MortgageOfficersOptions) => { const {baseUrl = "https://devint01-api.goodleap.com", fetch: fetchFn = fetch, jwt} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.state != null) query.append("state", String(args.state)) if (args.categoryCode != null) query.append("categoryCode", String(args.categoryCode)) if (jwt !== undefined) headers.set('authorization', `Bearer ${jwt}`) const url = `${baseUrl.replace(/\/+$/, '')}/posfinancing/rest/v2/mortgage-officers${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(schemaObject3, await res.json()) };