import React from "react" ; import PromiseRender from "./PromiseRender" ; export type IAuthorityType = undefined | string | string [ ] | Promise < boolean > | ( ( permissions : string [ ] ) => boolean | Promise < boolean > ) ; param authority param permissions param target param Exception const checkPermissions = < T , K > ( authority , permissions = [ ] , target , Exception ) : T | K | React . ReactNode => { if ( ! authority ) { return target ; } if ( Array . isArray ( authority ) ) { if ( permissions . some ( ( item ) => authority . includes ( item ) ) ) { return target ; } if ( permissions . length > 0 && permissions . every ( ( item ) => authority . includes ( item ) ) ) { return target ; } return Exception ; } if ( typeof authority === "string" ) { if ( permissions . some ( ( item ) => authority === item ) ) { return target ; } return Exception ; } if ( authority instanceof Promise ) { return ( < PromiseRender < T , K > ok = { target } error = { Exception } promise = { authority } / > ) ; } if ( typeof authority === "function" ) { const promise = authority ( permissions ) ; if ( ( promise as Promise < boolean > ) instanceof Promise ) { return ( < PromiseRender < T , K > ok = { target } error = { Exception } promise = { promise as Promise < boolean > } / > ) ; } if ( promise ) { return target ; } return Exception ; } throw new Error ( "Unsupported parameters" ) ; } ; export default checkPermissions ;