@cloudbase/app 1.5.3-alpha.0 → 1.8.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudbase/app",
3
- "version": "1.5.3-alpha.0",
3
+ "version": "1.8.2",
4
4
  "description": "cloudbase javascript sdk core",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -28,20 +28,9 @@
28
28
  "license": "ISC",
29
29
  "dependencies": {
30
30
  "@cloudbase/adapter-interface": "^0.4.0",
31
- "@cloudbase/types": "^1.2.3-alpha.0",
32
- "@cloudbase/utilities": "^1.4.3-alpha.0",
33
- "cloudbase-adapter-wx_mp": "0.2.0",
34
- "uuid": "^8.3.2"
35
- },
36
- "devDependencies": {
37
- "@types/uuid": "^8.3.1",
38
- "@typescript-eslint/eslint-plugin": "^2.30.0",
39
- "@typescript-eslint/parser": "^2.30.0",
40
- "eslint": "^6.8.0",
41
- "eslint-config-alloy": "^3.7.1",
42
- "husky": "^4.2.5",
43
- "lint-staged": "^10.2.0",
44
- "typescript": "^3.8.3"
31
+ "@cloudbase/types": "^1.8.2",
32
+ "@cloudbase/utilities": "^1.8.2",
33
+ "cloudbase-adapter-wx_mp": "0.2.0"
45
34
  },
46
35
  "husky": {
47
36
  "hooks": {
@@ -53,5 +42,5 @@
53
42
  "eslint --fix"
54
43
  ]
55
44
  },
56
- "gitHead": "eb161dd3c79b90a95a877984bf7c07cdb4563077"
45
+ "gitHead": "5b3ac0446269ebb9ad2b22cdcb48c379ded1cb07"
57
46
  }
@@ -31,40 +31,26 @@ let PROTOCOL = typeof location !== 'undefined' && location.protocol === 'http:'
31
31
  let BASE_URL = typeof process !== 'undefined' && process.env.NODE_ENV === 'e2e' && process.env.END_POINT === 'pre'
32
32
  ? '//tcb-pre.tencentcloudapi.com/web'
33
33
  : '//tcb-api.tencentcloudapi.com/web';
