diff --git a/types/redux-auth-wrapper/authWrapper.d.ts b/types/redux-auth-wrapper/authWrapper.d.ts new file mode 100644 index 0000000000..0b92331e3b --- /dev/null +++ b/types/redux-auth-wrapper/authWrapper.d.ts @@ -0,0 +1,15 @@ +import { ReactType } from "react"; +import { AuthConfig, AuthWrapperDecorator } from "redux-auth-wrapper"; + +export interface InjectedAuthProps { + isAuthenticated: boolean; + isAuthenticating: boolean; +} + +export interface AuthWrapperConfig extends AuthConfig { + FailureComponent?: ReactType; +} + +export function authWrapper( + config: AuthWrapperConfig +): AuthWrapperDecorator; diff --git a/types/redux-auth-wrapper/connectedAuthWrapper.d.ts b/types/redux-auth-wrapper/connectedAuthWrapper.d.ts new file mode 100644 index 0000000000..2beb0b1180 --- /dev/null +++ b/types/redux-auth-wrapper/connectedAuthWrapper.d.ts @@ -0,0 +1,9 @@ +import { ReactType } from "react"; +import { AuthBaseConfig, AuthWrapperDecorator } from "redux-auth-wrapper"; +import { AuthWrapperConfig, InjectedAuthProps } from "redux-auth-wrapper/authWrapper"; + +export type ConnectedAuthWrapperConfig = AuthWrapperConfig & AuthBaseConfig; + +export function connectedAuthWrapper( + config: ConnectedAuthWrapperConfig +): AuthWrapperDecorator; diff --git a/types/redux-auth-wrapper/history3/redirect.d.ts b/types/redux-auth-wrapper/history3/redirect.d.ts new file mode 100644 index 0000000000..a5ef70b3e1 --- /dev/null +++ b/types/redux-auth-wrapper/history3/redirect.d.ts @@ -0,0 +1,55 @@ +import { Action, Store } from "redux"; +import { Location, LocationDescriptorObject } from "history"; +import { AuthBaseConfig, AuthConfig, AuthWrapperDecorator, StateSelector } from "redux-auth-wrapper"; +import { InjectedAuthProps } from "redux-auth-wrapper/authWrapper"; +import { ConnectedAuthWrapperConfig } from "redux-auth-wrapper/connectedAuthWrapper"; + +export interface InjectedAuthReduxProps extends InjectedAuthProps { + redirectPath: string; +} + +export interface InjectedAuthRouterProps Action> extends InjectedAuthReduxProps { + redirect: Redirect; +} + +export interface ConnectedRouterRedirectConfig extends AuthBaseConfig { + redirectPath: string | StateSelector; + redirectAction?(location: Location): Action; + allowRedirectBack?: boolean | StateSelector; + redirectQueryParamName?: string; +} + +export function connectedRouterRedirect( + config: ConnectedRouterRedirectConfig +): AuthWrapperDecorator; + +export type ConnectedReduxRedirectConfig = ConnectedRouterRedirectConfig; + +export function connectedReduxRedirect( + config: ConnectedReduxRedirectConfig +): AuthWrapperDecorator; + +export type StateMutateSelector = (state: State, nextState: State) => R; + +export interface CreateOnEnterConfig extends AuthConfig { + redirectPath: string | StateMutateSelector; + authenticatedSelector: StateMutateSelector; + authenticatingSelector?: StateMutateSelector; + allowRedirectBack?: boolean | StateMutateSelector; + redirectQueryParamName?: string; +} + +export function createOnEnter(config: CreateOnEnterConfig): + (store: Store, nextState: State, redirect: (location: LocationDescriptorObject) => void) => void; + +export interface LocationHelperConfig { + redirectQueryParamName?: string; + locationSelector?(props: Props): LocationDescriptorObject; +} + +export interface LocationHelper { + getRedirectQueryParam(props: Props): string; + createRedirectLoc(props: Props, redirectPath: string): LocationDescriptorObject; +} + +export function locationHelperBuilder(config: LocationHelperConfig): LocationHelper; diff --git a/types/redux-auth-wrapper/history4/redirect.d.ts b/types/redux-auth-wrapper/history4/redirect.d.ts new file mode 100644 index 0000000000..74b4655bc1 --- /dev/null +++ b/types/redux-auth-wrapper/history4/redirect.d.ts @@ -0,0 +1,25 @@ +import { Action } from "redux"; +import { Location } from "history"; +import { AuthWrapperDecorator } from "redux-auth-wrapper"; +import { + ConnectedRouterRedirectConfig, + InjectedAuthReduxProps +} from "redux-auth-wrapper/history3/redirect"; + +export { + connectedRouterRedirect, + ConnectedRouterRedirectConfig, + InjectedAuthReduxProps, + InjectedAuthRouterProps, + locationHelperBuilder, + LocationHelper, + LocationHelperConfig +} from "redux-auth-wrapper/history3/redirect"; + +export interface ConnectedReduxRedirectConfig extends ConnectedRouterRedirectConfig { + redirectAction(location: Location): Action; +} + +export function connectedReduxRedirect( + config: ConnectedReduxRedirectConfig +): AuthWrapperDecorator; diff --git a/types/redux-auth-wrapper/index.d.ts b/types/redux-auth-wrapper/index.d.ts index 453a4e7e02..52bea44218 100644 --- a/types/redux-auth-wrapper/index.d.ts +++ b/types/redux-auth-wrapper/index.d.ts @@ -1,33 +1,21 @@ -// Type definitions for redux-auth-wrapper 1.0 +// Type definitions for redux-auth-wrapper 2.0 // Project: https://github.com/mjrussell/redux-auth-wrapper // Definitions by: Karol Janyst // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.3 -import { ComponentClass, StatelessComponent, ReactType } from "react"; -import { Action } from "redux"; -import { Location } from "history"; +import { ComponentClass, StatelessComponent, ComponentType, ReactType } from "react"; -export type ComponentConstructor

