amis-core 1.0.0-beta.0

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.
Files changed (211) hide show
  1. package/dist/Root.js +94 -0
  2. package/dist/RootRenderer.js +288 -0
  3. package/dist/SchemaRenderer.js +323 -0
  4. package/dist/Scoped.js +252 -0
  5. package/dist/WithRootStore.js +44 -0
  6. package/dist/WithStore.js +179 -0
  7. package/dist/__tests__/compat.test.d.ts +1 -0
  8. package/dist/__tests__/factory.test.d.ts +1 -0
  9. package/dist/__tests__/filter.test.d.ts +1 -0
  10. package/dist/__tests__/helper.d.ts +13 -0
  11. package/dist/__tests__/tokenize.test.d.ts +1 -0
  12. package/dist/actions/Action.js +119 -0
  13. package/dist/actions/AjaxAction.js +96 -0
  14. package/dist/actions/BreakAction.js +35 -0
  15. package/dist/actions/BroadcastAction.js +49 -0
  16. package/dist/actions/CmptAction.js +60 -0
  17. package/dist/actions/ContinueAction.js +35 -0
  18. package/dist/actions/CopyAction.js +43 -0
  19. package/dist/actions/CustomAction.js +49 -0
  20. package/dist/actions/DialogAction.js +104 -0
  21. package/dist/actions/DrawerAction.js +66 -0
  22. package/dist/actions/EmailAction.js +48 -0
  23. package/dist/actions/LinkAction.js +52 -0
  24. package/dist/actions/LoopAction.js +107 -0
  25. package/dist/actions/PageAction.js +80 -0
  26. package/dist/actions/ParallelAction.js +40 -0
  27. package/dist/actions/SwitchAction.js +52 -0
  28. package/dist/actions/ToastAction.js +35 -0
  29. package/dist/components/LazyComponent.js +99 -0
  30. package/dist/env.js +20 -0
  31. package/dist/envOverwrite.js +43 -0
  32. package/dist/factory.js +308 -0
  33. package/dist/index.js +218 -0
  34. package/dist/index2.js +255 -0
  35. package/dist/locale.js +114 -0
  36. package/dist/renderers/Form.js +1162 -0
  37. package/dist/renderers/Item.js +636 -0
  38. package/dist/renderers/Options.js +881 -0
  39. package/dist/renderers/Placeholder.js +31 -0
  40. package/dist/renderers/builtin.js +27 -0
  41. package/dist/renderers/wrapControl.js +507 -0
  42. package/dist/src/Root.d.ts +37 -0
  43. package/dist/src/RootRenderer.d.ts +27 -0
  44. package/dist/src/SchemaRenderer.d.ts +37 -0
  45. package/dist/src/Scoped.d.ts +38 -0
  46. package/dist/src/WithRootStore.d.ts +737 -0
  47. package/dist/src/WithStore.d.ts +5 -0
  48. package/dist/src/actions/Action.d.ts +38 -0
  49. package/dist/src/actions/AjaxAction.d.ts +25 -0
  50. package/dist/src/actions/BreakAction.d.ts +15 -0
  51. package/dist/src/actions/BroadcastAction.d.ts +16 -0
  52. package/dist/src/actions/CmptAction.d.ts +20 -0
  53. package/dist/src/actions/ContinueAction.d.ts +15 -0
  54. package/dist/src/actions/CopyAction.d.ts +20 -0
  55. package/dist/src/actions/CustomAction.d.ts +17 -0
  56. package/dist/src/actions/Decorators.d.ts +25 -0
  57. package/dist/src/actions/DialogAction.d.ts +57 -0
  58. package/dist/src/actions/DrawerAction.d.ts +30 -0
  59. package/dist/src/actions/EmailAction.d.ts +23 -0
  60. package/dist/src/actions/LinkAction.d.ts +24 -0
  61. package/dist/src/actions/LoopAction.d.ts +19 -0
  62. package/dist/src/actions/PageAction.d.ts +39 -0
  63. package/dist/src/actions/ParallelAction.d.ts +8 -0
  64. package/dist/src/actions/SwitchAction.d.ts +11 -0
  65. package/dist/src/actions/ToastAction.d.ts +19 -0
  66. package/dist/src/actions/index.d.ts +21 -0
  67. package/dist/src/components/LazyComponent.d.ts +32 -0
  68. package/dist/src/env.d.ts +172 -0
  69. package/dist/src/envOverwrite.d.ts +4 -0
  70. package/dist/src/factory.d.ts +78 -0
  71. package/dist/src/index.d.ts +25 -0
  72. package/dist/src/locale.d.ts +82 -0
  73. package/dist/src/renderers/Form.d.ts +352 -0
  74. package/dist/src/renderers/Item.d.ts +795 -0
  75. package/dist/src/renderers/Options.d.ts +150 -0
  76. package/dist/src/renderers/Placeholder.d.ts +6 -0
  77. package/dist/src/renderers/builtin.d.ts +1 -0
  78. package/dist/src/renderers/wrapControl.d.ts +446 -0
  79. package/dist/src/schema.d.ts +369 -0
  80. package/dist/src/store/app.d.ts +89 -0
  81. package/dist/src/store/combo.d.ts +8504 -0
  82. package/dist/src/store/crud.d.ts +124 -0
  83. package/dist/src/store/form.d.ts +3394 -0
  84. package/dist/src/store/formItem.d.ts +130 -0
  85. package/dist/src/store/iRenderer.d.ts +46 -0
  86. package/dist/src/store/index.d.ts +105 -0
  87. package/dist/src/store/list.d.ts +270 -0
  88. package/dist/src/store/manager.d.ts +115 -0
  89. package/dist/src/store/modal.d.ts +84 -0
  90. package/dist/src/store/node.d.ts +22 -0
  91. package/dist/src/store/pagination.d.ts +58 -0
  92. package/dist/src/store/root.d.ts +79 -0
  93. package/dist/src/store/service.d.ts +71 -0
  94. package/dist/src/store/table-v2.d.ts +309 -0
  95. package/dist/src/store/table.d.ts +8334 -0
  96. package/dist/src/theme.d.ts +83 -0
  97. package/dist/src/types.d.ts +217 -0
  98. package/dist/src/utils/Animation.d.ts +22 -0
  99. package/dist/src/utils/ColorScale.d.ts +19 -0
  100. package/dist/src/utils/DataSchema.d.ts +25 -0
  101. package/dist/src/utils/DataScope.d.ts +23 -0
  102. package/dist/src/utils/RootClose.d.ts +4 -0
  103. package/dist/src/utils/SimpleMap.d.ts +8 -0
  104. package/dist/src/utils/api.d.ts +27 -0
  105. package/dist/src/utils/attachmentAdpator.d.ts +7 -0
  106. package/dist/src/utils/autobind.d.ts +7 -0
  107. package/dist/src/utils/columnsSplit.d.ts +1 -0
  108. package/dist/src/utils/dataMapping.d.ts +3 -0
  109. package/dist/src/utils/date.d.ts +4 -0
  110. package/dist/src/utils/debug.d.ts +26 -0
  111. package/dist/src/utils/dom.d.ts +13 -0
  112. package/dist/src/utils/errors.d.ts +6 -0
  113. package/dist/src/utils/escapeHtml.d.ts +1 -0
  114. package/dist/src/utils/filter-schema.d.ts +12 -0
  115. package/dist/src/utils/filter.d.ts +1 -0
  116. package/dist/src/utils/formatDuration.d.ts +1 -0
  117. package/dist/src/utils/formula.d.ts +16 -0
  118. package/dist/src/utils/getVariable.d.ts +3 -0
  119. package/dist/src/utils/grammar.d.ts +4 -0
  120. package/dist/src/utils/handleAction.d.ts +6 -0
  121. package/dist/src/utils/helper.d.ts +286 -0
  122. package/dist/src/utils/icon.d.ts +12 -0
  123. package/dist/src/utils/image.d.ts +17 -0
  124. package/dist/src/utils/index.d.ts +12 -0
  125. package/dist/src/utils/isPureVariable.d.ts +1 -0
  126. package/dist/src/utils/json-schema-2-amis-schema.d.ts +6 -0
  127. package/dist/src/utils/keyToPath.d.ts +6 -0
  128. package/dist/src/utils/makeSorter.d.ts +1 -0
  129. package/dist/src/utils/markdown.d.ts +5 -0
  130. package/dist/src/utils/normalizeLink.d.ts +1 -0
  131. package/dist/src/utils/normalizeOptions.d.ts +7 -0
  132. package/dist/src/utils/object.d.ts +15 -0
  133. package/dist/src/utils/offset.d.ts +14 -0
  134. package/dist/src/utils/offsetParent.d.ts +4 -0
  135. package/dist/src/utils/optionValueCompare.d.ts +3 -0
  136. package/dist/src/utils/position.d.ts +15 -0
  137. package/dist/src/utils/prettyBytes.d.ts +1 -0
  138. package/dist/src/utils/renderer-event.d.ts +44 -0
  139. package/dist/src/utils/replaceText.d.ts +6 -0
  140. package/dist/src/utils/resize-sensor.d.ts +6 -0
  141. package/dist/src/utils/resolveVariable.d.ts +1 -0
  142. package/dist/src/utils/resolveVariableAndFilter.d.ts +1 -0
  143. package/dist/src/utils/scrollPosition.d.ts +6 -0
  144. package/dist/src/utils/string2regExp.d.ts +1 -0
  145. package/dist/src/utils/stripNumber.d.ts +1 -0
  146. package/dist/src/utils/style.d.ts +10 -0
  147. package/dist/src/utils/tokenize.d.ts +1 -0
  148. package/dist/src/utils/tpl-builtin.d.ts +18 -0
  149. package/dist/src/utils/tpl-lodash.d.ts +4 -0
  150. package/dist/src/utils/tpl.d.ts +11 -0
  151. package/dist/src/utils/validations.d.ts +39 -0
  152. package/dist/store/app.js +190 -0
  153. package/dist/store/combo.js +154 -0
  154. package/dist/store/crud.js +413 -0
  155. package/dist/store/form.js +579 -0
  156. package/dist/store/formItem.js +846 -0
  157. package/dist/store/iRenderer.js +189 -0
  158. package/dist/store/index.js +103 -0
  159. package/dist/store/list.js +248 -0
  160. package/dist/store/manager.js +56 -0
  161. package/dist/store/modal.js +54 -0
  162. package/dist/store/node.js +93 -0
  163. package/dist/store/pagination.js +60 -0
  164. package/dist/store/root.js +45 -0
  165. package/dist/store/service.js +415 -0
  166. package/dist/store/table-v2.js +471 -0
  167. package/dist/store/table.js +1105 -0
  168. package/dist/theme.js +93 -0
  169. package/dist/tsconfig.tsbuildinfo +1 -0
  170. package/dist/utils/Animation.js +290 -0
  171. package/dist/utils/DataSchema.js +120 -0
  172. package/dist/utils/DataScope.js +151 -0
  173. package/dist/utils/SimpleMap.js +46 -0
  174. package/dist/utils/api.js +555 -0
  175. package/dist/utils/autobind.js +94 -0
  176. package/dist/utils/dataMapping.js +136 -0
  177. package/dist/utils/date.js +90 -0
  178. package/dist/utils/debug.js +343 -0
  179. package/dist/utils/errors.js +23 -0
  180. package/dist/utils/escapeHtml.js +24 -0
  181. package/dist/utils/filter-schema.js +75 -0
  182. package/dist/utils/filter.js +481 -0
  183. package/dist/utils/formatDuration.js +27 -0
  184. package/dist/utils/formula.js +165 -0
  185. package/dist/utils/getVariable.js +29 -0
  186. package/dist/utils/grammar.js +54 -0
  187. package/dist/utils/helper.js +1283 -0
  188. package/dist/utils/isPureVariable.js +16 -0
  189. package/dist/utils/keyToPath.js +34 -0
  190. package/dist/utils/makeSorter.js +30 -0
  191. package/dist/utils/normalizeLink.js +47 -0
  192. package/dist/utils/normalizeOptions.js +92 -0
  193. package/dist/utils/object.js +156 -0
  194. package/dist/utils/offset.js +36 -0
  195. package/dist/utils/offsetParent.js +27 -0
  196. package/dist/utils/optionValueCompare.js +25 -0
  197. package/dist/utils/position.js +50 -0
  198. package/dist/utils/prettyBytes.js +28 -0
  199. package/dist/utils/renderer-event.js +131 -0
  200. package/dist/utils/replaceText.js +35 -0
  201. package/dist/utils/resize-sensor.js +177 -0
  202. package/dist/utils/resolveVariable.js +39 -0
  203. package/dist/utils/resolveVariableAndFilter.js +37 -0
  204. package/dist/utils/string2regExp.js +18 -0
  205. package/dist/utils/stripNumber.js +20 -0
  206. package/dist/utils/tokenize.js +33 -0
  207. package/dist/utils/tpl-builtin.js +71 -0
  208. package/dist/utils/tpl-lodash.js +71 -0
  209. package/dist/utils/tpl.js +140 -0
  210. package/dist/utils/validations.js +377 -0
  211. package/package.json +121 -0
