@cloudbase/app 2.0.2-alpha.0 → 2.5.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.
- package/.eslintrc.js +15 -0
- package/dist/cjs/constants/common.js +7 -7
- package/dist/cjs/index.js +30 -29
- package/dist/cjs/libs/adapter.d.ts +1 -1
- package/dist/cjs/libs/adapter.js +1 -1
- package/dist/cjs/libs/cache.d.ts +1 -1
- package/dist/cjs/libs/cache.js +1 -1
- package/dist/cjs/libs/component.js +15 -10
- package/dist/cjs/libs/request.d.ts +4 -6
- package/dist/cjs/libs/request.js +49 -47
- package/dist/esm/constants/common.js +7 -7
- package/dist/esm/index.js +30 -29
- package/dist/esm/libs/adapter.d.ts +1 -1
- package/dist/esm/libs/adapter.js +1 -1
- package/dist/esm/libs/cache.d.ts +1 -1
- package/dist/esm/libs/cache.js +2 -2
- package/dist/esm/libs/component.js +15 -10
- package/dist/esm/libs/request.d.ts +4 -6
- package/dist/esm/libs/request.js +50 -48
- package/package.json +12 -16
- package/src/constants/common.ts +15 -21
- package/src/global.d.ts +1 -1
- package/src/index.ts +91 -89
- package/src/libs/adapter.ts +2 -2
- package/src/libs/cache.ts +15 -33
- package/src/libs/component.ts +69 -66
- package/src/libs/request.ts +120 -127
- package/.eslintrc +0 -29
package/src/index.ts
CHANGED
|
@@ -1,68 +1,68 @@
|
|
|
1
|
-
import { adapters, constants, utils, helpers } from '@cloudbase/utilities'
|
|
2
|
-
import { SDKAdapterInterface, CloudbaseAdapter, IRequestConfig } from '@cloudbase/adapter-interface'
|
|
3
|
-
import { ICloudbaseConfig, ICloudbaseUpgradedConfig, ICloudbase, ICloudbaseExtension, KV, ICloudbasePlatformInfo } from '@cloudbase/types'
|
|
4
|
-
import { ICloudbaseAuth } from '@cloudbase/types/auth'
|
|
5
|
-
import adapterForWxMp from 'cloudbase-adapter-wx_mp'
|
|
6
|
-
import { registerComponent, registerHook } from './libs/component'
|
|
7
|
-
import { Platform } from './libs/adapter'
|
|
8
|
-
import { ICloudbaseComponent, ICloudbaseHook } from '@cloudbase/types/component'
|
|
9
|
-
import { ICloudbaseCache } from '@cloudbase/types/cache'
|
|
10
|
-
import { initCache, getCacheByEnvId, getLocalCache } from './libs/cache'
|
|
11
|
-
import { ICloudbaseRequest } from '@cloudbase/types/request'
|
|
12
|
-
import { initRequest, getRequestByEnvId } from './libs/request'
|
|
13
|
-
import { getSdkName, setSdkVersion, setEndPoint, setRegionLevelEndpoint, setSdkName } from './constants/common'
|
|
1
|
+
import { adapters, constants, utils, helpers } from '@cloudbase/utilities'
|
|
2
|
+
import { SDKAdapterInterface, CloudbaseAdapter, IRequestConfig } from '@cloudbase/adapter-interface'
|
|
3
|
+
import { ICloudbaseConfig, ICloudbaseUpgradedConfig, ICloudbase, ICloudbaseExtension, KV, ICloudbasePlatformInfo } from '@cloudbase/types'
|
|
4
|
+
import { ICloudbaseAuth } from '@cloudbase/types/auth'
|
|
5
|
+
import adapterForWxMp from 'cloudbase-adapter-wx_mp'
|
|
6
|
+
import { registerComponent, registerHook } from './libs/component'
|
|
7
|
+
import { Platform } from './libs/adapter'
|
|
8
|
+
import { ICloudbaseComponent, ICloudbaseHook } from '@cloudbase/types/component'
|
|
9
|
+
import { ICloudbaseCache } from '@cloudbase/types/cache'
|
|
10
|
+
import { initCache, getCacheByEnvId, getLocalCache } from './libs/cache'
|
|
11
|
+
import { ICloudbaseRequest } from '@cloudbase/types/request'
|
|
12
|
+
import { initRequest, getRequestByEnvId } from './libs/request'
|
|
13
|
+
import { getSdkName, setSdkVersion, setEndPoint, setRegionLevelEndpoint, setSdkName } from './constants/common'
|
|
14
14
|
export { getBaseEndPoint } from './constants/common'
|
|
15
|
-
const { useAdapters, useDefaultAdapter, RUNTIME } = adapters
|
|
16
|
-
const { ERRORS, COMMUNITY_SITE_URL } = constants
|
|
17
|
-
const { printWarn } = utils
|
|
18
|
-
const { catchErrorsDecorator } = helpers
|
|
15
|
+
const { useAdapters, useDefaultAdapter, RUNTIME } = adapters
|
|
16
|
+
const { ERRORS, COMMUNITY_SITE_URL } = constants
|
|
17
|
+
const { printWarn } = utils
|
|
18
|
+
const { catchErrorsDecorator } = helpers
|
|
19
19
|
|
|
20
20
|
/**
|
|
21
21
|
* @constant 默认配置
|
|
22
22
|
*/
|
|
23
23
|
const DEFAULT_INIT_CONFIG: Partial<ICloudbaseConfig> = {
|
|
24
24
|
timeout: 15000,
|
|
25
|
-
persistence: 'local'
|
|
26
|
-
}
|
|
25
|
+
persistence: 'local',
|
|
26
|
+
}
|
|
27
27
|
|
|
28
28
|
// timeout上限10分钟
|
|
29
|
-
const MAX_TIMEOUT = 1000 * 60 * 10
|
|
29
|
+
const MAX_TIMEOUT = 1000 * 60 * 10
|
|
30
30
|
// timeout下限100ms
|
|
31
|
-
const MIN_TIMEOUT = 100
|
|
31
|
+
const MIN_TIMEOUT = 100
|
|
32
32
|
|
|
33
|
-
const extensionMap: KV<ICloudbaseExtension> = {}
|
|
33
|
+
const extensionMap: KV<ICloudbaseExtension> = {}
|
|
34
34
|
|
|
35
35
|
class Cloudbase implements ICloudbase {
|
|
36
|
-
public authInstance: ICloudbaseAuth
|
|
37
|
-
public oauthInstance: any
|
|
38
|
-
public requestClient: any
|
|
36
|
+
public authInstance: ICloudbaseAuth
|
|
37
|
+
public oauthInstance: any
|
|
38
|
+
public requestClient: any
|
|
39
39
|
public oauthClient: any
|
|
40
|
-
private
|
|
40
|
+
private cloudbaseConfig: ICloudbaseConfig
|
|
41
41
|
|
|
42
42
|
constructor(config?: ICloudbaseConfig) {
|
|
43
|
-
this.
|
|
44
|
-
this.authInstance = null
|
|
43
|
+
this.cloudbaseConfig = config ? config : this.cloudbaseConfig
|
|
44
|
+
this.authInstance = null
|
|
45
45
|
this.oauthInstance = null
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
get config() {
|
|
49
|
-
return this.
|
|
49
|
+
return this.cloudbaseConfig
|
|
50
50
|
}
|
|
51
51
|
|
|
52
52
|
get platform(): ICloudbasePlatformInfo {
|
|
53
|
-
return Platform
|
|
53
|
+
return Platform
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
get cache(): ICloudbaseCache {
|
|
57
|
-
return getCacheByEnvId(this.
|
|
57
|
+
return getCacheByEnvId(this.cloudbaseConfig.env)
|
|
58
58
|
}
|
|
59
59
|
|
|
60
60
|
get localCache(): ICloudbaseCache {
|
|
61
|
-
return getLocalCache(this.
|
|
61
|
+
return getLocalCache(this.cloudbaseConfig.env)
|
|
62
62
|
}
|
|
63
63
|
|
|
64
64
|
get request(): ICloudbaseRequest {
|
|
65
|
-
return getRequestByEnvId(this.
|
|
65
|
+
return getRequestByEnvId(this.cloudbaseConfig.env)
|
|
66
66
|
}
|
|
67
67
|
|
|
68
68
|
|
|
@@ -73,78 +73,80 @@ class Cloudbase implements ICloudbase {
|
|
|
73
73
|
'请确认以下各项:',
|
|
74
74
|
' 1 - 调用 cloudbase.init() 的语法或参数是否正确',
|
|
75
75
|
' 2 - 如果是非浏览器环境,是否配置了安全应用来源(https://docs.cloudbase.net/api-reference/webv2/adapter.html#jie-ru-liu-cheng)',
|
|
76
|
-
`如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}
|
|
77
|
-
]
|
|
76
|
+
`如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
|
|
77
|
+
],
|
|
78
78
|
})
|
|
79
79
|
public init(config: ICloudbaseConfig): Cloudbase {
|
|
80
80
|
if (!config.env) {
|
|
81
81
|
throw new Error(JSON.stringify({
|
|
82
82
|
code: ERRORS.INVALID_PARAMS,
|
|
83
|
-
msg: 'env must not be specified'
|
|
84
|
-
}))
|
|
83
|
+
msg: 'env must not be specified',
|
|
84
|
+
}))
|
|
85
85
|
}
|
|
86
86
|
// 初始化时若未兼容平台,则使用默认adapter
|
|
87
87
|
if (!Platform.adapter) {
|
|
88
|
-
this.
|
|
88
|
+
this.useDefaultAdapter()
|
|
89
89
|
}
|
|
90
90
|
|
|
91
|
-
|
|
91
|
+
const reqConfig: IRequestConfig = {
|
|
92
92
|
timeout: config.timeout || 5000,
|
|
93
|
-
timeoutMsg: `[${getSdkName()}][REQUEST TIMEOUT] request had been abort since didn
|
|
94
|
-
}
|
|
93
|
+
timeoutMsg: `[${getSdkName()}][REQUEST TIMEOUT] request had been abort since didn't finished within${config.timeout / 1000}s`,
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
this.requestClient = new Platform.adapter.reqClass(reqConfig)
|
|
95
97
|
if (Platform.runtime !== RUNTIME.WEB) {
|
|
96
98
|
if (!config.appSecret) {
|
|
97
99
|
throw new Error(JSON.stringify({
|
|
98
100
|
code: ERRORS.INVALID_PARAMS,
|
|
99
|
-
msg: 'invalid appSecret'
|
|
100
|
-
}))
|
|
101
|
+
msg: 'invalid appSecret',
|
|
102
|
+
}))
|
|
101
103
|
}
|
|
102
104
|
// adapter提供获取应用标识的接口
|
|
103
|
-
const appSign = Platform.adapter.getAppSign ? Platform.adapter.getAppSign() : ''
|
|
105
|
+
const appSign = Platform.adapter.getAppSign ? Platform.adapter.getAppSign() : ''
|
|
104
106
|
if (config.appSign && appSign && config.appSign !== appSign) {
|
|
105
107
|
// 传入的appSign与sdk获取的不一致
|
|
106
108
|
throw new Error(JSON.stringify({
|
|
107
109
|
code: ERRORS.INVALID_PARAMS,
|
|
108
|
-
msg: 'invalid appSign'
|
|
109
|
-
}))
|
|
110
|
+
msg: 'invalid appSign',
|
|
111
|
+
}))
|
|
110
112
|
}
|
|
111
|
-
appSign && (config.appSign = appSign)
|
|
113
|
+
appSign && (config.appSign = appSign)
|
|
112
114
|
if (!config.appSign) {
|
|
113
115
|
throw new Error(JSON.stringify({
|
|
114
116
|
code: ERRORS.INVALID_PARAMS,
|
|
115
|
-
msg: 'invalid appSign'
|
|
116
|
-
}))
|
|
117
|
+
msg: 'invalid appSign',
|
|
118
|
+
}))
|
|
117
119
|
}
|
|
118
120
|
}
|
|
119
|
-
this.
|
|
121
|
+
this.cloudbaseConfig = {
|
|
120
122
|
...DEFAULT_INIT_CONFIG,
|
|
121
|
-
...config
|
|
122
|
-
}
|
|
123
|
+
...config,
|
|
124
|
+
}
|
|
123
125
|
// 修正timeout取值
|
|
124
|
-
this.
|
|
126
|
+
this.cloudbaseConfig.timeout = this.formatTimeout(this.cloudbaseConfig.timeout)
|
|
125
127
|
// 初始化cache和request
|
|
126
|
-
const { env, persistence, debug, timeout, appSecret, appSign, oauthClient } = this.
|
|
127
|
-
initCache({ env, persistence, debug, platformInfo: this.platform })
|
|
128
|
+
const { env, persistence, debug, timeout, appSecret, appSign, oauthClient } = this.cloudbaseConfig
|
|
129
|
+
initCache({ env, persistence, debug, platformInfo: this.platform })
|
|
128
130
|
|
|
129
131
|
|
|
130
132
|
setRegionLevelEndpoint(env, config.region || '')
|
|
131
133
|
|
|
132
|
-
const app = new Cloudbase(this.
|
|
133
|
-
initRequest({ env, region: config.region || '', timeout, appSecret, appSign, oauthClient, _fromApp: app })
|
|
134
|
-
app.requestClient = this.requestClient
|
|
135
|
-
return app
|
|
134
|
+
const app = new Cloudbase(this.cloudbaseConfig)
|
|
135
|
+
initRequest({ env, region: config.region || '', timeout, appSecret, appSign, oauthClient, _fromApp: app })
|
|
136
|
+
app.requestClient = this.requestClient
|
|
137
|
+
return app
|
|
136
138
|
}
|
|
137
139
|
|
|
138
140
|
public updateConfig(config: ICloudbaseUpgradedConfig) {
|
|
139
|
-
const { persistence, debug } = config
|
|
140
|
-
this.
|
|
141
|
-
this.
|
|
141
|
+
const { persistence, debug } = config
|
|
142
|
+
this.cloudbaseConfig.persistence = persistence
|
|
143
|
+
this.cloudbaseConfig.debug = debug
|
|
142
144
|
// persistence改动影响cache
|
|
143
|
-
initCache({ env: this.
|
|
145
|
+
initCache({ env: this.cloudbaseConfig.env, persistence, debug, platformInfo: this.platform })
|
|
144
146
|
}
|
|
145
147
|
|
|
146
148
|
public registerExtension(ext: ICloudbaseExtension) {
|
|
147
|
-
extensionMap[ext.name] = ext
|
|
149
|
+
extensionMap[ext.name] = ext
|
|
148
150
|
}
|
|
149
151
|
@catchErrorsDecorator({
|
|
150
152
|
title: '调用扩展能力失败',
|
|
@@ -152,25 +154,25 @@ class Cloudbase implements ICloudbase {
|
|
|
152
154
|
'请确认以下各项:',
|
|
153
155
|
' 1 - 调用 invokeExtension() 的语法或参数是否正确',
|
|
154
156
|
' 2 - 被调用的扩展能力是否已经安装并通过 registerExtension() 注册',
|
|
155
|
-
`如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}
|
|
156
|
-
]
|
|
157
|
+
`如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
|
|
158
|
+
],
|
|
157
159
|
})
|
|
158
160
|
public async invokeExtension(name: string, opts: any) {
|
|
159
|
-
const ext = extensionMap[name]
|
|
161
|
+
const ext = extensionMap[name]
|
|
160
162
|
if (!ext) {
|
|
161
163
|
throw new Error(JSON.stringify({
|
|
162
164
|
code: ERRORS.INVALID_PARAMS,
|
|
163
|
-
msg: `extension:${name} must be registered before invoke
|
|
164
|
-
}))
|
|
165
|
+
msg: `extension:${name} must be registered before invoke`,
|
|
166
|
+
}))
|
|
165
167
|
}
|
|
166
168
|
|
|
167
|
-
return await ext.invoke(opts, this)
|
|
169
|
+
return await ext.invoke(opts, this)
|
|
168
170
|
}
|
|
169
171
|
|
|
170
172
|
public useAdapters(adapters: CloudbaseAdapter | CloudbaseAdapter[]) {
|
|
171
|
-
const { adapter, runtime } = useAdapters(adapters) || {}
|
|
172
|
-
adapter && (Platform.adapter = adapter as SDKAdapterInterface)
|
|
173
|
-
runtime && (Platform.runtime = runtime as string)
|
|
173
|
+
const { adapter, runtime } = useAdapters(adapters) || {}
|
|
174
|
+
adapter && (Platform.adapter = adapter as SDKAdapterInterface)
|
|
175
|
+
runtime && (Platform.runtime = runtime as string)
|
|
174
176
|
}
|
|
175
177
|
|
|
176
178
|
public registerHook(hook: ICloudbaseHook) {
|
|
@@ -178,42 +180,42 @@ class Cloudbase implements ICloudbase {
|
|
|
178
180
|
}
|
|
179
181
|
|
|
180
182
|
public registerComponent(component: ICloudbaseComponent) {
|
|
181
|
-
registerComponent(Cloudbase, component)
|
|
183
|
+
registerComponent(Cloudbase, component)
|
|
182
184
|
}
|
|
183
185
|
|
|
184
186
|
public registerVersion(version: string) {
|
|
185
|
-
setSdkVersion(version)
|
|
187
|
+
setSdkVersion(version)
|
|
186
188
|
}
|
|
187
189
|
|
|
188
190
|
public registerSdkName(name: string) {
|
|
189
|
-
setSdkName(name)
|
|
191
|
+
setSdkName(name)
|
|
190
192
|
}
|
|
191
193
|
|
|
192
194
|
public registerEndPoint(url: string, protocol?: 'http' | 'https') {
|
|
193
195
|
setEndPoint(url, protocol)
|
|
194
196
|
}
|
|
195
197
|
|
|
196
|
-
private
|
|
197
|
-
const { adapter, runtime } = useDefaultAdapter()
|
|
198
|
-
Platform.adapter = adapter as SDKAdapterInterface
|
|
199
|
-
Platform.runtime = runtime as string
|
|
198
|
+
private useDefaultAdapter() {
|
|
199
|
+
const { adapter, runtime } = useDefaultAdapter()
|
|
200
|
+
Platform.adapter = adapter as SDKAdapterInterface
|
|
201
|
+
Platform.runtime = runtime as string
|
|
200
202
|
}
|
|
201
203
|
|
|
202
|
-
private
|
|
204
|
+
private formatTimeout(timeout: number) {
|
|
203
205
|
switch (true) {
|
|
204
206
|
case timeout > MAX_TIMEOUT:
|
|
205
|
-
printWarn(ERRORS.INVALID_PARAMS, 'timeout is greater than maximum value[10min]')
|
|
206
|
-
return MAX_TIMEOUT
|
|
207
|
+
printWarn(ERRORS.INVALID_PARAMS, 'timeout is greater than maximum value[10min]')
|
|
208
|
+
return MAX_TIMEOUT
|
|
207
209
|
case timeout < MIN_TIMEOUT:
|
|
208
|
-
printWarn(ERRORS.INVALID_PARAMS, 'timeout is less than maximum value[100ms]')
|
|
209
|
-
return MIN_TIMEOUT
|
|
210
|
+
printWarn(ERRORS.INVALID_PARAMS, 'timeout is less than maximum value[100ms]')
|
|
211
|
+
return MIN_TIMEOUT
|
|
210
212
|
default:
|
|
211
|
-
return timeout
|
|
213
|
+
return timeout
|
|
212
214
|
}
|
|
213
215
|
}
|
|
214
216
|
}
|
|
215
217
|
|
|
216
|
-
export const cloudbase: ICloudbase = new Cloudbase()
|
|
217
|
-
cloudbase.useAdapters(adapterForWxMp)
|
|
218
|
+
export const cloudbase: ICloudbase = new Cloudbase()
|
|
219
|
+
cloudbase.useAdapters(adapterForWxMp)
|
|
218
220
|
|
|
219
|
-
export default cloudbase
|
|
221
|
+
export default cloudbase
|
package/src/libs/adapter.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { ICloudbasePlatformInfo } from
|
|
1
|
+
import { ICloudbasePlatformInfo } from '@cloudbase/types'
|
|
2
2
|
|
|
3
|
-
export const Platform:ICloudbasePlatformInfo = {}
|
|
3
|
+
export const Platform: ICloudbasePlatformInfo = {}
|
package/src/libs/cache.ts
CHANGED
|
@@ -1,59 +1,41 @@
|
|
|
1
|
-
import { KV } from
|
|
2
|
-
import { cache } from
|
|
3
|
-
import { ICloudbaseCache, ICacheConfig } from
|
|
1
|
+
import { KV } from '@cloudbase/types'
|
|
2
|
+
import { cache } from '@cloudbase/utilities'
|
|
3
|
+
import { ICloudbaseCache, ICacheConfig } from '@cloudbase/types/cache'
|
|
4
4
|
|
|
5
|
-
const USER_INFO_KEY = 'user_info'
|
|
6
|
-
// const KEY_ACCESS_TOKEN = 'access_token';
|
|
7
|
-
// const KEY_ACCESS_TOKEN_EXPIRE = 'access_token_expire';
|
|
8
|
-
// const KEY_REFRESH_TOKEN = 'refresh_token';
|
|
9
|
-
// const KEY_ANONYMOUS_UUID = 'anonymous_uuid';
|
|
10
|
-
// const KEY_LOGIN_TYPE = 'login_type';
|
|
11
|
-
// const DEVICE_INFO = 'device_id';
|
|
5
|
+
const USER_INFO_KEY = 'user_info'
|
|
12
6
|
|
|
13
|
-
const { CloudbaseCache } = cache
|
|
7
|
+
const { CloudbaseCache } = cache
|
|
14
8
|
|
|
15
|
-
const cacheMap: KV<ICloudbaseCache> = {}
|
|
9
|
+
const cacheMap: KV<ICloudbaseCache> = {}
|
|
16
10
|
// 本地存储
|
|
17
|
-
const localCacheMap: KV<ICloudbaseCache> = {}
|
|
11
|
+
const localCacheMap: KV<ICloudbaseCache> = {}
|
|
18
12
|
|
|
19
13
|
export function initCache(config: ICacheConfig & { env: string }) {
|
|
20
|
-
const { env, platformInfo } = config
|
|
14
|
+
const { env, platformInfo } = config
|
|
21
15
|
|
|
22
|
-
|
|
23
|
-
// const accessTokenExpireKey = `${KEY_ACCESS_TOKEN_EXPIRE}_${env}`;
|
|
24
|
-
// const refreshTokenKey = `${KEY_REFRESH_TOKEN}_${env}`;
|
|
25
|
-
// const anonymousUuidKey = `${KEY_ANONYMOUS_UUID}_${env}`;
|
|
26
|
-
// const loginTypeKey = `${KEY_LOGIN_TYPE}_${env}`;
|
|
27
|
-
const userInfoKey = `${USER_INFO_KEY}_${env}`;
|
|
28
|
-
// const deviceIdKey = `${DEVICE_INFO}`; // 非环境级别
|
|
16
|
+
const userInfoKey = `${USER_INFO_KEY}_${env}`
|
|
29
17
|
|
|
30
18
|
const keys = {
|
|
31
19
|
userInfoKey,
|
|
32
|
-
|
|
33
|
-
// accessTokenExpireKey,
|
|
34
|
-
// refreshTokenKey,
|
|
35
|
-
// anonymousUuidKey,
|
|
36
|
-
// loginTypeKey,
|
|
37
|
-
// deviceIdKey
|
|
38
|
-
};
|
|
20
|
+
}
|
|
39
21
|
// 若指定env已存在cache则尝试更新persistence
|
|
40
22
|
cacheMap[env] = cacheMap[env] || (new CloudbaseCache({
|
|
41
23
|
...config,
|
|
42
24
|
keys,
|
|
43
25
|
platformInfo,
|
|
44
|
-
}))
|
|
26
|
+
}))
|
|
45
27
|
localCacheMap[env] = localCacheMap[env] || new CloudbaseCache({
|
|
46
28
|
...config,
|
|
47
29
|
keys,
|
|
48
30
|
platformInfo,
|
|
49
|
-
persistence: 'local'
|
|
50
|
-
})
|
|
31
|
+
persistence: 'local',
|
|
32
|
+
})
|
|
51
33
|
}
|
|
52
34
|
|
|
53
35
|
export function getCacheByEnvId(env: string): ICloudbaseCache {
|
|
54
|
-
return cacheMap[env]
|
|
36
|
+
return cacheMap[env]
|
|
55
37
|
}
|
|
56
38
|
|
|
57
39
|
export function getLocalCache(env: string): ICloudbaseCache {
|
|
58
|
-
return localCacheMap[env]
|
|
40
|
+
return localCacheMap[env]
|
|
59
41
|
}
|
package/src/libs/component.ts
CHANGED
|
@@ -1,113 +1,116 @@
|
|
|
1
|
-
import { KV } from '@cloudbase/types'
|
|
2
|
-
import { ICloudbaseComponent, ICloudbaseHook } from '@cloudbase/types/component'
|
|
3
|
-
import { constants } from '@cloudbase/utilities'
|
|
1
|
+
import { KV } from '@cloudbase/types'
|
|
2
|
+
import { ICloudbaseComponent, ICloudbaseHook } from '@cloudbase/types/component'
|
|
3
|
+
import { constants } from '@cloudbase/utilities'
|
|
4
4
|
|
|
5
|
-
const { ERRORS } = constants
|
|
5
|
+
const { ERRORS } = constants
|
|
6
6
|
|
|
7
|
-
const components:KV<ICloudbaseComponent> = {}
|
|
7
|
+
const components: KV<ICloudbaseComponent> = {}
|
|
8
8
|
|
|
9
|
-
export function registerComponent(app:any,component:ICloudbaseComponent){
|
|
10
|
-
const { name, namespace, entity, injectEvents, IIFE=false } = component
|
|
9
|
+
export function registerComponent(app: any, component: ICloudbaseComponent) {
|
|
10
|
+
const { name, namespace, entity, injectEvents, IIFE = false } = component
|
|
11
11
|
// 不允许重复注册或命名空间重名
|
|
12
|
-
if(components[name]||(namespace&&app[namespace])){
|
|
12
|
+
if (components[name] || (namespace && app[namespace])) {
|
|
13
13
|
throw new Error(JSON.stringify({
|
|
14
14
|
code: ERRORS.INVALID_OPERATION,
|
|
15
|
-
msg: `Duplicate component ${name}
|
|
16
|
-
}))
|
|
15
|
+
msg: `Duplicate component ${name}`,
|
|
16
|
+
}))
|
|
17
17
|
}
|
|
18
18
|
// IIFE类型的组件以app为scope执行entity函数,不挂载到app.prototype上
|
|
19
|
-
if(IIFE){
|
|
20
|
-
if(!entity||typeof entity !== 'function'){
|
|
19
|
+
if (IIFE) {
|
|
20
|
+
if (!entity || typeof entity !== 'function') {
|
|
21
21
|
throw new Error(JSON.stringify({
|
|
22
22
|
code: ERRORS.INVALID_PARAMS,
|
|
23
|
-
msg: 'IIFE component\'s entity must be a function'
|
|
24
|
-
}))
|
|
23
|
+
msg: 'IIFE component\'s entity must be a function',
|
|
24
|
+
}))
|
|
25
25
|
}
|
|
26
|
-
entity.call(app)
|
|
26
|
+
entity.call(app)
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
components[name] = component
|
|
29
|
+
components[name] = component
|
|
30
30
|
|
|
31
|
-
if(namespace){
|
|
32
|
-
(app as any).prototype[namespace] = entity
|
|
33
|
-
}else{
|
|
34
|
-
deepExtend((app as any).prototype,entity)
|
|
31
|
+
if (namespace) {
|
|
32
|
+
(app as any).prototype[namespace] = entity
|
|
33
|
+
} else {
|
|
34
|
+
deepExtend((app as any).prototype, entity)
|
|
35
35
|
}
|
|
36
|
-
if(injectEvents){
|
|
37
|
-
const { bus, events } = injectEvents
|
|
38
|
-
if(!bus
|
|
36
|
+
if (injectEvents) {
|
|
37
|
+
const { bus, events } = injectEvents
|
|
38
|
+
if (!bus || !events || events.length === 0) {
|
|
39
39
|
return
|
|
40
40
|
}
|
|
41
|
-
const originCallback = (app as any).prototype.fire || function(){}
|
|
42
|
-
if(!(app as any).prototype.events){
|
|
43
|
-
(app as any).prototype.events = {}
|
|
41
|
+
const originCallback = (app as any).prototype.fire || function () {}
|
|
42
|
+
if (!(app as any).prototype.events) {
|
|
43
|
+
(app as any).prototype.events = {}
|
|
44
44
|
}
|
|
45
|
-
const originEvents:KV<any> = (app as any).prototype.events || {}
|
|
46
|
-
if(originEvents[name]){
|
|
47
|
-
(app as any).prototype.events[name].events = [...(app as any).prototype.events[name].events
|
|
48
|
-
}else{
|
|
49
|
-
(app as any).prototype.events[name] = {bus,events}
|
|
45
|
+
const originEvents: KV<any> = (app as any).prototype.events || {}
|
|
46
|
+
if (originEvents[name]) {
|
|
47
|
+
(app as any).prototype.events[name].events = [...(app as any).prototype.events[name].events, ...events]
|
|
48
|
+
} else {
|
|
49
|
+
(app as any).prototype.events[name] = { bus, events }
|
|
50
50
|
}
|
|
51
|
-
(app as any).prototype.fire = function(eventName:string,data?:any){
|
|
52
|
-
originCallback(eventName,data)
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
51
|
+
(app as any).prototype.fire = function (eventName: string, data?: any) {
|
|
52
|
+
originCallback(eventName, data)
|
|
53
|
+
const eventNames = Object.keys(this.events)
|
|
54
|
+
for (const name of eventNames) {
|
|
55
|
+
const { bus, events: eventList } = this.events[name]
|
|
56
|
+
if (eventList.includes(eventName)) {
|
|
57
|
+
bus.fire(eventName, data)
|
|
58
|
+
break
|
|
58
59
|
}
|
|
59
60
|
}
|
|
60
61
|
}
|
|
61
62
|
}
|
|
62
63
|
}
|
|
63
64
|
|
|
64
|
-
function deepExtend(target:any,source:any):KV<any>{
|
|
65
|
+
function deepExtend(target: any, source: any): KV<any> {
|
|
65
66
|
if (!(source instanceof Object)) {
|
|
66
|
-
return source
|
|
67
|
+
return source
|
|
67
68
|
}
|
|
68
69
|
|
|
69
70
|
switch (source.constructor) {
|
|
70
|
-
case Date:
|
|
71
|
-
const dateValue = source as Date
|
|
72
|
-
return new Date(dateValue.getTime())
|
|
71
|
+
case Date: {
|
|
72
|
+
const dateValue = source as Date
|
|
73
|
+
return new Date(dateValue.getTime())
|
|
74
|
+
}
|
|
73
75
|
case Object:
|
|
74
76
|
if (target === undefined) {
|
|
75
|
-
target = {}
|
|
77
|
+
target = {}
|
|
76
78
|
}
|
|
77
|
-
break
|
|
79
|
+
break
|
|
78
80
|
case Array:
|
|
79
|
-
target = []
|
|
80
|
-
break
|
|
81
|
+
target = []
|
|
82
|
+
break
|
|
81
83
|
default:
|
|
82
|
-
return source
|
|
84
|
+
return source
|
|
83
85
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
86
|
+
const sourceKeys = Object.keys(source)
|
|
87
|
+
for (const key of sourceKeys) {
|
|
88
|
+
if (!Object.prototype.hasOwnProperty.call(source, key)) {
|
|
89
|
+
continue
|
|
87
90
|
}
|
|
88
|
-
target[key] = deepExtend(target[key],source[key])
|
|
91
|
+
target[key] = deepExtend(target[key], source[key])
|
|
89
92
|
}
|
|
90
93
|
|
|
91
|
-
return target
|
|
94
|
+
return target
|
|
92
95
|
}
|
|
93
96
|
|
|
94
|
-
export function registerHook(app:any,hook:ICloudbaseHook){
|
|
95
|
-
const { entity, target } = hook
|
|
96
|
-
if(
|
|
97
|
+
export function registerHook(app: any, hook: ICloudbaseHook) {
|
|
98
|
+
const { entity, target } = hook
|
|
99
|
+
if (Object.prototype.hasOwnProperty.call(app, target)) {
|
|
97
100
|
throw new Error(JSON.stringify({
|
|
98
101
|
code: ERRORS.INVALID_OPERATION,
|
|
99
|
-
msg: `target:${target} is not exist
|
|
100
|
-
}))
|
|
102
|
+
msg: `target:${target} is not exist`,
|
|
103
|
+
}))
|
|
101
104
|
}
|
|
102
|
-
const originMethod = app.prototype[target]
|
|
103
|
-
if(typeof originMethod !== 'function'){
|
|
105
|
+
const originMethod = app.prototype[target]
|
|
106
|
+
if (typeof originMethod !== 'function') {
|
|
104
107
|
throw new Error(JSON.stringify({
|
|
105
108
|
code: ERRORS.INVALID_OPERATION,
|
|
106
|
-
msg: `target:${target} is not a function which is the only type supports hook
|
|
107
|
-
}))
|
|
109
|
+
msg: `target:${target} is not a function which is the only type supports hook`,
|
|
110
|
+
}))
|
|
108
111
|
}
|
|
109
|
-
app.prototype[target] = function(...args:any){
|
|
110
|
-
entity.call(this
|
|
111
|
-
return originMethod.call(this
|
|
112
|
+
app.prototype[target] = function (...args: any) {
|
|
113
|
+
entity.call(this, ...args)
|
|
114
|
+
return originMethod.call(this, ...args)
|
|
112
115
|
}
|
|
113
|
-
}
|
|
116
|
+
}
|