34
- export function setEndPoint(url: string, protocol?: 'http' | 'https') {
34
+ export function setEndPoint(url: string, protocol?: 'http'|'https') {
35
35
  BASE_URL = url;
36
36
  if (protocol) {
37
37
  PROTOCOL = protocol;
38
38
  setUtilitiesProtocol(protocol);
39
39
  }
40
40
  }
41
- export function setRegionLevelEndpoint(env: string, region: string, protocol?: 'http' | 'https') {
41
+ export function setRegionLevelEndpoint(env: string, region: string, protocol?: 'http'|'https') {
42
42
  const endpoiont = region
43
43
  ? `//${env}.${region}.tcb-api.tencentcloudapi.com/web`
44
- : `//${env}.ap-shanghai.tcb-api.tencentcloudapi.com/web`
45
- setEndPoint(endpoiont, protocol)
44
+ : `//${env}.ap-shanghai.tcb-api.tencentcloudapi.com/web`;
45
+ setEndPoint(endpoiont, protocol);
46
46
  }
47
47
  export function getEndPoint() {
48
48
  return { BASE_URL, PROTOCOL };
49
49
  }
50
50
 
51
-
52
- export function getBaseEndPoint() {
53
- const { PROTOCOL, BASE_URL } = getEndPoint()
54
- const webEndpoint = `${PROTOCOL}${BASE_URL}`
55
- return `${new URL(webEndpoint).origin}`
56
- }
57
-
58
51
  export enum LOGINTYPE {
59
- NULL = 'NULL',
60
52
  ANONYMOUS = 'ANONYMOUS',
61
53
  WECHAT = 'WECHAT',
62
- WECHAT_PUBLIC = 'WECHAT-PUBLIC',
63
- WECHAT_OPEN = 'WECHAT-OPEN',
64
54
  CUSTOM = 'CUSTOM',
65
- EMAIL = 'EMAIL',
66
- USERNAME = 'USERNAME',
67
- PHONE = 'PHONE'
55
+ NULL = 'NULL' // 保留字,代表未登录
68
56
  }
69
-
70
- export const OAUTH2_LOGINTYPE_PREFIX = 'OAUTH2'
@@ -1,7 +1,7 @@
1
1
  export const EVENTS = {
2
- LOGIN_STATE_CHANGED : 'loginStateChanged',
3
- LOGIN_STATE_EXPIRED : 'loginStateExpire',
4
- LOGIN_TYPE_CHANGED : 'loginTypeChanged',
5
- ANONYMOUS_CONVERTED : 'anonymousConverted', //匿名账户被转正后触发
6
- ACCESS_TOKEN_REFRESHD: 'refreshAccessToken'
2
+ LOGIN_STATE_CHANGED: 'loginStateChanged',
3
+ LOGIN_STATE_EXPIRED: 'loginStateExpire',
4
+ LOGIN_TYPE_CHANGED: 'loginTypeChanged',
5
+ ANONYMOUS_CONVERTED: 'anonymousConverted', // 匿名账户被转正后触发
6
+ ACCESS_TOKEN_REFRESHD: 'refreshAccessToken',
7
7
  };
package/src/global.d.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  declare module '*.json';
2
- declare module 'cloudbase-adapter-wx_mp';
2
+ declare module 'cloudbase-adapter-wx_mp';
package/src/index.ts CHANGED
@@ -1,3 +1,5 @@
1
+ /* eslint-disable no-useless-escape */
2
+ /* eslint-disable @typescript-eslint/consistent-type-assertions */
1
3
  import { adapters, constants, utils, helpers } from '@cloudbase/utilities';
2
4
  import { SDKAdapterInterface, CloudbaseAdapter, IRequestConfig } from '@cloudbase/adapter-interface';
3
5
  import { ICloudbaseConfig, ICloudbaseUpgradedConfig, ICloudbase, ICloudbaseExtension, KV, ICloudbasePlatformInfo } from '@cloudbase/types';
@@ -11,7 +13,6 @@ import { initCache, getCacheByEnvId, getLocalCache } from './libs/cache';
11
13
  import { ICloudbaseRequest } from '@cloudbase/types/request';
12
14
  import { initRequest, getRequestByEnvId } from './libs/request';
13
15
  import { getSdkName, setSdkVersion, setEndPoint, setRegionLevelEndpoint, setSdkName } from './constants/common';
14
- export { getBaseEndPoint } from './constants/common'
15
16
  const { useAdapters, useDefaultAdapter, RUNTIME } = adapters;
16
17
  const { ERRORS, COMMUNITY_SITE_URL } = constants;
17
18
  const { printWarn } = utils;
@@ -22,7 +23,7 @@ const { catchErrorsDecorator } = helpers;
22
23
  */
23
24
  const DEFAULT_INIT_CONFIG: Partial<ICloudbaseConfig> = {
24
25
  timeout: 15000,
25
- persistence: 'local'
26
+ persistence: 'local',
26
27
  };
27
28
 
28
29
  // timeout上限10分钟
@@ -34,15 +35,12 @@ const extensionMap: KV<ICloudbaseExtension> = {};
34
35
 
35
36
  class Cloudbase implements ICloudbase {
36
37
  public authInstance: ICloudbaseAuth;
37
- public oauthInstance: any;
38
38
  public requestClient: any;
39
- public oauthClient: any
40
39
  private _config: ICloudbaseConfig;
41
40
 
42
41
  constructor(config?: ICloudbaseConfig) {
43
42
  this._config = config ? config : this._config;
44
43
  this.authInstance = null;
45
- this.oauthInstance = null
46
44
  }
47
45
 
48
46
  get config() {
@@ -73,14 +71,14 @@ class Cloudbase implements ICloudbase {
73
71
  '请确认以下各项:',
74
72
  ' 1 - 调用 cloudbase.init() 的语法或参数是否正确',
75
73
  ' 2 - 如果是非浏览器环境,是否配置了安全应用来源(https://docs.cloudbase.net/api-reference/webv2/adapter.html#jie-ru-liu-cheng)',
76
- `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`
77
- ]
74
+ `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
75
+ ],
78
76
  })
79
77
  public init(config: ICloudbaseConfig): Cloudbase {
80
78
  if (!config.env) {
81
79
  throw new Error(JSON.stringify({
82
80
  code: ERRORS.INVALID_PARAMS,
83
- msg: 'env must not be specified'
81
+ msg: 'env must not be specified',
84
82
  }));
85
83
  }
86
84
  // 初始化时若未兼容平台,则使用默认adapter
@@ -90,13 +88,13 @@ class Cloudbase implements ICloudbase {
90
88
 
91
89
  this.requestClient = new Platform.adapter.reqClass({
92
90
  timeout: config.timeout || 5000,
93
- timeoutMsg: `[${getSdkName()}][REQUEST TIMEOUT] request had been abort since didn\'t finished within${config.timeout / 1000}s`
91
+ timeoutMsg: `[${getSdkName()}][REQUEST TIMEOUT] request had been abort since didn\'t finished within${config.timeout / 1000}s`,
94
92
  } as IRequestConfig);
95
93
  if (Platform.runtime !== RUNTIME.WEB) {
96
94
  if (!config.appSecret) {
97
95
  throw new Error(JSON.stringify({
98
96
  code: ERRORS.INVALID_PARAMS,
99
- msg: 'invalid appSecret'
97
+ msg: 'invalid appSecret',
100
98
  }));
101
99
  }
102
100
  // adapter提供获取应用标识的接口
@@ -105,32 +103,32 @@ class Cloudbase implements ICloudbase {
105
103
  // 传入的appSign与sdk获取的不一致
106
104
  throw new Error(JSON.stringify({
107
105
  code: ERRORS.INVALID_PARAMS,
108
- msg: 'invalid appSign'
106
+ msg: 'invalid appSign',
109
107
  }));
110
108
  }
111
109
  appSign && (config.appSign = appSign);
112
110
  if (!config.appSign) {
113
111
  throw new Error(JSON.stringify({
114
112
  code: ERRORS.INVALID_PARAMS,
115
- msg: 'invalid appSign'
113
+ msg: 'invalid appSign',
116
114
  }));
117
115
  }
118
116
  }
119
117
  this._config = {
120
118
  ...DEFAULT_INIT_CONFIG,
121
- ...config
119
+ ...config,
122
120
  };
123
121
  // 修正timeout取值
124
122
  this._config.timeout = this._formatTimeout(this._config.timeout);
125
123
  // 初始化cache和request
126
- const { env, persistence, debug, timeout, appSecret, appSign, oauthClient } = this._config;
124
+ const { env, persistence, debug, timeout, appSecret, appSign } = this._config;
127
125
  initCache({ env, persistence, debug, platformInfo: this.platform });
126
+ initRequest({ env, region: config.region || '', timeout, appSecret, appSign });
128
127
 
129
128
  if (config.region) {
130
- setRegionLevelEndpoint(env, config.region || '')
129
+ setRegionLevelEndpoint(env, config.region || '');
131
130
  }
132
131
  const app = new Cloudbase(this._config);
133
- initRequest({ env, region: config.region || '', timeout, appSecret, appSign, oauthClient, _fromApp: app });
134
132
  app.requestClient = this.requestClient;
135
133
  return app;
136
134
  }
@@ -152,15 +150,15 @@ class Cloudbase implements ICloudbase {
152
150
  '请确认以下各项:',
153
151
  ' 1 - 调用 invokeExtension() 的语法或参数是否正确',
154
152
  ' 2 - 被调用的扩展能力是否已经安装并通过 registerExtension() 注册',
155
- `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`
156
- ]
153
+ `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
154
+ ],
157
155
  })
158
156
  public async invokeExtension(name: string, opts: any) {
159
157
  const ext = extensionMap[name];
160
158
  if (!ext) {
161
159
  throw new Error(JSON.stringify({
162
160
  code: ERRORS.INVALID_PARAMS,
163
- msg: `extension:${name} must be registered before invoke`
161
+ msg: `extension:${name} must be registered before invoke`,
164
162
  }));
165
163
  }
166
164
 
@@ -174,7 +172,7 @@ class Cloudbase implements ICloudbase {
174
172
  }
175
173
 
176
174
  public registerHook(hook: ICloudbaseHook) {
177
- registerHook(Cloudbase, hook)
175
+ registerHook(Cloudbase, hook);
178
176
  }
179
177
 
180
178
  public registerComponent(component: ICloudbaseComponent) {
@@ -190,7 +188,7 @@ class Cloudbase implements ICloudbase {
190
188
  }
191
189
 
192
190
  public registerEndPoint(url: string, protocol?: 'http' | 'https') {
193
- setEndPoint(url, protocol)
191
+ setEndPoint(url, protocol);
194
192
  }
195
193
 
196
194
  private _useDefaultAdapter() {
@@ -216,4 +214,4 @@ class Cloudbase implements ICloudbase {
216
214
  export const cloudbase: ICloudbase = new Cloudbase();
217
215
  cloudbase.useAdapters(adapterForWxMp);
218
216
 
219
- export default cloudbase;
217
+ export default cloudbase;
@@ -1,3 +1,3 @@
1
- import { ICloudbasePlatformInfo } from "@cloudbase/types";
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,6 +1,6 @@
1
- import { KV } from "@cloudbase/types";
2
- import { cache } from "@cloudbase/utilities";
3
- import { ICloudbaseCache, ICacheConfig } from "@cloudbase/types/cache";
1
+ import { KV } from '@cloudbase/types';
2
+ import { cache } from '@cloudbase/utilities';
3
+ import { ICloudbaseCache, ICacheConfig } from '@cloudbase/types/cache';
4
4
 
5
5
  const KEY_ACCESS_TOKEN = 'access_token';
6
6
  const KEY_ACCESS_TOKEN_EXPIRE = 'access_token_expire';
@@ -8,7 +8,6 @@ const KEY_REFRESH_TOKEN = 'refresh_token';
8
8
  const KEY_ANONYMOUS_UUID = 'anonymous_uuid';
9
9
  const KEY_LOGIN_TYPE = 'login_type';
10
10
  const USER_INFO_KEY = 'user_info';
11
- const DEVICE_INFO = 'device_id';
12
11
 
13
12
  const { CloudbaseCache } = cache;
14
13
 
@@ -16,8 +15,8 @@ const cacheMap: KV<ICloudbaseCache> = {};
16
15
  // 本地存储
17
16
  const localCacheMap: KV<ICloudbaseCache> = {};
18
17
 
19
- export function initCache(config: ICacheConfig&{env:string}) {
20
- const { env,persistence,platformInfo } = config;
18
+ export function initCache(config: ICacheConfig&{env: string}) {
19
+ const { env, persistence, platformInfo } = config;
21
20
 
22
21
  const accessTokenKey = `${KEY_ACCESS_TOKEN}_${env}`;
23
22
  const accessTokenExpireKey = `${KEY_ACCESS_TOKEN_EXPIRE}_${env}`;
@@ -25,7 +24,6 @@ export function initCache(config: ICacheConfig&{env:string}) {
25
24
  const anonymousUuidKey = `${KEY_ANONYMOUS_UUID}_${env}`;
26
25
  const loginTypeKey = `${KEY_LOGIN_TYPE}_${env}`;
27
26
  const userInfoKey = `${USER_INFO_KEY}_${env}`;
28
- const deviceIdKey = `${DEVICE_INFO}`; // 非环境级别
29
27
 
30
28
  const keys = {
31
29
  accessTokenKey,
@@ -34,20 +32,19 @@ export function initCache(config: ICacheConfig&{env:string}) {
34
32
  anonymousUuidKey,
35
33
  loginTypeKey,
36
34
  userInfoKey,
37
- deviceIdKey
38
35
  };
39
36
  // 若指定env已存在cache则尝试更新persistence
40
- cacheMap[env]?cacheMap[env].updatePersistence(persistence):(cacheMap[env] = new CloudbaseCache({
37
+ cacheMap[env] ? cacheMap[env].updatePersistence(persistence) : (cacheMap[env] = new CloudbaseCache({
41
38
  ...config,
42
39
  keys,
43
40
  platformInfo,
44
- alwaysLocalKeys: ['anonymousUuidKey']
41
+ alwaysLocalKeys: ['anonymousUuidKey'],
45
42
  }));
46
43
  localCacheMap[env] = localCacheMap[env] || new CloudbaseCache({
47
44
  ...config,
48
45
  keys,
49
46
  platformInfo,
50
- persistence: 'local'
47
+ persistence: 'local',
51
48
  });
52
49
  }
53
50
 
@@ -1,26 +1,28 @@
1
+ /* eslint-disable no-prototype-builtins */
2
+ /* eslint-disable no-case-declarations */
1
3
  import { KV } from '@cloudbase/types';
2
4
  import { ICloudbaseComponent, ICloudbaseHook } from '@cloudbase/types/component';
3
5
  import { constants } from '@cloudbase/utilities';
4
6
 
5
7
  const { ERRORS } = constants;
6
8
 
7
- const components:KV<ICloudbaseComponent> = {};
9
+ const components: KV<ICloudbaseComponent> = {};
8
10
 
9
- export function registerComponent(app:any,component:ICloudbaseComponent){
10
- const { name, namespace, entity, injectEvents, IIFE=false } = component;
11
+ export function registerComponent(app: any, component: ICloudbaseComponent) {
12
+ const { name, namespace, entity, injectEvents, IIFE = false } = component;
11
13
  // 不允许重复注册或命名空间重名
12
- if(components[name]||(namespace&&app[namespace])){
14
+ if (components[name] || (namespace && app[namespace])) {
13
15
  throw new Error(JSON.stringify({
14
16
  code: ERRORS.INVALID_OPERATION,
15
- msg: `Duplicate component ${name}`
17
+ msg: `Duplicate component ${name}`,
16
18
  }));
17
19
  }
18
20
  // IIFE类型的组件以app为scope执行entity函数,不挂载到app.prototype上
19
- if(IIFE){
20
- if(!entity||typeof entity !== 'function'){
21
+ if (IIFE) {
22
+ if (!entity || typeof entity !== 'function') {
21
23
  throw new Error(JSON.stringify({
22
24
  code: ERRORS.INVALID_PARAMS,
23
- msg: 'IIFE component\'s entity must be a function'
25
+ msg: 'IIFE component\'s entity must be a function',
24
26
  }));
25
27
  }
26
28
  entity.call(app);
@@ -28,40 +30,40 @@ export function registerComponent(app:any,component:ICloudbaseComponent){
28
30
 
29
31
  components[name] = component;
30
32
 
31
- if(namespace){
33
+ if (namespace) {
32
34
  (app as any).prototype[namespace] = entity;
33
- }else{
34
- deepExtend((app as any).prototype,entity);
35
+ } else {
36
+ deepExtend((app as any).prototype, entity);
35
37
  }
36
- if(injectEvents){
38
+ if (injectEvents) {
37
39
  const { bus, events } = injectEvents;
38
- if(!bus||!events||events.length===0){
39
- return
40
+ if (!bus || !events || events.length === 0) {
41
+ return;
40
42
  }
41
- const originCallback = (app as any).prototype.fire || function(){};
42
- if(!(app as any).prototype.events){
43
+ const originCallback = (app as any).prototype.fire || function () {};
44
+ if (!(app as any).prototype.events) {
43
45
  (app as any).prototype.events = {};
44
46
  }
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};
47
+ const originEvents: KV<any> = (app as any).prototype.events || {};
48
+ if (originEvents[name]) {
49
+ (app as any).prototype.events[name].events = [...(app as any).prototype.events[name].events, ...events];
50
+ } else {
51
+ (app as any).prototype.events[name] = { bus, events };
50
52
  }
51
- (app as any).prototype.fire = function(eventName:string,data?:any){
52
- originCallback(eventName,data);
53
- for(const name in this.events){
54
- const { bus, events:eventList } = this.events[name];
55
- if(eventList.includes(eventName)){
56
- bus.fire(eventName,data);
53
+ (app as any).prototype.fire = function (eventName: string, data?: any) {
54
+ originCallback(eventName, data);
55
+ for (const name in this.events) {
56
+ const { bus, events: eventList } = this.events[name];
57
+ if (eventList.includes(eventName)) {
58
+ bus.fire(eventName, data);
57
59
  break;
58
60
  }
59
61
  }
60
- }
62
+ };
61
63
  }
62
64
  }
63
65
 
64
- function deepExtend(target:any,source:any):KV<any>{
66
+ function deepExtend(target: any, source: any): KV<any> {
65
67
  if (!(source instanceof Object)) {
66
68
  return source;
67
69
  }
@@ -85,29 +87,29 @@ function deepExtend(target:any,source:any):KV<any>{
85
87
  if (!source.hasOwnProperty(key)) {
86
88
  continue;
87
89
  }
88
- target[key] = deepExtend(target[key],source[key]);
90
+ target[key] = deepExtend(target[key], source[key]);
89
91
  }
90
92
 
91
93
  return target;
92
94
  }
93
95
 
94
- export function registerHook(app:any,hook:ICloudbaseHook){
96
+ export function registerHook(app: any, hook: ICloudbaseHook) {
95
97
  const { entity, target } = hook;
96
- if(!app.prototype.hasOwnProperty(target)){
98
+ if (!app.prototype.hasOwnProperty(target)) {
97
99
  throw new Error(JSON.stringify({
98
100
  code: ERRORS.INVALID_OPERATION,
99
- msg: `target:${target} is not exist`
101
+ msg: `target:${target} is not exist`,
100
102
  }));
101
103
  }
102
104
  const originMethod = app.prototype[target];
103
- if(typeof originMethod !== 'function'){
105
+ if (typeof originMethod !== 'function') {
104
106
  throw new Error(JSON.stringify({
105
107
  code: ERRORS.INVALID_OPERATION,
106
- msg: `target:${target} is not a function which is the only type supports hook`
108
+ msg: `target:${target} is not a function which is the only type supports hook`,
107
109
  }));
108
110
  }
109
- app.prototype[target] = function(...args:any){
110
- entity.call(this,...args);
111
- return originMethod.call(this,...args);
112
- }
113
- }
111
+ app.prototype[target] = function (...args: any) {
112
+ entity.call(this, ...args);
113
+ return originMethod.call(this, ...args);
114
+ };
115
+ }