@theunderscorer/rpc-in-memory-link

1.1.0 • Public • Published

rpc-in-memory-link

This library was generated with Nx and is part of the Zen, a library collection made by TheUnderScorer.

Adapter for using RPC with in-memory communication.

logo.png

Install

With pnpm:

pnpm add @theunderscorer/rpc-in-memory-link

With npm:

npm install @theunderscorer/rpc-in-memory-link

Usage

import {
  defineRpcSchema,
  RpcClient,
  RpcReceiver,
} from '@theunderscorer/rpc-core';
import { createInMemoryLink } from '@theunderscorer/rpc-in-memory-link';
import { z } from 'zod';

const schema = defineRpcSchema({
  queries: {
    greet: query()
      .needs(
        z.object({
          name: z.string(),
        })
      )
      .returns(z.string()),
  },
});

const memoryLink = createInMemoryLink();

const receiver = new RpcReceiver(schema, [memoryLink.receiver]);

receiver.handleQuery('greet', (payload) => `Hello ${payload.name}`);

async function main() {
  const client = new RpcClient(schema, [memoryLink.client]);

  // Querying the greeting
  const response = await client.query('greet', {
    name: 'John',
  });

  console.log('response', response); // Hello John
}

Building

Run nx build rpc-in-memory-link to build the library.

Running unit tests

Run nx test rpc-in-memory-link to execute the unit tests via Jest.

Readme

Keywords

none

Package Sidebar

Install

npm i @theunderscorer/rpc-in-memory-link

Weekly Downloads

18

Version

1.1.0

License

MIT

Unpacked Size

10 kB

Total Files

6

Last publish

Collaborators

  • theunderscorer