Skip to content

Commit e20b0be

Browse files
committed
chore: add task docs
1 parent 4954ffa commit e20b0be

File tree

5 files changed

+132
-23
lines changed

5 files changed

+132
-23
lines changed

docs/docs/zh-CN/guide/task.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
---
2+
title: 任务
3+
---
4+
5+
## 任务是什么
6+
7+
该功能可以按照配置,静默运行代码、发起请求,并将响应用于规则中。例如,您可以使用该功能定期刷新某个 token,并将其放入 header 中。
8+
9+
## 配置指南
10+
11+
### 任务 Key
12+
13+
任务 Key 是任务的唯一标识,必须以字母开头,且只能包含字母、数字、下划线。Key 设置后不可修改。
14+
15+
### 运行类型
16+
17+
您可以选择在何时运行任务:
18+
19+
* 仅运行一次:仅在浏览器启动时运行一次。
20+
* 固定间隔任务:在浏览器启动时运行一次,并每隔固定时间运行一次。
21+
* 定时任务:配置 Cron 表达式,在浏览器启动时运行一次,并在指定时间重复运行。
22+
23+
您可以参考[该文档](https://help.aliyun.com/document_detail/133509.html)学习 Cron 表达式。
24+
25+
### 执行类型
26+
27+
常规方式下,您需要配置一个请求地址及请求参数。Header Editor 会定时向该地址发起请求。
28+
29+
响应类型可以用于后续步骤处理。
30+
31+
您可以使用 [JsonLogic](https://jsonlogic.com/) 对结果进行校验。使用 status 获取响应的 HTTP 状态码,使用 body 获取响应体。例如:
32+
33+
```js
34+
// 响应结果
35+
{
36+
"code": 200,
37+
}
38+
39+
// JsonLogic
40+
{
41+
"==": [{ "var" : "body.code" }, 200]
42+
}
43+
```
44+
45+
您也可以使用自定义函数。自定义函数不会传入任何参数,在函数中处理请求及校验逻辑,并返回结果。例如:
46+
47+
```js
48+
const res = await fetch('https://example.com');
49+
if (res.status !== 200) {
50+
throw new Error('Request failed');
51+
}
52+
return res.json();
53+
```
54+
55+
## 在规则中使用
56+
57+
在规则中,使用 `{$TASK.TASK_KEY.Path}` 获取任务返回的响应结果。
58+
59+
例如,您有一个 Key 为 `task1` 的任务,该任务返回了如下结果:
60+
61+
```json
62+
{
63+
"code": 200,
64+
"data": {
65+
"token": "123456"
66+
}
67+
}
68+
```
69+
70+
在规则中,您可以使用 `{$TASK.task1.data.token}` 获取任务返回的 token。
71+
72+
注意:
73+
* 可以在规则的 重定向至、请求/响应头内容、响应体中使用此语法。
74+
* 如果响应类型配置为“文本”,则任何 `{$TASK.task1.*}` 均会获取到完整的响应文本。
75+
* 当请求失败或尚未完成请求时,该语法会被替换为空字符串。

src/pages/background/request-handler/utils.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,15 @@ export function parseJSONPath(text: string) {
5555
return;
5656
}
5757

58-
const value = get(run.result, restPath);
59-
result = result.replace(match, value);
58+
if (typeof run.result === 'string') {
59+
result = result.replaceAll(match, run.result);
60+
} else {
61+
const value = get(run.result, restPath);
62+
result = result.replaceAll(
63+
match,
64+
typeof value === 'undefined' || value === null ? '' : value,
65+
);
66+
}
6067
});
6168

6269
return result;

src/pages/options/sections/import-and-export/import-drawer/index.tsx

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import * as React from 'react';
2-
import { SideSheet, Button, Table, Select, Toast } from '@douyinfe/semi-ui';
3-
import { css } from '@emotion/css';
41
import { IconSave } from '@douyinfe/semi-icons';
2+
import { Button, Select, SideSheet, Table, Toast } from '@douyinfe/semi-ui';
3+
import { css } from '@emotion/css';
4+
import * as React from 'react';
5+
import BoolRadioGroup from '@/pages/options/components/bool-radio';
56
import { selectGroup } from '@/pages/options/utils';
6-
import Api from '@/share/pages/api';
7+
import { TABLE_NAMES_ARR } from '@/share/core/constant';
78
import { fromJson } from '@/share/core/rule-utils';
9+
import type { BasicRule, ImportRule } from '@/share/core/types';
810
import { t } from '@/share/core/utils';
9-
import type { ImportRule, BasicRule } from '@/share/core/types';
10-
import BoolRadioGroup from '@/pages/options/components/bool-radio';
11-
import { TABLE_NAMES_ARR } from '@/share/core/constant';
11+
import Api from '@/share/pages/api';
1212

