UNPKG

@aeternity/aepp-sdk

Version:
288 lines (252 loc) 14.6 kB
import _Object$getOwnPropertySymbols from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols"; import _Object$getOwnPropertyDescriptor from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor"; import _Object$getOwnPropertyDescriptors from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptors"; import _Object$defineProperties from "@babel/runtime-corejs3/core-js-stable/object/define-properties"; import _Object$defineProperty from "@babel/runtime-corejs3/core-js-stable/object/define-property"; import _typeof from "@babel/runtime-corejs3/helpers/typeof"; import _objectWithoutProperties from "@babel/runtime-corejs3/helpers/objectWithoutProperties"; import _toConsumableArray from "@babel/runtime-corejs3/helpers/toConsumableArray"; import _defineProperty from "@babel/runtime-corejs3/helpers/defineProperty"; import _slicedToArray from "@babel/runtime-corejs3/helpers/slicedToArray"; import _asyncToGenerator from "@babel/runtime-corejs3/helpers/asyncToGenerator"; import _regeneratorRuntime from "@babel/runtime-corejs3/regenerator"; function ownKeys(object, enumerableOnly) { var keys = _Object$keys(object); if (_Object$getOwnPropertySymbols) { var symbols = _Object$getOwnPropertySymbols(object); if (enumerableOnly) symbols = _filterInstanceProperty(symbols).call(symbols, function (sym) { return _Object$getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { var _context13; _forEachInstanceProperty(_context13 = ownKeys(Object(source), true)).call(_context13, function (key) { _defineProperty(target, key, source[key]); }); } else if (_Object$getOwnPropertyDescriptors) { _Object$defineProperties(target, _Object$getOwnPropertyDescriptors(source)); } else { var _context14; _forEachInstanceProperty(_context14 = ownKeys(Object(source))).call(_context14, function (key) { _Object$defineProperty(target, key, _Object$getOwnPropertyDescriptor(source, key)); }); } } return target; } import _forEachInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/for-each"; import _Promise from "@babel/runtime-corejs3/core-js-stable/promise"; import _mapInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/map"; import _sliceInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/slice"; import _URL from "@babel/runtime-corejs3/core-js-stable/url"; import _Object$assign from "@babel/runtime-corejs3/core-js-stable/object/assign"; import _reduceInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/reduce"; import _concatInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/concat"; import _Object$values from "@babel/runtime-corejs3/core-js-stable/object/values"; import _flatInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/flat"; import _filterInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/filter"; import _Object$keys from "@babel/runtime-corejs3/core-js-stable/object/keys"; import _findInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/find"; import _includesInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/includes"; /* * ISC License (ISC) * Copyright (c) 2021 aeternity developers * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR * PERFORMANCE OF THIS SOFTWARE. */ /** * Generator of Swagger client module * @module @aeternity/aepp-sdk/es/utils/swagger * @export genSwaggerClient */ import SwaggerClient from 'swagger-client'; import fetch from 'cross-fetch'; import JsonBig from './json-big'; import { snakizeKeys, pascalizeKeys, mapObject, filterObject } from './other'; var warnedAboutInternalApiUsage = false; /** * Generator of Swagger client * @function * @alias module:@aeternity/aepp-sdk/es/utils/swagger * @rtype Object * @param {String} specUrl - Swagger specification URL on external node host * @param {Object} options * @param {String} [options.spec] - Override OpenAPI definition * @param {String} [options.internalUrl] - Node internal URL * @param {Boolean} [options.disableBigNumbers] * @param {Boolean} [options.disableCaseConversion] * @return {Object} Swagger client * @example (await genSwaggerClient('https://mainnet.aeternity.io/api')).getAccountByPubkey('ak_jupBUgZNbcC4krDLR3tAkw1iBZoBbkNeShAq4atBtpFWmz36r') */ export default /*#__PURE__*/(function () { var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(specUrl) { var _context2, _context4, _context5, _context6, _context7, _context8; var _ref2, spec, internalUrl, disableBigNumbers, disableCaseConversion, jsonImp, isAeternityNode, _context, _yield$Promise$all, _yield$Promise$all2, external, internal, combinedApi, opSpecs, api, _args2 = arguments; return _regeneratorRuntime.wrap(function _callee2$(_context12) { while (1) { switch (_context12.prev = _context12.next) { case 0: _ref2 = _args2.length > 1 && _args2[1] !== undefined ? _args2[1] : {}, spec = _ref2.spec, internalUrl = _ref2.internalUrl, disableBigNumbers = _ref2.disableBigNumbers, disableCaseConversion = _ref2.disableCaseConversion; _context12.t0 = spec; if (_context12.t0) { _context12.next = 8; break; } _context12.next = 5; return fetch(specUrl); case 5: _context12.next = 7; return _context12.sent.json(); case 7: _context12.t0 = _context12.sent; case 8: spec = _context12.t0; jsonImp = disableBigNumbers ? JSON : JsonBig; isAeternityNode = spec.info.title === 'Aeternity node'; if (isAeternityNode && spec.swagger === '2.0') { _forEachInstanceProperty(_context = ['claim', 'preclaim', 'transfer', 'revoke', 'update']).call(_context, function (name) { var _spec$paths$; var s = (_spec$paths$ = spec.paths["/debug/names/".concat(name)]) === null || _spec$paths$ === void 0 ? void 0 : _spec$paths$.post; if (s && !s.consumes) s.consumes = ['application/json']; }); } _context12.next = 14; return _Promise.all(_mapInstanceProperty(_context2 = [specUrl, internalUrl]).call(_context2, function (url) { var _context3; if (!url) return null; spec.schemes = [_sliceInstanceProperty(_context3 = new _URL(url).protocol).call(_context3, 0, -1)]; return SwaggerClient({ url: url, spec: spec, requestInterceptor: function requestInterceptor(request) { if (isAeternityNode && /^\/v3\/transactions\/\w+$/.test(new _URL(request.url).pathname)) { return _objectSpread(_objectSpread({}, request), {}, { url: request.url.replace('v3', 'v2') }); } return request; }, responseInterceptor: function responseInterceptor(response) { if (!response.text) return response; var body = jsonImp.parse(response.text); return _Object$assign(response, { body: disableCaseConversion ? body : pascalizeKeys(body) }); } }); })); case 14: _yield$Promise$all = _context12.sent; _yield$Promise$all2 = _slicedToArray(_yield$Promise$all, 2); external = _yield$Promise$all2[0]; internal = _yield$Promise$all2[1]; combinedApi = _reduceInstanceProperty(_context4 = _concatInstanceProperty(_context5 = []).call(_context5, _toConsumableArray(_Object$values(external.apis)), [mapObject((internal === null || internal === void 0 ? void 0 : internal.apis.internal) || {}, function (_ref3) { var _ref4 = _slicedToArray(_ref3, 2), key = _ref4[0], handler = _ref4[1]; return [key, function () { if (!warnedAboutInternalApiUsage) { console.warn('SDK\'s wrapper of aeternity node internal API is deprecated, please use external ' + 'equivalent (for example, "sdk.api.protectedDryRunTxs" instead of "sdk.api.dryRunTxs") ' + 'or create a wrapper of internal API by yourself (using "genSwaggerClient")'); warnedAboutInternalApiUsage = true; } return handler.apply(void 0, arguments); }]; })])).call(_context4, function (acc, n) { return _objectSpread(_objectSpread({}, acc), n); }); opSpecs = _reduceInstanceProperty(_context6 = _flatInstanceProperty(_context7 = _mapInstanceProperty(_context8 = _Object$values(spec.paths)).call(_context8, function (paths) { return _Object$values(paths); })).call(_context7)).call(_context6, function (acc, n) { return _objectSpread(_objectSpread({}, acc), {}, _defineProperty({}, n.operationId, n)); }, {}); api = mapObject(combinedApi, function (_ref5) { var _ref6 = _slicedToArray(_ref5, 2), opId = _ref6[0], handler = _ref6[1]; return [_sliceInstanceProperty(opId).call(opId, 0, 1).toLowerCase() + _sliceInstanceProperty(opId).call(opId, 1), /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() { var _context9, _context10; var _len, args, _key, opSpec, parameters, required, values, _mapObject, __requestBody, stringified, _args = arguments; return _regeneratorRuntime.wrap(function _callee$(_context11) { while (1) { switch (_context11.prev = _context11.next) { case 0: for (_len = _args.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = _args[_key]; } opSpec = opSpecs[opId]; parameters = _concatInstanceProperty(_context9 = []).call(_context9, _toConsumableArray(opSpec.parameters), _toConsumableArray(opSpec.requestBody ? [{ required: opSpec.requestBody.required, schema: _Object$values(opSpec.requestBody.content)[0].schema, name: '__requestBody' }] : [])); required = _mapInstanceProperty(_context10 = _filterInstanceProperty(parameters).call(parameters, function (param) { return param.required; })).call(_context10, function (p) { return p.name; }); if (!(args.length < required.length)) { _context11.next = 6; break; } throw new Error('swagger: Not enough arguments'); case 6: values = _reduceInstanceProperty(required).call(required, function (acc, req, idx) { return _objectSpread(_objectSpread({}, acc), {}, _defineProperty({}, req, args[idx])); }, args[required.length] || {}); _mapObject = mapObject(values, function (_ref8) { var _ref9 = _slicedToArray(_ref8, 2), param = _ref9[0], value = _ref9[1]; if (_typeof(value) !== 'object') return [param, value]; var rootKeys = _Object$keys(_findInstanceProperty(parameters).call(parameters, function (p) { return p.name === param; }).schema.properties); var filteredValue = filterObject(disableCaseConversion ? value : snakizeKeys(value), function (_ref10) { var _ref11 = _slicedToArray(_ref10, 1), key = _ref11[0]; return _includesInstanceProperty(rootKeys).call(rootKeys, key); }); return [param, jsonImp.stringify(filteredValue)]; }), __requestBody = _mapObject.__requestBody, stringified = _objectWithoutProperties(_mapObject, ["__requestBody"]); _context11.next = 10; return handler(stringified, { requestBody: __requestBody }); case 10: return _context11.abrupt("return", _context11.sent.body); case 11: case "end": return _context11.stop(); } } }, _callee); }))]; }); return _context12.abrupt("return", _Object$assign(external, { api: api })); case 22: case "end": return _context12.stop(); } } }, _callee2); })); return function (_x) { return _ref.apply(this, arguments); }; })(); //# sourceMappingURL=swagger.js.map