import React from 'react' ; import CheckPermissions from './CheckPermissions' ; /**
 * 默认不能访问任何页面
 * default is "NULL"
 */ const Exception403 = ( ) => 403 ; export const isComponentClass = ( component ) : boolean => { if ( ! component ) return false ; const proto = Object . getPrototypeOf ( component ) ; if ( proto === React . Component || proto === Function . prototype ) return true ; return isComponentClass ( proto ) ; } ; const checkIsInstantiation = ( target ) => { if ( isComponentClass ( target ) ) { const Target = target as React . ComponentClass ; return ( props ) => < Target { ... props } / > ; } if ( React . isValidElement ( target ) ) { return ( props ) => React . cloneElement ( target , props ) ; } return ( ) => target ; } ; param { string | function | Promise } authority param { ReactNode } error const authorize = ( authority , error ? ) => { /**
     * conversion into a class
     * 防止传入字符串时找不到staticContext造成报错
     * String parameters can cause staticContext not found error
     */ let classError = false ; if ( error ) { classError = ( ( ) => error ) as React . FunctionComponent ; } if ( ! authority ) { throw new Error ( 'authority is required' ) ; } return function decideAuthority ( target ) { const component = CheckPermissions ( authority , target , classError || Exception403 ) ; return checkIsInstantiation ( component ) ; } ; } ; export default authorize ;