@@ -60,8 +60,7 @@ var DEFAULT_API_OPTIONS = {
6060var DEFAULT_SERVICE_OPTIONS = {
6161 method : 'GET' ,
6262 domain : 'default' ,
63- prefix : 'default' ,
64- disableCache : false
63+ prefix : 'default'
6564} ;
6665var DEFAULT_CACHE_DRIVER = react_native_1 . AsyncStorage ;
6766var OfflineFirstAPI = ( function ( ) {
@@ -74,21 +73,21 @@ var OfflineFirstAPI = (function () {
7473 }
7574 OfflineFirstAPI . prototype . fetch = function ( service , options ) {
7675 return __awaiter ( this , void 0 , void 0 , function ( ) {
77- var serviceDefinition , fullPath , middlewares , fetchOptions , fetchHeaders , shouldUseCache , requestId , expiration , _sha , expirationDelay , cachedData , parsedResponseData , res , err_1 ;
78- return __generator ( this , function ( _a ) {
79- switch ( _a . label ) {
76+ var serviceDefinition , fullPath , middlewares , fetchOptions , fetchHeaders , shouldUseCache , requestId , expiration , _sha , expirationDelay , cachedData , parsedResponseData , res , _a , err_1 ;
77+ return __generator ( this , function ( _b ) {
78+ switch ( _b . label ) {
8079 case 0 :
8180 serviceDefinition = this . _APIServices [ service ] ;
8281 if ( ! serviceDefinition ) {
8382 throw new Error ( "Cannot fetch data from unregistered service '" + service + "'" ) ;
8483 }
8584 fullPath = this . _constructPath ( serviceDefinition , options ) ;
86- _a . label = 1 ;
85+ _b . label = 1 ;
8786 case 1 :
88- _a . trys . push ( [ 1 , 8 , , 9 ] ) ;
87+ _b . trys . push ( [ 1 , 10 , , 11 ] ) ;
8988 return [ 4 /*yield*/ , this . _applyMiddlewares ( serviceDefinition , fullPath , options ) ] ;
9089 case 2 :
91- middlewares = _a . sent ( ) ;
90+ middlewares = _b . sent ( ) ;
9291 fetchOptions = _merge ( middlewares , ( options && options . fetchOptions ) || { } , { method : serviceDefinition . method } , { headers : ( options && options . headers ) || { } } ) ;
9392 fetchHeaders = options && options . fetchHeaders ;
9493 shouldUseCache = this . _shouldUseCache ( serviceDefinition , options ) ;
@@ -101,7 +100,7 @@ var OfflineFirstAPI = (function () {
101100 expiration = Date . now ( ) + expirationDelay ;
102101 return [ 4 /*yield*/ , this . _getCachedData ( service , requestId , fullPath ) ] ;
103102 case 3 :
104- cachedData = _a . sent ( ) ;
103+ cachedData = _b . sent ( ) ;
105104 if ( cachedData . success && cachedData . fresh && shouldUseCache ) {
106105 this . _log ( "Using fresh cache for " + fullPath ) ;
107106 return [ 2 /*return*/ , cachedData . data ] ;
@@ -113,7 +112,7 @@ var OfflineFirstAPI = (function () {
113112 parsedResponseData = void 0 ;
114113 return [ 4 /*yield*/ , this . _fetch ( fullPath , fetchOptions ) ] ;
115114 case 4 :
116- res = _a . sent ( ) ;
115+ res = _b . sent ( ) ;
117116 // If the network request fails, return the cached data if it's valid, a throw an error
118117 if ( ! res . success ) {
119118 if ( cachedData . success && cachedData . data ) {
@@ -127,22 +126,29 @@ var OfflineFirstAPI = (function () {
127126 this . _log ( 'raw network response' , res ) ;
128127 if ( ! fetchHeaders ) return [ 3 /*break*/ , 5 ] ;
129128 parsedResponseData = res . data . headers && res . data . headers . map ? res . data . headers . map : { } ;
130- return [ 3 /*break*/ , 7 ] ;
131- case 5 : return [ 4 /*yield*/ , res . data . json ( ) ] ;
132- case 6 :
133- parsedResponseData = _a . sent ( ) ;
134- _a . label = 7 ;
129+ return [ 3 /*break*/ , 9 ] ;
130+ case 5 :
131+ if ( ! ( ( options && options . rawData ) || serviceDefinition . rawData ) ) return [ 3 /*break*/ , 6 ] ;
132+ _a = res . data ;
133+ return [ 3 /*break*/ , 8 ] ;
134+ case 6 : return [ 4 /*yield*/ , res . data . json ( ) ] ;
135135 case 7 :
136+ _a = _b . sent ( ) ;
137+ _b . label = 8 ;
138+ case 8 :
139+ parsedResponseData = _a ;
140+ _b . label = 9 ;
141+ case 9 :
136142 // Cache if it hasn't been disabled and if the network request has been successful
137143 if ( res . data . ok && shouldUseCache ) {
138144 this . _cache ( serviceDefinition , service , requestId , parsedResponseData , expiration ) ;
139145 }
140146 this . _log ( 'parsed network response' , parsedResponseData ) ;
141147 return [ 2 /*return*/ , parsedResponseData ] ;
142- case 8 :
143- err_1 = _a . sent ( ) ;
148+ case 10 :
149+ err_1 = _b . sent ( ) ;
144150 throw new Error ( err_1 ) ;
145- case 9 : return [ 2 /*return*/ ] ;
151+ case 11 : return [ 2 /*return*/ ] ;
146152 }
147153 } ) ;
148154 } ) ;
@@ -377,11 +383,10 @@ var OfflineFirstAPI = (function () {
377383 * @memberof OfflineFirstAPI
378384 */
379385 OfflineFirstAPI . prototype . _shouldUseCache = function ( serviceDefinition , options ) {
380- var cacheDisabledFromOptions = options && options . disableCache ;
381- if ( typeof cacheDisabledFromOptions !== 'undefined' ) {
382- return ! cacheDisabledFromOptions ;
386+ if ( options && typeof options . disableCache !== 'undefined' ) {
387+ return ! options . disableCache ;
383388 }
384- else if ( typeof serviceDefinition . disableCache !== 'undefined' ) {
389+ else if ( serviceDefinition && typeof serviceDefinition . disableCache !== 'undefined' ) {
385390 return ! serviceDefinition . disableCache ;
386391 }
387392 else {
@@ -606,11 +611,11 @@ var OfflineFirstAPI = (function () {
606611 OfflineFirstAPI . prototype . _mergeServicesWithDefaultValues = function ( services ) {
607612 var _this = this ;
608613 return _mapValues ( services , function ( service , serviceName ) {
609- if ( service . domain && ! _this . _APIOptions . domains [ service . domain ] ) {
614+ if ( service . domain && typeof _this . _APIOptions . domains [ service . domain ] === 'undefined' ) {
610615 throw new Error ( "Domain key " + service . domain + " specified for service " + serviceName + " hasn't been declared. \n" +
611616 'Please provide it in your OfflineFirstAPI parameters or leave it blank to use the default one.' ) ;
612617 }
613- if ( service . prefix && ! _this . _APIOptions . prefixes [ service . prefix ] ) {
618+ if ( service . prefix && typeof _this . _APIOptions . prefixes [ service . prefix ] === 'undefined' ) {
614619 throw new Error ( "Prefix key " + service . domain + " specified for service " + serviceName + " hasn't been declared. \n" +
615620 'Please provide it in your OfflineFirstAPI parameters or leave it blank to use the default one.' ) ;
616621 }
0 commit comments