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

Commit

Permalink
fix(method): node-fetch guarantees uppercase
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Mar 31, 2017
1 parent 6fba805 commit a1d68d6
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module.exports = cachingFetch
function cachingFetch (uri, _opts) {
const opts = {}
Object.keys(_opts || {}).forEach(k => { opts[k] = _opts[k] })
opts.method = opts.method && opts.method.toUpperCase()
if (typeof opts.cacheManager === 'string' && !Cache) { Cache = require('./cache') }
opts.cacheManager = opts.cacheManager && (
typeof opts.cacheManager === 'string'
Expand All @@ -34,7 +35,7 @@ function cachingFetch (uri, _opts) {
opts.cache = 'no-store'
}
if (
(!opts.method || opts.method.toLowerCase() === 'get') &&
(!opts.method || opts.method === 'GET') &&
opts.cacheManager &&
opts.cache !== 'no-store' &&
opts.cache !== 'reload'
Expand Down Expand Up @@ -98,13 +99,13 @@ function condFetch (uri, cachedRes, opts) {
newHeaders[k] = opts.headers[k]
})
if (cachedRes.headers.get('etag')) {
const condHeader = opts.method && opts.method.toLowerCase() !== 'get'
const condHeader = opts.method && opts.method !== 'GET'
? 'if-match'
: 'if-none-match'
newHeaders[condHeader] = cachedRes.headers.get('etag')
}
if (cachedRes.headers.get('last-modified')) {
const condHeader = opts.method && opts.method.toLowerCase() !== 'get'
const condHeader = opts.method && opts.method !== 'GET'
? 'if-unmodified-since'
: 'if-modified-since'
newHeaders[condHeader] = cachedRes.headers.get('last-modified')
Expand All @@ -115,13 +116,13 @@ function condFetch (uri, cachedRes, opts) {
condRes.body = cachedRes.body
// TODO - freshen up the cached entry
} else if (condRes.status >= 500) {
if (condRes.method.toLowerCase() === 'get') {
if (condRes.method === 'GET') {
return cachedRes
} else {
return opts.cacheManager.delete(uri).then(() => cachedRes)
}
}
if (condRes.method.toLowerCase() !== 'get') {
if (condRes.method !== 'GET') {
return opts.cacheManager.delete(uri).then(() => condRes)
} else {
return condRes
Expand Down Expand Up @@ -149,20 +150,20 @@ function remoteFetch (uri, opts) {
const req = new fetch.Request(uri, reqOpts)
return fetch(req).then(res => {
if (
req.method.toLowerCase() === 'get' &&
req.method === 'GET' &&
opts.cacheManager &&
opts.cache !== 'no-store' &&
// No other statuses should be stored!
res.status === 200
) {
return opts.cacheManager.put(req, res, opts.cacheOpts)
} else if (req.method.toLowerCase() !== 'post' && res.status >= 500) {
} else if (req.method !== 'POST' && res.status >= 500) {
return retryHandler(res)
} else {
return res
}
}).catch(err => {
if (req.method.toLowerCase() !== 'post') {
if (req.method !== 'POST') {
return retryHandler(err)
} else {
throw err
Expand Down

0 comments on commit a1d68d6

Please sign in to comment.