|
| 1 | +/* |
| 2 | + * *** MIT LICENSE *** |
| 3 | + * ------------------------------------------------------------------------- |
| 4 | + * This code may be modified and distributed under the MIT license. |
| 5 | + * See the LICENSE file for details. |
| 6 | + * ------------------------------------------------------------------------- |
| 7 | + * |
| 8 | + * @summary Definitions for redux-logic |
| 9 | + * |
| 10 | + * @author Alvis HT Tang <[email protected]> |
| 11 | + * @license MIT |
| 12 | + * @copyright Copyright (c) 2018 - All Rights Reserved. |
| 13 | + * ------------------------------------------------------------------------- |
| 14 | + */ |
| 15 | + |
| 16 | +// |
| 17 | +// ACTION |
| 18 | +// |
| 19 | + |
| 20 | +/* * * * * * |
| 21 | + | The following definitions are bascially identical to | |
| 22 | + | flux-standard-action without an extra package. It also | |
| 23 | + | makes use of conditional type to make the type of | |
| 24 | + | payload and meta more accurate. | |
| 25 | + * * * * * */ |
| 26 | + |
| 27 | +/** Action as an agrument */ |
| 28 | +export type ArgumentAction< |
| 29 | + Type extends string = string, |
| 30 | + Payload extends object = undefined, |
| 31 | + Meta extends object = undefined |
| 32 | +> = ActionBasis<Type> & Partial<Action<string, object, object>>; |
| 33 | + |
| 34 | +/** all different types of Action */ |
| 35 | +export type Action< |
| 36 | + Type extends string = string, |
| 37 | + Payload extends object = undefined, |
| 38 | + Meta extends object = undefined |
| 39 | +> = |
| 40 | + | ErroneousAction<Type, Meta> |
| 41 | + | (StandardAction<Type, Payload, Meta> & { error?: false }); |
| 42 | + |
| 43 | +/** Action without any error */ |
| 44 | +export type StandardAction< |
| 45 | + Type extends string = string, |
| 46 | + Payload extends object = undefined, |
| 47 | + Meta extends object = undefined |
| 48 | +> = ActionBasis<Type> & PayloadBasis<Payload> & MetaBasis<Meta>; |
| 49 | + |
| 50 | +/** Action with an Error */ |
| 51 | +export type ErroneousAction< |
| 52 | + Type extends string = string, |
| 53 | + Meta extends object = undefined |
| 54 | +> = ActionBasis<Type> & PayloadBasis<Error> & MetaBasis<Meta> & { error: true }; |
| 55 | + |
| 56 | +/* ----- Auxiliary Types ----- */ |
| 57 | + |
| 58 | +/** the most basic action object */ |
| 59 | +export interface ActionBasis<Type extends string = string> { |
| 60 | + type: Type extends infer U ? U : string; |
| 61 | +} |
| 62 | + |
| 63 | +/** return an interface with payload only if it presents */ |
| 64 | +export type PayloadBasis< |
| 65 | + Payload extends object = undefined |
| 66 | +> = Payload extends undefined ? {} : { payload: Payload }; |
| 67 | + |
| 68 | +/** return an interface with meta only if it presents */ |
| 69 | +export type MetaBasis<Meta extends object = undefined> = Meta extends undefined |
| 70 | + ? {} |
| 71 | + : { meta: Meta }; |
| 72 | + |
| 73 | +// ---------------------------------------- // |
0 commit comments