UNPKG

@seed-fe/permission

Version:

Framework-agnostic permission management library with batching, caching, and debouncing

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