@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/LICENSE CHANGED
@@ -158,3 +158,17 @@ MIT (stencil-vue2-output-target):
158
158
  The following files embed [stencil-vue2-output-target](https://github.com/diondree/stencil-vue2-output-target) MIT:
159
159
  `/packages/taro-components-library-vue2/src/vue-component-lib/utils.ts`
160
160
  See `/LICENSE` for details of the license.
161
+
162
+ ==================
163
+
164
+ MIT (weui):
165
+ The following files embed [stencil-vue2-output-target](https://github.com/Tencent/weui) MIT:
166
+ `/packages/taro-components/src/components/*.scss`
167
+ See `/LICENSE.txt` for details of the license.
168
+
169
+ ==================
170
+
171
+ Apache-2.0 (intersection-observer):
172
+ The following files embed [intersection-observer](https://github.com/GoogleChromeLabs/intersection-observer) Apache-2.0:
173
+ `/packages/taro-api/src/polyfill/intersection-observer.ts`
174
+ See `/LICENSE.txt` for details of the license.
package/dist/env.d.ts ADDED
@@ -0,0 +1,15 @@
1
+ declare const ENV_TYPE: {
2
+ WEAPP: string;
3
+ SWAN: string;
4
+ ALIPAY: string;
5
+ TT: string;
6
+ QQ: string;
7
+ JD: string;
8
+ WEB: string;
9
+ RN: string;
10
+ HARMONY: string;
11
+ QUICKAPP: string;
12
+ HARMONYHYBRID: string;
13
+ };
14
+ declare function getEnv(): string;
15
+ export { ENV_TYPE, getEnv };
package/dist/env.js ADDED
@@ -0,0 +1,53 @@
1
+ const ENV_TYPE = {
2
+ WEAPP: 'WEAPP',
3
+ SWAN: 'SWAN',
4
+ ALIPAY: 'ALIPAY',
5
+ TT: 'TT',
6
+ QQ: 'QQ',
7
+ JD: 'JD',
8
+ WEB: 'WEB',
9
+ RN: 'RN',
10
+ HARMONY: 'HARMONY',
11
+ QUICKAPP: 'QUICKAPP',
12
+ HARMONYHYBRID: 'HARMONYHYBRID',
13
+ };
14
+ function getEnv() {
15
+ if (process.env.TARO_ENV === 'weapp') {
16
+ return ENV_TYPE.WEAPP;
17
+ }
18
+ else if (process.env.TARO_ENV === 'alipay') {
19
+ return ENV_TYPE.ALIPAY;
20
+ }
21
+ else if (process.env.TARO_ENV === 'swan') {
22
+ return ENV_TYPE.SWAN;
23
+ }
24
+ else if (process.env.TARO_ENV === 'tt') {
25
+ return ENV_TYPE.TT;
26
+ }
27
+ else if (process.env.TARO_ENV === 'jd') {
28
+ return ENV_TYPE.JD;
29
+ }
30
+ else if (process.env.TARO_ENV === 'qq') {
31
+ return ENV_TYPE.QQ;
32
+ }
33
+ else if (process.env.TARO_ENV === 'harmony-hybrid') {
34
+ return ENV_TYPE.HARMONYHYBRID;
35
+ }
36
+ else if (process.env.TARO_ENV === 'h5' || process.env.TARO_PLATFORM === 'web') {
37
+ return ENV_TYPE.WEB;
38
+ }
39
+ else if (process.env.TARO_ENV === 'rn') {
40
+ return ENV_TYPE.RN;
41
+ }
42
+ else if (process.env.TARO_ENV === 'harmony' || process.env.TARO_PLATFORM === 'harmony') {
43
+ return ENV_TYPE.HARMONY;
44
+ }
45
+ else if (process.env.TARO_ENV === 'quickapp') {
46
+ return ENV_TYPE.QUICKAPP;
47
+ }
48
+ else {
49
+ return process.env.TARO_ENV || 'Unknown';
50
+ }
51
+ }
52
+
53
+ export { ENV_TYPE, getEnv };
@@ -0,0 +1,2 @@
1
+ declare const Taro: Record<string, unknown>;
2
+ export { Taro as default };
@@ -0,0 +1,243 @@
1
+ 'use strict';
2
+
3
+ var runtime = require('@tarojs/runtime');
4
+ var shared = require('@tarojs/shared');
5
+
6
+ const ENV_TYPE = {
7
+ WEAPP: 'WEAPP',
8
+ SWAN: 'SWAN',
9
+ ALIPAY: 'ALIPAY',
10
+ TT: 'TT',
11
+ QQ: 'QQ',
12
+ JD: 'JD',
13
+ WEB: 'WEB',
14
+ RN: 'RN',
15
+ HARMONY: 'HARMONY',
16
+ QUICKAPP: 'QUICKAPP',
17
+ HARMONYHYBRID: 'HARMONYHYBRID',
18
+ };
19
+ function getEnv() {
20
+ if (process.env.TARO_ENV === 'weapp') {
21
+ return ENV_TYPE.WEAPP;
22
+ }
23
+ else if (process.env.TARO_ENV === 'alipay') {
24
+ return ENV_TYPE.ALIPAY;
25
+ }
26
+ else if (process.env.TARO_ENV === 'swan') {
27
+ return ENV_TYPE.SWAN;
28
+ }
29
+ else if (process.env.TARO_ENV === 'tt') {
30
+ return ENV_TYPE.TT;
31
+ }
32
+ else if (process.env.TARO_ENV === 'jd') {
33
+ return ENV_TYPE.JD;
34
+ }
35
+ else if (process.env.TARO_ENV === 'qq') {
36
+ return ENV_TYPE.QQ;
37
+ }
38
+ else if (process.env.TARO_ENV === 'harmony-hybrid') {
39
+ return ENV_TYPE.HARMONYHYBRID;
40
+ }
41
+ else if (process.env.TARO_ENV === 'h5' || process.env.TARO_PLATFORM === 'web') {
42
+ return ENV_TYPE.WEB;
43
+ }
44
+ else if (process.env.TARO_ENV === 'rn') {
45
+ return ENV_TYPE.RN;
46
+ }
47
+ else if (process.env.TARO_ENV === 'harmony' || process.env.TARO_PLATFORM === 'harmony') {
48
+ return ENV_TYPE.HARMONY;
49
+ }
50
+ else if (process.env.TARO_ENV === 'quickapp') {
51
+ return ENV_TYPE.QUICKAPP;
52
+ }
53
+ else {
54
+ return process.env.TARO_ENV || 'Unknown';
55
+ }
56
+ }
57
+
58
+ class Chain {
59
+ constructor(requestParams, interceptors, index) {
60
+ this.index = index || 0;
61
+ this.requestParams = requestParams || {};
62
+ this.interceptors = interceptors || [];
63
+ }
64
+ proceed(requestParams = {}) {
65
+ this.requestParams = requestParams;
66
+ if (this.index >= this.interceptors.length) {
67
+ throw new Error('chain 参数错误, 请勿直接修改 request.chain');
68
+ }
69
+ const nextInterceptor = this._getNextInterceptor();
70
+ const nextChain = this._getNextChain();
71
+ const p = nextInterceptor(nextChain);
72
+ const res = p.catch(err => Promise.reject(err));
73
+ Object.keys(p).forEach(k => shared.isFunction(p[k]) && (res[k] = p[k]));
74
+ return res;
75
+ }
76
+ _getNextInterceptor() {
77
+ return this.interceptors[this.index];
78
+ }
79
+ _getNextChain() {
80
+ return new Chain(this.requestParams, this.interceptors, this.index + 1);
81
+ }
82
+ }
83
+
84
+ class Link {
85
+ constructor(interceptor) {
86
+ this.taroInterceptor = interceptor;
87
+ this.chain = new Chain();
88
+ }
89
+ request(requestParams) {
90
+ const chain = this.chain;
91
+ const taroInterceptor = this.taroInterceptor;
92
+ chain.interceptors = chain.interceptors
93
+ .filter(interceptor => interceptor !== taroInterceptor)
94
+ .concat(taroInterceptor);
95
+ return chain.proceed(Object.assign({}, requestParams));
96
+ }
97
+ addInterceptor(interceptor) {
98
+ this.chain.interceptors.push(interceptor);
99
+ }
100
+ cleanInterceptors() {
101
+ this.chain = new Chain();
102
+ }
103
+ }
104
+ function interceptorify(promiseifyApi) {
105
+ return new Link(function (chain) {
106
+ return promiseifyApi(chain.requestParams);
107
+ });
108
+ }
109
+
110
+ function timeoutInterceptor(chain) {
111
+ const requestParams = chain.requestParams;
112
+ let p;
113
+ const res = new Promise((resolve, reject) => {
114
+ const timeout = setTimeout(() => {
115
+ clearTimeout(timeout);
116
+ reject(new Error('网络链接超时,请稍后再试!'));
117
+ }, (requestParams && requestParams.timeout) || 60000);
118
+ p = chain.proceed(requestParams);
119
+ p
120
+ .then(res => {
121
+ if (!timeout)
122
+ return;
123
+ clearTimeout(timeout);
124
+ resolve(res);
125
+ })
126
+ .catch(err => {
127
+ timeout && clearTimeout(timeout);
128
+ reject(err);
129
+ });
130
+ });
131
+ // @ts-ignore
132
+ if (!shared.isUndefined(p) && shared.isFunction(p.abort))
133
+ res.abort = p.abort;
134
+ return res;
135
+ }
136
+ function logInterceptor(chain) {
137
+ const requestParams = chain.requestParams;
138
+ const { method, data, url } = requestParams;
139
+ // eslint-disable-next-line no-console
140
+ console.log(`http ${method || 'GET'} --> ${url} data: `, data);
141
+ const p = chain.proceed(requestParams);
142
+ const res = p
143
+ .then(res => {
144
+ // eslint-disable-next-line no-console
145
+ console.log(`http <-- ${url} result:`, res);
146
+ return res;
147
+ });
148
+ // @ts-ignore
149
+ if (shared.isFunction(p.abort))
150
+ res.abort = p.abort;
151
+ return res;
152
+ }
153
+
154
+ var interceptors = /*#__PURE__*/Object.freeze({
155
+ __proto__: null,
156
+ logInterceptor: logInterceptor,
157
+ timeoutInterceptor: timeoutInterceptor
158
+ });
159
+
160
+ function Behavior(options) {
161
+ return options;
162
+ }
163
+ function getPreload(current) {
164
+ return function (key, val) {
165
+ current.preloadData = shared.isObject(key)
166
+ ? key
167
+ : {
168
+ [key]: val
169
+ };
170
+ };
171
+ }
172
+ const defaultDesignWidth = 750;
173
+ const defaultDesignRatio = {
174
+ 640: 2.34 / 2,
175
+ 750: 1,
176
+ 828: 1.81 / 2
177
+ };
178
+ const defaultBaseFontSize = 20;
179
+ const defaultUnitPrecision = 5;
180
+ const defaultTargetUnit = 'rpx';
181
+ function getInitPxTransform(taro) {
182
+ return function (config) {
183
+ const { designWidth = defaultDesignWidth, deviceRatio = defaultDesignRatio, baseFontSize = defaultBaseFontSize, targetUnit = defaultTargetUnit, unitPrecision = defaultUnitPrecision, } = config;
184
+ taro.config = taro.config || {};
185
+ taro.config.designWidth = designWidth;
186
+ taro.config.deviceRatio = deviceRatio;
187
+ taro.config.baseFontSize = baseFontSize;
188
+ taro.config.targetUnit = targetUnit;
189
+ taro.config.unitPrecision = unitPrecision;
190
+ };
191
+ }
192
+ function getPxTransform(taro) {
193
+ return function (size) {
194
+ const config = taro.config || {};
195
+ const baseFontSize = config.baseFontSize;
196
+ const deviceRatio = config.deviceRatio || defaultDesignRatio;
197
+ const designWidth = (((input = 0) => shared.isFunction(config.designWidth)
198
+ ? config.designWidth(input)
199
+ : config.designWidth || defaultDesignWidth))(size);
200
+ if (!(designWidth in deviceRatio)) {
201
+ throw new Error(`deviceRatio 配置中不存在 ${designWidth} 的设置!`);
202
+ }
203
+ const targetUnit = config.targetUnit || defaultTargetUnit;
204
+ const unitPrecision = config.unitPrecision || defaultUnitPrecision;
205
+ const formatSize = ~~size;
206
+ let rootValue = 1 / deviceRatio[designWidth];
207
+ switch (targetUnit) {
208
+ case 'rem':
209
+ rootValue *= baseFontSize * 2;
210
+ break;
211
+ case 'px':
212
+ rootValue *= 2;
213
+ break;
214
+ }
215
+ let val = formatSize / rootValue;
216
+ if (unitPrecision >= 0 && unitPrecision <= 100) {
217
+ val = Number(val.toFixed(unitPrecision));
218
+ }
219
+ return val + targetUnit;
220
+ };
221
+ }
222
+
223
+ /* eslint-disable camelcase */
224
+ const Taro = {
225
+ Behavior,
226
+ getEnv,
227
+ ENV_TYPE,
228
+ Link,
229
+ interceptors,
230
+ Current: runtime.Current,
231
+ getCurrentInstance: runtime.getCurrentInstance,
232
+ options: runtime.options,
233
+ nextTick: runtime.nextTick,
234
+ eventCenter: runtime.eventCenter,
235
+ Events: runtime.Events,
236
+ getInitPxTransform,
237
+ interceptorify
238
+ };
239
+ Taro.initPxTransform = getInitPxTransform(Taro);
240
+ Taro.preload = getPreload(runtime.Current);
241
+ Taro.pxTransform = getPxTransform(Taro);
242
+
243
+ module.exports = Taro;
@@ -0,0 +1,2 @@
1
+ declare const Taro: Record<string, unknown>;
2
+ export { Taro as default };
@@ -0,0 +1,2 @@
1
+ declare const Taro: Record<string, unknown>;
2
+ export { Taro as default };