renderAuthorize.ts 912 Bytes
Newer Older
1 2
/* eslint-disable eslint-comments/disable-enable-pair */
/* eslint-disable import/no-mutable-exports */
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
3
let CURRENT: string | string[] = 'NULL';
4

ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
5
type CurrentAuthorityType = string | string[] | (() => typeof CURRENT);
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
6 7 8 9
/**
 * use  authority or getAuthority
 * @param {string|()=>String} currentAuthority
 */
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
10 11
const renderAuthorize = <T>(Authorized: T): ((currentAuthority: CurrentAuthorityType) => T) => (
  currentAuthority: CurrentAuthorityType,
12
): T => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
  if (currentAuthority) {
    if (typeof currentAuthority === 'function') {
      CURRENT = currentAuthority();
    }
    if (
      Object.prototype.toString.call(currentAuthority) === '[object String]' ||
      Array.isArray(currentAuthority)
    ) {
      CURRENT = currentAuthority as string[];
    }
  } else {
    CURRENT = 'NULL';
  }
  return Authorized;
};

export { CURRENT };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
30
export default <T>(Authorized: T) => renderAuthorize<T>(Authorized);