# Coverage

100%
74
74
0

## /Users/jordan/code/nuclear/examples/rest-api-example/src/modules/rest-api/action-types.js

100%
2
2
0

Line | Hits | Source
--- | --- | ---
1 | 1 | var keyMirror = require('keymirror')
2 |  |
3 | 1 | module.exports = keyMirror({
4 |  | API_FETCH_SUCCESS: null,
5 |  | API_FETCH_START: null,
6 |  | API_FETCH_FAIL: null,
7 |  |
8 |  | API_SAVE_SUCCESS: null,
9 |  | API_SAVE_START: null,
10 |  | API_SAVE_FAIL: null,
11 |  |
12 |  | API_DELETE_SUCCESS: null,
13 |  | API_DELETE_START: null,
14 |  | API_DELETE_FAIL: null,
15 |  | })
16 |  |

## /Users/jordan/code/nuclear/examples/rest-api-example/src/modules/rest-api/create-api-actions.js

100%
37
37
0

Line | Hits | Source
--- | --- | ---
1 | 1 | var Promise = require('es6-promise').Promise
2 | 1 | var Flux = require('../../flux')
3 | 1 | var actionTypes = require('./action-types')
4 |  |
5 |  | /**
6 |  | * @param {Object} model
7 |  | * @param {String} model.entity
8 |  | * @param {Function} model.save
9 |  | * @param {Function} model.fetch
10 |  | * @param {Function} model.fetchAll
11 |  | * @param {Function} model.delete
12 |  | * @return {Object}
13 |  | */
14 | 1 | module.exports = function(model) {
15 | 21 | var entity = model.entity;
16 | 21 | var apiActions = {}
17 |  |
18 | 21 | apiActions.fetch = function(params) {
19 | 6 | Flux.dispatch(actionTypes.API_FETCH_START, {
20 |  | model: model,
21 |  | method: 'fetch',
22 |  | params: params,
23 |  | })
24 | 6 | return model.fetch(params).then(
25 |  | onFetchSuccess.bind(null, model, params),
26 |  | onFetchFail.bind(null, model, params)
27 |  | )
28 |  | }
29 |  |
30 | 21 | apiActions.fetchAll = function(params) {
31 | 8 | Flux.dispatch(actionTypes.API_FETCH_START, {
32 |  | model: model,
33 |  | method: 'fetchAll',
34 |  | params: params,
35 |  | })
36 | 8 | return model.fetchAll(params).then(
37 |  | onFetchSuccess.bind(null, model, params),
38 |  | onFetchFail.bind(null, model, params)
39 |  | )
40 |  | }
41 |  |
42 | 21 | apiActions.save = function(params) {
43 | 5 | Flux.dispatch(actionTypes.API_SAVE_START, {
44 |  | params: params,
45 |  | })
46 | 5 | return model.save(params).then(
47 |  | onSaveSuccess.bind(null, model, params),
48 |  | onSaveFail.bind(null, model, params)
49 |  | )
50 |  | }
51 |  |
52 | 21 | apiActions['delete'] = function(params) {
53 | 4 | Flux.dispatch(actionTypes.API_DELETE_START, {
54 |  | params: params,
55 |  | })
56 | 4 | return model['delete'](params).then(
57 |  | onDeleteSuccess.bind(null, model, params),
58 |  | onDeleteFail.bind(null, model, params)
59 |  | )
60 |  | }
61 |  |
62 | 21 | return apiActions
63 |  | }
64 |  |
65 |  | /**
66 |  | * Handler for API fetch success, dispatches flux action to store the fetched
67 |  | * result in the api cache
68 |  | * @param {Model} model
69 |  | * @param {*} params used to call the `model.fetch(params)`
70 |  | * @param {Object} result
71 |  | * @return {Object}
72 |  | */
73 | 1 | function onFetchSuccess(model, params, result) {
74 | 10 | Flux.dispatch(actionTypes.API_FETCH_SUCCESS, {
75 |  | model: model,
76 |  | params: params,
77 |  | result: result,
78 |  | })
79 | 10 | return result
80 |  | }
81 |  |
82 |  | /**
83 |  | * Handler for API fetch success, dispatches flux action to store the fetched
84 |  | * result in the api cache
85 |  | * @param {Model} model
86 |  | * @param {*} params used to call the `model.fetch(params)`
87 |  | * @param {*} reason
88 |  | * @return {Object}
89 |  | */
90 | 1 | function onFetchFail(model, params, reason) {
91 | 4 | Flux.dispatch(actionTypes.API_FETCH_FAIL, {
92 |  | model: model,
93 |  | params: params,
94 |  | reason: reason,
95 |  | })
96 | 4 | return Promise.reject(reason)
97 |  | }
98 |  |
99 |  | /**
100 |  | * Handler for API save success, dispatches flux action to update the store with the
101 |  | * saved instance
102 |  | * @param {Model} model
103 |  | * @param {*} params used to call the `model.save(params)`
104 |  | * @param {Object} result
105 |  | * @return {Object}
106 |  | */
107 | 1 | function onSaveSuccess(model, params, result) {
108 | 3 | Flux.dispatch(actionTypes.API_SAVE_SUCCESS, {
109 |  | model: model,
110 |  | params: params,
111 |  | result: result,
112 |  | })
113 | 3 | return result
114 |  | }
115 |  |
116 |  | /**
117 |  | * Handler for API save success, dispatches flux action to update the store with the
118 |  | * saved instance
119 |  | * @param {Model} model
120 |  | * @param {*} params used to call the `model.save(params)`
121 |  | * @param {*} reason
122 |  | * @return {Object}
123 |  | */
124 | 1 | function onSaveFail(model, params, reason) {
125 | 2 | Flux.dispatch(actionTypes.API_SAVE_FAIL, {
126 |  | model: model,
127 |  | params: params,
128 |  | reason: reason,
129 |  | })
130 | 2 | return Promise.reject(reason)
131 |  | }
132 |  |
133 |  | /**
134 |  | * Handler for API delete success, dispatches flux action to remove the instance from the stores
135 |  | * @param {Model} model
136 |  | * @param {*} params used to call the `model.delete(params)`
137 |  | * @param {Object} result
138 |  | * @return {Object}
139 |  | */
140 | 1 | function onDeleteSuccess(model, params, result) {
141 | 2 | Flux.dispatch(actionTypes.API_DELETE_SUCCESS, {
142 |  | model: model,
143 |  | params: params,
144 |  | result: result,
145 |  | })
146 | 2 | return result
147 |  | }
148 |  |
149 |  | /**
150 |  | * Handler for API delete fail
151 |  | * @param {Model} model
152 |  | * @param {*} params used to call the `model.delete(params)`
153 |  | * @param {Object} result
154 |  | * @return {Object}
155 |  | */
156 | 1 | function onDeleteFail(model, params, reason) {
157 | 2 | Flux.dispatch(actionTypes.API_DELETE_FAIL, {
158 |  | model: model,
159 |  | params: params,
160 |  | reason: reason,
161 |  | })
162 | 2 | return Promise.reject(reason)
163 |  | }
164 |  |
165 |  |

