Skip to content

Commit

Permalink
feat(7): replace FormData objects with k-v map in object-hash replacer
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbinoDrought committed Sep 14, 2022
1 parent d113b65 commit fc5455e
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 1 deletion.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,24 @@ cachios.getCacheIdentifier = function (config) {
};
```

### Custom object-hash Replacer

By default, Cachios uses an internal `defaultReplacer` function to add FormData support to [object-hash](https://github.com/puleos/object-hash).

To override this, set the `getReplaced` property of your Cachios instance:

```js
const cachios = require('cachios');

cachios.getReplaced = function (thing) {
if (thing === 'foo') {
return 'bar';
}

return thing;
};
```

## License

[MIT](LICENSE.md)
22 changes: 21 additions & 1 deletion src/cachios.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,23 @@ function defaultResponseCopier(response) {
};
}

function defaultReplacer(thing) {
// object-hash doesn't handle FormData values, do it ourselves
// https://github.com/AlbinoDrought/cachios/issues/7
if (typeof FormData !== 'undefined' && thing instanceof FormData) {
var formDataValues = {};
var entriesIterator = thing.entries();
for (var entry = entriesIterator.next(); !entry.done || typeof entry.value !== 'undefined'; entry = entriesIterator.next()) {
formDataValues[entry.value[0]] = defaultReplacer(entry.value[1]);
}
return formDataValues;
}

// object-hash also doesn't support blobs, but I don't think we can read those synchronously

return thing;
}

function Cachios(axiosInstance, nodeCacheConf) {
this.axiosInstance = axiosInstance;
this.cache = new NodeCache(nodeCacheConf || {
Expand All @@ -29,10 +46,13 @@ function Cachios(axiosInstance, nodeCacheConf) {

this.getCacheIdentifier = defaultCacheIdentifer;
this.getResponseCopy = defaultResponseCopier;
this.getReplaced = defaultReplacer;
}

Cachios.prototype.getCacheKey = function (config) {
return hash(this.getCacheIdentifier(config));
return hash(this.getCacheIdentifier(config), {
replacer: this.getReplaced,
});
};

Cachios.prototype.getCachedValue = function (cacheKey) {
Expand Down
79 changes: 79 additions & 0 deletions tests/getReplaced.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* @jest-environment jsdom
*/const cachios = require('./../src');

const axios = require('axios');
const hash = require('object-hash');

describe('cachios.getReplaced', () => {
test('should be set by default', () => {
expect(cachios.getReplaced).toBeDefined();
});

test('should not modify common values', () => {
[
{ foo: 'bar' },
1234,
'Melon',
1.234,
null,
undefined,
].forEach((thing) => {
expect(cachios.getReplaced(thing)).toEqual(thing);
});
});

test('should be used when creating cache keys', () => {
const instance = cachios.create(axios);

instance.getReplaced = (thing) => {
console.log(thing);
if (thing === 'bar') {
return 'baz';
}
return thing;
};

const expected = hash({
method: 'GET',
url: '/something',
params: {
foo: 'baz',
},
data: {
stuff: [
{ foo: 'baz' },
],
},
});

const actual = instance.getCacheKey({
method: 'GET',
url: '/something',
params: {
foo: 'bar',
},
data: {
stuff: [
{ foo: 'bar' },
],
},
});

expect(actual).toBe(expected);
});

test('should transform FormData to an object', () => {
const instance = cachios.create(axios);

const fd = new FormData();
fd.append('foo', 'bar');
fd.append('transform', 'hello');

expect(instance.getReplaced(fd)).toEqual({
foo: 'bar',
transform: 'hello',
});
console.log(typeof cachios);
});
});

0 comments on commit fc5455e

Please sign in to comment.