import {z} from 'zod' export type GetApiAssetsAssetArgs = {asset: string}; export type GetApiAssetsAssetOptions = {baseUrl?: string | undefined, projectId?: string | undefined, fetch?: typeof fetch | undefined}; export const asset = z.object({asset: z.string(), policy_id: z.string(), asset_name: z.string().nullable(), fingerprint: z.string(), quantity: z.string(), initial_mint_tx_hash: z.string(), mint_or_burn_count: z.number().int(), onchain_metadata: z.record(z.string(), z.unknown()).nullable(), onchain_metadata_standard: z.enum(["CIP25v1", "CIP25v2", "CIP68v1", "CIP68v2", "CIP68v3"]).nullable().optional(), onchain_metadata_extra: z.string().nullable().optional(), metadata: z.object({name: z.string(), description: z.string(), ticker: z.string().nullable(), url: z.string().nullable(), logo: z.string().nullable(), decimals: z.number().int().lte(255).nullable()}).nullable()}); 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 }; /** * Specific asset * * GET /assets/{asset} */ export const getApiAssetsAsset = async (args: GetApiAssetsAssetArgs, options?: GetApiAssetsAssetOptions) => { const {baseUrl = "https://cardano-mainnet.blockfrost.io/api/v0", fetch: fetchFn = fetch, projectId} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (projectId !== undefined) headers.set("project_id", projectId) const url = `${baseUrl.replace(/\/+$/, '')}/assets/${encodeURIComponent(String(args.asset))}${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(asset, await res.json()) };