Skip to content

Commit d7b5aee

Browse files
authored
OpenAPI Enum type mapper (#609)
* Fixed #608 - Added enumMapper - Added wrapper enums for Order, BlockOrderBy and MeklePosition * Added sanity check
1 parent 25bc515 commit d7b5aee

24 files changed

+222
-37
lines changed

src/core/utils/DtoMapping.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,12 @@ export class DtoMapping {
7070
public static assign<T>(object: T, attributes: any): T {
7171
return Object.assign({ __proto__: Object.getPrototypeOf(object) }, object, attributes);
7272
}
73+
74+
/**
75+
* Map one enum type to another by value
76+
* @param value enum value to be mapped
77+
*/
78+
public static mapEnum<E1, E2>(value: E1 | undefined): E2 {
79+
return (value as unknown) as E2;
80+
}
7381
}

src/infrastructure/BlockHttp.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { Http } from './Http';
2626
import { BlockSearchCriteria } from './searchCriteria/BlockSearchCriteria';
2727
import { Page } from './Page';
2828
import { Address } from '../model/account/Address';
29+
import { DtoMapping } from '../core/utils/DtoMapping';
2930

3031
/**
3132
* Blockchain http repository.
@@ -71,8 +72,8 @@ export class BlockHttp extends Http implements BlockRepository {
7172
criteria.pageSize,
7273
criteria.pageNumber,
7374
criteria.offset,
74-
criteria.order,
75-
criteria.orderBy,
75+
DtoMapping.mapEnum(criteria.order),
76+
DtoMapping.mapEnum(criteria.orderBy),
7677
),
7778
(body) => super.toPage(body.pagination, body.data, this.toBlockInfo),
7879
);
@@ -129,7 +130,10 @@ export class BlockHttp extends Http implements BlockRepository {
129130
public getMerkleTransaction(height: UInt64, hash: string): Observable<MerkleProofInfo> {
130131
return this.call(
131132
this.blockRoutesApi.getMerkleTransaction(height.toString(), hash),
132-
(body) => new MerkleProofInfo(body.merklePath!.map((payload) => new MerklePathItem(payload.position, payload.hash))),
133+
(body) =>
134+
new MerkleProofInfo(
135+
body.merklePath!.map((payload) => new MerklePathItem(DtoMapping.mapEnum(payload.position), payload.hash)),
136+
),
133137
);
134138
}
135139
}

src/infrastructure/Listener.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import { TransactionStatusError } from '../model/transaction/TransactionStatusEr
2727
import { IListener } from './IListener';
2828
import { NamespaceRepository } from './NamespaceRepository';
2929
import { CreateTransactionFromDTO } from './transaction/CreateTransactionFromDTO';
30-
import { BlockInfoDTO } from 'symbol-openapi-typescript-node-client/dist/model/blockInfoDTO';
30+
import { BlockInfoDTO } from 'symbol-openapi-typescript-node-client';
3131
import { NewBlock } from '../model/blockchain/NewBlock';
3232
import { PublicAccount } from '../model/account/PublicAccount';
3333
import { UInt64 } from '../model/UInt64';

src/infrastructure/MosaicHttp.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { MosaicSearchCriteria } from './searchCriteria/MosaicSearchCriteria';
2727
import { Page } from './Page';
2828
import { MosaicRoutesApi, MosaicIds, MosaicInfoDTO } from 'symbol-openapi-typescript-node-client';
2929
import { Address } from '../model/account/Address';
30+
import { DtoMapping } from '../core/utils/DtoMapping';
3031

3132
/**
3233
* Mosaic http repository.
@@ -92,7 +93,7 @@ export class MosaicHttp extends Http implements MosaicRepository {
9293
criteria.pageSize,
9394
criteria.pageNumber,
9495
criteria.offset,
95-
criteria.order,
96+
DtoMapping.mapEnum(criteria.order),
9697
),
9798
(body) => super.toPage(body.pagination, body.data, this.toMosaicInfo, networkType),
9899
),

src/infrastructure/QueryParams.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { Order } from 'symbol-openapi-typescript-node-client/dist/model/order';
17+
import { Order } from './searchCriteria/Order';
1818

1919
/**
2020
* The query params structure describes pagination params for requests.

src/infrastructure/ReceiptHttp.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { UInt64 } from '../model/UInt64';
2525
import { Http } from './Http';
2626
import { CreateStatementFromDTO } from './receipt/CreateReceiptFromDTO';
2727
import { ReceiptRepository } from './ReceiptRepository';
28+
import { DtoMapping } from '../core/utils/DtoMapping';
2829

2930
/**
3031
* Receipt http repository.
@@ -38,12 +39,6 @@ export class ReceiptHttp extends Http implements ReceiptRepository {
3839
*/
3940
private readonly receiptRoutesApi: ReceiptRoutesApi;
4041

41-
/**
42-
* @internal
43-
* network type for the mappings.
44-
*/
45-
private readonly networkTypeObservable: Observable<NetworkType>;
46-
4742
/**
4843
* Constructor
4944
* @param url
@@ -52,7 +47,6 @@ export class ReceiptHttp extends Http implements ReceiptRepository {
5247
constructor(url: string, networkType?: NetworkType | Observable<NetworkType>) {
5348
super(url);
5449
this.receiptRoutesApi = new ReceiptRoutesApi(url);
55-
this.networkTypeObservable = this.createNetworkTypeObservable(networkType);
5650
this.receiptRoutesApi.useQuerystring = true;
5751
}
5852

@@ -68,7 +62,12 @@ export class ReceiptHttp extends Http implements ReceiptRepository {
6862
*/
6963
public getMerkleReceipts(height: UInt64, hash: string): Observable<MerkleProofInfo> {
7064
return observableFrom(this.receiptRoutesApi.getMerkleReceipts(height.toString(), hash)).pipe(
71-
map(({ body }) => new MerkleProofInfo(body.merklePath!.map((payload) => new MerklePathItem(payload.position, payload.hash)))),
65+
map(
66+
({ body }) =>
67+
new MerkleProofInfo(
68+
body.merklePath!.map((payload) => new MerklePathItem(DtoMapping.mapEnum(payload.position), payload.hash)),
69+
),
70+
),
7271
catchError((error) => throwError(this.errorHandling(error))),
7372
);
7473
}

src/infrastructure/TransactionHttp.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import { TransactionSearchCriteria } from './searchCriteria/TransactionSearchCri
3838
import { Page } from './Page';
3939
import { TransactionGroup } from './TransactionGroup';
4040
import http = require('http');
41+
import { DtoMapping } from '../core/utils/DtoMapping';
4142

4243
/**
4344
* Transaction http repository.
@@ -237,7 +238,7 @@ export class TransactionHttp extends Http implements TransactionRepository {
237238
criteria.pageSize,
238239
criteria.pageNumber,
239240
criteria.offset,
240-
criteria.order,
241+
DtoMapping.mapEnum(criteria.order),
241242
);
242243
case TransactionGroup.Unconfirmed:
243244
return this.transactionRoutesApi.searchUnconfirmedTransactions(
@@ -250,7 +251,7 @@ export class TransactionHttp extends Http implements TransactionRepository {
250251
criteria.pageSize,
251252
criteria.pageNumber,
252253
criteria.offset,
253-
criteria.order,
254+
DtoMapping.mapEnum(criteria.order),
254255
);
255256
case TransactionGroup.Partial:
256257
return this.transactionRoutesApi.searchPartialTransactions(
@@ -263,7 +264,7 @@ export class TransactionHttp extends Http implements TransactionRepository {
263264
criteria.pageSize,
264265
criteria.pageNumber,
265266
criteria.offset,
266-
criteria.order,
267+
DtoMapping.mapEnum(criteria.order),
267268
);
268269
}
269270
}

src/infrastructure/infrastructure.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,5 @@ export * from './paginationStreamer/TransactionPaginationStreamer';
5959
export * from './TransactionStatusHttp';
6060
export * from './TransactionStatusRepository';
6161
export * from './TransactionGroup';
62+
export * from './searchCriteria/BlockOrderBy';
63+
export * from './searchCriteria/Order';
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright 2020 NEM
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
export enum BlockOrderBy {
17+
Id = 'id',
18+
Height = 'height',
19+
}

src/infrastructure/searchCriteria/BlockSearchCriteria.ts

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

1717
import { SearchCriteria } from './SearchCriteria';
18-
import { BlockOrderByEnum } from 'symbol-openapi-typescript-node-client/dist/model/blockOrderByEnum';
18+
import { BlockOrderBy } from './BlockOrderBy';
1919

2020
/**
2121
* Defines the params used to search blocks. With this criteria, you can sort and filter
@@ -35,5 +35,5 @@ export interface BlockSearchCriteria extends SearchCriteria {
3535
/**
3636
* Order by block id or height. (optional)
3737
*/
38-
orderBy?: BlockOrderByEnum;
38+
orderBy?: BlockOrderBy;
3939
}

0 commit comments

Comments
 (0)