The easiest-to-use RPC library designed for TypeScript and JavaScript.
npm install delight-rpcsh
npm install --save delight-rpc
or
yarn add delight-rpc
`Usage
$3
`ts
import { createClient } from 'delight-rpc'interface IAPI {
foo(bar: string): string
}
const client = createClient(send)
const result = await client.foo('bar')
`$3
`ts
import { createBatchProxy, BatchClient } from 'delight-rpc'interface IAPI {
getNumber(): number
getString(): string
}
const client = new BatchClient(send)
const proxy = createBatchProxy()
const results = await client.parallel(
proxy.getNumber()
, proxy.getString()
)
//
Result comes from the return-style module, it has Rust-like APIs
results[0] // Result
results[1] // Result
`$3
`ts
import { createResponse, IRequest, IBatchRequest } from 'delight-rpc'const api: IAPI = {
foo(bar: string) {
return bar
}
}
async function handle(
request: IRequest | IBatchRequest
): Promise | IBatchResponse> {
return await createResponse(api, request)
}
`API
`ts
type ImplementationOf = {
[Key in FunctionKeys | KeysByType]:
Obj[Key] extends (...args: infer Args) => infer Result
? (...args: [...args: Args, signal?: AbortSignal]) => Awaitable>
: ImplementationOf
}type ParameterValidators = Partial<{
[Key in FunctionKeys | KeysByType]:
Obj[Key] extends (...args: infer Args) => unknown
? (...args: Args) => void
: ParameterValidators
}>
`$3
`ts
type ClientProxy = {
[Key in FunctionKeys | KeysByType]:
Obj[Key] extends (...args: infer Args) => infer Result
? (...args: [...args: Args, signal?: AbortSignal]) => Promise>
: ClientProxy
}function createClient(
send: (
request: IRequest
, signal?: AbortSignal
) => Awaitable>
, options?: {
parameterValidators?: ParameterValidators
expectedVersion?: string
channel?: string
}
): ClientProxy
`For easy distinction, when the method is not available,
MethodNotAvailable will be thrown instead of the general Error.$3
`ts
type MapRequestsToResults[]> = {
[Index in keyof RequestTuple]:
RequestTuple[Index] extends IRequestForBatchRequest
? Result
: never
}class BatchClient {
constructor(
send: (batchRequest: IBatchRequest) => Awaitable<
| IError
| IBatchResponse
>
, options?: {
expectedVersion?: string
channel?: string
}
)
async parallel[]>(
...requests: T
): Promise>
async series[]>(
...requests: T
): Promise>
}
`$3
`ts
type BatchClientProxy = {
[Key in FunctionKeys | KeysByType]:
Obj[Key] extends (...args: infer Args) => infer Result
? (...args: Args) => IRequestForBatchRequest, DataType>
: BatchClientProxy
}function createBatchProxy(
options?: {
parameterValidators?: ParameterValidators
}
): BatchClientProxy
`$3
`ts
const AnyChannelfunction createResponse(
api: ImplementationOf
, request: IRequest | IBatchRequest
, { parameterValidators = {}, version, channel, signal, ownPropsOnly = false }: {
parameterValidators?: ParameterValidators
version?:
${number}.${number}.${number}
channel?: string | RegExp | typeof AnyChannel
ownPropsOnly?: boolean
signal?: AbortSignal
} = {}
): Promise | IBatchResponse>
`createResponse returns null if the channel does not match.$3
`ts
class MethodNotAvailable extends CustomError {}
`$3
`ts
class VersionMismatch extends CustomError {}
`$3
`ts
class InternalError extends CustomError {}
`$3
`ts
function isRequest(val: unknown): val is IRequest
`$3
`ts
function isResult(val: unknown): val is IResult
`$3
`ts
function isError(val: unknown): val is IError
`$3
`ts
function isBatchRequest(val: unknown): val is IBatchRequest
`$3
`ts
function isBatchResponse(val: unknown): val is IBatchResponse
`$3
`ts
function matchChannel(
message: IDelightRPC
, channel:
| undefined
| string
| RegExp
| typeof AnyChannel
): boolean
`$3
`ts
function createAbort(id: string, channel: string | undefined): IAbort
`$3
`ts
function isAbort(val: unknown): val is IAbort
``