Skip to content

Commit 0dc446f

Browse files
authored
update fetch guide doc (#509)
1 parent be2e117 commit 0dc446f

File tree

4 files changed

+92
-99
lines changed

4 files changed

+92
-99
lines changed
Lines changed: 32 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
# Networking
22

3-
Many mobile apps need to load resources from remote URLs. You might want to make a POST request to a REST API, or you might need to fetch a large amount of static content from another server.
3+
Many mobile apps need to load resources from remote URLs. You might want to make a POST request to a REST API or fetch a large amount of static content from another server.
44

55
## Using Fetch
66

77
:::tip
88

9-
This feature depends on the http service provided by the integrated [Lynx Service](/guide/start/integrate-with-existing-apps).
9+
This feature depends on the HTTP service provided by the integrated [Lynx Service](/guide/start/integrate-with-existing-apps).
1010

1111
:::
1212

13-
Lynx provides the [Fetch API](/api/lynx-api/global/fetch) with the same usage as the Web. You can refer to the MDN guides on [Using Fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch) for more information.
13+
Lynx provides a [Fetch API](/api/lynx-api/global/fetch) that is compatible with the standard Web API. You can refer to the MDN guide on [Using Fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch) for more information.
1414

15-
This is an example app that fetches and displays user posts provided by the [JSONPlaceholder API](https://jsonplaceholder.typicode.com/). It initializes with a loading state and, upon mounting, triggers a Fetch API request to retrieve posts. The fetched data is then displayed in a scrollable list where each post is shown with its ID and title. If the request is still in progress, a "Loading..." message appears.
15+
This example app fetches and displays user posts from the [JSONPlaceholder API](https://jsonplaceholder.typicode.com/). It initializes with a loading state and triggers a Fetch API request to retrieve posts upon mounting. The fetched data is then displayed in a scrollable list, showing each post's ID and title. A "Loading..." message appears if the request is still in progress.
1616

1717
import { Go } from '@lynx';
1818

@@ -24,15 +24,15 @@ import { Go } from '@lynx';
2424
entry="src/fetch"
2525
/>
2626

27-
### Making requests
27+
### Making Requests
2828

29-
In order to fetch content from an arbitrary `URL`, you can pass the `URL` to `fetch`:
29+
To fetch content from an arbitrary `URL`, you can pass the `URL` to `fetch`:
3030

3131
```typescript
3232
fetch('https://jsonplaceholder.typicode.com/todos/1');
3333
```
3434

35-
`fetch` also takes an optional second argument that allows you to customize the HTTP request. You may want to specify additional `headers`, make a `POST` request, or provide JSON-based `body`:
35+
`fetch` also takes an optional second argument that allows you to customize the HTTP request. You may want to specify additional `headers`, make a `POST` request, or provide a JSON `body`:
3636

3737
```typescript
3838
fetch('https://jsonplaceholder.typicode.com/todos/1', {
@@ -50,11 +50,11 @@ fetch('https://jsonplaceholder.typicode.com/todos/1', {
5050

5151
Take a look at the [Fetch Request](/api/lynx-api/global/fetch#request) for the properties supported by Lynx.
5252

53-
### Handling the response
53+
### Handling the Response
5454

5555
The above examples show how you can make a request. In many cases, you will want to do something with the response.
5656

57-
Networking is an inherently asynchronous operation. `fetch` method will return a `Promise` that makes it straightforward to write code that works in an asynchronous manner. You can use the `async/await` syntax to wait the `promise` to end:
57+
Networking is an inherently asynchronous operation. The `fetch` method returns a `Promise`, which makes it straightforward to write code that works asynchronously. You can use the `async/await` syntax to await the promise's resolution:
5858

5959
```typescript
6060
const getDataFromApiAsync = async () => {
@@ -72,69 +72,58 @@ const getDataFromApiAsync = async () => {
7272

7373
Take a look at the [Fetch Response](/api/lynx-api/global/fetch#response) for the properties supported by Lynx.
7474

75-
Don't forget to catch any errors that may be thrown by `fetch`, otherwise they will be dropped silently.
75+
Don't forget to catch any errors that `fetch` may throw; otherwise, they will be silently ignored.
7676

77-
## Fetch chunk streaming
77+
## Chunked Transfer Encoding
7878

79-
Chunked encoding is useful when larger amounts of data are sent to the Front-End. Front-End can process each chunk
80-
when the chunk is received, to reduce waiting duration.
79+
Chunked Transfer Encoding is ideal for streaming large amounts of data to the client. This method allows the client to process data in chunks as they arrive, reducing overall latency.
8180

82-
You can refer to the MDN guides on [Transfer-Encoding chunked](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Transfer-Encoding#chunked)
83-
to know more about chunk streaming standard.
81+
To learn more about this standard, see the MDN guide on [Chunked transfer encoding](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Transfer-Encoding#chunked).
8482

8583
:::tip
8684

87-
To facilitate Front-End development, the http response body is split by Lynx:
85+
To facilitate front-end development, the HTTP response body is processed by Lynx:
8886

89-
The chunk size and '\r\n' delimiters are removed, only the chunk contents are returned as `arraybuffer`.
87+
The chunk size and `\r\n` delimiters are removed, and only the chunk's content is returned as an `arraybuffer`.
9088

9189
:::
9290

93-
To enable chunk streaming, you can mark switch `useStreaming` as `true` inside `lynxExtension`.
91+
To enable chunked transfer encoding, set the `useStreaming` option to `true` inside `lynxExtension`.
9492

95-
```typescript
96-
fetch('https://e3e0932a-9522-4aec-aec9-12cba94b0c7f.mock.pstmn.io/chunk', {
97-
lynxExtension: { useStreaming: true },
98-
}).then((response) => {
99-
const reader = response.body.getReader();
100-
while (true) {
101-
const { done, value } = await reader.read();
102-
if (done) {
103-
break;
104-
} else {
105-
const text = TextCodecHelper.decode(value);
106-
setData(text);
107-
}
108-
}
109-
});
110-
```
93+
<Go
94+
example="networking"
95+
defaultFile="src/chunked-encoding/index.tsx"
96+
defaultEntryFile="dist/chunked-encoding.lynx.bundle"
97+
entry="src/chunked-encoding"
98+
/>
11199

112100
You can refer to the MDN guides on [Response: body](https://developer.mozilla.org/en-US/docs/Web/API/Response/body)
113101
to know more about how to process streaming body.
114102

115103
## Using TextCodecHelper
116104

117-
Due to lack of support of `TextEncoder/TextDecoder` in `PrimJS`, to support
118-
basic usages of encoding and decoding, Lynx provides `TextCodecHelper`. This
119-
class supports conversions between `string` and `arraybuffer` under `utf-8`.
120-
How to use:
105+
Due to the lack of `TextEncoder`/`TextDecoder` support in `PrimJS`, Lynx provides
106+
`TextCodecHelper` for basic encoding and decoding operations. This class
107+
supports `UTF-8` conversions between `string` and `arraybuffer`.
108+
109+
Usage:
121110

122-
convert `arraybuffer` to `string`
111+
Convert `arraybuffer` to `string`:
123112

124113
```typescript
125114
const str = TextCodecHelper.decode(arraybuffer);
126115
```
127116

128-
convert `string` to `arraybuffer`
117+
Convert `string` to `arraybuffer`:
129118

130119
```typescript
131120
const arraybuffer = TextCodecHelper.encode(str);
132121
```
133122

134123
## Using Other Networking Libraries
135124

136-
Since The Fetch API is built into Lynx. This means that you can use third party libraries that depend on it.
125+
The Fetch API is built into Lynx, which means you can use third-party libraries that rely on it.
137126

138-
It is important to note that Lynx's Fetch API has subtle differences compared to the [Web Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API). You can check the [Fetch API Reference - Compatibility](/api/lynx-api/global/fetch#compatibility) section to learn more about these differences. As a result, you may need to adapt libraries from the Web ecosystem to ensure compatibility. If you encounter any issues on Lynx Fetch API, you are welcome to submit feature requests or [contribute](https://github.com/lynx-family/lynx/blob/develop/CONTRIBUTING.md) to help Lynx better support the Web ecosystem.
127+
It is important to note that Lynx's Fetch API has subtle differences compared to the [Web Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API). You can check the [Fetch API Reference - Compatibility](/api/lynx-api/global/fetch#compatibility) section to learn more about these differences. As a result, you may need to adapt libraries from the web ecosystem to ensure compatibility. If you encounter any issues with the Lynx Fetch API, you are welcome to submit feature requests or [contribute](https://github.com/lynx-family/lynx/blob/develop/CONTRIBUTING.md) to help Lynx better support the web ecosystem.
139128

140-
For frontend framework-specific data fetching solutions, such as using [TanStack Query (React Query)](https://tanstack.com/query/latest) in ReactLynx, you can refer to the [Data Fetching](/react/data-fetching) chapter of ReactLynx guide.
129+
For front-end framework-specific data fetching solutions, such as using [TanStack Query (React Query)](https://tanstack.com/query/latest) in ReactLynx, you can refer to the [Data Fetching](/react/data-fetching) chapter of the ReactLynx guide.

docs/zh/guide/interaction/networking.mdx

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66

77
:::tip
88

9-
本功能依赖你已经接入了 [Lynx Service](/guide/start/integrate-with-existing-apps) 提供的网络请求服务
9+
本功能依赖于已集成的 [Lynx Service](/guide/start/integrate-with-existing-apps) 所提供的 HTTP 服务。
1010

1111
:::
1212

13-
Lynx 提供了用法和 Web 标准完全一致的 [Fetch API](/api/lynx-api/global/fetch)供熟悉 Web 开发的开发者使用。你可以查阅 MDN 文档的[使用 Fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch) 了解更多信息
13+
Lynx 提供了与 Web 标准兼容的 [Fetch API](/api/lynx-api/global/fetch)用法与 Web 平台保持一致。你可以查阅 MDN [使用 Fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch) 指南了解更多信息
1414

15-
下面是一个使用 [JSONPlaceholder API](https://jsonplaceholder.typicode.com/) 获取并显示用户帖子的示例应用。应用启动后,在组件挂载(mounted)后触发 Fetch API 来请求帖子数据。获取的数据随后展示在一个可滚动的列表中,每个帖子都会显示其 `id``title`。如果请求仍在进行中,则界面上会显示 "Loading..." 消息。
15+
以下示例展示了一个从 [JSONPlaceholder API](https://jsonplaceholder.typicode.com/) 获取并显示用户帖子的应用。该应用在启动后会显示加载状态,并在组件挂载(mounted)后触发 Fetch API 请求以获取帖子数据。获取的数据将显示在一个可滚动的列表中,其中每个帖子都包含其 `id``title`。如果请求仍在进行中,界面将显示 “Loading... 消息。
1616

1717
import { Go } from '@lynx';
1818

@@ -26,13 +26,13 @@ import { Go } from '@lynx';
2626

2727
### 发起请求
2828

29-
通过 `fetch` 从任意 `URL` 获取内容
29+
要从任意 `URL` 获取内容,你可以将 `URL` 传递给 `fetch`
3030

3131
```typescript
3232
fetch('https://jsonplaceholder.typicode.com/todos/1');
3333
```
3434

35-
通过 `fetch` 的第二可选个参数,你可以自定义 HTTP 请求,如:添加自定义 `header`指定 `POST` 请求、携带 `JSON body`
35+
`fetch` 还接受第二个可选参数,用于自定义 HTTP 请求。你可以指定额外的 `header`发起 `POST` 请求或提供 `JSON` 格式的 `body`
3636

3737
```typescript
3838
fetch('https://jsonplaceholder.typicode.com/todos/1', {
@@ -48,13 +48,13 @@ fetch('https://jsonplaceholder.typicode.com/todos/1', {
4848
});
4949
```
5050

51-
请查看 [Fetch Request](/api/lynx-api/global/fetch#request) 获取 Lynx 支持的属性列表
51+
请参阅 [Fetch Request](/api/lynx-api/global/fetch#request) 了解 Lynx 支持的属性
5252

53-
### 处理请求结果
53+
### 处理响应
5454

55-
以上示例展示了如何发起请求。此外在许多情况下,你需要处理请求结果
55+
以上示例展示了如何发起请求。在许多情况下,你还需要处理响应
5656

57-
网络操作本质上是异步的`fetch` 方法遵循异步编程范式,返回一个 `Promise`。你可以使用 `async/await` 语法来等待 `Promise` 完成
57+
网络请求是一项异步操作`fetch` 方法会返回一个 `Promise`,这使得编写异步代码变得非常简单。你可以使用 `async/await` 语法来等待 `Promise` 的解析
5858

5959
```typescript
6060
const getDataFromApiAsync = async () => {
@@ -70,68 +70,58 @@ const getDataFromApiAsync = async () => {
7070
};
7171
```
7272

73-
请查看 [Fetch Response](/api/lynx-api/global/fetch#response) 获取 Lynx 支持的属性列表
73+
请参阅 [Fetch Response](/api/lynx-api/global/fetch#response) 了解 Lynx 支持的属性
7474

75-
不要忘记捕获 `fetch` 可能抛出的任何错误。
75+
不要忘记捕获 `fetch` 可能抛出的任何错误,否则它们将被静默忽略
7676

77-
## 使用 Fetch chunk streaming
77+
## 分块传输编码
7878

79-
传输大量的数据时,可选择使用 chunk streaming,分批处理返回数据,以降低网络请求耗时
79+
对于流式传输大量数据到客户端的场景,分块传输编码(Chunked Transfer Encoding)是理想的选择。该方法允许客户端在数据块到达时进行处理,从而减少整体延迟
8080

81-
你可以查阅 MDN 文档的 [Transfer-Encoding chunked](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Transfer-Encoding#chunked) 了解 chunk streaming 标准
81+
要了解有关此标准的更多信息,请参阅 MDN 关于[分块传输编码](https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Transfer-Encoding#chunked)的指南。
8282

8383
:::tip
8484

85-
为了简化前端开发,LynxSDK 内部将 http 返回的 body 进行分割
85+
为了简化前端开发,Lynx 会对 HTTP 响应体进行处理
8686

87-
chunk 大小与 '\r\n' 分隔符会被去除,只有 chunk 的内容会返回
87+
块大小和 `\r\n` 分隔符会被移除,仅将块内容返回
8888

8989
:::
9090

91-
`lynxExtension` 内的 `useStreaming` 设置为 `true`,以开启 Fetch chunk streaming
91+
要启用分块传输编码,请在 `lynxExtension` 中将 `useStreaming` 选项设置为 `true`
9292

93-
```typescript
94-
fetch('https://e3e0932a-9522-4aec-aec9-12cba94b0c7f.mock.pstmn.io/chunk', {
95-
lynxExtension: { useStreaming: true },
96-
}).then((response) => {
97-
const reader = response.body.getReader();
98-
while (true) {
99-
const { done, value } = await reader.read();
100-
if (done) {
101-
break;
102-
} else {
103-
const text = TextCodecHelper.decode(value);
104-
setData(text);
105-
}
106-
}
107-
});
108-
```
93+
<Go
94+
example="networking"
95+
defaultFile="src/chunked-encoding/index.tsx"
96+
defaultEntryFile="dist/chunked-encoding.lynx.bundle"
97+
entry="src/chunked-encoding"
98+
/>
10999

110100
你可以查阅 MDN 文档的 [Response: body](https://developer.mozilla.org/en-US/docs/Web/API/Response/body)
111101
了解如何处理 streaming body。
112102

113103
## 使用 TextCodecHelper
114104

115-
由于 `PrimJS` 不支持 `TextEncoder/TextDecoder`,为满足前端开发者基本的编解码需求
116-
提供 `TextCodecHelper` 以支持 `utf-8` 编码下 `string``arraybuffer` 间的转换。
117-
具体使用方法如下
105+
由于 `PrimJS` 缺少对 `TextEncoder`/`TextDecoder` 的支持,Lynx 提供了 `TextCodecHelper` 以满足基本的编解码需求。该类支持在 `UTF-8` 编码下 `string``arraybuffer` 之间的转换
106+
107+
用法如下
118108

119-
`arraybuffer` 转换为 `string`
109+
`arraybuffer` 转换为 `string`
120110

121111
```typescript
122112
const str = TextCodecHelper.decode(arraybuffer);
123113
```
124114

125-
`string` 转换为 `arraybuffer`
115+
`string` 转换为 `arraybuffer`
126116

127117
```typescript
128118
const arraybuffer = TextCodecHelper.encode(str);
129119
```
130120

131121
## 使用其他网络库
132122

133-
由于 Fetch API 已内置于 Lynx,这意味着您可以使用依赖于该 API 的第三方库
123+
Fetch API 已内置于 Lynx,这意味着你可以使用依赖于它的第三方库
134124

135-
需要注意的是,Lynx 的 Fetch API 与 [Web Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) 存在细微差异。你可以查阅 [Fetch API Reference - Compatibility](/api/lynx-api/global/fetch#compatibility) 了解更多关于这些差异的信息。因此,你可能需要调整 Web 生态中的库以确保兼容性。如果你在 Lynx Fetch API 上遇到任何问题,欢迎提交功能请求或[贡献](https://github.com/lynx-family/lynx/blob/develop/CONTRIBUTING.md)帮助 Lynx 更好地支持 Web 生态
125+
值得注意的是,Lynx 的 Fetch API 与 [Web Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) 相比存在细微差异。你可以查阅 [Fetch API 参考 - 兼容性](/api/lynx-api/global/fetch#compatibility)部分以了解更多信息。因此,你可能需要调整来自 Web 生态系统的库以确保兼容性。如果你在使用 Lynx Fetch API 时遇到任何问题,欢迎提交功能请求或通过[贡献](https://github.com/lynx-family/lynx/blob/develop/CONTRIBUTING.md)来帮助 Lynx 更好地支持 Web 生态系统
136126

137-
对于前端框架特定的数据获取解决方案,例如在 ReactLynx 中使用 [TanStack Query (React Query)](https://tanstack.com/query/latest)请参考 ReactLynx 指南的[数据获取](/react/data-fetching)章节
127+
对于特定于前端框架的数据获取解决方案,例如在 ReactLynx 中使用 [TanStack Query (React Query)](https://tanstack.com/query/latest)你可以参考 ReactLynx 指南的[数据获取](/react/data-fetching)一章

packages/lynx-example-packages/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"@lynx-example/lynx-api": "0.1.1",
3232
"@lynx-example/main-thread": "0.3.0",
3333
"@lynx-example/native-element": "0.6.1",
34-
"@lynx-example/networking": "0.3.0",
34+
"@lynx-example/networking": "0.4.3",
3535
"@lynx-example/page": "0.5.0",
3636
"@lynx-example/performance-api": "0.6.3",
3737
"@lynx-example/react-lifecycle": "0.4.0",

0 commit comments

Comments
 (0)