viem 0.0.0-main.20240421T031739 → 0.0.0-main.20240421T221437

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/_cjs/actions/public/watchBlockNumber.js +20 -2
  3. package/_cjs/actions/public/watchBlockNumber.js.map +1 -1
  4. package/_cjs/actions/public/watchBlocks.js +20 -2
  5. package/_cjs/actions/public/watchBlocks.js.map +1 -1
  6. package/_cjs/actions/public/watchContractEvent.js +22 -4
  7. package/_cjs/actions/public/watchContractEvent.js.map +1 -1
  8. package/_cjs/actions/public/watchEvent.js +22 -4
  9. package/_cjs/actions/public/watchEvent.js.map +1 -1
  10. package/_cjs/clients/transports/fallback.js +2 -2
  11. package/_cjs/clients/transports/fallback.js.map +1 -1
  12. package/_cjs/errors/version.js +1 -1
  13. package/_esm/actions/public/watchBlockNumber.js +20 -2
  14. package/_esm/actions/public/watchBlockNumber.js.map +1 -1
  15. package/_esm/actions/public/watchBlocks.js +20 -2
  16. package/_esm/actions/public/watchBlocks.js.map +1 -1
  17. package/_esm/actions/public/watchContractEvent.js +22 -4
  18. package/_esm/actions/public/watchContractEvent.js.map +1 -1
  19. package/_esm/actions/public/watchEvent.js +22 -4
  20. package/_esm/actions/public/watchEvent.js.map +1 -1
  21. package/_esm/clients/transports/fallback.js +2 -2
  22. package/_esm/clients/transports/fallback.js.map +1 -1
  23. package/_esm/errors/version.js +1 -1
  24. package/_types/actions/public/watchBlockNumber.d.ts +2 -2
  25. package/_types/actions/public/watchBlockNumber.d.ts.map +1 -1
  26. package/_types/actions/public/watchBlocks.d.ts +2 -2
  27. package/_types/actions/public/watchBlocks.d.ts.map +1 -1
  28. package/_types/actions/public/watchContractEvent.d.ts.map +1 -1
  29. package/_types/actions/public/watchEvent.d.ts.map +1 -1
  30. package/_types/clients/transports/fallback.d.ts +5 -3
  31. package/_types/clients/transports/fallback.d.ts.map +1 -1
  32. package/_types/errors/version.d.ts +1 -1
  33. package/_types/types/transport.d.ts +6 -1
  34. package/_types/types/transport.d.ts.map +1 -1
  35. package/_types/types/utils.d.ts +11 -0
  36. package/_types/types/utils.d.ts.map +1 -1
  37. package/actions/public/watchBlockNumber.ts +36 -17
  38. package/actions/public/watchBlocks.ts +25 -5
  39. package/actions/public/watchContractEvent.ts +66 -48
  40. package/actions/public/watchEvent.ts +24 -5
  41. package/clients/transports/fallback.ts +16 -13
  42. package/errors/version.ts +1 -1
  43. package/package.json +1 -1
  44. package/types/transport.ts +16 -1
  45. package/types/utils.ts +16 -0
@@ -80,20 +80,23 @@ export type FallbackTransportConfig = {
80
80
  retryDelay?: TransportConfig['retryDelay'] | undefined
81
81
  }
82
82
 
83
- export type FallbackTransport = Transport<
84
- 'fallback',
85
- {
86
- onResponse: (fn: OnResponseFn) => void
87
- transports: ReturnType<Transport>[]
88
- }
89
- >
83
+ export type FallbackTransport<transports extends Transport[] = Transport[]> =
84
+ Transport<
85
+ 'fallback',
86
+ {
87
+ onResponse: (fn: OnResponseFn) => void
88
+ transports: {
89
+ [key in keyof transports]: ReturnType<transports[key]>
90
+ }
91
+ }
92
+ >
90
93
 
91
94
  export type FallbackTransportErrorType = CreateTransportErrorType | ErrorType
92
95
 
