npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

baas_js

v1.0.12

Published

一个轻量的前端 BaaS SDK,提供身份认证、数据表 CRUD、以及自定义 API 调用能力。

Readme

baas_js

一个轻量的前端 BaaS SDK,提供身份认证、数据表 CRUD、以及自定义 API 调用能力。

支持链式调用,Promise 风格;自动管理令牌并在请求头中携带。

安装

npm install baas_js
# 或
yarn add baas_js

如果你在本地仓库直接使用源码,先打包:

npm run build

打包产物在 dist/,主入口为 dist/index.cjs.js (CJS) 与 dist/index.esm.js (ESM)。

快速开始

import { createClient } from "baas_js"; // ESM

const client = createClient({
  baseUrl: "https://your-baas.example.com",
  apiKey: "YOUR_API_KEY",
});

// 1) 登录
await client.auth.login({ phone: "13800000000", password: "******" });

// 2) 查询数据
const list = await client.db
  .from("todos")
  .list()
  .eq("status", "open")
  .order("created_at", "desc");

// 3) 创建数据
const created = await client.db
  .from("todos")
  .insert()
  .values({ title: "Buy milk", status: "open" });

// 4) 调用后端自定义 API
const result = await client.api
  .call("send_sms")
  .param("to", "+8613800000000")
  .param("text", "Hello")
  .header("X-Trace-Id", "abc123");

配置

  • baseUrl:后端服务基础地址(例如 https://api.example.com)。
  • apiKey:公钥,将通过请求头 CODE_FLYING 发送。

SDK 会将登录后返回的 token 保存在 localStoragebaas_token 键下,并在后续请求通过 Authorization: Bearer <token> 自动携带。

API 参考

createClient(config)

返回一个带有以下模块的客户端:

  • auth:认证模块
  • db:数据表模块
  • api:自定义 API 调用模块

auth 模块

  • login({ user_name?, phone?, email?, password })

    • 三选一:user_name / phone / email,必须提供其一;同时必须提供 password
    • 成功后会自动保存 token ,localStorage 存储key baas_token
    • 请求:POST /login/passwd,请求体:{ phone: account, password }
  • getUser

    • 获取当前登录的用户信息。返回的对象结构与注册接口的对象结构一致。
    • 不用使用 db.form().get()来获取用户。
  • logout()

    • 清除本地 token,并调用 GET /logout
  • register(data)

    • 注册接口,对象类型,包含要添加的字段数据,字段名必须与数据集的字段名一致。
    • 注册流程请调用 auth.register(),不要使用 db.insert("users")

用法示例:

await client.auth.login({ phone: "13800000000", password: "******" });
await client.auth.register({  })
await client.auth.logout();

db 模块

入口:client.db.from(table),返回一个查询构建器,支持:

  • 读取:list()get()
  • 写入:insert()update()delete()

过滤与控制:

  • 等值与比较:eqneqgtgteltlte
  • 集合与区间:inbetween
  • 组合条件:or(cb)(顶层 or,cb 内可继续使用上述过滤器)
  • 排序:order(field, directionOrOptions)asc/desc 或对象 { ascending: boolean }
  • 分页:page(number, size)(设置 currentpageSize

链式后缀:所有构建器都支持 Promise 风格,直接 await 即可。

示例:

// 查询 + 过滤 + 排序 + 分页
const res = await client.db
  .from("orders")
  .list()
  .eq("status", "paid")
  .or((q) => q.lt("amount", 100).gt("discount", 0))
  .order("created_at", { ascending: false })
  .page(1, 20);

// 插入
const inserted = await client.db
  .from("orders")
  .insert()
  .values({ amount: 199, status: "paid" });

// 更新
const updated = await client.db
  .from("orders")
  .update()
  .set({ status: "closed" })
  .eq("id", 123);

// 删除
const removed = await client.db
  .from("orders")
  .delete()
  .eq("id", 123);

api 模块

方法:

  • call(apiName):开始构建
  • param(key, value) / params(obj):设置请求体参数
  • header(key, value) / headers(obj):追加自定义请求头

示例:

const data = await client.api
  .call("send_email")
  .params({ to: "[email protected]", subject: "Hi" })
  .header("X-Request-Id", "rid-001");

请求与头信息

所有请求均通过内部 client.request(path, options) 发送:

  • Content-Type: application/json
  • CODE_FLYING: <apiKey>
  • Authorization: Bearer <token>(当已登录)

运行环境注意

  • 该 SDK 依赖 fetchlocalStorage。在浏览器环境可直接使用;若在 Node.js 环境使用,请自行提供相应 polyfill(如 undici/node-fetch 以及 localStorage 的替代实现)。

开发与构建

  • 构建:npm run build(使用 Rollup)
  • 主要源码:src/
    • 入口:src/index.js(导出 createClient
    • 客户端:src/client.js
    • 模块:src/modules/{auth,db,api}.js

许可

本项目使用 ISC 许可。