Skip to content

Commit

Permalink
Fix up linting and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
markerikson committed Oct 24, 2021
1 parent c477613 commit cfd2844
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 99 deletions.
9 changes: 8 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": [
"prettier",
"plugin:@typescript-eslint/recommended"
],
"env": {
"node": true
Expand All @@ -19,6 +20,12 @@
"no-use-before-define": [2, "nofunc"],
"no-unused-expressions": 0,
"import/no-unresolved": 0,
"import/named": 0
"import/named": 0,
"@typescript-eslint/ban-ts-ignore": 0,
"@typescript-eslint/explicit-function-return-type": 0,
"@typescript-eslint/no-explicit-any": 0,
"@typescript-eslint/member-delimiter-style": 0,
"@typescript-eslint/no-use-before-define": 1,
"@typescript-eslint/camelcase": 0
}
}
7 changes: 7 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"semi": false,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "none",
"arrowParens": "avoid"
}
8 changes: 1 addition & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"scripts": {
"clean": "rimraf lib dist es",
"prepublishOnly": "npm run clean && npm run lint && npm run test && npm run build",
"lint": "eslint src test",
"lint": "eslint src/*.ts test/*.ts",
"test": "jest",
"test:cov": "jest --coverage",
"test:typescript": "npm run test:typescript:main && npm run test:typescript:extended",
Expand All @@ -43,12 +43,6 @@
"api-types": "api-extractor run --local",
"format": "prettier --write \"{src,test}/**/*.{js,ts}\" \"docs/**/*.md\""
},
"prettier": {
"arrowParens": "always",
"proseWrap": "always",
"singleQuote": true,
"trailingComma": "all"
},
"peerDependencies": {
"redux": "^4"
},
Expand Down
55 changes: 26 additions & 29 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import type {
Action,
AnyAction,
} from 'redux';
import type { Action, AnyAction } from 'redux'

import type {
ThunkMiddleware
} from './types'
import type { ThunkMiddleware } from './types'

export type {
ThunkAction,
Expand All @@ -15,32 +10,34 @@ export type {
} from './types'

function createThunkMiddleware<
TState = {},
TBasicAction extends Action = AnyAction,
TExtraThunkArg = undefined
TState = {},
TBasicAction extends Action = AnyAction,
TExtraThunkArg = undefined
>(extraArgument?: TExtraThunkArg) {
const middleware: ThunkMiddleware<TState, TBasicAction, TExtraThunkArg> = ({ dispatch, getState }) => (next) => (action) => {
if (typeof action === 'function') {
return action(dispatch, getState, extraArgument);
}
const middleware: ThunkMiddleware<TState, TBasicAction, TExtraThunkArg> =
({ dispatch, getState }) =>
next =>
action => {
if (typeof action === 'function') {
return action(dispatch, getState, extraArgument)
}

return next(action);
};
return next(action)
}
return middleware
}

type CreateThunkMiddleware = typeof createThunkMiddleware

const thunk = createThunkMiddleware()
// @ts-ignore
thunk.withExtraArgument = createThunkMiddleware;
// @ts-ignore
thunk.withExtraArgument = createThunkMiddleware

export default thunk as typeof thunk & ThunkMiddleware & {
withExtraArgument<
TExtraThunkArg,
TState = {},
TBasicAction extends Action<any> = AnyAction
>(
extraArgument: TExtraThunkArg,
): ThunkMiddleware<TState, TBasicAction, TExtraThunkArg>;
}
export default thunk as typeof thunk &
ThunkMiddleware & {
withExtraArgument<
TExtraThunkArg,
TState = {},
TBasicAction extends Action<any> = AnyAction
>(
extraArgument: TExtraThunkArg
): ThunkMiddleware<TState, TBasicAction, TExtraThunkArg>
}
24 changes: 10 additions & 14 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
Action,
AnyAction,
Middleware,
} from 'redux';
import { Action, AnyAction, Middleware } from 'redux'