93
- export function fallback(
94
- transports_: Transport[],
96
+ export function fallback<const transports extends Transport[]>(
97
+ transports_: transports,
95
98
  config: FallbackTransportConfig = {},
96
- ): FallbackTransport {
99
+ ): FallbackTransport<transports> {
97
100
  const {
98
101
  key = 'fallback',
99
102
  name = 'Fallback',
@@ -101,7 +104,7 @@ export function fallback(
101
104
  retryCount,
102
105
  retryDelay,
103
106
  } = config
104
- return ({ chain, pollingInterval = 4_000, timeout, ...rest }) => {
107
+ return (({ chain, pollingInterval = 4_000, timeout, ...rest }) => {
105
108
  let transports = transports_
106
109
 
107
110
  let onResponse: OnResponseFn = () => {}
@@ -168,7 +171,7 @@ export function fallback(
168
171
  rankTransports({
169
172
  chain,
170
173
  interval: rankOptions.interval ?? pollingInterval,
171
- onTransports: (transports_) => (transports = transports_),
174
+ onTransports: (transports_) => (transports = transports_ as transports),
172
175
  sampleCount: rankOptions.sampleCount,
173
176
  timeout: rankOptions.timeout,
174
177
  transports,
@@ -176,7 +179,7 @@ export function fallback(
176
179
  })
177
180
  }
178
181
  return transport
179
- }
182
+ }) as FallbackTransport<transports>
180
183
  }
181
184
 
182
185
  function shouldThrow(error: Error) {
package/errors/version.ts CHANGED
@@ -1 +1 @@
1
- export const version = '0.0.0-main.20240421T031739'
1
+ export const version = '0.0.0-main.20240421T221437'
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "viem",
3
3
  "description": "TypeScript Interface for Ethereum",
4
- "version": "0.0.0-main.20240421T031739",
4
+ "version": "0.0.0-main.20240421T221437",
5
5
  "type": "module",
6
6
  "main": "./_cjs/index.js",
7
7
  "module": "./_esm/index.js",
@@ -1,10 +1,12 @@
1
1
  import type { Transport } from '../clients/transports/createTransport.js'
2
+ import type { FallbackTransport } from '../clients/transports/fallback.js'
3
+ import type { Some } from './utils.js'
2
4
 
3
5
  export type GetTransportConfig<TTransport extends Transport> =
4
6
  ReturnType<TTransport>['config']
5
7
 
6
8
  export type GetPollOptions<transport extends Transport> =
7
- | (GetTransportConfig<transport>['type'] extends 'webSocket'
9
+ | (HasTransportType<transport, 'webSocket'> extends true
8
10
  ? {
9
11
  batch?: undefined
10
12
  /**
@@ -28,3 +30,16 @@ export type GetPollOptions<transport extends Transport> =
28
30
  */
29
31
  pollingInterval?: number | undefined
30
32
  }
33
+
34
+ export type HasTransportType<transport extends Transport, type extends string> =
35
+ | (GetTransportConfig<transport>['type'] extends type ? true : never)
36
+ | (transport extends FallbackTransport<infer transports extends Transport[]>
37
+ ? Some<
38
+ {
39
+ [key in keyof transports]: GetTransportConfig<
40
+ transports[key]
41
+ >['type']
42
+ },
43
+ type
44
+ >
45
+ : false)
package/types/utils.ts CHANGED
@@ -191,6 +191,22 @@ export type Prettify<T> = {
191
191
  export type RequiredBy<T, K extends keyof T> = Omit<T, K> &
192
192
  ExactRequired<Pick<T, K>>
193
193
 
194
+ /**
195
+ * @description Returns truthy if `array` contains `value`.
196
+ *
197
+ * @example
198
+ * Some<[1, 2, 3], 2>
199
+ * => true
200
+ */
201
+ export type Some<array extends unknown[], value> = array extends [
202
+ value,
203
+ ...unknown[],
204
+ ]
205
+ ? true
206
+ : array extends [unknown, ...infer rest]
207
+ ? Some<rest, value>
208
+ : false
209
+
194
210
  /**
195
211
  * @description Creates a type that extracts the values of T.
196
212
  *