import {z} from 'zod' export type GetApiV2NftsCollectionsArgs = {limit?: number | undefined, offset?: number | undefined}; export type GetApiV2NftsCollectionsOptions = {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 collections * * GET /v2/nfts/collections */ export const getApiV2NftsCollections = async (args: GetApiV2NftsCollectionsArgs, options?: GetApiV2NftsCollectionsOptions) => { const {baseUrl = "https://tonapi.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) const url = `${baseUrl.replace(/\/+$/, '')}/v2/nfts/collections${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(nftCollections, await res.json()) };