@seed-fe/permission
Version:
Framework-agnostic permission management library with batching, caching, and debouncing
2 lines • 2.23 kB
JavaScript
import {createBatchRequest}from'@seed-fe/batch-request';var C="@seed-fe/permission",a={DELAY:50,MAX_SIZE:100},P={ENABLE:false,TTL:3e5},o={ALL:"all",ANY:"any"},S="permission-batch";var p=class extends Error{constructor(r,s,d,R){super(r);this.packageName=s;this.permissionConfig=d;this.originalError=R;this.name="PermissionError";}};function E(e){if(typeof e=="string")return {code:e,mode:o.ALL};if(Array.isArray(e))return {code:e,mode:o.ALL};let i=e;return {code:i.code,mode:i.mode||o.ALL,biz:i.biz}}function m(e){return typeof e=="boolean"?e:typeof e=="number"?e===1:false}function y(e){return e.map(i=>m(i))}function A(e){return JSON.stringify({code:Array.isArray(e.code)?[...e.code].sort():e.code,mode:e.mode||o.ALL,biz:e.biz||{}})}function f(e,i,r,s){return new p(e,i,r,s)}var l=null,g={batch:{delay:a.DELAY,maxSize:a.MAX_SIZE},cache:{enable:P.ENABLE,ttl:P.TTL}},c=null;function I(e){return JSON.stringify({code:Array.isArray(e.code)?[...e.code].sort():e.code,mode:e.mode||o.ALL,biz:e.biz||{}})}function b(e){return e.map(i=>m(i))}function u(e,i={}){l=e;let r={batch:{...g.batch,...i.batch},cache:{...g.cache,...i.cache}};c=createBatchRequest({endpoint:S,service:async s=>{let d=await e(s);return b(d)},batch:r.batch,cache:r.cache,cacheKey:I});}async function h(e,i){try{if(i){let s=await i([e]);return m(s[0])}if(!c)throw f("[@seed-fe/permission] Permission service not registered",C,e);return await c(e)}catch(r){throw r instanceof Error&&r.name==="PermissionError"?r:f("[@seed-fe/permission] Permission check failed",C,e,r)}}function v(){c&&l&&u(l);}function N(){return {hasGlobalService:!!l,hasBatchFunction:!!c}}function x(e){u(e.permissionService,e.permissionServiceOptions);}async function _(e,i){let r=E(e);return h(r,i)}export{a as BATCH_CONFIG,P as CACHE_CONFIG,S as PERMISSION_BATCH_ENDPOINT,o as PERMISSION_CHECK_MODES,p as PermissionError,_ as checkPermission,v as clearPermissionCache,x as configurePermission,h as coreCheckPermission,A as createPermissionCacheKey,f as createPermissionError,N as getPermissionStats,E as normalizePermissionConfig,m as normalizePermissionResult,y as normalizePermissionResults,u as registerPermissionService};//# sourceMappingURL=index.js.map
//# sourceMappingURL=index.js.map