@@ -0,0 +1,555 @@
1
+ /**
2
+ * amis-core v1.0.0-beta.0
3
+ * Copyright 2018-2022 fex
4
+ */
5
+
6
+ 'use strict';
7
+
8
+ Object.defineProperty(exports, '__esModule', { value: true });
9
+
10
+ var tslib = require('tslib');
11
+ var omit = require('lodash/omit');
12
+ var amisFormula = require('amis-formula');
13
+ var escapeHtml = require('./escapeHtml.js');
14
+ require('moment');
15
+ var object = require('./object.js');
16
+ var tokenize = require('./tokenize.js');
17
+ var dataMapping = require('./dataMapping.js');
18
+ require('./filter.js');
19
+ var tpl = require('./tpl.js');
20
+ var helper = require('./helper.js');
21
+ var isPlainObject = require('lodash/isPlainObject');
22
+ var debug = require('./debug.js');
23
+
24
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
25
+
26
+ var omit__default = /*#__PURE__*/_interopDefaultLegacy(omit);
27
+ var isPlainObject__default = /*#__PURE__*/_interopDefaultLegacy(isPlainObject);
28
+
29
+ var rSchema = /(?:^|raw\:)(get|post|put|delete|patch|options|head|jsonp):/i;
30
+ var apiCaches = [];
31
+ var isIE = !!document.documentMode;
32
+ function normalizeApi(api, defaultMethod) {
33
+ if (defaultMethod === void 0) { defaultMethod = 'get'; }
34
+ if (typeof api === 'string') {
35
+ var method = rSchema.test(api) ? RegExp.$1 : '';
36
+ method && (api = api.replace(method + ':', ''));
37
+ api = {
38
+ method: (method || defaultMethod),
39
+ url: api
40
+ };
41
+ }
42
+ else {
43
+ api = tslib.__assign({}, api);
44
+ }
45
+ api.url = typeof api.url === 'string' ? api.url.trim() : api.url;
46
+ return api;
47
+ }
48
+ function buildApi(api, data, options) {
49
+ if (options === void 0) { options = {}; }
50
+ api = normalizeApi(api, options.method);
51
+ var autoAppend = options.autoAppend, ignoreData = options.ignoreData, rest = tslib.__rest(options, ["autoAppend", "ignoreData"]);
52
+ api.config = tslib.__assign({}, rest);
53
+ api.method = (api.method || options.method || 'get').toLowerCase();
54
+ if (api.headers) {
55
+ api.headers = dataMapping.dataMapping(api.headers, data, undefined, false);
56
+ }
57
+ if (api.requestAdaptor && typeof api.requestAdaptor === 'string') {
58
+ api.requestAdaptor = str2function(api.requestAdaptor, 'api');
59
+ }
60
+ if (api.adaptor && typeof api.adaptor === 'string') {
61
+ api.adaptor = str2function(api.adaptor, 'payload', 'response', 'api');
62
+ }
63
+ if (!data) {
64
+ return api;
65
+ }
66
+ else if (data instanceof FormData ||
67
+ data instanceof Blob ||
68
+ data instanceof ArrayBuffer) {
69
+ api.data = data;
70
+ return api;
71
+ }
72
+ var raw = (api.url = api.url || '');
73
+ var ast = undefined;
74
+ try {
75
+ ast = amisFormula.parse(api.url);
76
+ }
77
+ catch (e) {
78
+ console.warn("api \u914D\u7F6E\u8BED\u6CD5\u51FA\u9519\uFF1A".concat(e));
79
+ return api;
80
+ }
81
+ var url = ast.body
82
+ .map(function (item, index) {
83
+ return item.type === 'raw' ? item.value : "__expression__".concat(index, "__");
84
+ })
85
+ .join('');
86
+ var idx = url.indexOf('?');
87
+ var replaceExpression = function (fragment, defaultFilter, defVal) {
88
+ if (defaultFilter === void 0) { defaultFilter = 'url_encode'; }
89
+ if (defVal === void 0) { defVal = undefined; }
90
+ return fragment.replace(/__expression__(\d+)__/g, function (_, index) {
91
+ var _a;
92
+ return ((_a = amisFormula.evaluate(ast.body[index], data, {
93
+ defaultFilter: defaultFilter
94
+ })) !== null && _a !== void 0 ? _a : defVal);
95
+ });
96
+ };
97
+ if (~idx) {
98
+ var hashIdx = url.indexOf('#');
99
+ var params = helper.qsparse(url.substring(idx + 1, ~hashIdx && hashIdx > idx ? hashIdx : undefined));
100
+ // 将里面的表达式运算完
101
+ helper.JSONTraverse(params, function (value, key, host) {
102
+ var _a;
103
+ if (typeof value === 'string' && /^__expression__(\d+)__$/.test(value)) {
104
+ host[key] = (_a = amisFormula.evaluate(ast.body[RegExp.$1].body, data)) !== null && _a !== void 0 ? _a : '';
105
+ }
106
+ else if (typeof value === 'string') {
107
+ // 参数值里面的片段不能 url_encode 了,所以是不处理
108
+ host[key] = replaceExpression(host[key], 'raw', '');
109
+ }
110
+ });
111
+ var left = replaceExpression(url.substring(0, idx), 'raw', '');
112
+ api.url =
113
+ left +
114
+ (~left.indexOf('?') ? '&' : '?') +
115
+ helper.qsstringify((api.query = dataMapping.dataMapping(params, data, undefined, api.convertKeyToPath))) +
116
+ (~hashIdx && hashIdx > idx
117
+ ? replaceExpression(url.substring(hashIdx))
118
+ : '');
119
+ }
120
+ else {
121
+ api.url = replaceExpression(url, 'raw', '');
122
+ }
123
+ if (ignoreData) {
124
+ return api;
125
+ }
126
+ if (api.data) {
127
+ api.body = api.data = dataMapping.dataMapping(api.data, data, undefined, api.convertKeyToPath);
128
+ }
129
+ else if (api.method === 'post' ||
130
+ api.method === 'put' ||
131
+ api.method === 'patch') {
132
+ api.body = api.data = object.cloneObject(data);
133
+ }
134
+ // get 类请求,把 data 附带到 url 上。
135
+ if (api.method === 'get' || api.method === 'jsonp') {
136
+ if (!api.data &&
137
+ ((!~raw.indexOf('$') && autoAppend) || api.forceAppendDataToQuery)) {
138
+ api.query = api.data = data;
139
+ }
140
+ else if (api.attachDataToQuery === false &&
141
+ api.data &&
142
+ ((!~raw.indexOf('$') && autoAppend) || api.forceAppendDataToQuery)) {
143
+ var idx_1 = api.url.indexOf('?');
144
+ if (~idx_1) {
145
+ var params = (api.query = tslib.__assign(tslib.__assign({}, helper.qsparse(api.url.substring(idx_1 + 1))), data));
146
+ api.url = api.url.substring(0, idx_1) + '?' + helper.qsstringify(params);
147
+ }
148
+ else {
149
+ api.query = data;
150
+ api.url += '?' + helper.qsstringify(data);
151
+ }
152
+ }
153
+ if (api.data && api.attachDataToQuery !== false) {
154
+ var idx_2 = api.url.indexOf('?');
155
+ if (~idx_2) {
156
+ var params = (api.query = tslib.__assign(tslib.__assign({}, helper.qsparse(api.url.substring(idx_2 + 1))), api.data));
157
+ api.url = api.url.substring(0, idx_2) + '?' + helper.qsstringify(params);
158
+ }
159
+ else {
160
+ api.query = api.data;
161
+ api.url += '?' + helper.qsstringify(api.data);
162
+ }
163
+ delete api.data;
164
+ }
165
+ }
166
+ if (api.graphql) {
167
+ if (api.method === 'get') {
168
+ api.query = api.data = tslib.__assign(tslib.__assign({}, api.query), { query: api.graphql });
169
+ }
170
+ else if (api.method === 'post' ||
171
+ api.method === 'put' ||
172
+ api.method === 'patch') {
173
+ api.body = api.data = {
174
+ query: api.graphql,
175
+ operationName: api.operationName,
176
+ variables: object.cloneObject(api.data)
177
+ };
178
+ }
179
+ }
180
+ return api;
181
+ }
182
+ function str2function(contents) {
183
+ var args = [];
184
+ for (var _i = 1; _i < arguments.length; _i++) {
185
+ args[_i - 1] = arguments[_i];
186
+ }
187
+ try {
188
+ var fn = new (Function.bind.apply(Function, tslib.__spreadArray(tslib.__spreadArray([void 0], args, false), [contents], false)))();
189
+ return fn;
190
+ }
191
+ catch (e) {
192
+ console.warn(e);
193
+ return null;
194
+ }
195
+ }
196
+ var AsyncFunction = Object.getPrototypeOf(function () {
197
+ return tslib.__awaiter(this, void 0, void 0, function () { return tslib.__generator(this, function (_a) {
198
+ return [2 /*return*/];
199
+ }); });
200
+ }).constructor;
201
+ function str2AsyncFunction(contents) {
202
+ var args = [];
203
+ for (var _i = 1; _i < arguments.length; _i++) {
204
+ args[_i - 1] = arguments[_i];
205
+ }
206
+ try {
207
+ var fn = new (AsyncFunction.bind.apply(AsyncFunction, tslib.__spreadArray(tslib.__spreadArray([void 0], args, false), [contents], false)))();
208
+ return fn;
209
+ }
210
+ catch (e) {
211
+ console.warn(e);
212
+ return null;
213
+ }
214
+ }
215
+ function responseAdaptor(ret, api) {
216
+ var data = ret.data;
217
+ var hasStatusField = true;
218
+ if (!data) {
219
+ throw new Error('Response is empty');
220
+ }
221
+ // 返回内容是 string,说明 content-type 不是 json,这时可能是返回了纯文本或 html
222
+ if (typeof data === 'string') {
223
+ var contentType = ret.headers['content-type'] || '';
224
+ // 如果是文本类型就尝试解析一下
225
+ if (ret.headers &&
226
+ contentType.startsWith('text/') &&
227
+ !contentType.includes('markdown')) {
228
+ try {
229
+ data = JSON.parse(data);
230
+ if (typeof data === 'undefined') {
231
+ throw new Error('Response should be JSON');
232
+ }
233
+ }
234
+ catch (e) {
235
+ var responseBrief = typeof data === 'string'
236
+ ? escapeHtml.escapeHtml(data.substring(0, 100))
237
+ : '';
238
+ throw new Error("Response should be JSON\n ".concat(responseBrief));
239
+ }
240
+ }
241
+ else {
242
+ if (api.responseType === 'blob') {
243
+ throw new Error('Should have "Content-Disposition" in Header');
244
+ }
245
+ else if (!contentType.includes('markdown')) {
246
+ throw new Error("Content type is wrong \"".concat(contentType, "\""));
247
+ }
248
+ }
249
+ }
250
+ // 兼容几种常见写法
251
+ if (data.hasOwnProperty('errorCode')) {
252
+ // 阿里 Java 规范
253
+ data.status = data.errorCode;
254
+ data.msg = data.errorMessage || data.errorMsg;
255
+ }
256
+ else if (data.hasOwnProperty('errno')) {
257
+ data.status = data.errno;
258
+ data.msg = data.errmsg || data.errstr || data.msg;
259
+ }
260
+ else if (data.hasOwnProperty('no')) {
261
+ data.status = data.no;
262
+ data.msg = data.error || data.msg;
263
+ }
264
+ else if (data.hasOwnProperty('error')) {
265
+ // Google JSON guide
266
+ // https://google.github.io/styleguide/jsoncstyleguide.xml#error
267
+ if (typeof data.error === 'object' && data.error.hasOwnProperty('code')) {
268
+ data.status = data.error.code;
269
+ data.msg = data.error.message;
270
+ }
271
+ else {
272
+ data.status = data.error;
273
+ data.msg = data.errmsg || data.msg;
274
+ }
275
+ }
276
+ if (!data.hasOwnProperty('status')) {
277
+ hasStatusField = false;
278
+ }
279
+ var payload = {
280
+ ok: hasStatusField === false || data.status == 0,
281
+ status: hasStatusField === false ? 0 : data.status,
282
+ msg: data.msg || data.message,
283
+ msgTimeout: data.msgTimeout,
284
+ data: !data.data && !hasStatusField ? data : data.data // 兼容直接返回数据的情况
285
+ };
286
+ // 兼容返回 schema 的情况,用于 app 模式
287
+ if (data && data.type) {
288
+ payload.data = data;
289
+ }
290
+ if (payload.status == 422) {
291
+ payload.errors = data.errors;
292
+ }
293
+ debug.debug('api', 'response', payload);
294
+ if (payload.ok && api.responseData) {
295
+ debug.debug('api', 'before dataMapping', payload.data);
296
+ var responseData = dataMapping.dataMapping(api.responseData, object.createObject({ api: api }, (Array.isArray(payload.data)
297
+ ? {
298
+ items: payload.data
299
+ }
300
+ : payload.data) || {}), undefined, api.convertKeyToPath);
301
+ debug.debug('api', 'after dataMapping', responseData);
302
+ payload.data = responseData;
303
+ }
304
+ return payload;
305
+ }
306
+ function wrapFetcher(fn, tracker) {
307
+ return function (api, data, options) {
308
+ var _a, _b;
309
+ api = buildApi(api, data, options);
310
+ if (api.requestAdaptor) {
311
+ debug.debug('api', 'before requestAdaptor', api);
312
+ api = api.requestAdaptor(api) || api;
313
+ debug.debug('api', 'after requestAdaptor', api);
314
+ }
315
+ if (api.data && (helper.hasFile(api.data) || api.dataType === 'form-data')) {
316
+ api.data =
317
+ api.data instanceof FormData
318
+ ? api.data
319
+ : helper.object2formData(api.data, api.qsOptions);
320
+ }
321
+ else if (api.data &&
322
+ typeof api.data !== 'string' &&
323
+ api.dataType === 'form') {
324
+ api.data = helper.qsstringify(api.data, api.qsOptions);
325
+ api.headers = api.headers || (api.headers = {});
326
+ api.headers['Content-Type'] = 'application/x-www-form-urlencoded';
327
+ }
328
+ else if (api.data &&
329
+ typeof api.data !== 'string' &&
330
+ api.dataType === 'json') {
331
+ api.data = JSON.stringify(api.data);
332
+ api.headers = api.headers || (api.headers = {});
333
+ api.headers['Content-Type'] = 'application/json';
334
+ }
335
+ debug.debug('api', 'request api', api);
336
+ tracker === null || tracker === void 0 ? void 0 : tracker({ eventType: 'api', eventData: omit__default["default"](api, ['config', 'data', 'body']) }, api.data);
337
+ if (((_a = api.method) === null || _a === void 0 ? void 0 : _a.toLocaleLowerCase()) === 'jsonp') {
338
+ return wrapAdaptor(jsonpFetcher(api), api);
339
+ }
340
+ if (typeof api.cache === 'number' && api.cache > 0) {
341
+ var apiCache = getApiCache(api);
342
+ return wrapAdaptor(apiCache
343
+ ? apiCache.cachedPromise
344
+ : setApiCache(api, fn(api)), api);
345
+ }
346
+ // IE 下 get 请求会被缓存,所以自动加个时间戳
347
+ if (isIE && api && ((_b = api.method) === null || _b === void 0 ? void 0 : _b.toLocaleLowerCase()) === 'get') {
348
+ var timeStamp = "_t=".concat(Date.now());
349
+ if (api.url.indexOf('?') === -1) {
350
+ api.url = api.url + "?".concat(timeStamp);
351
+ }
352
+ else {
353
+ api.url = api.url + "&".concat(timeStamp);
354
+ }
355
+ }
356
+ return wrapAdaptor(fn(api), api);
357
+ };
358
+ }
359
+ function wrapAdaptor(promise, api) {
360
+ var _this = this;
361
+ var adaptor = api.adaptor;
362
+ return adaptor
363
+ ? promise
364
+ .then(function (response) { return tslib.__awaiter(_this, void 0, void 0, function () {
365
+ var result;
366
+ return tslib.__generator(this, function (_a) {
367
+ switch (_a.label) {
368
+ case 0:
369
+ debug.debug('api', 'before adaptor data', response.data);
370
+ result = adaptor(response.data, response, api);
371
+ if (!(result === null || result === void 0 ? void 0 : result.then)) return [3 /*break*/, 2];
372
+ return [4 /*yield*/, result];
373
+ case 1:
374
+ result = _a.sent();
375
+ _a.label = 2;
376
+ case 2:
377
+ debug.debug('api', 'after adaptor data', result);
378
+ return [2 /*return*/, tslib.__assign(tslib.__assign({}, response), { data: result })];
379
+ }
380
+ });
381
+ }); })
382
+ .then(function (ret) { return responseAdaptor(ret, api); })
383
+ : promise.then(function (ret) { return responseAdaptor(ret, api); });
384
+ }
385
+ function jsonpFetcher(api) {
386
+ return new Promise(function (resolve, reject) {
387
+ var _a;
388
+ var script = document.createElement('script');
389
+ var src = api.url;
390
+ script.async = true;
391
+ function remove() {
392
+ if (script) {
393
+ // @ts-ignore
394
+ script.onload = script.onreadystatechange = script.onerror = null;
395
+ if (script.parentNode) {
396
+ script.parentNode.removeChild(script);
397
+ }
398
+ script = null;
399
+ }
400
+ }
401
+ var jsonp = ((_a = api.query) === null || _a === void 0 ? void 0 : _a.callback) || 'axiosJsonpCallback' + helper.uuid();
402
+ var old = window[jsonp];
403
+ window[jsonp] = function (responseData) {
404
+ window[jsonp] = old;
405
+ var response = {
406
+ data: responseData,
407
+ status: 200,
408
+ headers: {}
409
+ };
410
+ resolve(response);
411
+ };
412
+ var additionalParams = {
413
+ _: new Date().getTime(),
414
+ _callback: jsonp
415
+ };
416
+ src += (src.indexOf('?') >= 0 ? '&' : '?') + helper.qsstringify(additionalParams);
417
+ // @ts-ignore IE 为script.onreadystatechange
418
+ script.onload = script.onreadystatechange = function () {
419
+ // @ts-ignore
420
+ if (!script.readyState || /loaded|complete/.test(script.readyState)) {
421
+ remove();
422
+ }
423
+ };
424
+ script.onerror = function () {
425
+ remove();
426
+ var errResponse = {
427
+ status: 0,
428
+ headers: {}
429
+ };
430
+ reject(errResponse);
431
+ };
432
+ script.src = src;
433
+ document.head.appendChild(script);
434
+ });
435
+ }
436
+ function isApiOutdated(prevApi, nextApi, prevData, nextData) {
437
+ var _a;
438
+ if (!nextApi) {
439
+ return false;
440
+ }
441
+ else if (!prevApi) {
442
+ return true;
443
+ }
444
+ nextApi = normalizeApi(nextApi);
445
+ if (nextApi.autoRefresh === false) {
446
+ return false;
447
+ }
448
+ var trackExpression = (_a = nextApi.trackExpression) !== null && _a !== void 0 ? _a : nextApi.url;
449
+ if (typeof trackExpression !== 'string' || !~trackExpression.indexOf('$')) {
450
+ return false;
451
+ }
452
+ prevApi = normalizeApi(prevApi);
453
+ var isModified = false;
454
+ if (nextApi.trackExpression || prevApi.trackExpression) {
455
+ isModified =
456
+ tokenize.tokenize(prevApi.trackExpression || '', prevData) !==
457
+ tokenize.tokenize(nextApi.trackExpression || '', nextData);
458
+ }
459
+ else {
460
+ prevApi = buildApi(prevApi, prevData, { ignoreData: true });
461
+ nextApi = buildApi(nextApi, nextData, { ignoreData: true });
462
+ isModified = prevApi.url !== nextApi.url;
463
+ }
464
+ return !!(isModified &&
465
+ isValidApi(nextApi.url) &&
466
+ (!nextApi.sendOn || tpl.evalExpression(nextApi.sendOn, nextData)));
467
+ }
468
+ function isValidApi(api) {
469
+ return (api &&
470
+ /^(?:(https?|wss?|taf):\/\/[^\/]+)?(\/?[^\s\/\?]*){1,}(\?.*)?$/.test(api));
471
+ }
472
+ function isEffectiveApi(api, data, initFetch, initFetchOn) {
473
+ if (!api) {
474
+ return false;
475
+ }
476
+ if (initFetch === false) {
477
+ return false;
478
+ }
479
+ if (initFetchOn && data && !tpl.evalExpression(initFetchOn, data)) {
480
+ return false;
481
+ }
482
+ if (typeof api === 'string' && api.length) {
483
+ return true;
484
+ }
485
+ else if (object.isObject(api) && api.url) {
486
+ if (api.sendOn &&
487
+ data &&
488
+ !tpl.evalExpression(api.sendOn, data)) {
489
+ return false;
490
+ }
491
+ return true;
492
+ }
493
+ return false;
494
+ }
495
+ function isSameApi(apiA, apiB) {
496
+ return (apiA.method === apiB.method &&
497
+ apiA.url === apiB.url &&
498
+ !helper.isObjectShallowModified(apiA.data, apiB.data, false));
499
+ }
500
+ function getApiCache(api) {
501
+ // 清理过期cache
502
+ var now = Date.now();
503
+ var result;
504
+ for (var idx = 0, len = apiCaches.length; idx < len; idx++) {
505
+ var apiCache = apiCaches[idx];
506
+ if (now - apiCache.requestTime > apiCache.cache) {
507
+ apiCaches.splice(idx, 1);
508
+ len--;
509
+ idx--;
510
+ continue;
511
+ }
512
+ if (isSameApi(api, apiCache)) {
513
+ result = apiCache;
514
+ break;
515
+ }
516
+ }
517
+ return result;
518
+ }
519
+ function setApiCache(api, promise) {
520
+ apiCaches.push(tslib.__assign(tslib.__assign({}, api), { cachedPromise: promise, requestTime: Date.now() }));
521
+ return promise;
522
+ }
523
+ function clearApiCache() {
524
+ apiCaches.splice(0, apiCaches.length);
525
+ }
526
+ function normalizeApiResponseData(data) {
527
+ var _a;
528
+ if (typeof data === 'undefined') {
529
+ data = {};
530
+ }
531
+ else if (!isPlainObject__default["default"](data)) {
532
+ data = (_a = {},
533
+ _a[Array.isArray(data) ? 'items' : 'result'] = data,
534
+ _a);
535
+ }
536
+ return data;
537
+ }
538
+ // window.apiCaches = apiCaches;
539
+
540
+ exports.buildApi = buildApi;
541
+ exports.clearApiCache = clearApiCache;
542
+ exports.getApiCache = getApiCache;
543
+ exports.isApiOutdated = isApiOutdated;
544
+ exports.isEffectiveApi = isEffectiveApi;
545
+ exports.isSameApi = isSameApi;
546
+ exports.isValidApi = isValidApi;
547
+ exports.jsonpFetcher = jsonpFetcher;
548
+ exports.normalizeApi = normalizeApi;
549
+ exports.normalizeApiResponseData = normalizeApiResponseData;
550
+ exports.responseAdaptor = responseAdaptor;
551
+ exports.setApiCache = setApiCache;
552
+ exports.str2AsyncFunction = str2AsyncFunction;
553
+ exports.str2function = str2function;
554
+ exports.wrapAdaptor = wrapAdaptor;
555
+ exports.wrapFetcher = wrapFetcher;
@@ -0,0 +1,94 @@
1
+ /**
2
+ * amis-core v1.0.0-beta.0
3
+ * Copyright 2018-2022 fex
4
+ */
5
+
6
+ 'use strict';
7
+
8
+ Object.defineProperty(exports, '__esModule', { value: true });
9
+
10
+ var defineProperty = Object.defineProperty, getPrototypeOf = Object.getPrototypeOf;
11
+ function bind(fn, context) {
12
+ if (fn.bind) {
13
+ return fn.bind(context);
14
+ }
15
+ else {
16
+ return function __autobind__() {
17
+ return fn.apply(context, arguments);
18
+ };
19
+ }
20
+ }
21
+ var mapStore;
22
+ function getBoundSuper(obj, fn) {
23
+ if (typeof WeakMap === 'undefined') {
24
+ throw new Error("Using @autobind on ".concat(fn.name, "() requires WeakMap support due to its use of super.").concat(fn.name, "()\n See https://github.com/jayphelps/core-decorators.js/issues/20"));
25
+ }
26
+ if (!mapStore) {
27
+ mapStore = new WeakMap();
28
+ }
29
+ if (mapStore.has(obj) === false) {
30
+ mapStore.set(obj, new WeakMap());
31
+ }
32
+ var superStore = mapStore.get(obj);
33
+ if (superStore.has(fn) === false) {
34
+ superStore.set(fn, bind(fn, obj));
35
+ }
36
+ return superStore.get(fn);
37
+ }
38
+ function createDefaultSetter(key) {
39
+ return function set(newValue) {
40
+ Object.defineProperty(this, key, {
41
+ configurable: true,
42
+ writable: true,
43
+ // IS enumerable when reassigned by the outside word
44
+ enumerable: true,
45
+ value: newValue
46
+ });
47
+ return newValue;
48
+ };
49
+ }
50
+ function autobindMethod(target, key, _a) {
51
+ var fn = _a.value, configurable = _a.configurable, enumerable = _a.enumerable;
52
+ if (typeof fn !== 'function') {
53
+ throw new SyntaxError("@autobind can only be used on functions, not: ".concat(fn));
54
+ }
55
+ var constructor = target.constructor;
56
+ return {
57
+ configurable: configurable,
58
+ enumerable: enumerable,
59
+ get: function () {
60
+ // Class.prototype.key lookup
61
+ // Someone accesses the property directly on the prototype on which it is
62
+ // actually defined on, i.e. Class.prototype.hasOwnProperty(key)
63
+ if (this === target) {
64
+ return fn;
65
+ }
66
+ // Class.prototype.key lookup
67
+ // Someone accesses the property directly on a prototype but it was found
68
+ // up the chain, not defined directly on it
69
+ // i.e. Class.prototype.hasOwnProperty(key) == false && key in Class.prototype
70
+ if (this.constructor !== constructor &&
71
+ getPrototypeOf(this).constructor === constructor) {
72
+ return fn;
73
+ }
74
+ // Autobound method calling super.sameMethod() which is also autobound and so on.
75
+ if (this.constructor !== constructor &&
76
+ key in this.constructor.prototype) {
77
+ return getBoundSuper(this, fn);
78
+ }
79
+ var boundFn = bind(fn, this);
80
+ defineProperty(this, key, {
81
+ configurable: true,
82
+ writable: true,
83
+ // NOT enumerable when it's a bound method
84
+ enumerable: false,
85
+ value: boundFn
86
+ });
87
+ return boundFn;
88
+ },
89
+ set: createDefaultSetter(key)
90
+ };
91
+ }
92
+
93
+ exports.autobindMethod = autobindMethod;
94
+ exports.bind = bind;