UNPKG

@lx-frontend/multi-track

Version:

sls 日志上传功能,当前支持小程序、web

82 lines 2.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.WebTrack = void 0; var tslib_1 = require("tslib"); var track_1 = require("./track"); /** * 网页日志上报 * * @example * * ```ts * const track = new WebTrack({ * slsOptions: { * host: 'https://sls.aliyuncs.com', * project: 'your-project', * logstore: 'your-logstore', * topic: 'your-topic', * source: 'your-source', * tags: { * tag1: 'value1', * tag2: 'value2', * }, * }, * trackOptions: { * maxQueueSize: 10, * debounceTime: 1000, * localStorageKey: 'your-local-storage-key', * uploadEnabled: true, * }, * sendPayload: (logs) => logs, * }) * ``` */ var WebTrack = /** @class */ (function (_super) { tslib_1.__extends(WebTrack, _super); function WebTrack(trackConfig) { var _this = this; var sendPayload = trackConfig.sendPayload; _this = _super.call(this, trackConfig) || this; _this._format = sendPayload !== null && sendPayload !== void 0 ? sendPayload : (function (logs) { return logs; }); return _this; } WebTrack.prototype.format = function (log) { return this._format(log); }; WebTrack.prototype._upload = function (payload, headers) { var _this = this; // web 和 node 都支持 fetch return fetch(this.generateSlsUrl(), { method: 'POST', headers: headers, body: payload, }).then(function (res) { return tslib_1.__awaiter(_this, void 0, void 0, function () { var _a, _b, _c; return tslib_1.__generator(this, function (_d) { switch (_d.label) { case 0: _b = (_a = console).log; _c = ['res']; return [4 /*yield*/, res.text()]; case 1: _b.apply(_a, _c.concat([_d.sent()])); return [2 /*return*/]; } }); }); }); }; WebTrack.prototype._getStorage = function (key) { try { return JSON.parse(localStorage.getItem(key) || '[]'); } catch (_err) { return []; } }; WebTrack.prototype._setStorage = function (key, value) { localStorage.setItem(key, JSON.stringify(value)); }; return WebTrack; }(track_1.Track)); exports.WebTrack = WebTrack; //# sourceMappingURL=web.js.map