diff --git a/README.md b/README.md index b992455..2947af3 100644 --- a/README.md +++ b/README.md @@ -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/ diff --git a/src/fetch/messages.ts b/src/fetch/messages.ts index 48bfb31..f1b19ff 100644 --- a/src/fetch/messages.ts +++ b/src/fetch/messages.ts @@ -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"); @@ -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) { @@ -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) { diff --git a/src/fetch/openapi.ts b/src/fetch/openapi.ts index 664cac9..aca1986 100644 --- a/src/fetch/openapi.ts +++ b/src/fetch/openapi.ts @@ -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], diff --git a/src/sqlite.ts b/src/sqlite.ts index beadcb8..22cee87 100644 --- a/src/sqlite.ts +++ b/src/sqlite.ts @@ -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; }