JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 2
  • Score
    100M100P100Q28071F
  • License MIT

Package Exports

  • connect-miniprogram
  • connect-miniprogram/dist/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (connect-miniprogram) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

connect-miniprogram

A Connect client for Wexin Miniprogram

适配小程序的 Connect 客户端。可以在小程序中使用访问 Connect 生成的服务。支持 GRPC 和流式请求。

Polyfill

Connect libraries relys on some APIs that not provided in Wexin environment.

API Polyfilled by
Headers mswjs/headers-polyfill
TextEncoder samthor/fast-text-encoding
TextDecoder samthor/fast-text-encoding

Import the polyfill at the start of your code:

import 'connect-miniprogram/polyfill';

How to use

The usage of this library is basically the same with @bufbuild/connect-web. You can click the link to read its doc.

import { createPromiseClient } from '@bufbuild/connect';
import {
  createConnectTransport,
  createGrpcWebTransport,
} from 'connect-miniprogram';
import { ElizaService } from '@buf/bufbuild_eliza.bufbuild_connect-es/buf/connect/demo/eliza/v1/eliza_connect';

const connectTransport = createConnectTransport({
  baseUrl: 'https://demo.connect.build',
  // You need to mannualy pass the request function. You can also pass functions from 3rd party frameworks like `Taro.requst`, as long as they are compatible with Weixin's API
  request: wx.request,
});

// You can also use create a grpc-web transport. The usage is the same.
const grpcWebTransport = createGrpcWebTransport({
  baseUrl: 'https://demo.connect.build',
  request: wx.request,
});

const client = createPromiseClient(ElizaService, connectTransport);

async function unary() {
  const res = await client.say({
    sentence: 'I feel happy.',
  });
  console.log(res);
}

async function serverStream() {
  for await (const res of client.introduce({ name: 'Joseph' })) {
    console.log(res);
  }
}

Limitations

  • Doesn't support interceptor (yet). I will add this support in the future.
  • Doesn't support AbortSignal because Wexin doesn't have AbortSignal API.
  • Doesn't support stream request body because either fetch or Wexin dosen't support sending stream request.