diff --git a/packages/api/src/routes/nfts-list.js b/packages/api/src/routes/nfts-list.js index bee8b464b8..e7d3e12c13 100644 --- a/packages/api/src/routes/nfts-list.js +++ b/packages/api/src/routes/nfts-list.js @@ -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() } } : {} + ) } diff --git a/packages/api/src/routes/pins-list.js b/packages/api/src/routes/pins-list.js index 0ec285a49a..b42b5e8826 100644 --- a/packages/api/src/routes/pins-list.js +++ b/packages/api/src/routes/pins-list.js @@ -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)) diff --git a/packages/api/src/utils/db-client.js b/packages/api/src/utils/db-client.js index e8988d61f6..5af81a22b1 100644 --- a/packages/api/src/utils/db-client.js +++ b/packages/api/src/utils/db-client.js @@ -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( @@ -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) } @@ -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] || [], + } + }), + } } /** diff --git a/packages/api/src/utils/json-response.js b/packages/api/src/utils/json-response.js index 53652dbd22..342e9765a1 100644 --- a/packages/api/src/utils/json-response.js +++ b/packages/api/src/utils/json-response.js @@ -7,6 +7,7 @@ export class JSONResponse extends Response { constructor(body, init = {}) { const headers = { headers: { + ...init.headers, 'content-type': 'application/json;charset=UTF-8', }, } diff --git a/packages/api/test/nfts-list.spec.js b/packages/api/test/nfts-list.spec.js index 72f5e61fe8..0c348836ae 100644 --- a/packages/api/test/nfts-list.spec.js +++ b/packages/api/test/nfts-list.spec.js @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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)