Skip to content

Commit

Permalink
add support for reverse sorting of version if version is not specified
Browse files Browse the repository at this point in the history
  • Loading branch information
jackyzha0 committed Jan 23, 2022
1 parent 3a0dc23 commit 1133ee8
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ class ArweaveClient {
await this.client.transactions.sign(tx, this.#key)
const txResult = await this.client.transactions.post(tx)

// check if something went wrong
if (txResult.status !== 200) {
return Promise.reject(txResult)
}

// success, update doc data, add to cache
doc.txID = tx.id
this.cache.set(doc.name, doc)
Expand Down Expand Up @@ -200,6 +205,10 @@ class ArweaveClient {
owner {
address
}
tags {
name
value
}
}
}
}
Expand Down Expand Up @@ -228,7 +237,13 @@ class ArweaveClient {
const json = await req.json()

// safe to get first item as we specify specific tags in the query building stage
const txId = json.data.transactions.edges[0]?.node.id
const txId = version ?
json.data.transactions.edges[0]?.node.id :
json.data.transactions.edges.sort((a, b) => {
// we reverse sort edges if version is not defined to get latest version
const getVersion = (edge) => edge.node.tags.find(tag => tag.name === VERSION).value || 0
return getVersion(b) - getVersion(a)
})[0]?.node.id
if (!txId) {
return Promise.reject(`No transaction with name ${name} found`)
}
Expand Down

0 comments on commit 1133ee8

Please sign in to comment.