renderAuthorize.ts 912 B

123456789101112131415161718192021222324252627282930
  1. /* eslint-disable eslint-comments/disable-enable-pair */
  2. /* eslint-disable import/no-mutable-exports */
  3. let CURRENT: string | string[] = 'NULL';
  4. type CurrentAuthorityType = string | string[] | (() => typeof CURRENT);
  5. /**
  6. * use authority or getAuthority
  7. * @param {string|()=>String} currentAuthority
  8. */
  9. const renderAuthorize = <T>(Authorized: T): ((currentAuthority: CurrentAuthorityType) => T) => (
  10. currentAuthority: CurrentAuthorityType,
  11. ): T => {
  12. if (currentAuthority) {
  13. if (typeof currentAuthority === 'function') {
  14. CURRENT = currentAuthority();
  15. }
  16. if (
  17. Object.prototype.toString.call(currentAuthority) === '[object String]' ||
  18. Array.isArray(currentAuthority)
  19. ) {
  20. CURRENT = currentAuthority as string[];
  21. }
  22. } else {
  23. CURRENT = 'NULL';
  24. }
  25. return Authorized;
  26. };
  27. export { CURRENT };
  28. export default <T>(Authorized: T) => renderAuthorize<T>(Authorized);