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

@cloudbase/page-module

v1.9.0

Published

单页模板 SDK,该 SDK 同时支持 `云函数端` 和 `微信小程序端` 使用,内部自动识别运行环境,场景模块接口保持一致。

Downloads

165

Readme

@cloudbase/page-module

单页模板 SDK,该 SDK 同时支持 云函数端微信小程序端 使用,内部自动识别运行环境,场景模块接口保持一致。

Note: 云函数端使用,依赖的 wx-server-sdk 需自行安装。

Install

npm install @cloudbase/page-module
yarn add @cloudbase/page-module

Usage

初始化 PageModule 实例:

import { PageModule } from '@cloudbase/page-module'

const pageModule = new PageModule('tcb:SignUp')

调用接口

pageModule.callMethod('check_sign_able', {
  a: 1,
  b: 2
})

操作数据库

pageModule.database().collection('your_datasource_name').add({
  name: 'test'
})

API

Class: PageModule

  • init(moduleName: string, options: IPageModuleOptions = {}): PageModule - 初始化 PageModule 实例
    • moduleName: string - 单页模板模块名称
    • options - 可选参数
      • cloudbaseInstanceInitOptions - 初始化 CloudBase 实例的参数,请参考 sdk init 方法 参数
  • callMethod(methodName: string, data: object) - 调用接口
    • methodName: string - 数据源方法名称
    • data: object - 数据源参数,如: {a:1, b:2}
  • database(): Database - 获取数据库实例,实例说明见:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-sdk-api/Cloud.database.html

cloudbase - 默认已使用当前云函数所在环境初始化的 wx-server-sdk 实例

import { cloudbase } from '@cloudbase/page-module'

Note: 非云函数环境下,无当前环境,将使用默认环境,如不满足需求,请使用 wx-server-sdk 自行初始化实例

callSelfDefinedMethod(event, context) - 调用自定义方法

  • event - 云函数调用时传入的参数
    • methodName - 方法名
    • params - 方法参数
  • context - 云函数调用时传入的参数

以下为使用示例(TS版本)

目录结构:

$ tree -L 2
.
├── README.md
├── api
│   └── doSomething.ts    # 自定义方法
├── index.ts              # 云函数入口函数
├── node_modules
└── package.json

云函数入口函数:index.ts,该文件内通过 callSelfDefinedMethod 实现对自定义方法的调用

const { callSelfDefinedMethod } = require('@cloudbase/page-module')

exports.main = async function (event, context) {
  return callSelfDefinedMethod(event, context)
}

如有如下自定义 doSomething 方法:apis/doSomething.ts

const { cloudbase } = require('@cloudbase/page-module')

module.exports = async function doSomething(event, context) {
  console.log(event, context, 'doSomething')
  const result = await cloudbase.callFunction({
    name: 'doSomething',
  })

  console.log(result, 'cloudbase.callFunction')
}

如下为对入口函数进行调用的参数示例:

main({
  methodName: 'doSomething', params: {a: 1, b: 2, c: 3}
}, {
  hello: 'world'
})

isInvokeByServerSide(): boolean - 判断调用来源是否是服务器端,包括云函数

isInvokeByCloudFunction(): boolean - 判断调用来源是否是云函数