import {z} from 'zod' export type IntegrationSortField = 'created_at' | 'name' | 'type'; export type IntegrationSortOperator = 'asc' | 'desc'; export type IntegrationSortRequest = {field: IntegrationSortField, operator: IntegrationSortOperator}; export type ExternalIntegrationType = 'fireblocks' | 'fordefi' | 'slack' | 'telegram' | 'response_wallet'; export type IntegrationNameFilter = {contains?: (string | null) | undefined}; export type IntegrationFilterRequest = {type?: (Array | null) | undefined, name?: (IntegrationNameFilter | null) | undefined}; export type IntegrationSearchRequest = {pageSize?: number | undefined, page?: number | undefined, sort?: (Array | null) | undefined, filter?: (IntegrationFilterRequest | null) | undefined}; export type CreateApiV0PlatformIntegrationsSearchArgs = {body: IntegrationSearchRequest}; export type CreateApiV0PlatformIntegrationsSearchOptions = {baseUrl?: string | undefined, APIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const fireblocksIntegration = z.object({type: z.literal("fireblocks").optional(), integrationInstanceId: z.string(), name: z.string()}); export const slackWorkspace = z.object({id: z.string(), name: z.string()}); export const slackIntegration = z.object({type: z.literal("slack").optional(), integrationInstanceId: z.string(), workspace: slackWorkspace}); export const fordefiIntegration = z.object({type: z.literal("fordefi").optional(), integrationInstanceId: z.string(), name: z.string()}); export const telegramIntegration = z.object({type: z.literal("telegram").optional(), integrationInstanceId: z.string()}); export const responseWalletIntegration = z.object({type: z.literal("response_wallet").optional(), integrationInstanceId: z.string(), name: z.string(), address: z.string()}); export const pageAnnotatedUnionFireblocksIntegrationSlackIntegrationFordefiIntegrationTelegramIntegrationResponseWalletIntegrationFieldInfoAnnotationNoneTypeRequiredTrueDiscriminatorType = z.object({items: z.array(z.discriminatedUnion("type", [fireblocksIntegration, slackIntegration, fordefiIntegration, telegramIntegration, responseWalletIntegration])), total: z.number().int().gte(0).nullable(), page: z.number().int().gte(1).nullable(), size: z.number().int().gte(1).nullable(), pages: z.number().int().gte(0).nullable().optional()}); 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 Integrations * * POST /v0/platform/integrations/search * * Also accepts auth: JWT */ export const createApiV0PlatformIntegrationsSearch = async (args: CreateApiV0PlatformIntegrationsSearchArgs, options?: CreateApiV0PlatformIntegrationsSearchOptions) => { const {baseUrl = "https://api.blockaid.io", fetch: fetchFn = fetch, APIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKey !== undefined) headers.set("X-API-Key", APIKey) const url = `${baseUrl.replace(/\/+$/, '')}/v0/platform/integrations/search${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(pageAnnotatedUnionFireblocksIntegrationSlackIntegrationFordefiIntegrationTelegramIntegrationResponseWalletIntegrationFieldInfoAnnotationNoneTypeRequiredTrueDiscriminatorType, await res.json()) };