This commit is contained in:
21
node_modules/redux-thunk/LICENSE.md
generated
vendored
Normal file
21
node_modules/redux-thunk/LICENSE.md
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015-present Dan Abramov
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
406
node_modules/redux-thunk/README.md
generated
vendored
Normal file
406
node_modules/redux-thunk/README.md
generated
vendored
Normal file
@@ -0,0 +1,406 @@
|
||||
# Redux Thunk
|
||||
|
||||
Thunk [middleware](https://redux.js.org/tutorials/fundamentals/part-4-store#middleware) for Redux. It allows writing functions with logic inside that can interact with a Redux store's `dispatch` and `getState` methods.
|
||||
|
||||
For complete usage instructions and useful patterns, see the [Redux docs **Writing Logic with Thunks** page](https://redux.js.org/usage/writing-logic-thunks).
|
||||
|
||||

|
||||
[](https://www.npmjs.com/package/redux-thunk)
|
||||
[](https://www.npmjs.com/package/redux-thunk)
|
||||
|
||||
## Installation and Setup
|
||||
|
||||
### Redux Toolkit
|
||||
|
||||
If you're using [our official Redux Toolkit package](https://redux-toolkit.js.org) as recommended, there's nothing to install - RTK's `configureStore` API already adds the thunk middleware by default:
|
||||
|
||||
```js
|
||||
import { configureStore } from '@reduxjs/toolkit'
|
||||
|
||||
import todosReducer from './features/todos/todosSlice'
|
||||
import filtersReducer from './features/filters/filtersSlice'
|
||||
|
||||
const store = configureStore({
|
||||
reducer: {
|
||||
todos: todosReducer,
|
||||
filters: filtersReducer
|
||||
}
|
||||
})
|
||||
|
||||
// The thunk middleware was automatically added
|
||||
```
|
||||
|
||||
### Manual Setup
|
||||
|
||||
If you're using the basic Redux `createStore` API and need to set this up manually, first add the `redux-thunk` package:
|
||||
|
||||
```sh
|
||||
npm install redux-thunk
|
||||
|
||||
yarn add redux-thunk
|
||||
```
|
||||
|
||||
The thunk middleware is the default export.
|
||||
|
||||
<details>
|
||||
<summary><b>More Details: Importing the thunk middleware</b></summary>
|
||||
|
||||
If you're using ES modules:
|
||||
|
||||
```js
|
||||
import thunk from 'redux-thunk' // no changes here 😀
|
||||
```
|
||||
|
||||
If you use Redux Thunk 2.x in a CommonJS environment,
|
||||
[don’t forget to add `.default` to your import](https://github.com/reduxjs/redux-thunk/releases/tag/v2.0.0):
|
||||
|
||||
```diff
|
||||
- const thunk = require('redux-thunk')
|
||||
+ const thunk = require('redux-thunk').default
|
||||
```
|
||||
|
||||
Additionally, since 2.x, we also support a
|
||||
[UMD build](https://unpkg.com/redux-thunk/dist/redux-thunk.min.js) for use as a global script tag:
|
||||
|
||||
```js
|
||||
const ReduxThunk = window.ReduxThunk
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
Then, to enable Redux Thunk, use
|
||||
[`applyMiddleware()`](https://redux.js.org/api/applymiddleware):
|
||||
|
||||
```js
|
||||
import { createStore, applyMiddleware } from 'redux'
|
||||
import thunk from 'redux-thunk'
|
||||
import rootReducer from './reducers/index'
|
||||
|
||||
const store = createStore(rootReducer, applyMiddleware(thunk))
|
||||
```
|
||||
|
||||
### Injecting a Custom Argument
|
||||
|
||||
Since 2.1.0, Redux Thunk supports injecting a custom argument into the thunk middleware. This is typically useful for cases like using an API service layer that could be swapped out for a mock service in tests.
|
||||
|
||||
For Redux Toolkit, the `getDefaultMiddleware` callback inside of `configureStore` lets you pass in a custom `extraArgument`:
|
||||
|
||||
```js
|
||||
import { configureStore } from '@reduxjs/toolkit'
|
||||
import rootReducer from './reducer'
|
||||
import { myCustomApiService } from './api'
|
||||
|
||||
const store = configureStore({
|
||||
reducer: rootReducer,
|
||||
middleware: getDefaultMiddleware =>
|
||||
getDefaultMiddleware({
|
||||
thunk: {
|
||||
extraArgument: myCustomApiService
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
// later
|
||||
function fetchUser(id) {
|
||||
// The `extraArgument` is the third arg for thunk functions
|
||||
return (dispatch, getState, api) => {
|
||||
// you can use api here
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
If you need to pass in multiple values, combine them into a single object:
|
||||
|
||||
```js
|
||||
const store = configureStore({
|
||||
reducer: rootReducer,
|
||||
middleware: getDefaultMiddleware =>
|
||||
getDefaultMiddleware({
|
||||
thunk: {
|
||||
extraArgument: {
|
||||
api: myCustomApiService,
|
||||
otherValue: 42
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
// later
|
||||
function fetchUser(id) {
|
||||
return (dispatch, getState, { api, otherValue }) => {
|
||||
// you can use api and something else here
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
If you're setting up the store by hand, the default `thunk` export has an attached `thunk.withExtraArgument()` function that should be used to generate the correct thunk middleware:
|
||||
|
||||
```js
|
||||
const store = createStore(
|
||||
reducer,
|
||||
applyMiddleware(thunk.withExtraArgument(api))
|
||||
)
|
||||
```
|
||||
|
||||
## Why Do I Need This?
|
||||
|
||||
With a plain basic Redux store, you can only do simple synchronous updates by
|
||||
dispatching an action. Middleware extends the store's abilities, and lets you
|
||||
write async logic that interacts with the store.
|
||||
|
||||
Thunks are the recommended middleware for basic Redux side effects logic,
|
||||
including complex synchronous logic that needs access to the store, and simple
|
||||
async logic like AJAX requests.
|
||||
|
||||
For more details on why thunks are useful, see:
|
||||
|
||||
- **Redux docs: Writing Logic with Thunks**
|
||||
https://redux.js.org/usage/writing-logic-thunks
|
||||
The official usage guide page on thunks. Covers why they exist, how the thunk middleware works, and useful patterns for using thunks.
|
||||
|
||||
- **Stack Overflow: Dispatching Redux Actions with a Timeout**
|
||||
http://stackoverflow.com/questions/35411423/how-to-dispatch-a-redux-action-with-a-timeout/35415559#35415559
|
||||
Dan Abramov explains the basics of managing async behavior in Redux, walking
|
||||
through a progressive series of approaches (inline async calls, async action
|
||||
creators, thunk middleware).
|
||||
|
||||
- **Stack Overflow: Why do we need middleware for async flow in Redux?**
|
||||
http://stackoverflow.com/questions/34570758/why-do-we-need-middleware-for-async-flow-in-redux/34599594#34599594
|
||||
Dan Abramov gives reasons for using thunks and async middleware, and some
|
||||
useful patterns for using thunks.
|
||||
|
||||
- **What the heck is a "thunk"?**
|
||||
https://daveceddia.com/what-is-a-thunk/
|
||||
A quick explanation for what the word "thunk" means in general, and for Redux
|
||||
specifically.
|
||||
|
||||
- **Thunks in Redux: The Basics**
|
||||
https://medium.com/fullstack-academy/thunks-in-redux-the-basics-85e538a3fe60
|
||||
A detailed look at what thunks are, what they solve, and how to use them.
|
||||
|
||||
You may also want to read the
|
||||
**[Redux FAQ entry on choosing which async middleware to use](https://redux.js.org/faq/actions#what-async-middleware-should-i-use-how-do-you-decide-between-thunks-sagas-observables-or-something-else)**.
|
||||
|
||||
While the thunk middleware is not directly included with the Redux core library,
|
||||
it is used by default in our
|
||||
**[`@reduxjs/toolkit` package](https://github.com/reduxjs/redux-toolkit)**.
|
||||
|
||||
## Motivation
|
||||
|
||||
Redux Thunk [middleware](https://redux.js.org/tutorials/fundamentals/part-4-store#middleware)
|
||||
allows you to write action creators that return a function instead of an action.
|
||||
The thunk can be used to delay the dispatch of an action, or to dispatch only if
|
||||
a certain condition is met. The inner function receives the store methods
|
||||
`dispatch` and `getState` as parameters.
|
||||
|
||||
An action creator that returns a function to perform asynchronous dispatch:
|
||||
|
||||
```js
|
||||
const INCREMENT_COUNTER = 'INCREMENT_COUNTER'
|
||||
|
||||
function increment() {
|
||||
return {
|
||||
type: INCREMENT_COUNTER
|
||||
}
|
||||
}
|
||||
|
||||
function incrementAsync() {
|
||||
return dispatch => {
|
||||
setTimeout(() => {
|
||||
// Yay! Can invoke sync or async actions with `dispatch`
|
||||
dispatch(increment())
|
||||
}, 1000)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
An action creator that returns a function to perform conditional dispatch:
|
||||
|
||||
```js
|
||||
function incrementIfOdd() {
|
||||
return (dispatch, getState) => {
|
||||
const { counter } = getState()
|
||||
|
||||
if (counter % 2 === 0) {
|
||||
return
|
||||
}
|
||||
|
||||
dispatch(increment())
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## What’s a thunk?!
|
||||
|
||||
A [thunk](https://en.wikipedia.org/wiki/Thunk) is a function that wraps an
|
||||
expression to delay its evaluation.
|
||||
|
||||
```js
|
||||
// calculation of 1 + 2 is immediate
|
||||
// x === 3
|
||||
let x = 1 + 2
|
||||
|
||||
// calculation of 1 + 2 is delayed
|
||||
// foo can be called later to perform the calculation
|
||||
// foo is a thunk!
|
||||
let foo = () => 1 + 2
|
||||
```
|
||||
|
||||
The term [originated](https://en.wikipedia.org/wiki/Thunk#cite_note-1) as a
|
||||
humorous past-tense version of "think".
|
||||
|
||||
## Composition
|
||||
|
||||
Any return value from the inner function will be available as the return value
|
||||
of `dispatch` itself. This is convenient for orchestrating an asynchronous
|
||||
control flow with thunk action creators dispatching each other and returning
|
||||
Promises to wait for each other’s completion:
|
||||
|
||||
```js
|
||||
import { createStore, applyMiddleware } from 'redux'
|
||||
import thunk from 'redux-thunk'
|
||||
import rootReducer from './reducers'
|
||||
|
||||
// Note: this API requires redux@>=3.1.0
|
||||
const store = createStore(rootReducer, applyMiddleware(thunk))
|
||||
|
||||
function fetchSecretSauce() {
|
||||
return fetch('https://www.google.com/search?q=secret+sauce')
|
||||
}
|
||||
|
||||
// These are the normal action creators you have seen so far.
|
||||
// The actions they return can be dispatched without any middleware.
|
||||
// However, they only express “facts” and not the “async flow”.
|
||||
|
||||
function makeASandwich(forPerson, secretSauce) {
|
||||
return {
|
||||
type: 'MAKE_SANDWICH',
|
||||
forPerson,
|
||||
secretSauce
|
||||
}
|
||||
}
|
||||
|
||||
function apologize(fromPerson, toPerson, error) {
|
||||
return {
|
||||
type: 'APOLOGIZE',
|
||||
fromPerson,
|
||||
toPerson,
|
||||
error
|
||||
}
|
||||
}
|
||||
|
||||
function withdrawMoney(amount) {
|
||||
return {
|
||||
type: 'WITHDRAW',
|
||||
amount
|
||||
}
|
||||
}
|
||||
|
||||
// Even without middleware, you can dispatch an action:
|
||||
store.dispatch(withdrawMoney(100))
|
||||
|
||||
// But what do you do when you need to start an asynchronous action,
|
||||
// such as an API call, or a router transition?
|
||||
|
||||
// Meet thunks.
|
||||
// A thunk in this context is a function that can be dispatched to perform async
|
||||
// activity and can dispatch actions and read state.
|
||||
// This is an action creator that returns a thunk:
|
||||
function makeASandwichWithSecretSauce(forPerson) {
|
||||
// We can invert control here by returning a function - the "thunk".
|
||||
// When this function is passed to `dispatch`, the thunk middleware will intercept it,
|
||||
// and call it with `dispatch` and `getState` as arguments.
|
||||
// This gives the thunk function the ability to run some logic, and still interact with the store.
|
||||
return function (dispatch) {
|
||||
return fetchSecretSauce().then(
|
||||
sauce => dispatch(makeASandwich(forPerson, sauce)),
|
||||
error => dispatch(apologize('The Sandwich Shop', forPerson, error))
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// Thunk middleware lets me dispatch thunk async actions
|
||||
// as if they were actions!
|
||||
|
||||
store.dispatch(makeASandwichWithSecretSauce('Me'))
|
||||
|
||||
// It even takes care to return the thunk’s return value
|
||||
// from the dispatch, so I can chain Promises as long as I return them.
|
||||
|
||||
store.dispatch(makeASandwichWithSecretSauce('My partner')).then(() => {
|
||||
console.log('Done!')
|
||||
})
|
||||
|
||||
// In fact I can write action creators that dispatch
|
||||
// actions and async actions from other action creators,
|
||||
// and I can build my control flow with Promises.
|
||||
|
||||
function makeSandwichesForEverybody() {
|
||||
return function (dispatch, getState) {
|
||||
if (!getState().sandwiches.isShopOpen) {
|
||||
// You don’t have to return Promises, but it’s a handy convention
|
||||
// so the caller can always call .then() on async dispatch result.
|
||||
|
||||
return Promise.resolve()
|
||||
}
|
||||
|
||||
// We can dispatch both plain object actions and other thunks,
|
||||
// which lets us compose the asynchronous actions in a single flow.
|
||||
|
||||
return dispatch(makeASandwichWithSecretSauce('My Grandma'))
|
||||
.then(() =>
|
||||
Promise.all([
|
||||
dispatch(makeASandwichWithSecretSauce('Me')),
|
||||
dispatch(makeASandwichWithSecretSauce('My wife'))
|
||||
])
|
||||
)
|
||||
.then(() => dispatch(makeASandwichWithSecretSauce('Our kids')))
|
||||
.then(() =>
|
||||
dispatch(
|
||||
getState().myMoney > 42
|
||||
? withdrawMoney(42)
|
||||
: apologize('Me', 'The Sandwich Shop')
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// This is very useful for server side rendering, because I can wait
|
||||
// until data is available, then synchronously render the app.
|
||||
|
||||
store
|
||||
.dispatch(makeSandwichesForEverybody())
|
||||
.then(() =>
|
||||
response.send(ReactDOMServer.renderToString(<MyApp store={store} />))
|
||||
)
|
||||
|
||||
// I can also dispatch a thunk async action from a component
|
||||
// any time its props change to load the missing data.
|
||||
|
||||
import { connect } from 'react-redux'
|
||||
import { Component } from 'react'
|
||||
|
||||
class SandwichShop extends Component {
|
||||
componentDidMount() {
|
||||
this.props.dispatch(makeASandwichWithSecretSauce(this.props.forPerson))
|
||||
}
|
||||
|
||||
componentDidUpdate(prevProps) {
|
||||
if (prevProps.forPerson !== this.props.forPerson) {
|
||||
this.props.dispatch(makeASandwichWithSecretSauce(this.props.forPerson))
|
||||
}
|
||||
}
|
||||
|
||||
render() {
|
||||
return <p>{this.props.sandwiches.join('mustard')}</p>
|
||||
}
|
||||
}
|
||||
|
||||
export default connect(state => ({
|
||||
sandwiches: state.sandwiches
|
||||
}))(SandwichShop)
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
||||
41
node_modules/redux-thunk/dist/redux-thunk.js
generated
vendored
Normal file
41
node_modules/redux-thunk/dist/redux-thunk.js
generated
vendored
Normal file
@@ -0,0 +1,41 @@
|
||||
(function (global, factory) {
|
||||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
|
||||
typeof define === 'function' && define.amd ? define(factory) :
|
||||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.ReduxThunk = factory());
|
||||
})(this, (function () { 'use strict';
|
||||
|
||||
/** A function that accepts a potential "extra argument" value to be injected later,
|
||||
* and returns an instance of the thunk middleware that uses that value
|
||||
*/
|
||||
function createThunkMiddleware(extraArgument) {
|
||||
// Standard Redux middleware definition pattern:
|
||||
// See: https://redux.js.org/tutorials/fundamentals/part-4-store#writing-custom-middleware
|
||||
var middleware = function middleware(_ref) {
|
||||
var dispatch = _ref.dispatch,
|
||||
getState = _ref.getState;
|
||||
return function (next) {
|
||||
return function (action) {
|
||||
// The thunk middleware looks for any functions that were passed to `store.dispatch`.
|
||||
// If this "action" is really a function, call it and return the result.
|
||||
if (typeof action === 'function') {
|
||||
// Inject the store's `dispatch` and `getState` methods, as well as any "extra arg"
|
||||
return action(dispatch, getState, extraArgument);
|
||||
} // Otherwise, pass the action down the middleware chain as usual
|
||||
|
||||
|
||||
return next(action);
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
return middleware;
|
||||
}
|
||||
|
||||
var thunk = createThunkMiddleware(); // Attach the factory function so users can create a customized version
|
||||
// with whatever "extra arg" they want to inject into their thunks
|
||||
|
||||
thunk.withExtraArgument = createThunkMiddleware;
|
||||
|
||||
return thunk;
|
||||
|
||||
}));
|
||||
1
node_modules/redux-thunk/dist/redux-thunk.min.js
generated
vendored
Normal file
1
node_modules/redux-thunk/dist/redux-thunk.min.js
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).ReduxThunk=t()}(this,(function(){"use strict";function e(e){return function(t){var n=t.dispatch,u=t.getState;return function(t){return function(o){return"function"==typeof o?o(n,u,e):t(o)}}}}var t=e();return t.withExtraArgument=e,t}));
|
||||
7
node_modules/redux-thunk/es/index.d.ts
generated
vendored
Normal file
7
node_modules/redux-thunk/es/index.d.ts
generated
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
import type { Action, AnyAction } from 'redux';
|
||||
import type { ThunkMiddleware } from './types';
|
||||
export type { ThunkAction, ThunkDispatch, ThunkActionDispatch, ThunkMiddleware } from './types';
|
||||
declare const thunk: ThunkMiddleware<any, AnyAction, undefined> & {
|
||||
withExtraArgument<ExtraThunkArg, State = any, BasicAction extends Action<any> = AnyAction>(extraArgument: ExtraThunkArg): ThunkMiddleware<State, BasicAction, ExtraThunkArg>;
|
||||
};
|
||||
export default thunk;
|
||||
32
node_modules/redux-thunk/es/index.js
generated
vendored
Normal file
32
node_modules/redux-thunk/es/index.js
generated
vendored
Normal file
@@ -0,0 +1,32 @@
|
||||
/** A function that accepts a potential "extra argument" value to be injected later,
|
||||
* and returns an instance of the thunk middleware that uses that value
|
||||
*/
|
||||
function createThunkMiddleware(extraArgument) {
|
||||
// Standard Redux middleware definition pattern:
|
||||
// See: https://redux.js.org/tutorials/fundamentals/part-4-store#writing-custom-middleware
|
||||
var middleware = function middleware(_ref) {
|
||||
var dispatch = _ref.dispatch,
|
||||
getState = _ref.getState;
|
||||
return function (next) {
|
||||
return function (action) {
|
||||
// The thunk middleware looks for any functions that were passed to `store.dispatch`.
|
||||
// If this "action" is really a function, call it and return the result.
|
||||
if (typeof action === 'function') {
|
||||
// Inject the store's `dispatch` and `getState` methods, as well as any "extra arg"
|
||||
return action(dispatch, getState, extraArgument);
|
||||
} // Otherwise, pass the action down the middleware chain as usual
|
||||
|
||||
|
||||
return next(action);
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
return middleware;
|
||||
}
|
||||
|
||||
var thunk = createThunkMiddleware(); // Attach the factory function so users can create a customized version
|
||||
// with whatever "extra arg" they want to inject into their thunks
|
||||
|
||||
thunk.withExtraArgument = createThunkMiddleware;
|
||||
export default thunk;
|
||||
50
node_modules/redux-thunk/es/types.d.ts
generated
vendored
Normal file
50
node_modules/redux-thunk/es/types.d.ts
generated
vendored
Normal file
@@ -0,0 +1,50 @@
|
||||
import { Action, AnyAction, Middleware } from 'redux';
|
||||
/**
|
||||
* The dispatch method as modified by React-Thunk; overloaded so that you can
|
||||
* dispatch:
|
||||
* - standard (object) actions: `dispatch()` returns the action itself
|
||||
* - thunk actions: `dispatch()` returns the thunk's return value
|
||||
*
|
||||
* @template State The redux state
|
||||
* @template ExtraThunkArg The extra argument passed to the inner function of
|
||||
* thunks (if specified when setting up the Thunk middleware)
|
||||
* @template BasicAction The (non-thunk) actions that can be dispatched.
|
||||
*/
|
||||
export interface ThunkDispatch<State, ExtraThunkArg, BasicAction extends Action> {
|
||||
/** Accepts a thunk function, runs it, and returns whatever the thunk itself returns */
|
||||
<ReturnType>(thunkAction: ThunkAction<ReturnType, State, ExtraThunkArg, BasicAction>): ReturnType;
|
||||
/** Accepts a standard action object, and returns that action object */
|
||||
<Action extends BasicAction>(action: Action): Action;
|
||||
/** A union of the other two overloads for TS inference purposes */
|
||||
<ReturnType, Action extends BasicAction>(action: Action | ThunkAction<ReturnType, State, ExtraThunkArg, BasicAction>): Action | ReturnType;
|
||||
}
|
||||
/**
|
||||
* A "thunk" action (a callback function that can be dispatched to the Redux
|
||||
* store.)
|
||||
*
|
||||
* Also known as the "thunk inner function", when used with the typical pattern
|
||||
* of an action creator function that returns a thunk action.
|
||||
*
|
||||
* @template ReturnType The return type of the thunk's inner function
|
||||
* @template State The redux state
|
||||
* @template ExtraThunkArg Optional extra argument passed to the inner function
|
||||
* (if specified when setting up the Thunk middleware)
|
||||
* @template BasicAction The (non-thunk) actions that can be dispatched.
|
||||
*/
|
||||
export declare type ThunkAction<ReturnType, State, ExtraThunkArg, BasicAction extends Action> = (dispatch: ThunkDispatch<State, ExtraThunkArg, BasicAction>, getState: () => State, extraArgument: ExtraThunkArg) => ReturnType;
|
||||
/**
|
||||
* A generic type that takes a thunk action creator and returns a function
|
||||
* signature which matches how it would appear after being processed using
|
||||
* bindActionCreators(): a function that takes the arguments of the outer
|
||||
* function, and returns the return type of the inner "thunk" function.
|
||||
*
|
||||
* @template ActionCreator Thunk action creator to be wrapped
|
||||
*/
|
||||
export declare type ThunkActionDispatch<ActionCreator extends (...args: any[]) => ThunkAction<any, any, any, any>> = (...args: Parameters<ActionCreator>) => ReturnType<ReturnType<ActionCreator>>;
|
||||
/**
|
||||
* @template State The redux state
|
||||
* @template BasicAction The (non-thunk) actions that can be dispatched
|
||||
* @template ExtraThunkArg An optional extra argument to pass to a thunk's
|
||||
* inner function. (Only used if you call `thunk.withExtraArgument()`)
|
||||
*/
|
||||
export declare type ThunkMiddleware<State = any, BasicAction extends Action = AnyAction, ExtraThunkArg = undefined> = Middleware<ThunkDispatch<State, ExtraThunkArg, BasicAction>, State, ThunkDispatch<State, ExtraThunkArg, BasicAction>>;
|
||||
42
node_modules/redux-thunk/extend-redux.d.ts
generated
vendored
Normal file
42
node_modules/redux-thunk/extend-redux.d.ts
generated
vendored
Normal file
@@ -0,0 +1,42 @@
|
||||
import { ThunkAction } from './src/index'
|
||||
|
||||
/**
|
||||
* Globally alter the Redux `bindActionCreators` and `Dispatch` types to assume
|
||||
* that the thunk middleware always exists, for ease of use.
|
||||
* This is kept as a separate file that may be optionally imported, to
|
||||
* avoid polluting the default types in case the thunk middleware is _not_
|
||||
* actually being used.
|
||||
*
|
||||
* To add these types to your app:
|
||||
* import 'redux-thunk/extend-redux'
|
||||
*/
|
||||
declare module 'redux' {
|
||||
/**
|
||||
* Overload for bindActionCreators redux function, returns expects responses
|
||||
* from thunk actions
|
||||
*/
|
||||
function bindActionCreators<
|
||||
ActionCreators extends ActionCreatorsMapObject<any>
|
||||
>(
|
||||
actionCreators: ActionCreators,
|
||||
dispatch: Dispatch
|
||||
): {
|
||||
[ActionCreatorName in keyof ActionCreators]: ReturnType<
|
||||
ActionCreators[ActionCreatorName]
|
||||
> extends ThunkAction<any, any, any, any>
|
||||
? (
|
||||
...args: Parameters<ActionCreators[ActionCreatorName]>
|
||||
) => ReturnType<ReturnType<ActionCreators[ActionCreatorName]>>
|
||||
: ActionCreators[ActionCreatorName]
|
||||
}
|
||||
|
||||
/*
|
||||
* Overload to add thunk support to Redux's dispatch() function.
|
||||
* Useful for react-redux or any other library which could use this type.
|
||||
*/
|
||||
export interface Dispatch<A extends Action = AnyAction> {
|
||||
<ReturnType = any, State = any, ExtraThunkArg = any>(
|
||||
thunkAction: ThunkAction<ReturnType, State, ExtraThunkArg, A>
|
||||
): ReturnType
|
||||
}
|
||||
}
|
||||
40
node_modules/redux-thunk/lib/index.js
generated
vendored
Normal file
40
node_modules/redux-thunk/lib/index.js
generated
vendored
Normal file
@@ -0,0 +1,40 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = void 0;
|
||||
|
||||
/** A function that accepts a potential "extra argument" value to be injected later,
|
||||
* and returns an instance of the thunk middleware that uses that value
|
||||
*/
|
||||
function createThunkMiddleware(extraArgument) {
|
||||
// Standard Redux middleware definition pattern:
|
||||
// See: https://redux.js.org/tutorials/fundamentals/part-4-store#writing-custom-middleware
|
||||
var middleware = function middleware(_ref) {
|
||||
var dispatch = _ref.dispatch,
|
||||
getState = _ref.getState;
|
||||
return function (next) {
|
||||
return function (action) {
|
||||
// The thunk middleware looks for any functions that were passed to `store.dispatch`.
|
||||
// If this "action" is really a function, call it and return the result.
|
||||
if (typeof action === 'function') {
|
||||
// Inject the store's `dispatch` and `getState` methods, as well as any "extra arg"
|
||||
return action(dispatch, getState, extraArgument);
|
||||
} // Otherwise, pass the action down the middleware chain as usual
|
||||
|
||||
|
||||
return next(action);
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
return middleware;
|
||||
}
|
||||
|
||||
var thunk = createThunkMiddleware(); // Attach the factory function so users can create a customized version
|
||||
// with whatever "extra arg" they want to inject into their thunks
|
||||
|
||||
thunk.withExtraArgument = createThunkMiddleware;
|
||||
var _default = thunk;
|
||||
exports.default = _default;
|
||||
76
node_modules/redux-thunk/package.json
generated
vendored
Normal file
76
node_modules/redux-thunk/package.json
generated
vendored
Normal file
@@ -0,0 +1,76 @@
|
||||
{
|
||||
"name": "redux-thunk",
|
||||
"version": "2.4.2",
|
||||
"license": "MIT",
|
||||
"description": "Thunk middleware for Redux.",
|
||||
"repository": "github:reduxjs/redux-thunk",
|
||||
"bugs": "https://github.com/reduxjs/redux-thunk/issues",
|
||||
"homepage": "https://github.com/reduxjs/redux-thunk",
|
||||
"keywords": [
|
||||
"redux",
|
||||
"thunk",
|
||||
"middleware",
|
||||
"redux-middleware",
|
||||
"flux"
|
||||
],
|
||||
"author": "Dan Abramov <dan.abramov@me.com>",
|
||||
"main": "lib/index.js",
|
||||
"module": "es/index.js",
|
||||
"types": "es/index.d.ts",
|
||||
"sideEffects": false,
|
||||
"files": [
|
||||
"lib",
|
||||
"es",
|
||||
"src",
|
||||
"dist",
|
||||
"extend-redux.d.ts"
|
||||
],
|
||||
"scripts": {
|
||||
"clean": "rimraf lib dist es",
|
||||
"prepublishOnly": "npm run clean && npm run lint && npm run test && npm run build",
|
||||
"format": "prettier --write {src,test,typescript_test}/**/*.{js,ts}",
|
||||
"format:check": "prettier --check {src,test,typescript_test}/**/*.{js,ts}",
|
||||
"lint": "eslint {src,test,typescript_test}/**/*.{js,ts}",
|
||||
"test": "jest",
|
||||
"test:cov": "jest --coverage",
|
||||
"test:typescript": "npm run test:typescript:main && npm run test:typescript:extended",
|
||||
"test:typescript:main": "tsc --noEmit -p typescript_test/tsconfig.json",
|
||||
"test:typescript:extended": "tsc --noEmit -p typescript_test/typescript_extended/tsconfig.json",
|
||||
"build:commonjs": "cross-env BABEL_ENV=commonjs babel src/*.ts --ignore src/types.ts --extensions .ts --out-dir lib ",
|
||||
"build:es": "babel src/*.ts --ignore src/types.ts --extensions .ts --out-dir es",
|
||||
"build:umd": "cross-env NODE_ENV=development rollup -c -o dist/redux-thunk.js",
|
||||
"build:umd:min": "cross-env NODE_ENV=production rollup -c -o dist/redux-thunk.min.js",
|
||||
"build:types": "tsc",
|
||||
"build": "rimraf dist lib es && npm run build:types && npm run build:commonjs && npm run build:es && npm run build:umd && npm run build:umd:min",
|
||||
"api-types": "api-extractor run --local"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"redux": "^4"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/cli": "^7.15.7",
|
||||
"@babel/core": "^7.15.8",
|
||||
"@babel/preset-env": "^7.15.8",
|
||||
"@babel/preset-typescript": "^7.15.0",
|
||||
"@babel/register": "^7.15.3",
|
||||
"@microsoft/api-extractor": "^7.18.16",
|
||||
"@rollup/plugin-babel": "^5.3.0",
|
||||
"@rollup/plugin-commonjs": "^21.0.1",
|
||||
"@rollup/plugin-node-resolve": "^13.0.6",
|
||||
"@rollup/plugin-replace": "^3.0.0",
|
||||
"@types/jest": "^27.0.2",
|
||||
"@typescript-eslint/eslint-plugin": "^5.1.0",
|
||||
"@typescript-eslint/parser": "^5.1.0",
|
||||
"cross-env": "^7.0.3",
|
||||
"eslint": "^7.32.0",
|
||||
"eslint-config-prettier": "^8.3.0",
|
||||
"jest": "^27.3.1",
|
||||
"prettier": "^2.4.1",
|
||||
"redux": "^4",
|
||||
"rimraf": "^3.0.2",
|
||||
"rollup": "^2.58.1",
|
||||
"rollup-plugin-terser": "^7.0.2",
|
||||
"ts-jest": "27.0.7",
|
||||
"typescript": "^4.4"
|
||||
}
|
||||
}
|
||||
53
node_modules/redux-thunk/src/index.ts
generated
vendored
Normal file
53
node_modules/redux-thunk/src/index.ts
generated
vendored
Normal file
@@ -0,0 +1,53 @@
|
||||
import type { Action, AnyAction } from 'redux'
|
||||
|
||||
import type { ThunkMiddleware } from './types'
|
||||
|
||||
export type {
|
||||
ThunkAction,
|
||||
ThunkDispatch,
|
||||
ThunkActionDispatch,
|
||||
ThunkMiddleware
|
||||
} from './types'
|
||||
|
||||
/** A function that accepts a potential "extra argument" value to be injected later,
|
||||
* and returns an instance of the thunk middleware that uses that value
|
||||
*/
|
||||
function createThunkMiddleware<
|
||||
State = any,
|
||||
BasicAction extends Action = AnyAction,
|
||||
ExtraThunkArg = undefined
|
||||
>(extraArgument?: ExtraThunkArg) {
|
||||
// Standard Redux middleware definition pattern:
|
||||
// See: https://redux.js.org/tutorials/fundamentals/part-4-store#writing-custom-middleware
|
||||
const middleware: ThunkMiddleware<State, BasicAction, ExtraThunkArg> =
|
||||
({ dispatch, getState }) =>
|
||||
next =>
|
||||
action => {
|
||||
// The thunk middleware looks for any functions that were passed to `store.dispatch`.
|
||||
// If this "action" is really a function, call it and return the result.
|
||||
if (typeof action === 'function') {
|
||||
// Inject the store's `dispatch` and `getState` methods, as well as any "extra arg"
|
||||
return action(dispatch, getState, extraArgument)
|
||||
}
|
||||
|
||||
// Otherwise, pass the action down the middleware chain as usual
|
||||
return next(action)
|
||||
}
|
||||
return middleware
|
||||
}
|
||||
|
||||
const thunk = createThunkMiddleware() as ThunkMiddleware & {
|
||||
withExtraArgument<
|
||||
ExtraThunkArg,
|
||||
State = any,
|
||||
BasicAction extends Action = AnyAction
|
||||
>(
|
||||
extraArgument: ExtraThunkArg
|
||||
): ThunkMiddleware<State, BasicAction, ExtraThunkArg>
|
||||
}
|
||||
|
||||
// Attach the factory function so users can create a customized version
|
||||
// with whatever "extra arg" they want to inject into their thunks
|
||||
thunk.withExtraArgument = createThunkMiddleware
|
||||
|
||||
export default thunk
|
||||
91
node_modules/redux-thunk/src/types.ts
generated
vendored
Normal file
91
node_modules/redux-thunk/src/types.ts
generated
vendored
Normal file
@@ -0,0 +1,91 @@
|
||||
import { Action, AnyAction, Middleware } from 'redux'
|
||||
|
||||
/**
|
||||
* The dispatch method as modified by React-Thunk; overloaded so that you can
|
||||
* dispatch:
|
||||
* - standard (object) actions: `dispatch()` returns the action itself
|
||||
* - thunk actions: `dispatch()` returns the thunk's return value
|
||||
*
|
||||
* @template State The redux state
|
||||
* @template ExtraThunkArg The extra argument passed to the inner function of
|
||||
* thunks (if specified when setting up the Thunk middleware)
|
||||
* @template BasicAction The (non-thunk) actions that can be dispatched.
|
||||
*/
|
||||
export interface ThunkDispatch<
|
||||
State,
|
||||
ExtraThunkArg,
|
||||
BasicAction extends Action
|
||||
> {
|
||||
// When the thunk middleware is added, `store.dispatch` now has three overloads (NOTE: the order here matters for correct behavior and is very fragile - do not reorder these!):
|
||||
|
||||
// 1) The specific thunk function overload
|
||||
/** Accepts a thunk function, runs it, and returns whatever the thunk itself returns */
|
||||
<ReturnType>(
|
||||
thunkAction: ThunkAction<ReturnType, State, ExtraThunkArg, BasicAction>
|
||||
): ReturnType
|
||||
|
||||
// 2) The base overload.
|
||||
/** Accepts a standard action object, and returns that action object */
|
||||
<Action extends BasicAction>(action: Action): Action
|
||||
|
||||
// 3) A union of the other two overloads. This overload exists to work around a problem
|
||||
// with TS inference ( see https://github.com/microsoft/TypeScript/issues/14107 )
|
||||
/** A union of the other two overloads for TS inference purposes */
|
||||
<ReturnType, Action extends BasicAction>(
|
||||
action: Action | ThunkAction<ReturnType, State, ExtraThunkArg, BasicAction>
|
||||
): Action | ReturnType
|
||||
}
|
||||
|
||||
/**
|
||||
* A "thunk" action (a callback function that can be dispatched to the Redux
|
||||
* store.)
|
||||
*
|
||||
* Also known as the "thunk inner function", when used with the typical pattern
|
||||
* of an action creator function that returns a thunk action.
|
||||
*
|
||||
* @template ReturnType The return type of the thunk's inner function
|
||||
* @template State The redux state
|
||||
* @template ExtraThunkArg Optional extra argument passed to the inner function
|
||||
* (if specified when setting up the Thunk middleware)
|
||||
* @template BasicAction The (non-thunk) actions that can be dispatched.
|
||||
*/
|
||||
export type ThunkAction<
|
||||
ReturnType,
|
||||
State,
|
||||
ExtraThunkArg,
|
||||
BasicAction extends Action
|
||||
> = (
|
||||
dispatch: ThunkDispatch<State, ExtraThunkArg, BasicAction>,
|
||||
getState: () => State,
|
||||
extraArgument: ExtraThunkArg
|
||||
) => ReturnType
|
||||
|
||||
/**
|
||||
* A generic type that takes a thunk action creator and returns a function
|
||||
* signature which matches how it would appear after being processed using
|
||||
* bindActionCreators(): a function that takes the arguments of the outer
|
||||
* function, and returns the return type of the inner "thunk" function.
|
||||
*
|
||||
* @template ActionCreator Thunk action creator to be wrapped
|
||||
*/
|
||||
export type ThunkActionDispatch<
|
||||
ActionCreator extends (...args: any[]) => ThunkAction<any, any, any, any>
|
||||
> = (
|
||||
...args: Parameters<ActionCreator>
|
||||
) => ReturnType<ReturnType<ActionCreator>>
|
||||
|
||||
/**
|
||||
* @template State The redux state
|
||||
* @template BasicAction The (non-thunk) actions that can be dispatched
|
||||
* @template ExtraThunkArg An optional extra argument to pass to a thunk's
|
||||
* inner function. (Only used if you call `thunk.withExtraArgument()`)
|
||||
*/
|
||||
export type ThunkMiddleware<
|
||||
State = any,
|
||||
BasicAction extends Action = AnyAction,
|
||||
ExtraThunkArg = undefined
|
||||
> = Middleware<
|
||||
ThunkDispatch<State, ExtraThunkArg, BasicAction>,
|
||||
State,
|
||||
ThunkDispatch<State, ExtraThunkArg, BasicAction>
|
||||
>
|
||||
Reference in New Issue
Block a user