UNPKG

4.15 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = undefined;
7
8var _promise = require('babel-runtime/core-js/promise');
9
10var _promise2 = _interopRequireDefault(_promise);
11
12var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
13
14var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
15
16var _createClass2 = require('babel-runtime/helpers/createClass');
17
18var _createClass3 = _interopRequireDefault(_createClass2);
19
20var _superagent = require('superagent');
21
22var _superagent2 = _interopRequireDefault(_superagent);
23
24var _config = require('../config');
25
26var _config2 = _interopRequireDefault(_config);
27
28function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
29
30(function () {
31 var enterModule = require('react-hot-loader').enterModule;
32
33 enterModule && enterModule(module);
34})();
35
36var methods = ['get', 'post', 'put', 'patch', 'del'];
37
38function getProtocol(req) {
39 return "http:";
40 //return req.headers["x-proto"]=="http"?"http:":"https:";
41}
42function formatUrl(path, config, req) {
43 if (/^(https?:)|^(\/\/)/.test(path)) return path;
44 var adjustedPath = path[0] !== '/' ? '/' + path : path;
45 if (__SERVER__) {
46 var protocol = getProtocol(req);
47 if (config.apiPort != "" && config.apiPort != "80") {
48 return protocol + '//' + config.apiHost + ':' + config.apiPort + adjustedPath;
49 } else {
50 return protocol + '//' + config.apiHost + adjustedPath;
51 }
52 }
53 return '/proxy' + adjustedPath;
54}
55
56var ApiClient = function () {
57 function ApiClient(req) {
58 var _this = this;
59
60 (0, _classCallCheck3.default)(this, ApiClient);
61
62 methods.forEach(function (method) {
63 return _this[method] = function (path) {
64 var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
65 params = _ref.params,
66 data = _ref.data;
67
68 return new _promise2.default(function (resolve, reject) {
69 var request = _superagent2.default[method](formatUrl(path));
70
71 params = params || {};
72 params._ = +new Date();
73 request.query(params);
74
75 if (__SERVER__ && req.get('cookie')) {
76 request.set('cookie', req.get('cookie'));
77 }
78
79 if (data) {
80 request.send(data);
81 }
82
83 request.end(function (err) {
84 var _ref2 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
85 body = _ref2.body;
86
87 return err ? reject(body || err) : resolve(body);
88 });
89 });
90 };
91 });
92 }
93 /*
94 * There's a V8 bug where, when using Babel, exporting classes with only
95 * constructors sometimes fails. Until it's patched, this is a solution to
96 * "ApiClient is not defined" from issue #14.
97 * https://github.com/erikras/react-redux-universal-hot-example/issues/14
98 *
99 * Relevant Babel bug (but they claim it's V8): https://phabricator.babeljs.io/T2455
100 *
101 * Remove it at your own risk.
102 */
103
104
105 (0, _createClass3.default)(ApiClient, [{
106 key: 'empty',
107 value: function empty() {}
108 }, {
109 key: '__reactstandin__regenerateByEval',
110 value: function __reactstandin__regenerateByEval(key, code) {
111 this[key] = eval(code);
112 }
113 }]);
114 return ApiClient;
115}();
116
117exports.default = ApiClient;
118;
119
120(function () {
121 var reactHotLoader = require('react-hot-loader').default;
122
123 var leaveModule = require('react-hot-loader').leaveModule;
124
125 if (!reactHotLoader) {
126 return;
127 }
128
129 reactHotLoader.register(methods, 'methods', '../../src/helpers/ApiClientSuperagent.js');
130 reactHotLoader.register(getProtocol, 'getProtocol', '../../src/helpers/ApiClientSuperagent.js');
131 reactHotLoader.register(formatUrl, 'formatUrl', '../../src/helpers/ApiClientSuperagent.js');
132 reactHotLoader.register(ApiClient, 'ApiClient', '../../src/helpers/ApiClientSuperagent.js');
133 leaveModule(module);
134})();
135
136;
137module.exports = exports['default'];
138//# sourceMappingURL=ApiClientSuperagent.js.map
\No newline at end of file