Skip to content

Commit 638ea8e

Browse files
authored
Merge pull request #2 from Exilz/dev
v1.0.1
2 parents 83685a5 + 16d5c69 commit 638ea8e

File tree

4 files changed

+58
-35
lines changed

4 files changed

+58
-35
lines changed

dist/index.js

+29-8
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ var OfflineFirstAPI = (function () {
7272
}
7373
OfflineFirstAPI.prototype.fetch = function (service, options) {
7474
return __awaiter(this, void 0, void 0, function () {
75-
var serviceDefinition, fullPath, middlewares, fetchOptions, fetchHeaders, shouldCache, requestId, expiration, _sha, expirationDelay, cachedData, parsedResponseData, res, err_1;
75+
var serviceDefinition, fullPath, middlewares, fetchOptions, fetchHeaders, shouldUseCache, requestId, expiration, _sha, expirationDelay, cachedData, parsedResponseData, res, err_1;
7676
return __generator(this, function (_a) {
7777
switch (_a.label) {
7878
case 0:
@@ -89,8 +89,7 @@ var OfflineFirstAPI = (function () {
8989
middlewares = _a.sent();
9090
fetchOptions = _merge(middlewares, (options && options.fetchOptions) || {}, { method: serviceDefinition.method }, { headers: (options && options.headers) || {} });
9191
fetchHeaders = options && options.fetchHeaders;
92-
shouldCache = !this._APIOptions.disableCache &&
93-
!(serviceDefinition.disableCache || (options && options.disableCache));
92+
shouldUseCache = this._shouldUseCache(serviceDefinition, options);
9493
requestId = void 0;
9594
expiration = void 0;
9695
_sha = new sha('SHA-1', 'TEXT');
@@ -101,12 +100,12 @@ var OfflineFirstAPI = (function () {
101100
return [4 /*yield*/, this._getCachedData(service, requestId, fullPath)];
102101
case 3:
103102
cachedData = _a.sent();
104-
if (cachedData.success && cachedData.fresh) {
103+
if (cachedData.success && cachedData.fresh && shouldUseCache) {
105104
this._log("Using fresh cache for " + fullPath);
106105
return [2 /*return*/, cachedData.data];
107106
}
108107
// Network fetch
109-
this._logNetwork(serviceDefinition, fetchHeaders, options);
108+
this._logNetwork(serviceDefinition, fullPath, fetchHeaders, options);
110109
this._log('full URL for request', fullPath);
111110
this._log('full fetch options for request', fetchOptions);
112111
parsedResponseData = void 0;
@@ -133,7 +132,7 @@ var OfflineFirstAPI = (function () {
133132
_a.label = 7;
134133
case 7:
135134
// Cache if it hasn't been disabled and if the network request has been successful
136-
if (res.data.ok && shouldCache) {
135+
if (res.data.ok && shouldUseCache) {
137136
this._cache(service, requestId, parsedResponseData, expiration);
138137
}
139138
this._log('parsed network response', parsedResponseData);
@@ -344,6 +343,28 @@ var OfflineFirstAPI = (function () {
344343
});
345344
});
346345
};
346+
/**
347+
* Helper returning if caching should be enabled for a request.
348+
* Cache enabling priority :
349+
* option parameter of fetch() > service definition > default setting
350+
* @private
351+
* @param {IAPIService} serviceDefinition
352+
* @param {IFetchOptions} options
353+
* @returns {boolean}
354+
* @memberof OfflineFirstAPI
355+
*/
356+
OfflineFirstAPI.prototype._shouldUseCache = function (serviceDefinition, options) {
357+
var cacheDisabledFromOptions = options && options.disableCache;
358+
if (typeof cacheDisabledFromOptions !== 'undefined') {
359+
return !cacheDisabledFromOptions;
360+
}
361+
else if (typeof serviceDefinition.disableCache !== 'undefined') {
362+
return !serviceDefinition.disableCache;
363+
}
364+
else {
365+
return !this._APIOptions.disableCache;
366+
}
367+
};
347368
/**
348369
* Pushes a requestId into a service's dictionary and associate its expiration date to it.
349370
* @private
@@ -558,9 +579,9 @@ var OfflineFirstAPI = (function () {
558579
* @param {IFetchOptions} [options]
559580
* @memberof OfflineFirstAPI
560581
*/
561-
OfflineFirstAPI.prototype._logNetwork = function (serviceDefinition, fetchHeaders, options) {
582+
OfflineFirstAPI.prototype._logNetwork = function (serviceDefinition, fullPath, fetchHeaders, options) {
562583
if (this._APIOptions.printNetworkRequests) {
563-
console.log("%c Network request " + (fetchHeaders ? '(headers only)' : '') + " for " + serviceDefinition.path + " " +
584+
console.log("%c Network request " + (fetchHeaders ? '(headers only)' : '') + " for " + fullPath + " " +
564585
("(" + ((options && options.method) || serviceDefinition.method) + ")"), 'font-weight: bold; color: blue');
565586
}
566587
};

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-offline-api",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "Offline first API wrapper for react-native",
55
"main": "./dist/index.js",
66
"types": "./src/index.d.ts",

src/index.ts

+27-22
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,7 @@ export default class OfflineFirstAPI {
5959
{ headers: (options && options.headers) || {} }
6060
);
6161
const fetchHeaders = options && options.fetchHeaders;
62-
// Should be cached if : not disabled globally, not disabled for this service definition and not
63-
// disabled from the option parameter of fetch ()
64-
const shouldCache = !this._APIOptions.disableCache &&
65-
!(serviceDefinition.disableCache || (options && options.disableCache));
62+
const shouldUseCache = this._shouldUseCache(serviceDefinition, options);
6663
let requestId;
6764
let expiration;
6865

@@ -77,13 +74,13 @@ export default class OfflineFirstAPI {
7774
expiration = Date.now() + expirationDelay;
7875
const cachedData = await this._getCachedData(service, requestId, fullPath);
7976

80-
if (cachedData.success && cachedData.fresh) {
77+
if (cachedData.success && cachedData.fresh && shouldUseCache) {
8178
this._log(`Using fresh cache for ${fullPath}`);
8279
return cachedData.data;
8380
}
8481

8582
// Network fetch
86-
this._logNetwork(serviceDefinition, fetchHeaders, options);
83+
this._logNetwork(serviceDefinition, fullPath, fetchHeaders, options);
8784
this._log('full URL for request', fullPath);
8885
this._log('full fetch options for request', fetchOptions);
8986
let parsedResponseData;
@@ -107,7 +104,7 @@ export default class OfflineFirstAPI {
107104
}
108105

109106
// Cache if it hasn't been disabled and if the network request has been successful
110-
if (res.data.ok && shouldCache) {
107+
if (res.data.ok && shouldUseCache) {
111108
this._cache(service, requestId, parsedResponseData, expiration);
112109
}
113110

@@ -174,7 +171,6 @@ export default class OfflineFirstAPI {
174171
this._log('custom driver set');
175172
}
176173

177-
178174
/**
179175
* Simple helper that won't ever throw an error into the stack if the network request
180176
* isn't successful. This is useful to implement the cache's logic when the API is unreachable.
@@ -192,7 +188,6 @@ export default class OfflineFirstAPI {
192188
}
193189
}
194190

195-
196191
/**
197192
* Cache the network response for a request. Create the service dictionary if it hasn't been done yet,
198193
* store the expiration date to the requestId and finally store the data itself.
@@ -216,7 +211,6 @@ export default class OfflineFirstAPI {
216211
}
217212
}
218213

219-
220214
/**
221215
* Promise that tries to fetch a cached data. Resolves the data if successful and its freshness.
222216
* If this request hasn't been cached yet, resolves with success set to false.
@@ -253,6 +247,27 @@ export default class OfflineFirstAPI {
253247
}
254248

255249

250+
/**
251+
* Helper returning if caching should be enabled for a request.
252+
* Cache enabling priority :
253+
* option parameter of fetch() > service definition > default setting
254+
* @private
255+
* @param {IAPIService} serviceDefinition
256+
* @param {IFetchOptions} options
257+
* @returns {boolean}
258+
* @memberof OfflineFirstAPI
259+
*/
260+
private _shouldUseCache (serviceDefinition: IAPIService, options: IFetchOptions): boolean {
261+
const cacheDisabledFromOptions = options && options.disableCache;
262+
if (typeof cacheDisabledFromOptions !== 'undefined') {
263+
return !cacheDisabledFromOptions;
264+
} else if (typeof serviceDefinition.disableCache !== 'undefined') {
265+
return !serviceDefinition.disableCache;
266+
} else {
267+
return !this._APIOptions.disableCache;
268+
}
269+
}
270+
256271
/**
257272
* Pushes a requestId into a service's dictionary and associate its expiration date to it.
258273
* @private
@@ -279,7 +294,6 @@ export default class OfflineFirstAPI {
279294
}
280295
}
281296

282-
283297
/**
284298
* Promise that resolves every cache key associated to a service : the service dictionary's name, and all requestId
285299
* stored. This is useful to clear the cache without affecting the user's stored data not related to this API.
@@ -305,7 +319,6 @@ export default class OfflineFirstAPI {
305319
}
306320
}
307321

308-
309322
/**
310323
* Simple helper getting a service's dictionary cache key.
311324
* @private
@@ -317,7 +330,6 @@ export default class OfflineFirstAPI {
317330
return `${CACHE_PREFIX}:dictionary:${service}`;
318331
}
319332

320-
321333
/**
322334
* Simple helper getting a request's cache key.
323335
* @private
@@ -329,7 +341,6 @@ export default class OfflineFirstAPI {
329341
return `${CACHE_PREFIX}:${requestId}`;
330342
}
331343

332-
333344
/**
334345
* Resolve each middleware provided and merge them into a single object that will be passed to
335346
* the network request.
@@ -355,7 +366,6 @@ export default class OfflineFirstAPI {
355366
}
356367
}
357368

358-
359369
/**
360370
* Helper returning the full URL of a service and its options.
361371
* @private
@@ -374,7 +384,6 @@ export default class OfflineFirstAPI {
374384
return domainURL + prefix + '/' + parsedPath;
375385
}
376386

377-
378387
/**
379388
* Helper replacing the pathParameters from the service definition's path and appending
380389
* any supplied query parameters. For instance :
@@ -408,7 +417,6 @@ export default class OfflineFirstAPI {
408417
return path;
409418
}
410419

411-
412420
/**
413421
* Merge the supplied API options with the default ones.
414422
* @private
@@ -427,7 +435,6 @@ export default class OfflineFirstAPI {
427435
};
428436
}
429437

430-
431438
/**
432439
* For each suppliedservice, map the default service options to it and throw errors if the required
433440
* options are missing.
@@ -457,7 +464,6 @@ export default class OfflineFirstAPI {
457464
});
458465
}
459466

460-
461467
/**
462468
* Debug helper logging every network request.
463469
* @private
@@ -466,17 +472,16 @@ export default class OfflineFirstAPI {
466472
* @param {IFetchOptions} [options]
467473
* @memberof OfflineFirstAPI
468474
*/
469-
private _logNetwork (serviceDefinition: IAPIService, fetchHeaders: boolean, options?: IFetchOptions): void {
475+
private _logNetwork (serviceDefinition: IAPIService, fullPath: string, fetchHeaders: boolean, options?: IFetchOptions): void {
470476
if (this._APIOptions.printNetworkRequests) {
471477
console.log(
472-
`%c Network request ${fetchHeaders ? '(headers only)' : ''} for ${serviceDefinition.path} ` +
478+
`%c Network request ${fetchHeaders ? '(headers only)' : ''} for ${fullPath} ` +
473479
`(${(options && options.method) || serviceDefinition.method})`,
474480
'font-weight: bold; color: blue'
475481
);
476482
}
477483
}
478484

479-
480485
/**
481486
* Debug helper logging every major logic step when user has enabled debugging.
482487
* @private

tsconfig.json

+1-4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@
1313
"sourceMap": false,
1414
"alwaysStrict": true,
1515
"strictNullChecks": false,
16-
"noUnusedLocals": false,
17-
"types": [
18-
"src/index.d.ts"
19-
]
16+
"noUnusedLocals": false
2017
},
2118
"include": [
2219
"src"

0 commit comments

Comments
 (0)