@cloudbase/oauth 2.17.13 → 2.17.15

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.
@@ -4,6 +4,7 @@ import { AuthClientRequestOptions } from '../oauth2client/models'
4
4
  import { defaultStorage } from '../oauth2client/oauth2client'
5
5
  import { isInMpWebView, isMp } from '../utils/mp'
6
6
  import MyURLSearchParams from '../utils/urlSearchParams'
7
+ import { SDKAdapterInterface } from '@cloudbase/adapter-interface'
7
8
 
8
9
  export interface CaptchaOptions {
9
10
  clientId: string
@@ -11,6 +12,7 @@ export interface CaptchaOptions {
11
12
  storage: SimpleStorage
12
13
  // 打开网页并通过URL回调获取 CaptchaToken,针对不通的平台,该函数可以自定义实现, 默认集成浏览器端认证
13
14
  openURIWithCallback?: OpenURIWithCallbackFuction
15
+ adapter?: SDKAdapterInterface & { isMatch?: () => boolean }
14
16
  }
15
17
 
16
18
  type OpenURIWithCallbackFuction = (url: string) => Promise<CaptchaToken>
@@ -51,6 +53,10 @@ export class Captcha {
51
53
  this.tokenSectionName = `captcha_${opts.clientId}`
52
54
  }
53
55
 
56
+ public isMatch() {
57
+ return this.config.adapter?.isMatch?.() || isMp()
58
+ }
59
+
54
60
  /**
55
61
  * request http like simple fetch api, exp:request('/v1/user/me', {withCredentials:true})
56
62
  * @param {string} url
@@ -83,7 +89,7 @@ export class Captcha {
83
89
  }
84
90
 
85
91
  private getDefaultOpenURIWithCallback(): OpenURIWithCallbackFuction {
86
- if (!isMp() && !isInMpWebView()) {
92
+ if (!this.isMatch() && !isInMpWebView()) {
87
93
  if (window.location.search.indexOf('__captcha') > 0) {
88
94
  document.body.style.display = 'none'
89
95
  }
@@ -168,7 +174,7 @@ export class Captcha {
168
174
  captcha_token?: string
169
175
  expires_in?: number
170
176
  }
171
- if (!isMp() && !isInMpWebView()) {
177
+ if (!this.isMatch() && !isInMpWebView()) {
172
178
  const redirect_uri = `${window.location.origin + window.location.pathname}?__captcha=on`
173
179
  captchaTokenResp = await this.config.request<GetCaptchaResponse>(ApiUrls.GET_CAPTCHA_URL, {
174
180
  method: 'POST',
package/src/index.ts CHANGED
@@ -11,7 +11,7 @@ export class CloudbaseOAuth {
11
11
  public authApi: Auth
12
12
 
13
13
  constructor(authOptions: AuthOptions) {
14
- const { apiOrigin, clientId, env, storage, request, baseRequest, anonymousSignInFunc, wxCloud } = authOptions
14
+ const { apiOrigin, clientId, env, storage, request, baseRequest, anonymousSignInFunc, wxCloud, adapter } = authOptions
15
15
  this.oauth2client = new OAuth2Client({
16
16
  apiOrigin,
17
17
  clientId,
@@ -27,6 +27,7 @@ export class CloudbaseOAuth {
27
27
  ...authOptions,
28
28
  // 兼容老逻辑,有值传入则不走Auth内的验证码请求逻辑
29
29
  request: request ? this.oauth2client.request.bind(this.oauth2client) : undefined,
30
+ adapter,
30
31
  })
31
32
  }
32
33
  }
@@ -1,12 +1,14 @@
1
1
  /**
2
2
  * Single Promise
3
3
  */
4
+
5
+ let jsSdkFnPromiseMap = new Map()
4
6
  export class SinglePromise {
5
7
  private clientId: string
6
8
 
7
9
  constructor(options) {
8
10
  this.clientId = options?.clientId || ''
9
- globalThis.jsSdkFnPromiseMap = (globalThis.jsSdkFnPromiseMap || new Map()) as Map<string, Promise<any>>
11
+ jsSdkFnPromiseMap = (jsSdkFnPromiseMap || new Map()) as Map<string, Promise<any>>
10
12
  }
11
13
 
12
14
  /**
@@ -17,7 +19,7 @@ export class SinglePromise {
17
19
  */
18
20
  async run<T>(key: string, fn: () => Promise<T>): Promise<T> {
19
21
  key = `${this.clientId}_${key}`
20
- let result: Promise<any> = globalThis.jsSdkFnPromiseMap.get(key)
22
+ let result: Promise<any> = jsSdkFnPromiseMap.get(key)
21
23
 
22
24
  if (!(result as any)) {
23
25
  result = new Promise<any>((resolve, reject) => {
@@ -31,11 +33,11 @@ export class SinglePromise {
31
33
  } catch (error) {
32
34
  reject(error)
33
35
  } finally {
34
- globalThis.jsSdkFnPromiseMap.delete(key)
36
+ jsSdkFnPromiseMap.delete(key)
35
37
  }
36
38
  })()
37
39
  })
38
- globalThis.jsSdkFnPromiseMap.set(key, result)
40
+ jsSdkFnPromiseMap.set(key, result)
39
41
  }
40
42
  return result
41
43
  }
package/src/utils/mp.ts CHANGED
@@ -1,4 +1,8 @@
1
1
  export function isMp() {
2
+ if (typeof globalThis === 'undefined') {
3
+ return false
4
+ }
5
+
2
6
  const { wx } = globalThis as any
3
7
  if (typeof wx === 'undefined') {
4
8
  return false
@@ -16,6 +16,7 @@ const params = {
16
16
  externals: {},
17
17
  definePlugin: {
18
18
  'process.env.NODE_ENV': JSON.stringify('production'), // 注入环境变量,用于业务代码判断
19
+ 'process.env.IS_MP_BUILD': 'true',
19
20
  'globalThis.IS_MP_BUILD': true,
20
21
  },
21
22
  }