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

Add /query API endpoint #125

Merged
merged 8 commits into from
Apr 27, 2024
Merged
Changes from 1 commit
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
25 changes: 24 additions & 1 deletion src/http-api.ts
thehenrytsai marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type Dwn, RecordsRead, type RecordsReadReply } from '@tbd54566975/dwn-sdk-js';
import { type Dwn, RecordsRead, type RecordsReadReply, RecordsQuery, type RecordsQueryReply } from '@tbd54566975/dwn-sdk-js';

import cors from 'cors';
import type { Express, Request, Response } from 'express';
Expand Down Expand Up @@ -128,6 +128,29 @@ export class HttpApi {
}
});

this.#api.get('/:did/query', async (req, res) => {
const options = {} as any;
thehenrytsai marked this conversation as resolved.
Show resolved Hide resolved
for (const param in req.query as object) {
const keys = param.split('.');
csuwildcat marked this conversation as resolved.
Show resolved Hide resolved
const lastKey = keys.pop();
keys.reduce((obj, key) => obj[key] = obj[key] || {}, options)[lastKey] = req.query[param]
}
const record = await RecordsQuery.create({
csuwildcat marked this conversation as resolved.
Show resolved Hide resolved
filter: options.filter,
pagination: options.pagination,
dateSort: options.dateSort,
});
const reply = (await this.dwn.processMessage(req.params.did, record.toJSON())) as RecordsQueryReply;
if (reply.status.code === 200) {
csuwildcat marked this conversation as resolved.
Show resolved Hide resolved
res.setHeader('content-type', 'application/json');
return res.json(reply)
} else if (reply.status.code === 401) {
return res.sendStatus(404);
} else {
return res.status(reply.status.code).send(reply);
}
});
thehenrytsai marked this conversation as resolved.
Show resolved Hide resolved

this.#api.get('/', (_req, res) => {
// return a plain text string
res.setHeader('content-type', 'text/plain');
Expand Down
Loading