import {z} from 'zod' export type ChainName = string; export type PageCursor = string; export type PageSize = number; export type GetApiV1ChainsChainNameNftOwnersArgs = {chain_name: ChainName, from_updated_at?: string | undefined, page_cursor?: PageCursor | undefined, page_size?: PageSize | undefined}; export type GetApiV1ChainsChainNameNftOwnersOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const chain = z.object({id: z.string(), name: z.string()}); export const nFTOwner = z.object({chain: chain, contract_address: z.string(), token_id: z.string(), account_address: z.string(), balance: z.string(), updated_at: z.string().optional()}); export const page = z.object({previous_cursor: z.string().nullable(), next_cursor: z.string().nullable()}); export const listNFTOwnersResult = z.object({result: z.array(nFTOwner), 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 all NFT owners * * GET /v1/chains/{chain_name}/nft-owners */ export const getApiV1ChainsChainNameNftOwners = async (args: GetApiV1ChainsChainNameNftOwnersArgs, options?: GetApiV1ChainsChainNameNftOwnersOptions) => { 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.from_updated_at != null) query.append("from_updated_at", String(args.from_updated_at)) 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))}/nft-owners${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(listNFTOwnersResult, await res.json()) };