Skip to content

Commit

Permalink
includes list-event route with pages
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandrosGounis committed Oct 15, 2024
1 parent f72b224 commit b79c82c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
58 changes: 54 additions & 4 deletions src/events/list-events.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,61 @@
import { Env } from "../../worker-configuration";

export async function handleEvents(request: Request, env: Env) {
const prefix = 'event/app-stake-a';
const PREFIX = "event/app-stake-a"

const listResult = await env.EVENTS.list({ prefix });
const HEADERS = { 'Content-Type': 'application/json' }
const BATCH_SIZE = 5
const LIMIT = 200

export async function handleListEvents(request: Request, env: Env) {
const url = new URL(request.url)

const fullParam = url.searchParams.get('full')
const page = url.searchParams.get('page') || '0'
const returnFullResult = fullParam === 'true'

const listResult = await env.EVENTS.list({ prefix: PREFIX });

if (returnFullResult) {
const fullResult = await getFullResult(listResult, page, env)

return new Response(JSON.stringify(fullResult, null, 2), {
headers: HEADERS,
});
}

return new Response(JSON.stringify(listResult.keys, null, 2), {
headers: { 'Content-Type': 'application/json' },
headers: HEADERS
});
}

async function getFullResult(listResult: KVNamespaceListResult<unknown, string>, page: string, env: Env) {
const keys = listResult.keys.map(key => key.name);
const fullResults: Record<string, any>[] = [];


async function processBatch(batch: string[]) {
const results = await Promise.all(
batch.map(async (key) => {
const value = await env.EVENTS.get(key);
return { key, value: JSON.parse(value!) };
})
);

fullResults.push(...results);
};

const pageIndex = parseInt(page) || 0
const startIndex = pageIndex * LIMIT
const endIndex = startIndex + LIMIT - 1

for (let i = startIndex; i < keys.length; i += BATCH_SIZE) {
if (i > endIndex) {
break
}

const batch = keys.slice(i, i + BATCH_SIZE);
await processBatch(batch);
}

return fullResults
}
3 changes: 3 additions & 0 deletions src/router.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { RouteHandler, Router } from 'itty-router';
import { handleDiscover } from './discover/handle-discover';
import { handleEvents } from './events/handle-events';
import { handleListEvents } from './events/list-events';
import { handleExpore } from './explore/handle-explore';
import { handleMetadata } from './metadata/handle-metadata';
import { handleAssets } from './onboarding/discover/handle-assets';
Expand All @@ -24,6 +25,7 @@ export enum ROUTES {
VERSION = '/v1/version',

EVENTS = '/v1/events',
EVENTS_LIST = '/v1/list-events',
}

const router = Router();
Expand All @@ -44,6 +46,7 @@ router.get(ROUTES.PROXY, handleProxy as unknown as RouteHandler)
router.get(ROUTES.VERSION, handleVersion as unknown as RouteHandler)

router.get(ROUTES.EVENTS, handleEvents as unknown as RouteHandler)
// router.get(ROUTES.EVENTS_LIST, handleListEvents as unknown as RouteHandler)

router.all('*', () => new Response('Not Found.', { status: 404 }))

Expand Down

0 comments on commit b79c82c

Please sign in to comment.