Skip to content

Commit

Permalink
添加 load() 工具函数说明
Browse files Browse the repository at this point in the history
  • Loading branch information
vilicvane committed Nov 5, 2023
1 parent 01fa008 commit 7b12cd2
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 5 deletions.
10 changes: 10 additions & 0 deletions docs/.vitepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ gtag('config', 'G-HD5ZM67WNQ');
{text: '高级用法', link: '/script/advanced-usages.md'},
{text: '运行环境', link: '/script/runtime-environment.md'},
{text: '旧版迁移', link: '/script/legacy-migration.md'},
{
text: '工具箱',
items: [
{
text: 'load()',
link: '/script/utils/load.md',
},
],
collapsed: true,
},
{
text: '示例',
items: [
Expand Down
9 changes: 5 additions & 4 deletions docs/script/examples/npm-package-new-release.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@
::: code-group

```js [script.js]
import {script} from '@digshare/script';
import {script, load} from '@digshare/script';

export default script(async state => {
const response = await fetch('https://registry.npmjs.org/typescript/latest');

const {version: latest} = await response.json();
const {version: latest} = await load(
'https://registry.npmjs.org/typescript/latest',
'json',
);

if (!state) {
// 初始化 state 为当前版本。
Expand Down
2 changes: 1 addition & 1 deletion docs/script/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ npx dss deploy
? 确认继续? › (y/N)
```

如果是第一次部署脚本,`dss` 会自动打开浏览器,让我们选择频道进行授权,授权成功后将继续完成部署。
如果是第一次部署脚本,`dss` 会打印二维码,让我们使用盯梢应用扫码选择频道进行授权,授权成功后将继续完成部署。

::: tip 授权信息
授权成功后,密钥将会被自动保存到当前项目下的 `.dssrc` 文件中。该密钥可随时重新授权获得,如果你通过 Git 进行版本管理,建议将 `.dssrc` 文件加入 `.gitignore` 中。
Expand Down
45 changes: 45 additions & 0 deletions docs/script/utils/load.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# load()

盯梢脚本经常会涉及到网络请求,偶尔会遇到临时的错误,此时盯梢也会像频道主推送错误提示。但很多时候,这些错误是可以忽略的,或者可以通过重试来解决。

## 例子

```ts
import {load} from '@digshare/script';

// 获取文本
const html = await load('https://example.com', 'text');

// 获取 JSON 值
const data = await load('https://api.example.com/list', 'json');

// 获取流
const stream = await load('https://example.com/image.png');
```

## 签名

```ts
type LoadOptions = {
/**
* 尝试次数,默认为 3。
*/
attempts?: number;
/**
* 尝试间隔(毫秒),默认为 1000。
*/
attemptInterval?: number;
} & RequestInit;

type LoadType = 'text' | 'json' | 'blob' | 'arrayBuffer';

declare function load(
input: NodeJS.fetch.RequestInfo,
options?: LoadOptions,
): Promise<ReadableStream>;
declare function load<TType extends LoadType>(
input: NodeJS.fetch.RequestInfo,
type: TType,
options?: LoadOptions,
): Promise<Awaited<ReturnType<Response[TType]>>>;
```

0 comments on commit 7b12cd2

Please sign in to comment.