Skip to content

Commit

Permalink
breaking-change: improve webhook package (#1854)
Browse files Browse the repository at this point in the history
  • Loading branch information
shineli1984 authored Jun 3, 2024
1 parent f521fc8 commit 3437c9e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
8 changes: 4 additions & 4 deletions packages/minting-backend/sdk/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ImmutableConfiguration, ModuleConfiguration } from '@imtbl/config';
import { BlockchainData } from '@imtbl/blockchain-data';
import { init } from '@imtbl/webhook';
import { handle } from '@imtbl/webhook';
import { setEnvironment, setPublishableApiKey } from '@imtbl/metrics';
import { trackUncaughtException } from 'analytics';
import { mintingPersistence as mintingPersistencePg } from './persistence/pg/postgres';
Expand Down Expand Up @@ -28,7 +28,7 @@ const noopHandlers = {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
zkevmMintRequestUpdated: async (event: MintRequestEvent) => { },
// eslint-disable-next-line @typescript-eslint/no-unused-vars
others: async (..._args: any) => { }
all: async (..._args: any) => { }
};

export class MintingBackendModule {
Expand Down Expand Up @@ -71,12 +71,12 @@ export class MintingBackendModule {
}

async processMint(body: string | Record<string, unknown>, otherHandlers = noopHandlers) {
await init(body, this.baseConfig.environment, {
await handle(body, this.baseConfig.environment, {
zkevmMintRequestUpdated: async (event: MintRequestEvent) => {
await processMint(this.persistence, event, this.logger);
otherHandlers.zkevmMintRequestUpdated(event);
},
others: otherHandlers.others
all: otherHandlers.all
});
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/webhook/sdk/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { init } from './init';
import { handle } from './init';

export {
init,
handle,
};
29 changes: 20 additions & 9 deletions packages/webhook/sdk/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,19 @@ const allowedTopicArnPrefix = {
[Environment.SANDBOX]: 'arn:aws:sns:us-east-2:783421985614:'
};

export const init = async (
/**
* handle will validate webhook message origin and verify signature of the message and calls corresponding handlers passed in.
* @param body The request body to a webhook endpoint in json string or js object form.
* @param env The Immutable environment the webhook is set up for.
* @param handlers The optional handlers object for different events. The `all` handler will be triggered for all event types.
* @returns The event object from the webhook message after validation and verification.
*/
export const handle = async (
body: string | Record<string, unknown>,
env: Environment,
handlers: {
zkevmMintRequestUpdated: (event: any) => Promise<void>;
others?: (event: any) => Promise<void>;
handlers?: {
zkevmMintRequestUpdated?: (event: any) => Promise<void>;
all?: (event: any) => Promise<void>;
}
) => {
const msg: any = await new Promise((resolve, reject) => {
Expand All @@ -38,17 +45,21 @@ export const init = async (
});
});

const event = JSON.parse(msg.Message);
if (msg.Type === 'Notification') {
const event = JSON.parse(msg.Message);
switch (event.event_name) {
case 'imtbl_zkevm_mint_request_updated':
await handlers.zkevmMintRequestUpdated(event);
if (handlers?.zkevmMintRequestUpdated) {
await handlers?.zkevmMintRequestUpdated(event);
}
break;
default:
if (handlers.others) {
await handlers.others(event);
}
break;
}
if (handlers?.all) {
await handlers?.all(event);
}
}

return event;
};

0 comments on commit 3437c9e

Please sign in to comment.