From 1983e2ec071c91017d3ac991980e52f6b6db9b67 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Mon, 21 Oct 2019 23:13:40 -0400 Subject: [PATCH] tyding up --- package.json | 7 ++++++- src/buildActions/index.ts | 33 ++++++++++++-------------------- src/buildInitial/index.test-d.ts | 6 ++++++ src/index.ts | 28 +-------------------------- src/types.ts | 16 ++++++++++++++++ src/updux.ts | 4 ++-- tsconfig.json | 12 ++++++------ 7 files changed, 49 insertions(+), 57 deletions(-) create mode 100644 src/buildInitial/index.test-d.ts diff --git a/package.json b/package.json index aa78feb..3fd450b 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,13 @@ { "name": "updux", + "types": "dist", "version": "0.0.1", "main": "index.js", "license": "MIT", "dependencies": { "@types/jest": "^24.0.19", "@types/lodash": "^4.14.144", + "dtslint": "^0.9.8", "lodash": "^4.17.15", "redux": "^4.0.4", "ts-jest": "^24.1.0", @@ -19,5 +21,8 @@ "babel-jest": "^24.9.0", "jest": "^24.9.0", "updeep": "^1.2.0" - } + }, + "tsd": { + "directory": "test-d" + } } diff --git a/src/buildActions/index.ts b/src/buildActions/index.ts index f7a39af..de2d987 100644 --- a/src/buildActions/index.ts +++ b/src/buildActions/index.ts @@ -1,30 +1,21 @@ import fp from 'lodash/fp'; +import { ActionEffects, ActionCreator, ActionCreators, ActionMutations } from '../types'; -function actionFor(type) { - return (payload = null, meta = null) => { - return fp.pickBy(v => v !== null)({type, payload, meta}); - }; +function actionFor(type: string) { + return ( (payload = undefined, meta = undefined) => + fp.pickBy(v => v !== null)({type, payload, meta}) + ) as ActionCreator; } export default function buildActions( - mutations = {}, - effects = {}, - subActions = {}, + mutations : ActionMutations = {}, + effects : ActionEffects = {}, + subActions : ActionCreators = {}, ) { - let actions = { ...subActions }; + return { ...subActions, + ...fp.fromPairs([ ...Object.keys(mutations), ...Object.keys(effects) ] + .map( type => [ type, actionFor(type) ])) + }; - Object.keys(mutations).forEach(type => { - if (!actions[type]) { - actions[type] = actionFor(type); - } - }); - - Object.keys(effects).forEach(type => { - if (!actions[type]) { - actions[type] = actionFor(type); - } - }); - - return actions; } diff --git a/src/buildInitial/index.test-d.ts b/src/buildInitial/index.test-d.ts new file mode 100644 index 0000000..39abd06 --- /dev/null +++ b/src/buildInitial/index.test-d.ts @@ -0,0 +1,6 @@ +// TypeScript Version: 3.4 + +import buildInitial from '.'; + +// $ExpectType any +const x = buildInitial(); diff --git a/src/index.ts b/src/index.ts index 15520d5..ffdb8d5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,32 +3,6 @@ import u from 'updeep'; import Updux from './updux'; - - - -function updux(config) { +export default function updux(config) { return new Updux(config); - // const actions = buildActions( - // config.mutations, - // config.effects, - // fp.flatten( ( config.subduxes||{}).map( ({ actions }) => actions ) ) - // ); - - // const initial = buildInitial(config); - - // const mutations = buildMutations(config.mutations,config.subduxes); - - - - // return { - // reducer, - // upreducer, - // middleware, - // createStore, - // actions: ( actions as any ), - // mutations, - // initial, - // }; } - -export default updux; diff --git a/src/types.ts b/src/types.ts index c8dfa36..dbf1eb2 100644 --- a/src/types.ts +++ b/src/types.ts @@ -5,3 +5,19 @@ export type Mutation = (payload: any, action: any) => (state:any) => any; export type Mutations = Dictionary; +export type ActionMutations = Dictionary; + +export type Effect = (api:any) => (next: Function) => (action: any) => any; + +export type ActionEffects = Dictionary; + +export type Action = { + type: T, + payload: P, + meta: M, +} + +export type ActionCreator = (payload?: P, meta?: M ) => Action; + +export type ActionCreators = Dictionary; + diff --git a/src/updux.ts b/src/updux.ts index 7e6c378..0b780bf 100644 --- a/src/updux.ts +++ b/src/updux.ts @@ -3,7 +3,7 @@ import buildActions from './buildActions'; import buildInitial from './buildInitial'; import buildMutations from './buildMutations'; -import { Dictionary, Mutation } from './types'; +import { Dictionary, Mutation, ActionCreators } from './types'; import buildCreateStore from './buildCreateStore'; import buildMiddleware from './buildMiddleware'; import buildUpreducer from './buildUpreducer'; @@ -18,7 +18,7 @@ type UpduxConfig = { }; export class Updux { - actions: any; + actions: ActionCreators; subduxes: Dictionary; diff --git a/tsconfig.json b/tsconfig.json index f2613cb..63ebd76 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,19 +4,19 @@ "incremental": true, /* Enable incremental compilation */ "target": "ES2019", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */ "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */ - "lib": [ "dom", "es2017" ], /* Specify library files to be included in the compilation. */ + "lib": [ "dom", "es2019" ], /* Specify library files to be included in the compilation. */ // "allowJs": true, /* Allow javascript files to be compiled. */ // "checkJs": true, /* Report errors in .js files. */ // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */ - // "declaration": true, /* Generates corresponding '.d.ts' file. */ - // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ - // "sourceMap": true, /* Generates corresponding '.map' file. */ + "declaration": true, /* Generates corresponding '.d.ts' file. */ + "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ + "sourceMap": true, /* Generates corresponding '.map' file. */ // "outFile": "./", /* Concatenate and emit output to single file. */ "outDir": "./dist", /* Redirect output structure to the directory. */ - // "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ + "rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ // "composite": true, /* Enable project compilation */ // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */ - // "removeComments": true, /* Do not emit comments to output. */ + "removeComments": true, /* Do not emit comments to output. */ // "noEmit": true, /* Do not emit outputs. */ // "importHelpers": true, /* Import emit helpers from 'tslib'. */ // "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */