import { IErrorInfo } from '../type' import { CloudBase } from '../cloudbase' import { parse, format } from 'url' export class TcbError extends Error { public readonly code: string public readonly message: string public readonly requestId: string public constructor(error: IErrorInfo) { super(error.message) this.code = error.code this.message = error.message this.requestId = error.requestId } } export const filterValue = function filterValue(o, value) { for (let key in o) { if (o[key] === value) { delete o[key] } } } export const filterUndefined = function(o) { return filterValue(o, undefined) } // export const filterNull = function(o) { // return filterValue(o, null) // } // export const filterEmptyString = function(o) { // return filterValue(o, '') // } // export const warpPromise = function warp(fn) { // return function(...args) { // // 确保返回 Promise 实例 // return new Promise((resolve, reject) => { // try { // return fn(...args) // .then(resolve) // .catch(reject) // } catch (e) { // reject(e) // } // }) // } // } export const E = (errObj: IErrorInfo) => { return new TcbError(errObj) } export const isArray = arr => { return arr instanceof Array } export const camSafeUrlEncode = str => { return encodeURIComponent(str) .replace(/!/g, '%21') .replace(/'/g, '%27') .replace(/\(/g, '%28') .replace(/\)/g, '%29') .replace(/\*/g, '%2A') } export const map = (obj, fn) => { const o = isArray(obj) ? [] : {} for (let i in obj) { if (obj.hasOwnProperty(i)) { o[i] = fn(obj[i], i) } } return o } export const clone = obj => { return map(obj, function(v) { return typeof v === 'object' && v !== undefined && v !== null ? clone(v) : v }) } export const checkIsInScf = () => { return !!process.env.SCF_NAMESPACE } export const checkIsInContainer = () => { const { SCF_NAMESPACE } = CloudBase.getCloudbaseContext() return !!process.env.KUBERNETES_SERVICE_HOST && !process.env.SCF_NAMESPACE } export const delay = ms => { return new Promise(resolve => setTimeout(resolve, ms)) } export function second(): number { // istanbul ignore next return Math.floor(new Date().getTime() / 1000) } export function processReturn(throwOnCode: boolean, res: any) { if (throwOnCode === false) { // 不抛报错,正常return,兼容旧逻辑 return res } throw E({ ...res }) } // export function checkIsGray(): boolean { // const tcbContextConfig = getTcbContextConfig() // return tcbContextConfig[GRAY_ENV_KEY] === true // } export function getServerInjectUrl(): string { const tcbContextConfig = getTcbContextConfig() return tcbContextConfig['URL'] || '' } export function getTcbContextConfig(): any { try { const { TCB_CONTEXT_CNFG } = CloudBase.getCloudbaseContext() if (TCB_CONTEXT_CNFG) { // 检查约定环境变量字段是否存在 return JSON.parse(TCB_CONTEXT_CNFG) } return {} } catch (e) { console.log('parse context error...') return {} } } export function getWxUrl(config: any): string { const isInner = checkIsInScf() || checkIsInContainer() let protocol = config.isHttp ? 'http' : undefined let endpoint if (isInner) { endpoint = config.innerEndpoint } else { endpoint = config.endpoint } const urlObject = parse(endpoint) return format({ protocol: protocol || urlObject.protocol, host: urlObject.host, pathname: urlObject.pathname, search: urlObject.search, hash: urlObject.hash }) }