All files / json-crdt-server/src/routes/pubsub publish.ts

100% Statements 9/9
100% Branches 0/0
75% Functions 3/4
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36    4x 4x 93x 93x                     93x         93x                 93x 8x 8x      
import type {RouteDeps, Router, RouterBase} from '../types';
 
export const publish =
  ({t, services}: RouteDeps) =>
  <R extends RouterBase>(r: Router<R>) => {
    const Request = t.Object(
      t.Key('channel', t.str).options({
        title: 'Channel name',
        description: 'The name of the channel to publish to.',
      }),
      t.Key('message', t.any).options({
        title: 'Message',
        description: 'The message to publish to the channel. Can be any JSON/CBOR value.',
      }),
    );
 
    const Response = t.obj.options({
      title: 'Publish response',
      description: 'An empty object.',
    });
 
    const Func = t.Function(Request, Response).options({
      title: 'Publish to channel',
      intro: 'Publish a message to a channel.',
      description:
        'This method publishes a message to a global channel with the given `channel` name. ' +
        'All subscribers to the channel will receive the message. The `message` can be any value. ' +
        'The most efficient way to publish a message is to send a primitive or a `Uint8Array` buffer.',
    });
 
    return r.add('pubsub.publish', Func, async ({channel, message}) => {
      services.pubsub.publish(channel, message).catch(() => {});
      return {};
    });
  };