Skip to content

Commit

Permalink
update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
wangcheng committed Nov 21, 2024
1 parent 21bee6c commit 53e3ab3
Showing 1 changed file with 13 additions and 43 deletions.
56 changes: 13 additions & 43 deletions packages/connect-miniprogram/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# connect-miniprogram

A [Connect](https://connectrpc.com) and gRPC client for Weixin Miniprogram
A [Connect](https://github.com/connectrpc/connect-es/tree/main/packages/connect-web) client library Weixin Miniprogram. Both [Connect RPC](https://connectrpc.com/docs/protocol) and [gRPC-web](https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-WEB.md) protocols are supported.

适配小程序的 [Connect](https://connectrpc.com) 客户端。可以在小程序中使用 Connect 协议和 gRPC 协议的 API。
适配小程序的 [Connect](https://connectrpc.com) 客户端。可以在小程序中使用 [Connect RPC 协议](https://connectrpc.com/docs/protocol)[gRPC-web 协议](https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-WEB.md) API。

> Connect is a family of libraries for building browser and gRPC-compatible HTTP APIs: you write a short [Protocol Buffer](https://developers.google.com/protocol-buffers) schema and implement your application logic, and Connect generates code to handle marshaling, routing, compression, and content type negotiation. It also generates an idiomatic, type-safe client in any supported language.
Expand All @@ -18,44 +18,6 @@ Connect 依赖了一些微信小程序不支持的 API。所以需要引入 Poly
| [TextEncoder](https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder) | [samthor/fast-text-encoding](https://github.com/samthor/fast-text-encoding) |
| [TextDecoder](https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder) | [samthor/fast-text-encoding](https://github.com/samthor/fast-text-encoding) |

### Method 1 Use "shimming" (recommended)

Use the bundlers "shimming" feature to replace all usage of `Headers` `TextEncoder` `TextDecoder` with the polyfill version. This is the recommended method because it doesn't pollute the global scope.

使用打包工具的 “Shimming” 功能替换代码中所有的 `Headers` `TextEncoder` `TextDecoder`。比较推荐这种方法,因为这样不会污染 global 对象。

[Webpack](https://webpack.js.org/guides/shimming/#shimming-globals):

```js
module.exports = {
plugins: [
new webpack.ProvidePlugin({
Headers: ['connect-miniprogram/shims.js', 'HeadersPolyfill'],
TextDecoder: ['connect-miniprogram/shims.js', 'FastTextDecoder'],
TextEncoder: ['connect-miniprogram/shims.js', 'FastTextEncoder'],
}),
],
};
```

[Taro](https://docs.taro.zone/docs/config-detail/#miniwebpackchain):

```js
{
webpackChain: (chain, webpack) => {
chain.plugin('shimming').use(webpack.ProvidePlugin, [
{
Headers: ['connect-miniprogram/shims.js', 'HeadersPolyfill'],
TextDecoder: ['connect-miniprogram/shims.js', 'FastTextDecoder'],
TextEncoder: ['connect-miniprogram/shims.js', 'FastTextEncoder'],
},
]);
};
}
```

### Method 1 Use "polyfill"

Import the polyfill at the start of your code:

在小程序代码最开头插入以下内容:
Expand All @@ -66,10 +28,14 @@ import 'connect-miniprogram/polyfill';

## How to use 使用方法

The usage of this library is basically the same with [@connectrpc/connect-web](https://connectrpc.com/docs/web/getting-started/). You can click the link to read its doc. You can also clone the repo and try out the `example-taro` project.
This library is a port of the official [@connectrpc/connect-web](https://github.com/connectrpc/connect-es/tree/main/packages/connect-web) library. The usage is very similar to it. You can click [this link](https://connectrpc.com/docs/web/getting-started/) to read its doc. You can also clone the repo and try out the `example-taro` project.

使用方法跟 [@connectrpc/connect-web](https://connectrpc.com/docs/web/getting-started/) 基本相同,你可以点击链接查看文档。你也可以克隆代码,用 `example-taro` 尝试。

What this library do it we port `createConnectTransport` and `createGrpcWebTransport` to work in Miniprograms. You only need to import one of These 2 functions to create a `transport` object, then use the offical package `@connectrpc/connect` to create the client.

本库做的事情只是移植了 `createConnectTransport``createGrpcWebTransport` 两个函数,使他们能在微信小程序中工作。你需要引入他们来创建一个 `transport` 对象,然后用官方包 `@connectrpc/connect` 来创建 `client` 对象

```js
import { createPromiseClient } from '@connectrpc/connect';
import {
Expand Down Expand Up @@ -106,8 +72,12 @@ async function serverStream() {
}
```

## Limitations
## Limitations 局限性

- Doesn't support interceptor and `contextValues` option.
- Doesn't support `signal` option because Weixin doesn't have `AbortSignal` API.
- Doesn't support `interceptor` and `contextValues` option. I don't have a plan to implement them because they heavily depend on `AbortSignal` API
- Doesn't support stream request body because either `fetch` or Weixin dosen't support sending stream request.

- 不支持 `signal` 选项,因为微信不支持 `AbortSignal` API。
- 不支持 `interceptor``contextValues` 选项。我也不打算实现,因为它们依赖 `AbortSignal` API。
- 不支持客户端流式请求。因为微信和浏览器都不支持发送流失请求。

0 comments on commit 53e3ab3

Please sign in to comment.