diff --git a/src/http-api.ts b/src/http-api.ts index 4627909..718d1eb 100644 --- a/src/http-api.ts +++ b/src/http-api.ts @@ -130,17 +130,19 @@ export class HttpApi { this.#api.get('/:did/query', async (req, res) => { const options = {} as any; - for (const param in req.query as object) { + for (const param in req.query) { const keys = param.split('.'); const lastKey = keys.pop(); - keys.reduce((obj, key) => obj[key] = obj[key] || {}, options)[lastKey] = req.query[param] + // Set up the object tree + const lastLevel = keys.reduce((obj, key) => obj[key] = obj[key] || {}, options) + lastLevel[lastKey] = req.query[param]; } const record = await RecordsQuery.create({ filter: options.filter, pagination: options.pagination, dateSort: options.dateSort, }); - const reply = (await this.dwn.processMessage(req.params.did, record.toJSON())) as RecordsQueryReply; + const reply = (await this.dwn.processMessage(req.params.did, record.message)) as RecordsQueryReply; if (reply.status.code === 200) { res.setHeader('content-type', 'application/json'); return res.json(reply) diff --git a/tests/http-api.spec.ts b/tests/http-api.spec.ts index eabd370..773cca1 100644 --- a/tests/http-api.spec.ts +++ b/tests/http-api.spec.ts @@ -529,6 +529,52 @@ describe('http api', function () { }); }); + describe('/:did/query', function () { + it('returns record data if record is published', async function () { + const filePath = './fixtures/test.jpeg'; + const { + cid: expectedCid, + size, + stream, + } = await getFileAsReadStream(filePath); + + const { recordsWrite } = await createRecordsWriteMessage(alice, { + dataCid: expectedCid, + dataSize: size, + published: true, + }); + + const requestId = uuidv4(); + const dwnRequest = createJsonRpcRequest(requestId, 'dwn.processMessage', { + message: recordsWrite.toJSON(), + target: alice.did, + }); + + const response = await fetch('http://localhost:3000', { + method: 'POST', + headers: { + 'dwn-request': JSON.stringify(dwnRequest), + }, + body: stream, + }); + + expect(response.status).to.equal(200); + + const body = (await response.json()) as JsonRpcResponse; + expect(body.id).to.equal(requestId); + expect(body.error).to.not.exist; + + const { reply } = body.result; + expect(reply.status.code).to.equal(202); + + const { entries } = await fetch( + `http://localhost:3000/${alice.did}/query?filter.recordId=${recordsWrite.message.recordId}&foo.bar=1337`, + ).then(response => response.json()) as any; + + expect(entries?.length).to.equal(1); + }); + }); + describe('/info', function () { it('verify /info has some of the fields it is supposed to have', async function () { const resp = await fetch(`http://localhost:3000/info`);