构建 JavaScript 函数
对 JavaScript 函数的剖析,涵盖 HTTP 和作业函数、类型化合同、响应帮助程序、错误处理和日志记录。
每个函数都是functions/文件夹中的一个文件,默认导出defineFunction(...)对象。函数的名称是文件名。名称以下划线开头的文件是帮助程序,而不是函数。CLI 使functions中的uipath.json列表与文件夹保持同步。
import { defineFunction, defineSchema } from "@uipath/coded-functions-js-sdk";
export default defineFunction({
name: "process-order",
input: defineSchema<Input>(),
output: defineSchema<Output>(),
handler: async (input, ctx) => { /* ... */ },
});
import { defineFunction, defineSchema } from "@uipath/coded-functions-js-sdk";
export default defineFunction({
name: "process-order",
input: defineSchema<Input>(),
output: defineSchema<Output>(),
handler: async (input, ctx) => { /* ... */ },
});
HTTP 和作业功能
targetRuntime中的项目uipath.json决定了其中每个函数的调用方式。
targetRuntime | Functions | 声明 | 调用者 |
|---|---|---|---|
javascript-server | HTTP 函数 | method 并为每个函数设置path | 编码应用程序或任何 HTTP 客户端,通过函数的 HTTP 触发器 |
javascript | 作业功能 | method 并省略 path | Maestro 服务任务、运行作业活动、Orchestrator API 或作业触发器 |
uip function new --kind http或--kind job设置targetRuntime 。在 HTTP 函数的项目中,不带method和path函数将无法调用serve 、 run和pack 。HTTP 和作业函数的打包和部署方式相同。
HTTP 函数支持编码应用程序。作业功能是更大的自动化中的步骤。对于第一个触发器,请参阅HTTP 触发器和路由,对于第二个触发器,请参阅调用函数。
合同类型
defineSchema<T>() 将 TypeScript 接口转换为 JSON 架构,驱动在每个调用界面上绑定变量。界面是单一事实来源 — 它指定处理程序并声明合约:
interface CreateOrderInput {
/** Customer reference. */
customerId: string;
/** @default 1 */
quantity?: number;
}
interface CreateOrderInput {
/** Customer reference. */
customerId: string;
/** @default 1 */
quantity?: number;
}
可选属性在架构中将变为可选,并且 JSDoc @default 标签将保留默认值。在 JavaScript 项目中,请传递 JSON 架构对象文本来代替 defineSchema<T>()。
将架构作为文本写入函数文件中。通过变量引用的绑定或架构对象可以不包含在函数的合同中。
声明 output 是可选项。如果存在,则系统会在处理程序的返回值离开函数之前,对其进行验证。
返回结果
返回一个纯对象,以向 200 发送将该对象作为正文:
handler: async (input) => ({ orderId: input.customerId, processed: true }),
handler: async (input) => ({ orderId: input.customerId, processed: true }),
对于其他任何内容,请返回响应对象或使用帮助程序:
import { ok, created, notFound } from "@uipath/coded-functions-js-sdk";
return created({ id: "new-id" }); // 201
return notFound("No such order"); // 404
return { status: 202, body: { queued: true } };
import { ok, created, notFound } from "@uipath/coded-functions-js-sdk";
return created({ id: "new-id" }); // 201
return notFound("No such order"); // 404
return { status: 202, body: { queued: true } };
返回的带有数字status字段的对象被读取为响应: status成为 HTTP 状态,并且仅发送body 。将您自己的输出字段命名为其他名称,例如httpStatus 。
错误
引发 FunctionError 失败,并显示特定状态和消息:
import { FunctionError } from "@uipath/coded-functions-js-sdk";
if (!input.customerId) {
throw new FunctionError("customerId is required", 400);
}
import { FunctionError } from "@uipath/coded-functions-js-sdk";
if (!input.customerId) {
throw new FunctionError("customerId is required", 400);
}
未捕获的错误将变为 500。当函数作为作业运行时,引发的错误会使作业发生故障,并且消息会到达作业结果。
日志记录
使用 SDK 记录器记录。其条目在 Orchestrator 的作业日志级别中达到以下级别:
import { logger } from "@uipath/coded-functions-js-sdk";
logger.info(`Processed order ${input.orderId}`);
import { logger } from "@uipath/coded-functions-js-sdk";
logger.info(`Processed order ${input.orderId}`);
从不记录密码值。
后续步骤
- HTTP 触发器和路由
- 函数上下文— 身份、平台坐标和请求数据。
defineFunction参考— 每个字段及其默认值。