@tangany/waas
Version:
node.js SDK for Tangany Wallet as a Service API
199 lines • 9.59 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Request = void 0;
var t = require("typeforce");
var request_iterable_1 = require("./iterables/auto-pagination/request-iterable");
var request_page_iterable_1 = require("./iterables/pagewise/request-page-iterable");
var polling_helper_1 = require("./utils/polling-helper");
/**
* Instantiates a new interface to interact with asynchronous requests
* CAUTION: This class is the default implementation and just passes through the result received from the API when {@link get} is called.
* Thus, if a type is supplied for `TOutput` that does not match the format of the API response in `output`, the return type suggests that a conversion occurred.
* However, this is not the case. Therefore, use a child class with appropriate overrides (like {@link EthTransactionRequest}) if a conversion of the output is necessary.
* @param waas - {@link Waas} instance
* @param [id] - Asynchronous request id
* @template TOutput - Type of the request output (for child classes the type **after** conversion)
*/
var Request = /** @class */ (function () {
function Request(waas, id) {
var _this = this;
this.waas = waas;
this.id = id;
// tslint:disable-next-line:variable-name
this.__test_convertSearchParamsToURL = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
return Request.convertSearchParamsToURL.apply(_this, args);
};
// Make sure that the variable is set and has the correct type
t("?String", id);
}
/**
* Retrieves the current status for a long-running asynchronous request
* @see [docs]{@link https://docs.tangany.com/#a6351116-3e2c-4f02-add8-d424c6212f60}
*/
Request.prototype.get = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
// Since this class is the base implementation, just forward the fetched API result and apply the passed generic to it
t("String", this.id);
return [2 /*return*/, this.fetchStatus()];
});
});
};
Request.prototype.fetchStatus = function () {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
return [2 /*return*/, this.waas.wrap(function () { return _this.waas.instance.get("request/".concat(_this.id)); })];
});
});
};
/**
* Waits until the asynchronous request is completed and then resolves or rejects on error or timeout.
* This method does _not_ evaluate the output of the process (for example, the transaction status)
* because asynchronous requests are used for arbitrary calls and their output therefore varies.
* Attention: The method polls the API frequently and may result in excessive quota usage.
* @param [timeout] - Number of seconds after which the method stops waiting and rejects
* @param [pollingInterval] - Delay between API calls for polling (in milliseconds)
*/
Request.prototype.wait = function (timeout, pollingInterval) {
if (timeout === void 0) { timeout = 20e3; }
if (pollingInterval === void 0) { pollingInterval = 4e2; }
return __awaiter(this, void 0, void 0, function () {
var isCompleted;
return __generator(this, function (_a) {
isCompleted = function (res) { return res.process === "Completed"; };
return [2 /*return*/, (0, polling_helper_1.poll)(this.get.bind(this), isCompleted, "request ".concat(this.id), timeout, pollingInterval)];
});
});
};
/**
* Converts an {@link IRequestSearchParams} object to a valid url with query parameter
* @param [params] - Query parameters for filtering the request statuses
* @private
*/
Request.convertSearchParamsToURL = function (params) {
var e_1, _a;
var url = "requests";
if (!params) {
return url;
}
var i = 1;
var entries = Object.entries(params);
if (entries.length > 0) {
url += "?";
}
var _loop_1 = function (key, value) {
if (Array.isArray(value) && value.length > 0) {
value.map(function (item, index) {
url += "".concat(key, "=").concat(item);
if ((index + 1) < value.length) {
url += "&";
}
});
}
else if (value instanceof Date) {
url += "".concat(key, "=").concat(value.toISOString());
}
else {
url += "".concat(key, "=").concat(value);
}
if (i < entries.length) {
url += "&";
i++;
}
};
try {
for (var entries_1 = __values(entries), entries_1_1 = entries_1.next(); !entries_1_1.done; entries_1_1 = entries_1.next()) {
var _b = __read(entries_1_1.value, 2), key = _b[0], value = _b[1];
_loop_1(key, value);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (entries_1_1 && !entries_1_1.done && (_a = entries_1.return)) _a.call(entries_1);
}
finally { if (e_1) throw e_1.error; }
}
return url;
};
Request.prototype.list = function (params, options) {
var initialRequest = { url: Request.convertSearchParamsToURL(params) };
if (options === null || options === void 0 ? void 0 : options.autoPagination) {
return new request_iterable_1.RequestIterable(this.waas, initialRequest);
}
else {
return new request_page_iterable_1.RequestPageIterable(this.waas, initialRequest);
}
};
return Request;
}());
exports.Request = Request;
//# sourceMappingURL=request.js.map