-
Notifications
You must be signed in to change notification settings - Fork 911
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new
RpcRequest
, RpcResponse
types and their transformer types
- Loading branch information
1 parent
8e0c480
commit 925398e
Showing
5 changed files
with
138 additions
and
1 deletion.
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,5 @@ | ||
--- | ||
'@solana/rpc-spec': patch | ||
--- | ||
|
||
Add new `RpcRequest` and `RpcResponse` types with `RpcRequestTransformer`, `RpcResponseTransformer` and `createJsonRpcResponseTransformer` functions |
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,56 @@ | ||
import '@solana/test-matchers/toBeFrozenObject'; | ||
|
||
import { createJsonRpcResponseTransformer, RpcRequest, RpcResponse } from '../rpc-shared'; | ||
|
||
describe('createJsonRpcResponseTransformer', () => { | ||
it('can alter the value of the json Promise', async () => { | ||
expect.assertions(1); | ||
|
||
// Given a request and a response that returns a number. | ||
const request = { methodName: 'someMethod', params: [123] }; | ||
const response = { | ||
json: () => Promise.resolve(123), | ||
text: () => Promise.resolve('123'), | ||
}; | ||
|
||
// When we create a JSON transformer that doubles the number. | ||
const transformer = createJsonRpcResponseTransformer((json: unknown) => (json as number) * 2); | ||
|
||
// Then the transformed response should return the doubled number. | ||
const transformedResponse = transformer(response, request); | ||
transformedResponse satisfies RpcResponse<number>; | ||
await expect(transformedResponse.json()).resolves.toBe(246); | ||
}); | ||
|
||
it('does not alter the value of the text Promise', async () => { | ||
expect.assertions(1); | ||
|
||
// Given a request and a response that returns a number. | ||
const request = { methodName: 'someMethod', params: [123] }; | ||
const response = { | ||
json: () => Promise.resolve(123), | ||
text: () => Promise.resolve('123'), | ||
}; | ||
|
||
// When we create a JSON transformer that doubles the number. | ||
const transformer = createJsonRpcResponseTransformer((json: unknown) => (json as number) * 2); | ||
|
||
// Then the text should function should return the original string. | ||
const transformedResponse = transformer(response, request); | ||
await expect(transformedResponse.text()).resolves.toBe('123'); | ||
}); | ||
|
||
it('returns a frozen object as the Reponse', () => { | ||
// Given any response. | ||
const response = { | ||
json: () => Promise.resolve(123), | ||
text: () => Promise.resolve('123'), | ||
}; | ||
|
||
// When we pass it through a JSON transformer. | ||
const transformedResponse = createJsonRpcResponseTransformer(x => x)(response, {} as RpcRequest); | ||
|
||
// Then we expect the transformed response to be frozen. | ||
expect(transformedResponse).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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from './rpc'; | ||
export * from './rpc-api'; | ||
export * from './rpc-request'; | ||
export * from './rpc-shared'; | ||
export * from './rpc-transport'; |
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,35 @@ | ||
export type RpcRequest<TParams = unknown> = { | ||
readonly methodName: string; | ||
readonly params: TParams; | ||
}; | ||
|
||
export type RpcResponse<TResponse = unknown> = { | ||
readonly json: () => Promise<TResponse>; | ||
readonly text: () => Promise<string>; | ||
}; | ||
|
||
export type RpcRequestTransformer = { | ||
<TParams>(request: RpcRequest<unknown>): RpcRequest<TParams>; | ||
}; | ||
|
||
export type RpcResponseTransformer = { | ||
<TResponse>(response: RpcResponse<unknown>, request: RpcRequest<unknown>): RpcResponse<TResponse>; | ||
}; | ||
|
||
export type RpcResponseTransformerFor<TResponse> = { | ||
(response: RpcResponse<unknown>, request: RpcRequest<unknown>): RpcResponse<TResponse>; | ||
}; | ||
|
||
export function createJsonRpcResponseTransformer<TResponse>( | ||
jsonTransformer: (json: unknown, request: RpcRequest) => TResponse, | ||
): RpcResponseTransformerFor<TResponse> { | ||
return function (response: RpcResponse, request: RpcRequest): RpcResponse<TResponse> { | ||
return Object.freeze({ | ||
...response, | ||
json: async () => { | ||
const json = await response.json(); | ||
return jsonTransformer(json, request); | ||
}, | ||
}); | ||
}; | ||
} |