From c859fde8233dba3bfbb1826be2f718773291b720 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=B8=85?= Date: Mon, 8 Jan 2018 13:52:27 +0800 Subject: [PATCH] Authorized typescript (#573) * add Authorized typescript support * add currentAuthority is undefine test --- .../Authorized/CheckPermissions.test.js | 3 ++ src/components/Authorized/index.d.ts | 47 +++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 src/components/Authorized/index.d.ts diff --git a/src/components/Authorized/CheckPermissions.test.js b/src/components/Authorized/CheckPermissions.test.js index ee686b32..eda08a7f 100644 --- a/src/components/Authorized/CheckPermissions.test.js +++ b/src/components/Authorized/CheckPermissions.test.js @@ -13,6 +13,9 @@ describe('test CheckPermissions', () => { it('authority is undefined , return ok', () => { expect(checkPermissions(null, 'NULL', target, error)).toEqual('ok'); }); + it('currentAuthority is undefined , return error', () => { + expect(checkPermissions('admin', null, target, error)).toEqual('error'); + }); it('Wrong string permission authentication', () => { expect(checkPermissions('admin', 'user', target, error)).toEqual('error'); }); diff --git a/src/components/Authorized/index.d.ts b/src/components/Authorized/index.d.ts new file mode 100644 index 00000000..6b073a65 --- /dev/null +++ b/src/components/Authorized/index.d.ts @@ -0,0 +1,47 @@ +import * as React from "react"; +import * as H from "history"; +import { RouteProps } from "react-router"; + +type authorityFN = () => string; + +type authority = string | Array | authorityFN | Promise; + +interface Secured { + (authority: authority, error?: React.ReactNode): ( + target: React.ReactNode + ) => React.ReactNode; +} + +export interface AuthorizedRouteProps extends RouteProps { + authority: authority; +} +export class AuthorizedRoute extends React.Component< + AuthorizedRouteProps, + any +> { + constructor(props: AuthorizedRouteProps); +} + +interface check { + ( + authority: authority, + target: React.ReactNode, + Exception: React.ReactNode + ): React.ReactNode; +} + +interface AuthorizedProps { + authority: authority; + noMatch?: React.ReactNode; +} + +export class Authorized extends React.Component { + static Secured: Secured; + static AuthorizedRoute: typeof AuthorizedRoute; + static check: check; + constructor(props: AuthorizedProps); +} + +declare function renderAuthorize(currentAuthority: string): typeof Authorized; + +export default renderAuthorize; -- GitLab