-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathoutbound.ts
159 lines (144 loc) · 4.27 KB
/
outbound.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import { DfuseClientError } from "../types/error"
export type OutboundMessage<T = unknown> = {
type: OutboundMessageType
req_id: string
listen?: boolean
fetch?: boolean
start_block?: number
with_progress?: number
data: T
}
// **Important** The key must be the same as the API type but in upper snake case for "in" operation to work
export enum OutboundMessageType {
GET_ACTION_TRACES = "get_action_traces",
GET_TABLE_ROWS = "get_table_rows",
GET_TRANSACTION_LIFECYCLE = "get_transaction_lifecycle",
GET_HEAD_INFO = "get_head_info",
UNLISTEN = "unlisten",
}
export type StreamOptions = {
req_id?: string
fetch?: boolean
listen?: boolean
start_block?: number
with_progress?: number
irreversible_only?: boolean
}
export type GetActionTracesMessageData = {
accounts: string
receivers?: string
action_names?: string
with_dbops?: boolean
with_dtrxops?: boolean
with_ramops?: boolean
with_tableops?: boolean
with_inline_traces?: boolean
}
export function getActionTracesMessage(
data: GetActionTracesMessageData,
streamOptions: StreamOptions = {}
): OutboundMessage<GetActionTracesMessageData> {
return createOutboundMessage(OutboundMessageType.GET_ACTION_TRACES, data, {
listen: true,
...streamOptions,
})
}
export type GetTableRowsMessageData = {
code: string
scope: string
table: string
json?: boolean
lower_bound?: string
upper_bound?: string
}
/**
* @deprecated The message factories are deprecated, there is no need to create your
* own message anynore. This will be removed in a future release. The standard
* client does not use this anymore.
*/
export function getTableRowsMessage(
data: GetTableRowsMessageData,
streamOptions: StreamOptions = {}
): OutboundMessage<GetTableRowsMessageData> {
return createOutboundMessage(OutboundMessageType.GET_TABLE_ROWS, data, {
listen: true,
...streamOptions,
})
}
export type GetTransactionLifecycleMessageData = {
id: string
}
/**
* @deprecated The message factories are deprecated, there is no need to create your
* own message anynore. This will be removed in a future release. The standard
* client does not use this anymore.
*/
export function getTransactionLifecycleMessage(
data: GetTransactionLifecycleMessageData,
streamOptions: StreamOptions = {}
): OutboundMessage<GetTransactionLifecycleMessageData> {
return createOutboundMessage(OutboundMessageType.GET_TRANSACTION_LIFECYCLE, data, {
listen: true,
fetch: true,
...streamOptions,
})
}
/**
* @deprecated The message factories are deprecated, there is no need to create your
* own message anynore. This will be removed in a future release. The standard
* client does not use this anymore.
*/
export function getHeadInfoMessage(streamOptions: StreamOptions = {}): OutboundMessage {
return createOutboundMessage(
OutboundMessageType.GET_HEAD_INFO,
{},
{ listen: true, ...streamOptions }
)
}
export type UnlistenMessageData = {
req_id: string
}
/**
* @deprecated The message factories are deprecated, there is no need to create your
* own message anymore. This will be removed in a future release. The standard
* client does not use this anymore.
*/
export function unlistenMessage(data: UnlistenMessageData): OutboundMessage<UnlistenMessageData> {
return {
req_id: data.req_id,
type: OutboundMessageType.UNLISTEN,
data,
}
}
export type OutboundMessageFactory<T> = (
createOutboundMessage: (
type: OutboundMessageType,
data: T,
userOptions: StreamOptions
) => OutboundMessage<T>,
withDefaultOptions: (userOptions: StreamOptions) => StreamOptions
) => OutboundMessage<T>
/**
* Exported for consumption from internal packages. This does **not**
* have any **Backward compatibility** policy nor documentation attached
* to it.
*
* It will be moved and made private again when message factories
* above have been removed.
*/
export function createOutboundMessage<T>(
type: OutboundMessageType,
data: T,
options: StreamOptions
): OutboundMessage<T> {
const req_id = options.req_id
if (req_id === undefined) {
throw new DfuseClientError("All outbound message should have a 'req_id' value")
}
return {
type,
req_id,
data,
...options,
}
}