11import Request from './request' ;
22import Result from '../modules/result' ;
3- import config from '../../../config' ;
3+
44/**
55 * @method addSpread
66 * @description method to add the spread.
@@ -175,21 +175,6 @@ export function resultWrapper(result) {
175175 return result ;
176176} ;
177177
178- // // spread the result object
179- // export function spreadResult(result) {
180- // let _results = [];
181- // if (result && Object.keys(result).length) {
182- // if (typeof result.entries !== 'undefined') _results.push(result.entries);
183- // if (typeof result.assets !== 'undefined') _results.push(result.assets);
184- // if (typeof result.content_type !== 'undefined' || typeof result.schema !== 'undefined') _results.push(result.content_type || result.schema);
185- // if (typeof result.count !== 'undefined') _results.push(result.count);
186- // if (typeof result.entry !== 'undefined') _results = result.entry;
187- // if (typeof result.asset !== 'undefined') _results = result.asset;
188- // if (typeof result.items !== 'undefined') _results.push(result);
189- // }
190- // return _results;
191- // };
192-
193178// spread the result object
194179export function spreadResult ( result ) {
195180 let _results = [ ] ;
@@ -233,7 +218,6 @@ export function sendRequest(queryObject, options) {
233218 }
234219
235220 let self = queryObject ;
236- let continueFlag = false ;
237221 let cachePolicy = ( typeof self . queryCachePolicy !== 'undefined' ) ? self . queryCachePolicy : self . cachePolicy ;
238222 let tojson = ( typeof self . tojson !== 'undefined' ) ? self . tojson : false ;
239223 let isSingle = ( self . entry_uid || self . singleEntry || self . asset_uid ) ? true : false ;
@@ -249,14 +233,29 @@ export function sendRequest(queryObject, options) {
249233 }
250234 delete queryObject . requestParams . body . query ;
251235 queryObject . requestParams . body = merge ( queryObject . requestParams . body , cloneQueryObj ) ;
252- }
253236
237+ if ( queryObject . live_preview && queryObject . live_preview . enable === true && queryObject . live_preview . hash && queryObject . live_preview . hash !== "init" ) {
238+ if ( queryObject . live_preview . content_type_uid === queryObject . content_type_uid ) {
239+ queryObject . requestParams . body = merge ( queryObject . requestParams . body , { live_preview : queryObject . live_preview . hash || "init" } ) ;
240+ cachePolicy = 2 ; // network else cache
241+ if ( queryObject . requestParams . body [ 'environment' ] ) {
242+ delete queryObject . requestParams . body [ 'environment' ] ;
243+ }
244+ if ( queryObject . requestParams . headers [ 'access_token' ] )
245+ delete queryObject . requestParams . headers [ 'access_token' ] ;
246+
247+ queryObject . requestParams . headers [ 'authorization' ] = queryObject . live_preview . management_token
248+ } else if ( queryObject . live_preview . hash ) {
249+ cachePolicy = 1 ; // cache then network
250+ }
251+ }
252+ }
254253
255254 let getCacheCallback = function ( ) {
256255 return function ( err , entries ) {
257256 return new Promise ( function ( resolve , reject ) {
258257 try {
259- if ( err ) throw err ;
258+ if ( err ) reject ( err ) ;
260259 if ( ! tojson ) entries = resultWrapper ( entries ) ;
261260 resolve ( spreadResult ( entries ) ) ;
262261 } catch ( e ) {
@@ -288,10 +287,10 @@ export function sendRequest(queryObject, options) {
288287 } else {
289288 if ( cachePolicy === 2 && self . provider !== null ) {
290289 self . provider . get ( hashQuery , getCacheCallback ( ) ) ;
290+ return
291291 } else {
292292 return reject ( { error_code : 141 , error_message : 'The requested entry doesn\'t exist.' } ) ;
293293 }
294- return ;
295294 }
296295 }
297296 else if ( data . items ) {
@@ -308,7 +307,7 @@ export function sendRequest(queryObject, options) {
308307 if ( cachePolicy !== - 1 && self . provider !== null ) {
309308 self . provider . set ( hashQuery , entries , function ( err ) {
310309 try {
311- if ( err ) throw err ;
310+ if ( err ) reject ( err ) ;
312311 if ( ! tojson ) entries = resultWrapper ( entries ) ;
313312 return resolve ( spreadResult ( entries ) ) ;
314313 } catch ( e ) {
@@ -362,7 +361,6 @@ export function sendRequest(queryObject, options) {
362361 }
363362
364363 } ) ;
365- break ;
366364 case 2 :
367365 case 0 :
368366 case undefined :
@@ -380,7 +378,6 @@ export function sendRequest(queryObject, options) {
380378 try {
381379 if ( err || ! _data ) {
382380 reject ( err ) ;
383- //reject(Error("It broke"));
384381 } else {
385382 if ( ! tojson ) _data = resultWrapper ( _data ) ;
386383 resolve ( spreadResult ( _data ) ) ;
@@ -393,14 +390,13 @@ export function sendRequest(queryObject, options) {
393390 } ) ;
394391
395392 return promise . then ( function ( ) {
396- return new Promise ( function ( resolve , reject ) {
393+ return new Promise ( function ( resolve , reject ) {
397394 callback ( true , resolve , reject ) ;
398395 } ) ;
399396 } ) . catch ( ( error ) => {
400- return new Promise ( function ( resolve , reject ) {
397+ return new Promise ( function ( resolve , reject ) {
401398 callback ( true , resolve , reject ) ;
402399 } ) ;
403- console . error ( error )
404400 } )
405401 }
406402} ;
0 commit comments