import {z} from 'zod' export type GetApiKeyLinkValidationKeysArgs = {pageCursor?: string | undefined, pageSize?: number | undefined, sortBy?: 'createdAt' | undefined, order?: ('ASC' | 'DESC') | undefined, includeRevoked?: boolean | undefined}; export type GetApiKeyLinkValidationKeysOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const validationKeyDto = z.object({keyId: z.string().min(36).max(36), publicKeyPem: z.string(), daysTillExpired: z.number().gte(1), enabled: z.boolean(), createdAt: z.number(), expirationTime: z.number(), revoked: z.boolean()}); export const getValidationKeyResponseDto = z.object({data: z.array(validationKeyDto), next: z.string().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 }; /** * Get list of registered validation keys * * GET /key_link/validation_keys */ export const getApiKeyLinkValidationKeys = async (args: GetApiKeyLinkValidationKeysArgs, options?: GetApiKeyLinkValidationKeysOptions) => { 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.sortBy != null) query.append("sortBy", String(args.sortBy)) if (args.order != null) query.append("order", String(args.order)) if (args.includeRevoked != null) query.append("includeRevoked", String(args.includeRevoked)) const url = `${baseUrl.replace(/\/+$/, '')}/key_link/validation_keys${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(getValidationKeyResponseDto, await res.json()) };