import type { ResolvedRegister } from './internal/register.js' import type { Compute, IsNarrowable } from './internal/types.js' export type { Eth } from './internal/rpcSchemas/eth.js' export type { Wallet } from './internal/rpcSchemas/wallet.js' /** * Instantiates a statically typed Schema. This is a runtime-noop function, and is purposed * to be used as a type-level tag to be used with {@link ox#Provider.(from:function)} or * {@link ox#RpcTransport.(fromHttp:function)}. * * @example * ### Using with `Provider.from` * * ```ts twoslash * // @noErrors * import 'ox/window' * import { Provider, RpcSchema } from 'ox' * * const schema = RpcSchema.from< * | RpcSchema.Default * | { * Request: { * method: 'abe_foo', * params: [id: number], * } * ReturnType: string * } * | { * Request: { * method: 'abe_bar', * params: [id: string], * } * ReturnType: string * } * >() * * const provider = Provider.from(window.ethereum, { schema }) * * const blockNumber = await provider.request({ method: 'e' }) * // ^| * * * * * * ``` */ export function from(): schema { return null as never } /** * Extracts a schema item from a {@link ox#RpcSchema.Generic} or {@link ox#RpcSchema.MethodNameGeneric}. * * @example * ```ts twoslash * import { RpcSchema } from 'ox' * * type Item = RpcSchema.ExtractItem * // ^? * * * * * * * * * * * * * * ``` */ export type ExtractItem< schema extends Generic, methodName extends MethodNameGeneric = MethodNameGeneric, > = Compute<{ Request: ExtractRequest ReturnType: ExtractReturnType }> /** * Extracts request from a {@link ox#RpcSchema.Generic} or {@link ox#RpcSchema.MethodNameGeneric}. * * @example * ```ts twoslash * import { RpcSchema } from 'ox' * * type Request = RpcSchema.ExtractRequest * // ^? * * * * * * * * * * * * ``` */ export type ExtractRequest< schema extends Generic, methodName extends MethodNameGeneric = MethodNameGeneric, > = Extract /** * Type-safe union of all JSON-RPC Method Names. * * @example * ```ts twoslash * import { RpcSchema } from 'ox' * * type MethodName = RpcSchema.ExtractMethodName * // ^? * * * * * * * * * ``` */ export type ExtractMethodName = schema['Request']['method'] /** * Extracts parameters from a {@link ox#RpcSchema.Generic} or {@link ox#RpcSchema.MethodNameGeneric}. * * @example * ```ts twoslash * import { RpcSchema } from 'ox' * * type Eth_GetBlockByNumber = RpcSchema.ExtractParams * // ^? * * * * * * ``` */ export type ExtractParams< schema extends Generic, methodName extends MethodNameGeneric = MethodNameGeneric, > = ExtractRequest['params'] /** * Extracts return type from a {@link ox#RpcSchema.Generic} or {@link ox#RpcSchema.MethodNameGeneric}. * * @example * ```ts twoslash * import { RpcSchema } from 'ox' * * type ReturnType = RpcSchema.ExtractReturnType * // ^? * * * * * * * * * * * * ``` */ export type ExtractReturnType< schema extends Generic, methodName extends MethodNameGeneric = MethodNameGeneric, > = methodName extends schema['Request']['method'] ? IsNarrowable extends true ? Extract['ReturnType'] : unknown : unknown /** * Type to define a custom type-safe JSON-RPC Schema. * * @example * ```ts twoslash * import { RpcSchema, RpcRequest } from 'ox' * * type Schema = RpcSchema.From<{ * Request: { * method: 'eth_foobar', * params: [id: number], * } * ReturnType: string * }> * ``` */ export type From = schema /** * Generic type to define a JSON-RPC Method. * * @example * ```ts twoslash * import { RpcSchema } from 'ox' * * type Schema = RpcSchema.Generic * // ^? * * * * * * * ``` */ export type Generic = { Request: { method: name params?: params | undefined } ReturnType?: unknown } /** * Type-safe union of all JSON-RPC Methods. * * @example * ```ts twoslash * import { RpcSchema } from 'ox' * * type Schema = RpcSchema.Default * // ^? * * * * * * * * * * * * * * ``` */ export type Default = ResolvedRegister['RpcSchema'] /** * Generic type to define a JSON-RPC Method Name. * * @example * ```ts twoslash * import { RpcSchema } from 'ox' * * type Name = RpcSchema.MethodNameGeneric * // ^? * * * * * * ``` */ export type MethodNameGeneric = | schema['Request']['method'] | (string & {}) /** * Converts an Ox {@link ox#RpcSchema.Generic} (union of `{ Request, ReturnType }`) to * a [Viem-compatible RPC schema](https://viem.sh) (tuple of `{ Method, Parameters, ReturnType }`). * * @example * ```ts twoslash * import { RpcSchema } from 'ox' * * type ViemSchema = RpcSchema.ToViem< * | { * Request: { method: 'eth_blockNumber'; params?: undefined } * ReturnType: `0x${string}` * } * | { * Request: { method: 'eth_chainId'; params?: undefined } * ReturnType: `0x${string}` * } * > * // ^? [{ Method: 'eth_blockNumber'; Parameters?: undefined; ReturnType: `0x${string}` }, ...] * ``` */ export type ToViem = UnionToTuple< schema extends schema ? { Method: schema['Request']['method'] Parameters: schema['Request']['params'] ReturnType: schema extends { ReturnType: infer r } ? r : unknown } : never > /** * Converts a [Viem-compatible RPC schema](https://viem.sh) (tuple of `{ Method, Parameters, ReturnType }`) * to an Ox {@link ox#RpcSchema.Generic} (union of `{ Request, ReturnType }`). * * @example * ```ts twoslash * import { RpcSchema } from 'ox' * * type OxSchema = RpcSchema.FromViem<[ * { Method: 'eth_blockNumber'; Parameters?: undefined; ReturnType: `0x${string}` }, * { Method: 'eth_chainId'; Parameters?: undefined; ReturnType: `0x${string}` }, * ]> * // ^? { Request: { method: 'eth_blockNumber'; params?: undefined }; ReturnType: `0x${string}` } | ... * ``` */ export type FromViem = { [k in keyof schema]: schema[k] extends ViemSchemaItem ? { Request: { method: schema[k]['Method'] params: schema[k]['Parameters'] } ReturnType: schema[k]['ReturnType'] } : never }[number] /** @internal */ type ViemSchemaItem = { Method: string Parameters?: unknown ReturnType?: unknown } // --- Union-to-tuple helpers (order is not guaranteed, but stable) --- /** @internal */ type UnionToIntersection = ( union extends unknown ? (arg: () => union) => void : never ) extends (arg: infer intersection) => void ? intersection : never /** @internal */ type UnionLast = UnionToIntersection extends () => infer last ? last : never /** @internal */ type UnionToTuple> = [union] extends [never] ? [] : [...UnionToTuple>, last]