Fix type errors

This commit is contained in:
kazukinagata 2020-07-26 15:02:52 +09:00
parent 5f49d5b7b1
commit e35e3398e8
7 changed files with 3899 additions and 37 deletions

12
index.d.ts vendored
View File

@ -1,12 +1,12 @@
import { Reducer } from 'redux' import { Reducer } from 'redux'
import {GenerateAuthActions} from './src/actions'
import {GenerateRequireSignInWrapper} from './src/generate-require-signin-wrapper'
import { import {
ActionsGeneratorExport, ReduxState,
GenerateRequireSignInWrapperConfig,
RequireSignInWrapper,
} from './src/types' } from './src/types'
export const reduxTokenAuthReducer: Reducer<{}> export const reduxTokenAuthReducer: Reducer<ReduxState>
export const generateAuthActions: ActionsGeneratorExport export const generateAuthActions: GenerateAuthActions
export const generateRequireSignInWrapper: (config: GenerateRequireSignInWrapperConfig) => RequireSignInWrapper export const generateRequireSignInWrapper: GenerateRequireSignInWrapper

View File

@ -50,6 +50,7 @@
"axios": "^0.19.2", "axios": "^0.19.2",
"react": "^16.13.1", "react": "^16.13.1",
"react-redux": "^7.2.0", "react-redux": "^7.2.0",
"redux": "^4.0.5" "redux": "^4.0.5",
"redux-thunk": "^2.3.0"
} }
} }

View File

