import {z} from 'zod' export type ChainName = string; export type PageCursor = string; export type PageSize = number; export type GetApiV1ChainsChainNameAccountsAccountAddressCollectionsArgs = {account_address: string, chain_name: ChainName, page_cursor?: PageCursor | undefined, page_size?: PageSize | undefined}; export type GetApiV1ChainsChainNameAccountsAccountAddressCollectionsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const chain = z.object({id: z.string(), name: z.string()}); export const collectionContractType = z.enum(["ERC721", "ERC1155"]); export const assetVerificationStatus = z.enum(["verified", "unverified", "spam", "inactive"]); export const collection = z.object({chain: chain, name: z.string().nullable(), symbol: z.string().nullable(), contract_type: collectionContractType, contract_address: z.string(), description: z.string().nullable(), image: z.string().nullable(), external_link: z.string().nullable(), contract_uri: z.string().nullable().optional(), base_uri: z.string().nullable(), verification_status: assetVerificationStatus, indexed_at: z.string(), updated_at: z.string(), metadata_synced_at: z.string().nullable()}); export const page = z.object({previous_cursor: z.string().nullable(), next_cursor: z.string().nullable()}); export const listCollectionsResult = z.object({result: z.array(collection), 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 collections by NFT owner * * GET /v1/chains/{chain_name}/accounts/{account_address}/collections */ export const getApiV1ChainsChainNameAccountsAccountAddressCollections = async (args: GetApiV1ChainsChainNameAccountsAccountAddressCollectionsArgs, options?: GetApiV1ChainsChainNameAccountsAccountAddressCollectionsOptions) => { const {baseUrl = "https://api.sandbox.immutable.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page_cursor != null) query.append("page_cursor", String(args.page_cursor)) if (args.page_size != null) query.append("page_size", String(args.page_size)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/chains/${encodeURIComponent(String(args.chain_name))}/accounts/${encodeURIComponent(String(args.account_address))}/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(listCollectionsResult, await res.json()) };