File size: 8,069 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import warn from '@wordpress/warning';
import deterministicStringify from 'fast-json-stable-stringify';
import { get, merge } from 'lodash';
import { keyedReducer } from 'calypso/state/utils';

const noop = () => {};
const identity = ( data ) => data;

/**
 * Returns response data from an HTTP request success action if available
 * @param {Object} action may contain HTTP response data
 * @returns {*} response data if available
 */
export const getData = ( action ) => get( action, 'meta.dataLayer.data', undefined );

/**
 * Returns error data from an HTTP request failure action if available
 * @param {Object} action may contain HTTP response error data
 * @returns {*} error data if available
 */
export const getError = ( action ) => get( action, 'meta.dataLayer.error', undefined );

/**
 * Returns (response) headers data from an HTTP request action if available
 * @param   {Object}      action Request action for which to retrieve HTTP response headers
 * @returns {*} Headers data if available
 */
export const getHeaders = ( action ) => get( action, 'meta.dataLayer.headers', undefined );

/**
 * @typedef {Object} ProgressData
 * @property {number} loaded Number of bytes already transferred
 * @property {number} total  Total number of bytes to transfer
 */

/**
 * Returns progress data from an HTTP request progress action if available
 * @param  {Object} action          may contain HTTP progress data
 * @returns {ProgressData|undefined} Progress data if available
 */
export const getProgress = ( action ) => get( action, 'meta.dataLayer.progress', undefined );

/**
 * Returns stream record from an HTTP request action if available
 * @param {Object} action may contain stream record
 * @returns {*} response data if available
 */
export const getStreamRecord = ( action ) =>
	get( action, 'meta.dataLayer.streamRecord', undefined );

const getRequestStatus = ( action ) => {
	if ( undefined !== getError( action ) ) {
		return 'failure';
	}

	if ( undefined !== getData( action ) ) {
		return 'success';
	}

	return 'pending';
};

export const getRequestKey = ( fullAction ) => {
	const { meta, ...action } = fullAction;
	const requestKey = get( meta, 'dataLayer.requestKey' );

	return requestKey ? requestKey : deterministicStringify( action );
};

export const requestsReducerItem = (
	state = null,
	{ meta: { dataLayer: { lastUpdated, pendingSince, status } = {} } = {} }
) => {
	if ( status === undefined ) {
		return state;
	}
	return Object.assign(
		{ ...state },
		{ status },
		lastUpdated && { lastUpdated },
		pendingSince && { pendingSince }
	);
};

export const reducer = keyedReducer( 'meta.dataLayer.requestKey', requestsReducerItem );

/**
 * Tracks the state of network activity for a given request type
 *
 * When we issue _REQUEST type actions they usually create some
 * associated network activity by means of an HTTP request.
 * We may want to know what the status of those requests are, if
 * they have completed or if they have failed.
 *
 * This tracker stores the meta data for those requests which
 * can then be independently polled by React components which
 * need to know about those data requests.
 *
 * Note that this is meta data about remote data requests and
 * _not_ about network activity, which is why this is code is
 * here operating on the _REQUEST actions and not in the HTTP
 * pipeline as a processor on HTTP_REQUEST actions.
 * @param {Function} next next link in HTTP middleware chain
 * @returns {Function} middleware function to track requests
 */
export const trackRequests = ( next ) => ( store, action ) => {
	// progress events don't affect
	// any tracking meta at the moment
	if ( true !== get( action, 'meta.dataLayer.trackRequest' ) || getProgress( action ) ) {
		return next( store, action );
	}

	const requestKey = getRequestKey( action );
	const status = getRequestStatus( action );
	const dataLayer = Object.assign(
		{ requestKey, status },
		status === 'pending' ? { pendingSince: Date.now() } : { lastUpdated: Date.now() }
	);

	const dispatch = ( response ) => store.dispatch( merge( response, { meta: { dataLayer } } ) );

	next( { ...store, dispatch }, action );
};

/**
 * Dispatches to appropriate function based on HTTP request meta
 * @see state/data-layer/wpcom-http/actions#fetch creates HTTP requests
 *
 * When the WPCOM HTTP data layer handles requests it will add
 * response data and errors to a meta property on the given success
 * error, and progress handling actions.
 *
 * This function accepts several functions as the fetch, success, error and
 * progress handlers for actions and it will call the appropriate
 * one based on the stored meta.
 *
 * These handlers are action creators: based on the input data coming from the HTTP request,
 * it will return an action (or an action thunk) to be executed as a response to the given
 * HTTP event.
 *
 * If both error and response data is available this will call the
 * error handler in preference over the success handler, but the
 * response data will also still be available through the action meta.
 *
 * The functions should conform to the following type signatures:
 *   fetch  :: Action -> Action (action creator with one Action parameter)
 *   onSuccess  :: Action -> ResponseData -> Action (action creator with two params)
 *   onError    :: Action -> ErrorData -> Action
 *   onProgress :: Action -> ProgressData -> Action
 *   fromApi    :: ResponseData -> TransformedData throws TransformerError|SchemaError
 * @param {Function} middleware intercepts requests moving through the system
 * object - options object with named parameters:
 * function - options.fetch called if action lacks response meta; should create HTTP request
 * function - options.onSuccess called if the action meta includes response data
 * function - options.onError called if the action meta includes error data
 * function - options.onProgress called on progress events when uploading
 * function - options.fromApi maps between API data and Calypso data
 * @returns {(options: {fetch?: any; onSuccess?: any; onError?: any; onProgress?: any; fromApi?: any}) => any} action or action thunk to be executed in response to HTTP event
 */
export const requestDispatcher = ( middleware ) => ( options ) => {
	if ( ! options.fetch ) {
		warn( 'fetch handler is not defined: no request will ever be issued' );
	}

	if ( ! options.onSuccess ) {
		warn( 'onSuccess handler is not defined: response to the request is being ignored' );
	}

	if ( ! options.onError ) {
		warn( 'onError handler is not defined: error during the request is being ignored' );
	}

	return middleware( ( store, action ) => {
		// create the low-level action we want to dispatch
		const requestAction = createRequestAction( options, action );

		// dispatch the low level action (if any was created) and return the result
		if ( ! requestAction ) {
			return;
		}

		if ( Array.isArray( requestAction ) ) {
			return requestAction.map( store.dispatch );
		}

		return store.dispatch( requestAction );
	} );
};
export const dispatchRequest = requestDispatcher( trackRequests );

/*
 * Converts an application-level Calypso action that's handled by the data-layer middleware
 * into a low-level action. For example, HTTP request that's being initiated, or a response
 * action with a `meta.dataLayer` property.
 */
function createRequestAction( options, action ) {
	const {
		fetch = noop,
		onSuccess = noop,
		onError = noop,
		onProgress = noop,
		onStreamRecord = noop,
		fromApi = identity,
	} = options;

	const error = getError( action );
	if ( undefined !== error ) {
		return onError( action, error );
	}

	const data = getData( action );
	if ( undefined !== data ) {
		try {
			return onSuccess( action, fromApi( data ) );
		} catch ( err ) {
			return onError( action, err );
		}
	}

	const progress = getProgress( action );
	if ( undefined !== progress ) {
		return onProgress( action, progress );
	}

	const record = getStreamRecord( action );
	if ( undefined !== record ) {
		return onStreamRecord( action, record );
	}

	return fetch( action );
}