Skip to content

Commit 6096f3b

Browse files
authored
fix: add and adjust various transaction typings (#107)
1 parent 2ce3f92 commit 6096f3b

6 files changed

Lines changed: 54 additions & 21 deletions

File tree

__tests__/resources/transactions.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe("API - 2.0 - Resources - Transactions", () => {
1111
});
1212

1313
it("should call \"create\" method", async () => {
14-
const response = await resource.create([]);
14+
const response = await resource.create({ transactions: [] });
1515

1616
expect(response.status).toBe(200);
1717
});

src/connection.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ export class Connection {
1414
}
1515

1616
public api<T extends AvailableResourcesName>(name: T) {
17-
const selectedResourceClass = Resources[name];
17+
// Convert to lower case to be backward-compatible
18+
const selectedResourceClass = Resources[name.toLowerCase() as AvailableResourcesName];
1819
return new selectedResourceClass(this) as AvailableResource<T>;
1920
}
2021

@@ -35,10 +36,6 @@ export class Connection {
3536
private async sendRequest<T>(method: string, url: string, opts?: Record<string, any>): Promise<IResponse<T>> {
3637
opts = { ...this.opts, ...(opts || {}) };
3738

38-
if (opts.body && typeof opts !== "string") {
39-
opts.body = JSON.stringify(opts.body);
40-
}
41-
4239
// Do not retry unless explicitly stated.
4340
if (!opts.retry) {
4441
opts.retry = { retries: 0 };
@@ -49,7 +46,7 @@ export class Connection {
4946
}
5047

5148
try {
52-
const response = await ky(`${this.host}/${url}`, { ...opts, method });
49+
const response = await ky(`${this.host}/${url}`, { ...opts, method, json: opts.body });
5350

5451
return {
5552
body: await response.json(),

src/interfaces.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export interface IResponse<T> {
44
status: number;
55
}
66

7-
export type DataResponse<T> = { data: T };
7+
export type DataResponse<T> = { data: T; errors: any };
88
export interface PaginableResponse {
99
meta: {
1010
count: number;

src/resources/index.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ import { Wallets } from "./wallets";
1212

1313
// tslint:disable-next-line: variable-name
1414
export const Resources = {
15-
Blocks,
16-
Bridgechains,
17-
Businesses,
18-
Delegates,
19-
Locks,
20-
Node,
21-
Peers,
22-
Rounds,
23-
Transactions,
24-
Votes,
25-
Wallets,
15+
blocks: Blocks,
16+
bridgechains: Bridgechains,
17+
businesses: Businesses,
18+
delegates: Delegates,
19+
locks: Locks,
20+
node: Node,
21+
peers: Peers,
22+
rounds: Rounds,
23+
transactions: Transactions,
24+
votes: Votes,
25+
wallets: Wallets,
2626
};
2727

2828
export type AvailableResourcesName = keyof typeof Resources;

src/resources/transactions.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ export class Transactions extends Resource {
3131
*
3232
* @param payload The list of transactions to create.
3333
*/
34-
public async create(payload: object[]): Promise<ApiResponse<CreateTransactionApiResponse> & { errors?: any }> {
34+
public async create(
35+
payload: { transactions: object[] } & Record<string, any>,
36+
): Promise<ApiResponse<CreateTransactionApiResponse> & { errors?: any }> {
3537
return this.sendPost("transactions", payload);
3638
}
3739

src/resourcesTypes/transactions.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export interface Transaction {
1111
sender: string;
1212
senderPublicKey: string;
1313
recipient: string;
14-
asset?: Record<string, any>;
14+
asset?: TransactionAssets;
1515
signature: string;
1616
signSignature?: string;
1717
vendorField?: string;
@@ -24,6 +24,35 @@ export interface Transaction {
2424
nonce: string;
2525
}
2626

27+
export type TransactionAssets = {
28+
ipfs?: string;
29+
votes?: string[];
30+
delegate?: {
31+
username: string;
32+
};
33+
signature?: {
34+
publicKey: string;
35+
};
36+
multiSignature?: {
37+
publicKeys: string[];
38+
min: string;
39+
};
40+
lock?: {
41+
secretHash: string;
42+
expiration: {
43+
type: number;
44+
value: number;
45+
};
46+
};
47+
claim?: {
48+
lockTransactionId: string;
49+
unlockSecret: string;
50+
};
51+
refund?: {
52+
lockTransactionId: string;
53+
};
54+
} & Record<string, any>;
55+
2756
export interface CreateTransactionApiResponse {
2857
accept: string[];
2958
broadcast: string[];
@@ -94,11 +123,16 @@ export interface SearchTransactionsApiBody extends ApiBody {
94123
blockId?: string;
95124
type?: number;
96125
version?: number;
126+
network?: number;
97127
senderPublicKey?: string;
98128
senderId?: string;
99129
recipientId?: string;
100130
ownerId?: string;
101131
vendorFieldHex?: string;
132+
asset?: TransactionAssets;
133+
signature?: string;
134+
signatures?: string[];
135+
MultiSignatureAddress?: string;
102136
timestamp?: {
103137
from?: number;
104138
to?: number;

0 commit comments

Comments
 (0)