Forráskód Böngészése

Authorized typescript (#573)

* add Authorized typescript support

* add currentAuthority is undefine test
陈帅 8 éve
szülő
commit
c859fde823

+ 3 - 0
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');
   });

+ 47 - 0
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<string> | authorityFN | Promise<any>;
+
+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<AuthorizedProps, any> {
+  static Secured: Secured;
+  static AuthorizedRoute: typeof AuthorizedRoute;
+  static check: check;
+  constructor(props: AuthorizedProps);
+}
+
+declare function renderAuthorize(currentAuthority: string): typeof Authorized;
+
+export default renderAuthorize;