## /Users/jordan/code/nuclear/examples/rest-api-example/src/modules/rest-api/index.js

100%
12
12
0

Line | Hits | Source
--- | --- | ---
1 | 1 | var toImmutable = require('nuclear-js').toImmutable
2 | 1 | var Flux = require('../../flux')
3 |  |
4 |  | // register stores with Flux system
5 | 1 | Flux.registerStores({
6 |  | restApiCache: require('./stores/rest-api-cache-store'),
7 |  | })
8 |  |
9 | 1 | exports.createApiActions = require('./create-api-actions')
10 |  |
11 |  | /**
12 |  | * Creates a getter to the restApiCache store for a particular entity
13 |  | * This decouples the implementation details of the RestApi module's caching
14 |  | * to consumers of the cached data
15 |  | * @param {Model} model
16 |  | */
17 | 1 | exports.createEntityMapGetter = function(model) {
18 | 2 | return [
19 |  | ['restApiCache', model.entity],
20 |  | /**
21 |  | * @return {Immutable.Map}
22 |  | */
23 |  | function(entityMap) {
24 |  | // protect the entityMap here from being undefined, there are cases
25 |  | // where an entity type isn't loaded yet, so we need to always to
26 |  | // return an Immutable.Map for getters downstream
27 | 2 | if (!entityMap) {
28 | 1 | return toImmutable({})
29 |  | } else {
30 | 1 | return entityMap
31 |  | }
32 |  | }
33 |  | ]
34 |  | }
35 |  |
36 |  | /**
37 |  | * Creates a function that creates a getter that looks up the entity in the restApiCache by ID
38 |  | * @param {Model} model
39 |  | */
40 | 1 | exports.createByIdGetter = function(model) {
41 | 1 | return function(id) {
42 | 1 | return ['restApiCache', model.entity, id]
43 |  | }
44 |  | }
45 |  |

## /Users/jordan/code/nuclear/examples/rest-api-example/src/modules/rest-api/stores/rest-api-cache-store.js

100%
23
23
0

Line | Hits | Source
--- | --- | ---
1 |  | /**
2 |  | * Stores cached entities for the Rest API
3 |  | */
4 | 1 | var _ = require('lodash');
5 | 1 | var Nuclear = require('nuclear-js')
6 | 1 | var toImmutable = Nuclear.toImmutable
7 | 1 | var actionTypes = require('../action-types')
8 |  |
9 | 1 | module.exports = new Nuclear.Store({
10 |  | getInitialState: function() {
11 | 25 | return toImmutable({})
12 |  | },
13 |  |
14 |  | initialize: function() {
15 | 1 | this.on(actionTypes.API_FETCH_SUCCESS, loadData)
16 | 1 | this.on(actionTypes.API_SAVE_SUCCESS, loadData)
17 | 1 | this.on(actionTypes.API_DELETE_SUCCESS, removeData)
18 |  | },
19 |  | })
20 |  |
21 |  | /**
22 |  | * @param {Immutable.Map} state
23 |  | * @param {Object} payload
24 |  | * @param {Model} payload.model
25 |  | * @param {any} payload.params
26 |  | * @param {Object|Array} payload.result
27 |  | */
28 | 1 | function loadData(state, payload) {
29 | 15 | var entity = payload.model.entity
30 | 15 | var data = payload.result
31 |  |
32 | 15 | if (!data) {
33 |  | // no-op if no real data was returned
34 | 1 | return state
35 |  | }
36 |  |
37 | 14 | if (!_.isArray(data)) {
38 | 6 | data = [data]
39 |  | }
40 |  |
41 | 14 | return state.withMutations(function(state) {
42 | 14 | data.forEach(function(entry) {
43 | 24 | state.setIn([entity, entry.id], toImmutable(entry))
44 |  | })
45 |  | })
46 |  | }
47 |  |
48 |  | /**
49 |  | * @param {Immutable.Map} state
50 |  | * @param {Object} payload
51 |  | * @param {Model} payload.model
52 |  | * @param {any} payload.params
53 |  | * @param {Object|Array} payload.result
54 |  | */
55 | 1 | function removeData(state, payload) {
56 | 2 | var entity = payload.model.entity
57 |  | // we assume that params is the instance with an `id` property
58 | 2 | var id = payload.params.id
59 |  |
60 | 2 | return state.removeIn([entity, id]);
61 |  | }
62 |  |