Skip to content

Commit

Permalink
Stateless endpoints and action type
Browse files Browse the repository at this point in the history
  • Loading branch information
dimailn committed May 14, 2021
1 parent 866a0ab commit bfc4799
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 7 deletions.
1 change: 0 additions & 1 deletion __tests__/actions/rest.test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ describe 'REST actions', ->
nextPage: 2

dispatch = (action) ->
console.log action
{meta: {fetch}} = action
expect(fetch.url).toBe 'http://www.somesite.somedomain/todos.json'
expect(fetch.params).toEqual {page: 2}
Expand Down
45 changes: 45 additions & 0 deletions __tests__/reducers/action.test.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
trivialRedux = {defaultStates} = require '../../src/index'

api = trivialRedux(
todos:
entry: 'http://www.somesite.somedomain/todos'
type: 'action'
)

{reducers, types} = api

describe 'action reducer', ->
test 'execute pending', ->
state = Object.assign({}, defaultStates.action)
state = reducers.todos(state, type: types.todos.execute.load)

expect(state.pending).toBe true

test 'execute success', ->
data = {
someData: 'Something',
another: []
}
state = Object.assign({}, defaultStates.action)
state = reducers.todos(state, type: types.todos.execute.success, payload: data)

expect(state.data).toEqual data
expect(state.pending).toBe false
expect(typeof state.lastExecutedAt).toBe 'number'

test 'execute failure', ->
error = 'error'
state = Object.assign({}, defaultStates.action)
state = reducers.todos(state, type: types.todos.execute.failure, payload: error)

expect(state.pending).toBe false
expect(state.data).toBe error

test 'some action', ->
data = {
someData: 'Something',
another: []
}
state = Object.assign({}, defaultStates.action, data: data)

expect(reducers.todos(state, type: 'SOME_ACTION_TYPE')).toEqual state
20 changes: 20 additions & 0 deletions __tests__/requests/action.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
trivialRedux = require '../../src/index'
actionTypesFor = require '../../src/action_types'
actionTypeFor = require '../../src/action_type'

api = trivialRedux(
todos:
entry:'http://www.somesite.somedomain/todos'
type: 'action'
)

describe 'Fetch actions', ->
test 'execute action', ->
action = {meta: {fetch}, types} = api.requests.todos.execute(test: 1)

expect(types).toEqual actionTypesFor('execute', 'todos')
expect(fetch.url).toBe 'http://www.somesite.somedomain/todos.json'
expect(fetch.params).toBeUndefined()
expect(fetch.data).toEqual(test: 1)
expect(Object.keys(fetch).length).toBe 3
expect(action.isRequest).toBe true
17 changes: 17 additions & 0 deletions __tests__/stateless.test.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
trivialRedux = require '../src/index'
actionTypesFor = require '../src/action_types'
actionTypeFor = require '../src/action_type'

api = trivialRedux(
todos:
entry:'http://www.somesite.somedomain/todos'
type: 'rest'
stateless: true
)

describe 'Stateless endpoint', ->

test 'api contains only requests', ->
expect(api.requests.todos).toBeDefined()
expect(api.actions.todos).toBeUndefined()
expect(api.reducers.todo).toBeUndefined()
15 changes: 15 additions & 0 deletions src/actions/action.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
actionTypesFor = require '../action_types'
actionTypeFor = require '../action_type'
urlFormat = require '../utils/url_format'

module.exports = (entity_name, endpoint, settings) ->
format = urlFormat(settings)

execute: (data, options = {}) ->
types: actionTypesFor('execute', entity_name)
meta:
fetch: Object.assign({}, {
url: format(endpoint)
method: 'POST'
data
}, options)
3 changes: 2 additions & 1 deletion src/actions/index.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports =
fetch: require './fetch'
rest: require './rest'
setter: require './setter'
setter: require './setter'
action: require './action'
6 changes: 4 additions & 2 deletions src/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ trivialRedux = (endpoints, settings = {}) ->

customType = customTypes[type]


if customType?
customType.reducer.defaultState = customType.initialState if customType.hasOwnProperty('initialState')

Expand All @@ -45,8 +46,6 @@ trivialRedux = (endpoints, settings = {}) ->

api.types[name] = createActionTypes(name, actions, asyncActions)



api.actions[name] = Object.assign(
{}
prepareActions(actions, api.types[name])
Expand All @@ -56,8 +55,11 @@ trivialRedux = (endpoints, settings = {}) ->
api.reducers[name] = createReducer(name, customType.reducer, endpoint, api.types)
else
Object.keys(components).forEach (component) ->
return if ['reducers', 'actions'].includes(component) && endpoint.stateless

api[component][name] = components[component](name, endpoint, settings, api, type)


plugins.forEach (plugin) -> plugin(name, endpoint, api)

api
Expand Down
21 changes: 21 additions & 0 deletions src/reducers/action.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
actionTypesFor = require '../action_types'
actionTypeFor = require '../action_type'
defaultState = require '../states/fetch'

createActionReducerFor = (entity_name, initialState) ->
(state = initialState, action) ->
switch action.type
when @types.execute.load
Object.assign({}, state, pending: true)
when @types.execute.success
lastExecutedAt: new Date().getTime(), data: action.payload, pending: false
when @types.execute.failure
Object.assign({}, state, data: action.payload, pending: false)
when @types.reset
Object.assign({}, state, initialState)
else
state

createActionReducerFor.defaultState = defaultState

module.exports = createActionReducerFor
3 changes: 2 additions & 1 deletion src/reducers/index.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports =
fetch: require './fetch'
rest: require './rest'
setter: require './setter'
setter: require './setter'
action: require './action'
4 changes: 2 additions & 2 deletions src/utils/type_from.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ reducers = require '../reducers'
DEFAULT_ENDPOINT_TYPE = 'rest'

module.exports = (endpoint, settings) ->
type =
type =
if typeof endpoint is 'object'
endpoint.type || settings.type || DEFAULT_ENDPOINT_TYPE
else
DEFAULT_ENDPOINT_TYPE

[type, !(actions[type]? && reducers[type]?)]
[type, !(actions[type]? || reducers[type]?)]

0 comments on commit bfc4799

Please sign in to comment.