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