import {z} from 'zod' export type ChainName = string; export type CollectionMetadata = {name: string | null, symbol: string | null, description: string | null, image: string | null, external_link: string | null, contract_uri: string | null, base_uri: string | null}; export type RefreshCollectionMetadataRequest = {collection_metadata: CollectionMetadata}; export type CreateApiV1ChainsChainNameCollectionsContractAddressRefreshMetadataArgs = {contract_address: string, chain_name: ChainName, body: RefreshCollectionMetadataRequest}; export type CreateApiV1ChainsChainNameCollectionsContractAddressRefreshMetadataOptions = {baseUrl?: string | undefined, ImmutableApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const chain = z.object({id: z.string(), name: z.string()}); export const collectionMetadata = z.object({name: z.string().nullable(), symbol: z.string().nullable(), description: z.string().nullable(), image: z.string().nullable(), external_link: z.string().nullable(), contract_uri: z.string().nullable(), base_uri: z.string().nullable()}); export const refreshCollectionMetadataResult = z.object({contract_address: z.string(), chain: chain, collection_metadata: collectionMetadata}); 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 }; /** * Refresh collection metadata * * POST /v1/chains/{chain_name}/collections/{contract_address}/refresh-metadata * * Also accepts auth: BearerAuthWithClient, BearerAuth */ export const createApiV1ChainsChainNameCollectionsContractAddressRefreshMetadata = async (args: CreateApiV1ChainsChainNameCollectionsContractAddressRefreshMetadataArgs, options?: CreateApiV1ChainsChainNameCollectionsContractAddressRefreshMetadataOptions) => { const {baseUrl = "https://api.sandbox.immutable.com", fetch: fetchFn = fetch, ImmutableApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ImmutableApiKey !== undefined) headers.set("x-immutable-api-key", ImmutableApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/chains/${encodeURIComponent(String(args.chain_name))}/collections/${encodeURIComponent(String(args.contract_address))}/refresh-metadata${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(refreshCollectionMetadataResult, await res.json()) };