
Security News
Feross on the 10 Minutes or Less Podcast: Nobody Reads the Code
Socket CEO Feross Aboukhadijeh joins 10 Minutes or Less, a podcast by Ali Rohde, to discuss the recent surge in open source supply chain attacks.
@cloudbase/cloud-api
Advanced tools
云 API 3.0 请求封装,仅适用于 Node,100% 测试覆盖率。

支持 API 秘钥或临时秘钥鉴权。
import { CloudApiService } from '../src'
const service = new CloudApiService({
service: 'tcb',
credential: {
secretId: 'xxx',
secretKey: 'xxx',
token: 'xxx'
}
})
service.request('DescribeEnvs').then(console.log)
或
const { CloudApiService } = require('@cloudbase/cloud-api')
const service = new CloudApiService({
service: 'tcb',
credential: {
secretId: 'xxx',
secretKey: 'xxx'
}
})
service.request('DescribeEnvs').then(console.log)
结果
{
EnvList: [],
RequestId: '09a1ece6-7cb1-4a8f-b8b6-e4cc066d1fbe'
}
在某些情况下,你的 credential 可能是异步获取。你可以配置 getCredential 方法,异步传递 credential 信息。建议结合 getInstance() 方法使用,避免重复请求获取 credential 信息。
const tcbService = CloudApiService.getInstance({
service: 'tcb',
getCredential: async () => {
// 你的异步逻辑
return new Promise(resolve => {
setTimeout(() => {
resolve({})
}, 1000)
})
}
})
const res = await tcbService.request('DescribeEnvs')
定义:async request(action: string, data: Record<string, any> = {}, method: 'POST' | 'GET' = 'POST')
使用 request 方法发起请求。
getInstance() 静态方法内部是一个工厂函数,会根据服务类型缓存实例,能有效避免重复初始化实例带来的开销
const tcbService = CloudApiService.getInstance({
credential,
service: 'tcb'
})
const res = await tcbService.request('DescribeEnvs', {
empty: null,
b: undefined
})
在某些特殊的情况下,你可能需要在不退出进程的情况下重载 credential。你可以使用 clearCredentialCache() 方法清除内存中的 credential 缓存,使得下次请求前通过 getCredential 函数重新获取 credential
const tcbService = new CloudApiService({
service: 'tcb',
getCredential: () => {
return new Promise(resolve => {
setTimeout(() => {
resolve(credential)
}, 1000)
})
}
})
const res = await tcbService.request('DescribeEnvs')
tcbService.clearCredentialCache()
// 重新加载 credential
const res = await tcbService.request('DescribeEnvs')
下面是 TS 定义
interface ServiceOptions {
// 服务名,如 tcb scf
service: string
// 服务版本,如 2018-06-14
version?: string
// http 请求代理,如 http://127.0.0.1:1235
// 需要准确声明,本 SDK 不会自动解析 proxy
proxy?: string
// 超时时间,毫秒
timeout?: number
// 基本参数,会被拼接到请求参数中
baseParams?: Record<string, any>
// 身份信息
credential?: Credential
// 获取身份信息的函数
getCredential?: () => Promise<Credential> | Credential
}
interface Credential {
secretId: string
secretKey: string
token?: string
}
FAQs
The cloud api request package.
The npm package @cloudbase/cloud-api receives a total of 446 weekly downloads. As such, @cloudbase/cloud-api popularity was classified as not popular.
We found that @cloudbase/cloud-api demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 14 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Socket CEO Feross Aboukhadijeh joins 10 Minutes or Less, a podcast by Ali Rohde, to discuss the recent surge in open source supply chain attacks.

Research
/Security News
Campaign of 108 extensions harvests identities, steals sessions, and adds backdoors to browsers, all tied to the same C2 infrastructure.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.