import {z} from 'zod' export type CreateApiV2NftsCollectionsBulkArgs = {body: {account_ids: Array}}; export type CreateApiV2NftsCollectionsBulkOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const nftApprovedBy = z.array(z.enum(["getgems", "tonkeeper"])); export const nftCollectionMetadataStatus = z.object({is_broken: z.boolean().optional(), last_refresh_success: z.number().int().nullable().optional(), last_refresh_try: z.number().int().nullable().optional(), url: z.string().optional()}); export const accountAddress = z.object({address: z.string(), icon: z.string().optional(), is_scam: z.boolean(), is_wallet: z.boolean(), name: z.string().optional()}); export const imagePreview = z.object({resolution: z.string(), url: z.string()}); export const trustType = z.enum(["whitelist", "graylist", "blacklist", "none"]); export const nftCollection = z.object({address: z.string(), approved_by: nftApprovedBy, metadata: z.record(z.string(), z.unknown()).optional(), metadata_status: nftCollectionMetadataStatus.optional(), next_item_index: z.number().int(), owner: accountAddress.optional(), previews: z.array(imagePreview).optional(), raw_collection_content: z.string(), trust: trustType}); export const nftCollections = z.object({nft_collections: z.array(nftCollection)}); 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 NFT collection items by their addresses * * POST /v2/nfts/collections/_bulk */ export const createApiV2NftsCollectionsBulk = async (args: CreateApiV2NftsCollectionsBulkArgs, options?: CreateApiV2NftsCollectionsBulkOptions) => { const {baseUrl = "https://tonapi.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v2/nfts/collections/_bulk${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(nftCollections, await res.json()) };