跳转到内容

TypeScript 代码

TypeScript 是 Code 节点的默认语言。类型标注会在编译时被剥除, 随后代码在 QuickJS 引擎中执行。

符号作用
input从输入端口收到的值
vars运行变量的一份只读快照
__vars一个可读写的视图:getsethasdeleteclearkeystoObject
console.log / .info / .warn / .error写入执行日志
fetch(...)HTTP 调用,经由防火墙路由
return返回的值会从 result 端口发出

代码主体被包裹在一个异步函数里:returnawait 可以在顶层使用

interface Commande {
id: number;
montant: number;
statut: 'payee' | 'attente' | 'annulee';
}
const commandes = input as Commande[];
const payees = commandes.filter((c) => c.statut === 'payee');
const total = payees.reduce((s, c) => s + c.montant, 0);
console.log(`${payees.length} commandes payées, total ${total}`);
__vars.set('totalPaye', total);
return { nombre: payees.length, total };

它们既是全局变量,可以通过 require() 获取,无需在 packages 中声明任何东西:

全局变量
_lodashlodash
momentmoment
uuiduuid
nanoidnanoid
CryptoJScrypto-js
Ajvajv
ajvFormatsajv-formats
chaiexpectassertchai
cheeriocheerio
xml2jsxml2js
const signature = CryptoJS.HmacSHA256(input.corps, vars.secret).toString();
return { signature };

对不可用的模块调用 require() 会抛出一个明确的错误。

const reponse = await fetch(`${vars.baseUrl}/sante`);
const corps = await reponse.json();
if (corps.statut !== 'ok') throw new Error(`état ${corps.statut}`);
return corps;

这次调用与其他任何流量一样都要经过防火墙, 并且会在执行停止或超时时被中断。

请在 packages(npm)中声明它们;它们会被打包一次,随后离线挂载。

同一套环境也暴露 pm.*bru.* 兼容层 —— 参见 JavaScript 代码