From fcbc6b0c11f71064d98d46f1e9d94dee8c26b8ef Mon Sep 17 00:00:00 2001 From: Alex H Date: Sat, 6 Apr 2024 12:32:36 +0200 Subject: [PATCH] docs(nestjs-json-rpc,nestjs-json-rpc-sdk): Add readme --- libs/json-rpc/nestjs-json-rpc-sdk/README.md | 63 ++++++++++++------- .../json-rpc/nestjs-json-rpc-sdk/src/index.ts | 12 +++- .../nestjs-json-rpc-sdk/src/lib/types/rpc.ts | 2 - libs/json-rpc/nestjs-json-rpc/README.md | 17 ++++- libs/json-rpc/nestjs-json-rpc/project.json | 19 ++++++ .../rpc-ws-error-exception.filter.spec.ts | 5 +- 6 files changed, 92 insertions(+), 26 deletions(-) diff --git a/libs/json-rpc/nestjs-json-rpc-sdk/README.md b/libs/json-rpc/nestjs-json-rpc-sdk/README.md index cce3ecda..fd3c7b3b 100644 --- a/libs/json-rpc/nestjs-json-rpc-sdk/README.md +++ b/libs/json-rpc/nestjs-json-rpc-sdk/README.md @@ -20,7 +20,7 @@ npm install @klerick/nestjs-json-rpc-sdk ## Example Once the installation process is complete, we can import the **RpcFactory**. -For example, we have RPC serve which have service which implement this interface: +For example, we have RPC server which have service which implement this interface: ```typescript export type InputType = { @@ -34,9 +34,9 @@ export type OutputType = { }; export interface RpcService { - someMethode(firstArg: number): Promise; - someOtherMethode(firstArg: number, secondArgument: number): Promise; - methodeWithObjectParams(a: InputType): Promise; + someMethod(firstArg: number): Promise; + someOtherMethod(firstArg: number, secondArgument: number): Promise; + methodWithObjectParams(a: InputType): Promise; } ``` @@ -53,10 +53,10 @@ const { rpc, rpcBatch } = RpcFactory( false ); -rpc.RpcService.someMethode(1).sibcribe(r => console.log(r)) +rpc.RpcService.someMethod(1).sibcribe(r => console.log(r)) -const call1 = rpcForBatch.RpcService.someMethode(1); -const call2 = rpcForBatch.RpcService.methodeWithObjectParams({ +const call1 = rpcForBatch.RpcService.someMethod(1); +const call2 = rpcForBatch.RpcService.methodWithObjectParams({ a: 1, b: 2, }); @@ -66,7 +66,7 @@ rpcBatch(call1, call2).sibcribe(([result1, result2]) => console.log(result1, res ``` That's all:) -You can use typescript type checking: +You can use typescript for type checking: ```typescript import { RpcFactory, @@ -87,7 +87,11 @@ const { rpc, rpcBatch } = RpcFactory( false ); //TS2345: Argument of type string is not assignable to parameter of type number -const call = rpcForBatch.RpcService.someMethode('inccorectParam'); +const call = rpcForBatch.RpcService.someMethod('inccorectParam'); +//TS2339: Property IncorrectService does not exist on type MapperRpc +const call2 = rpcForBatch.IncorrectService.someMethod(1); +//TS2339: Property incorrectMethod does not exist on type RpcService +const call3 = rpcForBatch.RpcService.incorrectMethod(1); ``` @@ -111,6 +115,22 @@ const { rpc, rpcBatch } = RpcFactory( false ); ``` +Or you can implement your personal factory. + +You should implement **HttpAgentFactory** type + +```typescript + +type Transport = ( + body: PayloadRpc +) => Observable>; + +type HttpAgentFactory = ( + url: string +) => Transport; +``` + + if you want to use **Promise** instead of **Observer** @@ -129,12 +149,12 @@ const { rpc, rpcBatch, rpcForBatch } = RpcFactory( transport: TransportType.HTTP, httpAgentFactory: axiosTransportFactory(axios), }, - false + true // need true for use promise as result ); -const result = await rpcForBatch.RpcService.someMethode(1) +const result = await rpcForBatch.RpcService.someMethod(1) -const call1 = rpcForBatch.RpcService.someMethode(1); -const call2 = rpcForBatch.RpcService.methodeWithObjectParams({ +const call1 = rpcForBatch.RpcService.someMethod(1); +const call2 = rpcForBatch.RpcService.methodWithObjectParams({ a: 1, b: 2, }); @@ -144,20 +164,21 @@ const [result1, result2] = await rpcBatch(call1, call2); For use **WebSocket** ```typescript -import axios from 'axios'; import { RpcFactory, } from '@klerick/nestjs-json-rpc-sdk'; -import { WebSocket } from 'ws'; +import { WebSocket as ws } from 'ws'; import { webSocket } from 'rxjs/webSocket'; + const someUrl = 'ws://localhost:4200/rpc' const destroySubject = new Subject(); const nativeSocketInstance = webSocket(destroySubject); -const { rpc, rpcBatch, rpcForBatch } = RpcFactory( + +const { rpc, rpcBatch } = RpcFactory( { transport: TransportType.WS, useWsNativeSocket: true, // - Will be use native WebSocket - //nativeSocketImplementation: WebSocket, - if you use NodeJS you can use other implementation + //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), @@ -168,7 +189,6 @@ const { rpc, rpcBatch, rpcForBatch } = RpcFactory( ``` You can use **socket.io** ```typescript -import axios from 'axios'; import { RpcFactory, } from '@klerick/nestjs-json-rpc-sdk'; @@ -178,10 +198,10 @@ import { io } from 'socket.io-client'; const someUrl = 'ws://localhost:4200' const destroySubject = new Subject(); const ioSocketInstance = io(someUrl, { path: '/rpc' }) -const { rpc, rpcBatch, rpcForBatch } = RpcFactory( +const { rpc, rpcBatch } = RpcFactory( { transport: TransportType.WS, - useWsNativeSocket: false, // - Will be use native WebSocket + useWsNativeSocket: false, // - Will be use socket.io destroySubject, // - If you need close connection you need call destroySubject.next(true), ioSocketInstance }, @@ -189,7 +209,7 @@ const { rpc, rpcBatch, rpcForBatch } = RpcFactory( ); ``` -You can use module for Angular: +You can use Angular module: ```typescript @@ -199,6 +219,7 @@ import { 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, diff --git a/libs/json-rpc/nestjs-json-rpc-sdk/src/index.ts b/libs/json-rpc/nestjs-json-rpc-sdk/src/index.ts index 396c2641..a4a08917 100644 --- a/libs/json-rpc/nestjs-json-rpc-sdk/src/index.ts +++ b/libs/json-rpc/nestjs-json-rpc-sdk/src/index.ts @@ -4,4 +4,14 @@ export { ResultRpcFactoryPromise, ResultRpcFactory, } from './lib/factory'; -export { RpcConfig, TransportType, ErrorCodeType, RpcError } from './lib/types'; +export { + RpcConfig, + TransportType, + ErrorCodeType, + RpcError, + LoopFunc, + HttpAgentFactory, + Transport, + PayloadRpc, + RpcResult, +} from './lib/types'; diff --git a/libs/json-rpc/nestjs-json-rpc-sdk/src/lib/types/rpc.ts b/libs/json-rpc/nestjs-json-rpc-sdk/src/lib/types/rpc.ts index 23be2cc3..98792a29 100644 --- a/libs/json-rpc/nestjs-json-rpc-sdk/src/lib/types/rpc.ts +++ b/libs/json-rpc/nestjs-json-rpc-sdk/src/lib/types/rpc.ts @@ -13,8 +13,6 @@ export type PayloadRpc = { id: number; }; -export type IdRequest = () => number; - export type RpcResultObject = { jsonrpc: JsonRpcVersion; result: ReturnGenericType; diff --git a/libs/json-rpc/nestjs-json-rpc/README.md b/libs/json-rpc/nestjs-json-rpc/README.md index f034d5e5..19e8e900 100644 --- a/libs/json-rpc/nestjs-json-rpc/README.md +++ b/libs/json-rpc/nestjs-json-rpc/README.md @@ -59,6 +59,20 @@ export class AppModule { ``` `wsConfig` - is GatewayMetadata from `@nestjs/websockets/interfaces`; +***!!!!***: - NestJs by default using **socket.io** adapter, if you want to use native WebSocket, you should use **WsAdapter** +```typescript +import { WsAdapter } from '@nestjs/platform-ws'; +import { NestFactory } from '@nestjs/core'; +import { AppModule } from './app/app.module'; + +async function bootstrap() { + const app = await NestFactory.create(AppModule); + app.useWebSocketAdapter(new WsAdapter(app)); + app.init() + await app.listen(3000); +} +``` + To allow service to your RPC server, you should create class and add to providers the root **AppModule**. ```typescript @@ -103,6 +117,7 @@ export class AppModule { } ``` `@RpcHandler` - decorator which mark class as RPC service + `@RpcParamsPipe` - decorator for validate input data, @@ -112,7 +127,7 @@ After it, you can call you RPC service: POST /rpc ``` -- **body** - Create new User and add link to address +- **body** - for http request ```json {"jsonrpc": "2.0", "method": "RpcService.methodeWithObjectParams", "params": {"a": 23}, "id": 1} diff --git a/libs/json-rpc/nestjs-json-rpc/project.json b/libs/json-rpc/nestjs-json-rpc/project.json index 2e8aaeaa..bb46a4c0 100644 --- a/libs/json-rpc/nestjs-json-rpc/project.json +++ b/libs/json-rpc/nestjs-json-rpc/project.json @@ -27,6 +27,25 @@ "codeCoverage": true, "coverageReporters": ["json-summary"] } + }, + "upload-badge": { + "executor": "nx:run-commands", + "dependsOn": [ + { + "target": "test" + } + ], + "options": { + "commands": ["node tools/scripts/upload-badge.mjs nestjs-json-rpc"], + "cwd": "./", + "parallel": false, + "outputPath": "{workspaceRoot}/libs/json-rpc/nestjs-json-rpc" + } + }, + "nx-release-publish": { + "options": { + "packageRoot": "dist/libs/json-rpc/nestjs-json-rpc" + } } }, "tags": [] diff --git a/libs/json-rpc/nestjs-json-rpc/src/lib/modules/ws-socket-transport/filter/rpc-ws-error-exception.filter.spec.ts b/libs/json-rpc/nestjs-json-rpc/src/lib/modules/ws-socket-transport/filter/rpc-ws-error-exception.filter.spec.ts index 485a61e5..69ae8c9f 100644 --- a/libs/json-rpc/nestjs-json-rpc/src/lib/modules/ws-socket-transport/filter/rpc-ws-error-exception.filter.spec.ts +++ b/libs/json-rpc/nestjs-json-rpc/src/lib/modules/ws-socket-transport/filter/rpc-ws-error-exception.filter.spec.ts @@ -10,7 +10,10 @@ import { WS_EVENT_NAME } from '../constants'; describe('rpc-ws-error-exception.filter', () => { describe('WebSocket', () => { - const WebSocketInst = new WebSocket('http://0.0.0.0', {}); + const WebSocketInst = new WebSocket( + 'wss://demo.piesocket.com/v3/channel_123', + {} + ); let argumentsHost: ArgumentsHost; let getClient: () => WebSocket;