import {z} from 'zod' export type Recipient = string; export type SendNotification = boolean; export type Locale = string; export type ReuploadLinkedFiles = boolean; export type Compressed = boolean; export type NFTMetadata = {name: string, image: string, description: string, animation_url?: string | undefined, attributes?: (Array<{display_type?: ('boost_number' | 'boost_percentage' | 'number') | undefined, trait_type: string, value: string}>) | undefined}; export type NFTMetadataSolana = {name: string, image: string, description: string, symbol?: string | undefined, attributes?: (Array<{display_type?: ('boost_number' | 'boost_percentage' | 'number') | undefined, trait_type: string, value: string}>) | undefined}; export type NFTMetadataOptions = NFTMetadata | string | NFTMetadataSolana | {uri: string, name: string}; export type CreateApi20220609CollectionsCollectionIdNftsArgs = {collectionId: string, body: {recipient: Recipient, sendNotification?: SendNotification | undefined, locale?: Locale | undefined, reuploadLinkedFiles?: ReuploadLinkedFiles | undefined, compressed?: Compressed | undefined, metadata: NFTMetadataOptions} | {recipient: Recipient, sendNotification?: SendNotification | undefined, locale?: Locale | undefined, reuploadLinkedFiles?: ReuploadLinkedFiles | undefined, compressed?: Compressed | undefined, templateId: string}}; export type CreateApi20220609CollectionsCollectionIdNftsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const mintResponseSolana200 = z.object({id: z.string().optional(), onChain: z.object({status: z.string().optional(), chain: z.string().optional()}).optional(), actionId: z.string().optional()}); export const mintResponseEVM200 = z.object({id: z.string().optional(), onChain: z.object({status: z.string().optional(), chain: z.string().optional(), contractAddress: z.string().optional(), subscription: z.object({expiresAt: z.string().optional()}).optional()}).optional(), actionId: z.string().optional()}); export const createApi20220609CollectionsCollectionIdNftsResponse = z.union([mintResponseSolana200, mintResponseEVM200]); 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 }; /** * Mint NFT * * POST /2022-06-09/collections/{collectionId}/nfts */ export const createApi20220609CollectionsCollectionIdNfts = async (args: CreateApi20220609CollectionsCollectionIdNftsArgs, options?: CreateApi20220609CollectionsCollectionIdNftsOptions) => { const {baseUrl = "https://staging.crossmint.com/api", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/2022-06-09/collections/${encodeURIComponent(String(args.collectionId))}/nfts${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(createApi20220609CollectionsCollectionIdNftsResponse, await res.json()) };