Skip to content

Commit

Permalink
docs: plugin examples
Browse files Browse the repository at this point in the history
  • Loading branch information
CodyTseng committed Mar 24, 2024
1 parent 5213610 commit 6501f98
Showing 1 changed file with 38 additions and 32 deletions.
70 changes: 38 additions & 32 deletions packages/common/src/interfaces/plugin.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,34 @@ export type NostrRelayPlugin = HandleMessagePlugin | BroadcastPlugin;

/**
* A plugin that will be called when a new message is received from a client.
*
* @example
* ```ts
* // message logger plugin
* class MessageLoggerPlugin implements HandleMessagePlugin {
* async handleMessage(ctx, message, next) {
* const startTime = Date.now();
* console.log('Received message:', message);
* const result = await next();
* console.log('Message processed in', Date.now() - startTime, 'ms');
* return result;
* }
* }
*
* // blacklist plugin
* class BlacklistPlugin implements HandleMessagePlugin {
* blacklist = [
* // ...
* ];
*
* async handleMessage(ctx, message, next) {
* if (message[0] === 'EVENT' && blacklist.includes(message[1].pubkey)) {
* return;
* }
* return next();
* }
* }
* ```
*/
export interface HandleMessagePlugin {
/**
Expand All @@ -16,29 +44,6 @@ export interface HandleMessagePlugin {
* @param message The incoming message
* @param next The next function to call the next plugin
* @returns The result of the message handling
*
* @example
* ```ts
* // message logger plugin
* async handleMessage(ctx, message, next) {
* const startTime = Date.now();
* console.log('Received message:', message);
* const result = await next();
* console.log('Message processed in', Date.now() - startTime, 'ms');
* return result;
* }
*
* // blacklist plugin
* const blacklist = [
* // ...
* ];
* async handleMessage(ctx, message, next) {
* if (message[0] === 'EVENT' && blacklist.includes(message[1].pubkey)) {
* return;
* }
* return next();
* }
* ```
*/
handleMessage(
ctx: ClientContext,
Expand All @@ -49,6 +54,16 @@ export interface HandleMessagePlugin {

/**
* A plugin that will be called when an event is broadcasted.
*
* @example
* ```ts
* class RedisBroadcastPlugin implements BroadcastPlugin {
* async broadcast(ctx, event, next) {
* await redis.publish('events', JSON.stringify(event));
* return next();
* }
* }
* ```
*/
export interface BroadcastPlugin {
/**
Expand All @@ -58,15 +73,6 @@ export interface BroadcastPlugin {
* @param event The event to broadcast
* @param next The next function to call the next plugin
* @returns void
*
* @example
* ```ts
* // redis broadcast plugin
* async broadcast(ctx, event, next) {
* await redis.publish('events', JSON.stringify(event));
* return next();
* }
* ```
*/
broadcast(
ctx: ClientContext,
Expand Down

0 comments on commit 6501f98

Please sign in to comment.