Skip to content

Commit 858acf2

Browse files
committed
fix: add type definitions
Signed-off-by: getlarge <ed@getlarge.eu>
1 parent d26f667 commit 858acf2

11 files changed

+558
-0
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
],
1616
"main": "./dist/node/index.js",
1717
"browser": "./dist/browser/bigchaindb-driver.cjs2.min.js",
18+
"types": "./types/index.d.ts",
1819
"sideEffects": false,
1920
"scripts": {
2021
"lint": "eslint .",

types/Ed25519Keypair.d.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
export default class Ed25519Keypair {
6+
publicKey: string;
7+
privateKey: string;
8+
9+
constructor(seed?: Buffer);
10+
}

types/baseRequest.d.ts

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
export interface RequestConfig {
6+
headers?: Record<string, string | string[]>;
7+
jsonBody?: Record<string, any>;
8+
query?: Record<string, any>;
9+
method?: 'GET' | ' POST' | 'PUT';
10+
urlTemplateSpec?: any[] | Record<string, any>;
11+
[key: string]: any;
12+
}
13+
14+
export function ResponseError(
15+
message: string,
16+
status?: number,
17+
requestURI?: string
18+
): void;
19+
20+
declare function timeout<T = Response>(
21+
ms: number,
22+
promise: Promise<T>
23+
): Promise<T>;
24+
25+
declare function handleResponse(res: Response): Response;
26+
27+
export default function baseRequest(
28+
url: string,
29+
config: RequestConfig = {},
30+
requestTimeout?: number
31+
): Promise<Response>;

types/connection.d.ts

+162
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
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+
}

types/index.d.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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 Ed25519Keypair from './Ed25519Keypair'
6+
import Connection from './connection'
7+
import Transaction from './transaction'
8+
import ccJsonLoad from './utils/ccJsonLoad'
9+
import ccJsonify from './utils/ccJsonify'
10+
11+
export { ccJsonLoad, ccJsonify, Connection, Ed25519Keypair, Transaction }

types/request.d.ts

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
7+
export interface Node {
8+
endpoint: string;
9+
headers: Record<string, string | string[]>;
10+
}
11+
12+
export default class Request {
13+
private node: Node;
14+
private backoffTime: number;
15+
private retries: number;
16+
private connectionError?: Error;
17+
18+
constructor(node: Node);
19+
20+
async request<O = Record<string, any>>(
21+
urlPath: string,
22+
config: RequestConfig = {},
23+
timeout?: number,
24+
maxBackoffTime?: number
25+
): Promise<O>;
26+
27+
updateBackoffTime(maxBackoffTime: number): void;
28+
29+
getBackoffTimedelta(): number;
30+
31+
static sleep(ms: number): void;
32+
}

types/sanitize.d.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
declare type FilterFn = (val: any, key?: string) => void;
6+
7+
declare function filterFromObject<I = Record<string, any>>(
8+
obj: I,
9+
filter: Array | FilterFn,
10+
conf: { isInclusion?: boolean } = {}
11+
): Partial<I>;
12+
13+
declare function applyFilterOnObject<I = Record<string, any>>(
14+
obj: I,
15+
filterFn?: FilterFn
16+
): Partial<I>;
17+
18+
declare function selectFromObject<I = Record<string, any>>(
19+
obj: I,
20+
filter: Array | FilterFn
21+
): Partial<I>;
22+
23+
export default function sanitize<I = Record<string, any>>(
24+
obj: I
25+
): Partial<I> | I;

0 commit comments

Comments
 (0)