import {z} from 'zod' export type GetApiContactsArgs = {pageCursor?: string | undefined, pageSize?: number | undefined, includeTotal?: boolean | undefined, name?: string | undefined, types?: (Array<'COUNTERPARTY' | 'OWN_ACCOUNT'>) | undefined, containerId?: string | undefined, archived?: boolean | undefined, accessControl?: ('WHITELIST' | 'BLACKLIST') | undefined, includeTagIds?: Array | undefined, excludeTagIds?: Array | undefined, sortBy?: ('name' | 'updatedAt') | undefined, order?: ('ASC' | 'DESC') | undefined}; export type GetApiContactsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const contactApprovalRequest = z.object({id: z.string(), type: z.string()}).nullable(); export const contactTagAttachmentPending = z.object({action: z.enum(["ATTACH", "DETACH"]), approvalRequestId: z.string().optional()}).nullable(); export const contactTag = z.object({id: z.string(), label: z.string(), color: z.string().optional(), description: z.string().optional(), isProtected: z.boolean(), pendingApprovalRequest: contactApprovalRequest, pendingAttachment: contactTagAttachmentPending}); export const contact = z.object({id: z.string(), name: z.string(), type: z.enum(["COUNTERPARTY", "OWN_ACCOUNT"]), accessControl: z.enum(["WHITELIST", "BLACKLIST"]).optional(), notes: z.string().optional(), externalRefId: z.string().optional(), containerId: z.string().optional(), updatedAt: z.string(), archivedAt: z.string().optional(), tags: z.array(contactTag), pendingApprovalRequest: contactApprovalRequest}); export const contactsPagedResponse = z.object({data: z.array(contact), next: z.string().optional(), total: z.number().int().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 }; /** * List contacts * * GET /contacts */ export const getApiContacts = async (args: GetApiContactsArgs, options?: GetApiContactsOptions) => { const {baseUrl = "https://api.fireblocks.io/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.pageCursor != null) query.append("pageCursor", String(args.pageCursor)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.includeTotal != null) query.append("includeTotal", String(args.includeTotal)) if (args.name != null) query.append("name", String(args.name)) for (const item of args.types ?? []) query.append("types", String(item)) if (args.containerId != null) query.append("containerId", String(args.containerId)) if (args.archived != null) query.append("archived", String(args.archived)) if (args.accessControl != null) query.append("accessControl", String(args.accessControl)) for (const item of args.includeTagIds ?? []) query.append("includeTagIds", String(item)) for (const item of args.excludeTagIds ?? []) query.append("excludeTagIds", String(item)) if (args.sortBy != null) query.append("sortBy", String(args.sortBy)) if (args.order != null) query.append("order", String(args.order)) const url = `${baseUrl.replace(/\/+$/, '')}/contacts${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(contactsPagedResponse, await res.json()) };