grubba:rpc

v0.2.4Published 2 weeks ago

RPC

What is this package?

This package provides functions for building E2E type-safe RPCs. they are:

  • crateMethod
  • createPublication
  • createRouter

How to use it?

createMethod

1  const test1 = createMethod('name', z.any(), () => 'str');
2  const result = await test1();
3//    ˆ? is string and their value is 'str'

example of use

createMethod accepts 4 arguments:

  • name: string
  • schema: ZodSchema (validator)
  • handler: function that receives the arguments of the method and returns the result
  • config (optional): object with the following properties:
1type Config = {
2  rateLimit: { limit: number, interval: number },
3  methodHooks: {
4    beforeResolve: (args, err: null | Meteor.Error, result: T) => void,
5    afterResolve: (args, result: T) => void,
6    onErrorResolve: (err: Meteor.Error, result: T) => void,
7  }
8}

createPublication

1  const publication = createPublication('findRooms', z.tuple([z.object({level: z.number()})]), ({level}) => Rooms.find({level: level}));
2  const result = publication({level: 1}, (rooms) => console.log(rooms));
3//                                            ˆ? subscription 
4

example of use

createPublication accepts 4 arguments:

  • name: string
  • schema: ZodSchema (validator)
  • handler: function that is being published
  • config (optional): object with the following properties:

note that subscription returns the subscription handler the same way as Meteor.publish

1type Config = {
2  rateLimit: { limit: number, interval: number },
3  methodHooks: {
4    beforeResolve: (args, err: null | Meteor.Error, result: T) => void,
5    afterResolve: (args, result: T) => void,
6    onErrorResolve: (err: Meteor.Error, result: T) => void,
7  }
8}