Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
var ourFetch = function (url, options) {
options = options || {};
options.headers = options.headers || new Headers();
options.credentials = 'include';
if (opts.auth || host.auth) {
var nAuth = opts.auth || host.auth;
var str = nAuth.username + ':' + nAuth.password;
var token = btoa(unescape(encodeURIComponent(str)));
options.headers.set('Authorization', 'Basic ' + token);
}
var headers = opts.headers || {};
Object.keys(headers).forEach(function (key) {
options.headers.append(key, headers[key]);
});
/* istanbul ignore if */
return result;
}).then(postprocessAttachments(opts));
}
// We are using a temporary view, terrible for performance, good for testing
body = body || {};
Object.keys(fun).forEach(function (key) {
if (Array.isArray(fun[key])) {
body[key] = fun[key];
} else {
body[key] = fun[key].toString();
}
});
return db.fetch('_temp_view' + params, {
headers: new Headers({'Content-Type': 'application/json'}),
method: 'POST',
body: JSON.stringify(body)
}).then(function (response) {
ok = response.ok;
status = response.status;
return response.json();
}).then(function (result) {
if (!ok) {
result.status = status;
throw generateErrorFromResponse(result);
}
return result;
}).then(postprocessAttachments(opts));
}
params += (params[0] === '?' ? '&' : '?') + keysAsString;
} else {
method = 'POST';
if (typeof fun === 'string') {
body = {keys: opts.keys};
} else { // fun is {map : mapfun}, so append to this
fun.keys = opts.keys;
}
}
}
// We are referencing a query defined in the design doc
if (typeof fun === 'string') {
var parts = parseViewName(fun);
return db.fetch('_design/' + parts[0] + '/_view/' + parts[1] + params, {
headers: new Headers({'Content-Type': 'application/json'}),
method: method,
body: JSON.stringify(body)
}).then(function (response) {
ok = response.ok;
status = response.status;
return response.json();
}).then(function (result) {
if (!ok) {
result.status = status;
throw generateErrorFromResponse(result);
}
// fail the entire request if the result contains an error
result.rows.forEach(function (row) {
/* istanbul ignore if */
if (row.value && row.value.error && row.value.error === "builtin_reduce_error") {
throw new Error(row.reason);
function dbFetch(db, path, opts, callback) {
var status, ok;
opts.headers = new Headers({'Content-type': 'application/json'});
db.fetch(path, opts).then(function (response) {
status = response.status;
ok = response.ok;
return response.json();
}).then(function (json) {
if (!ok) {
json.status = status;
var err = generateErrorFromResponse(json);
callback(err);
} else {
callback(null, json);
}
}).catch(callback);
}
function fetchJSON(url, options, callback) {
var result = {};
options = options || {};
options.headers = options.headers || new Headers();
if (!options.headers.get('Content-Type')) {
options.headers.set('Content-Type', 'application/json');
}
if (!options.headers.get('Accept')) {
options.headers.set('Accept', 'application/json');
}
return ourFetch(url, options).then(function (response) {
result.ok = response.ok;
result.status = response.status;
return response.json();
}).then(function (json) {
result.data = json;
if (!result.ok) {
result.data.status = result.status;
function httpViewCleanup(db) {
return db.fetch('_view_cleanup', {
headers: new Headers({'Content-Type': 'application/json'}),
method: 'POST'
}).then(function (response) {
return response.json();
});
}