import {z} from 'zod' export type LedgerStateSelector = {state_version?: (number | null) | undefined, timestamp?: (string | null) | undefined, epoch?: (number | null) | undefined, round?: (number | null) | undefined} | null; export type Address = string; export type StatePackageBlueprintPageRequest = {at_ledger_state?: LedgerStateSelector | undefined, cursor?: (string | null) | undefined, limit_per_page?: (number | null) | undefined, package_address: Address}; export type CreateApiStatePackagePageBlueprintsArgs = {body: StatePackageBlueprintPageRequest}; export type CreateApiStatePackagePageBlueprintsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const networkName = z.string(); export const ledgerState = z.object({network: networkName, state_version: z.number().int(), proposer_round_timestamp: z.string(), epoch: z.number().int(), round: z.number().int()}); export const address = z.string(); export const bigDecimal = z.string(); export const royaltyAmount = z.object({amount: bigDecimal, unit: z.enum(["XRD", "USD"])}); export const blueprintMethodRoyalty = z.object({method_name: z.string(), royalty_amount: royaltyAmount.optional()}); export const blueprintRoyaltyConfig = z.object({is_enabled: z.boolean(), method_rules: z.array(blueprintMethodRoyalty).optional()}); export const packageBlueprintCollectionItem = z.object({name: z.string(), version: z.string(), definition: z.object({}), dependant_entities: z.array(address).optional(), auth_template: z.object({}).optional(), auth_template_is_locked: z.boolean().nullable().optional(), royalty_config: blueprintRoyaltyConfig.optional(), royalty_config_is_locked: z.boolean().nullable().optional()}); export const statePackageBlueprintPageResponse = z.object({ledger_state: ledgerState, total_count: z.number().int().nullable().optional(), next_cursor: z.string().nullable().optional(), items: z.array(packageBlueprintCollectionItem), package_address: address}); 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 }; /** * Get Package Blueprints Page * * POST /state/package/page/blueprints */ export const createApiStatePackagePageBlueprints = async (args: CreateApiStatePackagePageBlueprintsArgs, options?: CreateApiStatePackagePageBlueprintsOptions) => { const {baseUrl = "https://mainnet.radixdlt.com", 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(/\/+$/, '')}/state/package/page/blueprints${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(statePackageBlueprintPageResponse, await res.json()) };