Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): add count to list nfts api #2701

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
7 changes: 7 additions & 0 deletions packages/api/test/nfts-list.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ test.serial(
const res = await mf.dispatchFetch('http://miniflare.test', {
headers: { Authorization: `Bearer ${client.token}` },
})
t.is(parseInt(res.headers.get('count') || ''), 2)
const { ok, value } = await res.json()

t.is(value[0].cid, cid2)
Expand Down Expand Up @@ -88,6 +89,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 +112,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 +179,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 +208,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 +242,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
Loading