import {z} from 'zod' export type GetApiApiExternalBridgeArgs = {sortField?: ('Name' | 'RowsCount' | 'Type' | 'CreatedBy' | 'CreditUsage' | 'SharedWith' | 'LastUpdatedByUser' | 'CreatedAt') | undefined, sortDirection?: ('ASC' | 'DESC') | undefined, query?: string | undefined, viewMode?: string | undefined, folderId?: string | undefined, pageNumber?: number | undefined, pageSize?: number | undefined, filterType?: (Array<'RFP' | 'Meeting' | 'Purchase' | 'Buyer' | 'TopBuyer' | 'Contact' | 'Signal' | 'Conference' | 'JobChange' | 'SequenceBuyer' | 'SequenceContact' | 'SequenceJobChange' | 'VendorPresence' | 'ProductCompetitorPresence' | 'CompanionPresence'>) | undefined, createdByUserId?: Array | undefined, userIdsHaveAccessTo?: Array | undefined, buyerListId?: Array | undefined}; export type GetApiApiExternalBridgeOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const bridgeResponse = z.object({bridgeId: z.string(), name: z.string(), userId: z.string(), filterType: z.enum(["RFP", "Meeting", "Purchase", "Buyer", "TopBuyer", "Contact", "Signal", "Conference", "JobChange", "SequenceBuyer", "SequenceContact", "SequenceJobChange", "VendorPresence", "ProductCompetitorPresence", "CompanionPresence"]), status: z.enum(["Initializing", "Matching", "Draft", "Open", "Failed", "Deleting"]), updatedAt: z.string(), rowCount: z.number().int().optional()}); export const getApiApiExternalBridgeResponse = z.object({pageNumber: z.number().int(), pageSize: z.number().int(), totalItems: z.number().int(), totalPages: z.number().int(), result: z.array(bridgeResponse)}); 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 }; /** * List Bridges * * GET /api/external/bridge */ export const getApiApiExternalBridge = async (args: GetApiApiExternalBridgeArgs, options?: GetApiApiExternalBridgeOptions) => { 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.sortField != null) query.append("sortField", String(args.sortField)) if (args.sortDirection != null) query.append("sortDirection", String(args.sortDirection)) if (args.query != null) query.append("query", String(args.query)) if (args.viewMode != null) query.append("viewMode", String(args.viewMode)) if (args.folderId != null) query.append("folderId", String(args.folderId)) if (args.pageNumber != null) query.append("pageNumber", String(args.pageNumber)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.filterType != null) query.append("filterType", args.filterType.join(',')) if (args.createdByUserId != null) query.append("createdByUserId", args.createdByUserId.join(',')) if (args.userIdsHaveAccessTo != null) query.append("userIdsHaveAccessTo", args.userIdsHaveAccessTo.join(',')) if (args.buyerListId != null) query.append("buyerListId", args.buyerListId.join(',')) if (apiKey !== undefined) headers.set('authorization', `Bearer ${apiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/external/bridge${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(getApiApiExternalBridgeResponse, await res.json()) };