Skip to content

Commit

Permalink
feat: Add function to fetch wallet world stats
Browse files Browse the repository at this point in the history
  • Loading branch information
fzavalia committed Sep 29, 2023
1 parent 2908bb1 commit 230ecc0
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 4 deletions.
47 changes: 47 additions & 0 deletions src/lib/api/worlds.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { WalletStats, content } from './worlds'

beforeEach(() => {
global.fetch = jest.fn()
})

describe('when fetching the world stats for a wallet', () => {
let wallet: string

beforeEach(() => {
wallet = '0x123'
})

describe('when the fetch result is not ok', () => {
beforeEach(() => {
global.fetch = () => Promise.resolve({ ok: false } as Response)
})

it('should return null', async () => {
expect(await content.fetchWalletStats(wallet)).toBeNull()
})
})

describe('when the fetch result is ok', () => {
let walletStats: WalletStats

beforeEach(() => {
walletStats = {
wallet,
dclNames: [],
ensNames: [],
maxAllowedSpace: '',
usedSpace: ''
}

global.fetch = () =>
Promise.resolve({
ok: true,
json: () => Promise.resolve(walletStats)
} as Response)
})

it('should return null', async () => {
expect(await content.fetchWalletStats(wallet)).toBe(walletStats)
})
})
})
32 changes: 28 additions & 4 deletions src/lib/api/worlds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,36 @@ export type WorldInfo = {
}
}

export type WalletStats = {
wallet: string
dclNames: {
name: string
size: string
}[]
ensNames: {
name: string
size: string
}[]
usedSpace: string
maxAllowedSpace: string
}

export class WorldsAPI extends BaseAPI {
public async fetchWorld(name: string) {
const req = await fetch(`${this.url}/world/${name}/about`)
if (req.ok) {
const res = await req.json()
return res as WorldInfo
const result = await fetch(`${this.url}/world/${name}/about`)
if (result.ok) {
const json = await result.json()
return json as WorldInfo
} else {
return null
}
}

public async fetchWalletStats(address: string) {
const result = await fetch(`${this.url}/wallet/${address}/stats`)
if (result.ok) {
const json = await result.json()
return json as WalletStats
} else {
return null
}
Expand Down

0 comments on commit 230ecc0

Please sign in to comment.