@0xsequence/indexer 1.9.24 → 1.9.25
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.
- package/dist/0xsequence-indexer.cjs.dev.js +524 -53
- package/dist/0xsequence-indexer.cjs.prod.js +524 -53
- package/dist/0xsequence-indexer.esm.js +499 -54
- package/dist/declarations/src/indexer.gen.d.ts +219 -64
- package/package.json +1 -1
- package/src/indexer.gen.ts +986 -218
package/src/indexer.gen.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/* eslint-disable */
|
|
2
|
-
// sequence-indexer v0.4.0
|
|
2
|
+
// sequence-indexer v0.4.0 d33d29bc84a4439a42a4e1533035bc80c4ea226e
|
|
3
3
|
// --
|
|
4
|
-
// Code generated by webrpc-gen@v0.
|
|
4
|
+
// Code generated by webrpc-gen@v0.18.6 with typescript generator. DO NOT EDIT.
|
|
5
5
|
//
|
|
6
|
-
// webrpc-gen -schema=indexer.ridl -target=typescript
|
|
6
|
+
// webrpc-gen -schema=indexer.ridl -target=typescript -client -out=./clients/indexer.gen.ts
|
|
7
7
|
|
|
8
8
|
// WebRPC description and code-gen version
|
|
9
9
|
export const WebRPCVersion = 'v1'
|
|
@@ -12,7 +12,7 @@ export const WebRPCVersion = 'v1'
|
|
|
12
12
|
export const WebRPCSchemaVersion = 'v0.4.0'
|
|
13
13
|
|
|
14
14
|
// Schema hash generated from your RIDL schema
|
|
15
|
-
export const WebRPCSchemaHash = '
|
|
15
|
+
export const WebRPCSchemaHash = 'd33d29bc84a4439a42a4e1533035bc80c4ea226e'
|
|
16
16
|
|
|
17
17
|
//
|
|
18
18
|
// Types
|
|
@@ -29,7 +29,6 @@ export interface ContractInfo {
|
|
|
29
29
|
deployed: boolean
|
|
30
30
|
bytecodeHash: string
|
|
31
31
|
extensions: ContractInfoExtensions
|
|
32
|
-
|
|
33
32
|
updatedAt: string
|
|
34
33
|
}
|
|
35
34
|
|
|
@@ -46,21 +45,36 @@ export interface ContractInfoExtensions {
|
|
|
46
45
|
|
|
47
46
|
export interface TokenMetadata {
|
|
48
47
|
tokenId: string
|
|
49
|
-
contractAddress: string
|
|
50
48
|
name: string
|
|
51
|
-
description
|
|
52
|
-
image
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
audio: string
|
|
57
|
-
image_data: string
|
|
58
|
-
external_url: string
|
|
59
|
-
background_color: string
|
|
60
|
-
animation_url: string
|
|
49
|
+
description?: string
|
|
50
|
+
image?: string
|
|
51
|
+
video?: string
|
|
52
|
+
audio?: string
|
|
53
|
+
properties?: { [key: string]: any }
|
|
61
54
|
attributes: Array<{ [key: string]: any }>
|
|
62
|
-
|
|
55
|
+
image_data?: string
|
|
56
|
+
external_url?: string
|
|
57
|
+
background_color?: string
|
|
58
|
+
animation_url?: string
|
|
59
|
+
decimals?: number
|
|
60
|
+
updatedAt?: string
|
|
61
|
+
assets?: Array<Asset>
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface Asset {
|
|
65
|
+
id: number
|
|
66
|
+
collectionId: number
|
|
67
|
+
tokenId: string
|
|
68
|
+
url?: string
|
|
69
|
+
metadataField: string
|
|
70
|
+
name?: string
|
|
71
|
+
filesize?: number
|
|
72
|
+
mimeType?: string
|
|
73
|
+
width?: number
|
|
74
|
+
height?: number
|
|
75
|
+
updatedAt?: string
|
|
63
76
|
}
|
|
77
|
+
|
|
64
78
|
export enum ContractType {
|
|
65
79
|
UNKNOWN = 'UNKNOWN',
|
|
66
80
|
NATIVE = 'NATIVE',
|
|
@@ -73,36 +87,43 @@ export enum ContractType {
|
|
|
73
87
|
ERC1155_BRIDGE = 'ERC1155_BRIDGE',
|
|
74
88
|
SEQ_MARKETPLACE = 'SEQ_MARKETPLACE'
|
|
75
89
|
}
|
|
90
|
+
|
|
76
91
|
export enum EventLogType {
|
|
77
92
|
UNKNOWN = 'UNKNOWN',
|
|
78
93
|
BLOCK_ADDED = 'BLOCK_ADDED',
|
|
79
94
|
BLOCK_REMOVED = 'BLOCK_REMOVED'
|
|
80
95
|
}
|
|
96
|
+
|
|
81
97
|
export enum EventLogDataType {
|
|
82
98
|
UNKNOWN = 'UNKNOWN',
|
|
83
99
|
TOKEN_TRANSFER = 'TOKEN_TRANSFER',
|
|
84
100
|
NATIVE_TOKEN_TRANSFER = 'NATIVE_TOKEN_TRANSFER',
|
|
85
101
|
SEQUENCE_TXN = 'SEQUENCE_TXN'
|
|
86
102
|
}
|
|
103
|
+
|
|
87
104
|
export enum OrderStatus {
|
|
88
105
|
OPEN = 'OPEN',
|
|
89
106
|
CLOSED = 'CLOSED',
|
|
90
107
|
CANCELLED = 'CANCELLED'
|
|
91
108
|
}
|
|
109
|
+
|
|
92
110
|
export enum TxnTransferType {
|
|
93
111
|
UNKNOWN = 'UNKNOWN',
|
|
94
112
|
SEND = 'SEND',
|
|
95
113
|
RECEIVE = 'RECEIVE'
|
|
96
114
|
}
|
|
115
|
+
|
|
97
116
|
export enum TransactionStatus {
|
|
98
117
|
FAILED = 'FAILED',
|
|
99
118
|
SUCCESSFUL = 'SUCCESSFUL'
|
|
100
119
|
}
|
|
120
|
+
|
|
101
121
|
export enum TransactionType {
|
|
102
122
|
LegacyTxnType = 'LegacyTxnType',
|
|
103
123
|
AccessListTxnType = 'AccessListTxnType',
|
|
104
124
|
DynamicFeeTxnType = 'DynamicFeeTxnType'
|
|
105
125
|
}
|
|
126
|
+
|
|
106
127
|
export enum SortOrder {
|
|
107
128
|
DESC = 'DESC',
|
|
108
129
|
ASC = 'ASC'
|
|
@@ -148,7 +169,15 @@ export interface RuntimeChecks {
|
|
|
148
169
|
lastBlockNumWithState: number
|
|
149
170
|
bloomStatus: BloomStatus
|
|
150
171
|
bond: Bond
|
|
151
|
-
diskUsage:
|
|
172
|
+
diskUsage: DiskUsage
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export interface DiskUsage {
|
|
176
|
+
humanReadable: string
|
|
177
|
+
used: number
|
|
178
|
+
size: number
|
|
179
|
+
percent: number
|
|
180
|
+
dirs: { [key: string]: string }
|
|
152
181
|
}
|
|
153
182
|
|
|
154
183
|
export interface Bond {
|
|
@@ -219,7 +248,6 @@ export interface TokenBalance {
|
|
|
219
248
|
blockHash: string
|
|
220
249
|
blockNumber: number
|
|
221
250
|
chainId: number
|
|
222
|
-
|
|
223
251
|
contractInfo?: ContractInfo
|
|
224
252
|
tokenMetadata?: TokenMetadata
|
|
225
253
|
}
|
|
@@ -307,12 +335,13 @@ export interface TransactionHistoryFilter {
|
|
|
307
335
|
tokenID?: string
|
|
308
336
|
}
|
|
309
337
|
|
|
310
|
-
export interface
|
|
338
|
+
export interface TransactionFilter {
|
|
311
339
|
txnHash?: string
|
|
312
340
|
from?: string
|
|
313
341
|
to?: string
|
|
314
342
|
contractAddress?: string
|
|
315
|
-
|
|
343
|
+
event?: string
|
|
344
|
+
topicHash?: string
|
|
316
345
|
}
|
|
317
346
|
|
|
318
347
|
export interface TransactionReceipt {
|
|
@@ -357,11 +386,13 @@ export interface WebhookListener {
|
|
|
357
386
|
id: number
|
|
358
387
|
projectID: number
|
|
359
388
|
url: string
|
|
360
|
-
filters:
|
|
389
|
+
filters: EventFilter
|
|
361
390
|
updatedAt: string
|
|
362
391
|
}
|
|
363
392
|
|
|
364
|
-
export interface
|
|
393
|
+
export interface EventFilter {
|
|
394
|
+
events?: Array<string>
|
|
395
|
+
topicHashes?: Array<string>
|
|
365
396
|
contractAddresses?: Array<string>
|
|
366
397
|
tokenIDs?: Array<string>
|
|
367
398
|
accounts?: Array<string>
|
|
@@ -374,29 +405,52 @@ export interface MetadataOptions {
|
|
|
374
405
|
}
|
|
375
406
|
|
|
376
407
|
export interface Indexer {
|
|
377
|
-
ping(headers?: object): Promise<PingReturn>
|
|
378
|
-
version(headers?: object): Promise<VersionReturn>
|
|
379
|
-
runtimeStatus(headers?: object): Promise<RuntimeStatusReturn>
|
|
380
|
-
getChainID(headers?: object): Promise<GetChainIDReturn>
|
|
381
|
-
getEtherBalance(args: GetEtherBalanceArgs, headers?: object): Promise<GetEtherBalanceReturn>
|
|
382
|
-
getTokenBalances(args: GetTokenBalancesArgs, headers?: object): Promise<GetTokenBalancesReturn>
|
|
383
|
-
getTokenSupplies(args: GetTokenSuppliesArgs, headers?: object): Promise<GetTokenSuppliesReturn>
|
|
384
|
-
getTokenSuppliesMap(args: GetTokenSuppliesMapArgs, headers?: object): Promise<GetTokenSuppliesMapReturn>
|
|
385
|
-
getBalanceUpdates(args: GetBalanceUpdatesArgs, headers?: object): Promise<GetBalanceUpdatesReturn>
|
|
386
|
-
getTransactionHistory(
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
408
|
+
ping(headers?: object, signal?: AbortSignal): Promise<PingReturn>
|
|
409
|
+
version(headers?: object, signal?: AbortSignal): Promise<VersionReturn>
|
|
410
|
+
runtimeStatus(headers?: object, signal?: AbortSignal): Promise<RuntimeStatusReturn>
|
|
411
|
+
getChainID(headers?: object, signal?: AbortSignal): Promise<GetChainIDReturn>
|
|
412
|
+
getEtherBalance(args: GetEtherBalanceArgs, headers?: object, signal?: AbortSignal): Promise<GetEtherBalanceReturn>
|
|
413
|
+
getTokenBalances(args: GetTokenBalancesArgs, headers?: object, signal?: AbortSignal): Promise<GetTokenBalancesReturn>
|
|
414
|
+
getTokenSupplies(args: GetTokenSuppliesArgs, headers?: object, signal?: AbortSignal): Promise<GetTokenSuppliesReturn>
|
|
415
|
+
getTokenSuppliesMap(args: GetTokenSuppliesMapArgs, headers?: object, signal?: AbortSignal): Promise<GetTokenSuppliesMapReturn>
|
|
416
|
+
getBalanceUpdates(args: GetBalanceUpdatesArgs, headers?: object, signal?: AbortSignal): Promise<GetBalanceUpdatesReturn>
|
|
417
|
+
getTransactionHistory(
|
|
418
|
+
args: GetTransactionHistoryArgs,
|
|
419
|
+
headers?: object,
|
|
420
|
+
signal?: AbortSignal
|
|
421
|
+
): Promise<GetTransactionHistoryReturn>
|
|
422
|
+
syncBalance(args: SyncBalanceArgs, headers?: object, signal?: AbortSignal): Promise<SyncBalanceReturn>
|
|
423
|
+
fetchTransactionReceipt(
|
|
424
|
+
args: FetchTransactionReceiptArgs,
|
|
425
|
+
headers?: object,
|
|
426
|
+
signal?: AbortSignal
|
|
427
|
+
): Promise<FetchTransactionReceiptReturn>
|
|
428
|
+
getOrderbookOrders(args: GetOrderbookOrdersArgs, headers?: object, signal?: AbortSignal): Promise<GetOrderbookOrdersReturn>
|
|
429
|
+
getTopOrders(args: GetTopOrdersArgs, headers?: object, signal?: AbortSignal): Promise<GetTopOrdersReturn>
|
|
391
430
|
fetchTransactionReceiptWithFilter(
|
|
392
431
|
args: FetchTransactionReceiptWithFilterArgs,
|
|
393
|
-
headers?: object
|
|
432
|
+
headers?: object,
|
|
433
|
+
signal?: AbortSignal
|
|
394
434
|
): Promise<FetchTransactionReceiptWithFilterReturn>
|
|
395
|
-
getAllWebhookListeners(
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
435
|
+
getAllWebhookListeners(
|
|
436
|
+
args: GetAllWebhookListenersArgs,
|
|
437
|
+
headers?: object,
|
|
438
|
+
signal?: AbortSignal
|
|
439
|
+
): Promise<GetAllWebhookListenersReturn>
|
|
440
|
+
getWebhookListener(args: GetWebhookListenerArgs, headers?: object, signal?: AbortSignal): Promise<GetWebhookListenerReturn>
|
|
441
|
+
addWebhookListener(args: AddWebhookListenerArgs, headers?: object, signal?: AbortSignal): Promise<AddWebhookListenerReturn>
|
|
442
|
+
updateWebhookListener(
|
|
443
|
+
args: UpdateWebhookListenerArgs,
|
|
444
|
+
headers?: object,
|
|
445
|
+
signal?: AbortSignal
|
|
446
|
+
): Promise<UpdateWebhookListenerReturn>
|
|
447
|
+
removeWebhookListener(
|
|
448
|
+
args: RemoveWebhookListenerArgs,
|
|
449
|
+
headers?: object,
|
|
450
|
+
signal?: AbortSignal
|
|
451
|
+
): Promise<RemoveWebhookListenerReturn>
|
|
452
|
+
subscribeReceipts(args: SubscribeReceiptsArgs, options: WebrpcStreamOptions<SubscribeReceiptsReturn>): Promise<void>
|
|
453
|
+
subscribeEvents(args: SubscribeEventsArgs, options: WebrpcStreamOptions<SubscribeEventsReturn>): Promise<void>
|
|
400
454
|
}
|
|
401
455
|
|
|
402
456
|
export interface PingArgs {}
|
|
@@ -526,7 +580,7 @@ export interface GetTopOrdersReturn {
|
|
|
526
580
|
orders: Array<OrderbookOrder>
|
|
527
581
|
}
|
|
528
582
|
export interface FetchTransactionReceiptWithFilterArgs {
|
|
529
|
-
filter:
|
|
583
|
+
filter: TransactionFilter
|
|
530
584
|
maxBlockWait?: number
|
|
531
585
|
}
|
|
532
586
|
|
|
@@ -550,7 +604,7 @@ export interface GetWebhookListenerReturn {
|
|
|
550
604
|
}
|
|
551
605
|
export interface AddWebhookListenerArgs {
|
|
552
606
|
url: string
|
|
553
|
-
filters:
|
|
607
|
+
filters: EventFilter
|
|
554
608
|
projectId?: number
|
|
555
609
|
}
|
|
556
610
|
|
|
@@ -574,6 +628,20 @@ export interface RemoveWebhookListenerArgs {
|
|
|
574
628
|
export interface RemoveWebhookListenerReturn {
|
|
575
629
|
status: boolean
|
|
576
630
|
}
|
|
631
|
+
export interface SubscribeReceiptsArgs {
|
|
632
|
+
filter: TransactionFilter
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
export interface SubscribeReceiptsReturn {
|
|
636
|
+
receipt: TransactionReceipt
|
|
637
|
+
}
|
|
638
|
+
export interface SubscribeEventsArgs {
|
|
639
|
+
filter: EventFilter
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
export interface SubscribeEventsReturn {
|
|
643
|
+
log: EventLog
|
|
644
|
+
}
|
|
577
645
|
|
|
578
646
|
//
|
|
579
647
|
// Client
|
|
@@ -592,226 +660,507 @@ export class Indexer implements Indexer {
|
|
|
592
660
|
return this.hostname + this.path + name
|
|
593
661
|
}
|
|
594
662
|
|
|
595
|
-
ping = (headers?: object): Promise<PingReturn> => {
|
|
596
|
-
return this.fetch(this.url('Ping'), createHTTPRequest({}, headers)).then(
|
|
597
|
-
|
|
598
|
-
return {
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
663
|
+
ping = (headers?: object, signal?: AbortSignal): Promise<PingReturn> => {
|
|
664
|
+
return this.fetch(this.url('Ping'), createHTTPRequest({}, headers, signal)).then(
|
|
665
|
+
res => {
|
|
666
|
+
return buildResponse(res).then(_data => {
|
|
667
|
+
return {
|
|
668
|
+
status: <boolean>_data.status
|
|
669
|
+
}
|
|
670
|
+
})
|
|
671
|
+
},
|
|
672
|
+
error => {
|
|
673
|
+
throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` })
|
|
674
|
+
}
|
|
675
|
+
)
|
|
603
676
|
}
|
|
604
677
|
|
|
605
|
-
version = (headers?: object): Promise<VersionReturn> => {
|
|
606
|
-
return this.fetch(this.url('Version'), createHTTPRequest({}, headers)).then(
|
|
607
|
-
|
|
608
|
-
return {
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
678
|
+
version = (headers?: object, signal?: AbortSignal): Promise<VersionReturn> => {
|
|
679
|
+
return this.fetch(this.url('Version'), createHTTPRequest({}, headers, signal)).then(
|
|
680
|
+
res => {
|
|
681
|
+
return buildResponse(res).then(_data => {
|
|
682
|
+
return {
|
|
683
|
+
version: <Version>_data.version
|
|
684
|
+
}
|
|
685
|
+
})
|
|
686
|
+
},
|
|
687
|
+
error => {
|
|
688
|
+
throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` })
|
|
689
|
+
}
|
|
690
|
+
)
|
|
613
691
|
}
|
|
614
692
|
|
|
615
|
-
runtimeStatus = (headers?: object): Promise<RuntimeStatusReturn> => {
|
|
616
|
-
return this.fetch(this.url('RuntimeStatus'), createHTTPRequest({}, headers)).then(
|
|
617
|
-
|
|
618
|
-
return {
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
693
|
+
runtimeStatus = (headers?: object, signal?: AbortSignal): Promise<RuntimeStatusReturn> => {
|
|
694
|
+
return this.fetch(this.url('RuntimeStatus'), createHTTPRequest({}, headers, signal)).then(
|
|
695
|
+
res => {
|
|
696
|
+
return buildResponse(res).then(_data => {
|
|
697
|
+
return {
|
|
698
|
+
status: <RuntimeStatus>_data.status
|
|
699
|
+
}
|
|
700
|
+
})
|
|
701
|
+
},
|
|
702
|
+
error => {
|
|
703
|
+
throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` })
|
|
704
|
+
}
|
|
705
|
+
)
|
|
623
706
|
}
|
|
624
707
|
|
|
625
|
-
getChainID = (headers?: object): Promise<GetChainIDReturn> => {
|
|
626
|
-
return this.fetch(this.url('GetChainID'), createHTTPRequest({}, headers)).then(
|
|
627
|
-
|
|
628
|
-
return {
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
708
|
+
getChainID = (headers?: object, signal?: AbortSignal): Promise<GetChainIDReturn> => {
|
|
709
|
+
return this.fetch(this.url('GetChainID'), createHTTPRequest({}, headers, signal)).then(
|
|
710
|
+
res => {
|
|
711
|
+
return buildResponse(res).then(_data => {
|
|
712
|
+
return {
|
|
713
|
+
chainID: <number>_data.chainID
|
|
714
|
+
}
|
|
715
|
+
})
|
|
716
|
+
},
|
|
717
|
+
error => {
|
|
718
|
+
throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` })
|
|
719
|
+
}
|
|
720
|
+
)
|
|
633
721
|
}
|
|
634
722
|
|
|
635
|
-
getEtherBalance = (args: GetEtherBalanceArgs, headers?: object): Promise<GetEtherBalanceReturn> => {
|
|
636
|
-
return this.fetch(this.url('GetEtherBalance'), createHTTPRequest(args, headers)).then(
|
|
637
|
-
|
|
638
|
-
return {
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
723
|
+
getEtherBalance = (args: GetEtherBalanceArgs, headers?: object, signal?: AbortSignal): Promise<GetEtherBalanceReturn> => {
|
|
724
|
+
return this.fetch(this.url('GetEtherBalance'), createHTTPRequest(args, headers, signal)).then(
|
|
725
|
+
res => {
|
|
726
|
+
return buildResponse(res).then(_data => {
|
|
727
|
+
return {
|
|
728
|
+
balance: <EtherBalance>_data.balance
|
|
729
|
+
}
|
|
730
|
+
})
|
|
731
|
+
},
|
|
732
|
+
error => {
|
|
733
|
+
throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` })
|
|
734
|
+
}
|
|
735
|
+
)
|
|
643
736
|
}
|
|
644
737
|
|
|
645
|
-
getTokenBalances = (args: GetTokenBalancesArgs, headers?: object): Promise<GetTokenBalancesReturn> => {
|
|
646
|
-
return this.fetch(this.url('GetTokenBalances'), createHTTPRequest(args, headers)).then(
|
|
647
|
-
|
|
648
|
-
return {
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
738
|
+
getTokenBalances = (args: GetTokenBalancesArgs, headers?: object, signal?: AbortSignal): Promise<GetTokenBalancesReturn> => {
|
|
739
|
+
return this.fetch(this.url('GetTokenBalances'), createHTTPRequest(args, headers, signal)).then(
|
|
740
|
+
res => {
|
|
741
|
+
return buildResponse(res).then(_data => {
|
|
742
|
+
return {
|
|
743
|
+
page: <Page>_data.page,
|
|
744
|
+
balances: <Array<TokenBalance>>_data.balances
|
|
745
|
+
}
|
|
746
|
+
})
|
|
747
|
+
},
|
|
748
|
+
error => {
|
|
749
|
+
throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` })
|
|
750
|
+
}
|
|
751
|
+
)
|
|
654
752
|
}
|
|
655
753
|
|
|
656
|
-
getTokenSupplies = (args: GetTokenSuppliesArgs, headers?: object): Promise<GetTokenSuppliesReturn> => {
|
|
657
|
-
return this.fetch(this.url('GetTokenSupplies'), createHTTPRequest(args, headers)).then(
|
|
658
|
-
|
|
659
|
-
return {
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
754
|
+
getTokenSupplies = (args: GetTokenSuppliesArgs, headers?: object, signal?: AbortSignal): Promise<GetTokenSuppliesReturn> => {
|
|
755
|
+
return this.fetch(this.url('GetTokenSupplies'), createHTTPRequest(args, headers, signal)).then(
|
|
756
|
+
res => {
|
|
757
|
+
return buildResponse(res).then(_data => {
|
|
758
|
+
return {
|
|
759
|
+
page: <Page>_data.page,
|
|
760
|
+
contractType: <ContractType>_data.contractType,
|
|
761
|
+
tokenIDs: <Array<TokenSupply>>_data.tokenIDs
|
|
762
|
+
}
|
|
763
|
+
})
|
|
764
|
+
},
|
|
765
|
+
error => {
|
|
766
|
+
throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` })
|
|
767
|
+
}
|
|
768
|
+
)
|
|
666
769
|
}
|
|
667
770
|
|
|
668
|
-
getTokenSuppliesMap = (
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
771
|
+
getTokenSuppliesMap = (
|
|
772
|
+
args: GetTokenSuppliesMapArgs,
|
|
773
|
+
headers?: object,
|
|
774
|
+
signal?: AbortSignal
|
|
775
|
+
): Promise<GetTokenSuppliesMapReturn> => {
|
|
776
|
+
return this.fetch(this.url('GetTokenSuppliesMap'), createHTTPRequest(args, headers, signal)).then(
|
|
777
|
+
res => {
|
|
778
|
+
return buildResponse(res).then(_data => {
|
|
779
|
+
return {
|
|
780
|
+
supplies: <{ [key: string]: Array<TokenSupply> }>_data.supplies
|
|
781
|
+
}
|
|
782
|
+
})
|
|
783
|
+
},
|
|
784
|
+
error => {
|
|
785
|
+
throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` })
|
|
786
|
+
}
|
|
787
|
+
)
|
|
676
788
|
}
|
|
677
789
|
|
|
678
|
-
getBalanceUpdates = (args: GetBalanceUpdatesArgs, headers?: object): Promise<GetBalanceUpdatesReturn> => {
|
|
679
|
-
return this.fetch(this.url('GetBalanceUpdates'), createHTTPRequest(args, headers)).then(
|
|
680
|
-
|
|
681
|
-
return {
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
790
|
+
getBalanceUpdates = (args: GetBalanceUpdatesArgs, headers?: object, signal?: AbortSignal): Promise<GetBalanceUpdatesReturn> => {
|
|
791
|
+
return this.fetch(this.url('GetBalanceUpdates'), createHTTPRequest(args, headers, signal)).then(
|
|
792
|
+
res => {
|
|
793
|
+
return buildResponse(res).then(_data => {
|
|
794
|
+
return {
|
|
795
|
+
page: <Page>_data.page,
|
|
796
|
+
balances: <Array<TokenBalance>>_data.balances
|
|
797
|
+
}
|
|
798
|
+
})
|
|
799
|
+
},
|
|
800
|
+
error => {
|
|
801
|
+
throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` })
|
|
802
|
+
}
|
|
803
|
+
)
|
|
687
804
|
}
|
|
688
805
|
|
|
689
|
-
getTransactionHistory = (
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
806
|
+
getTransactionHistory = (
|
|
807
|
+
args: GetTransactionHistoryArgs,
|
|
808
|
+
headers?: object,
|
|
809
|
+
signal?: AbortSignal
|
|
810
|
+
): Promise<GetTransactionHistoryReturn> => {
|
|
811
|
+
return this.fetch(this.url('GetTransactionHistory'), createHTTPRequest(args, headers, signal)).then(
|
|
812
|
+
res => {
|
|
813
|
+
return buildResponse(res).then(_data => {
|
|
814
|
+
return {
|
|
815
|
+
page: <Page>_data.page,
|
|
816
|
+
transactions: <Array<Transaction>>_data.transactions
|
|
817
|
+
}
|
|
818
|
+
})
|
|
819
|
+
},
|
|
820
|
+
error => {
|
|
821
|
+
throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` })
|
|
822
|
+
}
|
|
823
|
+
)
|
|
698
824
|
}
|
|
699
825
|
|
|
700
|
-
syncBalance = (args: SyncBalanceArgs, headers?: object): Promise<SyncBalanceReturn> => {
|
|
701
|
-
return this.fetch(this.url('SyncBalance'), createHTTPRequest(args, headers)).then(
|
|
702
|
-
|
|
703
|
-
return {
|
|
704
|
-
|
|
705
|
-
|
|
826
|
+
syncBalance = (args: SyncBalanceArgs, headers?: object, signal?: AbortSignal): Promise<SyncBalanceReturn> => {
|
|
827
|
+
return this.fetch(this.url('SyncBalance'), createHTTPRequest(args, headers, signal)).then(
|
|
828
|
+
res => {
|
|
829
|
+
return buildResponse(res).then(_data => {
|
|
830
|
+
return {}
|
|
831
|
+
})
|
|
832
|
+
},
|
|
833
|
+
error => {
|
|
834
|
+
throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` })
|
|
835
|
+
}
|
|
836
|
+
)
|
|
706
837
|
}
|
|
707
838
|
|
|
708
|
-
fetchTransactionReceipt = (
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
839
|
+
fetchTransactionReceipt = (
|
|
840
|
+
args: FetchTransactionReceiptArgs,
|
|
841
|
+
headers?: object,
|
|
842
|
+
signal?: AbortSignal
|
|
843
|
+
): Promise<FetchTransactionReceiptReturn> => {
|
|
844
|
+
return this.fetch(this.url('FetchTransactionReceipt'), createHTTPRequest(args, headers, signal)).then(
|
|
845
|
+
res => {
|
|
846
|
+
return buildResponse(res).then(_data => {
|
|
847
|
+
return {
|
|
848
|
+
receipt: <TransactionReceipt>_data.receipt
|
|
849
|
+
}
|
|
850
|
+
})
|
|
851
|
+
},
|
|
852
|
+
error => {
|
|
853
|
+
throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` })
|
|
854
|
+
}
|
|
855
|
+
)
|
|
716
856
|
}
|
|
717
857
|
|
|
718
|
-
getOrderbookOrders = (
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
858
|
+
getOrderbookOrders = (
|
|
859
|
+
args: GetOrderbookOrdersArgs,
|
|
860
|
+
headers?: object,
|
|
861
|
+
signal?: AbortSignal
|
|
862
|
+
): Promise<GetOrderbookOrdersReturn> => {
|
|
863
|
+
return this.fetch(this.url('GetOrderbookOrders'), createHTTPRequest(args, headers, signal)).then(
|
|
864
|
+
res => {
|
|
865
|
+
return buildResponse(res).then(_data => {
|
|
866
|
+
return {
|
|
867
|
+
page: <Page>_data.page,
|
|
868
|
+
orders: <Array<OrderbookOrder>>_data.orders
|
|
869
|
+
}
|
|
870
|
+
})
|
|
871
|
+
},
|
|
872
|
+
error => {
|
|
873
|
+
throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` })
|
|
874
|
+
}
|
|
875
|
+
)
|
|
727
876
|
}
|
|
728
877
|
|
|
729
|
-
getTopOrders = (args: GetTopOrdersArgs, headers?: object): Promise<GetTopOrdersReturn> => {
|
|
730
|
-
return this.fetch(this.url('GetTopOrders'), createHTTPRequest(args, headers)).then(
|
|
731
|
-
|
|
732
|
-
return {
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
878
|
+
getTopOrders = (args: GetTopOrdersArgs, headers?: object, signal?: AbortSignal): Promise<GetTopOrdersReturn> => {
|
|
879
|
+
return this.fetch(this.url('GetTopOrders'), createHTTPRequest(args, headers, signal)).then(
|
|
880
|
+
res => {
|
|
881
|
+
return buildResponse(res).then(_data => {
|
|
882
|
+
return {
|
|
883
|
+
orders: <Array<OrderbookOrder>>_data.orders
|
|
884
|
+
}
|
|
885
|
+
})
|
|
886
|
+
},
|
|
887
|
+
error => {
|
|
888
|
+
throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` })
|
|
889
|
+
}
|
|
890
|
+
)
|
|
737
891
|
}
|
|
738
892
|
|
|
739
893
|
fetchTransactionReceiptWithFilter = (
|
|
740
894
|
args: FetchTransactionReceiptWithFilterArgs,
|
|
741
|
-
headers?: object
|
|
895
|
+
headers?: object,
|
|
896
|
+
signal?: AbortSignal
|
|
742
897
|
): Promise<FetchTransactionReceiptWithFilterReturn> => {
|
|
743
|
-
return this.fetch(this.url('FetchTransactionReceiptWithFilter'), createHTTPRequest(args, headers)).then(
|
|
744
|
-
|
|
745
|
-
return {
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
898
|
+
return this.fetch(this.url('FetchTransactionReceiptWithFilter'), createHTTPRequest(args, headers, signal)).then(
|
|
899
|
+
res => {
|
|
900
|
+
return buildResponse(res).then(_data => {
|
|
901
|
+
return {
|
|
902
|
+
receipt: <TransactionReceipt>_data.receipt
|
|
903
|
+
}
|
|
904
|
+
})
|
|
905
|
+
},
|
|
906
|
+
error => {
|
|
907
|
+
throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` })
|
|
908
|
+
}
|
|
909
|
+
)
|
|
750
910
|
}
|
|
751
911
|
|
|
752
|
-
getAllWebhookListeners = (
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
912
|
+
getAllWebhookListeners = (
|
|
913
|
+
args: GetAllWebhookListenersArgs,
|
|
914
|
+
headers?: object,
|
|
915
|
+
signal?: AbortSignal
|
|
916
|
+
): Promise<GetAllWebhookListenersReturn> => {
|
|
917
|
+
return this.fetch(this.url('GetAllWebhookListeners'), createHTTPRequest(args, headers, signal)).then(
|
|
918
|
+
res => {
|
|
919
|
+
return buildResponse(res).then(_data => {
|
|
920
|
+
return {
|
|
921
|
+
listeners: <Array<WebhookListener>>_data.listeners
|
|
922
|
+
}
|
|
923
|
+
})
|
|
924
|
+
},
|
|
925
|
+
error => {
|
|
926
|
+
throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` })
|
|
927
|
+
}
|
|
928
|
+
)
|
|
760
929
|
}
|
|
761
930
|
|
|
762
|
-
getWebhookListener = (
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
931
|
+
getWebhookListener = (
|
|
932
|
+
args: GetWebhookListenerArgs,
|
|
933
|
+
headers?: object,
|
|
934
|
+
signal?: AbortSignal
|
|
935
|
+
): Promise<GetWebhookListenerReturn> => {
|
|
936
|
+
return this.fetch(this.url('GetWebhookListener'), createHTTPRequest(args, headers, signal)).then(
|
|
937
|
+
res => {
|
|
938
|
+
return buildResponse(res).then(_data => {
|
|
939
|
+
return {
|
|
940
|
+
listener: <WebhookListener>_data.listener
|
|
941
|
+
}
|
|
942
|
+
})
|
|
943
|
+
},
|
|
944
|
+
error => {
|
|
945
|
+
throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` })
|
|
946
|
+
}
|
|
947
|
+
)
|
|
770
948
|
}
|
|
771
949
|
|
|
772
|
-
addWebhookListener = (
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
950
|
+
addWebhookListener = (
|
|
951
|
+
args: AddWebhookListenerArgs,
|
|
952
|
+
headers?: object,
|
|
953
|
+
signal?: AbortSignal
|
|
954
|
+
): Promise<AddWebhookListenerReturn> => {
|
|
955
|
+
return this.fetch(this.url('AddWebhookListener'), createHTTPRequest(args, headers, signal)).then(
|
|
956
|
+
res => {
|
|
957
|
+
return buildResponse(res).then(_data => {
|
|
958
|
+
return {
|
|
959
|
+
status: <boolean>_data.status,
|
|
960
|
+
listener: <WebhookListener>_data.listener
|
|
961
|
+
}
|
|
962
|
+
})
|
|
963
|
+
},
|
|
964
|
+
error => {
|
|
965
|
+
throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` })
|
|
966
|
+
}
|
|
967
|
+
)
|
|
781
968
|
}
|
|
782
969
|
|
|
783
|
-
updateWebhookListener = (
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
970
|
+
updateWebhookListener = (
|
|
971
|
+
args: UpdateWebhookListenerArgs,
|
|
972
|
+
headers?: object,
|
|
973
|
+
signal?: AbortSignal
|
|
974
|
+
): Promise<UpdateWebhookListenerReturn> => {
|
|
975
|
+
return this.fetch(this.url('UpdateWebhookListener'), createHTTPRequest(args, headers, signal)).then(
|
|
976
|
+
res => {
|
|
977
|
+
return buildResponse(res).then(_data => {
|
|
978
|
+
return {
|
|
979
|
+
status: <boolean>_data.status
|
|
980
|
+
}
|
|
981
|
+
})
|
|
982
|
+
},
|
|
983
|
+
error => {
|
|
984
|
+
throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` })
|
|
985
|
+
}
|
|
986
|
+
)
|
|
791
987
|
}
|
|
792
988
|
|
|
793
|
-
removeWebhookListener = (
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
989
|
+
removeWebhookListener = (
|
|
990
|
+
args: RemoveWebhookListenerArgs,
|
|
991
|
+
headers?: object,
|
|
992
|
+
signal?: AbortSignal
|
|
993
|
+
): Promise<RemoveWebhookListenerReturn> => {
|
|
994
|
+
return this.fetch(this.url('RemoveWebhookListener'), createHTTPRequest(args, headers, signal)).then(
|
|
995
|
+
res => {
|
|
996
|
+
return buildResponse(res).then(_data => {
|
|
997
|
+
return {
|
|
998
|
+
status: <boolean>_data.status
|
|
999
|
+
}
|
|
1000
|
+
})
|
|
1001
|
+
},
|
|
1002
|
+
error => {
|
|
1003
|
+
throw WebrpcRequestFailedError.new({ cause: `fetch(): ${error.message || ''}` })
|
|
1004
|
+
}
|
|
1005
|
+
)
|
|
1006
|
+
}
|
|
1007
|
+
|
|
1008
|
+
subscribeReceipts = (args: SubscribeReceiptsArgs, options: WebrpcStreamOptions<SubscribeReceiptsReturn>): Promise<void> => {
|
|
1009
|
+
const _fetch = () =>
|
|
1010
|
+
this.fetch(this.url('SubscribeReceipts'), createHTTPRequest(args, options.headers, options.signal)).then(
|
|
1011
|
+
async res => {
|
|
1012
|
+
await sseResponse(res, options, _fetch)
|
|
1013
|
+
},
|
|
1014
|
+
error => {
|
|
1015
|
+
options.onError(error, _fetch)
|
|
798
1016
|
}
|
|
799
|
-
|
|
800
|
-
|
|
1017
|
+
)
|
|
1018
|
+
return _fetch()
|
|
1019
|
+
}
|
|
1020
|
+
subscribeEvents = (args: SubscribeEventsArgs, options: WebrpcStreamOptions<SubscribeEventsReturn>): Promise<void> => {
|
|
1021
|
+
const _fetch = () =>
|
|
1022
|
+
this.fetch(this.url('SubscribeEvents'), createHTTPRequest(args, options.headers, options.signal)).then(
|
|
1023
|
+
async res => {
|
|
1024
|
+
await sseResponse(res, options, _fetch)
|
|
1025
|
+
},
|
|
1026
|
+
error => {
|
|
1027
|
+
options.onError(error, _fetch)
|
|
1028
|
+
}
|
|
1029
|
+
)
|
|
1030
|
+
return _fetch()
|
|
801
1031
|
}
|
|
802
1032
|
}
|
|
803
1033
|
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
1034
|
+
const sseResponse = async (res: Response, options: WebrpcStreamOptions<any>, retryFetch: () => Promise<void>) => {
|
|
1035
|
+
const { onMessage, onOpen, onClose, onError } = options
|
|
1036
|
+
|
|
1037
|
+
if (!res.ok) {
|
|
1038
|
+
try {
|
|
1039
|
+
await buildResponse(res)
|
|
1040
|
+
} catch (error) {
|
|
1041
|
+
// @ts-ignore
|
|
1042
|
+
onError(error, retryFetch)
|
|
1043
|
+
}
|
|
1044
|
+
return
|
|
1045
|
+
}
|
|
1046
|
+
|
|
1047
|
+
if (!res.body) {
|
|
1048
|
+
onError(
|
|
1049
|
+
WebrpcBadResponseError.new({
|
|
1050
|
+
status: res.status,
|
|
1051
|
+
cause: 'Invalid response, missing body'
|
|
1052
|
+
}),
|
|
1053
|
+
retryFetch
|
|
1054
|
+
)
|
|
1055
|
+
return
|
|
1056
|
+
}
|
|
1057
|
+
|
|
1058
|
+
onOpen && onOpen()
|
|
1059
|
+
|
|
1060
|
+
const reader = res.body.getReader()
|
|
1061
|
+
const decoder = new TextDecoder()
|
|
1062
|
+
let buffer = ''
|
|
1063
|
+
let lastReadTime = Date.now()
|
|
1064
|
+
const timeout = (10 + 1) * 1000
|
|
1065
|
+
let intervalId: any
|
|
1066
|
+
|
|
1067
|
+
try {
|
|
1068
|
+
intervalId = setInterval(() => {
|
|
1069
|
+
if (Date.now() - lastReadTime > timeout) {
|
|
1070
|
+
throw WebrpcStreamLostError.new({ cause: 'Stream timed out' })
|
|
1071
|
+
}
|
|
1072
|
+
}, timeout)
|
|
1073
|
+
|
|
1074
|
+
while (true) {
|
|
1075
|
+
let value
|
|
1076
|
+
let done
|
|
1077
|
+
try {
|
|
1078
|
+
;({ value, done } = await reader.read())
|
|
1079
|
+
lastReadTime = Date.now()
|
|
1080
|
+
buffer += decoder.decode(value, { stream: true })
|
|
1081
|
+
} catch (error) {
|
|
1082
|
+
let message = ''
|
|
1083
|
+
if (error instanceof Error) {
|
|
1084
|
+
message = error.message
|
|
1085
|
+
}
|
|
1086
|
+
|
|
1087
|
+
if (error instanceof DOMException && error.name === 'AbortError') {
|
|
1088
|
+
onError(
|
|
1089
|
+
WebrpcRequestFailedError.new({
|
|
1090
|
+
message: 'AbortError',
|
|
1091
|
+
cause: `AbortError: ${message}`
|
|
1092
|
+
}),
|
|
1093
|
+
() => {
|
|
1094
|
+
throw new Error('Abort signal cannot be used to reconnect')
|
|
1095
|
+
}
|
|
1096
|
+
)
|
|
1097
|
+
} else {
|
|
1098
|
+
onError(
|
|
1099
|
+
WebrpcStreamLostError.new({
|
|
1100
|
+
cause: `reader.read(): ${message}`
|
|
1101
|
+
}),
|
|
1102
|
+
retryFetch
|
|
1103
|
+
)
|
|
1104
|
+
}
|
|
1105
|
+
return
|
|
1106
|
+
}
|
|
1107
|
+
|
|
1108
|
+
let lines = buffer.split('\n')
|
|
1109
|
+
for (let i = 0; i < lines.length - 1; i++) {
|
|
1110
|
+
if (lines[i].length == 0) {
|
|
1111
|
+
continue
|
|
1112
|
+
}
|
|
1113
|
+
let data: any
|
|
1114
|
+
try {
|
|
1115
|
+
data = JSON.parse(lines[i])
|
|
1116
|
+
if (data.hasOwnProperty('webrpcError')) {
|
|
1117
|
+
const error = data.webrpcError
|
|
1118
|
+
const code: number = typeof error.code === 'number' ? error.code : 0
|
|
1119
|
+
onError((webrpcErrorByCode[code] || WebrpcError).new(error), retryFetch)
|
|
1120
|
+
return
|
|
1121
|
+
}
|
|
1122
|
+
} catch (error) {
|
|
1123
|
+
if (error instanceof Error && error.message === 'Abort signal cannot be used to reconnect') {
|
|
1124
|
+
throw error
|
|
1125
|
+
}
|
|
1126
|
+
onError(
|
|
1127
|
+
WebrpcBadResponseError.new({
|
|
1128
|
+
status: res.status,
|
|
1129
|
+
// @ts-ignore
|
|
1130
|
+
cause: `JSON.parse(): ${error.message}`
|
|
1131
|
+
}),
|
|
1132
|
+
retryFetch
|
|
1133
|
+
)
|
|
1134
|
+
}
|
|
1135
|
+
onMessage(data)
|
|
1136
|
+
}
|
|
1137
|
+
|
|
1138
|
+
if (!done) {
|
|
1139
|
+
buffer = lines[lines.length - 1]
|
|
1140
|
+
continue
|
|
1141
|
+
}
|
|
1142
|
+
|
|
1143
|
+
onClose && onClose()
|
|
1144
|
+
return
|
|
1145
|
+
}
|
|
1146
|
+
} catch (error) {
|
|
1147
|
+
// @ts-ignore
|
|
1148
|
+
if (error instanceof WebrpcStreamLostError) {
|
|
1149
|
+
onError(error, retryFetch)
|
|
1150
|
+
} else {
|
|
1151
|
+
throw error
|
|
1152
|
+
}
|
|
1153
|
+
} finally {
|
|
1154
|
+
clearInterval(intervalId)
|
|
1155
|
+
}
|
|
808
1156
|
}
|
|
809
1157
|
|
|
810
|
-
const createHTTPRequest = (body: object = {}, headers: object = {}): object => {
|
|
1158
|
+
const createHTTPRequest = (body: object = {}, headers: object = {}, signal: AbortSignal | null = null): object => {
|
|
811
1159
|
return {
|
|
812
1160
|
method: 'POST',
|
|
813
1161
|
headers: { ...headers, 'Content-Type': 'application/json' },
|
|
814
|
-
body: JSON.stringify(body || {})
|
|
1162
|
+
body: JSON.stringify(body || {}),
|
|
1163
|
+
signal
|
|
815
1164
|
}
|
|
816
1165
|
}
|
|
817
1166
|
|
|
@@ -820,14 +1169,433 @@ const buildResponse = (res: Response): Promise<any> => {
|
|
|
820
1169
|
let data
|
|
821
1170
|
try {
|
|
822
1171
|
data = JSON.parse(text)
|
|
823
|
-
} catch (
|
|
824
|
-
|
|
1172
|
+
} catch (error) {
|
|
1173
|
+
let message = ''
|
|
1174
|
+
if (error instanceof Error) {
|
|
1175
|
+
message = error.message
|
|
1176
|
+
}
|
|
1177
|
+
throw WebrpcBadResponseError.new({
|
|
1178
|
+
status: res.status,
|
|
1179
|
+
cause: `JSON.parse(): ${message}: response text: ${text}`
|
|
1180
|
+
})
|
|
825
1181
|
}
|
|
826
1182
|
if (!res.ok) {
|
|
827
|
-
|
|
1183
|
+
const code: number = typeof data.code === 'number' ? data.code : 0
|
|
1184
|
+
throw (webrpcErrorByCode[code] || WebrpcError).new(data)
|
|
828
1185
|
}
|
|
829
1186
|
return data
|
|
830
1187
|
})
|
|
831
1188
|
}
|
|
832
1189
|
|
|
1190
|
+
//
|
|
1191
|
+
// Errors
|
|
1192
|
+
//
|
|
1193
|
+
|
|
1194
|
+
export class WebrpcError extends Error {
|
|
1195
|
+
name: string
|
|
1196
|
+
code: number
|
|
1197
|
+
message: string
|
|
1198
|
+
status: number
|
|
1199
|
+
cause?: string
|
|
1200
|
+
|
|
1201
|
+
/** @deprecated Use message instead of msg. Deprecated in webrpc v0.11.0. */
|
|
1202
|
+
msg: string
|
|
1203
|
+
|
|
1204
|
+
constructor(name: string, code: number, message: string, status: number, cause?: string) {
|
|
1205
|
+
super(message)
|
|
1206
|
+
this.name = name || 'WebrpcError'
|
|
1207
|
+
this.code = typeof code === 'number' ? code : 0
|
|
1208
|
+
this.message = message || `endpoint error ${this.code}`
|
|
1209
|
+
this.msg = this.message
|
|
1210
|
+
this.status = typeof status === 'number' ? status : 0
|
|
1211
|
+
this.cause = cause
|
|
1212
|
+
Object.setPrototypeOf(this, WebrpcError.prototype)
|
|
1213
|
+
}
|
|
1214
|
+
|
|
1215
|
+
static new(payload: any): WebrpcError {
|
|
1216
|
+
return new this(payload.error, payload.code, payload.message || payload.msg, payload.status, payload.cause)
|
|
1217
|
+
}
|
|
1218
|
+
}
|
|
1219
|
+
|
|
1220
|
+
// Webrpc errors
|
|
1221
|
+
|
|
1222
|
+
export class WebrpcEndpointError extends WebrpcError {
|
|
1223
|
+
constructor(
|
|
1224
|
+
name: string = 'WebrpcEndpoint',
|
|
1225
|
+
code: number = 0,
|
|
1226
|
+
message: string = 'endpoint error',
|
|
1227
|
+
status: number = 0,
|
|
1228
|
+
cause?: string
|
|
1229
|
+
) {
|
|
1230
|
+
super(name, code, message, status, cause)
|
|
1231
|
+
Object.setPrototypeOf(this, WebrpcEndpointError.prototype)
|
|
1232
|
+
}
|
|
1233
|
+
}
|
|
1234
|
+
|
|
1235
|
+
export class WebrpcRequestFailedError extends WebrpcError {
|
|
1236
|
+
constructor(
|
|
1237
|
+
name: string = 'WebrpcRequestFailed',
|
|
1238
|
+
code: number = -1,
|
|
1239
|
+
message: string = 'request failed',
|
|
1240
|
+
status: number = 0,
|
|
1241
|
+
cause?: string
|
|
1242
|
+
) {
|
|
1243
|
+
super(name, code, message, status, cause)
|
|
1244
|
+
Object.setPrototypeOf(this, WebrpcRequestFailedError.prototype)
|
|
1245
|
+
}
|
|
1246
|
+
}
|
|
1247
|
+
|
|
1248
|
+
export class WebrpcBadRouteError extends WebrpcError {
|
|
1249
|
+
constructor(
|
|
1250
|
+
name: string = 'WebrpcBadRoute',
|
|
1251
|
+
code: number = -2,
|
|
1252
|
+
message: string = 'bad route',
|
|
1253
|
+
status: number = 0,
|
|
1254
|
+
cause?: string
|
|
1255
|
+
) {
|
|
1256
|
+
super(name, code, message, status, cause)
|
|
1257
|
+
Object.setPrototypeOf(this, WebrpcBadRouteError.prototype)
|
|
1258
|
+
}
|
|
1259
|
+
}
|
|
1260
|
+
|
|
1261
|
+
export class WebrpcBadMethodError extends WebrpcError {
|
|
1262
|
+
constructor(
|
|
1263
|
+
name: string = 'WebrpcBadMethod',
|
|
1264
|
+
code: number = -3,
|
|
1265
|
+
message: string = 'bad method',
|
|
1266
|
+
status: number = 0,
|
|
1267
|
+
cause?: string
|
|
1268
|
+
) {
|
|
1269
|
+
super(name, code, message, status, cause)
|
|
1270
|
+
Object.setPrototypeOf(this, WebrpcBadMethodError.prototype)
|
|
1271
|
+
}
|
|
1272
|
+
}
|
|
1273
|
+
|
|
1274
|
+
export class WebrpcBadRequestError extends WebrpcError {
|
|
1275
|
+
constructor(
|
|
1276
|
+
name: string = 'WebrpcBadRequest',
|
|
1277
|
+
code: number = -4,
|
|
1278
|
+
message: string = 'bad request',
|
|
1279
|
+
status: number = 0,
|
|
1280
|
+
cause?: string
|
|
1281
|
+
) {
|
|
1282
|
+
super(name, code, message, status, cause)
|
|
1283
|
+
Object.setPrototypeOf(this, WebrpcBadRequestError.prototype)
|
|
1284
|
+
}
|
|
1285
|
+
}
|
|
1286
|
+
|
|
1287
|
+
export class WebrpcBadResponseError extends WebrpcError {
|
|
1288
|
+
constructor(
|
|
1289
|
+
name: string = 'WebrpcBadResponse',
|
|
1290
|
+
code: number = -5,
|
|
1291
|
+
message: string = 'bad response',
|
|
1292
|
+
status: number = 0,
|
|
1293
|
+
cause?: string
|
|
1294
|
+
) {
|
|
1295
|
+
super(name, code, message, status, cause)
|
|
1296
|
+
Object.setPrototypeOf(this, WebrpcBadResponseError.prototype)
|
|
1297
|
+
}
|
|
1298
|
+
}
|
|
1299
|
+
|
|
1300
|
+
export class WebrpcServerPanicError extends WebrpcError {
|
|
1301
|
+
constructor(
|
|
1302
|
+
name: string = 'WebrpcServerPanic',
|
|
1303
|
+
code: number = -6,
|
|
1304
|
+
message: string = 'server panic',
|
|
1305
|
+
status: number = 0,
|
|
1306
|
+
cause?: string
|
|
1307
|
+
) {
|
|
1308
|
+
super(name, code, message, status, cause)
|
|
1309
|
+
Object.setPrototypeOf(this, WebrpcServerPanicError.prototype)
|
|
1310
|
+
}
|
|
1311
|
+
}
|
|
1312
|
+
|
|
1313
|
+
export class WebrpcInternalErrorError extends WebrpcError {
|
|
1314
|
+
constructor(
|
|
1315
|
+
name: string = 'WebrpcInternalError',
|
|
1316
|
+
code: number = -7,
|
|
1317
|
+
message: string = 'internal error',
|
|
1318
|
+
status: number = 0,
|
|
1319
|
+
cause?: string
|
|
1320
|
+
) {
|
|
1321
|
+
super(name, code, message, status, cause)
|
|
1322
|
+
Object.setPrototypeOf(this, WebrpcInternalErrorError.prototype)
|
|
1323
|
+
}
|
|
1324
|
+
}
|
|
1325
|
+
|
|
1326
|
+
export class WebrpcClientDisconnectedError extends WebrpcError {
|
|
1327
|
+
constructor(
|
|
1328
|
+
name: string = 'WebrpcClientDisconnected',
|
|
1329
|
+
code: number = -8,
|
|
1330
|
+
message: string = 'client disconnected',
|
|
1331
|
+
status: number = 0,
|
|
1332
|
+
cause?: string
|
|
1333
|
+
) {
|
|
1334
|
+
super(name, code, message, status, cause)
|
|
1335
|
+
Object.setPrototypeOf(this, WebrpcClientDisconnectedError.prototype)
|
|
1336
|
+
}
|
|
1337
|
+
}
|
|
1338
|
+
|
|
1339
|
+
export class WebrpcStreamLostError extends WebrpcError {
|
|
1340
|
+
constructor(
|
|
1341
|
+
name: string = 'WebrpcStreamLost',
|
|
1342
|
+
code: number = -9,
|
|
1343
|
+
message: string = 'stream lost',
|
|
1344
|
+
status: number = 0,
|
|
1345
|
+
cause?: string
|
|
1346
|
+
) {
|
|
1347
|
+
super(name, code, message, status, cause)
|
|
1348
|
+
Object.setPrototypeOf(this, WebrpcStreamLostError.prototype)
|
|
1349
|
+
}
|
|
1350
|
+
}
|
|
1351
|
+
|
|
1352
|
+
export class WebrpcStreamFinishedError extends WebrpcError {
|
|
1353
|
+
constructor(
|
|
1354
|
+
name: string = 'WebrpcStreamFinished',
|
|
1355
|
+
code: number = -10,
|
|
1356
|
+
message: string = 'stream finished',
|
|
1357
|
+
status: number = 0,
|
|
1358
|
+
cause?: string
|
|
1359
|
+
) {
|
|
1360
|
+
super(name, code, message, status, cause)
|
|
1361
|
+
Object.setPrototypeOf(this, WebrpcStreamFinishedError.prototype)
|
|
1362
|
+
}
|
|
1363
|
+
}
|
|
1364
|
+
|
|
1365
|
+
// Schema errors
|
|
1366
|
+
|
|
1367
|
+
export class UnauthorizedError extends WebrpcError {
|
|
1368
|
+
constructor(
|
|
1369
|
+
name: string = 'Unauthorized',
|
|
1370
|
+
code: number = 1000,
|
|
1371
|
+
message: string = 'Unauthorized access',
|
|
1372
|
+
status: number = 0,
|
|
1373
|
+
cause?: string
|
|
1374
|
+
) {
|
|
1375
|
+
super(name, code, message, status, cause)
|
|
1376
|
+
Object.setPrototypeOf(this, UnauthorizedError.prototype)
|
|
1377
|
+
}
|
|
1378
|
+
}
|
|
1379
|
+
|
|
1380
|
+
export class PermissionDeniedError extends WebrpcError {
|
|
1381
|
+
constructor(
|
|
1382
|
+
name: string = 'PermissionDenied',
|
|
1383
|
+
code: number = 1001,
|
|
1384
|
+
message: string = 'Permission denied',
|
|
1385
|
+
status: number = 0,
|
|
1386
|
+
cause?: string
|
|
1387
|
+
) {
|
|
1388
|
+
super(name, code, message, status, cause)
|
|
1389
|
+
Object.setPrototypeOf(this, PermissionDeniedError.prototype)
|
|
1390
|
+
}
|
|
1391
|
+
}
|
|
1392
|
+
|
|
1393
|
+
export class SessionExpiredError extends WebrpcError {
|
|
1394
|
+
constructor(
|
|
1395
|
+
name: string = 'SessionExpired',
|
|
1396
|
+
code: number = 1002,
|
|
1397
|
+
message: string = 'Session expired',
|
|
1398
|
+
status: number = 0,
|
|
1399
|
+
cause?: string
|
|
1400
|
+
) {
|
|
1401
|
+
super(name, code, message, status, cause)
|
|
1402
|
+
Object.setPrototypeOf(this, SessionExpiredError.prototype)
|
|
1403
|
+
}
|
|
1404
|
+
}
|
|
1405
|
+
|
|
1406
|
+
export class MethodNotFoundError extends WebrpcError {
|
|
1407
|
+
constructor(
|
|
1408
|
+
name: string = 'MethodNotFound',
|
|
1409
|
+
code: number = 1003,
|
|
1410
|
+
message: string = 'Method not found',
|
|
1411
|
+
status: number = 0,
|
|
1412
|
+
cause?: string
|
|
1413
|
+
) {
|
|
1414
|
+
super(name, code, message, status, cause)
|
|
1415
|
+
Object.setPrototypeOf(this, MethodNotFoundError.prototype)
|
|
1416
|
+
}
|
|
1417
|
+
}
|
|
1418
|
+
|
|
1419
|
+
export class RequestConflictError extends WebrpcError {
|
|
1420
|
+
constructor(
|
|
1421
|
+
name: string = 'RequestConflict',
|
|
1422
|
+
code: number = 1004,
|
|
1423
|
+
message: string = 'Conflict with target resource',
|
|
1424
|
+
status: number = 0,
|
|
1425
|
+
cause?: string
|
|
1426
|
+
) {
|
|
1427
|
+
super(name, code, message, status, cause)
|
|
1428
|
+
Object.setPrototypeOf(this, RequestConflictError.prototype)
|
|
1429
|
+
}
|
|
1430
|
+
}
|
|
1431
|
+
|
|
1432
|
+
export class AbortedError extends WebrpcError {
|
|
1433
|
+
constructor(
|
|
1434
|
+
name: string = 'Aborted',
|
|
1435
|
+
code: number = 1005,
|
|
1436
|
+
message: string = 'Request aborted',
|
|
1437
|
+
status: number = 0,
|
|
1438
|
+
cause?: string
|
|
1439
|
+
) {
|
|
1440
|
+
super(name, code, message, status, cause)
|
|
1441
|
+
Object.setPrototypeOf(this, AbortedError.prototype)
|
|
1442
|
+
}
|
|
1443
|
+
}
|
|
1444
|
+
|
|
1445
|
+
export class TimeoutError extends WebrpcError {
|
|
1446
|
+
constructor(
|
|
1447
|
+
name: string = 'Timeout',
|
|
1448
|
+
code: number = 2000,
|
|
1449
|
+
message: string = 'Request timed out',
|
|
1450
|
+
status: number = 0,
|
|
1451
|
+
cause?: string
|
|
1452
|
+
) {
|
|
1453
|
+
super(name, code, message, status, cause)
|
|
1454
|
+
Object.setPrototypeOf(this, TimeoutError.prototype)
|
|
1455
|
+
}
|
|
1456
|
+
}
|
|
1457
|
+
|
|
1458
|
+
export class InvalidArgumentError extends WebrpcError {
|
|
1459
|
+
constructor(
|
|
1460
|
+
name: string = 'InvalidArgument',
|
|
1461
|
+
code: number = 2001,
|
|
1462
|
+
message: string = 'Invalid argument',
|
|
1463
|
+
status: number = 0,
|
|
1464
|
+
cause?: string
|
|
1465
|
+
) {
|
|
1466
|
+
super(name, code, message, status, cause)
|
|
1467
|
+
Object.setPrototypeOf(this, InvalidArgumentError.prototype)
|
|
1468
|
+
}
|
|
1469
|
+
}
|
|
1470
|
+
|
|
1471
|
+
export class UnavailableError extends WebrpcError {
|
|
1472
|
+
constructor(
|
|
1473
|
+
name: string = 'Unavailable',
|
|
1474
|
+
code: number = 2002,
|
|
1475
|
+
message: string = 'Unavailable resource',
|
|
1476
|
+
status: number = 0,
|
|
1477
|
+
cause?: string
|
|
1478
|
+
) {
|
|
1479
|
+
super(name, code, message, status, cause)
|
|
1480
|
+
Object.setPrototypeOf(this, UnavailableError.prototype)
|
|
1481
|
+
}
|
|
1482
|
+
}
|
|
1483
|
+
|
|
1484
|
+
export class QueryFailedError extends WebrpcError {
|
|
1485
|
+
constructor(
|
|
1486
|
+
name: string = 'QueryFailed',
|
|
1487
|
+
code: number = 2003,
|
|
1488
|
+
message: string = 'Query failed',
|
|
1489
|
+
status: number = 0,
|
|
1490
|
+
cause?: string
|
|
1491
|
+
) {
|
|
1492
|
+
super(name, code, message, status, cause)
|
|
1493
|
+
Object.setPrototypeOf(this, QueryFailedError.prototype)
|
|
1494
|
+
}
|
|
1495
|
+
}
|
|
1496
|
+
|
|
1497
|
+
export class NotFoundError extends WebrpcError {
|
|
1498
|
+
constructor(
|
|
1499
|
+
name: string = 'NotFound',
|
|
1500
|
+
code: number = 3000,
|
|
1501
|
+
message: string = 'Resource not found',
|
|
1502
|
+
status: number = 0,
|
|
1503
|
+
cause?: string
|
|
1504
|
+
) {
|
|
1505
|
+
super(name, code, message, status, cause)
|
|
1506
|
+
Object.setPrototypeOf(this, NotFoundError.prototype)
|
|
1507
|
+
}
|
|
1508
|
+
}
|
|
1509
|
+
|
|
1510
|
+
export class ProjectNotFoundError extends WebrpcError {
|
|
1511
|
+
constructor(
|
|
1512
|
+
name: string = 'ProjectNotFound',
|
|
1513
|
+
code: number = 3002,
|
|
1514
|
+
message: string = 'Project not found',
|
|
1515
|
+
status: number = 0,
|
|
1516
|
+
cause?: string
|
|
1517
|
+
) {
|
|
1518
|
+
super(name, code, message, status, cause)
|
|
1519
|
+
Object.setPrototypeOf(this, ProjectNotFoundError.prototype)
|
|
1520
|
+
}
|
|
1521
|
+
}
|
|
1522
|
+
|
|
1523
|
+
export class MetadataCallFailedError extends WebrpcError {
|
|
1524
|
+
constructor(
|
|
1525
|
+
name: string = 'MetadataCallFailed',
|
|
1526
|
+
code: number = 3003,
|
|
1527
|
+
message: string = 'Metadata service call failed',
|
|
1528
|
+
status: number = 0,
|
|
1529
|
+
cause?: string
|
|
1530
|
+
) {
|
|
1531
|
+
super(name, code, message, status, cause)
|
|
1532
|
+
Object.setPrototypeOf(this, MetadataCallFailedError.prototype)
|
|
1533
|
+
}
|
|
1534
|
+
}
|
|
1535
|
+
|
|
1536
|
+
export enum errors {
|
|
1537
|
+
WebrpcEndpoint = 'WebrpcEndpoint',
|
|
1538
|
+
WebrpcRequestFailed = 'WebrpcRequestFailed',
|
|
1539
|
+
WebrpcBadRoute = 'WebrpcBadRoute',
|
|
1540
|
+
WebrpcBadMethod = 'WebrpcBadMethod',
|
|
1541
|
+
WebrpcBadRequest = 'WebrpcBadRequest',
|
|
1542
|
+
WebrpcBadResponse = 'WebrpcBadResponse',
|
|
1543
|
+
WebrpcServerPanic = 'WebrpcServerPanic',
|
|
1544
|
+
WebrpcInternalError = 'WebrpcInternalError',
|
|
1545
|
+
WebrpcClientDisconnected = 'WebrpcClientDisconnected',
|
|
1546
|
+
WebrpcStreamLost = 'WebrpcStreamLost',
|
|
1547
|
+
WebrpcStreamFinished = 'WebrpcStreamFinished',
|
|
1548
|
+
Unauthorized = 'Unauthorized',
|
|
1549
|
+
PermissionDenied = 'PermissionDenied',
|
|
1550
|
+
SessionExpired = 'SessionExpired',
|
|
1551
|
+
MethodNotFound = 'MethodNotFound',
|
|
1552
|
+
RequestConflict = 'RequestConflict',
|
|
1553
|
+
Aborted = 'Aborted',
|
|
1554
|
+
Timeout = 'Timeout',
|
|
1555
|
+
InvalidArgument = 'InvalidArgument',
|
|
1556
|
+
Unavailable = 'Unavailable',
|
|
1557
|
+
QueryFailed = 'QueryFailed',
|
|
1558
|
+
NotFound = 'NotFound',
|
|
1559
|
+
ProjectNotFound = 'ProjectNotFound',
|
|
1560
|
+
MetadataCallFailed = 'MetadataCallFailed'
|
|
1561
|
+
}
|
|
1562
|
+
|
|
1563
|
+
const webrpcErrorByCode: { [code: number]: any } = {
|
|
1564
|
+
[0]: WebrpcEndpointError,
|
|
1565
|
+
[-1]: WebrpcRequestFailedError,
|
|
1566
|
+
[-2]: WebrpcBadRouteError,
|
|
1567
|
+
[-3]: WebrpcBadMethodError,
|
|
1568
|
+
[-4]: WebrpcBadRequestError,
|
|
1569
|
+
[-5]: WebrpcBadResponseError,
|
|
1570
|
+
[-6]: WebrpcServerPanicError,
|
|
1571
|
+
[-7]: WebrpcInternalErrorError,
|
|
1572
|
+
[-8]: WebrpcClientDisconnectedError,
|
|
1573
|
+
[-9]: WebrpcStreamLostError,
|
|
1574
|
+
[-10]: WebrpcStreamFinishedError,
|
|
1575
|
+
[1000]: UnauthorizedError,
|
|
1576
|
+
[1001]: PermissionDeniedError,
|
|
1577
|
+
[1002]: SessionExpiredError,
|
|
1578
|
+
[1003]: MethodNotFoundError,
|
|
1579
|
+
[1004]: RequestConflictError,
|
|
1580
|
+
[1005]: AbortedError,
|
|
1581
|
+
[2000]: TimeoutError,
|
|
1582
|
+
[2001]: InvalidArgumentError,
|
|
1583
|
+
[2002]: UnavailableError,
|
|
1584
|
+
[2003]: QueryFailedError,
|
|
1585
|
+
[3000]: NotFoundError,
|
|
1586
|
+
[3002]: ProjectNotFoundError,
|
|
1587
|
+
[3003]: MetadataCallFailedError
|
|
1588
|
+
}
|
|
1589
|
+
|
|
833
1590
|
export type Fetch = (input: RequestInfo, init?: RequestInit) => Promise<Response>
|
|
1591
|
+
|
|
1592
|
+
export interface WebrpcStreamOptions<T> extends WebrpcOptions {
|
|
1593
|
+
onMessage: (message: T) => void
|
|
1594
|
+
onError: (error: WebrpcError, reconnect: () => void) => void
|
|
1595
|
+
onOpen?: () => void
|
|
1596
|
+
onClose?: () => void
|
|
1597
|
+
}
|
|
1598
|
+
export interface WebrpcOptions {
|
|
1599
|
+
headers?: HeadersInit
|
|
1600
|
+
signal?: AbortSignal
|
|
1601
|
+
}
|