Skip to content

Commit a992abe

Browse files
authored
Merge pull request #2010 from aeternity/autorest-date
Return time as Date instance
2 parents 3fc8b0a + e0e33ea commit a992abe

File tree

5 files changed

+55
-27
lines changed

5 files changed

+55
-27
lines changed

src/utils/autorest.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ export const createSerializer = (
3838
console.warn(`AeSdk internal error: BigInt value ${responseBody} handled incorrectly`);
3939
responseBody = +responseBody.replace(bigIntPrefix, '');
4040
}
41-
return deserialize.call(this, mapper, responseBody, objectName, options);
41+
const result = deserialize.call(this, mapper, responseBody, objectName, options);
42+
// TODO: remove after fixing https://github.com/aeternity/ae_mdw/issues/1891
43+
// and https://github.com/aeternity/aeternity/issues/4386
44+
if (result instanceof Date) return new Date(+result / 1000);
45+
return result;
4246
}
4347
if (typeof responseBody === 'number' && responseBody > Number.MAX_SAFE_INTEGER) {
4448
throw new InternalError(`Number ${responseBody} is not accurate to be converted to BigInt`);

test/integration/Middleware.ts

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ function copyFields(
1616
.filter((key) => source[key] != null)
1717
.forEach((key) => {
1818
expect(typeof target[key]).to.be.equal(typeof source[key]);
19+
expect(target[key]?.constructor).to.be.equal(source[key]?.constructor);
1920
if (typeof target[key] === 'string' && target[key][2] === '_') {
2021
expect(target[key].slice(0, 2)).to.be.equal(source[key].slice(0, 2));
2122
}
@@ -74,14 +75,15 @@ describe('Middleware API', () => {
7475
prevKeyHash: 'kh_11111111111111111111111111111111273Yts',
7576
stateHash: 'bs_HwreBuvhDCzAdkL2upX6qhEAkCXirujYP5BXkPDF7NZV76fdR',
7677
target: 1338,
77-
time: 0,
78+
time: undefined,
7879
transactionsCount: 0,
7980
version: 1,
8081
}],
8182
next: null,
8283
prev: null,
8384
}, middleware);
8485
expectedRes.data.unshift(...res.data.slice(0, -1));
86+
expect(res.data[0].time.getFullYear()).to.be.within(2024, 2030);
8587
expect(res).to.be.eql(expectedRes);
8688
});
8789

