UNPKG

@planjs/utils

Version:

🔧 Common tools collection

184 lines (156 loc) 4.61 kB
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2"; import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck"; import _createClass from "@babel/runtime/helpers/esm/createClass"; import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; import { parse, stringify } from 'query-string'; import { isBrowser } from '../is/is-platform'; /** * oauth授权回调页面参数处理 * 浏览器端才能用 */ var OauthClient = /*#__PURE__*/function () { function OauthClient(_ref) { var consumerKeys = _ref.consumerKeys, mode = _ref.mode, _ref$url = _ref.url, url = _ref$url === void 0 ? isBrowser() ? window.location.href : '' : _ref$url, qsParseArgs = _ref.qsParseArgs, qsStringifyArgs = _ref.qsStringifyArgs; _classCallCheck(this, OauthClient); _defineProperty(this, "consumerKeys", void 0); _defineProperty(this, "mode", void 0); _defineProperty(this, "url", void 0); _defineProperty(this, "_qsParseArgs", void 0); _defineProperty(this, "_qsStringifyArgs", void 0); this.consumerKeys = consumerKeys; this.mode = mode; this.setUrl(url); this._qsParseArgs = qsParseArgs || {}; this._qsStringifyArgs = qsStringifyArgs || {}; } _createClass(OauthClient, [{ key: "_stringify", value: function _stringify(obj) { return stringify(obj, this._qsStringifyArgs); } }, { key: "_parse", value: function _parse(str) { return parse(str, this._qsParseArgs); } }, { key: "isHashMode", get: function get() { return this.mode === 'hash'; } }, { key: "isHistoryMode", get: function get() { return this.mode === 'history'; } }, { key: "hashQuery", get: function get() { return this._parse(this.url.hash.split('?').pop() || '') || {}; } }, { key: "searchQuery", get: function get() { return this._parse(this.url.search); } /** * 当前url query参数 url query / hash query * 如果没有传则处理左右参数 */ }, { key: "query", get: function get() { return _objectSpread(_objectSpread({}, this.isHashMode || !this.mode ? this.hashQuery : {}), this.isHistoryMode || !this.mode ? this.searchQuery : {}); } /** * 获取掉oauth参数的,返回新的对象 * @param obj */ }, { key: "filterConsumerKeys", value: function filterConsumerKeys(obj) { var _this = this; return Object.keys(obj).filter(function (v) { return !_this.consumerKeys.includes(v); }).reduce(function (acc, k) { acc[k] = obj[k]; return acc; }, {}); } /** * 去除掉 oauth consumerKeys 参数 */ }, { key: "originalUrl", get: function get() { return this.getOriginalUrl(this.url); } /** * 传入自定义的地址,过滤参数 * @param uri */ }, { key: "getOriginalUrl", value: function getOriginalUrl(uri) { var _url$hash$split; var url = uri instanceof URL ? uri : new URL(uri); var t = new URL(url.href); var hashQuery = this._parse(((_url$hash$split = url.hash.split('?')) === null || _url$hash$split === void 0 ? void 0 : _url$hash$split[1]) || '') || {}; var searchQuery = this._parse(url.search); if (!this.mode || this.isHashMode) { var s = this._stringify(this.filterConsumerKeys(hashQuery)); t.hash = url.hash.split('?')[0] || ''; if (s) t.hash += '?' + s; } if (!this.mode || this.isHistoryMode) { var _s = this._stringify(this.filterConsumerKeys(searchQuery)); t.search = '?' + _s; } return t.href; } }, { key: "hasAuthParams", get: function get() { var _this2 = this; return this.consumerKeys.every(function (v) { return !!_this2.query[v]; }); } /** * 设置要处理参数的url * @param url */ }, { key: "setUrl", value: function setUrl(url) { if (url instanceof URL) { this.url = url; } else { this.url = new URL(url); } return this; } /** * 获取授权参数 * @return 返回oauth回调参数 */ }, { key: "getAuthParams", value: function getAuthParams() { var _this3 = this; if (this.hasAuthParams) { return this.consumerKeys.reduce(function (acc, k) { acc[k] = _this3.query[k]; return acc; }, {}); } } }]); return OauthClient; }(); export default OauthClient;