Skip to content

Commit

Permalink
Move RpcRequest and RpcResponse types to rpc-spec-types
Browse files Browse the repository at this point in the history
  • Loading branch information
lorisleiva committed Oct 18, 2024
1 parent 601d031 commit f1cee41
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 33 deletions.
6 changes: 6 additions & 0 deletions .changeset/giant-seals-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@solana/rpc-spec-types': patch
'@solana/rpc-spec': patch
---

Move RpcRequest and RpcResponse types to rpc-spec-types
23 changes: 22 additions & 1 deletion packages/rpc-spec-types/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
1 change: 1 addition & 0 deletions packages/rpc-spec-types/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './overloads';
export * from './rpc-message';
export * from './rpc-request';
export * from './rpc-response';
export * from './type-helpers';
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@ export type RpcRequest<TParams = unknown> = {
readonly params: TParams;
};

export type RpcResponse<TResponse = unknown> = TResponse;

export type RpcRequestTransformer = {
<TParams>(request: RpcRequest<TParams>): RpcRequest;
};

export type RpcResponseTransformer<TResponse = unknown> = {
(response: RpcResponse, request: RpcRequest): RpcResponse<TResponse>;
};
8 changes: 8 additions & 0 deletions packages/rpc-spec-types/src/rpc-response.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
import type { RpcRequest } from './rpc-request';

export type RpcResponse<TResponse = unknown> = TResponse;

export type RpcResponseTransformer<TResponse = unknown> = {
(response: RpcResponse, request: RpcRequest): RpcResponse<TResponse>;
};

interface IHasIdentifier {
readonly id: number;
}
Expand Down
19 changes: 0 additions & 19 deletions packages/rpc-spec/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<TResponse>` 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<TRpcMethods>`

For each of `TRpcMethods` this object exposes a method with the same name that maps between its input arguments and a `RpcApiRequestPlan<TResponse>` that describes how to prepare a JSON RPC request to fetch `TResponse`.
Expand Down
3 changes: 2 additions & 1 deletion packages/rpc-spec/src/__tests__/rpc-api-test.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
1 change: 0 additions & 1 deletion packages/rpc-spec/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './rpc';
export * from './rpc-api';
export * from './rpc-shared';
export * from './rpc-transport';
10 changes: 7 additions & 3 deletions packages/rpc-spec/src/rpc-api.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion packages/rpc-spec/src/rpc-transport.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RpcResponse } from './rpc-shared';
import { RpcResponse } from '@solana/rpc-spec-types';

type RpcTransportRequest = Readonly<{
payload: unknown;
Expand Down
1 change: 1 addition & 0 deletions packages/rpc-transport-http/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"dependencies": {
"@solana/errors": "workspace:*",
"@solana/rpc-spec": "workspace:*",
"@solana/rpc-spec-types": "workspace:*",
"undici-types": "^6.20.0"
},
"devDependencies": {
Expand Down
3 changes: 2 additions & 1 deletion packages/rpc-transport-http/src/http-transport.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { SOLANA_ERROR__RPC__TRANSPORT_HTTP_ERROR, SolanaError } from '@solana/errors';
import { RpcResponse, RpcTransport } from '@solana/rpc-spec';
import type { RpcTransport } from '@solana/rpc-spec';
import type { RpcResponse } from '@solana/rpc-spec-types';
import type Dispatcher from 'undici-types/dispatcher';

import {
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f1cee41

Please sign in to comment.