@@ -98,7 +100,7 @@ describe('Middleware API', () => {
98100
prevHash: 'kh_cKJy5CavGHMCpzmZz5s38Yw2F6Es6t2ED3Rddb7zgakZ5xZwJ',
99101
prevKeyHash: 'kh_cKJy5CavGHMCpzmZz5s38Yw2F6Es6t2ED3Rddb7zgakZ5xZwJ',
100102
stateHash: 'bs_2JgJeoSoVCYgmhSimw473A4L6CGFTagQjDFDsSZJQFuemyiZZa',
101-
time: 1721957163938,
103+
time: new Date(1721957163938),
102104
transactionsCount: 1,
103105
version: 6,
104106
signature: 'sg_DmGnGbbfUNuYgvJyvA927kbqJ9mVDHoKMHYvRQR89LcmAV26WwUvLSdJwdvohnGcr58VRJtzjikEaJ9HuFwduo3jbMr9E',
@@ -115,15 +117,15 @@ describe('Middleware API', () => {
115117
const expectedRes: typeof res = new MiddlewarePage({
116118
data: [{
117119
blockHash: 'mh_f4S91p7y6hojhGhPHwzoXdjvZVWcuaBg759BDUzHDsQmYnC4o',
118-
blockTime: 1721994542947,
120+
blockTime: new Date(1721994542947),
119121
height: 11,
120122
payload: {
121123
blockHash: 'mh_f4S91p7y6hojhGhPHwzoXdjvZVWcuaBg759BDUzHDsQmYnC4o',
122124
blockHeight: 11,
123125
encodedTx: 'tx_+QEQCwH4hLhAfiGhhOZxwmnQvbdccs6QAfNW0eLK/B/Q/mnyRecva/7S5CzZM4CJc2A9/wIe/q6+SvycqfH44siQmJsCIo0rCLhA4jKt6g+45BFsA/1yHuMTtm2gYbocs1HsHQJQgVf84dFmEscGNzih38qBjerWY2Poscr9rKoDE2oCm0+VkrhkA7iG+IQyAqEBZaKlte018CTFZNmmlaD9LKcD19Vo4SqKqkJrvn4KnomHAca/UmNAAKEBpQ3vKE1jiARiiFIDILt4wWVsMHwCRAf74T0FFhAGxKqHAca/UmNAAAABAIYQFHIeoADAwKBsGHHwj5GEh4Gy4HE+O8s2b64SKuVCuBGlgfmEluytjAcR+sQp',
124126
hash: 'th_26quLwJJ5CezBuXKnm2duH7bgmBGBTkqjL1m9ybroZ9Kndp8h2',
125127
microIndex: 0,
126-
microTime: 1721994542947,
128+
microTime: new Date(1721994542947),
127129
signatures: [
128130
'sg_HW6JCb97ZBcn5hAoiqWNDoYVus1qqK9Ne2Ls1GjriSPzWhWkKX7EZigKbBayLcTmM2LNYpc2vcEBGFzEzsHBsssuLBcqV',
129131
'sg_WbQJc3RweFShfr3YgFk5Wqin4QRsr9a487tuvXxi4yLHtYEqRXwffVUD2iz5GXAkJEXayyLMmGQpP22beMYNNYnyKrJNW',
@@ -150,7 +152,7 @@ describe('Middleware API', () => {
150152
type: 'ChannelCreateTxEvent',
151153
}, {
152154
blockHash: 'mh_2GzNyPoPZvKavxfvorCc7gwTFs8u6HKzua2z7fcakCxh66JfrU',
153-
blockTime: 1721911705246,
155+
blockTime: new Date(1721911705246),
154156
height: 3,
155157
payload: {
156158
amount: 3n,
@@ -160,15 +162,15 @@ describe('Middleware API', () => {
160162
type: 'InternalTransferEvent',
161163
}, {
162164
blockHash: 'mh_2GzNyPoPZvKavxfvorCc7gwTFs8u6HKzua2z7fcakCxh66JfrU',
163-
blockTime: 1721911705246,
165+
blockTime: new Date(1721911705246),
164166
height: 3,
165167
payload: {
166168
blockHash: 'mh_2GzNyPoPZvKavxfvorCc7gwTFs8u6HKzua2z7fcakCxh66JfrU',
167169
blockHeight: 3,
168170
encodedTx: 'tx_+KULAfhCuEBXzxuqo82mMkKCtMLjwrBwYa6B1zvwpqUeGT49lce9p0QMPb2tfgcgScxi2N87JfOwRpuI2iOsrP69uMtYfKIIuF34WyACoQGlDe8oTWOIBGKIUgMgu3jBZWwwfAJEB/vhPQUWEAbEqgKkMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODAwLmNoYWluAIcBxr9SY0AAhg9Vhk+YAAarXkK8',
169171
hash: 'th_2CKnN6EorvNiwwqRjSzXLrPLiHmcwo4Ny22dwCrSYRoD6MVGK1',
170172
microIndex: 0,
171-
microTime: 1721911705246,
173+
microTime: new Date(1721911705246),
172174
signatures: [
173175
'sg_CVJMvQ7TPCbcmEn5GXFY9bh8okNTuE956PiiZaSJ2V9JWKgimmP86L5NUiZpFgeE6Am7QJk7KYwxJMGgFhQSXJJxrhqFJ',
174176
],
@@ -188,15 +190,15 @@ describe('Middleware API', () => {
188190
type: 'NameClaimTxEvent',
189191
}, {
190192
blockHash: 'mh_2SiHrRABSj8Hgdt3Dc2ypaKrUkjCTK92pUx1nZKLEAYMVhUu8G',
191-
blockTime: 1721911705161,
193+
blockTime: new Date(1721911705161),
192194
height: 2,
193195
payload: {
194196
blockHash: 'mh_2SiHrRABSj8Hgdt3Dc2ypaKrUkjCTK92pUx1nZKLEAYMVhUu8G',
195197
blockHeight: 2,
196198
encodedTx: 'tx_+PcLAfhCuEB+Tq5sUOIMedGLODH48nG1b7KNt4Dre9vyXC3eH2EjFe2EkVGpx4QN/eQU2OLHXJgg1/7uFO5S3pMlI+jqv78FuK/4rSoBoQGlDe8oTWOIBGKIUgMgu3jBZWwwfAJEB/vhPQUWEAbEqgG4avhoRgOg2qvS0QZEjddEG/XeWW7yVgfv7YPK4+Tsp1rY/AENke3AuDue/kTWRB8ANwA3ABoOgj8BAz/+gHggkgA3AQcHAQEAmC8CEUTWRB8RaW5pdBGAeCCSGWdldEFyZ4IvAIU4LjAuMACDCAADhkdlNJ1oAAUAAEyEO5rKAIcrEUTWRB8/Bmn2yg==',
197199
hash: 'th_2JMR7C1DjrGeZWyyLMkccRLga1Lct8Syy9hcZKD9PEZkN5JvSD',
198200
microIndex: 0,
199-
microTime: 1721911705161,
201+
microTime: new Date(1721911705161),
200202
signatures: [
201203
'sg_HXRkFjgjsFmFLZ1ywBgYj9VouQK1BySqCViALxq3ge69a86aDgd1ESqNXhCLebh7fH6SohTjbLXXxhjPnYXaGJfiX7DQV',
202204
],
@@ -230,15 +232,15 @@ describe('Middleware API', () => {
230232
type: 'ContractCreateTxEvent',
231233
}, {
232234
blockHash: 'mh_Q6fJrHYWiaAz8277zX8fgAgyCpmBtioFd1RQdC36oRuCtjz19',
233-
blockTime: 1721911704982,
235+
blockTime: new Date(1721911704982),
234236
height: 1,
235237
payload: {
236238
blockHash: 'mh_Q6fJrHYWiaAz8277zX8fgAgyCpmBtioFd1RQdC36oRuCtjz19',
237239
blockHeight: 1,
238240
encodedTx: 'tx_+KMLAfhCuEDgjc7zMPb+xRW+pI0L5OqwjI+OBF0ee1zgmlkXsavoYXHZw7vTx6vxaAZxxs4ts/eZhAmqVGg3EmTsRAtccKMDuFv4WQwBoQGEDJdLlxZHdkVLoRnYTtxNYFio3skrbtxXirLTC0xCAKEBpQ3vKE1jiARiiFIDILt4wWVsMHwCRAf74T0FFhAGxKqIDeC2s6dkAACGD0w2IAgABAGAJs2FDw==',
239241
hash: 'th_U26TdBBNT56HFXWAb4ktFyWBTuCAnwdajecTL4ss2BhciRviG',
240242
microIndex: 0,
241-
microTime: 1721911704982,
243+
microTime: new Date(1721911704982),
242244
signatures: [
243245
'sg_WNvkq9RewEjZDrDLqXMUoyBd8pGzqAuyaDfG3bQAfGx4tF6smTLyYnFWmtY8SrJRnEHbriDUm836DSJSkMjiijLKBsSzo',
244246
],
@@ -279,7 +281,7 @@ describe('Middleware API', () => {
279281
encodedTx: 'tx_+KMLAfhCuEDgjc7zMPb+xRW+pI0L5OqwjI+OBF0ee1zgmlkXsavoYXHZw7vTx6vxaAZxxs4ts/eZhAmqVGg3EmTsRAtccKMDuFv4WQwBoQGEDJdLlxZHdkVLoRnYTtxNYFio3skrbtxXirLTC0xCAKEBpQ3vKE1jiARiiFIDILt4wWVsMHwCRAf74T0FFhAGxKqIDeC2s6dkAACGD0w2IAgABAGAJs2FDw==',
280282
hash: 'th_U26TdBBNT56HFXWAb4ktFyWBTuCAnwdajecTL4ss2BhciRviG',
281283
microIndex: 0,
282-
microTime: 1721973919196,
284+
microTime: new Date(1721973919196),
283285
signatures: [
284286
'sg_WNvkq9RewEjZDrDLqXMUoyBd8pGzqAuyaDfG3bQAfGx4tF6smTLyYnFWmtY8SrJRnEHbriDUm836DSJSkMjiijLKBsSzo',
285287
],
@@ -377,7 +379,7 @@ describe('Middleware API', () => {
377379
blockHash: 'mh_zmcTZSgZcuQ9fL6h6iNNp3ftFvbQ2FjtgPHg9qEHgbwyqFFpi',
378380
eventName: null,
379381
logIdx: 0,
380-
blockTime: 1721968249016,
382+
blockTime: new Date(1721968249016),
381383
eventHash: 'KGBGHR0NTNENA10FD9MJS5P39C1LD4T9AUBIPIDL772714A57HH0====',
382384
callTxHash: 'th_2cNd6j4CtZYaY6F6AWNbYDXZGkaQbaAjjjtBiLATiaiXJ1P812',
383385
contractTxHash: 'th_giDXk5C5Fmvec2yBDEmSn74ZnKCMJv1Nso6uaSbfTup1xSx2Y',
@@ -430,8 +432,8 @@ describe('Middleware API', () => {
430432
active: true,
431433
hash: 'nm_2VSJFCVStB8ZdkLWcyd4adywYoyqYNzMt9Td924Jf8ESi94Nni',
432434
activeFrom: 3,
433-
approximateActivationTime: 1721740187500,
434-
approximateExpireTime: 1754140007661,
435+
approximateActivationTime: new Date(1721740187500),
436+
approximateExpireTime: new Date(1754140007661),
435437
expireHeight: 180003,
436438
pointers: {
437439
account_pubkey: presetAccount1Address,
@@ -535,7 +537,7 @@ describe('Middleware API', () => {
535537
active: true,
536538
blockHash: 'mh_2AVwWGLB7H8McaS1Yr7dfGoepTTVmTXJVFU5TCeDDAxgkyGDAr',
537539
blockHeight: 6,
538-
blockTime: 1721994539489,
540+
blockTime: new Date(1721994539489),
539541
key: 'account_pubkey',
540542
name: '123456789012345678901234567801.chain',
541543
sourceTxHash: 'th_2U32kq8HH1qxS5rohqVGzC9mF9E3mdcj3pZC6o9kfjCB4t1p8h',
@@ -570,16 +572,16 @@ describe('Middleware API', () => {
570572
const res = await middleware.getNamesAuctions();
571573
const expectedRes: typeof res = new MiddlewarePage({
572574
data: [{
573-
activationTime: 1721975996873,
574-
approximateExpireTime: 1722407457100,
575+
activationTime: new Date(1721975996873),
576+
approximateExpireTime: new Date(1722407457100),
575577
auctionEnd: 2407,
576578
lastBid: {
577579
blockHash: 'mh_BoBikwwf68giAEFKNYEh93uNkGu9enzx8cjn2vX7CRTnY5g6T',
578580
blockHeight: 7,
579581
encodedTx: 'tx_+IoLAfhCuEA6/CTIyE5UbHQIB8sWFKudzIu8dWfB71IRqDzbp0IUIiIpvPIEg4s/2nZ5aHrh7XxFc2+GqsRkqw8XffUTpxcCuEL4QCACoQFloqW17TXwJMVk2aaVoP0spwPX1WjhKoqqQmu+fgqeiQOHMS5jaGFpbgCJHupYdyGHT8AAhg7Xy82AAArCRC+X',
580582
hash: 'th_C7LscPqF5Nf5QrgZDSVbY92v7rruefN1qHjrHVuk2bdNwZF1e',
581583
microIndex: 0,
582-
microTime: 1721975996873,
584+
microTime: new Date(1721975996873),
583585
signatures: [
584586
'sg_8iagZbC7qnDeRDNkm1y1LyQCUqgobMKNH1G6Pv7QatFfPyo2oPzy5sUQdojZSY9BK7poupGqfQz2Eo8VnVkCyaaBRN8ks',
585587
],
@@ -619,7 +621,7 @@ describe('Middleware API', () => {
619621
data: [{
620622
active: true,
621623
activeFrom: 10,
622-
approximateExpireTime: 1722066317304,
624+
approximateExpireTime: new Date(1722066317304),
623625
expireHeight: 510,
624626
format: {
625627
query: 'string',
@@ -633,7 +635,7 @@ describe('Middleware API', () => {
633635
hash: 'th_299u2zPGuFDJPpmYM6ZpRaAiCnRViGwW4aph12Hz9Qr1Cc7tPP',
634636
txHash: 'th_299u2zPGuFDJPpmYM6ZpRaAiCnRViGwW4aph12Hz9Qr1Cc7tPP',
635637
microIndex: 0,
636-
microTime: 1721976497295,
638+
microTime: new Date(1721976497295),
637639
signatures: [
638640
'sg_NaZNFJArMypD4wp4MbJ2cMvG6aWk7PSynP9qVsti1CabtMKSUbPwRUz55Yer7XiNURN6PcycF7NwBANaeJPMCpwKoWM9b',
639641
],
@@ -656,7 +658,7 @@ describe('Middleware API', () => {
656658
},
657659
encodedTx: 'tx_+IsLAfhCuECk8CD7+rO/nCOX4fF6BylVDytJmDquVV56cv7/Lvsg23evMjX45PwdRDn2x/HGBuduMmUQaOESI+GoNarbsNEIuEP4QRYBoQFloqW17TXwJMVk2aaVoP0spwPX1WjhKoqqQmu+fgqeiQaGc3RyaW5nhnN0cmluZwAAggH0hg7x34XgAA0A0ekNLA==',
658660
},
659-
registerTime: 1721976497295,
661+
registerTime: new Date(1721976497295),
660662
registerTxHash: 'th_299u2zPGuFDJPpmYM6ZpRaAiCnRViGwW4aph12Hz9Qr1Cc7tPP',
661663
}],
662664
next: null,
@@ -673,7 +675,7 @@ describe('Middleware API', () => {
673675
const expectedRes: typeof res = {
674676
active: true,
675677
activeFrom: 10,
676-
approximateExpireTime: 1722066317304,
678+
approximateExpireTime: new Date(1722066317304),
677679
expireHeight: 510,
678680
...{ extends: [] }, // TODO: rewrite after solving https://github.com/aeternity/ae_mdw/issues/1872
679681
format: {
@@ -683,7 +685,7 @@ describe('Middleware API', () => {
683685
oracle: 'ok_mm92WC5DaSxLfWouNABCU9Uo1bDMFEXgbbnWU8n8o9u1e3qQp',
684686
queryFee: 0n,
685687
register: {} as any, // TODO: fix after solving https://github.com/aeternity/ae_mdw/issues/1872
686-
registerTime: 1721976497295,
688+
registerTime: new Date(1721976497295),
687689
registerTxHash: 'th_299u2zPGuFDJPpmYM6ZpRaAiCnRViGwW4aph12Hz9Qr1Cc7tPP',
688690
};
689691
copyFields(expectedRes, res, ['registerTime', 'approximateExpireTime']);
@@ -707,7 +709,7 @@ describe('Middleware API', () => {
707709
initiator: 'ak_mm92WC5DaSxLfWouNABCU9Uo1bDMFEXgbbnWU8n8o9u1e3qQp',
708710
initiatorAmount: 500000000000000n,
709711
lastUpdatedHeight: 11,
710-
lastUpdatedTime: 1721984829629,
712+
lastUpdatedTime: new Date(1721984829629),
711713
lastUpdatedTxHash: 'th_26quLwJJ5CezBuXKnm2duH7bgmBGBTkqjL1m9ybroZ9Kndp8h2',
712714
lastUpdatedTxType: 'ChannelCreateTx',
713715
lockPeriod: 1,
@@ -741,7 +743,7 @@ describe('Middleware API', () => {
741743
initiator: 'ak_mm92WC5DaSxLfWouNABCU9Uo1bDMFEXgbbnWU8n8o9u1e3qQp',
742744
initiatorAmount: 500000000000000n,
743745
lastUpdatedHeight: 11,
744-
lastUpdatedTime: 1721984829629,
746+
lastUpdatedTime: new Date(1721984829629),
745747
lastUpdatedTxHash: 'th_26quLwJJ5CezBuXKnm2duH7bgmBGBTkqjL1m9ybroZ9Kndp8h2',
746748
lastUpdatedTxType: 'ChannelCreateTx',
747749
lockPeriod: 1,

test/integration/node.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ describe('Node client', () => {
122122
}));
123123
});
124124

125+
it('returns time as Date', async () => {
126+
const block = await node.getTopHeader();
127+
expect(block.time).to.be.instanceOf(Date);
128+
expect(block.time.getFullYear()).to.be.within(2024, 2030);
129+
});
130+
125131
it('doesn\'t remember failed version request', async () => {
126132
let shouldFail = true;
127133
class CustomNode extends Node {

tooling/autorest/middleware.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ directive:
4545
to 'type:object'
4646
https://github.com/Azure/autorest/issues/4118
4747
48+
- from: openapi-document
49+
where: $..properties[time,micro_time,approximate_activation_time,approximate_expire_time,register_time,block_time,activation_time,creation_time,microtime,last_updated_time]
50+
transform: $.format = 'unixtime'
51+
reason: >
52+
Convert time as milliseconds to dates
53+
https://github.com/aeternity/ae_mdw/issues/1891
54+
4855
- from: openapi-document
4956
where: $.servers
5057
transform: $ = []

tooling/autorest/node.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,15 @@ directive:
143143
throw errors even for explained response with not 200 code
144144
https://github.com/Azure/autorest.typescript/issues/463#issuecomment-524203041
145145
146+
- from: openapi-document
147+
where: $..properties.time
148+
set:
149+
type: 'integer'
150+
format: 'unixtime'
151+
reason: >
152+
Convert time as milliseconds to dates
153+
https://github.com/aeternity/aeternity/issues/4386
154+
146155
- from: openapi-document
147156
where: $.components.schemas
148157
transform: >

0 commit comments

Comments
 (0)