UNPKG

9.3 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
6
7var fusionCore = require('fusion-core');
8var fusionPluginUniversalEvents = require('fusion-plugin-universal-events');
9var fusionTokens = require('fusion-tokens');
10var mapObject = _interopDefault(require('just-map-object'));
11var isEqual = _interopDefault(require('just-compare'));
12
13/** Copyright (c) 2018 Uber Technologies, Inc.
14 *
15 * This source code is licensed under the MIT license found in the
16 * LICENSE file in the root directory of this source tree.
17 *
18 *
19 */
20var RPCToken = fusionCore.createToken('RPCToken');
21var RPCHandlersToken = fusionCore.createToken('RPCHandlersToken');
22var BodyParserOptionsToken = fusionCore.createToken('BodyParserOptionsToken');
23var RPCHandlersConfigToken = fusionCore.createToken('RPCHandlersConfigToken');
24
25var formatApiPath = function formatApiPath(apiPath) {
26 return ("/" + apiPath + "/").replace(/\/{2,}/g, '/');
27};
28
29function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
30
31function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
32
33/** Copyright (c) 2018 Uber Technologies, Inc.
34 *
35 * This source code is licensed under the MIT license found in the
36 * LICENSE file in the root directory of this source tree.
37 *
38 *
39 */
40
41/* eslint-env browser */
42var statKey = 'rpc:method-client';
43
44var RPC =
45/*#__PURE__*/
46function () {
47 function RPC(_ref) {
48 var fetch = _ref.fetch,
49 emitter = _ref.emitter,
50 rpcConfig = _ref.rpcConfig;
51 this.fetch = fetch;
52 this.config = rpcConfig || {};
53 this.emitter = emitter;
54 this.apiPath = formatApiPath(rpcConfig && rpcConfig.apiPath ? rpcConfig.apiPath : 'api');
55 }
56
57 var _proto = RPC.prototype;
58
59 _proto.request = function request(rpcId, args, headers) {
60 if (!this.fetch) {
61 throw new Error('fusion-plugin-rpc requires `fetch`');
62 }
63
64 if (!this.emitter) {
65 throw new Error('Missing emitter registered to UniversalEventsToken');
66 }
67
68 var fetch = this.fetch;
69 var emitter = this.emitter;
70 var apiPath = this.apiPath;
71 var startTime = Date.now(); // TODO(#3) handle args instanceof FormData
72
73 return fetch("" + apiPath + rpcId, {
74 method: 'POST',
75 headers: _objectSpread({
76 'Content-Type': 'application/json'
77 }, headers || {}),
78 body: JSON.stringify(args || {})
79 }).then(function (r) {
80 return r.json();
81 }).then(function (args) {
82 var status = args.status,
83 data = args.data;
84
85 if (status === 'success') {
86 emitter.emit(statKey, {
87 method: rpcId,
88 status: 'success',
89 timing: Date.now() - startTime
90 });
91 return data;
92 } else {
93 emitter.emit(statKey, {
94 method: rpcId,
95 error: data,
96 status: 'failure',
97 timing: Date.now() - startTime
98 });
99 return Promise.reject(data);
100 }
101 });
102 };
103
104 return RPC;
105}();
106
107var pluginFactory = function pluginFactory() {
108 return fusionCore.createPlugin({
109 deps: {
110 fetch: fusionTokens.FetchToken,
111 emitter: fusionPluginUniversalEvents.UniversalEventsToken,
112 rpcConfig: RPCHandlersConfigToken.optional
113 },
114 provides: function provides(deps) {
115 var _deps$fetch = deps.fetch,
116 fetch = _deps$fetch === void 0 ? window.fetch : _deps$fetch,
117 emitter = deps.emitter,
118 rpcConfig = deps.rpcConfig;
119 return {
120 from: function from() {
121 return new RPC({
122 fetch: fetch,
123 emitter: emitter,
124 rpcConfig: rpcConfig
125 });
126 }
127 };
128 }
129 });
130};
131
132var browserDataFetching = true && pluginFactory();
133
134function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
135
136/** Copyright (c) 2018 Uber Technologies, Inc.
137 *
138 * This source code is licensed under the MIT license found in the
139 * LICENSE file in the root directory of this source tree.
140 *
141 *
142 */
143var MissingHandlerError =
144/*#__PURE__*/
145function (_Error) {
146 _inheritsLoose(MissingHandlerError, _Error);
147
148 function MissingHandlerError(method) {
149 var _this;
150
151 _this = _Error.call(this, "Missing RPC handler for " + method) || this;
152 _this.code = 'ERR_MISSING_HANDLER';
153 Error.captureStackTrace(_this, MissingHandlerError);
154 return _this;
155 }
156
157 return MissingHandlerError;
158}(Error);
159
160function _inheritsLoose$1(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
161
162/** Copyright (c) 2018 Uber Technologies, Inc.
163 *
164 * This source code is licensed under the MIT license found in the
165 * LICENSE file in the root directory of this source tree.
166 *
167 *
168 */
169var ResponseError =
170/*#__PURE__*/
171function (_Error) {
172 _inheritsLoose$1(ResponseError, _Error);
173
174 function ResponseError(message) {
175 var _this;
176
177 _this = _Error.call(this, message) || this;
178 _this.code = null;
179 _this.meta = null;
180 Error.captureStackTrace(_this, ResponseError);
181 return _this;
182 }
183
184 return ResponseError;
185}(Error);
186
187/** Copyright (c) 2018 Uber Technologies, Inc.
188 *
189 * This source code is licensed under the MIT license found in the
190 * LICENSE file in the root directory of this source tree.
191 *
192 *
193 */
194
195/* eslint-env node */
196
197/** Copyright (c) 2018 Uber Technologies, Inc.
198 *
199 * This source code is licensed under the MIT license found in the
200 * LICENSE file in the root directory of this source tree.
201 *
202 *
203 */
204var RPC$2 =
205/*#__PURE__*/
206function () {
207 function RPC(handlers) {
208 this.handlers = handlers;
209 }
210
211 var _proto = RPC.prototype;
212
213 _proto.request = function request(method, args) {
214 return new Promise(function ($return, $error) {
215 if (!this.handlers) {
216 return $error(new Error('fusion-plugin-rpc requires `handlers`'));
217 }
218
219 if (!this.handlers[method]) {
220 return $error(new MissingHandlerError(method));
221 }
222
223 return $return(this.handlers[method](args));
224 }.bind(this));
225 };
226
227 return RPC;
228}();
229
230var plugin = fusionCore.createPlugin({
231 deps: {
232 handlers: RPCHandlersToken,
233 emitter: fusionPluginUniversalEvents.UniversalEventsToken
234 },
235 provides: function provides(_temp) {
236 var _ref = _temp === void 0 ? {} : _temp,
237 handlers = _ref.handlers;
238
239 return {
240 from: function from() {
241 return new RPC$2(handlers);
242 }
243 };
244 }
245});
246
247function _objectSpread$1(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty$1(target, key, source[key]); }); } return target; }
248
249function _defineProperty$1(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
250
251var getMockRpcHandlers = function getMockRpcHandlers(fixtures, onMockRpc) {
252 return fixtures.reduce(function (rpcHandlers, fixture) {
253 return _objectSpread$1({}, rpcHandlers, mapObject(fixture, function (rpcId, responseDetails) {
254 return function () {
255 var $args = arguments;
256 return new Promise(function ($return, $error) {
257 for (var _len = $args.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
258 args[_key] = $args[_key];
259 }
260
261 var response = Array.isArray(responseDetails) ? responseDetails.filter(function (item) {
262 return isEqual(item.args, args);
263 })[0].response : responseDetails;
264 onMockRpc && onMockRpc(rpcId, args, response);
265
266 if (response instanceof Error) {
267 return $error(response);
268 }
269
270 return $return(response);
271 });
272 };
273 }));
274 }, {});
275};
276
277/** Copyright (c) 2018 Uber Technologies, Inc.
278 *
279 * This source code is licensed under the MIT license found in the
280 * LICENSE file in the root directory of this source tree.
281 *
282 *
283 */
284var index = browserDataFetching;
285
286exports.default = index;
287exports.mock = plugin;
288exports.ResponseError = ResponseError;
289exports.BodyParserOptionsToken = BodyParserOptionsToken;
290exports.RPCToken = RPCToken;
291exports.RPCHandlersToken = RPCHandlersToken;
292exports.RPCHandlersConfigToken = RPCHandlersConfigToken;
293exports.getMockRpcHandlers = getMockRpcHandlers;