import {z} from 'zod' export type GetApiApiExternalBuyerQuickSearchArgs = {buyerName: string, buyerStateCode?: string | undefined, limit?: string | undefined}; export type GetApiApiExternalBuyerQuickSearchOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const buyerBrief = z.object({id: z.string(), name: z.string(), type: z.enum(["AdministrativeAgency", "City", "CityAttorney", "CityClerk", "CityEngineer", "CityManager", "CitySecretary", "County", "CountyAttorney", "CountyClerk", "CountyEngineer", "CountyManager", "CulturalFacility", "Department", "DiplomaticEntity", "District", "FinancialAgency", "HealthcareFacility", "HigherEducation", "LawEnforcementAgency", "LegislativeBody", "MilitaryOrganization", "NonProfitOrganization", "PrivateCompany", "PublicWorksAgency", "RegulatoryAgency", "School", "SchoolDistrict", "SpecialDistrict", "State", "StateAgency", "StateAttorney", "StateClerk", "StateEngineer", "TransportationAgency", "UncategorizedAgency", "UncategorizedClerk", "UncategorizedDistrict", "PoliceDepartment", "FireDepartment", "Library", "IndigenousGovernment", "Federal", "Other"]).nullable().optional(), countryCode: z.string(), stateCode: z.string().nullable().optional(), url: z.string().nullable().optional(), fromSourceFile: z.boolean(), tags: z.array(z.string()).optional(), linkedinSlug: z.string().nullable().optional(), logoPath: z.string().nullable().optional()}); export const buyerSearchResponse = z.object({buyers: z.array(buyerBrief)}); 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 Buyers * * GET /api/external/buyer/quick/search */ export const getApiApiExternalBuyerQuickSearch = async (args: GetApiApiExternalBuyerQuickSearchArgs, options?: GetApiApiExternalBuyerQuickSearchOptions) => { const {baseUrl = "https://dashboard.starbridge.ai", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.buyerName != null) query.append("buyerName", String(args.buyerName)) if (args.buyerStateCode != null) query.append("buyerStateCode", String(args.buyerStateCode)) if (args.limit != null) query.append("limit", String(args.limit)) if (apiKey !== undefined) headers.set('authorization', `Bearer ${apiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/external/buyer/quick/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(buyerSearchResponse, await res.json()) };