|
| 1 | +<p align='center'> |
| 2 | + <a href="https://www.npmjs.com/package/json-api-nestjs" target="_blank"><img src="https://img.shields.io/npm/v/@klerick/nestjs-json-rpc-sdk.svg" alt="NPM Version" /></a> |
| 3 | + <a href="https://www.npmjs.com/package/json-api-nestjs" target="_blank"><img src="https://img.shields.io/npm/l/@klerick/nestjs-json-rpc-sdk.svg" alt="Package License" /></a> |
| 4 | + <a href="https://www.npmjs.com/package/json-api-nestjs" target="_blank"><img src="https://img.shields.io/npm/dm/@klerick/nestjs-json-rpc-sdk.svg" alt="NPM Downloads" /></a> |
| 5 | + <a href="http://commitizen.github.io/cz-cli/" target="_blank"><img src="https://img.shields.io/badge/commitizen-friendly-brightgreen.svg" alt="Commitizen friendly" /></a> |
| 6 | + <img src="https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/klerick/397d521f54660656f2fd6195ec482581/raw/nestjs-json-rpc-sdk.json" alt="Coverage Badge" /> |
| 7 | +</p> |
| 8 | + |
1 | 9 | # nestjs-json-rpc-sdk
|
2 | 10 |
|
3 |
| -This library was generated with [Nx](https://nx.dev). |
| 11 | +The plugin of client for help work with JSON-ROC over [nestjs-json-rpc](https://www.npmjs.com/package/@klerick/nestjs-json-rpc) |
| 12 | +Work with RPC look like call function |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +```bash $ |
| 17 | +npm install @klerick/nestjs-json-rpc-sdk |
| 18 | +``` |
| 19 | + |
| 20 | +## Example |
| 21 | + |
| 22 | +Once the installation process is complete, we can import the **RpcFactory**. |
| 23 | +For example, we have RPC serve which have service which implement this interface: |
| 24 | + |
| 25 | +```typescript |
| 26 | +export type InputType = { |
| 27 | + a: number; |
| 28 | + b: number; |
| 29 | +}; |
| 30 | + |
| 31 | +export type OutputType = { |
| 32 | + c: string; |
| 33 | + d: string; |
| 34 | +}; |
| 35 | + |
| 36 | +export interface RpcService { |
| 37 | + someMethode(firstArg: number): Promise<number>; |
| 38 | + someOtherMethode(firstArg: number, secondArgument: number): Promise<string>; |
| 39 | + methodeWithObjectParams(a: InputType): Promise<OutputType>; |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +```typescript |
| 44 | +import { |
| 45 | + RpcFactory, |
| 46 | +} from '@klerick/nestjs-json-rpc-sdk'; |
| 47 | +const { rpc, rpcBatch } = RpcFactory( |
| 48 | + { |
| 49 | + rpcHost: `http://localhost:${port}`, |
| 50 | + rpcPath: `/api/rpc`, |
| 51 | + transport: TransportType.HTTP, |
| 52 | + }, |
| 53 | + false |
| 54 | +); |
| 55 | + |
| 56 | +rpc.RpcService.someMethode(1).sibcribe(r => console.log(r)) |
| 57 | + |
| 58 | +const call1 = rpcForBatch.RpcService.someMethode(1); |
| 59 | +const call2 = rpcForBatch.RpcService.methodeWithObjectParams({ |
| 60 | + a: 1, |
| 61 | + b: 2, |
| 62 | +}); |
| 63 | + |
| 64 | +rpcBatch(call1, call2).sibcribe(([result1, result2]) => console.log(result1, result2)) |
| 65 | + |
| 66 | +``` |
| 67 | +That's all:) |
| 68 | + |
| 69 | +You can use typescript type checking: |
| 70 | +```typescript |
| 71 | +import { |
| 72 | + RpcFactory, |
| 73 | +} from '@klerick/nestjs-json-rpc-sdk'; |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | +type MapperRpc = { |
| 78 | + RpcService: RpcService; |
| 79 | +}; |
| 80 | + |
| 81 | +const { rpc, rpcBatch } = RpcFactory<MapperRpc>( |
| 82 | + { |
| 83 | + rpcHost: `http://localhost:${port}`, |
| 84 | + rpcPath: `/api/rpc`, |
| 85 | + transport: TransportType.HTTP, |
| 86 | + }, |
| 87 | + false |
| 88 | +); |
| 89 | +//TS2345: Argument of type string is not assignable to parameter of type number |
| 90 | +const call = rpcForBatch.RpcService.someMethode('inccorectParam'); |
| 91 | + |
| 92 | +``` |
| 93 | + |
| 94 | + |
| 95 | +By default, HTTP transport using fetch, but you can set other: |
| 96 | + |
| 97 | +```typescript |
| 98 | +import axios from 'axios'; |
| 99 | +import { |
| 100 | + RpcFactory, |
| 101 | + axiosTransportFactory, |
| 102 | +} from '@klerick/nestjs-json-rpc-sdk'; |
| 103 | + |
| 104 | +const { rpc, rpcBatch } = RpcFactory<MapperRpc>( |
| 105 | + { |
| 106 | + rpcHost: `http://localhost:4200`, |
| 107 | + rpcPath: `/api/rpc`, |
| 108 | + transport: TransportType.HTTP, |
| 109 | + httpAgentFactory: axiosTransportFactory(axios), |
| 110 | + }, |
| 111 | + false |
| 112 | +); |
| 113 | +``` |
| 114 | + |
| 115 | +if you want to use **Promise** instead of **Observer** |
| 116 | + |
| 117 | +***!!!!***: - you need to use another object for prepare rpc batch call |
| 118 | +```typescript |
| 119 | +import axios from 'axios'; |
| 120 | +import { |
| 121 | + RpcFactory, |
| 122 | + axiosTransportFactory, |
| 123 | +} from '@klerick/nestjs-json-rpc-sdk'; |
| 124 | + |
| 125 | +const { rpc, rpcBatch, rpcForBatch } = RpcFactory<MapperRpc>( |
| 126 | + { |
| 127 | + rpcHost: `http://localhost:4200`, |
| 128 | + rpcPath: `/api/rpc`, |
| 129 | + transport: TransportType.HTTP, |
| 130 | + httpAgentFactory: axiosTransportFactory(axios), |
| 131 | + }, |
| 132 | + false |
| 133 | +); |
| 134 | +const result = await rpcForBatch.RpcService.someMethode(1) |
| 135 | + |
| 136 | +const call1 = rpcForBatch.RpcService.someMethode(1); |
| 137 | +const call2 = rpcForBatch.RpcService.methodeWithObjectParams({ |
| 138 | + a: 1, |
| 139 | + b: 2, |
| 140 | +}); |
| 141 | + |
| 142 | +const [result1, result2] = await rpcBatch(call1, call2); |
| 143 | +``` |
| 144 | + |
| 145 | +For use **WebSocket** |
| 146 | +```typescript |
| 147 | +import axios from 'axios'; |
| 148 | +import { |
| 149 | + RpcFactory, |
| 150 | +} from '@klerick/nestjs-json-rpc-sdk'; |
| 151 | +import { WebSocket } from 'ws'; |
| 152 | +import { webSocket } from 'rxjs/webSocket'; |
| 153 | +const someUrl = 'ws://localhost:4200/rpc' |
| 154 | +const destroySubject = new Subject<boolean>(); |
| 155 | +const nativeSocketInstance = webSocket<any>(destroySubject); |
| 156 | +const { rpc, rpcBatch, rpcForBatch } = RpcFactory<MapperRpc>( |
| 157 | + { |
| 158 | + transport: TransportType.WS, |
| 159 | + useWsNativeSocket: true, // - Will be use native WebSocket |
| 160 | + //nativeSocketImplementation: WebSocket, - if you use NodeJS you can use other implementation |
| 161 | + rpcHost: `http://localhost:4200`, |
| 162 | + rpcPath: `/rpc`, |
| 163 | + destroySubject, // - If you need close connection you need call destroySubject.next(true), |
| 164 | + //nativeSocketInstance - you can use your owner socket instance |
| 165 | + }, |
| 166 | + false |
| 167 | +); |
| 168 | +``` |
| 169 | +You can use **socket.io** |
| 170 | +```typescript |
| 171 | +import axios from 'axios'; |
| 172 | +import { |
| 173 | + RpcFactory, |
| 174 | +} from '@klerick/nestjs-json-rpc-sdk'; |
| 175 | + |
| 176 | +import { io } from 'socket.io-client'; |
| 177 | + |
| 178 | +const someUrl = 'ws://localhost:4200' |
| 179 | +const destroySubject = new Subject<boolean>(); |
| 180 | +const ioSocketInstance = io(someUrl, { path: '/rpc' }) |
| 181 | +const { rpc, rpcBatch, rpcForBatch } = RpcFactory<MapperRpc>( |
| 182 | + { |
| 183 | + transport: TransportType.WS, |
| 184 | + useWsNativeSocket: false, // - Will be use native WebSocket |
| 185 | + destroySubject, // - If you need close connection you need call destroySubject.next(true), |
| 186 | + ioSocketInstance |
| 187 | + }, |
| 188 | + false |
| 189 | +); |
| 190 | +``` |
| 191 | + |
| 192 | +You can use module for Angular: |
| 193 | + |
| 194 | +```typescript |
| 195 | + |
| 196 | +import { |
| 197 | + JsonRpcAngular, |
| 198 | + JsonRpcAngularConfig, |
| 199 | + TransportType, |
| 200 | +} from '@klerick/nestjs-json-rpc-sdk/json-rpc-sdk.module' |
| 201 | +import { Subject } from 'rxjs'; |
| 202 | +import { |
| 203 | + JSON_RPC, |
| 204 | + RPC_BATCH, |
| 205 | + Rpc, |
| 206 | +} from '@klerick/nestjs-json-rpc-sdk/json-rpc-sdk.module'; |
| 207 | + |
| 208 | +@Component({ |
| 209 | + standalone: true, |
| 210 | + selector: 'nestjs-json-api-root', |
| 211 | + templateUrl: './app.component.html', |
| 212 | + styleUrl: './app.component.css', |
| 213 | +}) |
| 214 | +export class AppComponent { |
| 215 | + private rpc = inject<Rpc<MapperRpc>>(JSON_RPC); |
| 216 | + private rpcBatch = inject(RPC_BATCH); |
| 217 | +} |
| 218 | + |
| 219 | +const destroySubjectToken = new InjectionToken('destroySubjectToken', { |
| 220 | + factory: () => new Subject<boolean>(), |
| 221 | +}); |
4 | 222 |
|
5 |
| -## Building |
| 223 | +const tokenSocketInst = new InjectionToken('tokenSocketInst', { |
| 224 | + factory: () => webSocket('ws://localhost:4200/rpc'), |
| 225 | +}); |
| 226 | +const tokenIoSocketInst = new InjectionToken('tokenIoSocketInst', { |
| 227 | + factory: () => io('http://localhost:4200', { path: '/rpc' }), |
| 228 | +}); |
6 | 229 |
|
7 |
| -Run `nx build nestjs-json-rpc-sdk` to build the library. |
| 230 | +const httpConfig: JsonRpcAngularConfig = { |
| 231 | + transport: TransportType.HTTP, |
| 232 | + rpcPath: '/api/rpc', |
| 233 | + rpcHost: 'http://localhost:4200', |
| 234 | +}; |
| 235 | +const wsConfig: JsonRpcAngularConfig = { |
| 236 | + transport: TransportType.WS, |
| 237 | + useWsNativeSocket: true, |
| 238 | + rpcPath: 'rpc', |
| 239 | + rpcHost: 'ws://localhost:4200', |
| 240 | + destroySubjectToken, |
| 241 | +}; |
| 242 | +const wsConfigWithToken: JsonRpcAngularConfig = { |
| 243 | + transport: TransportType.WS, |
| 244 | + useWsNativeSocket: true, |
| 245 | + tokenSocketInst, |
| 246 | + destroySubjectToken, |
| 247 | +}; |
| 248 | +const ioConfig: JsonRpcAngularConfig = { |
| 249 | + transport: TransportType.WS, |
| 250 | + useWsNativeSocket: false, |
| 251 | + destroySubjectToken, |
| 252 | + tokenSocketInst: tokenIoSocketInst, |
| 253 | +}; |
8 | 254 |
|
9 |
| -## Running unit tests |
| 255 | +bootstrapApplication(AppComponent, { |
| 256 | + providers: [ |
| 257 | + importProvidersFrom( |
| 258 | + JsonRpcAngular.forRoot(httpConfig) |
| 259 | + ), |
| 260 | + ], |
| 261 | +}).catch((err) => |
| 262 | + console.error(err) |
| 263 | +); |
10 | 264 |
|
11 |
| -Run `nx test nestjs-json-rpc-sdk` to execute the unit tests via [Jest](https://jestjs.io). |
| 265 | +``` |
0 commit comments