import {z} from 'zod' export type ChainName = string; export type GetApiV1ChainsChainNameCollectionsContractAddressArgs = {contract_address: string, chain_name: ChainName}; export type GetApiV1ChainsChainNameCollectionsContractAddressOptions = {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 getCollectionResult = z.object({result: collection}); 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 collection by contract address * * GET /v1/chains/{chain_name}/collections/{contract_address} */ export const getApiV1ChainsChainNameCollectionsContractAddress = async (args: GetApiV1ChainsChainNameCollectionsContractAddressArgs, options?: GetApiV1ChainsChainNameCollectionsContractAddressOptions) => { const {baseUrl = "https://api.sandbox.immutable.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/chains/${encodeURIComponent(String(args.chain_name))}/collections/${encodeURIComponent(String(args.contract_address))}${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(getCollectionResult, await res.json()) };