import {z} from 'zod' export type NodeTypeEnum = 'shared' | 'dedicated'; export type Provider = 'aws' | 'azure' | 'gcloud' | 'vzo'; export type Region = 'asia-southeast1' | 'ap-southeast-1' | 'us-west-2' | 'us-east-1' | 'uksouth' | 'eu3'; export type EthereumNodeDetails = {mode?: ('full' | 'archive') | undefined, version?: string | undefined, api_namespaces?: Array | undefined, https_endpoint?: string | undefined, wss_endpoint?: string | undefined, beacon_endpoint?: string | undefined, graphql_endpoint?: string | undefined, auth_username?: string | undefined, auth_password?: string | undefined, auth_key?: string | undefined}; export type BinanceSmartChainNodeDetails = {mode?: ('full' | 'archive') | undefined, version?: string | undefined, api_namespaces?: Array | undefined, https_endpoint?: string | undefined, wss_endpoint?: string | undefined, graphql_endpoint?: string | undefined, auth_username?: string | undefined, auth_password?: string | undefined, auth_key?: string | undefined}; export type AvalancheNodeDetails = {version?: string | undefined, api_namespaces?: Array | undefined, https_endpoint?: string | undefined, wss_endpoint?: string | undefined, auth_username?: string | undefined, auth_password?: string | undefined, auth_key?: string | undefined}; export type FantomNodeDetails = {version?: string | undefined, api_namespaces?: Array | undefined, https_endpoint?: string | undefined, wss_endpoint?: string | undefined, graphql_endpoint?: string | undefined, auth_username?: string | undefined, auth_password?: string | undefined, auth_key?: string | undefined}; export type TezosNodeDetails = {version?: string | undefined, https_endpoint?: string | undefined, auth_username?: string | undefined, auth_password?: string | undefined, auth_key?: string | undefined}; export type SolanaNodeDetails = {version?: string | undefined, https_endpoint?: string | undefined, wss_endpoint?: string | undefined, auth_username?: string | undefined, auth_password?: string | undefined, auth_key?: string | undefined}; export type StarknetNodeDetails = {version?: string | undefined, https_endpoint?: string | undefined, auth_username?: string | undefined, auth_password?: string | undefined, auth_key?: string | undefined}; export type BitcoinNodeDetails = {version?: string | undefined, https_endpoint?: string | undefined, auth_username?: string | undefined, auth_password?: string | undefined, auth_key?: string | undefined}; export type NodeDetails = EthereumNodeDetails | BinanceSmartChainNodeDetails | AvalancheNodeDetails | FantomNodeDetails | TezosNodeDetails | SolanaNodeDetails | StarknetNodeDetails | BitcoinNodeDetails; export type NodeStatusEnum = 'pending' | 'running' | 'maintenance' | 'failed' | 'error' | 'stopping' | 'stopped' | 'starting'; export type Object = {id?: string | undefined, name?: string | undefined}; export type Creator = {id?: string | undefined, email?: string | undefined, first_name?: string | undefined, last_name?: string | undefined, organization?: Object | undefined}; export type CreatedAt = string; export type NodeRoleCreateEnum = 'peer'; export type EthereumNodeCreateConfiguration = {archive: boolean}; export type PolygonPoSNodeCreateConfiguration = {archive: boolean}; export type BinanceSmartChainNodeCreateConfiguration = {archive: boolean}; export type AvalancheNodeCreateConfiguration = {archive: boolean}; export type FantomNodeCreateConfiguration = {archive: boolean}; export type TezosNodeCreateConfiguration = {archive: boolean, tezos_node_type: string}; export type SolanaNodeCreateConfiguration = Record; export type StarknetNodeCreateConfiguration = Record; export type NodeCreateConfiguration = EthereumNodeCreateConfiguration | PolygonPoSNodeCreateConfiguration | BinanceSmartChainNodeCreateConfiguration | AvalancheNodeCreateConfiguration | FantomNodeCreateConfiguration | TezosNodeCreateConfiguration | SolanaNodeCreateConfiguration | StarknetNodeCreateConfiguration; export type NodeCreate = {id?: string | undefined, name: string, network: string, organization?: string | undefined, type: NodeTypeEnum, provider: Provider, region: Region, details?: NodeDetails | undefined, status?: NodeStatusEnum | undefined, creator?: Creator | undefined, created_at?: CreatedAt | undefined, role: NodeRoleCreateEnum, configuration: NodeCreateConfiguration}; export type CreateApiV1NodesArgs = {body: NodeCreate}; export type CreateApiV1NodesOptions = {baseUrl?: string | undefined, APIKeyAuthentication?: string | undefined, fetch?: typeof fetch | undefined}; export const nodeTypeEnum = z.enum(["shared", "dedicated"]); export const provider = z.enum(["aws", "azure", "gcloud", "vzo"]); export const region = z.enum(["asia-southeast1", "ap-southeast-1", "us-west-2", "us-east-1", "uksouth", "eu3"]); export const ethereumNodeDetails = z.object({mode: z.enum(["full", "archive"]).optional(), version: z.string().optional(), api_namespaces: z.array(z.string()).optional(), https_endpoint: z.string().optional(), wss_endpoint: z.string().optional(), beacon_endpoint: z.string().optional(), graphql_endpoint: z.string().optional(), auth_username: z.string().optional(), auth_password: z.string().optional(), auth_key: z.string().optional()}); export const binanceSmartChainNodeDetails = z.object({mode: z.enum(["full", "archive"]).optional(), version: z.string().optional(), api_namespaces: z.array(z.string()).optional(), https_endpoint: z.string().optional(), wss_endpoint: z.string().optional(), graphql_endpoint: z.string().optional(), auth_username: z.string().optional(), auth_password: z.string().optional(), auth_key: z.string().optional()}); export const avalancheNodeDetails = z.object({version: z.string().optional(), api_namespaces: z.array(z.string()).optional(), https_endpoint: z.string().optional(), wss_endpoint: z.string().optional(), auth_username: z.string().optional(), auth_password: z.string().optional(), auth_key: z.string().optional()}); export const fantomNodeDetails = z.object({version: z.string().optional(), api_namespaces: z.array(z.string()).optional(), https_endpoint: z.string().optional(), wss_endpoint: z.string().optional(), graphql_endpoint: z.string().optional(), auth_username: z.string().optional(), auth_password: z.string().optional(), auth_key: z.string().optional()}); export const tezosNodeDetails = z.object({version: z.string().optional(), https_endpoint: z.string().optional(), auth_username: z.string().optional(), auth_password: z.string().optional(), auth_key: z.string().optional()}); export const solanaNodeDetails = z.object({version: z.string().optional(), https_endpoint: z.string().optional(), wss_endpoint: z.string().optional(), auth_username: z.string().optional(), auth_password: z.string().optional(), auth_key: z.string().optional()}); export const starknetNodeDetails = z.object({version: z.string().optional(), https_endpoint: z.string().optional(), auth_username: z.string().optional(), auth_password: z.string().optional(), auth_key: z.string().optional()}); export const bitcoinNodeDetails = z.object({version: z.string().optional(), https_endpoint: z.string().optional(), auth_username: z.string().optional(), auth_password: z.string().optional(), auth_key: z.string().optional()}); export const nodeDetails = z.union([ethereumNodeDetails, binanceSmartChainNodeDetails, avalancheNodeDetails, fantomNodeDetails, tezosNodeDetails, solanaNodeDetails, starknetNodeDetails, bitcoinNodeDetails]); export const nodeStatusEnum = z.enum(["pending", "running", "maintenance", "failed", "error", "stopping", "stopped", "starting"]); export const object = z.object({id: z.string().optional(), name: z.string().optional()}); export const creator = z.object({id: z.string().optional(), email: z.string().optional(), first_name: z.string().optional(), last_name: z.string().optional(), organization: object.optional()}); export const createdAt = z.string(); export const nodeRoleEnum = z.literal("peer"); export const ethereumNodeConfiguration = z.object({archive: z.boolean().optional()}); export const polygonPoSNodeConfiguration = z.object({archive: z.boolean().optional()}); export const binanceSmartChainNodeConfiguration = z.object({archive: z.boolean().optional()}); export const avalancheNodeConfiguration = z.object({archive: z.boolean().optional()}); export const fantomNodeConfiguration = z.object({archive: z.boolean().optional()}); export const tezosNodeConfiguration = z.object({archive: z.boolean().optional(), tezos_node_type: z.string().optional()}); export const solanaNodeConfiguration = z.object({}); export const starknetNodeConfiguration = z.object({}); export const nodeConfiguration = z.union([ethereumNodeConfiguration, polygonPoSNodeConfiguration, binanceSmartChainNodeConfiguration, avalancheNodeConfiguration, fantomNodeConfiguration, tezosNodeConfiguration, solanaNodeConfiguration, starknetNodeConfiguration]); export const node = z.object({id: z.string().optional(), name: z.string().optional(), network: z.string().optional(), organization: z.string().optional(), type: nodeTypeEnum.optional(), provider: provider.optional(), region: region.optional(), details: nodeDetails.optional(), status: nodeStatusEnum.optional(), creator: creator.optional(), created_at: createdAt.optional(), role: nodeRoleEnum.optional(), configuration: nodeConfiguration.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 }; /** * Create Node * * POST /v1/nodes/ */ export const createApiV1Nodes = async (args: CreateApiV1NodesArgs, options?: CreateApiV1NodesOptions) => { const {baseUrl = "https://api.chainstack.com", fetch: fetchFn = fetch, APIKeyAuthentication} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKeyAuthentication !== undefined) headers.set('authorization', `Bearer ${APIKeyAuthentication}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/nodes/${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(node, await res.json()) };