renderAuthorize.ts 804 Bytes
Newer Older
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
1
let CURRENT: string | string[] = 'NULL';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
2
type CurrentAuthorityType = string | string[] | (() => typeof CURRENT);
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
3 4 5 6
/**
 * use  authority or getAuthority
 * @param {string|()=>String} currentAuthority
 */
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
7 8
const renderAuthorize = <T>(Authorized: T): ((currentAuthority: CurrentAuthorityType) => T) => (
  currentAuthority: CurrentAuthorityType,
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
) => {
  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
27
export default <T>(Authorized: T) => renderAuthorize<T>(Authorized);