Skip to content
This repository was archived by the owner on Mar 18, 2026. It is now read-only.

Commit 235537e

Browse files
committed
feat: Add endpoints to get a specific event or all events
1 parent 51fffb3 commit 235537e

1 file changed

Lines changed: 80 additions & 1 deletion

File tree

src/durable-objects/chainhooks-do.ts

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export class ChainhooksDO extends DurableObject<Env> {
1010
// Configuration constants
1111
private readonly BASE_PATH: string = '/chainhooks';
1212
private readonly CACHE_PREFIX: string = this.BASE_PATH.replaceAll('/', '');
13-
private readonly SUPPORTED_ENDPOINTS: string[] = ['/post-event'];
13+
private readonly SUPPORTED_ENDPOINTS: string[] = ['/post-event', '/events', '/events/:id'];
1414

1515
constructor(ctx: DurableObjectState, env: Env) {
1616
super(ctx, env);
@@ -53,6 +53,29 @@ export class ChainhooksDO extends DurableObject<Env> {
5353
return await this.handlePostEvent(request);
5454
}
5555

56+
// Handle get all events endpoint
57+
if (endpoint === '/events') {
58+
if (method !== 'GET') {
59+
throw new ApiError(ErrorCode.INVALID_REQUEST, {
60+
reason: `Method ${method} not allowed for this endpoint. Use GET.`,
61+
});
62+
}
63+
64+
return await this.handleGetAllEvents();
65+
}
66+
67+
// Handle get specific event endpoint
68+
if (endpoint.startsWith('/events/')) {
69+
if (method !== 'GET') {
70+
throw new ApiError(ErrorCode.INVALID_REQUEST, {
71+
reason: `Method ${method} not allowed for this endpoint. Use GET.`,
72+
});
73+
}
74+
75+
const eventId = endpoint.replace('/events/', '');
76+
return await this.handleGetEvent(eventId);
77+
}
78+
5679
// If we get here, the endpoint is not supported
5780
throw new ApiError(ErrorCode.NOT_FOUND, {
5881
resource: endpoint,
@@ -108,4 +131,60 @@ export class ChainhooksDO extends DurableObject<Env> {
108131
});
109132
}
110133
}
134+
135+
private async handleGetEvent(eventId: string): Promise<any> {
136+
const logger = Logger.getInstance(this.env);
137+
138+
try {
139+
// Retrieve the event from storage
140+
const event = await this.ctx.storage.get(`event_${eventId}`);
141+
142+
if (!event) {
143+
throw new ApiError(ErrorCode.NOT_FOUND, {
144+
resource: `Event with ID ${eventId}`,
145+
});
146+
}
147+
148+
return {
149+
event,
150+
};
151+
} catch (error) {
152+
if (error instanceof ApiError) {
153+
throw error;
154+
}
155+
156+
logger.error(`Error retrieving event ${eventId}`, error instanceof Error ? error : new Error(String(error)));
157+
throw new ApiError(ErrorCode.INTERNAL_ERROR, {
158+
reason: `Failed to retrieve event ${eventId}`,
159+
});
160+
}
161+
}
162+
163+
private async handleGetAllEvents(): Promise<any> {
164+
const logger = Logger.getInstance(this.env);
165+
166+
try {
167+
// Get all keys that start with "event_"
168+
const eventKeys = await this.ctx.storage.list({ prefix: 'event_' });
169+
170+
// Create an array to hold all events
171+
const events: Record<string, any> = {};
172+
173+
// Retrieve each event and add it to the array
174+
for (const [key, value] of eventKeys) {
175+
const eventId = key.replace('event_', '');
176+
events[eventId] = value;
177+
}
178+
179+
return {
180+
events,
181+
count: Object.keys(events).length,
182+
};
183+
} catch (error) {
184+
logger.error('Error retrieving all events', error instanceof Error ? error : new Error(String(error)));
185+
throw new ApiError(ErrorCode.INTERNAL_ERROR, {
186+
reason: 'Failed to retrieve events',
187+
});
188+
}
189+
}
111190
}

0 commit comments

Comments
 (0)