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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@mpneon/cloud

v1.3.0

Published

微信小程序云函数框架

Downloads

1

Readme

@mpneon/cloud

微信小程序云函数框架

特性

  • 云函数路由(代码共享)
  • 定时任务(支持多个)

起步

新建一个云函数 neon,安装 @mpneon/cloud 作为它的依赖。

用例

路由

使用 app.route() 方法创建路由。

// sum.js
module.exports = (request) => {
    const { a, b } = request.event

    return a + b
}
// index.js
const app = new (require('@mpneon/cloud').Application)();

app.route('sum', require('./sum'))

exports.main = (event, context) => app.handle(event, context);

在小程序端调用时,调用云函数 neon,并传入 $path 字段表示要访问的函数路由。

// app.js
wx.cloud.callFunction({
  // 云函数名称
  name: 'neon',
  // 传给云函数的参数
  data: {
    $path: 'sum',
    a: 1,
    b: 2,
  },
  success: function(res) {
    console.log(res.result) // 3
  },
  fail: console.error
})

使用云数据库

你可以使用 use('db') 方法来获取云数据库实例。

// users.js
module.exports = (request) => {
    const db = use('db')

    const { data } = await db.collection('users').get()

    return data
}

全局方法 use() 还可用于获取其它模块实例,如:

  • use('cloud') 获取 wx.cloud

获取当前请求用户

你可以从 request.user 字段(异步)获取发起当前请求的用户。

// users.js
module.exports = async (request) => {

    const user = await request.user

    return user
}

默认情况下,会从云数据库的 users 集合中查询 _id 为当前 OPENID 的记录。如果你需要自定义解析当前用户的方法,使用 app.useUserResolver((openid, requestcontext) => Promise<any>)

// index.js

app.route('user', require('./user'))

app.useUserResolver((openid) => new Promise(resolve => {
  const db = use('db')
  // 取 users 集合中 openid 字段为 OPENID 的记录
  db.collection("users")
    .where({
      openid
    })
    .get()
    .then(({ data: users}) => resolve(users[0] || null));
}))

exports.main = (event, context) => app.handle(event, context)

定时任务

若要使用定时任务,须根据小程序文档设置一个每分钟执行的定时触发器。

// config.json
{
  "triggers": [
    {
      "name": "neon.schedule",
      "type": "timer",
      "config": "0 * * * * * *"
    }
  ]
}

然后使用 app.cron() 方法注册你的定时任务。

// task.js
module.exports = (request) => {
    console.log('Taske invoked every other minute')
}
// index.js
const app = new (require('@mpneon/cloud').Application)();

app.route('sum', require('./sum'))

app.cron('*/2 * * * *', require('./task'))

exports.main = (event, context) => app.handle(event, context);

注意 app.cron() 方法只支持标准 Cron 表达式,即不支持秒和年。

License

MIT Licensed.