Skip to content

Commit ce328ef

Browse files
authored
Merge pull request #8 from Exilz/v2.1.0
v2.1.0
2 parents 29d6ae0 + b002348 commit ce328ef

File tree

6 files changed

+52
-13
lines changed

6 files changed

+52
-13
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ Key | Type | Description | Example
161161
`disableCache` | `boolean` | Optional, completely disables caching (overriden by service definitions & `fetch`'s `option` parameter)
162162
`cacheExpiration` | `number` | Optional default expiration of cached data in ms (overriden by service definitions & `fetch`'s `option` parameter)
163163
`cachePrefix` | `string` | Optional, prefix of the keys stored on your cache, defaults to `offlineApiCache`
164+
`ignoreHeadersWhenCaching` | `boolean` | Optional, your requests will be cached independently from the headers you sent. Defaults to `false`
164165
`capServices` | `boolean` | Optional, enable capping for every service, defaults to `false`, see [limiting the size of your cache](#limiting-the-size-of-your-cache)
165166
`capLimit` | `number` | Optional quantity of cached items for each service, defaults to `50`, see [limiting the size of your cache](#limiting-the-size-of-your-cache)
166167
`offlineDriver` | `IAPIDriver` | Optional, see [use your own driver for caching](#use-your-own-driver-for-caching)
@@ -180,6 +181,7 @@ Key | Type | Description | Example
180181
`disableCache` | `boolean` | Optional, disables the cache for this service (override your [API's global options](#api-options))
181182
`capService` | `boolean` | Optional, enable or disable capping for this specific service, see [limiting the size of your cache](#limiting-the-size-of-your-cache)
182183
`capLimit` | `number` | Optional quantity of cached items for this specific service, defaults to `50`, see [limiting the size of your cache](#limiting-the-size-of-your-cache)
184+
`ignoreHeadersWhenCaching` | `boolean` | Optional, your requests will be cached independently from the headers you sent. Defaults to `false`
183185
`rawData` | `boolean` | Disables JSON parsing from your network requests, useful if you want to fetch XML or anything else from your api
184186

185187
## Fetch options

demo/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"dependencies": {
1010
"react": "16.0.0-alpha.12",
1111
"react-native": "0.47.1",
12-
"react-native-offline-api": "2.0.0"
12+
"react-native-offline-api": "2.1.0"
1313
},
1414
"devDependencies": {
1515
"babel-jest": "20.0.3",

dist/index.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ var DEFAULT_API_OPTIONS = {
5454
disableCache: false,
5555
cacheExpiration: 5 * 60 * 1000,
5656
cachePrefix: 'offlineApiCache',
57+
ignoreHeadersWhenCaching: false,
5758
capServices: false,
5859
capLimit: 50
5960
};
@@ -73,7 +74,7 @@ var OfflineFirstAPI = /** @class */ (function () {
7374
}
7475
OfflineFirstAPI.prototype.fetch = function (service, options) {
7576
return __awaiter(this, void 0, void 0, function () {
76-
var serviceDefinition, _a, fullPath, withoutQueryParams, middlewares, fetchOptions, fetchHeaders, shouldUseCache, requestId, expiration, _sha, expirationDelay, cachedData, parsedResponseData, res, _b, err_1;
77+
var serviceDefinition, _a, fullPath, withoutQueryParams, middlewares, fetchOptions, fetchHeaders, shouldUseCache, expiration, requestId, expirationDelay, cachedData, parsedResponseData, res, _b, err_1;
7778
return __generator(this, function (_c) {
7879
switch (_c.label) {
7980
case 0:
@@ -91,11 +92,8 @@ var OfflineFirstAPI = /** @class */ (function () {
9192
fetchOptions = _merge(middlewares, (options && options.fetchOptions) || {}, { method: serviceDefinition.method }, { headers: (options && options.headers) || {} });
9293
fetchHeaders = options && options.fetchHeaders;
9394
shouldUseCache = this._shouldUseCache(serviceDefinition, options);
94-
requestId = void 0;
9595
expiration = void 0;
96-
_sha = new sha('SHA-1', 'TEXT');
97-
_sha.update(fullPath + ":" + (fetchHeaders ? 'headersOnly' : '') + ":" + JSON.stringify(fetchOptions));
98-
requestId = _sha.getHash('HEX');
96+
requestId = this._buildRequestId(serviceDefinition, fullPath, fetchHeaders, fetchOptions, options);
9997
expirationDelay = (options && options.expiration) || serviceDefinition.expiration || this._APIOptions.cacheExpiration;
10098
expiration = Date.now() + expirationDelay;
10199
return [4 /*yield*/, this._getCachedData(service, requestId, fullPath)];
@@ -543,6 +541,22 @@ var OfflineFirstAPI = /** @class */ (function () {
543541
});
544542
});
545543
};
544+
OfflineFirstAPI.prototype._buildRequestId = function (serviceDefinition, fullPath, fetchHeaders, mergedOptions, // fully merged options
545+
fetchOptions // fetch options
546+
) {
547+
var ignoreHeadersWhenCaching = this._APIOptions.ignoreHeadersWhenCaching ||
548+
serviceDefinition.ignoreHeadersWhenCaching ||
549+
(fetchOptions && fetchOptions.ignoreHeadersWhenCaching);
550+
var _sha = new sha('SHA-1', 'TEXT');
551+
var requestStringId = fullPath + ":" + (fetchHeaders ? 'headersOnly' : '');
552+
Object.keys(mergedOptions).forEach(function (key) {
553+
if (!ignoreHeadersWhenCaching || key !== 'headers') {
554+
requestStringId += JSON.stringify(mergedOptions[key]);
555+
}
556+
});
557+
_sha.update(requestStringId);
558+
return _sha.getHash('HEX');
559+
};
546560
/**
547561
* Helper returning the full URL of a service and its options.
548562
* @private

package.json

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

src/index.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const DEFAULT_API_OPTIONS = {
2222
disableCache: false,
2323
cacheExpiration: 5 * 60 * 1000,
2424
cachePrefix: 'offlineApiCache',
25+
ignoreHeadersWhenCaching: false,
2526
capServices: false,
2627
capLimit: 50
2728
};
@@ -67,13 +68,8 @@ export default class OfflineFirstAPI {
6768
);
6869
const fetchHeaders = options && options.fetchHeaders;
6970
const shouldUseCache = this._shouldUseCache(serviceDefinition, options);
70-
let requestId;
7171
let expiration;
72-
73-
// Build a short, hashed, identifier for that request
74-
const _sha = new sha('SHA-1', 'TEXT');
75-
_sha.update(`${fullPath}:${fetchHeaders ? 'headersOnly' : ''}:${JSON.stringify(fetchOptions)}`);
76-
requestId = _sha.getHash('HEX');
72+
const requestId = this._buildRequestId(serviceDefinition, fullPath, fetchHeaders, fetchOptions, options);
7773

7874
// Expiration priority : option parameter of fetch() > service definition > default setting
7975
const expirationDelay =
@@ -426,6 +422,31 @@ export default class OfflineFirstAPI {
426422
}
427423
}
428424

425+
private _buildRequestId (
426+
serviceDefinition: IAPIService,
427+
fullPath: string,
428+
fetchHeaders: boolean,
429+
mergedOptions?: IFetchOptions, // fully merged options
430+
fetchOptions?: IFetchOptions // fetch options
431+
): string {
432+
const ignoreHeadersWhenCaching =
433+
this._APIOptions.ignoreHeadersWhenCaching ||
434+
serviceDefinition.ignoreHeadersWhenCaching ||
435+
(fetchOptions && fetchOptions.ignoreHeadersWhenCaching);
436+
437+
const _sha = new sha('SHA-1', 'TEXT');
438+
let requestStringId = `${fullPath}:${fetchHeaders ? 'headersOnly' : ''}`;
439+
440+
Object.keys(mergedOptions).forEach((key) => {
441+
if (!ignoreHeadersWhenCaching || key !== 'headers') {
442+
requestStringId += JSON.stringify(mergedOptions[key]);
443+
}
444+
});
445+
446+
_sha.update(requestStringId);
447+
return _sha.getHash('HEX');
448+
}
449+
429450
/**
430451
* Helper returning the full URL of a service and its options.
431452
* @private

src/interfaces.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export interface IAPIOptions {
77
disableCache?: boolean;
88
cacheExpiration?: number;
99
cachePrefix?: string;
10+
ignoreHeadersWhenCaching?: boolean;
1011
capServices?: boolean;
1112
capLimit?: number;
1213
offlineDriver: IAPIDriver;
@@ -19,6 +20,7 @@ export interface IAPIService {
1920
domain?: string;
2021
prefix?: string;
2122
middlewares?: APIMiddleware[];
23+
ignoreHeadersWhenCaching?: boolean;
2224
disableCache?: boolean;
2325
capService?: boolean;
2426
capLimit?: number;

0 commit comments

Comments
 (0)