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 🙏

© 2025 – Pkg Stats / Ryan Hefner

pengjuan-cloudfunction-common-utils

v0.0.2

Published

微信小程序云函数通用工具库,提供标准化API响应、权限验证、数据验证等功能

Readme

pengjuan-cloudfunction-common-utils

微信小程序云函数通用工具库,提供标准化API响应、权限验证、数据验证等功能。

安装

npm install pengjuan-cloudfunction-common-utils

功能特性

  • 🚀 标准化API响应格式
  • 🔐 用户权限验证
  • ✅ 数据验证工具
  • 📊 数据处理工具
  • 🛡️ 统一错误处理
  • 📄 分页响应支持

使用方法

导入模块

const {
  ApiResponse,
  AuthUtils,
  ValidationUtils,
  DataUtils,
  withErrorHandler,
  db,
  cloud
} = require('pengjuan-cloudfunction-common-utils')

ApiResponse - API响应格式化

成功响应

// 基本成功响应
return ApiResponse.success(data, '操作成功')

// 分页响应
return ApiResponse.paginated(list, total, page, pageSize, '获取成功')

错误响应

return ApiResponse.error('操作失败', -1, error)

AuthUtils - 权限验证

验证用户登录状态

try {
  const user = await AuthUtils.validateUser(wxContext)
  console.log('用户信息:', user)
} catch (error) {
  return ApiResponse.error(error.message)
}

验证活动创建者权限

try {
  await AuthUtils.validateActivityCreator(activityId, userId)
  // 权限验证通过,继续执行
} catch (error) {
  return ApiResponse.error(error.message)
}

验证记录所有者权限

try {
  await AuthUtils.validateRecordOwner(recordId, userId)
  // 权限验证通过,继续执行
} catch (error) {
  return ApiResponse.error(error.message)
}

ValidationUtils - 数据验证

验证必填字段

try {
  ValidationUtils.validateRequired(params, ['name', 'email', 'phone'])
  // 验证通过,继续执行
} catch (error) {
  return ApiResponse.error(error.message)
}

验证活动数据

try {
  ValidationUtils.validateActivity(activityData)
  // 验证通过,继续执行
} catch (error) {
  return ApiResponse.error(error.message)
}

DataUtils - 数据处理

数据类型转换

// 转换为字符串
const str = DataUtils.toString(value)

// 转换为数字
const num = DataUtils.toNumber(value, 0) // 默认值为0

字段过滤

const filteredData = DataUtils.filterFields(data, ['name', 'email', 'phone'])

添加时间戳

// 创建时添加时间戳
const dataWithTimestamps = DataUtils.addTimestamps(data)

// 更新时添加时间戳
const updatedData = DataUtils.addTimestamps(data, true)

withErrorHandler - 统一错误处理

exports.main = withErrorHandler(async (event, context) => {
  // 你的云函数逻辑
  const result = await someAsyncOperation()
  return ApiResponse.success(result)
})

完整示例

const {
  ApiResponse,
  AuthUtils,
  ValidationUtils,
  DataUtils,
  withErrorHandler
} = require('@pengjuan/cloudfunction-utils')

exports.main = withErrorHandler(async (event, context) => {
  const { name, description, startTime } = event
  
  // 验证用户登录
  const user = await AuthUtils.validateUser(context)
  
  // 验证必填字段
  ValidationUtils.validateRequired(event, ['name', 'description', 'startTime'])
  
  // 数据处理
  const activityData = DataUtils.addTimestamps({
    name: DataUtils.toString(name),
    description: DataUtils.toString(description),
    startTime: new Date(startTime),
    creatorId: user._id
  })
  
  // 保存到数据库
  const result = await db.collection('activities').add({
    data: activityData
  })
  
  return ApiResponse.success(result, '活动创建成功')
})

API 文档

ApiResponse

  • success(data, message) - 成功响应
  • error(message, code, error) - 错误响应
  • paginated(list, total, page, pageSize, message) - 分页响应

AuthUtils

  • validateUser(wxContext) - 验证用户登录状态
  • validateActivityCreator(activityId, userId) - 验证活动创建者权限
  • validateRecordOwner(recordId, userId) - 验证记录所有者权限

ValidationUtils

  • validateRequired(params, requiredFields) - 验证必填字段
  • validateActivity(activity) - 验证活动数据

DataUtils

  • toString(value) - 转换为字符串
  • toNumber(value, defaultValue) - 转换为数字
  • filterFields(obj, allowedFields) - 字段过滤
  • addTimestamps(data, isUpdate) - 添加时间戳

依赖要求

  • Node.js >= 12.0.0
  • wx-server-sdk >= 2.0.0

许可证

MIT

贡献

欢迎提交 Issue 和 Pull Request!

更新日志

0.0.2

  • 初始版本发布
  • 提供基础的API响应、权限验证、数据验证功能