import {z} from 'zod' export type GetApiTrustedDestinationsArgs = {afterId?: string | undefined, limit?: number | undefined, ownerType?: 'CUSTOMER' | undefined, ownerId?: string | undefined, assetTypeId?: string | undefined}; export type GetApiTrustedDestinationsOptions = {baseUrl?: string | undefined, ApiAccessKey?: string | undefined, fetch?: typeof fetch | undefined}; export const trustedDestinationCrypto = z.object({address: z.string(), assetType: z.string(), isNetworkLevel: z.boolean(), memo: z.string().optional(), networkId: z.string()}); export const trustedDestinationFiatBeneficiary = z.object({account: z.string(), address: z.string(), city: z.string(), country: z.string(), name: z.string(), state: z.string(), type: z.enum(["BUSINESS", "INDIVIDUAL"]), zipCode: z.string()}); export const trustedDestinationFiatBank = z.object({address: z.string(), city: z.string(), code: z.string(), country: z.string(), id: z.string(), name: z.string(), state: z.string(), zipCode: z.string()}); export const trustedDestinationFiat = z.object({beneficiary: trustedDestinationFiatBeneficiary, beneficiaryBank: trustedDestinationFiatBank, intermediaryBank: trustedDestinationFiatBank.optional(), memo: z.string().optional(), usage: z.enum(["MY_ORGANIZATION", "THIRD_PARTY"])}); export const trustedDestinationOwner = z.object({id: z.string(), type: z.literal("CUSTOMER")}); export const trustedDestination = z.object({crypto: trustedDestinationCrypto.optional(), description: z.string().optional(), fiat: trustedDestinationFiat.optional(), id: z.string(), name: z.string().optional(), owner: trustedDestinationOwner.optional(), type: z.string()}); export const page = z.object({next: z.string().nullable().optional()}); export const trustedDestinationsResponse = z.object({data: z.array(trustedDestination), page: page}); 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 trusted destinations * * GET /trusted_destinations */ export const getApiTrustedDestinations = async (args: GetApiTrustedDestinationsArgs, options?: GetApiTrustedDestinationsOptions) => { const {baseUrl = "https://api.anchorage-staging.com/v2", fetch: fetchFn = fetch, ApiAccessKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.afterId != null) query.append("afterId", String(args.afterId)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.ownerType != null) query.append("ownerType", String(args.ownerType)) if (args.ownerId != null) query.append("ownerId", String(args.ownerId)) if (args.assetTypeId != null) query.append("assetTypeId", String(args.assetTypeId)) if (ApiAccessKey !== undefined) headers.set("Api-Access-Key", ApiAccessKey) const url = `${baseUrl.replace(/\/+$/, '')}/trusted_destinations${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(trustedDestinationsResponse, await res.json()) };