wowok
Version:
Wowok Blockchain TypeScript API
1 lines • 2.02 kB
JavaScript
import{PACKAGE_VERSION,TARGETED_RPC_VERSION}from'../version.js';import{JsonRpcError,WowHTTPStatusError}from'./errors.js';import{WebsocketClient}from'./rpc-websocket-client.js';export class JsonRpcHTTPTransport{#requestId=0x0;#options;#websocketClient;constructor(a){this.#options=a;}['fetch'](a,b){const c=this.#options['fetch']??fetch;if(!c)throw new Error('The\x20current\x20environment\x20does\x20not\x20support\x20fetch,\x20you\x20can\x20provide\x20a\x20fetch\x20implementation\x20in\x20the\x20options\x20for\x20WowHTTPTransport.');return c(a,b);}#getWebsocketClient(){if(!this.#websocketClient){const a=this.#options['WebSocketConstructor']??WebSocket;if(!a)throw new Error('The\x20current\x20environment\x20does\x20not\x20support\x20WebSocket,\x20you\x20can\x20provide\x20a\x20WebSocketConstructor\x20in\x20the\x20options\x20for\x20WowHTTPTransport.');this.#websocketClient=new WebsocketClient(this.#options['websocket']?.['url']??this.#options['url'],{'WebSocketConstructor':a,...this.#options['websocket']});}return this.#websocketClient;}async['request'](a){this.#requestId+=0x1;const b=await this['fetch'](this.#options['rpc']?.['url']??this.#options['url'],{'method':'POST','signal':a['signal'],'headers':{'Content-Type':'application/json','Client-Sdk-Type':'typescript','Client-Sdk-Version':PACKAGE_VERSION,'Client-Target-Api-Version':TARGETED_RPC_VERSION,'Client-Request-Method':a['method'],...this.#options['rpc']?.['headers']},'body':JSON['stringify']({'jsonrpc':'2.0','id':this.#requestId,'method':a['method'],'params':a['params']})});if(!b['ok'])throw new WowHTTPStatusError('Unexpected\x20status\x20code:\x20'+b['status'],b['status'],b['statusText']);const c=await b['json']();if('error'in c&&c['error']!=null)throw new JsonRpcError(c['error']['message'],c['error']['code']);return c['result'];}async['subscribe'](a){const b=await this.#getWebsocketClient()['subscribe'](a);return a['signal']&&(a['signal']['throwIfAborted'](),a['signal']['addEventListener']('abort',()=>{b();})),async()=>!!await b();}}