-
Notifications
You must be signed in to change notification settings - Fork 910
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update RPC APIs to use new types (#3150)
This PR updates the RPC Api layer such that they use the new `RpcRequest`, `RpcResponse`, `RpcRequestTransformer` and `RpcResponseTransformer` types. Note that I had to duplicate the implementation of `getDefaultResponseTransformerForSolanaRpc` into `getDefaultResponseTransformerForSolanaRpcSubscriptions` in order to leave RPC Subscription packages unaffected by the refactoring. These will have their own dedicated PR stack further down the line.
- Loading branch information
1 parent
7b9025b
commit a705413
Showing
12 changed files
with
271 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
'@solana/rpc-subscriptions-api': patch | ||
'@solana/rpc-transformers': patch | ||
'@solana/rpc-spec': patch | ||
'@solana/rpc-api': patch | ||
--- | ||
|
||
Make `RpcApi` use new `RpcRequestTransformer` and `RpcResponseTransformer` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import '@solana/test-matchers/toBeFrozenObject'; | ||
|
||
import { createRpcApi } from '../rpc-api'; | ||
import { RpcRequest, RpcResponse } from '../rpc-shared'; | ||
|
||
type DummyApi = { | ||
someMethod(...args: unknown[]): unknown; | ||
}; | ||
|
||
describe('createRpcApi', () => { | ||
it('returns a plan containing the method name and parameters provided', () => { | ||
// Given a dummy API. | ||
const api = createRpcApi<DummyApi>(); | ||
|
||
// When we call a method on the API. | ||
const plan = api.someMethod(1, 'two', { three: [4] }); | ||
|
||
// Then we expect the plan to contain the method name and the provided parameters. | ||
expect(plan).toEqual({ | ||
methodName: 'someMethod', | ||
params: [1, 'two', { three: [4] }], | ||
}); | ||
}); | ||
it('applies the request transformer to the provided method name', () => { | ||
// Given a dummy API with a request transformer that appends 'Transformed' to the method name. | ||
const api = createRpcApi<DummyApi>({ | ||
requestTransformer: <T>(request: RpcRequest<unknown>) => | ||
({ ...request, methodName: `${request.methodName}Transformed` }) as RpcRequest<T>, | ||
}); | ||
|
||
// When we call a method on the API. | ||
const plan = api.someMethod(); | ||
|
||
// Then we expect the plan to contain the transformed method name. | ||
expect(plan.methodName).toBe('someMethodTransformed'); | ||
}); | ||
it('applies the request transformer to the provided params', () => { | ||
// Given a dummy API with a request transformer that doubles the provided params. | ||
const api = createRpcApi<DummyApi>({ | ||
requestTransformer: <T>(request: RpcRequest<unknown>) => | ||
({ ...request, params: (request.params as number[]).map(x => x * 2) }) as RpcRequest<T>, | ||
}); | ||
|
||
// When we call a method on the API. | ||
const plan = api.someMethod(1, 2, 3); | ||
|
||
// Then we expect the plan to contain the transformed params. | ||
expect(plan.params).toEqual([2, 4, 6]); | ||
}); | ||
it('includes the provided response transformer in the plan', () => { | ||
// Given a dummy API with a response transformer. | ||
const responseTransformer = <T>(response: RpcResponse<unknown>) => response as RpcResponse<T>; | ||
const api = createRpcApi<DummyApi>({ responseTransformer }); | ||
|
||
// When we call a method on the API. | ||
const plan = api.someMethod(1, 2, 3); | ||
|
||
// Then we expect the plan to contain the response transformer. | ||
expect(plan.responseTransformer).toBe(responseTransformer); | ||
}); | ||
it('returns a frozen object', () => { | ||
// Given a dummy API. | ||
const api = createRpcApi<DummyApi>(); | ||
|
||
// When we call a method on the API. | ||
const plan = api.someMethod(); | ||
|
||
// Then we expect the returned plan to be frozen. | ||
expect(plan).toBeFrozenObject(); | ||
}); | ||
it('also returns a frozen object with a request transformer', () => { | ||
// Given a dummy API with a request transformer. | ||
const api = createRpcApi<DummyApi>({ | ||
requestTransformer: <T>(request: RpcRequest<unknown>) => | ||
({ ...request, methodName: 'transformed' }) as RpcRequest<T>, | ||
}); | ||
|
||
// When we call a method on the API. | ||
const plan = api.someMethod(); | ||
|
||
// Then we expect the returned plan to be frozen. | ||
expect(plan).toBeFrozenObject(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.