diff --git a/.changeset/giant-seals-mix.md b/.changeset/giant-seals-mix.md new file mode 100644 index 00000000000..ca9f52324a6 --- /dev/null +++ b/.changeset/giant-seals-mix.md @@ -0,0 +1,6 @@ +--- +'@solana/rpc-spec-types': patch +'@solana/rpc-spec': patch +--- + +Move RpcRequest and RpcResponse types to rpc-spec-types diff --git a/packages/rpc-spec-types/README.md b/packages/rpc-spec-types/README.md index 8db2a17b155..d9e812a1c11 100644 --- a/packages/rpc-spec-types/README.md +++ b/packages/rpc-spec-types/README.md @@ -14,4 +14,25 @@ # @solana/rpc-spec-types -TODO +This package contains core types that can be used on both RPC and RPC Subscriptions specifications. It can be used standalone, but it is also exported as part of the Solana JavaScript SDK [`@solana/web3.js@rc`](https://github.com/solana-labs/solana-web3.js/tree/master/packages/library). + +## Types + +### `RpcRequest` + +An object that describes the elements of an RPC or RPC Subscriptions request. It consists of the following properties: + +- `methodName`: The name of the RPC method or subscription requested. +- `params`: The parameters to be passed to the RPC server. + +### `RpcRequestTransformer` + +A function that accepts an `RpcRequest` and returns another `RpcRequest`. This allows the `RpcApi` to transform the request before it is sent to the RPC server. + +### `RpcResponse` + +A type that represents the response from an RPC server. This could be any sort of data which is why `RpcResponse` defaults to `unknown`. You may use a type parameter to specify the shape of the response — e.g. `RpcResponse<{ result: number }>`. + +### `RpcResponseTransformer` + +A function that accepts an `RpcResponse` and returns another `RpcResponse`. This allows the `RpcApi` to transform the response before it is returned to the caller. diff --git a/packages/rpc-spec-types/src/index.ts b/packages/rpc-spec-types/src/index.ts index a151cd974c6..878f73d707a 100644 --- a/packages/rpc-spec-types/src/index.ts +++ b/packages/rpc-spec-types/src/index.ts @@ -1,4 +1,5 @@ export * from './overloads'; export * from './rpc-message'; +export * from './rpc-request'; export * from './rpc-response'; export * from './type-helpers'; diff --git a/packages/rpc-spec/src/rpc-shared.ts b/packages/rpc-spec-types/src/rpc-request.ts similarity index 51% rename from packages/rpc-spec/src/rpc-shared.ts rename to packages/rpc-spec-types/src/rpc-request.ts index 7e4b1e74c02..1b15caade12 100644 --- a/packages/rpc-spec/src/rpc-shared.ts +++ b/packages/rpc-spec-types/src/rpc-request.ts @@ -3,12 +3,6 @@ export type RpcRequest = { readonly params: TParams; }; -export type RpcResponse = TResponse; - export type RpcRequestTransformer = { (request: RpcRequest): RpcRequest; }; - -export type RpcResponseTransformer = { - (response: RpcResponse, request: RpcRequest): RpcResponse; -}; diff --git a/packages/rpc-spec-types/src/rpc-response.ts b/packages/rpc-spec-types/src/rpc-response.ts index 485323271f7..7efba62a60a 100644 --- a/packages/rpc-spec-types/src/rpc-response.ts +++ b/packages/rpc-spec-types/src/rpc-response.ts @@ -1,3 +1,11 @@ +import type { RpcRequest } from './rpc-request'; + +export type RpcResponse = TResponse; + +export type RpcResponseTransformer = { + (response: RpcResponse, request: RpcRequest): RpcResponse; +}; + interface IHasIdentifier { readonly id: number; } diff --git a/packages/rpc-spec/README.md b/packages/rpc-spec/README.md index 40695b85b7d..ce76acf5123 100644 --- a/packages/rpc-spec/README.md +++ b/packages/rpc-spec/README.md @@ -41,25 +41,6 @@ Calling the `send(options)` method on a `PendingRpcRequest` will trigger the req An object that exposes all of the functions described by `TRpcMethods`, and fulfils them using `TRpcTransport`. Calling each method returns a `PendingRpcRequest` where `TResponse` is that method's response type. -### `RpcRequest` - -An object that describes the elements of a JSON RPC request. It consists of the following properties: - -- `methodName`: The name of the JSON RPC method to be called. -- `params`: The parameters to be passed to the JSON RPC method. - -### `RpcRequestTransformer` - -A function that accepts an `RpcRequest` and returns another `RpcRequest`. This allows the `RpcApi` to transform the request before it is sent to the JSON RPC server. - -### `RpcResponse` - -A type that represents the response from a JSON RPC server. This could be any sort of data which is why `RpcResponse` defaults to `unknown`. You may use a type parameter to specify the shape of the response — e.g. `RpcResponse<{ result: number }>`. - -### `RpcResponseTransformer` - -A function that accepts an `RpcResponse` and returns another `RpcResponse`. This allows the `RpcApi` to transform the response before it is returned to the caller. - ### `RpcApi` For each of `TRpcMethods` this object exposes a method with the same name that maps between its input arguments and a `RpcApiRequestPlan` that describes how to prepare a JSON RPC request to fetch `TResponse`. diff --git a/packages/rpc-spec/src/__tests__/rpc-api-test.ts b/packages/rpc-spec/src/__tests__/rpc-api-test.ts index 43d2f0874c3..3be092b1560 100644 --- a/packages/rpc-spec/src/__tests__/rpc-api-test.ts +++ b/packages/rpc-spec/src/__tests__/rpc-api-test.ts @@ -1,7 +1,8 @@ import '@solana/test-matchers/toBeFrozenObject'; +import type { RpcRequest, RpcResponse } from '@solana/rpc-spec-types'; + import { createJsonRpcApi } from '../rpc-api'; -import { RpcRequest, RpcResponse } from '../rpc-shared'; type DummyApi = { someMethod(...args: unknown[]): unknown; diff --git a/packages/rpc-spec/src/index.ts b/packages/rpc-spec/src/index.ts index dec5b1a2316..70976a6f746 100644 --- a/packages/rpc-spec/src/index.ts +++ b/packages/rpc-spec/src/index.ts @@ -1,4 +1,3 @@ export * from './rpc'; export * from './rpc-api'; -export * from './rpc-shared'; export * from './rpc-transport'; diff --git a/packages/rpc-spec/src/rpc-api.ts b/packages/rpc-spec/src/rpc-api.ts index cce38ee7c36..92ac2f75992 100644 --- a/packages/rpc-spec/src/rpc-api.ts +++ b/packages/rpc-spec/src/rpc-api.ts @@ -1,6 +1,10 @@ -import { Callable, createRpcMessage } from '@solana/rpc-spec-types'; - -import { RpcRequestTransformer, RpcResponse, RpcResponseTransformer } from './rpc-shared'; +import { + Callable, + createRpcMessage, + RpcRequestTransformer, + RpcResponse, + RpcResponseTransformer, +} from '@solana/rpc-spec-types'; export type RpcApiConfig = Readonly<{ requestTransformer?: RpcRequestTransformer; diff --git a/packages/rpc-spec/src/rpc-transport.ts b/packages/rpc-spec/src/rpc-transport.ts index 04b2b5eda08..ed7ff6deb02 100644 --- a/packages/rpc-spec/src/rpc-transport.ts +++ b/packages/rpc-spec/src/rpc-transport.ts @@ -1,4 +1,4 @@ -import { RpcResponse } from './rpc-shared'; +import { RpcResponse } from '@solana/rpc-spec-types'; type RpcTransportRequest = Readonly<{ payload: unknown;