Skip to content

Instantly share code, notes, and snippets.

@rajatxs
Created April 30, 2023 17:30
Show Gist options
  • Save rajatxs/15e65b99e460cf517a5af096e8bab6aa to your computer and use it in GitHub Desktop.
Save rajatxs/15e65b99e460cf517a5af096e8bab6aa to your computer and use it in GitHub Desktop.
Simple JSON-RPC utility for web
import { HttpService } from 'https://gist.github.com/rajatxs/c17ef2f25b3ab7e935654d69c7ee5d8c#file-http-service-ts'
export type RpcVersion = '1.0' | '2.0'
export interface RpcRequestPayload {
jsonrpc: RpcVersion
method: string
params: Array<any>
id: number
}
export interface RpcResponseErrorObject {
statusCode?: number
message?: string
data?: any
}
export interface RpcResponse<ResultType> {
jsonrpc: RpcVersion
result: ResultType
error?: string | RpcResponseErrorObject
id: number
}
export class RpcService extends HttpService {
private requestCounter: number = 0
public constructor(public baseUrl: string) {
super(baseUrl, 'json', {
'Content-Type': 'application/json',
})
}
public async call<ResultType>(method: string, params: Array<any> | any, id: number = NaN) {
let payload: RpcRequestPayload
let response: RpcResponse<ResultType>
if (!Array.isArray(params)) {
params = [params]
}
if (Number.isNaN(id)) {
id = ++this.requestCounter
}
payload = {
jsonrpc: '2.0',
method,
params,
id,
}
response = await this.post<RpcResponse<ResultType>>('', payload)
if (typeof response !== 'object') {
throw new Error('something went wrong')
}
if (response.id !== id) {
throw new Error('invalid request')
}
if (response.error) {
if (typeof response.error === 'string') {
throw new Error(response.error)
} else if (typeof response.error === 'object') {
throw new Error(response.error.message)
} else {
throw new Error('rpc response error')
}
}
return response.result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment