Skip to content

Commit 6da5098

Browse files
authored
feat(api): add count to list nfts api (#2701)
# Goals You can't display a progress bar if you don't know what you're progressing toward :) # Implementation Essentially, use the count feature for posgrest-js that is used in web3.storage old - add to count db.listUploads feature, which changes return type - modify JSONResponse to actually allow passing headers - update tests, though I wasn't able to run them cause mysterious connection refusal errors with my docker setup. but I think I got them passing here
1 parent 88fdf0b commit 6da5098

5 files changed

Lines changed: 28 additions & 14 deletions

File tree

packages/api/src/routes/nfts-list.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,13 @@ export async function nftList(event, ctx) {
3333
throw new HTTPError('invalid params', 400)
3434
}
3535

36-
const nfts = await db.listUploads(user.id, options)
36+
const data = await db.listUploads(user.id, options)
3737

38-
return new JSONResponse({
39-
ok: true,
40-
value: nfts?.map((n) => toNFTResponse(n)),
41-
})
38+
return new JSONResponse(
39+
{
40+
ok: true,
41+
value: data.uploads?.map((n) => toNFTResponse(n)),
42+
},
43+
data.count ? { headers: { Count: data.count.toString() } } : {}
44+
)
4245
}

packages/api/src/routes/pins-list.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export async function pinsList(event, ctx) {
4545
// Aggregate result into proper output
4646
let count = 0
4747
const results = []
48-
for (const upload of data) {
48+
for (const upload of data.uploads) {
4949
if (upload.content.pin.length > 0) {
5050
count++
5151
results.push(toPinsResponse(upload))

packages/api/src/utils/db-client.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ export class DBClient {
373373
const from = this.client.from('upload')
374374
const match = opts.match || 'exact'
375375
let query = from
376-
.select(this.uploadQuery)
376+
.select(this.uploadQuery, { count: 'exact' })
377377
.eq('user_id', userId)
378378
.is('deleted_at', null)
379379
.filter(
@@ -422,7 +422,7 @@ export class DBClient {
422422
query = query.gte('inserted_at', opts.after)
423423
}
424424

425-
const { data: uploads, error } = await query
425+
const { data: uploads, count, error } = await query
426426
if (error) {
427427
throw new DBError(error)
428428
}
@@ -431,12 +431,15 @@ export class DBClient {
431431

432432
const deals = await this.getDealsFromDagcargoFDW(cids)
433433

434-
return uploads?.map((u) => {
435-
return {
436-
...u,
437-
deals: deals[u.content_cid] || [],
438-
}
439-
})
434+
return {
435+
count,
436+
uploads: uploads?.map((u) => {
437+
return {
438+
...u,
439+
deals: deals[u.content_cid] || [],
440+
}
441+
}),
442+
}
440443
}
441444

442445
/**

packages/api/src/utils/json-response.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export class JSONResponse extends Response {
77
constructor(body, init = {}) {
88
const headers = {
99
headers: {
10+
...init.headers,
1011
'content-type': 'application/json;charset=UTF-8',
1112
},
1213
}

packages/api/test/nfts-list.spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ test.serial(
5757
const res = await mf.dispatchFetch('http://miniflare.test', {
5858
headers: { Authorization: `Bearer ${client.token}` },
5959
})
60+
t.is(parseInt(res.headers.get('count') || ''), 2)
6061
const { ok, value } = await res.json()
6162

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

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

115118
t.is(value.length, 10)
@@ -176,6 +179,8 @@ test.serial('should list only active nfts', async (t) => {
176179
const res = await mf.dispatchFetch('http://miniflare.test', {
177180
headers: { Authorization: `Bearer ${client.token}` },
178181
})
182+
t.is(parseInt(res.headers.get('count') || ''), 1)
183+
179184
const { ok, value } = await res.json()
180185

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

208214
t.true(ok)
@@ -236,6 +242,7 @@ test.serial(
236242
const res = await mf.dispatchFetch('http://miniflare.test', {
237243
headers: { Authorization: `Bearer ${client.token}` },
238244
})
245+
t.is(parseInt(res.headers.get('count') || ''), 1)
239246
const { ok, value } = await res.json()
240247

241248
t.true(ok)

0 commit comments

Comments
 (0)