@cloudbase/app 1.5.2-alpha.0 → 1.8.1

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.2-alpha.0",
3
+ "version": "1.8.1",
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.2-alpha.0",
32
- "@cloudbase/utilities": "^1.4.2-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.1",
32
+ "@cloudbase/utilities": "^1.8.1",
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": "d158e30a395d8fcd6b7c7e78a66006b194a3eddb"
45
+ "gitHead": "1becff3801c6dda6e3cd45127d2881aef987c7bf"
57
46
  }
@@ -8,56 +8,49 @@ const { setSdkName: setUtilitiesSdkName, setProtocol: setUtilitiesProtocol } = c
8
8
  let sdk_version = '';
9
9
  let sdk_name = '@cloudbase/js-sdk';
10
10
 
11
- export function setSdkVersion(version:string){
11
+ export function setSdkVersion(version: string) {
12
12
  sdk_version = version;
13
13
  }
14
- export function getSdkVersion(){
14
+ export function getSdkVersion() {
15
15
  return sdk_version;
16
16
  }
17
- export function setSdkName(name:string){
17
+ export function setSdkName(name: string) {
18
18
  sdk_name = name;
19
19
  setUtilitiesSdkName(name);
20
20
  }
21
- export function getSdkName(){
21
+ export function getSdkName() {
22
22
  return sdk_name;
23
23
  }
24
24
  export const DATA_VERSION = '2020-01-10';
25
25
  /**
26
26
  * request
27
27
  */
28
- let PROTOCOL = typeof location !== 'undefined' && location.protocol === 'http:'
29
- ? 'http:'
28
+ let PROTOCOL = typeof location !== 'undefined' && location.protocol === 'http:'
29
+ ? 'http:'
30
30
  : 'https:';
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
- if(protocol){
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
- export function getEndPoint(){
47
+ export function getEndPoint() {
48
48
  return { BASE_URL, PROTOCOL };
49
49
  }
50
50
 
51
51
  export enum LOGINTYPE {
52
- NULL = 'NULL',
53
52
  ANONYMOUS = 'ANONYMOUS',
54
53
  WECHAT = 'WECHAT',
55
- WECHAT_PUBLIC = 'WECHAT-PUBLIC',
56
- WECHAT_OPEN = 'WECHAT-OPEN',
57
54
  CUSTOM = 'CUSTOM',
58
- EMAIL = 'EMAIL',
59
- USERNAME = 'USERNAME',
60
- PHONE = 'PHONE'
55
+ NULL = 'NULL' // 保留字,代表未登录
61
56
  }
62
-
63
- 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';
@@ -21,7 +23,7 @@ const { catchErrorsDecorator } = helpers;
21
23
  */
22
24
  const DEFAULT_INIT_CONFIG: Partial<ICloudbaseConfig> = {
23
25
  timeout: 15000,
24
- persistence: 'local'
26
+ persistence: 'local',
25
27
  };
26
28
 
27
29
  // timeout上限10分钟
@@ -34,7 +36,6 @@ const extensionMap: KV<ICloudbaseExtension> = {};
34
36
  class Cloudbase implements ICloudbase {
35
37
  public authInstance: ICloudbaseAuth;
36
38
  public requestClient: any;
37
- public oauthClient: any
38
39
  private _config: ICloudbaseConfig;
39
40
 
40
41
  constructor(config?: ICloudbaseConfig) {
@@ -70,14 +71,14 @@ class Cloudbase implements ICloudbase {
70
71
  '请确认以下各项:',
71
72
  ' 1 - 调用 cloudbase.init() 的语法或参数是否正确',
72
73
  ' 2 - 如果是非浏览器环境,是否配置了安全应用来源(https://docs.cloudbase.net/api-reference/webv2/adapter.html#jie-ru-liu-cheng)',
73
- `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`
74
- ]
74
+ `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
75
+ ],
75
76
  })
76
77
  public init(config: ICloudbaseConfig): Cloudbase {
77
78
  if (!config.env) {
78
79
  throw new Error(JSON.stringify({
79
80
  code: ERRORS.INVALID_PARAMS,
80
- msg: 'env must not be specified'
81
+ msg: 'env must not be specified',
81
82
  }));
82
83
  }
83
84
  // 初始化时若未兼容平台,则使用默认adapter
@@ -87,13 +88,13 @@ class Cloudbase implements ICloudbase {
87
88
 
88
89
  this.requestClient = new Platform.adapter.reqClass({
89
90
  timeout: config.timeout || 5000,
90
- 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`,
91
92
  } as IRequestConfig);
92
93
  if (Platform.runtime !== RUNTIME.WEB) {
93
94
  if (!config.appSecret) {
94
95
  throw new Error(JSON.stringify({
95
96
  code: ERRORS.INVALID_PARAMS,
96
- msg: 'invalid appSecret'
97
+ msg: 'invalid appSecret',
97
98
  }));
98
99
  }
99
100
  // adapter提供获取应用标识的接口
@@ -102,30 +103,30 @@ class Cloudbase implements ICloudbase {
102
103
  // 传入的appSign与sdk获取的不一致
103
104
  throw new Error(JSON.stringify({
104
105
  code: ERRORS.INVALID_PARAMS,
105
- msg: 'invalid appSign'
106
+ msg: 'invalid appSign',
106
107
  }));
107
108
  }
108
109
  appSign && (config.appSign = appSign);
109
110
  if (!config.appSign) {
110
111
  throw new Error(JSON.stringify({
111
112
  code: ERRORS.INVALID_PARAMS,
112
- msg: 'invalid appSign'
113
+ msg: 'invalid appSign',
113
114
  }));
114
115
  }
115
116
  }
116
117
  this._config = {
117
118
  ...DEFAULT_INIT_CONFIG,
118
- ...config
119
+ ...config,
119
120
  };
120
121
  // 修正timeout取值
121
122
  this._config.timeout = this._formatTimeout(this._config.timeout);
122
123
  // 初始化cache和request
123
- const { env, persistence, debug, timeout, appSecret, appSign, oauthClient } = this._config;
124
+ const { env, persistence, debug, timeout, appSecret, appSign } = this._config;
124
125
  initCache({ env, persistence, debug, platformInfo: this.platform });
125
- initRequest({ env, region: config.region || '', timeout, appSecret, appSign, oauthClient });
126
+ initRequest({ env, region: config.region || '', timeout, appSecret, appSign });
126
127
 
127
128
  if (config.region) {
128
- setRegionLevelEndpoint(env, config.region || '')
129
+ setRegionLevelEndpoint(env, config.region || '');
129
130
  }
130
131
  const app = new Cloudbase(this._config);
131
132
  app.requestClient = this.requestClient;
@@ -149,15 +150,15 @@ class Cloudbase implements ICloudbase {
149
150
  '请确认以下各项:',
150
151
  ' 1 - 调用 invokeExtension() 的语法或参数是否正确',
151
152
  ' 2 - 被调用的扩展能力是否已经安装并通过 registerExtension() 注册',
152
- `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`
153
- ]
153
+ `如果问题依然存在,建议到官方问答社区提问或寻找帮助:${COMMUNITY_SITE_URL}`,
154
+ ],
154
155
  })
155
156
  public async invokeExtension(name: string, opts: any) {
156
157
  const ext = extensionMap[name];
157
158
  if (!ext) {
158
159
  throw new Error(JSON.stringify({
159
160
  code: ERRORS.INVALID_PARAMS,
160
- msg: `extension:${name} must be registered before invoke`
161
+ msg: `extension:${name} must be registered before invoke`,
161
162
  }));
162
163
  }
163
164
 
@@ -171,7 +172,7 @@ class Cloudbase implements ICloudbase {
171
172
  }
172
173
 
173
174
  public registerHook(hook: ICloudbaseHook) {
174
- registerHook(Cloudbase, hook)
175
+ registerHook(Cloudbase, hook);
175
176
  }
176
177
 
177
178
  public registerComponent(component: ICloudbaseComponent) {
@@ -187,7 +188,7 @@ class Cloudbase implements ICloudbase {
187
188
  }
188
189
 
189
190
  public registerEndPoint(url: string, protocol?: 'http' | 'https') {
190
- setEndPoint(url, protocol)
191
+ setEndPoint(url, protocol);
191
192
  }
192
193
 
193
194
  private _useDefaultAdapter() {
@@ -213,4 +214,4 @@ class Cloudbase implements ICloudbase {
213
214
  export const cloudbase: ICloudbase = new Cloudbase();
214
215
  cloudbase.useAdapters(adapterForWxMp);
215
216
 
216
- 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
+ }