import {z} from 'zod' export type Spacemeshv2alpha1NodeStatusRequest = Record; export type CreateApiSpacemeshV2alpha1NodeServiceStatusArgs = {body: Spacemeshv2alpha1NodeStatusRequest}; export type CreateApiSpacemeshV2alpha1NodeServiceStatusOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const spacemeshv2alpha1NodeStatusResponseSyncStatus = z.enum(["SYNC_STATUS_UNSPECIFIED", "SYNC_STATUS_OFFLINE", "SYNC_STATUS_SYNCING", "SYNC_STATUS_SYNCED"]); export const spacemeshv2alpha1NodeStatusResponse = z.object({appliedLayer: z.number().int().optional(), connectedPeers: z.string().optional(), currentLayer: z.number().int().optional(), latestLayer: z.number().int().optional(), processedLayer: z.number().int().optional(), status: spacemeshv2alpha1NodeStatusResponseSyncStatus.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 }; /** * Node status * * POST /spacemesh.v2alpha1.NodeService/Status */ export const createApiSpacemeshV2alpha1NodeServiceStatus = async (args: CreateApiSpacemeshV2alpha1NodeServiceStatusArgs, options?: CreateApiSpacemeshV2alpha1NodeServiceStatusOptions) => { const {baseUrl = "https://testnet-api.spacemesh.network", 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(/\/+$/, '')}/spacemesh.v2alpha1.NodeService/Status${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(spacemeshv2alpha1NodeStatusResponse, await res.json()) };