1 | import { createToken, createPlugin } from 'fusion-core';
|
2 | import { UniversalEventsToken } from 'fusion-plugin-universal-events';
|
3 | import { FetchToken } from 'fusion-tokens';
|
4 | import mapObject from 'just-map-object';
|
5 | import isEqual from 'just-compare';
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | const RPCToken = createToken('RPCToken');
|
15 | const RPCHandlersToken = createToken('RPCHandlersToken');
|
16 | const BodyParserOptionsToken = createToken('BodyParserOptionsToken');
|
17 | const RPCHandlersConfigToken = createToken('RPCHandlersConfigToken');
|
18 |
|
19 | const formatApiPath = apiPath => `/${apiPath}/`.replace(/\/{2,}/g, '/');
|
20 |
|
21 | 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; }
|
22 |
|
23 | 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; }
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | const statKey = 'rpc:method-client';
|
35 |
|
36 | class RPC {
|
37 | constructor({
|
38 | fetch,
|
39 | emitter,
|
40 | rpcConfig
|
41 | }) {
|
42 | this.fetch = fetch;
|
43 | this.config = rpcConfig || {};
|
44 | this.emitter = emitter;
|
45 | this.apiPath = formatApiPath(rpcConfig && rpcConfig.apiPath ? rpcConfig.apiPath : 'api');
|
46 | }
|
47 |
|
48 | request(rpcId, args, headers) {
|
49 | if (!this.fetch) {
|
50 | throw new Error('fusion-plugin-rpc requires `fetch`');
|
51 | }
|
52 |
|
53 | if (!this.emitter) {
|
54 | throw new Error('Missing emitter registered to UniversalEventsToken');
|
55 | }
|
56 |
|
57 | const fetch = this.fetch;
|
58 | const emitter = this.emitter;
|
59 | const apiPath = this.apiPath;
|
60 | const startTime = Date.now();
|
61 |
|
62 | return fetch(`${apiPath}${rpcId}`, {
|
63 | method: 'POST',
|
64 | headers: _objectSpread({
|
65 | 'Content-Type': 'application/json'
|
66 | }, headers || {}),
|
67 | body: JSON.stringify(args || {})
|
68 | }).then(r => r.json()).then(args => {
|
69 | const {
|
70 | status,
|
71 | data
|
72 | } = args;
|
73 |
|
74 | if (status === 'success') {
|
75 | emitter.emit(statKey, {
|
76 | method: rpcId,
|
77 | status: 'success',
|
78 | timing: Date.now() - startTime
|
79 | });
|
80 | return data;
|
81 | } else {
|
82 | emitter.emit(statKey, {
|
83 | method: rpcId,
|
84 | error: data,
|
85 | status: 'failure',
|
86 | timing: Date.now() - startTime
|
87 | });
|
88 | return Promise.reject(data);
|
89 | }
|
90 | });
|
91 | }
|
92 |
|
93 | }
|
94 |
|
95 | const pluginFactory = () => createPlugin({
|
96 | deps: {
|
97 | fetch: FetchToken,
|
98 | emitter: UniversalEventsToken,
|
99 | rpcConfig: RPCHandlersConfigToken.optional
|
100 | },
|
101 | provides: deps => {
|
102 | const {
|
103 | fetch = window.fetch,
|
104 | emitter,
|
105 | rpcConfig
|
106 | } = deps;
|
107 | return {
|
108 | from: () => new RPC({
|
109 | fetch,
|
110 | emitter,
|
111 | rpcConfig
|
112 | })
|
113 | };
|
114 | }
|
115 | });
|
116 |
|
117 | var browserDataFetching = true && pluginFactory();
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 | class MissingHandlerError extends Error {
|
127 | constructor(method) {
|
128 | super(`Missing RPC handler for ${method}`);
|
129 | this.code = 'ERR_MISSING_HANDLER';
|
130 | Error.captureStackTrace(this, MissingHandlerError);
|
131 | }
|
132 |
|
133 | }
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 | class ResponseError extends Error {
|
143 | constructor(message) {
|
144 | super(message);
|
145 | this.code = null;
|
146 | this.meta = null;
|
147 | Error.captureStackTrace(this, ResponseError);
|
148 | }
|
149 |
|
150 | }
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 | class RPC$2 {
|
170 | constructor(handlers) {
|
171 | this.handlers = handlers;
|
172 | }
|
173 |
|
174 | async request(method, args) {
|
175 | if (!this.handlers) {
|
176 | throw new Error('fusion-plugin-rpc requires `handlers`');
|
177 | }
|
178 |
|
179 | if (!this.handlers[method]) {
|
180 | throw new MissingHandlerError(method);
|
181 | }
|
182 |
|
183 | return this.handlers[method](args);
|
184 | }
|
185 |
|
186 | }
|
187 |
|
188 | const plugin = createPlugin({
|
189 | deps: {
|
190 | handlers: RPCHandlersToken,
|
191 | emitter: UniversalEventsToken
|
192 | },
|
193 | provides: ({
|
194 | handlers
|
195 | } = {}) => {
|
196 | return {
|
197 | from: () => new RPC$2(handlers)
|
198 | };
|
199 | }
|
200 | });
|
201 |
|
202 | 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; }
|
203 |
|
204 | 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; }
|
205 |
|
206 | const getMockRpcHandlers = (fixtures, onMockRpc) => fixtures.reduce((rpcHandlers, fixture) => _objectSpread$1({}, rpcHandlers, mapObject(fixture, (rpcId, responseDetails) => async (...args) => {
|
207 | const response = Array.isArray(responseDetails) ? responseDetails.filter(item => isEqual(item.args, args))[0].response : responseDetails;
|
208 | onMockRpc && onMockRpc(rpcId, args, response);
|
209 |
|
210 | if (response instanceof Error) {
|
211 | throw response;
|
212 | }
|
213 |
|
214 | return response;
|
215 | })), {});
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 | var index = browserDataFetching;
|
225 |
|
226 | export default index;
|
227 | export { plugin as mock, ResponseError, BodyParserOptionsToken, RPCToken, RPCHandlersToken, RPCHandlersConfigToken, getMockRpcHandlers };
|