-
Notifications
You must be signed in to change notification settings - Fork 11
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-sdk): sdk for rpc
- http transport - factory for native js - angular module Closes: #77
- Loading branch information
Showing
47 changed files
with
1,117 additions
and
90 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"extends": ["../../../.eslintrc.base.json"], | ||
"ignorePatterns": ["!**/*"], | ||
"overrides": [ | ||
{ | ||
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.ts", "*.tsx"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.js", "*.jsx"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.json"], | ||
"parser": "jsonc-eslint-parser", | ||
"rules": { | ||
"@nx/dependency-checks": "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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# nestjs-json-rpc-sdk | ||
|
||
This library was generated with [Nx](https://nx.dev). | ||
|
||
## Building | ||
|
||
Run `nx build nestjs-json-rpc-sdk` to build the library. | ||
|
||
## Running unit tests | ||
|
||
Run `nx test nestjs-json-rpc-sdk` to execute the unit tests via [Jest](https://jestjs.io). |
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,11 @@ | ||
/* eslint-disable */ | ||
export default { | ||
displayName: 'nestjs-json-rpc-sdk', | ||
preset: '../../../jest.preset.js', | ||
testEnvironment: 'node', | ||
transform: { | ||
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }], | ||
}, | ||
moduleFileExtensions: ['ts', 'js', 'html'], | ||
coverageDirectory: '../../../coverage/libs/json-rpc/nestjs-json-rpc-sdk', | ||
}; |
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,7 @@ | ||
{ | ||
"name": "@klerick/nestjs-json-rpc-sdk", | ||
"version": "0.0.1", | ||
"dependencies": { | ||
"tslib": "^2.3.0" | ||
} | ||
} |
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,23 @@ | ||
{ | ||
"name": "nestjs-json-rpc-sdk", | ||
"$schema": "../../../node_modules/nx/schemas/project-schema.json", | ||
"sourceRoot": "libs/json-rpc/nestjs-json-rpc-sdk/src", | ||
"projectType": "library", | ||
"targets": { | ||
"build": { | ||
"executor": "@nx/js:tsc", | ||
"outputs": ["{options.outputPath}"], | ||
"options": { | ||
"outputPath": "dist/libs/json-rpc/nestjs-json-rpc-sdk", | ||
"main": "libs/json-rpc/nestjs-json-rpc-sdk/src/index.ts", | ||
"tsConfig": "libs/json-rpc/nestjs-json-rpc-sdk/tsconfig.lib.json", | ||
"assets": ["libs/json-rpc/nestjs-json-rpc-sdk/*.md"] | ||
} | ||
}, | ||
"publish": { | ||
"command": "node tools/scripts/publish.mjs nestjs-json-rpc-sdk {args.ver} {args.tag}", | ||
"dependsOn": ["build"] | ||
} | ||
}, | ||
"tags": [] | ||
} |
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 @@ | ||
export * from './lib/nestjs-json-rpc-sdk'; |
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 @@ | ||
export * from './lib/json-rpc-angular'; |
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 @@ | ||
export const JSON_RPC_VERSION = '2.0'; |
34 changes: 34 additions & 0 deletions
34
libs/json-rpc/nestjs-json-rpc-sdk/src/lib/factory/axios-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,34 @@ | ||
import { Axios, AxiosResponse } from 'axios'; | ||
import { Observable } from 'rxjs'; | ||
|
||
import { | ||
HttpAgentFactory, | ||
LoopFunc, | ||
PayloadRpc, | ||
ReturnTransportCall, | ||
RpcResult, | ||
} from '../types'; | ||
import { map } from 'rxjs/operators'; | ||
|
||
export function axiosTransportFactory<T extends LoopFunc>( | ||
axios: Axios | ||
): HttpAgentFactory<T> { | ||
return (url: string) => (body: PayloadRpc<T>) => { | ||
const controller = new AbortController(); | ||
const signal = controller.signal; | ||
|
||
return new Observable<AxiosResponse<RpcResult<T>>>((subscriber) => { | ||
axios | ||
.post< | ||
ReturnTransportCall<T>, | ||
AxiosResponse<RpcResult<T>, PayloadRpc<T>>, | ||
PayloadRpc<T> | ||
>(url, body, { signal }) | ||
.then((response) => subscriber.next(response)) | ||
.catch((error: unknown) => subscriber.error(error)) | ||
.finally(() => subscriber.complete()); | ||
|
||
return { unsubscribe: () => controller.abort() }; | ||
}).pipe(map((r) => r.data)); | ||
}; | ||
} |
9 changes: 9 additions & 0 deletions
9
libs/json-rpc/nestjs-json-rpc-sdk/src/lib/factory/id-request.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,9 @@ | ||
import { idRequest } from './id-request'; | ||
|
||
describe('id-request', () => { | ||
it('should be increment', () => { | ||
expect(idRequest()).toBe(1); | ||
expect(idRequest()).toBe(2); | ||
expect(idRequest()).toBe(3); | ||
}); | ||
}); |
2 changes: 2 additions & 0 deletions
2
libs/json-rpc/nestjs-json-rpc-sdk/src/lib/factory/id-request.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,2 @@ | ||
let i = 0; | ||
export const idRequest = () => ++i; |
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,4 @@ | ||
export * from './axios-transport.factory'; | ||
export * from './id-request'; | ||
export * from './rpc.factory'; | ||
export * from './transport.factory'; |
39 changes: 39 additions & 0 deletions
39
libs/json-rpc/nestjs-json-rpc-sdk/src/lib/factory/rpc.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,39 @@ | ||
import { RpcConfig, RpcReturnList, RpcBatch, RpcBatchPromise } from '../types'; | ||
import { transportFactory } from './transport.factory'; | ||
import { RpcBatchFactory, rpcProxy, RpcBatchFactoryPromise } from '../utils'; | ||
|
||
type ResultRpcFactory<T extends object> = { | ||
rpc: RpcReturnList<T, false>; | ||
rpcBatch: RpcBatch; | ||
}; | ||
type ResultRpcFactoryPromise<T extends object> = { | ||
rpc: RpcReturnList<T, true>; | ||
rpcForBatch: RpcReturnList<T, false>; | ||
rpcBatch: RpcBatchPromise; | ||
}; | ||
|
||
export function RpcFactory<T extends object>( | ||
options: RpcConfig, | ||
usePromise: false | ||
): ResultRpcFactory<T>; | ||
export function RpcFactory<T extends object>( | ||
options: RpcConfig, | ||
usePromise: true | ||
): ResultRpcFactoryPromise<T>; | ||
export function RpcFactory<T extends object>( | ||
options: RpcConfig, | ||
usePromise: true | false = false | ||
): ResultRpcFactory<T> | ResultRpcFactoryPromise<T> { | ||
const transport = transportFactory(options); | ||
let rpc: RpcReturnList<T, true> | RpcReturnList<T, false>; | ||
let rpcForBatch: RpcReturnList<T, false>; | ||
|
||
if (usePromise) { | ||
rpc = rpcProxy<RpcReturnList<T, true>>(transport, usePromise); | ||
rpcForBatch = rpcProxy<RpcReturnList<T, false>>(transport, usePromise); | ||
return { rpc, rpcForBatch, rpcBatch: RpcBatchFactoryPromise(transport) }; | ||
} else { | ||
rpc = rpcProxy<RpcReturnList<T, false>>(transport, usePromise); | ||
return { rpc, rpcBatch: RpcBatchFactory(transport) }; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
libs/json-rpc/nestjs-json-rpc-sdk/src/lib/factory/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,39 @@ | ||
import { fromFetch } from 'rxjs/fetch'; | ||
import { | ||
RpcConfig, | ||
Transport, | ||
TransportType, | ||
RpcHttpConfig, | ||
LoopFunc, | ||
PayloadRpc, | ||
RpcResult, | ||
} from '../types'; | ||
|
||
function httpTransport<T extends LoopFunc>( | ||
config: RpcHttpConfig | ||
): Transport<T> { | ||
const url = new URL(config.rpcPath, config.rpcHost).toString(); | ||
if (config.httpAgentFactory) { | ||
return config.httpAgentFactory(url); | ||
} | ||
|
||
return (body: PayloadRpc<T>) => | ||
fromFetch<RpcResult<T>>(url, { | ||
method: 'post', | ||
body: JSON.stringify(body), | ||
selector: (r) => r.json(), | ||
}); | ||
} | ||
|
||
export function transportFactory<T extends LoopFunc>( | ||
rpcConfig: RpcConfig | ||
): Transport<T> { | ||
switch (rpcConfig.transport) { | ||
case TransportType.HTTP: | ||
return httpTransport(rpcConfig); | ||
case TransportType.WS: | ||
throw new Error('Unknown transport'); | ||
default: | ||
throw new Error('Unknown transport'); | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
libs/json-rpc/nestjs-json-rpc-sdk/src/lib/json-rpc-angular.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,80 @@ | ||
import { | ||
Component, | ||
inject, | ||
InjectionToken, | ||
ModuleWithProviders, | ||
NgModule, | ||
} from '@angular/core'; | ||
import { HttpClientModule, HttpClient } from '@angular/common/http'; | ||
import { | ||
LoopFunc, | ||
RpcMainHttpConfig, | ||
RpcHttpConfig, | ||
RpcWsConfig, | ||
Transport, | ||
TransportType, | ||
PayloadRpc, | ||
RpcResult, | ||
RpcReturnList, | ||
RpcBatch, | ||
} from './types'; | ||
import { transportFactory } from './factory'; | ||
import { RpcBatchFactory, rpcProxy } from './utils'; | ||
|
||
type Rpc<T extends object> = RpcReturnList<T, false>; | ||
|
||
export { TransportType, Rpc }; | ||
|
||
export const JSON_RPC_SDK_CONFIG = new InjectionToken<JsonRpcAngularConfig>( | ||
'Main config object for sdk' | ||
); | ||
|
||
export const JSON_RPC_SDK_TRANSPORT = new InjectionToken<Transport<LoopFunc>>( | ||
'Transport for RPC', | ||
{ | ||
factory: () => { | ||
const config = inject(JSON_RPC_SDK_CONFIG); | ||
const httpClient = inject(HttpClient); | ||
if (config.transport === TransportType.HTTP) { | ||
(config as unknown as RpcHttpConfig)['httpAgentFactory'] = | ||
(url: string) => (body: PayloadRpc<LoopFunc>) => { | ||
return httpClient.post<RpcResult<LoopFunc>>(url, body); | ||
}; | ||
} | ||
return transportFactory(config); | ||
}, | ||
} | ||
); | ||
|
||
export const JSON_RPC = new InjectionToken<RpcReturnList<object, false>>( | ||
'Rpc client', | ||
{ | ||
factory: () => | ||
rpcProxy<RpcReturnList<any, true>>(inject(JSON_RPC_SDK_TRANSPORT), false), | ||
} | ||
); | ||
|
||
export const RPC_BATCH = new InjectionToken<RpcBatch>('Rpc client for batch', { | ||
factory: () => RpcBatchFactory(inject(JSON_RPC_SDK_TRANSPORT)), | ||
}); | ||
|
||
export type JsonRpcAngularConfig = RpcMainHttpConfig | RpcWsConfig; | ||
|
||
@NgModule({ | ||
imports: [HttpClientModule], | ||
}) | ||
export class JsonRpcAngular { | ||
static forRoot( | ||
config: JsonRpcAngularConfig | ||
): ModuleWithProviders<JsonRpcAngular> { | ||
return { | ||
ngModule: JsonRpcAngular, | ||
providers: [ | ||
{ | ||
useValue: config, | ||
provide: JSON_RPC_SDK_CONFIG, | ||
}, | ||
], | ||
}; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
libs/json-rpc/nestjs-json-rpc-sdk/src/lib/nestjs-json-rpc-sdk.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,7 @@ | ||
import { nestjsJsonRpcSdk } from './nestjs-json-rpc-sdk'; | ||
|
||
describe('nestjsJsonRpcSdk', () => { | ||
it('should work', () => { | ||
expect(nestjsJsonRpcSdk()).toEqual('nestjs-json-rpc-sdk'); | ||
}); | ||
}); |
5 changes: 5 additions & 0 deletions
5
libs/json-rpc/nestjs-json-rpc-sdk/src/lib/nestjs-json-rpc-sdk.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,5 @@ | ||
import { RpcConfig } from './types'; | ||
|
||
export function nestjsJsonRpcSdk(rpcConfig: RpcConfig): string { | ||
return 'nestjs-json-rpc-sdk'; | ||
} |
Oops, something went wrong.