/**
* The dispatch method as modified by React-Thunk; overloaded so that you can
Expand All @@ -21,15 +17,15 @@ export interface ThunkDispatch<
TBasicAction extends Action
> {
<TReturnType>(
thunkAction: ThunkAction<TReturnType, TState, TExtraThunkArg, TBasicAction>,
): TReturnType;
<A extends TBasicAction>(action: A): A;
thunkAction: ThunkAction<TReturnType, TState, TExtraThunkArg, TBasicAction>
): TReturnType
<A extends TBasicAction>(action: A): A
// This overload is the union of the two above (see TS issue #14107).
<TReturnType, TAction extends TBasicAction>(
action:
| TAction
| ThunkAction<TReturnType, TState, TExtraThunkArg, TBasicAction>,
): TAction | TReturnType;
| ThunkAction<TReturnType, TState, TExtraThunkArg, TBasicAction>
): TAction | TReturnType
}

/**
Expand All @@ -53,8 +49,8 @@ export type ThunkAction<
> = (
dispatch: ThunkDispatch<TState, TExtraThunkArg, TBasicAction>,
getState: () => TState,
extraArgument: TExtraThunkArg,
) => TReturnType;
extraArgument: TExtraThunkArg
) => TReturnType

/**
* A generic type that takes a thunk action creator and returns a function
Expand All @@ -68,7 +64,7 @@ export type ThunkActionDispatch<
TActionCreator extends (...args: any[]) => ThunkAction<any, any, any, any>
> = (
...args: Parameters<TActionCreator>
) => ReturnType<ReturnType<TActionCreator>>;
) => ReturnType<ReturnType<TActionCreator>>

/**
* @template TState The redux state
Expand All @@ -84,4 +80,4 @@ export type ThunkMiddleware<
ThunkDispatch<TState, TExtraThunkArg, TBasicAction>,
TState,
ThunkDispatch<TState, TExtraThunkArg, TBasicAction>
>;
>
94 changes: 47 additions & 47 deletions test/test.ts
Original file line number Diff line number Diff line change
@@ -1,106 +1,106 @@
import thunkMiddleware from '../src/index';
import thunkMiddleware from '../src/index'

describe('thunk middleware', () => {
const doDispatch = () => {};
const doGetState = () => 42;
const doDispatch = () => {}
const doGetState = () => 42
const nextHandler = thunkMiddleware({
dispatch: doDispatch,
getState: doGetState,
});
getState: doGetState
})

it('must return a function to handle next', () => {
expect(nextHandler).toBeInstanceOf(Function)
expect(nextHandler.length).toBe(1)
});
})

describe('handle next', () => {
it('must return a function to handle action', () => {
// @ts-ignore
const actionHandler = nextHandler();
const actionHandler = nextHandler()

expect(actionHandler).toBeInstanceOf(Function)
expect(actionHandler.length).toBe(1)
});
})

describe('handle action', () => {
it('must run the given action function with dispatch and getState', (done) => {
it('must run the given action function with dispatch and getState', done => {
// @ts-ignore
const actionHandler = nextHandler();
const actionHandler = nextHandler()

actionHandler((dispatch: any, getState: any) => {
expect(dispatch).toBe(doDispatch)
expect(getState).toBe(doGetState)
done();
});
});
done()
})
})

it('must pass action to next if not a function', (done) => {
const actionObj = {};
it('must pass action to next if not a function', done => {
const actionObj = {}

// @ts-ignore
const actionHandler = nextHandler((action) => {
const actionHandler = nextHandler(action => {
expect(action).toBe(actionObj)
done();
});
done()
})

actionHandler(actionObj);
});
actionHandler(actionObj)
})

it('must return the return value of next if not a function', () => {
const expected = 'redux';
const expected = 'redux'
// @ts-ignore
const actionHandler = nextHandler(() => expected);
const actionHandler = nextHandler(() => expected)

// @ts-ignore
const outcome = actionHandler();
const outcome = actionHandler()
expect(outcome).toBe(expected)
});
})

it('must return value as expected if a function', () => {
const expected = 'rocks';
const expected = 'rocks'
// @ts-ignore
const actionHandler = nextHandler();
const actionHandler = nextHandler()

const outcome = actionHandler(() => expected);
const outcome = actionHandler(() => expected)
expect(outcome).toBe(expected)
});
})

it('must be invoked synchronously if a function', () => {
// @ts-ignore
const actionHandler = nextHandler();
let mutated = 0;
const actionHandler = nextHandler()
let mutated = 0

// eslint-disable-next-line no-plusplus
actionHandler(() => mutated++);
actionHandler(() => mutated++)
expect(mutated).toBe(1)
});
});
});
})
})
})

describe('handle errors', () => {
it('must throw if argument is non-object', (done) => {
it('must throw if argument is non-object', done => {
try {
// @ts-expect-error
thunkMiddleware();
thunkMiddleware()
} catch (err) {
done();
done()
}
});
});
})
})

describe('withExtraArgument', () => {
it('must pass the third argument', (done) => {
const extraArg = { lol: true };
it('must pass the third argument', done => {
const extraArg = { lol: true }
// @ts-ignore
thunkMiddleware.withExtraArgument(extraArg)({
dispatch: doDispatch,
getState: doGetState,
getState: doGetState
})()((dispatch: any, getState: any, arg: any) => {
expect(dispatch).toBe(doDispatch)
expect(getState).toBe(doGetState)
expect(arg).toBe(extraArg)
done();
});
});
});
});
done()
})
})
})
})
3 changes: 2 additions & 1 deletion typescript_test/typescript.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import {
applyMiddleware,
bindActionCreators,
Expand Down Expand Up @@ -44,7 +45,7 @@ store.dispatch((dispatch) => {
// @ts-expect-error
store.dispatch({ type: 'BAZ' });
});

function testGetState(): ThunkResult<void> {
return (dispatch, getState) => {
const state = getState();
Expand Down

0 comments on commit cfd2844

Please sign in to comment.