@wps365-open/appbase-js
v0.0.8
Published
JavaScript SDK for AppBase BaaS — Database, Auth, Realtime, Storage, Functions
Keywords
Readme
@wps365-open/appbase-js
AppBase JavaScript SDK — 为 AppBase BaaS 平台提供的客户端 SDK。
支持 Database、Auth、Realtime、Storage、Functions 五大模块,链式查询 API,单包架构,零运行时依赖。
安装
pnpm add @wps365-open/appbase-js快速开始
import { createClient } from '@wps365-open/appbase-js'
const client = createClient({ projectId: 'your-project-id' })数据库查询
// 查询
const { data, error } = await client
.from('notes')
.select('id, title, content')
.eq('pinned', true)
.order('created_at', { ascending: false })
.limit(10)
// 插入
const { data, error } = await client
.from('notes')
.insert({ title: 'Hello', content: 'World' })
// 更新
const { data, error } = await client
.from('notes')
.update({ pinned: true })
.eq('id', '123')
// 删除
const { data, error } = await client
.from('notes')
.delete()
.eq('id', '123')认证
// 获取当前用户
const { data: { user }, error } = await client.auth.getUser()
// 获取会话
const { data: { session } } = await client.auth.getSession()实时订阅
const channel = client
.channel('db-changes')
.on('postgres_changes',
{ event: '*', table: 'notes' },
(payload) => console.log('Change:', payload)
)
.subscribe((status) => console.log('Status:', status))
// 取消订阅
client.removeChannel(channel)文件存储
// 上传
const { data, error } = await client.storage
.from('avatars')
.upload('user/avatar.png', file)
// 获取公开 URL
const { data: { publicUrl } } = client.storage
.from('avatars')
.getPublicUrl('user/avatar.png')Edge Functions
const { data, error } = await client.functions.invoke('hello-world', {
body: { name: 'World' }
})错误处理
所有 API 调用返回 { data, error } 元组,永不抛出异常:
const { data, error } = await client.from('notes').select()
if (error) {
console.error(error.message) // string
console.error(error.code) // number | string
} else {
console.log(data)
}插件扩展
通过 client.use() 注册自定义插件:
import type { Plugin } from '@wps365-open/appbase-js'
const myPlugin: Plugin = {
name: 'analytics',
install(client, fetchClient) {
// 扩展 client 能力
}
}
client.use(myPlugin)构建产物
| 格式 | 文件 | 用途 |
| ----- | --------------------------------------- | ------------------------- |
| ESM | dist/index.mjs | 现代打包工具 (Vite, Webpack 5+) |
| CJS | dist/index.cjs | Node.js / 传统打包工具 |
| Types | dist/index.d.mts / dist/index.d.cts | TypeScript 类型支持 |
开发
pnpm install # 安装依赖
pnpm build # 构建
pnpm test # 运行测试
pnpm typecheck # 类型检查License
MIT
