Skip to content

Commit bd08c75

Browse files
committed
docs(nestjs-json-rpc,nestjs-json-rpc-sdk): Add readme
1 parent e09dd84 commit bd08c75

File tree

6 files changed

+388
-66
lines changed

6 files changed

+388
-66
lines changed

apps/json-api-server/src/app/rpc/rpc.module.ts

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,14 @@
1-
import { Injectable, Module, ParseIntPipe, UsePipes } from '@nestjs/common';
1+
import { Module } from '@nestjs/common';
22
import { NestjsJsonRpcModule, TransportType } from '@klerick/nestjs-json-rpc';
33
import { RpcService } from './service/rpc.service';
4-
import {
5-
MessageBody,
6-
SubscribeMessage,
7-
WebSocketGateway,
8-
WsResponse,
9-
} from '@nestjs/websockets';
10-
import { from, Observable } from 'rxjs';
11-
import { map } from 'rxjs/operators';
12-
import {
13-
ArgumentMetadata,
14-
PipeTransform,
15-
} from '@nestjs/common/interfaces/features/pipe-transform.interface';
16-
17-
@WebSocketGateway({
18-
cors: {
19-
origin: '*',
20-
},
21-
path: '/rpc/',
22-
})
23-
class TesWebSocketService {
24-
constructor() {
25-
console.log(1213);
26-
}
27-
28-
@UsePipes(ParseIntPipe)
29-
@SubscribeMessage('events')
30-
findAll(@MessageBody() data: number): Observable<WsResponse<number>> {
31-
return from([1, 2, 3]).pipe(
32-
map((item) => ({ event: 'events', data: item }))
33-
);
34-
}
35-
}
364

375
@Module({
386
imports: [
39-
NestjsJsonRpcModule.forRootAsync({
7+
NestjsJsonRpcModule.forRoot({
408
path: 'rpc',
419
transport: TransportType.HTTP,
4210
}),
43-
NestjsJsonRpcModule.forRootAsync({
11+
NestjsJsonRpcModule.forRoot({
4412
transport: TransportType.WS,
4513
wsConfig: {
4614
path: '/rpc',

libs/json-api/json-api-nestjs-sdk/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
# json-api-nestjs-sdk
1010

11-
The plugin of Angular for help work with JSON API over [json-api-nestjs](https://www.npmjs.com/package/json-api-nestjs)
11+
The plugin of client for help work with JSON API over [json-api-nestjs](https://www.npmjs.com/package/json-api-nestjs)
1212

1313

1414
## Installation
Lines changed: 259 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,265 @@
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+
19
# nestjs-json-rpc-sdk
210

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+
});
4222

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+
});
6229

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+
};
8254

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+
);
10264

11-
Run `nx test nestjs-json-rpc-sdk` to execute the unit tests via [Jest](https://jestjs.io).
265+
```

libs/json-rpc/nestjs-json-rpc-sdk/src/lib/types/config.ts

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import { PayloadRpc, RpcResult, Transport } from './rpc';
1+
import { Transport } from './rpc';
22
import { HttpAgentFactory, LoopFunc } from './utils';
33

44
import type { Socket } from 'socket.io-client';
55
import { Subject } from 'rxjs';
66
import { WebSocketSubject } from 'rxjs/internal/observable/dom/WebSocketSubject';
7-
import { WsResponse } from '../factory/ws-transport.factory';
87

98
export enum TransportType {
109
HTTP,
@@ -23,20 +22,6 @@ export type RpcTransportHttpConfig = {
2322

2423
export type RpcHttpConfig = RpcMainHttpConfig & RpcTransportHttpConfig;
2524

26-
type UseNativeSocket =
27-
| {
28-
useWsNativeSocket: true;
29-
rpcPath: string;
30-
rpcHost: string;
31-
webSocketCtor?: {
32-
new (url: string, protocols?: string | string[]): any;
33-
};
34-
}
35-
| {
36-
useWsNativeSocket: false;
37-
webSocketCtor: Socket;
38-
};
39-
4025
export type RpcNativeSocketFactory = {
4126
rpcPath: string;
4227
rpcHost: string;
@@ -46,9 +31,7 @@ export type RpcNativeSocketFactory = {
4631
};
4732

4833
export type RpcNativeSocketInstance = {
49-
nativeSocketInstance: WebSocketSubject<
50-
WsResponse<PayloadRpc<LoopFunc> | RpcResult<LoopFunc>>
51-
>;
34+
nativeSocketInstance: WebSocketSubject<any>;
5235
};
5336

5437
export type RpcNativeSocketTrue = {

0 commit comments

Comments
 (0)