@wxcloud/cloud-sdk

1.0.4 • Public • Published

@wxcloud/cloud-sdk

微信云开发团队提供了Web SDK,支持在 Web 中访问云开发资源。 但 Web SDK 只支持常规 Web 应用(即浏览器环境)的开发,不兼容其他类 Web 平台。由于类 Web 平台在网络络请求、本地存储等特性上与浏览器环境有明显差异,现提供 @wxcloud/cloud-sdk 以支持跨端适配的能力。在传入不同平台适配对象之后,在其他类 Web 平台也可以访问微信云开发资源。

安装

npm install @wxcloud/cloud-sdk --save

使用

@wxcloud/cloud-sdk 导出了 useAdapterinitCloud 方法。先通过 useAdapter 传入跨端的适配对象,然后调用 initCloud 获取 cloud 对象,即引入 Web SDK 时自动挂载在 window 下的 cloud 。

const { useAdapter, initCloud } = require('@wxcloud/cloud-sdk')
import { adapters } from './example/adapters'

// platform 可传入标识平台的字符
useAdapter({ adapters, platform: 'platform' })
const cloud = initCloud()

在浏览器环境中使用可以不调用 useAdapter

const { initCloud } = require('@wxcloud/cloud-sdk')

const cloud = initCloud()

适配对象

目前需要适配的对象有 sessionStorage、localStorage、network、reqClass、webSocketClass。 其需要实现的接口如下所示:

interface IWXCloudSdkAdapterInterface {
  reqClass: IRequestEngine
  sessionStorage: IStorageAdapter
  localStorage: IStorageAdapter
  webSocketClass: IWebSocketAdapter
  network: INetworkAdapter
}

type onNetworkStatusChangeCb = (res: {
  isConnected: boolean
  networkType: 'wifi' | '4g' | '3g' | '2g' | 'unknown' | 'none'
}) => void

interface INetworkAdapter {
  getNetWorkType: () => string
  onNetworkStatusChange: (cb: onNetworkStatusChangeCb) => void
}

interface IStorageAdapter {
  setItem: (key: string, value: string) => void
  getItem: (key: string) => string | null
}

interface IWebSocketAdapter {
  new (url: string, protocols?: string[]): IWebSocketAdapter
  readyState: number
  send: (data: string | ArrayBuffer) => void
  close: (code?: number | undefined, reason?: string | undefined) => void
}

对于适配逻辑较复杂的 reqClass, @wxcloud/cloud-sdk 提供 requestAdapterWrapper 包装方法来降低适配难度。 对于开发者,只需要完成以下 IRequestAdapterInterface 接口的实现后传入 requestAdapterWrapper 即可。

interface IRequestParam {
  url: string
  data: string | ArrayBuffer
  header: AnyObject
  method: string
  dataType: string
  responseType: string
  timeout: number
}

interface IRequestAdapterCallbackResponse {
  statusCode: number | string
  response: string | object
  headers: { [key: string]: any }
  statusMessage: string
}

type IRequestAdapterInterface = (req: IRequestParam, cb: (resp: IRequestAdapterCallbackResponse) => void) => void

为方便开发者理解适配器需完成的逻辑,我们以在微信小程序中使用 @wxcloud/cloud-sdk 为例,提供了所需适配对象的完整适配实现,具体可参考 example/adapters.js

支持 customDomain

由于 第三方 Cookie 限制,在浏览器环境中使用登录模式时,需要开发者自建服务器转发 https://servicewechat.com/wxa-qbase/ 的请求。在非浏览器环境内,支持在实例化 cloud.Cloud 时传入 customDomain 指定转发的域名,如果未传入,默认直接请求 https://servicewechat.com/wxa-qbase/

cosnt c = new cloud.Cloud({
  identityless: true,
  resourceAppid: 'appid', // miniprogram appid
  resourceEnv: 'env',
  config: { // optional
    customDomain: 'https://user-sever.com'
  }
})

说明

  • downloadFile、uploadFile 暂不支持适配。
  • 公众号登录相关的 api 在微信客户端之外调用无法正常完成相应逻辑。

对于 @wxcloud/cloud-sdk 的问题,欢迎在微信开放社区发帖提问。

Readme

Keywords

Package Sidebar

Install

npm i @wxcloud/cloud-sdk

Weekly Downloads

219

Version

1.0.4

License

MIT

Unpacked Size

667 kB

Total Files

5

Last publish

Collaborators

  • haotianlong
  • lehoyan
  • dengkunli
  • starkwang
  • wechat-miniprogram