@middy/cache
Advanced tools
Comparing version 1.0.0-alpha.58 to 1.0.0-alpha.59
@@ -55,2 +55,69 @@ const { invoke } = require('../../test-helpers') | ||
}) | ||
test('It should cache things using custom cache settings and discard result of a resolve promised with setValue', async () => { | ||
const calculateCacheId = (event) => Promise.resolve(event.id) | ||
const myStorage = {} | ||
const getValue = (key) => new Promise((resolve, reject) => { | ||
setTimeout(() => resolve(myStorage[key]), 50) | ||
}) | ||
const setValue = (key, value) => new Promise((resolve, reject) => { | ||
setTimeout(() => { | ||
myStorage[key] = value | ||
return resolve('Value to discard') | ||
}, 50) | ||
}) | ||
const originalHandler = jest.fn((event, context, cb) => { | ||
cb(null, event.a + event.b) | ||
}) | ||
const handler = middy(originalHandler) | ||
.use(cache({ | ||
calculateCacheId, | ||
getValue, | ||
setValue | ||
})) | ||
const event = { id: 'some_unique_id', a: 2, b: 3 } | ||
const response = await invoke(handler, event) | ||
const response2 = await invoke(handler, event) | ||
expect(response).toEqual(response2) | ||
expect(originalHandler.mock.calls.length).toBe(1) | ||
}) | ||
test('It should throw en error if setValue return a rejected promise', async () => { | ||
const errMessage = 'Something bad happened' | ||
const calculateCacheId = (event) => Promise.resolve(event.id) | ||
const myStorage = {} | ||
const getValue = (key) => new Promise((resolve, reject) => { | ||
setTimeout(() => resolve(myStorage[key]), 50) | ||
}) | ||
const setValue = (key, value) => new Promise((resolve, reject) => { | ||
setTimeout(() => { | ||
myStorage[key] = value | ||
return reject(new Error(errMessage)) | ||
}, 50) | ||
}) | ||
const originalHandler = jest.fn((event, context, cb) => { | ||
cb(null, event.a + event.b) | ||
}) | ||
const handler = middy(originalHandler) | ||
.use(cache({ | ||
calculateCacheId, | ||
getValue, | ||
setValue | ||
})) | ||
const event = { id: 'some_unique_id', a: 2, b: 3 } | ||
try { | ||
await invoke(handler, event) | ||
} catch (e) { | ||
expect(e.message).toEqual(errMessage) | ||
expect(originalHandler.mock.calls.length).toBe(1) | ||
} | ||
}) | ||
}) |
@@ -35,5 +35,6 @@ const { createHash } = require('crypto') | ||
options.setValue(currentCacheKey, handler.response) | ||
.then(next) | ||
.then(() => next()) | ||
.catch(err => next(err)) | ||
} | ||
}) | ||
} |
{ | ||
"name": "@middy/cache", | ||
"version": "1.0.0-alpha.58", | ||
"version": "1.0.0-alpha.59", | ||
"description": "Cache middleware for the middy framework", | ||
@@ -44,6 +44,6 @@ "engines": { | ||
"devDependencies": { | ||
"@middy/core": "^1.0.0-alpha.58", | ||
"@middy/core": "^1.0.0-alpha.59", | ||
"es6-promisify": "^6.0.2" | ||
}, | ||
"gitHead": "df2dd587d4a43540950eaba77e913ade322ca32b" | ||
"gitHead": "338ed037a83b0ae46af3ed5b94bedea85fd6156a" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11935
155