1 |
|
2 | "use strict";
|
3 |
|
4 | Object.defineProperty(exports, "__esModule", {
|
5 | value: true
|
6 | });
|
7 |
|
8 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
9 |
|
10 | var _lodash = require("lodash");
|
11 |
|
12 | var _lodash2 = _interopRequireDefault(_lodash);
|
13 |
|
14 | var _bluebird = require("bluebird");
|
15 |
|
16 | var _bluebird2 = _interopRequireDefault(_bluebird);
|
17 |
|
18 | var _errors = require("./errors");
|
19 |
|
20 | var _shared = require("./shared");
|
21 |
|
22 | var send = function send(verb, url, data) {
|
23 | return new _bluebird2["default"](function (resolve, reject) {
|
24 | var xhr = api.createXhr();
|
25 | xhr.open(verb, url);
|
26 | if (_lodash2["default"].isObject(data)) {
|
27 | data = JSON.stringify(data);
|
28 | xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
|
29 | }
|
30 | xhr.onreadystatechange = function () {
|
31 | if (xhr.readyState === 4) {
|
32 | (0, _shared.handleRequestComplete)(xhr.status, xhr.statusText, xhr.responseText, resolve, reject);
|
33 | }
|
34 | };
|
35 | xhr.send(data);
|
36 | });
|
37 | };
|
38 |
|
39 | var api = {
|
40 | HttpError: _errors.HttpError,
|
41 | HttpParseError: _errors.HttpParseError,
|
42 |
|
43 | |
44 |
|
45 |
|
46 |
|
47 |
|
48 | createXhr: function createXhr() {
|
49 |
|
50 | return new XMLHttpRequest();
|
51 | },
|
52 |
|
53 | |
54 |
|
55 |
|
56 |
|
57 |
|
58 | get: function get(url) {
|
59 | return send("GET", url);
|
60 | },
|
61 |
|
62 | |
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | post: function post(url, data) {
|
74 | return send("POST", url, data);
|
75 | },
|
76 |
|
77 | |
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 | put: function put(url, data) {
|
89 | return send("PUT", url, data);
|
90 | },
|
91 |
|
92 | |
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | "delete": function _delete(url) {
|
102 | return send("DELETE", url);
|
103 | }
|
104 | };
|
105 |
|
106 | exports["default"] = api;
|
107 | module.exports = exports["default"]; |
\ | No newline at end of file |