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

hwj-common-lib

v1.1.0

Published

微信小程序云开发通用模块库,支持灵活的环境配置

Downloads

17

Readme

hwj-common-lib

微信小程序云开发通用模块库

功能特性

  • 统一的错误处理
  • JWT 认证
  • 数据库操作封装
  • 缓存管理
  • 灵活的环境配置

安装

npm install hwj-common-lib

使用方法

基本用法

const { 
  configure,
  ValidationError,
  LoginError,
  errorHandler,
  Database,
  Cache
} = require('hwj-common-lib')

// 配置环境
configure({
  CLOUD_ENV: 'your-cloud-environment-id'
})

// 使用数据库
const userDB = new Database('users')
const users = await userDB.find({ status: 'active' })

// 使用缓存
const cache = new Cache('namespace')
await cache.set('key', 'value', 3600) // 缓存1小时

// 错误处理
try {
  // 业务代码
} catch (error) {
  return errorHandler(error)
}

配置优先级

环境配置采用以下优先级顺序:

  1. 通过 configure() 函数提供的配置
  2. 环境变量 (process.env.CLOUD_ENV 等)
  3. 默认配置值

在云函数中使用

在微信小程序云函数中使用时,建议在云函数入口处进行配置:

const { configure } = require('hwj-common-lib')

// 在云函数入口处配置
configure({
  CLOUD_ENV: 'your-cloud-environment-id'
})

exports.main = async (event, context) => {
  // 您的云函数代码...
}

API 文档

配置函数

  • configure(config): 配置库的环境和选项
  • getCurrentConfig(): 获取当前配置
  • initializeCloud(cloudEnv): 初始化云环境

Database 类

  • create(data): 创建记录
  • update(id, data): 更新记录
  • find(query, options): 查询记录
  • count(query): 统计记录数
  • delete(id): 删除记录

Cache 类

  • set(key, value, ttl): 设置缓存
  • get(key): 获取缓存
  • del(key): 删除缓存
  • batchDelete(pattern): 批量删除缓存

错误处理

  • ValidationError: 参数验证错误
  • BusinessError: 业务逻辑错误
  • LoginError: 登录相关错误
  • TokenError: Token相关错误
  • DeviceError: 设备相关错误

认证相关

  • generateToken(payload): 生成JWT token
  • verifyToken(token): 验证JWT token
  • auth(event): 认证中间件