UNPKG

@looker/sdk-rtl

Version:
123 lines 8.04 kB
var _templateObject; function _wrapRegExp() { _wrapRegExp = function _wrapRegExp(e, r) { return new BabelRegExp(e, void 0, r); }; var e = RegExp.prototype, r = new WeakMap(); function BabelRegExp(e, t, p) { var o = new RegExp(e, t); return r.set(o, p || r.get(e)), _setPrototypeOf(o, BabelRegExp.prototype); } function buildGroups(e, t) { var p = r.get(t); return Object.keys(p).reduce(function (r, t) { var o = p[t]; if ("number" == typeof o) r[t] = e[o];else { for (var i = 0; void 0 === e[o[i]] && i + 1 < o.length;) i++; r[t] = e[o[i]]; } return r; }, Object.create(null)); } return _inherits(BabelRegExp, RegExp), BabelRegExp.prototype.exec = function (r) { var t = e.exec.call(this, r); if (t) { t.groups = buildGroups(t, this); var p = t.indices; p && (p.groups = buildGroups(p, this)); } return t; }, BabelRegExp.prototype[Symbol.replace] = function (t, p) { if ("string" == typeof p) { var o = r.get(this); return e[Symbol.replace].call(this, t, p.replace(/\$<([^>]+)>/g, function (e, r) { var t = o[r]; return "$" + (Array.isArray(t) ? t.join("$") : t); })); } if ("function" == typeof p) { var i = this; return e[Symbol.replace].call(this, t, function () { var e = arguments; return "object" != typeof e[e.length - 1] && (e = [].slice.call(e)).push(buildGroups(e, i)), p.apply(this, e); }); } return e[Symbol.replace].call(this, t, p); }, _wrapRegExp.apply(this, arguments); } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : String(i); } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); } import { sdkOk } from './transport'; export var ErrorCodesUrl = 'https://static-a.cdn.looker.app/errorcodes/'; export var ErrorDocNotFound = '### No documentation found for '; var ErrorDocPatternExpression = String.raw(_templateObject || (_templateObject = _taggedTemplateLiteral(["(?<redirector>(https://docs.looker.com/r|https://cloud.google.com/looker/docs/r)?/err/)(?<apiVersion>.*)/(?<statusCode>d{3})(?<apiPath>.*)"], ["(?<redirector>(https:\\/\\/docs\\.looker\\.com\\/r|https:\\/\\/cloud\\.google\\.com\\/looker\\/docs\\/r)?\\/err\\/)(?<apiVersion>.*)\\/(?<statusCode>\\d{3})(?<apiPath>.*)"]))); export var ErrorDocRx = RegExp(ErrorDocPatternExpression, 'i'); var sdkUrlGet = function () { var _ref = _asyncToGenerator(function* (sdk, url) { return yield sdkOk(sdk.authSession.transport.request('GET', url)); }); return function sdkUrlGet(_x, _x2) { return _ref.apply(this, arguments); }; }(); export class ErrorDoc { constructor(sdk) { var getter = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : sdkUrlGet; var cdnUrl = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : ErrorCodesUrl; this.sdk = sdk; this.getter = getter; this.cdnUrl = cdnUrl; _defineProperty(this, "_index", undefined); } load() { var _this = this; return _asyncToGenerator(function* () { if (!_this._index) { try { var result = yield _this.getter(_this.sdk, _this.indexUrl); if (typeof result === 'string') { result = JSON.parse(result); } _this._index = result; } catch (e) { return Promise.resolve({}); } } return _this._index; })(); } get indexUrl() { return this.contentUrl('index.json'); } get index() { if (!this._index) { this.load().catch(reason => console.error(reason)); } return this._index; } specPath(path) { var result = path.replace(/:\w+/g, found => "{".concat(found.substring(1), "}")); return result; } errorKey(docUrl) { var bits = this.parse(docUrl); if (!bits.redirector) return ''; return this.specPath("".concat(bits.statusCode).concat(bits.apiPath)); } notFound(key) { return "".concat(ErrorDocNotFound).concat(key); } getContent(url) { var _this2 = this; return _asyncToGenerator(function* () { try { return yield _this2.getter(_this2.sdk, url); } catch (e) { return Promise.resolve(_this2.notFound(e.message)); } })(); } content(docUrl) { var _this3 = this; return _asyncToGenerator(function* () { var key = _this3.errorKey(docUrl); if (!key) { return Promise.resolve(_this3.notFound('bad error code link')); } yield _this3.load(); var item = _this3.index ? _this3.index[key] : undefined; if (!item) { var code = key.split('/')[0]; item = _this3.index ? _this3.index[code] : undefined; if (!item) { return Promise.resolve(_this3.notFound(key)); } } var url = _this3.contentUrl(item.url); return yield _this3.getContent(url); })(); } contentUrl(urlPath) { return "".concat(this.cdnUrl).concat(urlPath); } methodName(errorMdUrl) { var _match$groups; var ErrorMdRx = _wrapRegExp(/(\w+)_\d{3}\.md/i, { name: 1 }); var match = errorMdUrl.match(ErrorMdRx); return (match === null || match === void 0 ? void 0 : (_match$groups = match.groups) === null || _match$groups === void 0 ? void 0 : _match$groups.name) || ''; } parse(docUrl) { var _match$groups2, _match$groups3, _match$groups4, _match$groups5; var match = docUrl.match(ErrorDocRx); return { redirector: (match === null || match === void 0 ? void 0 : (_match$groups2 = match.groups) === null || _match$groups2 === void 0 ? void 0 : _match$groups2.redirector) || '', apiVersion: (match === null || match === void 0 ? void 0 : (_match$groups3 = match.groups) === null || _match$groups3 === void 0 ? void 0 : _match$groups3.apiVersion) || '', statusCode: (match === null || match === void 0 ? void 0 : (_match$groups4 = match.groups) === null || _match$groups4 === void 0 ? void 0 : _match$groups4.statusCode) || '', apiPath: (match === null || match === void 0 ? void 0 : (_match$groups5 = match.groups) === null || _match$groups5 === void 0 ? void 0 : _match$groups5.apiPath) || '' }; } } //# sourceMappingURL=errorDoc.js.map