import {z} from 'zod' export type GetApiV1ChainNameAddressWalletAddressCollectionCollectionContractArgs = {chainName: string, walletAddress: string, collectionContract: string, 'traits-filter'?: string | undefined, 'values-filter'?: string | undefined}; export type GetApiV1ChainNameAddressWalletAddressCollectionCollectionContractOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1ChainNameAddressWalletAddressCollectionCollectionContractResponse = z.object({updated_at: z.string().optional(), address: z.string().optional(), collection: z.string().optional(), is_spam: z.boolean().optional(), items: z.array(z.object({contract_name: z.string().optional(), contract_ticker_symbol: z.string().optional(), contract_address: z.string().optional(), token_id: z.string().optional(), supports_erc: z.array(z.string()).optional(), last_transfered_at: z.string().optional(), balance: z.string().optional(), balance_24h: z.string().optional(), type: z.string().optional(), nft_data: z.object({token_id: z.string().optional(), token_url: z.string().optional(), original_owner: z.string().optional(), current_owner: z.string().optional(), external_data: z.object({name: z.string().optional(), description: z.string().optional(), asset_url: z.string().optional(), asset_file_extension: z.string().optional(), asset_mime_type: z.string().optional(), asset_size_bytes: z.string().optional(), image: z.string().optional(), image_256: z.string().optional(), image_512: z.string().optional(), image_1024: z.string().optional(), animation_url: z.string().optional(), external_url: z.string().optional(), attributes: z.array(z.object({trait_type: z.string().optional(), value: z.string().optional()})).optional(), thumbnails: z.object({image_256: z.string().optional(), image_512: z.string().optional(), image_1024: z.string().optional(), image_opengraph_url: z.string().optional(), thumbhash: z.string().optional()}).optional(), image_preview: z.string().optional(), asset_properties: z.object({asset_width: z.number().int().optional(), asset_height: z.number().int().optional(), dominant_color: z.string().optional()}).optional()}).optional(), asset_cached: z.boolean().optional(), image_cached: z.boolean().optional()}).optional()})).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 }; /** * Verifies ownership of NFTs (including ERC-721 and ERC-1155) within a collection. * * GET /v1/{chainName}/address/{walletAddress}/collection/{collectionContract}/ */ export const getApiV1ChainNameAddressWalletAddressCollectionCollectionContract = async (args: GetApiV1ChainNameAddressWalletAddressCollectionCollectionContractArgs, options?: GetApiV1ChainNameAddressWalletAddressCollectionCollectionContractOptions) => { const {baseUrl = "https://api.covalenthq.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['traits-filter'] != null) query.append("traits-filter", String(args['traits-filter'])) if (args['values-filter'] != null) query.append("values-filter", String(args['values-filter'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/${encodeURIComponent(String(args.chainName))}/address/${encodeURIComponent(String(args.walletAddress))}/collection/${encodeURIComponent(String(args.collectionContract))}/${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(getApiV1ChainNameAddressWalletAddressCollectionCollectionContractResponse, await res.json()) };