1313
interface ImportDrawerProps {
1414
onCancel?: () => void;
@@ -22,7 +22,10 @@ interface ImportDrawerState {
2222
useRecommend: boolean;
2323
}
2424

25-
export default class ImportDrawer extends React.Component<ImportDrawerProps, ImportDrawerState> {
25+
export default class ImportDrawer extends React.Component<
26+
ImportDrawerProps,
27+
ImportDrawerState
28+
> {
2629
constructor(props: any) {
2730
super(props);
2831

@@ -50,13 +53,13 @@ export default class ImportDrawer extends React.Component<ImportDrawerProps, Imp
5053
let totalCount = 0;
5154
const importList: ImportRule[] = [];
5255
const list = typeof content === 'string' ? fromJson(content) : content;
53-
TABLE_NAMES_ARR.forEach((tableName) => {
56+
TABLE_NAMES_ARR.forEach(tableName => {
5457
if (!list[tableName]) {
5558
return;
5659
}
57-
list[tableName].forEach((e) => {
60+
list[tableName].forEach(e => {
5861
totalCount++;
59-
Api.getRules(tableName, { name: e.name }).then((rule) => {
62+
Api.getRules(tableName, { name: e.name }).then(rule => {
6063
const it: ImportRule = {
6164
...e,
6265
group: e.group || t('ungrouped'),
@@ -82,9 +85,10 @@ export default class ImportDrawer extends React.Component<ImportDrawerProps, Imp
8285
}
8386

8487
handleConfirm() {
88+
// TODO: 处理 task 导入
8589
// 确认导入
8690
const queue: any[] = [];
87-
this.state.list.forEach((e: BasicRule) => {
91+
this.state.list.forEach((e: any) => {
8892
// 不导入
8993
if (e.importAction === 3) {
9094
return;
@@ -131,7 +135,7 @@ export default class ImportDrawer extends React.Component<ImportDrawerProps, Imp
131135
}
132136

133137
handleSelectGroup(item: ImportRule) {
134-
selectGroup(item.group).then((res) => {
138+
selectGroup(item.group).then(res => {
135139
item.group = res;
136140
this.forceUpdate();
137141
});
@@ -166,11 +170,18 @@ export default class ImportDrawer extends React.Component<ImportDrawerProps, Imp
166170
}
167171
`}
168172
footer={
169-
<div style={{ display: 'flex', justifyContent: 'flex-end', gap: '8px' }}>
173+
<div
174+
style={{ display: 'flex', justifyContent: 'flex-end', gap: '8px' }}
175+
>
170176
<Button type="tertiary" theme="light" onClick={this.handleCancel}>
171177
{t('cancel')}
172178
</Button>
173-
<Button type="primary" theme="solid" onClick={this.handleConfirm} icon={<IconSave />}>
179+
<Button
180+
type="primary"
181+
theme="solid"
182+
onClick={this.handleConfirm}
183+
icon={<IconSave />}
184+
>
174185
{t('save')}
175186
</Button>
176187
</div>
@@ -196,7 +207,11 @@ export default class ImportDrawer extends React.Component<ImportDrawerProps, Imp
196207
<span>
197208
<span>{value}</span>
198209
&nbsp;
199-
<Button className="select-group" size="small" onClick={this.handleSelectGroup.bind(this, item)}>
210+
<Button
211+
className="select-group"
212+
size="small"
213+
onClick={this.handleSelectGroup.bind(this, item)}
214+
>
200215
{t('choose')}
201216
</Button>
202217
</span>
@@ -207,10 +222,16 @@ export default class ImportDrawer extends React.Component<ImportDrawerProps, Imp
207222
render: (_v: any, item: ImportRule) => (
208223
<Select
209224
value={item.importAction}
210-
onChange={(value: string) => this.handleActionChange(item, value)}
225+
onChange={(value: string) =>
226+
this.handleActionChange(item, value)
227+
}
211228
optionList={[
212229
{ label: t('import_new'), value: 1 },
213-
{ label: t('import_override'), value: 2, disabled: item.importOldId === -1 },
230+
{
231+
label: t('import_override'),
232+
value: 2,
233+
disabled: item.importOldId === -1,
234+
},
214235
{ label: t('import_drop'), value: 3 },
215236
]}
216237
/>
@@ -243,10 +264,15 @@ export default class ImportDrawer extends React.Component<ImportDrawerProps, Imp
243264
<span>
244265
<span>{this.state.group}</span>
245266
&nbsp;
246-
<Button className="select-group" size="small" onClick={this.handleSelectAll}>
267+
<Button
268+
className="select-group"
269+
size="small"
270+
onClick={this.handleSelectAll}
271+
>
247272
{t('choose')}
248273
</Button>
249-
</span>),
274+
</span>
275+
),
250276
value: false,
251277
},
252278
]}

src/pages/options/sections/tasks/edit/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ const TaskConfig = () => {
7171
<Form.TextArea
7272
field="fetch.body"
7373
label={t('request_body')}
74-
placeholder={t('request_body_placeholder')}
7574
rows={4}
7675
/>
7776
<Form.Select
@@ -255,6 +254,7 @@ const Edit = ({ visible, task: taskProp, onClose }: EditProps) => {
255254
field="key"
256255
label={t('task_key')}
257256
placeholder={t('task_key_placeholder')}
257+
disabled={Boolean(initInput?.key)}
258258
/>
259259

260260
<Form.Input

src/share/core/rule-utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export function initRule(
6969
}
7070

7171
export function createExport(arr: { [key: string]: Array<Rule | InitdRule> }) {
72+
// TODO: 一并导出规则
7273
const result: { [key: string]: BasicRule[] } = {};
7374
Object.keys(arr).forEach(k => {
7475
result[k] = arr[k].map(e => convertToBasicRule(e));

0 commit comments

Comments
 (0)