UNPKG

vue-app-sdk

Version:
182 lines (181 loc) 5.45 kB
var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); import { createPersistentRef, assign } from "../../utils/index2.js"; import "../../vendors/nice-fns/dist/compactObject.js"; import "../../vendors/nice-fns/dist/pascalCase.js"; import { toValue } from "vue"; import isString from "../../vendors/lodash-es/isString.js"; const TOKEN_ID = Symbol("token"); function defaultState() { return { accessToken: {}, refreshToken: {} }; } class Token { constructor(options = {}) { __publicField(this, "id", TOKEN_ID); /** * 令牌状态 */ __publicField(this, "_state"); /** * 令牌配置 */ __publicField(this, "_tokenProfile", {}); /** * 指定令牌是否存在 * @param type 令牌类型 * @param key 令牌键 * @example * ```ts * // 应用仅存在单令牌 * token.has('accessToken') * * // 应用存在多令牌 * token.has('accessToken', 'second') * ``` */ __publicField(this, "has", (type, key = Token.defaultKey) => { return !!this.get(type, key); }); /** * 获取指定令牌 * @param type 令牌类型 * @param key 令牌键 * @example * ```ts * // 应用仅存在单令牌 * token.get('accessToken') * * // 应用存在多令牌 * token.get('accessToken', 'second') * ``` */ __publicField(this, "get", (type, key = Token.defaultKey) => { return this._state.value[type][key]; }); /** * 清理令牌状态 * @param type 令牌类型,设为空时清理全部令牌 * @param key 令牌键,设为空时清理指定类型全部令牌 * @example * ```ts * // 应用仅存在单令牌 * token.clear('accessToken') * * // 应用存在多令牌 * token.clear('accessToken', 'second') * * // 删除指定类型所有令牌 * token.clear('accessToken', '') * * // 删除全部令牌 * token.clear() * ``` */ __publicField(this, "clear", (type, key = Token.defaultKey) => { if (type) { if (key) this.set(type, "", key); else this._state.value[type] = {}; } else { this._state.value = defaultState(); } }); /** * 转为 JWT(JSON Web Token) 格式令牌 * @param prefix JWT(JSON Web Token) 前缀 * @param key 令牌键 * @returns JWT(JSON Web Token) 格式令牌 * @example * ```ts * // 使用默认 `jwtPrefix` * token.toJWT() // 'Bearer abcdefg' * * // 使用自定义 `jwtPrefix` * token.toJWT('Token') // 'Token abcdefg'' * ``` */ __publicField(this, "toJWT", (prefix, key = Token.defaultKey) => { if (prefix == null) prefix = this.getTokenProfile(key).jwtPrefix; return [prefix, this.get("accessToken", key)].filter(Boolean).join(" "); }); /** * 根据设定令牌格式获取访问令牌 * @param key 令牌键 * @example * ```ts * // 普通格式 * const token = createToken() * token.set('accessToken', 'abcdefg') * token.resolve() // 'abcdefg' * * // JWT 格式 * const token = createToken({ format: 'jwt' }) * token.set('accessToken', 'abcdefg') * token.resolve() // 'Bearer abcdefg' * * // 单独设置 * const token = createToken() * token.set('accessToken', 'abcdefg', { format: 'jwt', jwtPrefix: 'Token' }) * token.resolve() // 'Token abcdefg' * ``` */ __publicField(this, "resolve", (key = Token.defaultKey) => { const profile = this.getTokenProfile(key); return toValue(profile.format) === "jwt" ? this.toJWT(profile.jwtPrefix, key) : this.get("accessToken", key); }); /** * 获取令牌配置 * @param key 令牌键 */ __publicField(this, "getTokenProfile", (key = Token.defaultKey) => { return Object.assign( { format: "normal", jwtPrefix: "Bearer" }, this.options, this._tokenProfile[key] ); }); __publicField(this, "install", (sdk) => { sdk.hook("sdk:cleanup", this.clear); }); this.options = options; const { persistent = true, window, storage, storageKey = "__VUE_APP_SDK__TOKEN__" } = options; this._state = createPersistentRef({ persistent, window, storage, storageKey, value: defaultState() }); this.set = this.set.bind(this); } /** * 默认令牌键 */ static get defaultKey() { return "__PRIVATE__"; } set(...args) { if (isString(args[0])) { const [type, value, ..._args] = args; args = [{ [type]: value }, ..._args]; } const [state, keyOrProfile, profileOrNil] = args; const key = (isString(keyOrProfile) ? keyOrProfile : Token.defaultKey) || Token.defaultKey; const profile = isString(keyOrProfile) ? profileOrNil : keyOrProfile; Object.entries(state).forEach(([type, value]) => { assign(this._state.value[type], { [key]: value }); }); if (!this._tokenProfile[key]) this._tokenProfile[key] = {}; assign(this._tokenProfile[key], profile); } } export { TOKEN_ID, Token };