import {z} from 'zod' export type GetApiTokenizationTemplatesArgs = {limit?: number | undefined, offset?: number | undefined, pageCursor?: string | undefined, pageSize?: number | undefined, type?: ('FUNGIBLE_TOKEN' | 'NON_FUNGIBLE_TOKEN' | 'TOKEN_UTILITY') | undefined, initializationPhase?: ('ON_DEPLOYMENT' | 'POST_DEPLOYMENT') | undefined}; export type GetApiTokenizationTemplatesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const auditorData = z.object({name: z.string(), imageURL: z.string(), link: z.string()}); export const contractAttributes = z.object({useCases: z.array(z.string()).optional(), standards: z.array(z.string()).optional(), auditor: auditorData.optional()}); export const vendorDto = z.object({id: z.string(), name: z.string()}); export const leanContractDto = z.object({id: z.string(), name: z.string(), description: z.string(), attributes: contractAttributes.optional(), isPublic: z.boolean(), canDeploy: z.boolean().optional(), owner: z.string().optional(), vendor: vendorDto.optional(), type: z.enum(["FUNGIBLE_TOKEN", "NON_FUNGIBLE_TOKEN", "NON_TOKEN", "TOKEN_EXTENSION", "TOKEN_UTILITY"]).optional()}); export const templatesPaginatedResponse = z.object({data: z.array(leanContractDto), next: z.string().nullable().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 }; /** * List all contract templates * * GET /tokenization/templates */ export const getApiTokenizationTemplates = async (args: GetApiTokenizationTemplatesArgs, options?: GetApiTokenizationTemplatesOptions) => { const {baseUrl = "https://api.fireblocks.io/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.pageCursor != null) query.append("pageCursor", String(args.pageCursor)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.type != null) query.append("type", String(args.type)) if (args.initializationPhase != null) query.append("initializationPhase", String(args.initializationPhase)) const url = `${baseUrl.replace(/\/+$/, '')}/tokenization/templates${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(templatesPaginatedResponse, await res.json()) };