-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nestjs-json-rpc,nestjs-json-rpc-sdk): add ws transport
Add WebSocket transport for backend Add Websocket transport for sdk,
- Loading branch information
Showing
39 changed files
with
842 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
apps/json-api-server-e2e/src/json-api/json-rpc/run-ws-json-rpc.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { INestApplication } from '@nestjs/common'; | ||
import { | ||
ResultRpcFactoryPromise, | ||
ErrorCodeType, | ||
RpcError, | ||
} from '@klerick/nestjs-json-rpc-sdk'; | ||
|
||
import { creatWsRpcSdk, MapperRpc, run } from '../utils/run-application'; | ||
|
||
let app: INestApplication; | ||
|
||
beforeAll(async () => { | ||
app = await run(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await app.close(); | ||
}); | ||
|
||
describe('Run ws json rpc:', () => { | ||
let rpc: ResultRpcFactoryPromise<MapperRpc>['rpc']; | ||
let rpcBatch: ResultRpcFactoryPromise<MapperRpc>['rpcBatch']; | ||
let rpcForBatch: ResultRpcFactoryPromise<MapperRpc>['rpcForBatch']; | ||
beforeEach(() => { | ||
({ rpc, rpcBatch, rpcForBatch } = creatWsRpcSdk()); | ||
}); | ||
|
||
describe('Should be correct response', () => { | ||
it('Should be call one method', async () => { | ||
const input = 1; | ||
const result = await rpc.RpcService.someMethode(input); | ||
expect(result).toBe(input); | ||
}); | ||
|
||
it('Should be correct response batch', async () => { | ||
const input = 1; | ||
const input2 = { | ||
a: 1, | ||
b: 2, | ||
}; | ||
const call1 = rpcForBatch.RpcService.someMethode(input); | ||
const call2 = rpcForBatch.RpcService.methodeWithObjectParams(input2); | ||
|
||
const [result1, result2] = await rpcBatch(call1, call2); | ||
expect(result1).toBe(input); | ||
if ('error' in result2) { | ||
throw Error('Return error'); | ||
} | ||
expect(result2.d).toEqual(`${input2.a}`); | ||
expect(result2.c).toEqual(`${input2.b}`); | ||
}); | ||
}); | ||
|
||
describe('Check error', () => { | ||
it('Should throw an error ' + ErrorCodeType.MethodNotFound, async () => { | ||
const input = 1; | ||
expect.assertions(6); | ||
try { | ||
// @ts-ignore | ||
await rpc.IncorrectService.incorrectMethode(input); | ||
} catch (e) { | ||
expect(e).toBeInstanceOf(RpcError); | ||
expect((e as RpcError).code).toBe(-32601); | ||
expect((e as RpcError).message).toBe(ErrorCodeType.MethodNotFound); | ||
} | ||
try { | ||
// @ts-ignore | ||
await rpc.RpcService.incorrectMethode(input); | ||
} catch (e) { | ||
expect(e).toBeInstanceOf(RpcError); | ||
expect((e as RpcError).code).toBe(-32601); | ||
expect((e as RpcError).message).toBe(ErrorCodeType.MethodNotFound); | ||
} | ||
}); | ||
|
||
it('Should throw an error ' + ErrorCodeType.InvalidParams, async () => { | ||
const input = 'llll'; | ||
expect.assertions(3); | ||
try { | ||
// @ts-ignore | ||
await rpc.RpcService.someMethode(input); | ||
} catch (e) { | ||
expect(e).toBeInstanceOf(RpcError); | ||
expect((e as RpcError).code).toBe(-32602); | ||
expect((e as RpcError).message).toBe(ErrorCodeType.InvalidParams); | ||
} | ||
}); | ||
|
||
it('Should throw an error ' + ErrorCodeType.ServerError, async () => { | ||
const input = 5; | ||
expect.assertions(4); | ||
try { | ||
await rpc.RpcService.someMethode(input); | ||
} catch (e) { | ||
expect(e).toBeInstanceOf(RpcError); | ||
expect((e as RpcError).code).toBe(-32099); | ||
expect((e as RpcError).message).toBe(ErrorCodeType.ServerError); | ||
expect((e as RpcError).data.title).toBe('Custom Error'); | ||
} | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export const JSON_RPC_VERSION = '2.0'; | ||
export const WS_EVENT_NAME = 'rpc'; |
13 changes: 13 additions & 0 deletions
13
libs/json-rpc/nestjs-json-rpc-sdk/src/lib/factory/fetch-transport.factory.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { fromFetch } from 'rxjs/fetch'; | ||
import { LoopFunc, PayloadRpc, RpcResult, Transport } from '../types'; | ||
|
||
export function fetchTransportFactory<T extends LoopFunc>( | ||
url: string | ||
): Transport<T> { | ||
return (body: PayloadRpc<T>) => | ||
fromFetch<RpcResult<T>>(url, { | ||
method: 'post', | ||
body: JSON.stringify(body), | ||
selector: (r) => r.json(), | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.