Skip to content

Commit

Permalink
feat: callback to intercept and modify predicates before re-registration
Browse files Browse the repository at this point in the history
  • Loading branch information
zone117x committed Oct 31, 2024
1 parent a01470e commit 0faf121
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
4 changes: 4 additions & 0 deletions components/client/typescript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Static, Type } from '@fastify/type-provider-typebox';
import { BitcoinIfThisOptionsSchema, BitcoinIfThisSchema } from './schemas/bitcoin/if_this';
import { StacksIfThisOptionsSchema, StacksIfThisSchema } from './schemas/stacks/if_this';
import { logger } from './util/logger';
import { PredicateSchema } from './schemas/predicate';

const EventObserverOptionsSchema = Type.Object({
/** Event observer host name (usually '0.0.0.0') */
Expand Down Expand Up @@ -40,6 +41,9 @@ const EventObserverOptionsSchema = Type.Object({
* up to date. If they become obsolete, we will attempt to re-register them.
*/
predicate_health_check_interval_ms: Type.Optional(Type.Integer({ default: 5000 })),
predicate_re_register_callback: Type.Optional(
Type.Function([PredicateSchema], Type.Promise(PredicateSchema))
),
});
/** Chainhook event observer configuration options */
export type EventObserverOptions = Static<typeof EventObserverOptionsSchema>;
Expand Down
6 changes: 5 additions & 1 deletion components/client/typescript/src/predicates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,15 @@ async function registerPredicate(
authorization_header: `Bearer ${observer.auth_token}`,
},
};
const newPredicate = pendingPredicate as Predicate;
let newPredicate = pendingPredicate as Predicate;
newPredicate.uuid = randomUUID();
if (newPredicate.networks.mainnet) newPredicate.networks.mainnet.then_that = thenThat;
if (newPredicate.networks.testnet) newPredicate.networks.testnet.then_that = thenThat;

if (observer.predicate_re_register_callback) {
newPredicate = await observer.predicate_re_register_callback(newPredicate);
}

const path = observer.node_type === 'chainhook' ? `/v1/chainhooks` : `/v1/observers`;
await request(`${chainhook.base_url}${path}`, {
method: 'POST',
Expand Down

0 comments on commit 0faf121

Please sign in to comment.