From 4cc235a9f85f5dcd74ba0c31297f303ca8f6edba Mon Sep 17 00:00:00 2001 From: v1rtl Date: Fri, 24 Sep 2021 18:47:31 +0300 Subject: [PATCH] fix lint issues and support generics for params --- .github/FUNDING.yml | 4 ++++ .vscode/settings.json | 3 ++- README.md | 7 ++++++- request.ts | 5 +++-- server.ts | 4 ++-- types.ts | 2 +- 6 files changed, 18 insertions(+), 7 deletions(-) create mode 100644 .github/FUNDING.yml diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..cf263cc --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1,4 @@ +# These are supported funding model platforms + +ko_fi: v1rtl +liberapay: v1rtl diff --git a/.vscode/settings.json b/.vscode/settings.json index cbac569..e1533c2 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,4 @@ { - "deno.enable": true + "deno.enable": true, + "deno.lint": true } diff --git a/README.md b/README.md index 2776f23..e64201a 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,11 @@ JSONRPC server implementation with native WebSocket, based on [jsonrpc](https://github.com/Vehmloewff/jsonrpc). +## Features + +- No dependencies +- Typed parameters + ## Example ```ts @@ -15,7 +20,7 @@ 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]}` }) diff --git a/request.ts b/request.ts index 026063d..6efb7a3 100644 --- a/request.ts +++ b/request.ts @@ -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) => { @@ -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') @@ -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' } } diff --git a/server.ts b/server.ts index 80f1d27..97cb13a 100644 --- a/server.ts +++ b/server.ts @@ -71,7 +71,7 @@ export class App { * @param method method name * @param handler method handler */ - method(method: string, handler: (params: Parameters, clientId: string) => Promise) { + method(method: string, handler: (params: Parameters, clientId: string) => any | Promise) { this.methods.set(method, handler) } @@ -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') diff --git a/types.ts b/types.ts index abd1cbb..6fee75d 100644 --- a/types.ts +++ b/types.ts @@ -1,4 +1,4 @@ -export type Parameters = any[] +export type Parameters = T[] export interface JsonRpcRequest { method: string