Skip to content

Commit

Permalink
fix(effects): fix ts error when attempting to define string values fo…
Browse files Browse the repository at this point in the history
…r effects config

const config = {effects: ['get']} // error
  • Loading branch information
larrybotha committed Jul 13, 2018
1 parent c0a9194 commit a2d0c37
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
14 changes: 11 additions & 3 deletions src/create-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,24 @@ import {
ICreateResourceConfig,
ICreateResourceReturn,
} from './types/create-resource';
import {Effect} from './types/effect-creator-factory';
import {Effects} from './types/effect-creator-factory';

const createResource: CreateResource = options => {
if (!options.name || !options.url) {
throw new Error('name and url is required for createResource options');
}

const config: ICreateResourceConfig = {
configureRequest: (effect: Effect) => ({}),
configureRequest: (effect: Effects) => ({}),
customEffectCreators: [],
effects: ['create', 'find', 'get', 'patch', 'remove', 'update'],
effects: [
Effects.create,
Effects.find,
Effects.get,
Effects.patch,
Effects.remove,
Effects.update,
],
provider: fetchProvider,
...options,
};
Expand All @@ -31,6 +38,7 @@ const createResource: CreateResource = options => {
const actions = getActions(actionTypes);
const streamCreator = createStreamCreator(actionTypes);
const effectCreators = config.effects
.filter(effect => !!Effects[effect])
.map(effect => createEffectCreator({actionTypes, actions, effect, config}))
.concat(config.customEffectCreators.map(effectCreator => effectCreator(actionTypes, actions)));

Expand Down
1 change: 0 additions & 1 deletion src/effect-creator-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {getUrl} from './utils';
import {IActionCreators} from './types/action-creators';
import {ICreateResourceConfig} from './types/create-resource';
import {
Effect,
ICreateEffectCreator,
IResourceResponseError,
ResourceResponse,
Expand Down
7 changes: 3 additions & 4 deletions src/types/create-resource.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import {IAction, IEffectCreator, IStreamCreator} from 'xstream-store';

import {IActionCreators} from './action-creators';
import {Effect} from './effect-creator-factory';
import {Effects} from './effect-creator-factory';
import {Provider} from './providers';

export type CreateEffectCreator = (actionTypes: any, actions: IActionCreators) => IEffectCreator;

export interface ICreateResourceConfig {
configureRequest?: (effect: Effect) => {[key: string]: any};
configureRequest?: (effect: Effects) => {[key: string]: any};
customEffectCreators?: CreateEffectCreator[];
effects?: Effect[];
effects?: Effects[];
name: string;
provider?: Provider;
url: string;
[key: string]: any;
}

export interface ICreateResourceReturn {
Expand Down
11 changes: 9 additions & 2 deletions src/types/effect-creator-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ import {IActionCreators} from './action-creators';
import {ICreateResourceConfig} from './create-resource';
import {IError, IResource} from './stream-creator-factory';

export type Effect = 'create' | 'find' | 'get' | 'patch' | 'remove' | 'update';
export enum Effects {
create = 'create',
find = 'find',
get = 'get',
patch = 'patch',
remove = 'remove',
update = 'update',
}

export interface IResourceResponseError {
error: IError;
Expand All @@ -15,5 +22,5 @@ export interface ICreateEffectCreator {
actions: IActionCreators;
actionTypes: {[key: string]: string};
config: ICreateResourceConfig;
effect: Effect;
effect: Effects;
}

0 comments on commit a2d0c37

Please sign in to comment.