-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathCatalystAPI.ts
More file actions
62 lines (49 loc) · 1.81 KB
/
CatalystAPI.ts
File metadata and controls
62 lines (49 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import API from "decentraland-gatsby/dist/utils/api/API"
import Options from "decentraland-gatsby/dist/utils/api/Options"
import Time from "decentraland-gatsby/dist/utils/date/Time"
import env from "decentraland-gatsby/dist/utils/env"
import { Paginated, Permission } from "../entities/Place/types"
export default class CatalystAPI extends API {
static Url = env(`CATALYST_URL`, "https://peer.decentraland.org/")
static Cache = new Map<string, CatalystAPI>()
static from(url: string) {
if (!this.Cache.has(url)) {
this.Cache.set(url, new CatalystAPI(url))
}
return this.Cache.get(url)!
}
static get() {
return this.from(env("CATALYST_URL", this.Url))
}
async getAllOperatedLands(address: string): Promise<Permission[]> {
const allElements: Permission[] = []
let pageNum = 0
const pageSize = 100 // Default page size, adjust if needed
let hasMorePages = true
while (hasMorePages) {
const controller = new AbortController()
const fetchOptions = new Options({ signal: controller.signal })
const timeoutId = setTimeout(() => {
controller.abort()
}, Time.Second * 10)
try {
const response = await this.fetch<Paginated<Permission>>(
`/lambdas/users/${address}/lands-permissions?pageNum=${pageNum}&pageSize=${pageSize}`,
fetchOptions
)
if (response.elements && response.elements.length > 0) {
allElements.push(...response.elements)
}
hasMorePages =
response.elements && response.elements.length === pageSize
pageNum++
} catch (error) {
console.error(`Error fetching operated lands page ${pageNum}:`, error)
break // return already fetched elements
} finally {
clearTimeout(timeoutId)
}
}
return allElements
}
}