Skip to content

Commit

Permalink
feat(api): add count to nfts api
Browse files Browse the repository at this point in the history
  • Loading branch information
hannahhoward committed Jun 29, 2024
1 parent 88fdf0b commit 26fc200
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 14 deletions.
13 changes: 8 additions & 5 deletions packages/api/src/routes/nfts-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@ export async function nftList(event, ctx) {
throw new HTTPError('invalid params', 400)
}

const nfts = await db.listUploads(user.id, options)
const data = await db.listUploads(user.id, options)

return new JSONResponse({
ok: true,
value: nfts?.map((n) => toNFTResponse(n)),
})
return new JSONResponse(
{
ok: true,
value: data.uploads?.map((n) => toNFTResponse(n)),
},
data.count ? { headers: { Count: data.count.toString() } } : {}
)
}
2 changes: 1 addition & 1 deletion packages/api/src/routes/pins-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export async function pinsList(event, ctx) {
// Aggregate result into proper output
let count = 0
const results = []
for (const upload of data) {
for (const upload of data.uploads) {
if (upload.content.pin.length > 0) {
count++
results.push(toPinsResponse(upload))
Expand Down
19 changes: 11 additions & 8 deletions packages/api/src/utils/db-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ export class DBClient {
const from = this.client.from('upload')
const match = opts.match || 'exact'
let query = from
.select(this.uploadQuery)
.select(this.uploadQuery, { count: 'exact' })
.eq('user_id', userId)
.is('deleted_at', null)
.filter(
Expand Down Expand Up @@ -422,7 +422,7 @@ export class DBClient {
query = query.gte('inserted_at', opts.after)
}

const { data: uploads, error } = await query
const { data: uploads, count, error } = await query
if (error) {
throw new DBError(error)
}
Expand All @@ -431,12 +431,15 @@ export class DBClient {

const deals = await this.getDealsFromDagcargoFDW(cids)

return uploads?.map((u) => {
return {
...u,
deals: deals[u.content_cid] || [],
}
})
return {
count,
uploads: uploads?.map((u) => {
return {
...u,
deals: deals[u.content_cid] || [],
}
}),
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/utils/json-response.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export class JSONResponse extends Response {
constructor(body, init = {}) {
const headers = {
headers: {
...init.headers,
'content-type': 'application/json;charset=UTF-8',
},
}
Expand Down
8 changes: 8 additions & 0 deletions packages/api/test/nfts-list.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ test.serial('should list 0 nfts with date before any uploads', async (t) => {
const res = await mf.dispatchFetch(`http://miniflare.test/?before=${date}`, {
headers: { Authorization: `Bearer ${client.token}` },
})
t.is(parseInt(res.headers.get('count') || ''), 0)
const { ok, value } = await res.json()

t.is(value.length, 0)
Expand All @@ -57,6 +58,7 @@ test.serial(
const res = await mf.dispatchFetch('http://miniflare.test', {
headers: { Authorization: `Bearer ${client.token}` },
})
t.is(parseInt(res.headers.get('count') || ''), 0)
const { ok, value } = await res.json()

t.is(value[0].cid, cid2)
Expand Down Expand Up @@ -88,6 +90,7 @@ test.serial('should list 1 nft with param limit=1', async (t) => {
const res = await mf.dispatchFetch('http://miniflare.test/?limit=1', {
headers: { Authorization: `Bearer ${client.token}` },
})
t.is(parseInt(res.headers.get('count') || ''), 2)
const { ok, value } = await res.json()

t.is(value.length, 1)
Expand All @@ -110,6 +113,7 @@ test.serial('should list the default 10 nfts with no params', async (t) => {
const res = await mf.dispatchFetch('http://miniflare.test', {
headers: { Authorization: `Bearer ${client.token}` },
})
t.is(parseInt(res.headers.get('count') || ''), 10)
const { ok, value } = await res.json()

t.is(value.length, 10)
Expand Down Expand Up @@ -176,6 +180,8 @@ test.serial('should list only active nfts', async (t) => {
const res = await mf.dispatchFetch('http://miniflare.test', {
headers: { Authorization: `Bearer ${client.token}` },
})
t.is(parseInt(res.headers.get('count') || ''), 1)

const { ok, value } = await res.json()

t.true(ok)
Expand Down Expand Up @@ -203,6 +209,7 @@ test.serial('should list nfts with their parts', async (t) => {
const res = await mf.dispatchFetch('http://miniflare.test', {
headers: { Authorization: `Bearer ${client.token}` },
})
t.is(parseInt(res.headers.get('count') || ''), 1)
const { ok, value } = await res.json()

t.true(ok)
Expand Down Expand Up @@ -236,6 +243,7 @@ test.serial(
const res = await mf.dispatchFetch('http://miniflare.test', {
headers: { Authorization: `Bearer ${client.token}` },
})
t.is(parseInt(res.headers.get('count') || ''), 1)
const { ok, value } = await res.json()

t.true(ok)
Expand Down

0 comments on commit 26fc200

Please sign in to comment.