import {z} from 'zod' export type V3IPFSMetadata = {description?: string | undefined, document_attribute_keys?: Array | undefined, image?: string | undefined, name?: string | undefined}; export type CoreapiMintNFTV3Request = {document_id?: string | undefined, grant_read_access?: boolean | undefined, ipfs_metadata?: V3IPFSMetadata | undefined, owner?: string | undefined}; export type CreateApiV3NftsCollectionsCollectionIdCommitAndMintArgs = {authorization: string, collection_id: string, body: CoreapiMintNFTV3Request}; export type CreateApiV3NftsCollectionsCollectionIdCommitAndMintOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const coreapiNFTResponseHeader = z.object({job_id: z.string().optional()}); export const v3IPFSMetadata = z.object({description: z.string().optional(), document_attribute_keys: z.array(z.string()).optional(), image: z.string().optional(), name: z.string().optional()}); export const coreapiMintNFTV3Response = z.object({collection_id: z.number().int().optional(), document_id: z.string().optional(), header: coreapiNFTResponseHeader.optional(), ipfs_metadata: v3IPFSMetadata.optional(), item_id: z.string().optional(), owner: z.string().optional()}); 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 }; /** * commits a pending document and mints an NFT on the Centrifuge chain. * * POST /v3/nfts/collections/{collection_id}/commit_and_mint */ export const createApiV3NftsCollectionsCollectionIdCommitAndMint = async (args: CreateApiV3NftsCollectionsCollectionIdCommitAndMintArgs, options?: CreateApiV3NftsCollectionsCollectionIdCommitAndMintOptions) => { const {baseUrl = "http://localhost:8082", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.authorization != null) headers.set("authorization", String(args.authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/v3/nfts/collections/${encodeURIComponent(String(args.collection_id))}/commit_and_mint${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(coreapiMintNFTV3Response, await res.json()) };