Skip to content

Commit

Permalink
[FAI-7161] Add getWebhookEvent to faros client for use by Hermes lamb…
Browse files Browse the repository at this point in the history
…da worker (#158)
  • Loading branch information
JoshSauter authored Aug 1, 2023
1 parent 41d38a0 commit c999bc7
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 4 deletions.
31 changes: 29 additions & 2 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import {
Phantom,
SecretName,
UpdateAccount,
WebhookEventStatus,
UpdateWebhookEventStatus,
WebhookEvent,
} from './types';
import {Utils} from './utils';

Expand Down Expand Up @@ -434,7 +435,9 @@ export class FarosClient {
};
}

async updateWebhookEventStatus(status: WebhookEventStatus): Promise<void> {
async updateWebhookEventStatus(
status: UpdateWebhookEventStatus
): Promise<void> {
try {
await this.api.patch(
`/webhooks/${status.webhookId}/events/${status.eventId}`,
Expand All @@ -451,4 +454,28 @@ export class FarosClient {
);
}
}

async getWebhookEvent(
webhookId: string,
eventId: string
): Promise<WebhookEvent> {
try {
const response = await this.api.get(
`/webhooks/${webhookId}/events/${eventId}`
);
let event = response.data;
event = {
...event,
createdAt: event.createdAt ? new Date(event.createdAt) : undefined,
receivedAt: new Date(event.receivedAt),
updatedAt: new Date(event.updatedAt),
};
return event as WebhookEvent;
} catch (err: any) {
throw wrapApiError(
err,
`unable to retrieve event: ${eventId} for webhook: ${webhookId}`
);
}
}
}
20 changes: 19 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,27 @@ export interface Model {
dataSchema: any;
}

export interface WebhookEventStatus {
export interface UpdateWebhookEventStatus {
webhookId: string;
eventId: string;
status: string;
error?: string;
}

export interface WebhookEvent {
id: string;
name: string;
webhookId: string;
event: any;
status: WebhookEventStatus;
error?: string;
createdAt?: Date;
receivedAt: Date;
updatedAt: Date;
}

export enum WebhookEventStatus {
Pending = 'Pending',
Ok = 'OK',
Error = 'Error',
}
31 changes: 30 additions & 1 deletion test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import nock from 'nock';

import {FarosClient, FarosClientConfig, Schema} from '../src';
import {GRAPH_VERSION_HEADER} from '../src/client';
import {Phantom} from '../src/types';
import {Phantom, WebhookEvent, WebhookEventStatus} from '../src/types';

const apiUrl = 'https://test.faros.ai';
const clientConfig = {url: apiUrl, apiKey: 'test-key'};
Expand Down Expand Up @@ -481,4 +481,33 @@ describe('client', () => {
});
mock.done();
});

test('get webhook event', async () => {
const webhookId = 'testWebhookId';
const eventId = 'testEventId';
const now = new Date();
const mockReplyEvent: WebhookEvent = {
id: eventId,
webhookId,
event: {
eventType: 'push',
commit: {
sha: '0xdeadbeef'
}
},
name: 'push',
status: WebhookEventStatus.Pending,
createdAt: now,
receivedAt: now,
updatedAt: now,
};

const mock = nock(apiUrl)
.get(`/webhooks/${webhookId}/events/${eventId}`)
.reply(200, mockReplyEvent);

const event = await client.getWebhookEvent(webhookId, eventId);
expect(event).toEqual(mockReplyEvent);
mock.done();
});
});

0 comments on commit c999bc7

Please sign in to comment.