-
Notifications
You must be signed in to change notification settings - Fork 915
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract
isJsonRpcPayload
method to rpc-spec
- Loading branch information
1 parent
42cece0
commit 0d92cd7
Showing
6 changed files
with
78 additions
and
13 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,5 @@ | ||
--- | ||
'@solana/rpc-spec': patch | ||
--- | ||
|
||
Add new `isJsonRpcPayload` helper method |
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,29 @@ | ||
import { isJsonRpcPayload } from '../rpc-transport'; | ||
|
||
describe('isJsonRpcPayload', () => { | ||
it('recognizes JSON RPC payloads', () => { | ||
expect(isJsonRpcPayload({ jsonrpc: '2.0', method: 'getFoo', params: [123] })).toBe(true); | ||
}); | ||
it('returns false if the payload is not an object', () => { | ||
expect(isJsonRpcPayload(undefined)).toBe(false); | ||
expect(isJsonRpcPayload(null)).toBe(false); | ||
expect(isJsonRpcPayload(true)).toBe(false); | ||
expect(isJsonRpcPayload(false)).toBe(false); | ||
expect(isJsonRpcPayload([])).toBe(false); | ||
expect(isJsonRpcPayload('o hai')).toBe(false); | ||
expect(isJsonRpcPayload(123)).toBe(false); | ||
expect(isJsonRpcPayload(123n)).toBe(false); | ||
}); | ||
it('returns false if the payload is an empty object', () => { | ||
expect(isJsonRpcPayload({})).toBe(false); | ||
}); | ||
it('returns false if the payload is not a JSON RPC v2', () => { | ||
expect(isJsonRpcPayload({ jsonrpc: '42.0', method: 'getFoo', params: [123] })).toBe(false); | ||
}); | ||
it('returns false if the method name is missing', () => { | ||
expect(isJsonRpcPayload({ jsonrpc: '2.0', params: [123] })).toBe(false); | ||
}); | ||
it('returns false if the parameters are missing', () => { | ||
expect(isJsonRpcPayload({ jsonrpc: '2.0', method: 'getFoo' })).toBe(false); | ||
}); | ||
}); |
9 changes: 9 additions & 0 deletions
9
packages/rpc-spec/src/__typetests__/rpc-transport-typetest.ts
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,9 @@ | ||
import { isJsonRpcPayload } from '../rpc-transport'; | ||
|
||
{ | ||
// [isJsonRpcPayload]: It narrows the type of the payload to a JSON RPC payload. | ||
const payload = {} as unknown; | ||
if (isJsonRpcPayload(payload)) { | ||
payload satisfies { jsonrpc: '2.0'; method: string; params: unknown }; | ||
} | ||
} |
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