UNPKG

@foxpage/foxpage-manager

Version:

foxpage resource manager

42 lines (41 loc) 1.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PackageFetcher = void 0; const foxpage_shared_1 = require("@foxpage/foxpage-shared"); const fetch_service_1 = require("./fetch-service"); /** * package fetcher * * @export * @class PackageFetcher */ class PackageFetcher { constructor(source, option = {}) { this.resource = source; this.option = Object.assign({ maxRetryTime: 3 }, option); this.retryCount = 1; this.messages = new foxpage_shared_1.Messages(); } /** * fetch package code * * @return {*} {Promise<Option<FetchResult>>} */ async fetch() { if (this.retryCount > this.option.maxRetryTime) { return foxpage_shared_1.optional.fail(this.messages.stringify()); } if (this.result) { return foxpage_shared_1.optional.ok(this.result); } const res = await (0, fetch_service_1.fetchPackage)(this.resource, Object.assign(Object.assign({}, (this.option.requestOpt || {})), { timeout: this.option.downloadTimeout })); if (res.ok) { this.result = res.data; return res; } this.messages.push(res.error); this.retryCount += 1; return this.fetch(); } } exports.PackageFetcher = PackageFetcher;