-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #308 from bigchaindb/add-type-defs
Add type defs
- Loading branch information
Showing
15 changed files
with
640 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Copyright BigchainDB GmbH and BigchainDB contributors | ||
// SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0) | ||
// Code is Apache-2.0 and docs are CC-BY-4.0 | ||
|
||
export default class Ed25519Keypair { | ||
publicKey: string; | ||
privateKey: string; | ||
|
||
constructor(seed?: Buffer); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright BigchainDB GmbH and BigchainDB contributors | ||
// SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0) | ||
// Code is Apache-2.0 and docs are CC-BY-4.0 | ||
|
||
export interface RequestConfig { | ||
headers?: Record<string, string | string[]>; | ||
jsonBody?: Record<string, any>; | ||
query?: Record<string, any>; | ||
method?: 'GET' | ' POST' | 'PUT'; | ||
urlTemplateSpec?: any[] | Record<string, any>; | ||
[key: string]: any; | ||
} | ||
|
||
export function ResponseError( | ||
message: string, | ||
status?: number, | ||
requestURI?: string | ||
): void; | ||
|
||
declare function timeout<T = Response>( | ||
ms: number, | ||
promise: Promise<T> | ||
): Promise<T>; | ||
|
||
declare function handleResponse(res: Response): Response; | ||
|
||
export default function baseRequest( | ||
url: string, | ||
config: RequestConfig = {}, | ||
requestTimeout?: number | ||
): Promise<Response>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
// Copyright BigchainDB GmbH and BigchainDB contributors | ||
// SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0) | ||
// Code is Apache-2.0 and docs are CC-BY-4.0 | ||
|
||
import type { RequestConfig } from './baseRequest'; | ||
import type { Node } from './request'; | ||
import type Transport from './transport'; | ||
import type { | ||
CreateTransaction, | ||
TransactionOperations, | ||
TransferTransaction, | ||
TransactionCommon, | ||
} from './transaction'; | ||
|
||
declare const HEADER_BLACKLIST = ['content-type']; | ||
declare const DEFAULT_NODE = 'http://localhost:9984/api/v1/'; | ||
declare const DEFAULT_TIMEOUT = 20000; // The default value is 20 seconds | ||
|
||
export interface InputNode { | ||
endpoint: string; | ||
} | ||
|
||
export enum Endpoints { | ||
blocks = 'blocks', | ||
blocksDetail = 'blocksDetail', | ||
outputs = 'outputs', | ||
transactions = 'transactions', | ||
transactionsSync = 'transactionsSync', | ||
transactionsAsync = 'transactionsAsync', | ||
transactionsCommit = 'transactionsCommit', | ||
transactionsDetail = 'transactionsDetail', | ||
assets = 'assets', | ||
metadata = 'metadata', | ||
} | ||
|
||
export interface EndpointsUrl { | ||
[Endpoints.blocks]: 'blocks'; | ||
[Endpoints.blocksDetail]: 'blocks/%(blockHeight)s'; | ||
[Endpoints.outputs]: 'outputs'; | ||
[Endpoints.transactions]: 'transactions'; | ||
[Endpoints.transactionsSync]: 'transactions?mode=sync'; | ||
[Endpoints.transactionsAsync]: 'transactions?mode=async'; | ||
[Endpoints.transactionsCommit]: 'transactions?mode=commit'; | ||
[Endpoints.transactionsDetail]: 'transactions/%(transactionId)s'; | ||
[Endpoints.assets]: 'assets'; | ||
[Endpoints.metadata]: 'metadata'; | ||
} | ||
|
||
export interface EndpointsResponse< | ||
O = TransactionOperations.CREATE, | ||
A = Record<string, any>, | ||
M = Record<string, any> | ||
> { | ||
[Endpoints.blocks]: number[]; | ||
[Endpoints.blocksDetail]: { | ||
height: number; | ||
transactions: (CreateTransaction | TransferTransaction)[]; | ||
}; | ||
[Endpoints.outputs]: { | ||
transaction_id: string; | ||
output_index: number; | ||
}[]; | ||
[Endpoints.transactions]: O extends TransactionOperations.CREATE | ||
? CreateTransaction[] | ||
: O extends TransactionOperations.TRANSFER | ||
? TransferTransaction[] | ||
: (CreateTransaction | TransferTransaction)[]; | ||
[Endpoints.transactionsSync]: O extends TransactionOperations.CREATE | ||
? CreateTransaction<A, M> | ||
: TransferTransaction<M>; | ||
[Endpoints.transactionsAsync]: O extends TransactionOperations.CREATE | ||
? CreateTransaction<A, M> | ||
: TransferTransaction<M>; | ||
[Endpoints.transactionsCommit]: O extends TransactionOperations.CREATE | ||
? CreateTransaction<A, M> | ||
: TransferTransaction<M>; | ||
[Endpoints.transactionsDetail]: O extends TransactionOperations.CREATE | ||
? CreateTransaction<A, M> | ||
: TransferTransaction<M>; | ||
[Endpoints.assets]: { id: string; data: Record<string, any> }[]; | ||
[Endpoints.metadata]: { id: string; metadata: Record<string, any> }[]; | ||
} | ||
|
||
export default class Connection { | ||
private transport: Transport; | ||
private normalizedNodes: Node[]; | ||
private headers: Record<string, string | string[]>; | ||
|
||
constructor( | ||
nodes: string | InputNode | (string | InputNode)[], | ||
headers: Record<string, string | string[]> = {}, | ||
timeout?: number | ||
); | ||
|
||
static normalizeNode( | ||
node: string | InputNode, | ||
headers: Record<string, string | string[]> | ||
): Node; | ||
|
||
static getApiUrls<E = Endpoint>(endpoint: E): EndpointsUrl[E]; | ||
|
||
private _req<E = Endpoint, O = Record<string, any>>( | ||
path: EndpointsUrl[E], | ||
options: RequestConfig = {} | ||
): Promise<O>; | ||
|
||
getBlock( | ||
blockHeight: number | string | ||
): Promise<EndpointsResponse[Endpoints.blocksDetail]>; | ||
|
||
getTransaction<O = TransactionOperations.CREATE>( | ||
transactionId: string | ||
): Promise<EndpointsResponse<O>[Endpoints.transactionsDetail]>; | ||
|
||
listBlocks(transactionId: string): Promise<EndpointsResponse[Endpoints.blocks]>; | ||
|
||
listOutputs( | ||
publicKey: string, | ||
spent?: boolean | ||
): Promise<EndpointsResponse[Endpoints.outputs]>; | ||
|
||
listTransactions( | ||
assetId: string, | ||
operation?: TransactionOperations | ||
): Promise<EndpointsResponse<typeof operation>[Endpoints.transactions]>; | ||
|
||
postTransaction< | ||
O = TransactionOperations.CREATE, | ||
A = Record<string, any>, | ||
M = Record<string, any> | ||
>( | ||
transaction: TransactionCommon<O> | ||
): Promise<EndpointsResponse<O, A, M>[Endpoints.transactionsCommit]>; | ||
|
||
postTransactionSync< | ||
O = TransactionOperations.CREATE, | ||
A = Record<string, any>, | ||
M = Record<string, any> | ||
>( | ||
transaction: TransactionCommon<O> | ||
): Promise<EndpointsResponse<O, A, M>[Endpoints.transactionsSync]>; | ||
|
||
postTransactionAsync< | ||
O = TransactionOperations.CREATE, | ||
A = Record<string, any>, | ||
M = Record<string, any> | ||
>( | ||
transaction: TransactionCommon<O> | ||
): Promise<EndpointsResponse<O, A, M>[Endpoints.transactionsAsync]>; | ||
|
||
postTransactionCommit< | ||
O = TransactionOperations.CREATE, | ||
A = Record<string, any>, | ||
M = Record<string, any> | ||
>( | ||
transaction: TransactionCommon<O> | ||
): Promise<EndpointsResponse<O, A, M>[Endpoints.transactionsCommit]>; | ||
|
||
searchAssets(search: string): Promise<EndpointsResponse[Endpoints.assets]>; | ||
|
||
searchMetadata(search: string): Promise<EndpointsResponse[Endpoints.metadata]>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright BigchainDB GmbH and BigchainDB contributors | ||
// SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0) | ||
// Code is Apache-2.0 and docs are CC-BY-4.0 | ||
|
||
import Ed25519Keypair from './Ed25519Keypair' | ||
import Connection from './connection' | ||
import Transaction from './transaction' | ||
import ccJsonLoad from './utils/ccJsonLoad' | ||
import ccJsonify from './utils/ccJsonify' | ||
|
||
export { ccJsonLoad, ccJsonify, Connection, Ed25519Keypair, Transaction } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright BigchainDB GmbH and BigchainDB contributors | ||
// SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0) | ||
// Code is Apache-2.0 and docs are CC-BY-4.0 | ||
|
||
import type { RequestConfig } from './baseRequest'; | ||
|
||
export interface Node { | ||
endpoint: string; | ||
headers: Record<string, string | string[]>; | ||
} | ||
|
||
export default class Request { | ||
private node: Node; | ||
private backoffTime: number; | ||
private retries: number; | ||
private connectionError?: Error; | ||
|
||
constructor(node: Node); | ||
|
||
async request<O = Record<string, any>>( | ||
urlPath: string, | ||
config: RequestConfig = {}, | ||
timeout?: number, | ||
maxBackoffTime?: number | ||
): Promise<O>; | ||
|
||
updateBackoffTime(maxBackoffTime: number): void; | ||
|
||
getBackoffTimedelta(): number; | ||
|
||
static sleep(ms: number): void; | ||
} |
Oops, something went wrong.