UNPKG

@dgpub/prime-sdk

Version:

Etherspot Prime (Account Abstraction) SDK

114 lines (113 loc) 3.95 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ApiService = void 0; const core_1 = require("@apollo/client/core"); const context_1 = require("@apollo/client/link/context"); const ws_1 = require("@apollo/client/link/ws"); const utilities_1 = require("@apollo/client/utilities"); const cross_fetch_1 = __importDefault(require("cross-fetch")); const ethers_1 = require("ethers"); const common_1 = require("../common"); const utils_1 = require("./utils"); class ApiService { constructor(options) { this.options = Object.assign({ port: null, useSsl: false }, options); this.onInit(); } async query(query, options) { options = Object.assign({ variables: {}, fetchPolicy: 'no-cache' }, options); const { variables, fetchPolicy, models, } = options; return this.wrapCall(() => this.apolloClient.query({ query, fetchPolicy, variables: this.prepareApiVariables(variables), }), models); } async mutate(mutation, options) { options = Object.assign({ variables: {} }, options); const { variables, models, } = options; return this.wrapCall(() => this.apolloClient.mutate({ mutation, variables: this.prepareApiVariables(variables), }), models); } subscribe(query, options) { const { variables, models, } = options; return this.apolloClient .subscribe({ query, variables: this.prepareApiVariables(variables), }) .map(({ data }) => (0, utils_1.mapApiResult)(data, models)); } onInit() { const httpLink = new core_1.HttpLink({ fetch: cross_fetch_1.default, uri: (0, utils_1.buildApiUri)(this.options, 'http'), }); const wsLink = new ws_1.WebSocketLink({ webSocketImpl: WebSocket, uri: (0, utils_1.buildApiUri)(this.options, 'ws', 'graphql'), options: { reconnect: true, lazy: true, }, }); const authLink = (0, context_1.setContext)(async () => { return { headers: {}, }; }); const link = (0, core_1.split)(({ query }) => { const definition = (0, utilities_1.getMainDefinition)(query); return definition.kind === 'OperationDefinition' && definition.operation === 'subscription'; }, wsLink, authLink.concat(httpLink)); this.apolloClient = new core_1.ApolloClient({ link, cache: new core_1.InMemoryCache(), }); } async wrapCall(call, models) { const wrapped = async () => { let result; try { const { data } = await call(); result = (0, utils_1.mapApiResult)(data, models); } catch (err) { (0, utils_1.catchApiError)(err); } return result; }; let result; try { result = await wrapped(); } catch (err) { throw err; } return result; } prepareApiVariables(variables) { const result = {}; const keys = Object.keys(variables || {}); for (const key of keys) { let value; if ((0, common_1.isBigNumber)(variables[key])) { value = ethers_1.BigNumber.from(variables[key]).toHexString(); } else if (variables[key] instanceof Date) { value = variables[key].getTime(); } else { value = variables[key]; } result[key] = value; } return result; } } exports.ApiService = ApiService;