UNPKG

@cloudbase/app

Version:
129 lines (107 loc) 3.89 kB
import { EndPointKey } from '@cloudbase/types' import { constants } from '@cloudbase/utilities' const ZONE_CHINA = ['ap-shanghai', 'ap-guangzhou', 'ap-shenzhen-fsi', 'ap-shanghai-fsi', 'ap-nanjing', 'ap-beijing', 'ap-chengdu', 'ap-chongqing', 'ap-hongkong'] // @ts-ignore const { setSdkName: setUtilitiesSdkName, setProtocol: setUtilitiesProtocol } = constants /** * SDK */ let sdkVersion = '' let sdkName = '@cloudbase/js-sdk' export function setSdkVersion(version: string) { sdkVersion = version } export function getSdkVersion() { return sdkVersion } export function setSdkName(name: string) { sdkName = name setUtilitiesSdkName(name) } export function getSdkName() { return sdkName } export const DATA_VERSION = '2020-01-10' interface EndPointInfo { env: string endPointKey: EndPointKey region?: string baseUrl?: string protocol?: Protocol } type Protocol = 'http' | 'https' | 'http:' | 'https:' /** * 所有 endPoint 信息 * 避免直接操作该数组 * 使用 setEndPointInfo、 getEndPointInfo */ const END_POINT_INFO_ARR: Array<EndPointInfo> = [] /** 用来查找 endPoint 的字段 */ const END_POINT_INFO_SEARCH_KEYS = ['env', 'endPointKey', 'region'] export const DEFAULT_PROTOCOL: 'http:' | 'https:' = 'https:' function findMatchedInfo(info: EndPointInfo) { // eslint-disable-next-line max-len, eqeqeq return END_POINT_INFO_ARR.find(targetInfo => END_POINT_INFO_SEARCH_KEYS.filter(searchKey => info[searchKey] != null).every(searchKey => targetInfo[searchKey] === info[searchKey],),) } export function setEndPointInfo(newInfo: EndPointInfo) { if (newInfo.protocol && !/:$/.test(newInfo.protocol)) { newInfo.protocol = `${newInfo.protocol}:` as Protocol } const endPointInfo = findMatchedInfo(newInfo) if (endPointInfo) { // eslint-disable-next-line eqeqeq if (newInfo.baseUrl != null) { endPointInfo.baseUrl = newInfo.baseUrl } // eslint-disable-next-line eqeqeq if (newInfo.protocol != null) { endPointInfo.protocol = newInfo.protocol } } else { END_POINT_INFO_ARR.push({ ...newInfo, protocol: newInfo.protocol ?? DEFAULT_PROTOCOL }) } // 保持旧代码逻辑 if (newInfo.endPointKey === 'CLOUD_API') { setUtilitiesProtocol((newInfo.protocol ?? DEFAULT_PROTOCOL) as 'http:' | 'https:') } } export function getEndPointInfo(env: string, endPointKey: EndPointKey, region?: string) { return findMatchedInfo({ env, endPointKey, region }) } export interface ISetEndPointWithKey { key: EndPointKey url?: string protocol?: 'http' | 'https' } export function setGatewayEndPointWithEnv(env: string, protocol?: Protocol, region = 'ap-shanghai') { region = region || 'ap-shanghai' let baseUrl = `//${env}.api.tcloudbasegateway.com/v1` if (!ZONE_CHINA.includes(region)) { baseUrl = `//${env}.api.intl.tcloudbasegateway.com/v1` } setEndPointInfo({ endPointKey: 'GATEWAY', env, baseUrl, protocol }) } export function setRegionLevelEndpoint(env: string, region: string, protocol?: Protocol) { const baseUrl = `//${env}.${region || 'ap-shanghai'}.tcb-api.tencentcloudapi.com/web` setEndPointInfo({ env, region, baseUrl, protocol, endPointKey: 'CLOUD_API' }) } export function getBaseEndPoint(env: string, endPointKey: EndPointKey = 'CLOUD_API') { const info = getEndPointInfo(env, endPointKey || 'CLOUD_API') const { protocol: PROTOCOL, baseUrl: BASE_URL } = info const webEndpoint = `${PROTOCOL}${BASE_URL}` // @todo 临时兼容小程序 return webEndpoint.match(/(http(s)?:)?\/\/([^/?#]*)/)[0] // return `${new URL(webEndpoint).origin}` } export enum LOGINTYPE { NULL = 'NULL', ANONYMOUS = 'ANONYMOUS', WECHAT = 'WECHAT', WECHAT_PUBLIC = 'WECHAT-PUBLIC', WECHAT_OPEN = 'WECHAT-OPEN', CUSTOM = 'CUSTOM', EMAIL = 'EMAIL', USERNAME = 'USERNAME', PHONE = 'PHONE', } export const OAUTH2_LOGINTYPE_PREFIX = 'OAUTH2'