Skip to content
This repository has been archived by the owner on Jun 12, 2022. It is now read-only.

Commit

Permalink
feat(cache): export cache deletion functionality (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat authored Aug 23, 2017
1 parent 800a8e5 commit 3da4250
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
13 changes: 13 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,22 @@ cachingFetch.defaults = function (_uri, _opts) {
}

defaultedFetch.defaults = fetch.defaults
defaultedFetch.delete = fetch.delete
return defaultedFetch
}

cachingFetch.delete = cacheDelete
function cacheDelete (uri, opts) {
opts = configureOptions(opts)
if (opts.cacheManager) {
const req = new fetch.Request(uri, {
method: opts.method,
headers: opts.headers
})
return opts.cacheManager.delete(req, opts)
}
}

function initializeCache (opts) {
if (typeof opts.cacheManager === 'string') {
if (!Cache) {
Expand Down
33 changes: 33 additions & 0 deletions test/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,39 @@ test('nothing cached if body stream never used', t => {
})
})

test('exports cache deletion API', t => {
tnock(t, HOST).get('/test').twice().reply(200, CONTENT, HEADERS)
return fetch(`${HOST}/test`, {
cacheManager: CACHE,
retry: {retries: 0}
}).then(res => {
t.notOk(
res.headers.get('x-local-cache'),
'no cache headers if response is from network'
)
return res.buffer()
}).then(body => {
t.deepEqual(body, CONTENT, 'got remote content')
return fetch.delete(`${HOST}/test`, {
cacheManager: CACHE
})
}).then(() => {
return fetch(`${HOST}/test`, {
cacheManager: CACHE,
retry: {retries: 0}
})
}).then(res => {
t.equal(res.status, 200, 'request succeeded')
t.notOk(
res.headers.get('x-local-cache'),
'no cache headers if response is from network'
)
return res.buffer()
}).then(body => {
t.deepEqual(body, CONTENT, 'got remote content')
})
})

test('small responses cached', t => {
tnock(t, HOST).get('/test').reply(200, CONTENT, {
'Content-Length': CONTENT.length,
Expand Down

0 comments on commit 3da4250

Please sign in to comment.