Skip to content

Commit

Permalink
Update with suggested changes and test
Browse files Browse the repository at this point in the history
  • Loading branch information
csuwildcat committed Apr 26, 2024
1 parent 4b58c77 commit cac7278
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/http-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
46 changes: 46 additions & 0 deletions tests/http-api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
Expand Down

0 comments on commit cac7278

Please sign in to comment.