1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133 |
2×
2×
2×
2×
2×
2×
2×
2×
2×
2×
2×
8×
1×
22694×
22694×
22694×
1×
19399×
8×
8×
8×
19391×
5545×
15175×
196×
14979×
19391×
1196×
19391×
1×
26×
26×
22×
22×
22×
4×
4×
1×
4×
4×
22694×
22694×
21489×
22694×
22694×
1205×
1205×
22694×
131×
22694×
22560×
26×
26×
22534×
22534×
22534×
22534×
131×
131×
22534×
19399×
3135×
3135×
3135×
2×
2× | 'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _request = require('./request');
var _request2 = _interopRequireDefault(_request);
var _jsExtend = require('js-extend');
var _errors = require('./../errors');
var _clone = require('../../deps/clone');
var _clone2 = _interopRequireDefault(_clone);
var _applyTypeToBuffer = require('./applyTypeToBuffer');
var _applyTypeToBuffer2 = _interopRequireDefault(_applyTypeToBuffer);
var _defaultBody = require('./defaultBody');
var _defaultBody2 = _interopRequireDefault(_defaultBody);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function ajaxCore(options, callback) {
options = (0, _clone2.default)(options);
var defaultOptions = {
method: "GET",
headers: {},
json: true,
processData: true,
timeout: 10000,
cache: false
};
options = (0, _jsExtend.extend)(defaultOptions, options);
function onSuccess(obj, resp, cb) {
if (!options.binary && options.json && typeof obj === 'string') {
try {
obj = JSON.parse(obj);
} catch (e) {
// Probably a malformed JSON from server
return cb(e);
}
}
if (Array.isArray(obj)) {
obj = obj.map(function (v) {
if (v.error || v.missing) {
return (0, _errors.generateErrorFromResponse)(v);
} else {
return v;
}
});
}
if (options.binary) {
(0, _applyTypeToBuffer2.default)(obj, resp);
}
cb(null, obj, resp);
}
function onError(err, cb) {
var errParsed, errObj;
if (err.code && err.status) {
var err2 = new Error(err.message || err.code);
err2.status = err.status;
return cb(err2);
}
// We always get code && status in node
/* istanbul ignore next */
try {
errParsed = JSON.parse(err.responseText);
//would prefer not to have a try/catch clause
errObj = (0, _errors.generateErrorFromResponse)(errParsed);
} catch (e) {
errObj = (0, _errors.generateErrorFromResponse)(err);
}
/* istanbul ignore next */
cb(errObj);
}
Eif (options.json) {
if (!options.binary) {
options.headers.Accept = 'application/json';
}
options.headers['Content-Type'] = options.headers['Content-Type'] || 'application/json';
}
if (options.binary) {
options.encoding = null;
options.json = false;
}
if (!options.processData) {
options.json = false;
}
return (0, _request2.default)(options, function (err, response, body) {
if (err) {
err.status = response ? response.statusCode : 400;
return onError(err, callback);
}
var error;
var content_type = response.headers && response.headers['content-type'];
var data = body || (0, _defaultBody2.default)();
// CouchDB doesn't always return the right content-type for JSON data, so
// we check for ^{ and }$ (ignoring leading/trailing whitespace)
if (!options.binary && (options.json || !options.processData) && typeof data !== 'object' && (/json/.test(content_type) || /^[\s]*\{/.test(data) && /\}[\s]*$/.test(data))) {
try {
data = JSON.parse(data.toString());
} catch (e) {}
}
if (response.statusCode >= 200 && response.statusCode < 300) {
onSuccess(data, response, callback);
} else {
error = (0, _errors.generateErrorFromResponse)(data);
error.status = response.statusCode;
callback(error);
}
});
}
exports.default = ajaxCore;
module.exports = exports['default']; |