Skip to content

Commit

Permalink
use enum for webhook types, simplify configs and return vals
Browse files Browse the repository at this point in the history
Signed-off-by: david <[email protected]>
  • Loading branch information
daywiss committed Nov 19, 2024
1 parent 143b641 commit 43272ef
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
8 changes: 4 additions & 4 deletions packages/webhooks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ To use the `WebhookFactory`, you need to provide a configuration object and depe

- **Config**: This object should include:
- requireApiKey: boolean; - Should registration of new webhooks require an api key
- enabledEventProcessors: string[]; - What event processors should be enabled, like 'DepositStatus'
- enabledWebhooks: WebhookTypes[]; - What event processors should be enabled, like 'DepositStatus'

- **Dependencies**: This object should include:
- `postgres`: An instance of `DataSource` for database interactions.
Expand All @@ -21,14 +21,14 @@ To use the `WebhookFactory`, you need to provide a configuration object and depe
### Adding an event Example

```js
import { WebhookFactory } from "@repo/webhooks";
import { WebhookFactory, WebhookTypes } from "@repo/webhooks";
import { Logger } from "winston";
import { DataSource } from "@repo/indexer-database";

const webhooks = WebhookFactory(
{
requireApiKey: false,
enabledEventProcessors: ["DepositStatus"],
enableWebhooks: [WebhookTypes.DepositStatus],
},
{ postgres, logger },
);
Expand All @@ -39,7 +39,7 @@ import { DataSource } from "@repo/indexer-database";
// event:JSONValue
// }
// webhooks will be called after a successful write
webhooks.EventProcessorManager.write({
webhooks.write({
type: "DepositStatus",
event: {
originChainId,
Expand Down
4 changes: 2 additions & 2 deletions packages/webhooks/src/eventProcessorManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ export class EventProcessorManager {
);
return eventProcessor;
}
write(event: EventType): void {
write = (event: EventType): void => {
const webhook = this.getEventProcessor(event.type);
webhook.write(event.event);
}
};

async registerWebhook(
params: { type: string; url: string; filter: JSONValue },
Expand Down
14 changes: 9 additions & 5 deletions packages/webhooks/src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ import { DepositStatusProcessor } from "./eventProcessors";
import { WebhookRequestRepository } from "./database/webhookRequestRepository";
import { WebhookRouter } from "./router";

type Config = {
export enum WebhookTypes {
DepositStatus = "DepositStatus",
}

export type Config = {
requireApiKey: boolean;
enabledEventProcessors: string[];
enabledWebhooks: WebhookTypes[];
};
type Dependencies = {
postgres: DataSource;
Expand All @@ -21,7 +25,7 @@ export function WebhookFactory(config: Config, deps: Dependencies) {
const { logger, postgres } = deps;
const notifier = new WebhookNotifier({ logger });
assert(
config.enabledEventProcessors.length,
config.enabledWebhooks.length,
"No webhooks enabled, specify one in config",
);
const eventProcessorManager = new EventProcessorManager(
Expand All @@ -31,7 +35,7 @@ export function WebhookFactory(config: Config, deps: Dependencies) {
logger,
},
);
config.enabledEventProcessors.forEach((name) => {
config.enabledWebhooks.forEach((name) => {
const hooks = new WebhookRequestRepository(new MemoryStore());
switch (name) {
// add more webhook types here
Expand All @@ -53,7 +57,7 @@ export function WebhookFactory(config: Config, deps: Dependencies) {
});
const router = WebhookRouter({ eventProcessorManager });
return {
eventProcessorManager,
write: eventProcessorManager.write,
router,
};
}
Expand Down

0 comments on commit 43272ef

Please sign in to comment.