Skip to content

Commit

Permalink
chore: tweak RpcTransport
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed Oct 30, 2024
1 parent c6c52c8 commit 6959a7f
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 31 deletions.
20 changes: 20 additions & 0 deletions src/internal/Provider/from.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,26 @@ import { address } from '../../../test/constants/addresses.js'
test('default', async () => {
const store = RpcRequest.createStore()

const provider = Provider.from({
async request(args) {
return await fetch(anvilMainnet.rpcUrl, {
body: JSON.stringify(store.prepare(args)),
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
}).then((res) => res.json())
},
})

const blockNumber = await provider.request({ method: 'eth_blockNumber' })

expect(blockNumber).toMatchInlineSnapshot(`"0x12f2974"`)
})

test('behavior: pre-parsed', async () => {
const store = RpcRequest.createStore()

const provider = Provider.from({
async request(args) {
return await fetch(anvilMainnet.rpcUrl, {
Expand Down
20 changes: 17 additions & 3 deletions src/internal/Provider/from.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type * as Errors from '../../Errors.js'
import { RpcResponse_parse } from '../RpcResponse/parse.js'
import { Provider_IsUndefinedError } from './errors.js'
import type { Provider } from './types.js'

Expand Down Expand Up @@ -116,11 +117,24 @@ import type { Provider } from './types.js'
* @param provider - The EIP-1193 provider to convert.
* @returns An typed EIP-1193 Provider.
*/
export function Provider_from<provider extends Provider | unknown>(
export function Provider_from<const provider extends Provider | unknown>(
provider: provider | Provider,
): provider extends Provider ? provider : Provider {
): Provider
export function Provider_from(provider: Provider): Provider {

Check failure on line 123 in src/internal/Provider/from.ts

View workflow job for this annotation

GitHub Actions / Verify / Checks

Missing JSDoc comment
if (!provider) throw new Provider_IsUndefinedError()
return provider as never
return {
on: provider.on?.bind(provider),
removeListener: provider.removeListener?.bind(provider),
async request(args) {
const result = await provider.request(args)
if (
typeof result === 'object' &&
'jsonrpc' in (result as { jsonrpc?: unknown })
)
return RpcResponse_parse(result) as never
return result
},
}
}

export declare namespace Provider_from {
Expand Down
19 changes: 16 additions & 3 deletions src/internal/RpcResponse/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,23 @@ import type { RpcResponse } from './types.js'
* @param options - Parsing options.
* @returns Typed JSON-RPC result, or response object (if `safe` is `true`).
*/
export function RpcResponse_parse<returnType, safe extends boolean = false>(
response: unknown,
export function RpcResponse_parse<
const response extends RpcResponse | unknown,
returnType,
safe extends boolean = false,
>(
response: response,
options: RpcResponse_parse.Options<returnType, safe> = {},
): RpcResponse_parse.ReturnType<returnType, safe> {
): RpcResponse_parse.ReturnType<
unknown extends response
? returnType
: response extends RpcResponse
? response extends { result: infer result }
? result
: never
: returnType,
safe
> {
const { safe = false } = options
const response_ = response as RpcResponse
if (safe) return response as never
Expand Down
4 changes: 2 additions & 2 deletions src/internal/RpcTransport/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ test('default', async () => {
`)
})

test('behavior: safe', async () => {
test('behavior: raw', async () => {
const transport = RpcTransport_create(
{
request: async () => {
Expand All @@ -30,7 +30,7 @@ test('behavior: safe', async () => {
}
},
},
{ safe: true },
{ raw: true },
)
const accounts = await transport.request({ method: 'eth_accounts' })
expect(accounts).toMatchInlineSnapshot(`
Expand Down
10 changes: 5 additions & 5 deletions src/internal/RpcTransport/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import type { RpcTransport, RpcTransport_Options } from './types.js'
/** @internal */
export function RpcTransport_create<
options extends Record<string, unknown> = {},
safe extends boolean = false,
raw extends boolean = false,
>(
transport: RpcTransport_create.Transport<options>,
options_root: RpcTransport_create.Options<safe> = {},
): RpcTransport<safe> {
options_root: RpcTransport_create.Options<raw> = {},
): RpcTransport<raw> {
const requestStore = RpcRequest_createStore()

return {
Expand All @@ -23,7 +23,7 @@ export function RpcTransport_create<
const data = await transport.request(body, options as never)

return RpcResponse_parse(data, {
safe: options.safe ?? options_root.safe,
safe: options.raw ?? options_root.raw,
}) as never
},
}
Expand All @@ -38,7 +38,7 @@ export declare namespace RpcTransport_create {
) => Promise<RpcResponse>
}

type Options<safe extends boolean = false> = RpcTransport_Options<safe>
type Options<raw extends boolean = false> = RpcTransport_Options<raw>

type ErrorType =
| RpcRequest_createStore.ErrorType
Expand Down
4 changes: 2 additions & 2 deletions src/internal/RpcTransport/fromHttp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ test('default', async () => {
`)
})

test('options: safe', async () => {
const transport = RpcTransport.fromHttp(anvilMainnet.rpcUrl, { safe: true })
test('options: raw', async () => {
const transport = RpcTransport.fromHttp(anvilMainnet.rpcUrl, { raw: true })

const blockNumber = await transport.request({ method: 'eth_accounts' })

Expand Down
6 changes: 3 additions & 3 deletions src/internal/RpcTransport/fromHttp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@ export function RpcTransport_fromHttp<safe extends boolean = false>(
return data as never
},
},
{ safe: options.safe },
{ raw: options.raw },
)
}

export declare namespace RpcTransport_fromHttp {
type Options<safe extends boolean = false> = RpcTransport_Options<
safe,
type Options<raw extends boolean = false> = RpcTransport_Options<
raw,
RpcTransport_HttpOptions
>

Expand Down
26 changes: 13 additions & 13 deletions src/internal/RpcTransport/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import type { Compute } from '../types.js'

/** Root type for an RPC Transport. */
export type RpcTransport<
safe extends boolean = false,
raw extends boolean = false,
options extends Record<string, unknown> = {},
> = Compute<{
request: RpcTransport_RequestFn<safe, options>
request: RpcTransport_RequestFn<raw, options>
}>

/** HTTP-based RPC Transport. */
export type RpcTransport_Http<safe extends boolean = false> = RpcTransport<
safe,
export type RpcTransport_Http<raw extends boolean = false> = RpcTransport<
raw,
RpcTransport_HttpOptions
>

Expand All @@ -26,18 +26,18 @@ export type RpcTransport_Http<safe extends boolean = false> = RpcTransport<
////////////////////////////////////////////////////////////////////////

export type RpcTransport_Options<
safe extends boolean | undefined = undefined,
raw extends boolean | undefined = undefined,
options extends Record<string, unknown> = {},
> = {
/**
* Enables safe mode – responses will return an object with `result` and `error` properties instead of returning the `result` directly and throwing errors.
* Enables raw mode – responses will return an object with `result` and `error` properties instead of returning the `result` directly and throwing errors.
*
* - `true`: a JSON-RPC response object will be returned with `result` and `error` properties.
* - `false`: the JSON-RPC response object's `result` property will be returned directly, and JSON-RPC Errors will be thrown.
*
* @default false
*/
safe?: safe | boolean | undefined
raw?: raw | boolean | undefined
} & options

export type RpcTransport_HttpOptions = {
Expand All @@ -59,20 +59,20 @@ export type RpcTransport_HttpOptions = {
////////////////////////////////////////////////////////////////////////

export type RpcTransport_RequestFn<
safe extends boolean = false,
raw extends boolean = false,
options extends Record<string, unknown> = {},
> = <
schema extends RpcSchema_Generic | RpcSchema_MethodNameGeneric,
safe_override extends boolean | undefined = undefined,
raw_override extends boolean | undefined = undefined,
>(
parameters: Compute<RpcSchema_ExtractRequest<schema>>,
options?: RpcTransport_Options<safe_override, options> | undefined,
options?: RpcTransport_Options<raw_override, options> | undefined,
) => Promise<
safe_override extends boolean
? safe_override extends true
raw_override extends boolean
? raw_override extends true
? RpcResponse<RpcSchema_ExtractReturnType<schema>>
: RpcSchema_ExtractReturnType<schema>
: safe extends true
: raw extends true
? RpcResponse<RpcSchema_ExtractReturnType<schema>>
: RpcSchema_ExtractReturnType<schema>
>

0 comments on commit 6959a7f

Please sign in to comment.