Skip to content

Commit

Permalink
wip: 更新配置文件示例
Browse files Browse the repository at this point in the history
  • Loading branch information
renxia committed Jan 24, 2024
1 parent 69427e7 commit aededaf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
4 changes: 3 additions & 1 deletion example/rules/print-res-body.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module.exports = {
on: 'res-body', // 规则执行的时机,res-body 表示在收到响应体后触发
ruleId: 'print-response-body', // 规则唯一标记,必须设置且唯一
desc: '打印接口返回内容', // 规则描述
method: '*',
url: '**', // ** 表示匹配所有 url 地址
handler({ url, req, reqBody, resHeaders, resBody, X }) {
// 只处理文本类型的请求
Expand All @@ -19,6 +20,7 @@ module.exports = {
}

// 若返回 body 参数则会以该内容返回
// return { body: modifyedResBody };
// 若返回 envConfig 参数则会以该内容写入环境变量文件
// return { body: modifyedResBody, envConfig };
},
};
37 changes: 31 additions & 6 deletions w2.x-scripts.config.sample.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
const rulesIndex = require.resolve('@lzwme/whistle.x-scripts/x-scripts-rules', { paths: require.main.paths });
const rules = require(rulesIndex);
console.log('内置 rules:', rules.map(rule => rule.ruleId).join(', '));

/** @type {import('@lzwme/whistle.x-scripts').W2CookiesConfig} */
const config = {
// debug: false,
Expand All @@ -17,10 +13,39 @@ const config = {
/** 数据处理防抖时间间隔。单位为秒,默认为 10 (s) */
throttleTime: 10,
/** 指定规则集文件路径或所在目录,尝试从该列表加载自定义的规则集 */
// ruleDirs: [rulesIndex, './custom-x-scripts-rules'],
ruleDirs: [
// require.resolve('@lzwme/x-scripts-rules', { paths: require.main.paths }),
'./local-x-scripts-rules',
],
/** 自定义脚本规则 */
rules: [
...rules, // 使用内置规则
// ...rules,
{
on: 'req-body',
ruleId: 'rule-test',
desc: '这是一条测试规则示例',
method: '*',
url: '**',
toQL: false,
toEnvFile: true,
handler({ url, req, reqBody, resHeaders, resBody, X }) {
// 只处理文本类型的请求
if (X.isText(req.headers) && !/\.(js|css)/.test(url)) {
// X 是提供的工具类对象,方便简化脚本编写逻辑调用
const { magenta, gray, cyan } = X.FeUtils.color;

console.log(`\n\n[${magenta('handler')}][${cyan(req.method)}] -`, gray(url));
console.log(cyan('\req headers:'), req.headers);
console.log(cyan('\res headers:'), resHeaders);
if (reqBody) console.log(cyan('请求参数:'), reqBody.toString());
if (resBody) console.log(cyan('返回内容:'), resBody);
}

// 若返回 body 参数则会以该内容返回
// 若返回 envConfig 参数则会以该内容写入环境变量文件
// return { body: modifyedResBody, envConfig };
},
},
],
};

Expand Down

0 comments on commit aededaf

Please sign in to comment.