Skip to content

Commit 92602c1

Browse files
authored
Merge pull request #457 from decentraliser/query-params
Query params arguments in constructor, fixes #456
2 parents 42fb1dd + 8f4178d commit 92602c1

File tree

6 files changed

+53
-37
lines changed

6 files changed

+53
-37
lines changed

e2e/infrastructure/AccountHttp.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ describe('AccountHttp', () => {
228228
describe('transactions', () => {
229229
it('should call transactions successfully by type', async () => {
230230
const transactions = await accountRepository.getAccountTransactions(
231-
publicAccount.address, new QueryParams(), new TransactionFilter().setType([TransactionType.TRANSFER])).toPromise();
231+
publicAccount.address, new QueryParams(), new TransactionFilter({ types: [TransactionType.TRANSFER] })).toPromise();
232232
expect(transactions.length).to.be.greaterThan(0);
233233
transactions.forEach((t) => {
234234
expect(t.type).to.be.eq(TransactionType.TRANSFER);

e2e/infrastructure/BlockHttp.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ describe('BlockHttp', () => {
112112

113113
it('should return block transactions data given height with paginated transactionId', async () => {
114114
const transactions = await blockRepository.getBlockTransactions(UInt64.fromUint(1),
115-
new QueryParams().setPageSize(10).setId(nextId)).toPromise();
115+
new QueryParams({ pageSize: 10, id: nextId})).toPromise();
116116
expect(transactions[0].transactionInfo!.id).to.be.equal(firstId);
117117
expect(transactions.length).to.be.greaterThan(0);
118118
});

src/infrastructure/Http.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export abstract class Http {
5959

6060
transactionFilter(filter?: TransactionFilter): any {
6161
return {
62-
type: filter ? filter.convertCSV(filter.type) : undefined,
62+
type: filter ? filter.convertCSV(filter.types) : undefined,
6363
};
6464
}
6565

src/infrastructure/QueryParams.ts

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,35 +34,49 @@ export class QueryParams {
3434
* Page size between 10 and 100, otherwise 10
3535
*/
3636
public pageSize = 10;
37-
/**
38-
* Id after which we want objects to be returned
39-
*/
40-
public id?: string;
4137
/**
4238
* Order of transactions.
4339
* DESC. Newer to older.
4440
* ASC. Older to newer.
4541
*/
42+
4643
public order: Order = Order.DESC;
4744

4845
/**
49-
* Constructor
46+
* Id after which we want objects to be returned
5047
*/
51-
constructor() {
52-
}
53-
54-
public setPageSize(pageSize: number): QueryParams {
55-
this.pageSize = (pageSize >= 10 && pageSize <= 100) ? pageSize : 10;
56-
return this;
57-
}
48+
public id?: string;
5849

59-
public setId(id?: string): QueryParams {
60-
this.id = id;
61-
return this;
50+
/**
51+
* Constructor
52+
* @param {{
53+
* pageSize?: number,
54+
* order?: Order,
55+
* id?: string;
56+
* }} configuration arguments
57+
*/
58+
constructor(args?: {
59+
pageSize?: number,
60+
order?: Order,
61+
id?: string;
62+
}) {
63+
if (args) {
64+
if (args.pageSize) this.setPageSize(args.pageSize)
65+
if (args.order) this.order = args.order
66+
if (args.id) this.id = args.id
67+
}
6268
}
6369

64-
public setOrder(order: Order = Order.DESC): QueryParams {
65-
this.order = order;
66-
return this;
70+
/**
71+
* Set page size
72+
* @private
73+
* @param {number} [pageSize]
74+
* @returns {void}
75+
*/
76+
private setPageSize(pageSize?: number): void {
77+
if (pageSize && pageSize > 100) {
78+
throw new Error('The page size has to be between 10 and 100')
79+
}
80+
this.pageSize = pageSize || 10
6781
}
6882
}

src/infrastructure/TransactionFilter.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616

1717
import { TransactionType } from '../model/transaction/TransactionType';
18-
import { QueryParams } from './QueryParams';
1918

2019
/**
2120
* The Transaction filter class
@@ -24,27 +23,29 @@ export class TransactionFilter {
2423
/**
2524
* Transaction type list
2625
*/
27-
public type?: TransactionType[];
26+
readonly types?: TransactionType[];
27+
2828
/**
2929
* Constructor
30+
* @param {{
31+
* type: TransactionType[],
32+
* }} [args]
3033
*/
31-
constructor() {
32-
}
33-
34-
public setType(type?: TransactionType[]): TransactionFilter {
35-
this.type = type;
36-
return this;
34+
constructor(args?: {
35+
types?: TransactionType[],
36+
}) {
37+
if (args && args.types) this.types = args.types
3738
}
3839

3940
/**
40-
* Return comma seperated list
41-
* @param type Transaction type list
41+
* Return comma separated list
42+
* @param types Transaction type list
4243
*/
43-
public convertCSV(type?: TransactionType[]): string | undefined {
44-
if (!type || type.length === 0) {
44+
public convertCSV(types?: TransactionType[]): string | undefined {
45+
if (!types || types.length === 0) {
4546
return undefined;
4647
} else {
47-
return type.map((t) => t.valueOf().toString()).join(',');
48+
return types.map((t) => t.valueOf().toString()).join(',');
4849
}
4950
}
5051
}

test/infrastructure/TransactionFilter.spec.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ import { TransactionType } from '../../src/model/transaction/TransactionType';
2020

2121
describe('TransactionFilter', () => {
2222
it('should return correct query param', () => {
23-
const param = new TransactionFilter()
24-
.setType([TransactionType.TRANSFER, TransactionType.ACCOUNT_LINK]);
23+
const param = new TransactionFilter({
24+
types: [TransactionType.TRANSFER, TransactionType.ACCOUNT_LINK],
25+
})
2526

26-
expect(param.convertCSV(param.type)).to.be.equal('16724,16716');
27+
expect(param.convertCSV(param.types)).to.be.equal('16724,16716');
2728
});
2829
});

0 commit comments

Comments
 (0)