Skip to content

Commit

Permalink
Updated README and swagger docs, fixed sqlite issue
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelpapineau committed Nov 28, 2023
1 parent 3141631 commit 34dec0f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,25 @@ Send Text Message with the following JSON payloads
| GET `/traceId` | Returns all `traceId` by `chain`
| GET `/moduleHash` | Returns all available `moduleHash`
| GET `/moduleHashByChain` | Returns all available `moduleHash` by `chain`
| GET `/openapi` | Returns api documentation in JSON format
| GET `/messages` | Returns the most recent messages
| POST `/` {timestamp, signature, body} | Webhook HTTP POST (Ed25519 signature)
| POST `/` {"message": "PING"} | Webhook HTTP POST Ping

## Parameters
### /messages
| Parameter | Type | Description |
|-----------------|--------|------------------------------------------|
| `chain` | string | Filter results by chain name, cannot be used with distinct
| `moduleHash` | string | Filter results by module hash
| `limit` | int | Limit number of results shown with a maximum value of 50
| `sort` | string | Sort by asc (ascending) or desc (descending)
| `distinct` | bool | If set to true, will return list of results distinct by chain.

### Example Request
```
/messages?chain=value1&moduleHash=value2&limit=value3&sort=value4
```
## WebSockets examples

- [`Bun`](/examples/bun) - https://bun.sh/
Expand Down
9 changes: 4 additions & 5 deletions src/fetch/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as sqlite from "../sqlite.js";
import { DEFAULT_RECENT_MESSAGES_LIMIT, RECENT_MESSAGES_LIMIT } from "../config.js";
import Database from "bun:sqlite";
import { db } from "../../index.js";
import { toJSON } from "./cors.js";
import { toJSON, toText } from "./cors.js";

export function parseLimit(searchParams: URLSearchParams) {
const value = searchParams.get("limit");
Expand Down Expand Up @@ -30,8 +30,8 @@ export function handleMessages(req: Request) {
// // error handling
// if (distinct !== "true" && distinct !== null) return toText("distinct must be set to true if declared", 400 );
// if (distinct === "true" && chain) return toText("chain cannot be set if distinct is set to true", 400 );

selectMessages(db, limit, sort, chain, moduleHash);
return toJSON(selectMessages(db, limit, sort, chain, moduleHash));
//console.log(messages)
}

export function insertMessages(db: Database, traceId: string, timestamp: string, chain?: string) {
Expand Down Expand Up @@ -65,8 +65,7 @@ export function selectMessages(db: Database, limit: number, sortBy: string, chai
// if (distinct) messages = selectDistinct(distinct, messages, db, chain, sortBy, limit);
if (chain) messages = sqlite.selectAllRecent(db, "messagesByChain", "*", sortBy, limit).filter((message: any) => message.value.includes(chain));
if (moduleHash) messages = messages.filter((message: any) => message.value.includes(moduleHash));

return toJSON(messages);
return messages
}

// export function selectDistinct(distinct?: string, messages?: any, db?: any, chain?: string, sortBy?: string, limit?: number) {
Expand Down
14 changes: 14 additions & 0 deletions src/fetch/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ export default new OpenApiBuilder()
responses: {200: { description: "OK", content: { "text/plain": {example: "OK"}} } },
},
})
.addPath("/messages", {
get: {
tags: [TAGS.USAGE],
parameters: [
{name: "chain", in: "query", schema: {type: "string"}, required: false},
{name: "moduleHash", in: "query", schema: {type: "string"}, required: false},
{name: "limit", in: "query", schema: {type: "integer"}, required: false},
{name: "sort", in: "query", schema: {type: "string"}, required: false},
{name: "distinct", in: "query", schema: {type: "boolean"}, required: false},
],
summary: "Provides list of recent messages",
responses: {200: {description: "OpenAPI JSON Specification", content: { "application/json": { schema: { type: "string" } } } }},
},
})
.addPath("/health", {
get: {
tags: [TAGS.MONITORING],
Expand Down
4 changes: 2 additions & 2 deletions src/sqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export function createDb(filename: string) {
create(db, "traceId");
create(db, "chain");
createTime(db, "connection");
createRecent(db, "recentMessages")
createRecent(db, "recentMessagesByChain")
createRecent(db, "messages")
createRecent(db, "messagesByChain")
return db;
}

Expand Down

0 comments on commit 34dec0f

Please sign in to comment.