import {z} from 'zod' export type ChainName = string; export type PageCursor = string; export type PageSize = number; export type MintRequestStatus = 'pending' | 'succeeded' | 'failed'; export type GetApiV1ChainsChainNameCollectionsContractAddressNftsMintRequestsArgs = {contract_address: string, chain_name: ChainName, page_cursor?: PageCursor | undefined, page_size?: PageSize | undefined, status?: MintRequestStatus | undefined}; export type GetApiV1ChainsChainNameCollectionsContractAddressNftsMintRequestsOptions = {baseUrl?: string | undefined, ImmutableApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const chain = z.object({id: z.string(), name: z.string()}); export const mintRequestErrorMessage = z.object({message: z.string().optional()}).nullable(); export const mintRequestStatus = z.enum(["pending", "succeeded", "failed"]); export const getMintRequestResult = z.object({chain: chain, collection_address: z.string(), reference_id: z.string(), owner_address: z.string(), token_id: z.string().nullable(), amount: z.string().nullable().optional(), activity_id: z.string().nullable().optional(), transaction_hash: z.string().nullable(), created_at: z.string(), updated_at: z.string(), error: mintRequestErrorMessage, status: mintRequestStatus}); export const page = z.object({previous_cursor: z.string().nullable(), next_cursor: z.string().nullable()}); export const listMintRequestsResult = z.object({result: z.array(getMintRequestResult), 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 mint requests * * GET /v1/chains/{chain_name}/collections/{contract_address}/nfts/mint-requests */ export const getApiV1ChainsChainNameCollectionsContractAddressNftsMintRequests = async (args: GetApiV1ChainsChainNameCollectionsContractAddressNftsMintRequestsArgs, options?: GetApiV1ChainsChainNameCollectionsContractAddressNftsMintRequestsOptions) => { const {baseUrl = "https://api.sandbox.immutable.com", fetch: fetchFn = fetch, ImmutableApiKey} = 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)) if (args.status != null) query.append("status", String(args.status)) 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))}/nfts/mint-requests${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(listMintRequestsResult, await res.json()) };