@tarojs/api 4.0.0-canary.1 → 4.0.0-canary.10

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/dist/index.esm.js CHANGED
@@ -1,346 +1,241 @@
1
- import _typeof from '@babel/runtime/helpers/typeof';
2
1
  import { Current, getCurrentInstance, options, nextTick, eventCenter, Events } from '@tarojs/runtime';
3
- import { isWebPlatform } from '@tarojs/shared';
4
- import _objectSpread from '@babel/runtime/helpers/objectSpread2';
5
- import _classCallCheck from '@babel/runtime/helpers/classCallCheck';
6
- import _createClass from '@babel/runtime/helpers/createClass';
7
- import _defineProperty from '@babel/runtime/helpers/defineProperty';
2
+ import { isFunction, isUndefined, isObject } from '@tarojs/shared';
8
3
 
9
- function isFunction(x) {
10
- return typeof x === 'function';
11
- }
12
- function isUndefined(x) {
13
- return typeof x === 'undefined';
14
- }
15
- function isObject(x) {
16
- return x && _typeof(x) === 'object';
17
- }
18
-
19
- var isBadObj = function isBadObj(x) {
20
- return !isObject(x);
4
+ const ENV_TYPE = {
5
+ WEAPP: 'WEAPP',
6
+ SWAN: 'SWAN',
7
+ ALIPAY: 'ALIPAY',
8
+ TT: 'TT',
9
+ QQ: 'QQ',
10
+ JD: 'JD',
11
+ WEB: 'WEB',
12
+ RN: 'RN',
13
+ HARMONY: 'HARMONY',
14
+ QUICKAPP: 'QUICKAPP',
15
+ HARMONYHYBRID: 'HARMONYHYBRID',
21
16
  };
22
- function throwTypeError(s) {
23
- throw new TypeError(s);
24
- }
25
- if (!isFunction(Object.assign)) {
26
- // Must be writable: true, enumerable: false, configurable: true
27
- Object.assign = function (target) {
28
- // .length of function is 2
29
- if (target == null) {
30
- // TypeError if undefined or null
31
- throwTypeError('Cannot convert undefined or null to object');
17
+ function getEnv() {
18
+ if (process.env.TARO_ENV === 'weapp') {
19
+ return ENV_TYPE.WEAPP;
32
20
  }
33
- var to = Object(target);
34
- for (var index = 1; index < arguments.length; index++) {
35
- var nextSource = arguments[index];
36
- if (nextSource != null) {
37
- // Skip over if undefined or null
38
- for (var nextKey in nextSource) {
39
- // Avoid bugs when hasOwnProperty is shadowed
40
- if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
41
- to[nextKey] = nextSource[nextKey];
42
- }
43
- }
44
- }
21
+ else if (process.env.TARO_ENV === 'alipay') {
22
+ return ENV_TYPE.ALIPAY;
45
23
  }
46
- return to;
47
- };
48
- }
49
- if (!isFunction(Object.defineProperties)) {
50
- Object.defineProperties = function (obj, properties) {
51
- function convertToDescriptor(desc) {
52
- function hasProperty(obj, prop) {
53
- return Object.prototype.hasOwnProperty.call(obj, prop);
54
- }
55
- if (isBadObj(desc)) {
56
- throwTypeError('bad desc');
57
- }
58
- var d = {};
59
- if (hasProperty(desc, 'enumerable')) d.enumerable = !!desc.enumerable;
60
- if (hasProperty(desc, 'configurable')) {
61
- d.configurable = !!desc.configurable;
62
- }
63
- if (hasProperty(desc, 'value')) d.value = desc.value;
64
- if (hasProperty(desc, 'writable')) d.writable = !!desc.writable;
65
- if (hasProperty(desc, 'get')) {
66
- var g = desc.get;
67
- if (!isFunction(g) && !isUndefined(g)) {
68
- throwTypeError('bad get');
69
- }
70
- d.get = g;
71
- }
72
- if (hasProperty(desc, 'set')) {
73
- var s = desc.set;
74
- if (!isFunction(s) && !isUndefined(s)) {
75
- throwTypeError('bad set');
76
- }
77
- d.set = s;
78
- }
79
- if (('get' in d || 'set' in d) && ('value' in d || 'writable' in d)) {
80
- throwTypeError('identity-confused descriptor');
81
- }
82
- return d;
24
+ else if (process.env.TARO_ENV === 'swan') {
25
+ return ENV_TYPE.SWAN;
83
26
  }
84
- if (isBadObj(obj)) throwTypeError('bad obj');
85
- properties = Object(properties);
86
- var keys = Object.keys(properties);
87
- var descs = [];
88
- for (var i = 0; i < keys.length; i++) {
89
- descs.push([keys[i], convertToDescriptor(properties[keys[i]])]);
27
+ else if (process.env.TARO_ENV === 'tt') {
28
+ return ENV_TYPE.TT;
90
29
  }
91
- for (var _i = 0; _i < descs.length; _i++) {
92
- Object.defineProperty(obj, descs[_i][0], descs[_i][1]);
30
+ else if (process.env.TARO_ENV === 'jd') {
31
+ return ENV_TYPE.JD;
32
+ }
33
+ else if (process.env.TARO_ENV === 'qq') {
34
+ return ENV_TYPE.QQ;
35
+ }
36
+ else if (process.env.TARO_ENV === 'harmony-hybrid') {
37
+ return ENV_TYPE.HARMONYHYBRID;
38
+ }
39
+ else if (process.env.TARO_ENV === 'h5' || process.env.TARO_PLATFORM === 'web') {
40
+ return ENV_TYPE.WEB;
41
+ }
42
+ else if (process.env.TARO_ENV === 'rn') {
43
+ return ENV_TYPE.RN;
44
+ }
45
+ else if (process.env.TARO_ENV === 'harmony' || process.env.TARO_PLATFORM === 'harmony') {
46
+ return ENV_TYPE.HARMONY;
47
+ }
48
+ else if (process.env.TARO_ENV === 'quickapp') {
49
+ return ENV_TYPE.QUICKAPP;
50
+ }
51
+ else {
52
+ return process.env.TARO_ENV || 'Unknown';
93
53
  }
94
- return obj;
95
- };
96
- }
97
-
98
- var ENV_TYPE = {
99
- WEAPP: 'WEAPP',
100
- SWAN: 'SWAN',
101
- ALIPAY: 'ALIPAY',
102
- TT: 'TT',
103
- QQ: 'QQ',
104
- JD: 'JD',
105
- WEB: 'WEB',
106
- RN: 'RN',
107
- HARMONY: 'HARMONY',
108
- QUICKAPP: 'QUICKAPP'
109
- };
110
- var isWeb = isWebPlatform();
111
- function getEnv() {
112
- if (process.env.TARO_ENV === 'weapp') {
113
- return ENV_TYPE.WEAPP;
114
- } else if (process.env.TARO_ENV === 'alipay') {
115
- return ENV_TYPE.ALIPAY;
116
- } else if (process.env.TARO_ENV === 'swan') {
117
- return ENV_TYPE.SWAN;
118
- } else if (process.env.TARO_ENV === 'tt') {
119
- return ENV_TYPE.TT;
120
- } else if (process.env.TARO_ENV === 'jd') {
121
- return ENV_TYPE.JD;
122
- } else if (process.env.TARO_ENV === 'qq') {
123
- return ENV_TYPE.QQ;
124
- } else if (isWeb) {
125
- return ENV_TYPE.WEB;
126
- } else if (process.env.TARO_ENV === 'rn') {
127
- return ENV_TYPE.RN;
128
- } else if (process.env.TARO_ENV === 'harmony') {
129
- return ENV_TYPE.HARMONY;
130
- } else if (process.env.TARO_ENV === 'quickapp') {
131
- return ENV_TYPE.QUICKAPP;
132
- } else {
133
- return process.env.TARO_ENV || 'Unknown';
134
- }
135
54
  }
136
55
 
137
- var Chain = /*#__PURE__*/function () {
138
- function Chain(requestParams, interceptors, index) {
139
- _classCallCheck(this, Chain);
140
- this.index = index || 0;
141
- this.requestParams = requestParams;
142
- this.interceptors = interceptors || [];
143
- }
144
- _createClass(Chain, [{
145
- key: "proceed",
146
- value: function proceed(requestParams) {
147
- this.requestParams = requestParams;
148
- if (this.index >= this.interceptors.length) {
149
- throw new Error('chain 参数错误, 请勿直接修改 request.chain');
150
- }
151
- var nextInterceptor = this._getNextInterceptor();
152
- var nextChain = this._getNextChain();
153
- var p = nextInterceptor(nextChain);
154
- var res = p.catch(function (err) {
155
- return Promise.reject(err);
156
- });
157
- Object.keys(p).forEach(function (k) {
158
- return isFunction(p[k]) && (res[k] = p[k]);
159
- });
160
- return res;
56
+ class Chain {
57
+ constructor(requestParams, interceptors, index) {
58
+ this.index = index || 0;
59
+ this.requestParams = requestParams || {};
60
+ this.interceptors = interceptors || [];
61
+ }
62
+ proceed(requestParams = {}) {
63
+ this.requestParams = requestParams;
64
+ if (this.index >= this.interceptors.length) {
65
+ throw new Error('chain 参数错误, 请勿直接修改 request.chain');
66
+ }
67
+ const nextInterceptor = this._getNextInterceptor();
68
+ const nextChain = this._getNextChain();
69
+ const p = nextInterceptor(nextChain);
70
+ const res = p.catch(err => Promise.reject(err));
71
+ Object.keys(p).forEach(k => isFunction(p[k]) && (res[k] = p[k]));
72
+ return res;
161
73
  }
162
- }, {
163
- key: "_getNextInterceptor",
164
- value: function _getNextInterceptor() {
165
- return this.interceptors[this.index];
74
+ _getNextInterceptor() {
75
+ return this.interceptors[this.index];
166
76
  }
167
- }, {
168
- key: "_getNextChain",
169
- value: function _getNextChain() {
170
- return new Chain(this.requestParams, this.interceptors, this.index + 1);
77
+ _getNextChain() {
78
+ return new Chain(this.requestParams, this.interceptors, this.index + 1);
171
79
  }
172
- }]);
173
- return Chain;
174
- }();
80
+ }
175
81
 
176
- var Link = /*#__PURE__*/function () {
177
- function Link(interceptor) {
178
- _classCallCheck(this, Link);
179
- this.taroInterceptor = interceptor;
180
- this.chain = new Chain();
181
- }
182
- _createClass(Link, [{
183
- key: "request",
184
- value: function request(requestParams) {
185
- var chain = this.chain;
186
- var taroInterceptor = this.taroInterceptor;
187
- chain.interceptors = chain.interceptors.filter(function (interceptor) {
188
- return interceptor !== taroInterceptor;
189
- }).concat(taroInterceptor);
190
- return chain.proceed(_objectSpread({}, requestParams));
82
+ class Link {
83
+ constructor(interceptor) {
84
+ this.taroInterceptor = interceptor;
85
+ this.chain = new Chain();
86
+ }
87
+ request(requestParams) {
88
+ const chain = this.chain;
89
+ const taroInterceptor = this.taroInterceptor;
90
+ chain.interceptors = chain.interceptors
91
+ .filter(interceptor => interceptor !== taroInterceptor)
92
+ .concat(taroInterceptor);
93
+ return chain.proceed(Object.assign({}, requestParams));
191
94
  }
192
- }, {
193
- key: "addInterceptor",
194
- value: function addInterceptor(interceptor) {
195
- this.chain.interceptors.push(interceptor);
95
+ addInterceptor(interceptor) {
96
+ this.chain.interceptors.push(interceptor);
196
97
  }
197
- }, {
198
- key: "cleanInterceptors",
199
- value: function cleanInterceptors() {
200
- this.chain = new Chain();
98
+ cleanInterceptors() {
99
+ this.chain = new Chain();
201
100
  }
202
- }]);
203
- return Link;
204
- }();
101
+ }
205
102
  function interceptorify(promiseifyApi) {
206
- return new Link(function (chain) {
207
- return promiseifyApi(chain.requestParams);
208
- });
103
+ return new Link(function (chain) {
104
+ return promiseifyApi(chain.requestParams);
105
+ });
209
106
  }
210
107
 
211
108
  function timeoutInterceptor(chain) {
212
- var requestParams = chain.requestParams;
213
- var p;
214
- var res = new Promise(function (resolve, reject) {
215
- var timeout = setTimeout(function () {
216
- timeout = null;
217
- reject(new Error('网络链接超时,请稍后再试!'));
218
- }, requestParams && requestParams.timeout || 60000);
219
- p = chain.proceed(requestParams);
220
- p.then(function (res) {
221
- if (!timeout) return;
222
- clearTimeout(timeout);
223
- resolve(res);
224
- }).catch(function (err) {
225
- timeout && clearTimeout(timeout);
226
- reject(err);
109
+ const requestParams = chain.requestParams;
110
+ let p;
111
+ const res = new Promise((resolve, reject) => {
112
+ const timeout = setTimeout(() => {
113
+ clearTimeout(timeout);
114
+ reject(new Error('网络链接超时,请稍后再试!'));
115
+ }, (requestParams && requestParams.timeout) || 60000);
116
+ p = chain.proceed(requestParams);
117
+ p
118
+ .then(res => {
119
+ if (!timeout)
120
+ return;
121
+ clearTimeout(timeout);
122
+ resolve(res);
123
+ })
124
+ .catch(err => {
125
+ timeout && clearTimeout(timeout);
126
+ reject(err);
127
+ });
227
128
  });
228
- });
229
- if (!isUndefined(p) && isFunction(p.abort)) res.abort = p.abort;
230
- return res;
129
+ // @ts-ignore
130
+ if (!isUndefined(p) && isFunction(p.abort))
131
+ res.abort = p.abort;
132
+ return res;
231
133
  }
232
134
  function logInterceptor(chain) {
233
- var requestParams = chain.requestParams;
234
- var method = requestParams.method,
235
- data = requestParams.data,
236
- url = requestParams.url;
237
-
238
- // eslint-disable-next-line no-console
239
- console.log("http ".concat(method || 'GET', " --> ").concat(url, " data: "), data);
240
- var p = chain.proceed(requestParams);
241
- var res = p.then(function (res) {
135
+ const requestParams = chain.requestParams;
136
+ const { method, data, url } = requestParams;
242
137
  // eslint-disable-next-line no-console
243
- console.log("http <-- ".concat(url, " result:"), res);
138
+ console.log(`http ${method || 'GET'} --> ${url} data: `, data);
139
+ const p = chain.proceed(requestParams);
140
+ const res = p
141
+ .then(res => {
142
+ // eslint-disable-next-line no-console
143
+ console.log(`http <-- ${url} result:`, res);
144
+ return res;
145
+ });
146
+ // @ts-ignore
147
+ if (isFunction(p.abort))
148
+ res.abort = p.abort;
244
149
  return res;
245
- });
246
- if (isFunction(p.abort)) res.abort = p.abort;
247
- return res;
248
150
  }
249
151
 
250
152
  var interceptors = /*#__PURE__*/Object.freeze({
251
153
  __proto__: null,
252
- timeoutInterceptor: timeoutInterceptor,
253
- logInterceptor: logInterceptor
154
+ logInterceptor: logInterceptor,
155
+ timeoutInterceptor: timeoutInterceptor
254
156
  });
255
157
 
256
158
  function Behavior(options) {
257
- return options;
159
+ return options;
258
160
  }
259
161
  function getPreload(current) {
260
- return function (key, val) {
261
- current.preloadData = isObject(key) ? key : _defineProperty({}, key, val);
262
- };
162
+ return function (key, val) {
163
+ current.preloadData = isObject(key)
164
+ ? key
165
+ : {
166
+ [key]: val
167
+ };
168
+ };
263
169
  }
264
- var defaultDesignWidth = 750;
265
- var defaultDesignRatio = {
266
- 640: 2.34 / 2,
267
- 750: 1,
268
- 828: 1.81 / 2
170
+ const defaultDesignWidth = 750;
171
+ const defaultDesignRatio = {
172
+ 640: 2.34 / 2,
173
+ 750: 1,
174
+ 828: 1.81 / 2
269
175
  };
270
- var defaultBaseFontSize = 20;
271
- var defaultUnitPrecision = 5;
272
- var defaultTargetUnit = 'rpx';
176
+ const defaultBaseFontSize = 20;
177
+ const defaultUnitPrecision = 5;
178
+ const defaultTargetUnit = 'rpx';
273
179
  function getInitPxTransform(taro) {
274
- return function (config) {
275
- var _config$designWidth = config.designWidth,
276
- designWidth = _config$designWidth === void 0 ? defaultDesignWidth : _config$designWidth,
277
- _config$deviceRatio = config.deviceRatio,
278
- deviceRatio = _config$deviceRatio === void 0 ? defaultDesignRatio : _config$deviceRatio,
279
- _config$baseFontSize = config.baseFontSize,
280
- baseFontSize = _config$baseFontSize === void 0 ? defaultBaseFontSize : _config$baseFontSize,
281
- _config$targetUnit = config.targetUnit,
282
- targetUnit = _config$targetUnit === void 0 ? defaultTargetUnit : _config$targetUnit,
283
- _config$unitPrecision = config.unitPrecision,
284
- unitPrecision = _config$unitPrecision === void 0 ? defaultUnitPrecision : _config$unitPrecision;
285
- taro.config = taro.config || {};
286
- taro.config.designWidth = designWidth;
287
- taro.config.deviceRatio = deviceRatio;
288
- taro.config.baseFontSize = baseFontSize;
289
- taro.config.targetUnit = targetUnit;
290
- taro.config.unitPrecision = unitPrecision;
291
- };
180
+ return function (config) {
181
+ const { designWidth = defaultDesignWidth, deviceRatio = defaultDesignRatio, baseFontSize = defaultBaseFontSize, targetUnit = defaultTargetUnit, unitPrecision = defaultUnitPrecision, } = config;
182
+ taro.config = taro.config || {};
183
+ taro.config.designWidth = designWidth;
184
+ taro.config.deviceRatio = deviceRatio;
185
+ taro.config.baseFontSize = baseFontSize;
186
+ taro.config.targetUnit = targetUnit;
187
+ taro.config.unitPrecision = unitPrecision;
188
+ };
292
189
  }
293
190
  function getPxTransform(taro) {
294
- return function (size) {
295
- var config = taro.config || {};
296
- var baseFontSize = config.baseFontSize;
297
- var deviceRatio = config.deviceRatio || defaultDesignRatio;
298
- var designWidth = function () {
299
- var input = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
300
- return isFunction(config.designWidth) ? config.designWidth(input) : config.designWidth || defaultDesignWidth;
301
- }(size);
302
- if (!(designWidth in deviceRatio)) {
303
- throw new Error("deviceRatio \u914D\u7F6E\u4E2D\u4E0D\u5B58\u5728 ".concat(designWidth, " \u7684\u8BBE\u7F6E\uFF01"));
304
- }
305
- var targetUnit = config.targetUnit || defaultTargetUnit;
306
- var unitPrecision = config.unitPrecision || defaultUnitPrecision;
307
- var formatSize = ~~size;
308
- var rootValue = 1 / deviceRatio[designWidth];
309
- switch (targetUnit) {
310
- case 'rem':
311
- rootValue *= baseFontSize * 2;
312
- break;
313
- case 'px':
314
- rootValue *= 2;
315
- break;
316
- }
317
- var val = formatSize / rootValue;
318
- if (unitPrecision >= 0 && unitPrecision <= 100) {
319
- val = Number(val.toFixed(unitPrecision));
320
- }
321
- return val + targetUnit;
322
- };
191
+ return function (size) {
192
+ const config = taro.config || {};
193
+ const baseFontSize = config.baseFontSize;
194
+ const deviceRatio = config.deviceRatio || defaultDesignRatio;
195
+ const designWidth = (((input = 0) => isFunction(config.designWidth)
196
+ ? config.designWidth(input)
197
+ : config.designWidth || defaultDesignWidth))(size);
198
+ if (!(designWidth in deviceRatio)) {
199
+ throw new Error(`deviceRatio 配置中不存在 ${designWidth} 的设置!`);
200
+ }
201
+ const targetUnit = config.targetUnit || defaultTargetUnit;
202
+ const unitPrecision = config.unitPrecision || defaultUnitPrecision;
203
+ const formatSize = ~~size;
204
+ let rootValue = 1 / deviceRatio[designWidth];
205
+ switch (targetUnit) {
206
+ case 'rem':
207
+ rootValue *= baseFontSize * 2;
208
+ break;
209
+ case 'px':
210
+ rootValue *= 2;
211
+ break;
212
+ }
213
+ let val = formatSize / rootValue;
214
+ if (unitPrecision >= 0 && unitPrecision <= 100) {
215
+ val = Number(val.toFixed(unitPrecision));
216
+ }
217
+ return val + targetUnit;
218
+ };
323
219
  }
324
220
 
325
221
  /* eslint-disable camelcase */
326
- var Taro = {
327
- Behavior: Behavior,
328
- getEnv: getEnv,
329
- ENV_TYPE: ENV_TYPE,
330
- Link: Link,
331
- interceptors: interceptors,
332
- Current: Current,
333
- getCurrentInstance: getCurrentInstance,
334
- options: options,
335
- nextTick: nextTick,
336
- eventCenter: eventCenter,
337
- Events: Events,
338
- getInitPxTransform: getInitPxTransform,
339
- interceptorify: interceptorify
222
+ const Taro = {
223
+ Behavior,
224
+ getEnv,
225
+ ENV_TYPE,
226
+ Link,
227
+ interceptors,
228
+ Current,
229
+ getCurrentInstance,
230
+ options,
231
+ nextTick,
232
+ eventCenter,
233
+ Events,
234
+ getInitPxTransform,
235
+ interceptorify
340
236
  };
341
237
  Taro.initPxTransform = getInitPxTransform(Taro);
342
238
  Taro.preload = getPreload(Current);
343
239
  Taro.pxTransform = getPxTransform(Taro);
344
240
 
345
241
  export { Taro as default };
346
- //# sourceMappingURL=index.esm.js.map