npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@klerick/nestjs-json-rpc-sdk

v1.0.0

Published

Client for RPS server, which use @klerick/nestjs-json-rpc

Downloads

30

Readme

nestjs-json-rpc-sdk

The plugin of client for help work with JSON-ROC over nestjs-json-rpc Work with RPC look like call function

Installation

npm install @klerick/nestjs-json-rpc-sdk

Example

Once the installation process is complete, we can import the RpcFactory. For example, we have RPC server which have service which implement this interface:

export type InputType = {
  a: number;
  b: number;
};

export type OutputType = {
  c: string;
  d: string;
};

export interface RpcService {
  someMethod(firstArg: number): Promise<number>;
  someOtherMethod(firstArg: number, secondArgument: number): Promise<string>;
  methodWithObjectParams(a: InputType): Promise<OutputType>;
}
import {
  RpcFactory,
} from '@klerick/nestjs-json-rpc-sdk';
const { rpc, rpcBatch } = RpcFactory(
  {
    rpcHost: `http://localhost:${port}`,
    rpcPath: `/api/rpc`,
    transport: TransportType.HTTP,
  },
  false
);

rpc.RpcService.someMethod(1).sibcribe(r => console.log(r))

const call1 = rpcForBatch.RpcService.someMethod(1);
const call2 = rpcForBatch.RpcService.methodWithObjectParams({
  a: 1,
  b: 2,
});

rpcBatch(call1, call2).sibcribe(([result1, result2]) => console.log(result1, result2))

That's all:)

You can use typescript for type checking:

import {
  RpcFactory,
} from '@klerick/nestjs-json-rpc-sdk';



type MapperRpc = {
  RpcService: RpcService;
};

const { rpc, rpcBatch } = RpcFactory<MapperRpc>(
  {
    rpcHost: `http://localhost:${port}`,
    rpcPath: `/api/rpc`,
    transport: TransportType.HTTP,
  },
  false
);
//TS2345: Argument of type string is not assignable to parameter of type number
const call = rpc.RpcService.someMethod('inccorectParam');
//TS2339: Property IncorrectService does not exist on type MapperRpc
const call2 = rpc.IncorrectService.someMethod(1);
//TS2339: Property incorrectMethod does not exist on type RpcService
const call3 = rpc.RpcService.incorrectMethod(1);

By default, HTTP transport using fetch, but you can set other:

import axios from 'axios';
import {
  RpcFactory,
  axiosTransportFactory,
} from '@klerick/nestjs-json-rpc-sdk';

const { rpc, rpcBatch } = RpcFactory<MapperRpc>(
  {
    rpcHost: `http://localhost:4200`,
    rpcPath: `/api/rpc`,
    transport: TransportType.HTTP,
    httpAgentFactory: axiosTransportFactory(axios),
  },
  false
);

Or you can implement your personal factory.

You should implement HttpAgentFactory type


type Transport<T extends LoopFunc> = (
  body: PayloadRpc<T>
) => Observable<RpcResult<T>>;

type HttpAgentFactory<T extends LoopFunc> = (
  url: string
) => Transport<T>;

if you want to use Promise instead of Observer

!!!!: - you need to use another object for prepare rpc batch call

import axios from 'axios';
import {
  RpcFactory,
  axiosTransportFactory,
} from '@klerick/nestjs-json-rpc-sdk';

const { rpc, rpcBatch, rpcForBatch } = RpcFactory<MapperRpc>(
  {
    rpcHost: `http://localhost:4200`,
    rpcPath: `/api/rpc`,
    transport: TransportType.HTTP,
    httpAgentFactory: axiosTransportFactory(axios),
  },
  true // need true for use promise as result
);
const result = await rpc.RpcService.someMethod(1)

const call1 = rpcForBatch.RpcService.someMethod(1);
const call2 = rpcForBatch.RpcService.methodWithObjectParams({
  a: 1,
  b: 2,
});

const [result1, result2] = await rpcBatch(call1, call2);

For use WebSocket

import {
  RpcFactory,
} from '@klerick/nestjs-json-rpc-sdk';
import { WebSocket as ws } from 'ws';
import { webSocket } from 'rxjs/webSocket';

const someUrl = 'ws://localhost:4200/rpc'
const destroySubject = new Subject<boolean>();
const nativeSocketInstance = webSocket<any>(destroySubject);

const { rpc, rpcBatch } = RpcFactory<MapperRpc>(
  {
    transport: TransportType.WS,
    useWsNativeSocket: true, // - Will be use native WebSocket
    //nativeSocketImplementation: ws, - if you use NodeJS you can use other implementation
    rpcHost: `http://localhost:4200`,
    rpcPath: `/rpc`,
    destroySubject, // - If you need close connection you need call destroySubject.next(true),
    //nativeSocketInstance - you can use your owner socket instance
  },
  false
);

You can use socket.io

import {
  RpcFactory,
} from '@klerick/nestjs-json-rpc-sdk';

import { io } from 'socket.io-client';

const someUrl = 'ws://localhost:4200'
const destroySubject = new Subject<boolean>();
const ioSocketInstance = io(someUrl, { path: '/rpc' })
const { rpc, rpcBatch } = RpcFactory<MapperRpc>(
  {
    transport: TransportType.WS,
    useWsNativeSocket: false, // - Will be use socket.io
    destroySubject, // - If you need close connection you need call destroySubject.next(true),
    ioSocketInstance
  },
  false
);

You can use Angular module:


import {
  JsonRpcAngular,
  JsonRpcAngularConfig,
  TransportType,
} from '@klerick/nestjs-json-rpc-sdk/json-rpc-sdk.module'
import { Subject } from 'rxjs';
import { io } from 'socket.io-client';
import {
  JSON_RPC,
  RPC_BATCH,
  Rpc,
} from '@klerick/nestjs-json-rpc-sdk/json-rpc-sdk.module';

@Component({
  standalone: true,
  selector: 'nestjs-json-api-root',
  templateUrl: './app.component.html',
  styleUrl: './app.component.css',
})
export class AppComponent {
  private rpc = inject<Rpc<MapperRpc>>(JSON_RPC);
  private rpcBatch = inject(RPC_BATCH);
}

const destroySubjectToken = new InjectionToken('destroySubjectToken', {
  factory: () => new Subject<boolean>(),
});

const tokenSocketInst = new InjectionToken('tokenSocketInst', {
  factory: () => webSocket('ws://localhost:4200/rpc'),
});
const tokenIoSocketInst = new InjectionToken('tokenIoSocketInst', {
  factory: () => io('http://localhost:4200', { path: '/rpc' }),
});

const httpConfig: JsonRpcAngularConfig = {
  transport: TransportType.HTTP,
  rpcPath: '/api/rpc',
  rpcHost: 'http://localhost:4200',
};
const wsConfig: JsonRpcAngularConfig = {
  transport: TransportType.WS,
  useWsNativeSocket: true,
  rpcPath: 'rpc',
  rpcHost: 'ws://localhost:4200',
  destroySubjectToken,
};
const wsConfigWithToken: JsonRpcAngularConfig = {
  transport: TransportType.WS,
  useWsNativeSocket: true,
  tokenSocketInst,
  destroySubjectToken,
};
const ioConfig: JsonRpcAngularConfig = {
  transport: TransportType.WS,
  useWsNativeSocket: false,
  destroySubjectToken,
  tokenSocketInst: tokenIoSocketInst,
};

bootstrapApplication(AppComponent, {
  providers: [
    importProvidersFrom(
      JsonRpcAngular.forRoot(httpConfig)
    ),
  ],
}).catch((err) =>
  console.error(err)
);