= ComponentClass

| StatelessComponent

; +export type StateSelector = (state: State, props: OwnProps) => R; -export interface InjectedProps { - authData?: AuthData; -} +export type AuthWrapperDecorator = (component: ComponentType) => ComponentClass; -export interface AuthWrapperConfig { - allowRedirectBack?: boolean | ((location: Location, redirectPath: string) => boolean); - authenticatingSelector?(state: State, ownProps?: Props): boolean; - authSelector(state: State, ownProps?: Props): AuthData; - FailureComponent?: ReactType; - failureRedirectPath?: string | ((state: State, ownProps?: Props) => string); - LoadingComponent?: ReactType; - redirectQueryParamName?: string; +export interface AuthConfig { + AuthenticatingComponent?: ReactType; wrapperDisplayName?: string; - predicate?(authData: AuthData): boolean; - propMapper?(ownProps: Props): InjectedProps & Props; - redirectAction?(...args: any[]): Action; } -export type AuthDecorator = (component: ComponentConstructor) => ComponentClass; - -export function UserAuthWrapper(config: AuthWrapperConfig): AuthDecorator; +export interface AuthBaseConfig extends AuthConfig { + authenticatedSelector: StateSelector; + authenticatingSelector?: StateSelector; +} diff --git a/types/redux-auth-wrapper/redux-auth-wrapper-tests.tsx b/types/redux-auth-wrapper/redux-auth-wrapper-tests.tsx index e37390410e..a009bdcc7f 100644 --- a/types/redux-auth-wrapper/redux-auth-wrapper-tests.tsx +++ b/types/redux-auth-wrapper/redux-auth-wrapper-tests.tsx @@ -1,28 +1,168 @@ import * as React from "react"; -import { StatelessComponent } from "react"; -import { UserAuthWrapper } from "redux-auth-wrapper"; +import { Component, StatelessComponent } from "react"; +import { createStore } from "redux"; +import { push } from "react-router-redux"; +import { LocationDescriptorObject } from "history"; -const Auth = UserAuthWrapper({ +import { + connectedRouterRedirect as H3connectedRouterRedirect, + connectedReduxRedirect as H3connectedReduxRedirect, + createOnEnter, + locationHelperBuilder as H3locationHelperBuilder +} from "redux-auth-wrapper/history3/redirect"; + +import { + connectedRouterRedirect, + connectedReduxRedirect, + locationHelperBuilder +} from "redux-auth-wrapper/history4/redirect"; + +import { authWrapper } from "redux-auth-wrapper/authWrapper"; +import { connectedAuthWrapper } from "redux-auth-wrapper/connectedAuthWrapper"; + +interface TestReduxState { + foo: string; +} + +interface TestComponentProps { + foo: string; +} + +const stringSelector = (state: TestReduxState, ownProps: TestComponentProps) => "fizz"; +const boolSelector = (state: TestReduxState, ownProps: TestComponentProps) => true; +const stringStateSelector = (state: TestReduxState, nextState: TestReduxState) => "buzz"; +const boolStateSelector = (state: TestReduxState, nextState: TestReduxState) => true; + +/* History 3 */ + +H3connectedRouterRedirect({ + redirectPath: "/login", + authenticatedSelector: boolSelector, + authenticatingSelector: boolSelector, + AuthenticatingComponent: "div", + wrapperDisplayName: "Auth", allowRedirectBack: true, - authenticatingSelector(state: any) { - return state.auth.loading; - }, - authSelector(state: any) { - return state.auth; - }, - FailureComponent: () => (

), - failureRedirectPath: "/401", - LoadingComponent: () => (
), - redirectAction: () => ({ type : "redirect" }), - redirectQueryParamName: "next", - predicate(authData: any) { - return authData.authorized; - }, - wrapperDisplayName: "TestAuth" + redirectQueryParamName: "redirect" +})( + ({ + foo, + isAuthenticated, + isAuthenticating, + redirect, + redirectPath + }) => null +); + +H3connectedReduxRedirect({ + redirectPath: stringSelector, + authenticatedSelector: boolSelector, + authenticatingSelector: boolSelector, + AuthenticatingComponent: "div", + wrapperDisplayName: "Auth", + allowRedirectBack: boolSelector, + redirectQueryParamName: "redirect" +})( + ({ + foo, + isAuthenticated, + isAuthenticating, + redirectPath + }) => null +); + +const store = createStore(() => ({})); + +const enter = createOnEnter({ + redirectPath: "/login", + authenticatedSelector: boolStateSelector, + authenticatingSelector: boolStateSelector, + allowRedirectBack: true, + redirectQueryParamName: "redirect" }); +enter(store, { foo : "bar" }, (location: LocationDescriptorObject) => {}); -export const TestAuthComponent: StatelessComponent = () => { - return (
); -}; +const H3helper = H3locationHelperBuilder({ + redirectQueryParamName: "redirect", + locationSelector: ({ foo }) => ({ pathname : "foo" }) +}); +H3helper.getRedirectQueryParam({ foo : "bar" }); +H3helper.createRedirectLoc({ foo : "bar" }, "redirect"); -const TestAuth = Auth(TestAuthComponent); +/* History 4 */ + +connectedRouterRedirect({ + redirectPath: "/login", + authenticatedSelector: boolSelector, + authenticatingSelector: boolSelector, + AuthenticatingComponent: (props: any) => null, + wrapperDisplayName: "Auth", + allowRedirectBack: true, + redirectQueryParamName: "redirect" +})( + ({ + foo, + isAuthenticated, + isAuthenticating, + redirect, + redirectPath + }) => null +); + +class Loading extends Component { + render() { + return null; + } +} + +connectedReduxRedirect({ + redirectPath: "/login", + redirectAction: push, + authenticatedSelector: boolSelector, + authenticatingSelector: boolSelector, + AuthenticatingComponent: Loading, + wrapperDisplayName: "Auth", + allowRedirectBack: true, + redirectQueryParamName: "redirect" +})( + ({ + foo, + isAuthenticated, + isAuthenticating, + redirectPath + }) => null +); + +const helper = locationHelperBuilder({ + redirectQueryParamName: "redirect", + locationSelector: ({ foo }) => ({ pathname : "foo" }) +}); +helper.getRedirectQueryParam({ foo : "bar" }); +helper.createRedirectLoc({ foo : "bar" }, "redirect"); + +/* Other Wrappers */ + +authWrapper({ + AuthenticatingComponent: "div", + FailureComponent: "div", + wrapperDisplayName: "Auth" +})( + ({ + foo, + isAuthenticated, + isAuthenticating + }) => null +); + +connectedAuthWrapper({ + authenticatedSelector: boolSelector, + authenticatingSelector: boolSelector, + AuthenticatingComponent: "div", + FailureComponent: "div", + wrapperDisplayName: "Auth" +})( + ({ + foo, + isAuthenticated, + isAuthenticating + }) => null +); diff --git a/types/redux-auth-wrapper/tsconfig.json b/types/redux-auth-wrapper/tsconfig.json index 5aae706c27..33b9707503 100644 --- a/types/redux-auth-wrapper/tsconfig.json +++ b/types/redux-auth-wrapper/tsconfig.json @@ -10,16 +10,16 @@ "strictNullChecks": true, "baseUrl": "../", "typeRoots": ["../"], - "paths": { - "history": ["history/v3"], - "history/*": ["history/v3/*"] - }, "types": [], "noEmit": true, "forceConsistentCasingInFileNames": true, "jsx": "react" }, "files": [ + "history3/redirect.d.ts", + "history4/redirect.d.ts", + "authWrapper.d.ts", + "connectedAuthWrapper.d.ts", "index.d.ts", "redux-auth-wrapper-tests.tsx" ] diff --git a/types/redux-auth-wrapper/v1/index.d.ts b/types/redux-auth-wrapper/v1/index.d.ts new file mode 100644 index 0000000000..453a4e7e02 --- /dev/null +++ b/types/redux-auth-wrapper/v1/index.d.ts @@ -0,0 +1,33 @@ +// Type definitions for redux-auth-wrapper 1.0 +// Project: https://github.com/mjrussell/redux-auth-wrapper +// Definitions by: Karol Janyst +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 + +import { ComponentClass, StatelessComponent, ReactType } from "react"; +import { Action } from "redux"; +import { Location } from "history"; + +export type ComponentConstructor

= ComponentClass

| StatelessComponent

; + +export interface InjectedProps { + authData?: AuthData; +} + +export interface AuthWrapperConfig { + allowRedirectBack?: boolean | ((location: Location, redirectPath: string) => boolean); + authenticatingSelector?(state: State, ownProps?: Props): boolean; + authSelector(state: State, ownProps?: Props): AuthData; + FailureComponent?: ReactType; + failureRedirectPath?: string | ((state: State, ownProps?: Props) => string); + LoadingComponent?: ReactType; + redirectQueryParamName?: string; + wrapperDisplayName?: string; + predicate?(authData: AuthData): boolean; + propMapper?(ownProps: Props): InjectedProps & Props; + redirectAction?(...args: any[]): Action; +} + +export type AuthDecorator = (component: ComponentConstructor) => ComponentClass; + +export function UserAuthWrapper(config: AuthWrapperConfig): AuthDecorator; diff --git a/types/redux-auth-wrapper/v1/redux-auth-wrapper-tests.tsx b/types/redux-auth-wrapper/v1/redux-auth-wrapper-tests.tsx new file mode 100644 index 0000000000..e37390410e --- /dev/null +++ b/types/redux-auth-wrapper/v1/redux-auth-wrapper-tests.tsx @@ -0,0 +1,28 @@ +import * as React from "react"; +import { StatelessComponent } from "react"; +import { UserAuthWrapper } from "redux-auth-wrapper"; + +const Auth = UserAuthWrapper({ + allowRedirectBack: true, + authenticatingSelector(state: any) { + return state.auth.loading; + }, + authSelector(state: any) { + return state.auth; + }, + FailureComponent: () => (

), + failureRedirectPath: "/401", + LoadingComponent: () => (
), + redirectAction: () => ({ type : "redirect" }), + redirectQueryParamName: "next", + predicate(authData: any) { + return authData.authorized; + }, + wrapperDisplayName: "TestAuth" +}); + +export const TestAuthComponent: StatelessComponent = () => { + return (
); +}; + +const TestAuth = Auth(TestAuthComponent); diff --git a/types/redux-auth-wrapper/v1/tsconfig.json b/types/redux-auth-wrapper/v1/tsconfig.json new file mode 100644 index 0000000000..ec5711a05e --- /dev/null +++ b/types/redux-auth-wrapper/v1/tsconfig.json @@ -0,0 +1,29 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6", + "dom" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../../", + "typeRoots": [ + "../../" + ], + "types": [], + "paths": { + "history": ["history/v3"], + "history/*": ["history/v3/*"], + "redux-auth-wrapper": ["redux-auth-wrapper/v1"] + }, + "noEmit": true, + "forceConsistentCasingInFileNames": true, + "jsx": "react" + }, + "files": [ + "index.d.ts", + "redux-auth-wrapper-tests.tsx" + ] +} diff --git a/types/redux-auth-wrapper/v1/tslint.json b/types/redux-auth-wrapper/v1/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/redux-auth-wrapper/v1/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }