|
| 1 | +// Copyright BigchainDB GmbH and BigchainDB contributors |
| 2 | +// SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0) |
| 3 | +// Code is Apache-2.0 and docs are CC-BY-4.0 |
| 4 | + |
| 5 | +import type { RequestConfig } from './baseRequest'; |
| 6 | +import type { Node } from './request'; |
| 7 | +import type Transport from './transport'; |
| 8 | +import type { |
| 9 | + CreateTransaction, |
| 10 | + TransactionOperations, |
| 11 | + TransferTransaction, |
| 12 | + TransactionCommon, |
| 13 | +} from './transaction'; |
| 14 | + |
| 15 | +declare const HEADER_BLACKLIST = ['content-type']; |
| 16 | +declare const DEFAULT_NODE = 'http://localhost:9984/api/v1/'; |
| 17 | +declare const DEFAULT_TIMEOUT = 20000; // The default value is 20 seconds |
| 18 | + |
| 19 | +export interface InputNode { |
| 20 | + endpoint: string; |
| 21 | +} |
| 22 | + |
| 23 | +export enum Endpoints { |
| 24 | + blocks = 'blocks', |
| 25 | + blocksDetail = 'blocksDetail', |
| 26 | + outputs = 'outputs', |
| 27 | + transactions = 'transactions', |
| 28 | + transactionsSync = 'transactionsSync', |
| 29 | + transactionsAsync = 'transactionsAsync', |
| 30 | + transactionsCommit = 'transactionsCommit', |
| 31 | + transactionsDetail = 'transactionsDetail', |
| 32 | + assets = 'assets', |
| 33 | + metadata = 'metadata', |
| 34 | +} |
| 35 | + |
| 36 | +export interface EndpointsUrl { |
| 37 | + [Endpoints.blocks]: 'blocks'; |
| 38 | + [Endpoints.blocksDetail]: 'blocks/%(blockHeight)s'; |
| 39 | + [Endpoints.outputs]: 'outputs'; |
| 40 | + [Endpoints.transactions]: 'transactions'; |
| 41 | + [Endpoints.transactionsSync]: 'transactions?mode=sync'; |
| 42 | + [Endpoints.transactionsAsync]: 'transactions?mode=async'; |
| 43 | + [Endpoints.transactionsCommit]: 'transactions?mode=commit'; |
| 44 | + [Endpoints.transactionsDetail]: 'transactions/%(transactionId)s'; |
| 45 | + [Endpoints.assets]: 'assets'; |
| 46 | + [Endpoints.metadata]: 'metadata'; |
| 47 | +} |
| 48 | + |
| 49 | +export interface EndpointsResponse< |
| 50 | + O = TransactionOperations.CREATE, |
| 51 | + A = Record<string, any>, |
| 52 | + M = Record<string, any> |
| 53 | +> { |
| 54 | + [Endpoints.blocks]: number[]; |
| 55 | + [Endpoints.blocksDetail]: { |
| 56 | + height: number; |
| 57 | + transactions: (CreateTransaction | TransferTransaction)[]; |
| 58 | + }; |
| 59 | + [Endpoints.outputs]: { |
| 60 | + transaction_id: string; |
| 61 | + output_index: number; |
| 62 | + }[]; |
| 63 | + [Endpoints.transactions]: O extends TransactionOperations.CREATE |
| 64 | + ? CreateTransaction[] |
| 65 | + : O extends TransactionOperations.TRANSFER |
| 66 | + ? TransferTransaction[] |
| 67 | + : (CreateTransaction | TransferTransaction)[]; |
| 68 | + [Endpoints.transactionsSync]: O extends TransactionOperations.CREATE |
| 69 | + ? CreateTransaction<A, M> |
| 70 | + : TransferTransaction<M>; |
| 71 | + [Endpoints.transactionsAsync]: O extends TransactionOperations.CREATE |
| 72 | + ? CreateTransaction<A, M> |
| 73 | + : TransferTransaction<M>; |
| 74 | + [Endpoints.transactionsCommit]: O extends TransactionOperations.CREATE |
| 75 | + ? CreateTransaction<A, M> |
| 76 | + : TransferTransaction<M>; |
| 77 | + [Endpoints.transactionsDetail]: O extends TransactionOperations.CREATE |
| 78 | + ? CreateTransaction<A, M> |
| 79 | + : TransferTransaction<M>; |
| 80 | + [Endpoints.assets]: { id: string; data: Record<string, any> }[]; |
| 81 | + [Endpoints.metadata]: { id: string; metadata: Record<string, any> }[]; |
| 82 | +} |
| 83 | + |
| 84 | +export default class Connection { |
| 85 | + private transport: Transport; |
| 86 | + private normalizedNodes: Node[]; |
| 87 | + private headers: Record<string, string | string[]>; |
| 88 | + |
| 89 | + constructor( |
| 90 | + nodes: string | InputNode | (string | InputNode)[], |
| 91 | + headers: Record<string, string | string[]> = {}, |
| 92 | + timeout?: number |
| 93 | + ); |
| 94 | + |
| 95 | + static normalizeNode( |
| 96 | + node: string | InputNode, |
| 97 | + headers: Record<string, string | string[]> |
| 98 | + ): Node; |
| 99 | + |
| 100 | + static getApiUrls<E = Endpoint>(endpoint: E): EndpointsUrl[E]; |
| 101 | + |
| 102 | + private _req<E = Endpoint, O = Record<string, any>>( |
| 103 | + path: EndpointsUrl[E], |
| 104 | + options: RequestConfig = {} |
| 105 | + ): Promise<O>; |
| 106 | + |
| 107 | + getBlock( |
| 108 | + blockHeight: number | string |
| 109 | + ): Promise<EndpointsUrl[Endpoints.blocksDetail]>; |
| 110 | + |
| 111 | + getTransaction<O = TransactionOperations.CREATE>( |
| 112 | + transactionId: string |
| 113 | + ): Promise<EndpointsUrl<O>[Endpoints.transactionsDetail]>; |
| 114 | + |
| 115 | + listBlocks(transactionId: string): Promise<EndpointsUrl[Endpoints.blocks]>; |
| 116 | + |
| 117 | + listOutputs( |
| 118 | + publicKey: string, |
| 119 | + spent?: boolean |
| 120 | + ): Promise<EndpointsUrl[Endpoints.outputs]>; |
| 121 | + |
| 122 | + listTransactions<O = TransactionOperations.CREATE>( |
| 123 | + assetId: string, |
| 124 | + operation: O |
| 125 | + ): Promise<EndpointsUrl<O>[Endpoints.transactions]>; |
| 126 | + |
| 127 | + postTransaction< |
| 128 | + O = TransactionOperations.CREATE, |
| 129 | + A = Record<string, any>, |
| 130 | + M = Record<string, any> |
| 131 | + >( |
| 132 | + transaction: TransactionCommon<O> |
| 133 | + ): Promise<EndpointsUrl<O, A, M>[Endpoints.transactionsCommit]>; |
| 134 | + |
| 135 | + postTransactionSync< |
| 136 | + O = TransactionOperations.CREATE, |
| 137 | + A = Record<string, any>, |
| 138 | + M = Record<string, any> |
| 139 | + >( |
| 140 | + transaction: TransactionCommon<O> |
| 141 | + ): Promise<EndpointsUrl<O, A, M>[Endpoints.transactionsSync]>; |
| 142 | + |
| 143 | + postTransactionAsync< |
| 144 | + O = TransactionOperations.CREATE, |
| 145 | + A = Record<string, any>, |
| 146 | + M = Record<string, any> |
| 147 | + >( |
| 148 | + transaction: TransactionCommon<O> |
| 149 | + ): Promise<EndpointsUrl<O, A, M>[Endpoints.transactionsAsync]>; |
| 150 | + |
| 151 | + postTransactionCommit< |
| 152 | + O = TransactionOperations.CREATE, |
| 153 | + A = Record<string, any>, |
| 154 | + M = Record<string, any> |
| 155 | + >( |
| 156 | + transaction: TransactionCommon<O> |
| 157 | + ): Promise<EndpointsUrl<O, A, M>[Endpoints.transactionsCommit]>; |
| 158 | + |
| 159 | + searchAssets(search: string): Promise<EndpointsUrl[Endpoints.assets]>; |
| 160 | + |
| 161 | + searchMetadata(search: string): Promise<EndpointsUrl[Endpoints.metadata]>; |
| 162 | +} |
0 commit comments