@ -1,6 +1,5 @@
import axios from 'axios' import axios from 'axios'
import { import {
Dispatch,
Store, Store,
} from 'redux' } from 'redux'
import { import {
@ -11,7 +10,6 @@ import {
UserRegistrationDetails, UserRegistrationDetails,
UserSignInCredentials, UserSignInCredentials,
UserSignOutCredentials, UserSignOutCredentials,
ActionsExport,
REGISTRATION_REQUEST_SENT, REGISTRATION_REQUEST_SENT,
REGISTRATION_REQUEST_SUCCEEDED, REGISTRATION_REQUEST_SUCCEEDED,
REGISTRATION_REQUEST_FAILED, REGISTRATION_REQUEST_FAILED,
@ -38,6 +36,7 @@ import {
SignOutRequestSucceededAction, SignOutRequestSucceededAction,
SignOutRequestFailedAction, SignOutRequestFailedAction,
SetHasVerificationBeenAttemptedAction, SetHasVerificationBeenAttemptedAction,
AppThunk,
} from './types' } from './types'
import AsyncLocalStorage from './AsyncLocalStorage' import AsyncLocalStorage from './AsyncLocalStorage'
import { import {
@ -122,7 +121,7 @@ export const setHasVerificationBeenAttempted = (
// Async Redux Thunk actions: // Async Redux Thunk actions:
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
const generateAuthActions = (config: { [key: string]: any }): ActionsExport => { const generateAuthActions = (config: { [key: string]: any }) => {
const { const {
authUrl, authUrl,
storage, storage,
@ -134,7 +133,7 @@ const generateAuthActions = (config: { [key: string]: any }): ActionsExport => {
const registerUser = ( const registerUser = (
userRegistrationDetails: UserRegistrationDetails, userRegistrationDetails: UserRegistrationDetails,
) => async function (dispatch: Dispatch<{}>): Promise<void> { ): AppThunk => async function (dispatch) {
dispatch(registrationRequestSent()) dispatch(registrationRequestSent())
const { const {
email, email,
@ -168,7 +167,7 @@ const generateAuthActions = (config: { [key: string]: any }): ActionsExport => {
const verifyToken = ( const verifyToken = (
verificationParams: VerificationParams, verificationParams: VerificationParams,
) => async function (dispatch: Dispatch<{}>): Promise<void> { ): AppThunk => async function (dispatch) {
dispatch(verifyTokenRequestSent()) dispatch(verifyTokenRequestSent())
try { try {
const response = await axios({ const response = await axios({
@ -187,7 +186,7 @@ const generateAuthActions = (config: { [key: string]: any }): ActionsExport => {
const signInUser = ( const signInUser = (
userSignInCredentials: UserSignInCredentials, userSignInCredentials: UserSignInCredentials,
) => async function (dispatch: Dispatch<{}>): Promise<void> { ): AppThunk => async function (dispatch) {
dispatch(signInRequestSent()) dispatch(signInRequestSent())
const { const {
email, email,
@ -212,7 +211,7 @@ const generateAuthActions = (config: { [key: string]: any }): ActionsExport => {
} }
} }
const signOutUser = () => async function (dispatch: Dispatch<{}>): Promise<void> { const signOutUser = (): AppThunk => async function (dispatch) {
const userSignOutCredentials: UserSignOutCredentials = { const userSignOutCredentials: UserSignOutCredentials = {
'access-token': await Storage.getItem('access-token') as string, 'access-token': await Storage.getItem('access-token') as string,
client: await Storage.getItem('client') as string, client: await Storage.getItem('client') as string,
@ -257,3 +256,4 @@ const generateAuthActions = (config: { [key: string]: any }): ActionsExport => {
} }
export default generateAuthActions export default generateAuthActions
export type GenerateAuthActions = typeof generateAuthActions

View File

@ -1,31 +1,35 @@
import * as React from 'react' import * as React from 'react'
import { ComponentClass } from 'react' import { connect, ConnectedProps } from 'react-redux'
import { connect } from 'react-redux'
import { import {
GenerateRequireSignInWrapperConfig, GenerateRequireSignInWrapperConfig,
ReduxState, ReduxState,
RequireSignInWrapper,
} from './types' } from './types'
const generateRequireSignInWrapper = ( const generateRequireSignInWrapper = (
{ redirectPathIfNotSignedIn }: GenerateRequireSignInWrapperConfig { redirectPathIfNotSignedIn }: GenerateRequireSignInWrapperConfig
): RequireSignInWrapper => { ) => {
const requireSignInWrapper = (PageComponent: ComponentClass): ComponentClass => { const requireSignInWrapper = (PageComponent: React.ComponentClass | React.FunctionComponent) => {
interface WrapperProps {
readonly hasVerificationBeenAttempted: boolean const mapStateToProps = (state: ReduxState) => ({
readonly isSignedIn: boolean hasVerificationBeenAttempted: state.reduxTokenAuth.currentUser.hasVerificationBeenAttempted,
isSignedIn: state.reduxTokenAuth.currentUser.isSignedIn
})
const connector = connect(mapStateToProps)
type PropsFromRedux = ConnectedProps<typeof connector>
type Props = PropsFromRedux & {
readonly history: { readonly history: {
readonly replace: (path: string) => void readonly replace: (path: string) => void
} }
} }
class GatedPage extends React.Component<Props> {
class GatedPage extends React.Component<WrapperProps> { public componentDidUpdate() {
public componentWillReceiveProps(nextProps: WrapperProps): void {
const { const {
history, history,
hasVerificationBeenAttempted, hasVerificationBeenAttempted,
isSignedIn, isSignedIn,
} = nextProps } = this.props
if (hasVerificationBeenAttempted && !isSignedIn) { if (hasVerificationBeenAttempted && !isSignedIn) {
history.replace(redirectPathIfNotSignedIn) history.replace(redirectPathIfNotSignedIn)
} }
@ -44,11 +48,6 @@ const generateRequireSignInWrapper = (
} }
} }
const mapStateToProps = (state: ReduxState) => ({
hasVerificationBeenAttempted: state.reduxTokenAuth.currentUser.hasVerificationBeenAttempted,
isSignedIn: state.reduxTokenAuth.currentUser.isSignedIn
})
return connect( return connect(
mapStateToProps, mapStateToProps,
)(GatedPage) )(GatedPage)
@ -58,3 +57,4 @@ const generateRequireSignInWrapper = (
} }
export default generateRequireSignInWrapper export default generateRequireSignInWrapper
export type GenerateRequireSignInWrapper = typeof generateRequireSignInWrapper

View File

@ -1,10 +1,9 @@
import { import {
combineReducers, combineReducers,
Reducer,
} from 'redux' } from 'redux'
import currentUser from './current-user' import currentUser from './current-user'
const reduxTokenAuthReducer: Reducer<{}> = combineReducers({ const reduxTokenAuthReducer = combineReducers({
currentUser, currentUser,
}) })

View File

@ -1,8 +1,8 @@
import { ComponentClass } from 'react'
import { import {
Dispatch,
Store, Store,
Action
} from 'redux' } from 'redux'
import { ThunkAction } from 'redux-thunk'
export interface UserAttributes { export interface UserAttributes {
[key: string]: string | number | null [key: string]: string | number | null
@ -179,7 +179,15 @@ export type ReduxAction = RegistrationRequestSentAction
| SignOutRequestFailedAction | SignOutRequestFailedAction
| SetHasVerificationBeenAttemptedAction | SetHasVerificationBeenAttemptedAction
export type ReduxAsyncAction = (input?: any) => (dispatch: Dispatch<{}>) => Promise<void>
export type AppThunk<ReturnType = Promise<void>> = ThunkAction<
ReturnType,
ReduxState,
unknown,
Action<string>
>
export type ReduxAsyncAction = (input?: any) => AppThunk
export type VerifyCredentialsFunction = (store: Store<{}>) => void export type VerifyCredentialsFunction = (store: Store<{}>) => void
@ -201,7 +209,7 @@ export interface GenerateRequireSignInWrapperConfig {
readonly redirectPathIfNotSignedIn: string readonly redirectPathIfNotSignedIn: string
} }
export type RequireSignInWrapper = (PageComponent: ComponentClass) => ComponentClass // export type RequireSignInWrapper = (PageComponent: ComponentClass) => ComponentClass
export interface DeviceStorage { export interface DeviceStorage {
readonly getItem: (key: string) => Promise<any> readonly getItem: (key: string) => Promise<any>

3854
yarn.lock Normal file

File diff suppressed because it is too large Load Diff