Skip to content

Commit

Permalink
minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
thehenrytsai committed Apr 29, 2024
1 parent 6723df3 commit 5faae02
Showing 1 changed file with 37 additions and 33 deletions.
70 changes: 37 additions & 33 deletions src/http-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,33 +87,45 @@ export class HttpApi {
}

/**
* Configures the HTTP server's request handlers.
* Handles RecordsRead of a particular `recordId`. Abstracted out because we have 2 routes that handle the same request.
*/
#setupRoutes(): void {

const leadTailSlashRegex = /^\/|\/$/;
#recordsReadHandler = async (req, res): Promise<any> => {
const record = await RecordsRead.create({
filter: { recordId: req.params.id },
});
const reply = await this.dwn.processMessage(req.params.did, record.message);
return HttpApi.#readReplyHandler(res, reply);
}

function readReplyHandler(res, reply): any {
if (reply.status.code === 200) {
if (reply?.record?.data) {
const stream = reply.record.data;
delete reply.record.data;
/**
* Handles the reply of a `RecordsRead`.
*/
static #readReplyHandler(res, reply): any {
if (reply.status.code === 200) {
if (reply?.record?.data) {
const stream = reply.record.data;
delete reply.record.data;

res.setHeader('content-type', reply.record.descriptor.dataFormat);
res.setHeader('dwn-response', JSON.stringify(reply));
res.setHeader('content-type', reply.record.descriptor.dataFormat);
res.setHeader('dwn-response', JSON.stringify(reply));

return stream.pipe(res);
} else {
return res.sendStatus(400);
}
}
else if (reply.status.code === 401) {
return res.sendStatus(404);
}
else {
return res.status(reply.status.code).send(reply);
return stream.pipe(res);
} else {
return res.sendStatus(400);
}
}
else if (reply.status.code === 401) {
return res.sendStatus(404);
}
else {
return res.status(reply.status.code).send(reply);
}
}

/**
* Configures the HTTP server's request handlers.
*/
#setupRoutes(): void {

this.#api.get('/health', (_req, res) => {
// return 200 ok
Expand All @@ -133,7 +145,7 @@ export class HttpApi {
const query = await RecordsQuery.create({
filter: {
protocol: req.params.protocol,
protocolPath: (req.params[0] || '').replace(leadTailSlashRegex)
protocolPath: (req.params[0] || '').replace(/^\/|\/$/) // removes leading and trailing slashes from the path
},
pagination: { limit: 1 },
dateSort: DateSort.PublishedDescending
Expand All @@ -147,7 +159,7 @@ export class HttpApi {
filter: { recordId: entries[0].recordId },
});
const reply = await this.dwn.processMessage(req.params.did, record.toJSON());
return readReplyHandler(res, reply);
return HttpApi.#readReplyHandler(res, reply);
}
else {
return res.sendStatus(404);
Expand Down Expand Up @@ -183,16 +195,8 @@ export class HttpApi {
}
})

const recordsReadHandler = async (req, res): Promise<any> => {
const record = await RecordsRead.create({
filter: { recordId: req.params.id },
});
const reply = await this.dwn.processMessage(req.params.did, record.message);
return readReplyHandler(res, reply);
}

this.#api.get('/:did/read/records/:id', recordsReadHandler);
this.#api.get('/:did/records/:id', recordsReadHandler);
this.#api.get('/:did/read/records/:id', this.#recordsReadHandler);
this.#api.get('/:did/records/:id', this.#recordsReadHandler);

this.#api.get('/:did/query/protocols', async (req, res) => {
const query = await ProtocolsQuery.create({});
Expand Down

0 comments on commit 5faae02

Please sign in to comment.