https://github.com/delight-rpc/delight-rpc
🌲 The easiest-to-use RPC library designed for TypeScript and JavaScript.
https://github.com/delight-rpc/delight-rpc
browser esm library nodejs npm-package typescript
Last synced: 9 months ago
JSON representation
🌲 The easiest-to-use RPC library designed for TypeScript and JavaScript.
- Host: GitHub
- URL: https://github.com/delight-rpc/delight-rpc
- Owner: delight-rpc
- License: mit
- Created: 2020-12-20T06:46:03.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2023-04-04T02:04:30.000Z (about 3 years ago)
- Last Synced: 2024-12-14T01:11:57.886Z (over 1 year ago)
- Topics: browser, esm, library, nodejs, npm-package, typescript
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/delight-rpc
- Size: 2.71 MB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# delight-rpc
The easiest-to-use RPC library designed for TypeScript and JavaScript.
## Install
```sh
npm install --save delight-rpc
# or
yarn add delight-rpc
```
## Usage
### Client
```ts
import { createClient } from 'delight-rpc'
interface IAPI {
foo(bar: string): string
}
const client = createClient(send)
const result = await client.foo('bar')
```
### BatchClient
```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
```
### Server
```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
}>
```
### createClient
```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`.
### BatchClient
```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>
}
```
### createBatchProxy
```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
```
### createResponse
```ts
const AnyChannel
function 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.
### MethodNotAvailable
```ts
class MethodNotAvailable extends CustomError {}
```
### VersionMismatch
```ts
class VersionMismatch extends CustomError {}
```
### InternalError
```ts
class InternalError extends CustomError {}
```
### isRequst
```ts
function isRequest(val: unknown): val is IRequest
```
### isResult
```ts
function isResult(val: unknown): val is IResult
```
### isError
```ts
function isError(val: unknown): val is IError
```
### isBatchRequest
```ts
function isBatchRequest(val: unknown): val is IBatchRequest
```
### isBatchResponse
```ts
function isBatchResponse(val: unknown): val is IBatchResponse
```
### matchChannel
```ts
function matchChannel(
message: IDelightRPC
, channel:
| undefined
| string
| RegExp
| typeof AnyChannel
): boolean
```
### createAbort
```ts
function createAbort(id: string, channel: string | undefined): IAbort
```
### isAbort
```ts
function isAbort(val: unknown): val is IAbort
```