1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
6 |
|
7 | var fusionCore = require('fusion-core');
|
8 | var fusionPluginUniversalEvents = require('fusion-plugin-universal-events');
|
9 | var fusionTokens = require('fusion-tokens');
|
10 | var mapObject = _interopDefault(require('just-map-object'));
|
11 | var isEqual = _interopDefault(require('just-compare'));
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | var RPCToken = fusionCore.createToken('RPCToken');
|
21 | var RPCHandlersToken = fusionCore.createToken('RPCHandlersToken');
|
22 | var BodyParserOptionsToken = fusionCore.createToken('BodyParserOptionsToken');
|
23 | var RPCHandlersConfigToken = fusionCore.createToken('RPCHandlersConfigToken');
|
24 |
|
25 | var formatApiPath = function formatApiPath(apiPath) {
|
26 | return ("/" + apiPath + "/").replace(/\/{2,}/g, '/');
|
27 | };
|
28 |
|
29 | function _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 |
|
31 | function _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 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | var statKey = 'rpc:method-client';
|
43 |
|
44 | var RPC =
|
45 |
|
46 | function () {
|
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();
|
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 |
|
107 | var 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 |
|
132 | var browserDataFetching = true && pluginFactory();
|
133 |
|
134 | function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 | var MissingHandlerError =
|
144 |
|
145 | function (_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 |
|
160 | function _inheritsLoose$1(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 | var ResponseError =
|
170 |
|
171 | function (_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 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 | var RPC$2 =
|
205 |
|
206 | function () {
|
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 |
|
230 | var 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 |
|
247 | function _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 |
|
249 | function _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 |
|
251 | var 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 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 | var index = browserDataFetching;
|
285 |
|
286 | exports.default = index;
|
287 | exports.mock = plugin;
|
288 | exports.ResponseError = ResponseError;
|
289 | exports.BodyParserOptionsToken = BodyParserOptionsToken;
|
290 | exports.RPCToken = RPCToken;
|
291 | exports.RPCHandlersToken = RPCHandlersToken;
|
292 | exports.RPCHandlersConfigToken = RPCHandlersConfigToken;
|
293 | exports.getMockRpcHandlers = getMockRpcHandlers;
|