grubba:rpc

v0.4.4Published 2 weeks ago

RPC

What is this package?

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

  • crateMethod
  • createPublication

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) must be a zod.tuple
  • handler (optional): function that receives the arguments of the method and returns the result
  • config (optional): object with the following properties:
1type Config<S, T> = {
2  rateLimit?: {
3    interval: number,
4    limit: number
5  },
6  methodHooks?: {
7    onBeforeResolve?: Array<(raw: unknown, parsed: S,) => void>;
8    onAfterResolve?: Array<(raw: Maybe<T>, parsed: S, result: T) => void>;
9    onErrorResolve?: Array<(err: Meteor.Error | Error | unknown, raw: Maybe<T>, parsed: S) => void>;
10  }
11}

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) must be a zod.tuple
  • handler (optional): 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<S, T> = {
2  rateLimit?: {
3    interval: number,
4    limit: number
5  },
6  methodHooks?: {
7    onBeforeResolve?: Array<(raw: unknown, parsed: S,) => void>;
8    onAfterResolve?: Array<(raw: Maybe<T>, parsed: S, result: T) => void>;
9    onErrorResolve?: Array<(err: Meteor.Error | Error | unknown, raw: Maybe<T>, parsed: S) => void>;
10  }
11}