'f'
mh-two-thousand-and-two
2024-04-12 26f2711ef9461961fb953e2b497bd314ef95e345
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
var each = require('./each');
var defaults = require('./defaults');
var noop = require('./noop');
var has = require('./has');
var root = require('./root');
var Promise = root.Promise;
exports = function(url, options) {
    options = options || {};
    defaults(options, exports.setting);
    return new Promise(function(resolve, reject) {
        var xhr = options.xhr();
        var headers = options.headers;
        var body = options.body;
        var timeout = options.timeout;
        var abortTimer;
        xhr.withCredentials = options.credentials == 'include';
        xhr.onload = function() {
            clearTimeout(abortTimer);
            resolve(getRes(xhr));
        };
        xhr.onerror = reject;
        xhr.open(options.method, url, true);
        each(headers, function(val, key) {
            xhr.setRequestHeader(key, val);
        });
        if (timeout > 0) {
            setTimeout(function() {
                xhr.onload = noop;
                xhr.abort();
                reject(Error('timeout'));
            }, timeout);
        }
        xhr.send(body);
    });
};
var regHeaders = /^(.*?):\s*([\s\S]*?)$/gm;
function getRes(xhr) {
    var keys = [];
    var all = [];
    var headers = {};
    var header;
    xhr.getAllResponseHeaders().replace(regHeaders, function(m, key, val) {
        key = key.toLowerCase();
        keys.push(key);
 
        all.push([key, val]);
        header = headers[key];
        headers[key] = header ? header + ',' + val : val;
    });
    return {
        ok: xhr.status >= 200 && xhr.status < 400,
        status: xhr.status,
        statusText: xhr.statusText,
        url: xhr.responseURL,
        clone: function() {
            return getRes(xhr);
        },
        text: function() {
            return Promise.resolve(xhr.responseText);
        },
        json: function() {
            return Promise.resolve(xhr.responseText).then(JSON.parse);
        },
        xml: function() {
            return Promise.resolve(xhr.responseXML);
        },
        blob: function() {
            return Promise.resolve(new Blob([xhr.response]));
        },
        headers: {
            keys: function() {
                return keys;
            },
            entries: function() {
                return all;
            },
            get: function(name) {
                return headers[name.toLowerCase()];
            },
            has: function(name) {
                return has(headers, name);
            }
        }
    };
}
exports.setting = {
    method: 'GET',
    headers: {},
    timeout: 0,
    xhr: function() {
        return new XMLHttpRequest();
    }
};
 
module.exports = exports;