1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = undefined;
|
7 |
|
8 | var _promise = require('babel-runtime/core-js/promise');
|
9 |
|
10 | var _promise2 = _interopRequireDefault(_promise);
|
11 |
|
12 | var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
|
13 |
|
14 | var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
|
15 |
|
16 | var _createClass2 = require('babel-runtime/helpers/createClass');
|
17 |
|
18 | var _createClass3 = _interopRequireDefault(_createClass2);
|
19 |
|
20 | var _superagent = require('superagent');
|
21 |
|
22 | var _superagent2 = _interopRequireDefault(_superagent);
|
23 |
|
24 | var _config = require('../config');
|
25 |
|
26 | var _config2 = _interopRequireDefault(_config);
|
27 |
|
28 | function _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 |
|
36 | var methods = ['get', 'post', 'put', 'patch', 'del'];
|
37 |
|
38 | function getProtocol(req) {
|
39 | return "http:";
|
40 |
|
41 | }
|
42 | function 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 |
|
56 | var 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 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
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 |
|
117 | exports.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 | ;
|
137 | module.exports = exports['default'];
|
138 |
|
\ | No newline at end of file |