@cuipengyu5/bling-renderer-core 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/es/adapter/index.d.ts +24 -0
- package/es/adapter/index.js +95 -0
- package/es/components/Div.d.ts +2 -0
- package/es/components/Div.js +19 -0
- package/es/components/VisualDom/index.css +19 -0
- package/es/components/VisualDom/index.d.ts +3 -0
- package/es/components/VisualDom/index.js +44 -0
- package/es/context/index.d.ts +1 -0
- package/es/context/index.js +11 -0
- package/es/hoc/index.d.ts +7 -0
- package/es/hoc/index.js +90 -0
- package/es/hoc/leaf.d.ts +32 -0
- package/es/hoc/leaf.js +521 -0
- package/es/index.d.ts +7 -0
- package/es/index.js +9 -0
- package/es/renderer/addon.d.ts +2 -0
- package/es/renderer/addon.js +107 -0
- package/es/renderer/base.d.ts +13 -0
- package/es/renderer/base.js +1120 -0
- package/es/renderer/block.d.ts +2 -0
- package/es/renderer/block.js +43 -0
- package/es/renderer/component.d.ts +2 -0
- package/es/renderer/component.js +59 -0
- package/es/renderer/index.d.ts +8 -0
- package/es/renderer/index.js +8 -0
- package/es/renderer/page.d.ts +2 -0
- package/es/renderer/page.js +91 -0
- package/es/renderer/renderer.d.ts +2 -0
- package/es/renderer/renderer.js +230 -0
- package/es/renderer/temp.d.ts +2 -0
- package/es/renderer/temp.js +139 -0
- package/es/style.js +1 -0
- package/es/types/index.d.ts +275 -0
- package/es/types/index.js +1 -0
- package/es/utils/common.d.ts +119 -0
- package/es/utils/common.js +378 -0
- package/es/utils/data-helper.d.ts +74 -0
- package/es/utils/data-helper.js +300 -0
- package/es/utils/index.d.ts +3 -0
- package/es/utils/index.js +3 -0
- package/es/utils/is-use-loop.d.ts +2 -0
- package/es/utils/is-use-loop.js +16 -0
- package/es/utils/logger.d.ts +3 -0
- package/es/utils/logger.js +5 -0
- package/es/utils/request.d.ts +54 -0
- package/es/utils/request.js +209 -0
- package/lib/adapter/index.d.ts +24 -0
- package/lib/adapter/index.js +99 -0
- package/lib/components/Div.d.ts +2 -0
- package/lib/components/Div.js +24 -0
- package/lib/components/VisualDom/index.css +19 -0
- package/lib/components/VisualDom/index.d.ts +3 -0
- package/lib/components/VisualDom/index.js +49 -0
- package/lib/context/index.d.ts +1 -0
- package/lib/context/index.js +16 -0
- package/lib/hoc/index.d.ts +7 -0
- package/lib/hoc/index.js +95 -0
- package/lib/hoc/leaf.d.ts +32 -0
- package/lib/hoc/leaf.js +526 -0
- package/lib/index.d.ts +7 -0
- package/lib/index.js +34 -0
- package/lib/renderer/addon.d.ts +2 -0
- package/lib/renderer/addon.js +112 -0
- package/lib/renderer/base.d.ts +13 -0
- package/lib/renderer/base.js +1126 -0
- package/lib/renderer/block.d.ts +2 -0
- package/lib/renderer/block.js +48 -0
- package/lib/renderer/component.d.ts +2 -0
- package/lib/renderer/component.js +64 -0
- package/lib/renderer/index.d.ts +8 -0
- package/lib/renderer/index.js +18 -0
- package/lib/renderer/page.d.ts +2 -0
- package/lib/renderer/page.js +96 -0
- package/lib/renderer/renderer.d.ts +2 -0
- package/lib/renderer/renderer.js +235 -0
- package/lib/renderer/temp.d.ts +2 -0
- package/lib/renderer/temp.js +144 -0
- package/lib/style.js +1 -0
- package/lib/types/index.d.ts +275 -0
- package/lib/types/index.js +3 -0
- package/lib/utils/common.d.ts +119 -0
- package/lib/utils/common.js +409 -0
- package/lib/utils/data-helper.d.ts +74 -0
- package/lib/utils/data-helper.js +306 -0
- package/lib/utils/index.d.ts +3 -0
- package/lib/utils/index.js +21 -0
- package/lib/utils/is-use-loop.d.ts +2 -0
- package/lib/utils/is-use-loop.js +19 -0
- package/lib/utils/logger.d.ts +3 -0
- package/lib/utils/logger.js +9 -0
- package/lib/utils/request.d.ts +54 -0
- package/lib/utils/request.js +217 -0
- package/package.json +64 -0
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
import _extends from "@babel/runtime/helpers/extends";
|
|
2
|
+
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/objectWithoutPropertiesLoose";
|
|
3
|
+
var _excluded = ["uri", "url", "method", "headers", "params"],
|
|
4
|
+
_excluded2 = ["headers"];
|
|
5
|
+
/* eslint-disable no-console */
|
|
6
|
+
/* eslint-disable max-len */
|
|
7
|
+
/* eslint-disable object-curly-newline */
|
|
8
|
+
import { isJSFunction } from '@alilc/lowcode-utils';
|
|
9
|
+
import { transformArrayToMap, transformStringToFunction } from './common';
|
|
10
|
+
import { jsonp, request, get, post } from './request';
|
|
11
|
+
import logger from './logger';
|
|
12
|
+
var DS_STATUS = {
|
|
13
|
+
INIT: 'init',
|
|
14
|
+
LOADING: 'loading',
|
|
15
|
+
LOADED: 'loaded',
|
|
16
|
+
ERROR: 'error'
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* do request for standard DataSourceType
|
|
20
|
+
* @param {DataSourceType} type type of DataSourceItem
|
|
21
|
+
* @param {any} options
|
|
22
|
+
*/
|
|
23
|
+
export function doRequest(type, options) {
|
|
24
|
+
// eslint-disable-next-line prefer-const
|
|
25
|
+
var uri = options.uri,
|
|
26
|
+
url = options.url,
|
|
27
|
+
_options$method = options.method,
|
|
28
|
+
method = _options$method === void 0 ? 'GET' : _options$method,
|
|
29
|
+
headers = options.headers,
|
|
30
|
+
params = options.params,
|
|
31
|
+
otherProps = _objectWithoutPropertiesLoose(options, _excluded);
|
|
32
|
+
otherProps = otherProps || {};
|
|
33
|
+
if (type === 'jsonp') {
|
|
34
|
+
return jsonp(uri, params, otherProps);
|
|
35
|
+
}
|
|
36
|
+
if (type === 'fetch') {
|
|
37
|
+
switch (method.toUpperCase()) {
|
|
38
|
+
case 'GET':
|
|
39
|
+
return get(uri, params, headers, otherProps);
|
|
40
|
+
case 'POST':
|
|
41
|
+
return post(uri, params, headers, otherProps);
|
|
42
|
+
default:
|
|
43
|
+
return request(uri, method, params, headers, otherProps);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
logger.log("Engine default dataSource does not support type:[" + type + "] dataSource request!", options);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// TODO: according to protocol, we should implement errorHandler/shouldFetch/willFetch/requestHandler and isSync controll.
|
|
50
|
+
export var DataHelper = /*#__PURE__*/function () {
|
|
51
|
+
function DataHelper(comp, config, appHelper, parser) {
|
|
52
|
+
/**
|
|
53
|
+
* host object that will be "this" object when excuting dataHandler
|
|
54
|
+
*
|
|
55
|
+
* @type {*}
|
|
56
|
+
* @memberof DataHelper
|
|
57
|
+
*/
|
|
58
|
+
this.host = void 0;
|
|
59
|
+
/**
|
|
60
|
+
* data source config
|
|
61
|
+
*
|
|
62
|
+
* @type {DataSource}
|
|
63
|
+
* @memberof DataHelper
|
|
64
|
+
*/
|
|
65
|
+
this.config = void 0;
|
|
66
|
+
/**
|
|
67
|
+
* a parser function which will be called to process config data
|
|
68
|
+
* which eventually will call common/utils.processData() to process data
|
|
69
|
+
* (originalConfig) => parsedConfig
|
|
70
|
+
* @type {*}
|
|
71
|
+
* @memberof DataHelper
|
|
72
|
+
*/
|
|
73
|
+
this.parser = void 0;
|
|
74
|
+
/**
|
|
75
|
+
* config.list
|
|
76
|
+
*
|
|
77
|
+
* @type {any[]}
|
|
78
|
+
* @memberof DataHelper
|
|
79
|
+
*/
|
|
80
|
+
this.ajaxList = void 0;
|
|
81
|
+
this.ajaxMap = void 0;
|
|
82
|
+
this.dataSourceMap = void 0;
|
|
83
|
+
this.appHelper = void 0;
|
|
84
|
+
this.host = comp;
|
|
85
|
+
this.config = config || {};
|
|
86
|
+
this.parser = parser;
|
|
87
|
+
this.ajaxList = (config === null || config === void 0 ? void 0 : config.list) || [];
|
|
88
|
+
this.ajaxMap = transformArrayToMap(this.ajaxList, 'id');
|
|
89
|
+
this.dataSourceMap = this.generateDataSourceMap();
|
|
90
|
+
this.appHelper = appHelper;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// 更新config,只会更新配置,状态保存;
|
|
94
|
+
var _proto = DataHelper.prototype;
|
|
95
|
+
_proto.updateConfig = function updateConfig(config) {
|
|
96
|
+
var _config,
|
|
97
|
+
_this = this;
|
|
98
|
+
if (config === void 0) {
|
|
99
|
+
config = {};
|
|
100
|
+
}
|
|
101
|
+
this.config = config;
|
|
102
|
+
this.ajaxList = ((_config = config) === null || _config === void 0 ? void 0 : _config.list) || [];
|
|
103
|
+
var ajaxMap = transformArrayToMap(this.ajaxList, 'id');
|
|
104
|
+
// 删除已经移除的接口
|
|
105
|
+
Object.keys(this.ajaxMap).forEach(function (key) {
|
|
106
|
+
if (!ajaxMap[key]) {
|
|
107
|
+
delete _this.dataSourceMap[key];
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
this.ajaxMap = ajaxMap;
|
|
111
|
+
// 添加未加入到dataSourceMap中的接口
|
|
112
|
+
this.ajaxList.forEach(function (item) {
|
|
113
|
+
if (!_this.dataSourceMap[item.id]) {
|
|
114
|
+
_this.dataSourceMap[item.id] = {
|
|
115
|
+
status: DS_STATUS.INIT,
|
|
116
|
+
load: function load() {
|
|
117
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
118
|
+
args[_key] = arguments[_key];
|
|
119
|
+
}
|
|
120
|
+
// @ts-ignore
|
|
121
|
+
return _this.getDataSource.apply(_this, [item.id].concat(args));
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
return this.dataSourceMap;
|
|
127
|
+
};
|
|
128
|
+
_proto.generateDataSourceMap = function generateDataSourceMap() {
|
|
129
|
+
var _this2 = this;
|
|
130
|
+
var res = {};
|
|
131
|
+
this.ajaxList.forEach(function (item) {
|
|
132
|
+
res[item.id] = {
|
|
133
|
+
status: DS_STATUS.INIT,
|
|
134
|
+
load: function load() {
|
|
135
|
+
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
|
|
136
|
+
args[_key2] = arguments[_key2];
|
|
137
|
+
}
|
|
138
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
139
|
+
// @ts-ignore
|
|
140
|
+
return _this2.getDataSource.apply(_this2, [item.id].concat(args));
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
});
|
|
144
|
+
return res;
|
|
145
|
+
};
|
|
146
|
+
_proto.updateDataSourceMap = function updateDataSourceMap(id, data, error) {
|
|
147
|
+
this.dataSourceMap[id].error = error || undefined;
|
|
148
|
+
this.dataSourceMap[id].data = data;
|
|
149
|
+
this.dataSourceMap[id].status = error ? DS_STATUS.ERROR : DS_STATUS.LOADED;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* get all dataSourceItems which marked as isInit === true
|
|
154
|
+
* @private
|
|
155
|
+
* @returns
|
|
156
|
+
* @memberof DataHelper
|
|
157
|
+
*/;
|
|
158
|
+
_proto.getInitDataSourseConfigs = function getInitDataSourseConfigs() {
|
|
159
|
+
var _this3 = this;
|
|
160
|
+
var initConfigs = this.parser(this.ajaxList).filter(function (item) {
|
|
161
|
+
// according to [spec](https://lowcode-engine.cn/lowcode), isInit should be boolean true to be working
|
|
162
|
+
if (item.isInit === true) {
|
|
163
|
+
_this3.dataSourceMap[item.id].status = DS_STATUS.LOADING;
|
|
164
|
+
return true;
|
|
165
|
+
}
|
|
166
|
+
return false;
|
|
167
|
+
});
|
|
168
|
+
return initConfigs;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* process all dataSourceItems which marked as isInit === true, and get dataSource request results.
|
|
173
|
+
* @public
|
|
174
|
+
* @returns
|
|
175
|
+
* @memberof DataHelper
|
|
176
|
+
*/;
|
|
177
|
+
_proto.getInitData = function getInitData() {
|
|
178
|
+
var _this4 = this;
|
|
179
|
+
var initSyncData = this.getInitDataSourseConfigs();
|
|
180
|
+
// 所有 datasource 的 datahandler
|
|
181
|
+
return this.asyncDataHandler(initSyncData).then(function (res) {
|
|
182
|
+
var dataHandler = _this4.config.dataHandler;
|
|
183
|
+
return _this4.handleData(null, dataHandler, res, null);
|
|
184
|
+
});
|
|
185
|
+
};
|
|
186
|
+
_proto.getDataSource = function getDataSource(id, params, otherOptions, callback) {
|
|
187
|
+
var req = this.parser(this.ajaxMap[id]);
|
|
188
|
+
var options = req.options || {};
|
|
189
|
+
var callbackFn = callback;
|
|
190
|
+
var otherOptionsObj = otherOptions;
|
|
191
|
+
if (typeof otherOptions === 'function') {
|
|
192
|
+
callbackFn = otherOptions;
|
|
193
|
+
otherOptionsObj = {};
|
|
194
|
+
}
|
|
195
|
+
var _ref = otherOptionsObj || {},
|
|
196
|
+
headers = _ref.headers,
|
|
197
|
+
otherProps = _objectWithoutPropertiesLoose(_ref, _excluded2);
|
|
198
|
+
if (!req) {
|
|
199
|
+
logger.warn("getDataSource API named " + id + " not exist");
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
return this.asyncDataHandler([_extends({}, req, {
|
|
203
|
+
options: _extends({}, options, {
|
|
204
|
+
// 支持参数为array的情况,当参数为array时,不做参数合并
|
|
205
|
+
params: Array.isArray(options.params) || Array.isArray(params) ? params || options.params : _extends({}, options.params, params),
|
|
206
|
+
headers: _extends({}, options.headers, headers)
|
|
207
|
+
}, otherProps)
|
|
208
|
+
})]).then(function (res) {
|
|
209
|
+
try {
|
|
210
|
+
callbackFn && callbackFn(res && res[id]);
|
|
211
|
+
} catch (e) {
|
|
212
|
+
logger.error('load请求回调函数报错', e);
|
|
213
|
+
}
|
|
214
|
+
return res && res[id];
|
|
215
|
+
})["catch"](function (err) {
|
|
216
|
+
try {
|
|
217
|
+
callbackFn && callbackFn(null, err);
|
|
218
|
+
} catch (e) {
|
|
219
|
+
logger.error('load请求回调函数报错', e);
|
|
220
|
+
}
|
|
221
|
+
return err;
|
|
222
|
+
});
|
|
223
|
+
};
|
|
224
|
+
_proto.asyncDataHandler = function asyncDataHandler(asyncDataList) {
|
|
225
|
+
var _this5 = this;
|
|
226
|
+
return new Promise(function (resolve, reject) {
|
|
227
|
+
var allReq = [];
|
|
228
|
+
asyncDataList.forEach(function (req) {
|
|
229
|
+
var id = req.id,
|
|
230
|
+
type = req.type;
|
|
231
|
+
// TODO: need refactoring to remove 'legao' related logic
|
|
232
|
+
if (!id || !type || type === 'legao') {
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
allReq.push(req);
|
|
236
|
+
});
|
|
237
|
+
if (allReq.length === 0) {
|
|
238
|
+
resolve({});
|
|
239
|
+
}
|
|
240
|
+
var res = {};
|
|
241
|
+
Promise.all(allReq.map(function (item) {
|
|
242
|
+
return new Promise(function (innerResolve) {
|
|
243
|
+
var type = item.type,
|
|
244
|
+
id = item.id,
|
|
245
|
+
dataHandler = item.dataHandler,
|
|
246
|
+
options = item.options;
|
|
247
|
+
var fetchHandler = function fetchHandler(data, error) {
|
|
248
|
+
res[id] = _this5.handleData(id, dataHandler, data, error);
|
|
249
|
+
_this5.updateDataSourceMap(id, res[id], error);
|
|
250
|
+
innerResolve({});
|
|
251
|
+
};
|
|
252
|
+
var doFetch = function doFetch(innerType, innerOptions) {
|
|
253
|
+
var _doRequest;
|
|
254
|
+
(_doRequest = doRequest(innerType, innerOptions)) === null || _doRequest === void 0 ? void 0 : _doRequest.then(function (data) {
|
|
255
|
+
fetchHandler(data, undefined);
|
|
256
|
+
})["catch"](function (err) {
|
|
257
|
+
fetchHandler(undefined, err);
|
|
258
|
+
});
|
|
259
|
+
};
|
|
260
|
+
_this5.dataSourceMap[id].status = DS_STATUS.LOADING;
|
|
261
|
+
doFetch(type, options);
|
|
262
|
+
});
|
|
263
|
+
})).then(function () {
|
|
264
|
+
resolve(res);
|
|
265
|
+
})["catch"](function (e) {
|
|
266
|
+
reject(e);
|
|
267
|
+
});
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* process data using dataHandler
|
|
273
|
+
*
|
|
274
|
+
* @param {(string | null)} id request id, will be used in error message, can be null
|
|
275
|
+
* @param {*} dataHandler
|
|
276
|
+
* @param {*} data
|
|
277
|
+
* @param {*} error
|
|
278
|
+
* @returns
|
|
279
|
+
* @memberof DataHelper
|
|
280
|
+
*/;
|
|
281
|
+
_proto.handleData = function handleData(id, dataHandler, data, error) {
|
|
282
|
+
var dataHandlerFun = dataHandler;
|
|
283
|
+
if (isJSFunction(dataHandler)) {
|
|
284
|
+
dataHandlerFun = transformStringToFunction(dataHandler.value);
|
|
285
|
+
}
|
|
286
|
+
if (!dataHandlerFun || typeof dataHandlerFun !== 'function') {
|
|
287
|
+
return data;
|
|
288
|
+
}
|
|
289
|
+
try {
|
|
290
|
+
return dataHandlerFun.call(this.host, data, error);
|
|
291
|
+
} catch (e) {
|
|
292
|
+
if (id) {
|
|
293
|
+
logger.error("[" + id + "]\u5355\u4E2A\u8BF7\u6C42\u6570\u636E\u5904\u7406\u51FD\u6570\u8FD0\u884C\u51FA\u9519", e);
|
|
294
|
+
} else {
|
|
295
|
+
logger.error('请求数据处理函数运行出错', e);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
};
|
|
299
|
+
return DataHelper;
|
|
300
|
+
}();
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { isJSExpression } from '@alilc/lowcode-utils';
|
|
2
|
+
|
|
3
|
+
// 1.渲染模式下,loop 是数组,则按照数组长度渲染组件
|
|
4
|
+
// 2.设计模式下,loop 需要长度大于 0,按照循环模式渲染,防止无法设计的情况
|
|
5
|
+
export default function isUseLoop(loop, isDesignMode) {
|
|
6
|
+
if (isJSExpression(loop)) {
|
|
7
|
+
return true;
|
|
8
|
+
}
|
|
9
|
+
if (!isDesignMode) {
|
|
10
|
+
return true;
|
|
11
|
+
}
|
|
12
|
+
if (!Array.isArray(loop)) {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
return loop.length > 0;
|
|
16
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import 'whatwg-fetch';
|
|
2
|
+
/**
|
|
3
|
+
* this is a private method, export for testing purposes only.
|
|
4
|
+
*
|
|
5
|
+
* @export
|
|
6
|
+
* @param {*} dataAPI
|
|
7
|
+
* @param {*} params
|
|
8
|
+
* @returns
|
|
9
|
+
*/
|
|
10
|
+
export declare function buildUrl(dataAPI: any, params: any): any;
|
|
11
|
+
/**
|
|
12
|
+
* do Get request
|
|
13
|
+
*
|
|
14
|
+
* @export
|
|
15
|
+
* @param {*} dataAPI
|
|
16
|
+
* @param {*} [params={}]
|
|
17
|
+
* @param {*} [headers={}]
|
|
18
|
+
* @param {*} [otherProps={}]
|
|
19
|
+
* @returns
|
|
20
|
+
*/
|
|
21
|
+
export declare function get(dataAPI: any, params?: {}, headers?: {}, otherProps?: {}): Promise<unknown>;
|
|
22
|
+
/**
|
|
23
|
+
* do Post request
|
|
24
|
+
*
|
|
25
|
+
* @export
|
|
26
|
+
* @param {*} dataAPI
|
|
27
|
+
* @param {*} [params={}]
|
|
28
|
+
* @param {*} [headers={}]
|
|
29
|
+
* @param {*} [otherProps={}]
|
|
30
|
+
* @returns
|
|
31
|
+
*/
|
|
32
|
+
export declare function post(dataAPI: any, params?: {}, headers?: any, otherProps?: {}): Promise<unknown>;
|
|
33
|
+
/**
|
|
34
|
+
* do request
|
|
35
|
+
*
|
|
36
|
+
* @export
|
|
37
|
+
* @param {*} dataAPI
|
|
38
|
+
* @param {string} [method='GET']
|
|
39
|
+
* @param {*} data
|
|
40
|
+
* @param {*} [headers={}]
|
|
41
|
+
* @param {*} [otherProps={}]
|
|
42
|
+
* @returns
|
|
43
|
+
*/
|
|
44
|
+
export declare function request(dataAPI: any, method: string, data: any, headers?: {}, otherProps?: any): Promise<unknown>;
|
|
45
|
+
/**
|
|
46
|
+
* do jsonp request
|
|
47
|
+
*
|
|
48
|
+
* @export
|
|
49
|
+
* @param {*} dataAPI
|
|
50
|
+
* @param {*} [params={}]
|
|
51
|
+
* @param {*} [otherProps={}]
|
|
52
|
+
* @returns
|
|
53
|
+
*/
|
|
54
|
+
export declare function jsonp(dataAPI: any, params?: {}, otherProps?: {}): Promise<unknown>;
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import _extends from "@babel/runtime/helpers/extends";
|
|
2
|
+
import 'whatwg-fetch';
|
|
3
|
+
import fetchJsonp from 'fetch-jsonp';
|
|
4
|
+
import { serializeParams } from '.';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* this is a private method, export for testing purposes only.
|
|
8
|
+
*
|
|
9
|
+
* @export
|
|
10
|
+
* @param {*} dataAPI
|
|
11
|
+
* @param {*} params
|
|
12
|
+
* @returns
|
|
13
|
+
*/
|
|
14
|
+
export function buildUrl(dataAPI, params) {
|
|
15
|
+
var paramStr = serializeParams(params);
|
|
16
|
+
if (paramStr) {
|
|
17
|
+
return dataAPI.indexOf('?') > 0 ? dataAPI + "&" + paramStr : dataAPI + "?" + paramStr;
|
|
18
|
+
}
|
|
19
|
+
return dataAPI;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* do Get request
|
|
24
|
+
*
|
|
25
|
+
* @export
|
|
26
|
+
* @param {*} dataAPI
|
|
27
|
+
* @param {*} [params={}]
|
|
28
|
+
* @param {*} [headers={}]
|
|
29
|
+
* @param {*} [otherProps={}]
|
|
30
|
+
* @returns
|
|
31
|
+
*/
|
|
32
|
+
export function get(dataAPI, params, headers, otherProps) {
|
|
33
|
+
if (params === void 0) {
|
|
34
|
+
params = {};
|
|
35
|
+
}
|
|
36
|
+
if (headers === void 0) {
|
|
37
|
+
headers = {};
|
|
38
|
+
}
|
|
39
|
+
if (otherProps === void 0) {
|
|
40
|
+
otherProps = {};
|
|
41
|
+
}
|
|
42
|
+
var processedHeaders = _extends({
|
|
43
|
+
Accept: 'application/json'
|
|
44
|
+
}, headers);
|
|
45
|
+
var url = buildUrl(dataAPI, params);
|
|
46
|
+
return request(url, 'GET', null, processedHeaders, otherProps);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* do Post request
|
|
51
|
+
*
|
|
52
|
+
* @export
|
|
53
|
+
* @param {*} dataAPI
|
|
54
|
+
* @param {*} [params={}]
|
|
55
|
+
* @param {*} [headers={}]
|
|
56
|
+
* @param {*} [otherProps={}]
|
|
57
|
+
* @returns
|
|
58
|
+
*/
|
|
59
|
+
export function post(dataAPI, params, headers, otherProps) {
|
|
60
|
+
if (params === void 0) {
|
|
61
|
+
params = {};
|
|
62
|
+
}
|
|
63
|
+
if (headers === void 0) {
|
|
64
|
+
headers = {};
|
|
65
|
+
}
|
|
66
|
+
if (otherProps === void 0) {
|
|
67
|
+
otherProps = {};
|
|
68
|
+
}
|
|
69
|
+
var processedHeaders = _extends({
|
|
70
|
+
Accept: 'application/json',
|
|
71
|
+
'Content-Type': 'application/x-www-form-urlencoded'
|
|
72
|
+
}, headers);
|
|
73
|
+
var body = processedHeaders['Content-Type'].indexOf('application/json') > -1 || Array.isArray(params) ? JSON.stringify(params) : serializeParams(params);
|
|
74
|
+
return request(dataAPI, 'POST', body, processedHeaders, otherProps);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* do request
|
|
79
|
+
*
|
|
80
|
+
* @export
|
|
81
|
+
* @param {*} dataAPI
|
|
82
|
+
* @param {string} [method='GET']
|
|
83
|
+
* @param {*} data
|
|
84
|
+
* @param {*} [headers={}]
|
|
85
|
+
* @param {*} [otherProps={}]
|
|
86
|
+
* @returns
|
|
87
|
+
*/
|
|
88
|
+
export function request(dataAPI, method, data, headers, otherProps) {
|
|
89
|
+
if (method === void 0) {
|
|
90
|
+
method = 'GET';
|
|
91
|
+
}
|
|
92
|
+
if (headers === void 0) {
|
|
93
|
+
headers = {};
|
|
94
|
+
}
|
|
95
|
+
if (otherProps === void 0) {
|
|
96
|
+
otherProps = {};
|
|
97
|
+
}
|
|
98
|
+
var processedHeaders = headers || {};
|
|
99
|
+
var payload = data;
|
|
100
|
+
if (method === 'PUT' || method === 'DELETE') {
|
|
101
|
+
processedHeaders = _extends({
|
|
102
|
+
Accept: 'application/json',
|
|
103
|
+
'Content-Type': 'application/json'
|
|
104
|
+
}, processedHeaders);
|
|
105
|
+
payload = JSON.stringify(payload || {});
|
|
106
|
+
}
|
|
107
|
+
return new Promise(function (resolve, reject) {
|
|
108
|
+
if (otherProps.timeout) {
|
|
109
|
+
setTimeout(function () {
|
|
110
|
+
reject(new Error('timeout'));
|
|
111
|
+
}, otherProps.timeout);
|
|
112
|
+
}
|
|
113
|
+
fetch(dataAPI, _extends({
|
|
114
|
+
method: method,
|
|
115
|
+
credentials: 'include',
|
|
116
|
+
headers: processedHeaders,
|
|
117
|
+
body: payload
|
|
118
|
+
}, otherProps)).then(function (response) {
|
|
119
|
+
switch (response.status) {
|
|
120
|
+
case 200:
|
|
121
|
+
case 201:
|
|
122
|
+
case 202:
|
|
123
|
+
return response.json();
|
|
124
|
+
case 204:
|
|
125
|
+
if (method === 'DELETE') {
|
|
126
|
+
return {
|
|
127
|
+
success: true
|
|
128
|
+
};
|
|
129
|
+
} else {
|
|
130
|
+
return {
|
|
131
|
+
__success: false,
|
|
132
|
+
code: response.status
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
case 400:
|
|
136
|
+
case 401:
|
|
137
|
+
case 403:
|
|
138
|
+
case 404:
|
|
139
|
+
case 406:
|
|
140
|
+
case 410:
|
|
141
|
+
case 422:
|
|
142
|
+
case 500:
|
|
143
|
+
return response.json().then(function (res) {
|
|
144
|
+
return {
|
|
145
|
+
__success: false,
|
|
146
|
+
code: response.status,
|
|
147
|
+
data: res
|
|
148
|
+
};
|
|
149
|
+
})["catch"](function () {
|
|
150
|
+
return {
|
|
151
|
+
__success: false,
|
|
152
|
+
code: response.status
|
|
153
|
+
};
|
|
154
|
+
});
|
|
155
|
+
default:
|
|
156
|
+
}
|
|
157
|
+
return null;
|
|
158
|
+
}).then(function (json) {
|
|
159
|
+
if (!json) {
|
|
160
|
+
reject(json);
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
if (json.__success !== false) {
|
|
164
|
+
resolve(json);
|
|
165
|
+
} else {
|
|
166
|
+
// eslint-disable-next-line no-param-reassign
|
|
167
|
+
delete json.__success;
|
|
168
|
+
reject(json);
|
|
169
|
+
}
|
|
170
|
+
})["catch"](function (err) {
|
|
171
|
+
reject(err);
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* do jsonp request
|
|
178
|
+
*
|
|
179
|
+
* @export
|
|
180
|
+
* @param {*} dataAPI
|
|
181
|
+
* @param {*} [params={}]
|
|
182
|
+
* @param {*} [otherProps={}]
|
|
183
|
+
* @returns
|
|
184
|
+
*/
|
|
185
|
+
export function jsonp(dataAPI, params, otherProps) {
|
|
186
|
+
if (params === void 0) {
|
|
187
|
+
params = {};
|
|
188
|
+
}
|
|
189
|
+
if (otherProps === void 0) {
|
|
190
|
+
otherProps = {};
|
|
191
|
+
}
|
|
192
|
+
return new Promise(function (resolve, reject) {
|
|
193
|
+
var processedOtherProps = _extends({
|
|
194
|
+
timeout: 5000
|
|
195
|
+
}, otherProps);
|
|
196
|
+
var url = buildUrl(dataAPI, params);
|
|
197
|
+
fetchJsonp(url, processedOtherProps).then(function (response) {
|
|
198
|
+
response.json();
|
|
199
|
+
}).then(function (json) {
|
|
200
|
+
if (json) {
|
|
201
|
+
resolve(json);
|
|
202
|
+
} else {
|
|
203
|
+
reject();
|
|
204
|
+
}
|
|
205
|
+
})["catch"](function (err) {
|
|
206
|
+
reject(err);
|
|
207
|
+
});
|
|
208
|
+
});
|
|
209
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { IRuntime, IRendererModules } from '../types';
|
|
2
|
+
export declare enum Env {
|
|
3
|
+
React = "react"
|
|
4
|
+
}
|
|
5
|
+
declare class Adapter {
|
|
6
|
+
runtime: IRuntime;
|
|
7
|
+
builtinModules: string[];
|
|
8
|
+
env: Env;
|
|
9
|
+
renderers: IRendererModules;
|
|
10
|
+
configProvider: any;
|
|
11
|
+
constructor();
|
|
12
|
+
initRuntime(): void;
|
|
13
|
+
setRuntime(runtime: IRuntime): void;
|
|
14
|
+
isValidRuntime(runtime: IRuntime): boolean;
|
|
15
|
+
getRuntime(): IRuntime;
|
|
16
|
+
setEnv(env: Env): void;
|
|
17
|
+
isReact(): boolean;
|
|
18
|
+
setRenderers(renderers: IRendererModules): void;
|
|
19
|
+
getRenderers(): {};
|
|
20
|
+
setConfigProvider(Comp: any): void;
|
|
21
|
+
getConfigProvider(): any;
|
|
22
|
+
}
|
|
23
|
+
declare const _default: Adapter;
|
|
24
|
+
export default _default;
|