Skip to content

Commit

Permalink
fix lint issues and support generics for params
Browse files Browse the repository at this point in the history
  • Loading branch information
v1rtl committed Sep 24, 2021
1 parent b714e6d commit 4cc235a
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 7 deletions.
4 changes: 4 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# These are supported funding model platforms

ko_fi: v1rtl
liberapay: v1rtl
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"deno.enable": true
"deno.enable": true,
"deno.lint": true
}
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@

JSONRPC server implementation with native WebSocket, based on [jsonrpc](https://github.com/Vehmloewff/jsonrpc).

## Features

- No dependencies
- Typed parameters

## Example

```ts
import { App } from 'https://x.nest.land/rpc/mod.ts'

const app = new App()

app.method('hello', (params) => {
app.method<[string]>('hello', (params) => {
return `Hello ${params[0]}`
})

Expand Down
5 changes: 3 additions & 2 deletions request.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { JsonRpcRequest } from './types.ts'
import { makeArray } from './utils.ts'

// deno-lint-ignore no-explicit-any
export function send(socket: WebSocket, message: any) {
const messages = makeArray(message)
messages.forEach((message) => {
Expand All @@ -15,7 +16,7 @@ export function parseRequest(json: string): (JsonRpcRequest | 'invalid')[] | 'pa
const arr = makeArray(JSON.parse(json))
const res: (JsonRpcRequest | 'invalid')[] = []

for (let obj of arr) {
for (const obj of arr) {
if (typeof obj !== 'object') res.push('invalid')
else if (!obj) res.push('invalid')
else if (obj.jsonrpc !== '2.0') res.push('invalid')
Expand All @@ -26,7 +27,7 @@ export function parseRequest(json: string): (JsonRpcRequest | 'invalid')[] | 'pa
if (!res.length) return ['invalid']

return res
} catch (e) {
} catch {
return 'parse-error'
}
}
4 changes: 2 additions & 2 deletions server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class App {
* @param method method name
* @param handler method handler
*/
method(method: string, handler: (params: Parameters, clientId: string) => Promise<any>) {
method<T = any>(method: string, handler: (params: Parameters<T>, clientId: string) => any | Promise<any>) {
this.methods.set(method, handler)
}

Expand All @@ -88,7 +88,7 @@ export class App {
const requests = parseRequest(data)
if (requests === 'parse-error') return send(sock, { id: null, error: { code: -32700, message: 'Parse error' } })

const responses: any[] = []
const responses: unknown[] = []

const promises = requests.map(async (request) => {
if (request === 'invalid')
Expand Down
2 changes: 1 addition & 1 deletion types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type Parameters = any[]
export type Parameters<T = any> = T[]

export interface JsonRpcRequest {
method: string
Expand Down

0 comments on commit 4cc235a

Please sign in to comment.