@mysten/sui
Version:
Sui TypeScript API
1 lines • 3.32 kB
Source Map (JSON)
{"version":3,"file":"http-transport.mjs","names":["#options","#requestId"],"sources":["../../src/jsonRpc/http-transport.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { PACKAGE_VERSION, TARGETED_RPC_VERSION } from '../version.js';\nimport { JsonRpcError, SuiHTTPStatusError } from './errors.js';\n\n/**\n * An object defining headers to be passed to the RPC server\n */\nexport type HttpHeaders = { [header: string]: string };\n\nexport interface JsonRpcHTTPTransportOptions {\n\tfetch?: typeof fetch;\n\turl: string;\n\trpc?: {\n\t\theaders?: HttpHeaders;\n\t\turl?: string;\n\t};\n}\n\nexport interface JsonRpcTransportRequestOptions {\n\tmethod: string;\n\tparams: unknown[];\n\tsignal?: AbortSignal;\n}\n\nexport interface JsonRpcTransport {\n\trequest<T = unknown>(input: JsonRpcTransportRequestOptions): Promise<T>;\n}\n\nexport class JsonRpcHTTPTransport implements JsonRpcTransport {\n\t#requestId = 0;\n\t#options: JsonRpcHTTPTransportOptions;\n\n\tconstructor(options: JsonRpcHTTPTransportOptions) {\n\t\tthis.#options = options;\n\t}\n\n\tfetch(input: RequestInfo, init?: RequestInit): Promise<Response> {\n\t\tconst fetchFn = this.#options.fetch ?? fetch;\n\n\t\tif (!fetchFn) {\n\t\t\tthrow new Error(\n\t\t\t\t'The current environment does not support fetch, you can provide a fetch implementation in the options for SuiHTTPTransport.',\n\t\t\t);\n\t\t}\n\n\t\treturn fetchFn(input, init);\n\t}\n\n\tasync request<T>(input: JsonRpcTransportRequestOptions): Promise<T> {\n\t\tthis.#requestId += 1;\n\n\t\tconst res = await this.fetch(this.#options.rpc?.url ?? this.#options.url, {\n\t\t\tmethod: 'POST',\n\t\t\tsignal: input.signal,\n\t\t\theaders: {\n\t\t\t\t'Content-Type': 'application/json',\n\t\t\t\t'Client-Sdk-Type': 'typescript',\n\t\t\t\t'Client-Sdk-Version': PACKAGE_VERSION,\n\t\t\t\t'Client-Target-Api-Version': TARGETED_RPC_VERSION,\n\t\t\t\t'Client-Request-Method': input.method,\n\t\t\t\t...this.#options.rpc?.headers,\n\t\t\t},\n\t\t\tbody: JSON.stringify({\n\t\t\t\tjsonrpc: '2.0',\n\t\t\t\tid: this.#requestId,\n\t\t\t\tmethod: input.method,\n\t\t\t\tparams: input.params,\n\t\t\t}),\n\t\t});\n\n\t\tif (!res.ok) {\n\t\t\tthrow new SuiHTTPStatusError(\n\t\t\t\t`Unexpected status code: ${res.status}`,\n\t\t\t\tres.status,\n\t\t\t\tres.statusText,\n\t\t\t);\n\t\t}\n\n\t\tconst data = await res.json();\n\n\t\tif ('error' in data && data.error != null) {\n\t\t\tthrow new JsonRpcError(data.error.message, data.error.code);\n\t\t}\n\n\t\treturn data.result;\n\t}\n}\n"],"mappings":";;;;AA8BA,IAAa,uBAAb,MAA8D;CAC7D,aAAa;CACb;CAEA,YAAY,SAAsC;AACjD,QAAKA,UAAW;;CAGjB,MAAM,OAAoB,MAAuC;EAChE,MAAM,UAAU,MAAKA,QAAS,SAAS;AAEvC,MAAI,CAAC,QACJ,OAAM,IAAI,MACT,8HACA;AAGF,SAAO,QAAQ,OAAO,KAAK;;CAG5B,MAAM,QAAW,OAAmD;AACnE,QAAKC,aAAc;EAEnB,MAAM,MAAM,MAAM,KAAK,MAAM,MAAKD,QAAS,KAAK,OAAO,MAAKA,QAAS,KAAK;GACzE,QAAQ;GACR,QAAQ,MAAM;GACd,SAAS;IACR,gBAAgB;IAChB,mBAAmB;IACnB,sBAAsB;IACtB,6BAA6B;IAC7B,yBAAyB,MAAM;IAC/B,GAAG,MAAKA,QAAS,KAAK;IACtB;GACD,MAAM,KAAK,UAAU;IACpB,SAAS;IACT,IAAI,MAAKC;IACT,QAAQ,MAAM;IACd,QAAQ,MAAM;IACd,CAAC;GACF,CAAC;AAEF,MAAI,CAAC,IAAI,GACR,OAAM,IAAI,mBACT,2BAA2B,IAAI,UAC/B,IAAI,QACJ,IAAI,WACJ;EAGF,MAAM,OAAO,MAAM,IAAI,MAAM;AAE7B,MAAI,WAAW,QAAQ,KAAK,SAAS,KACpC,OAAM,IAAI,aAAa,KAAK,MAAM,SAAS,KAAK,MAAM,KAAK;AAG5D,SAAO,KAAK"}