functions
latest
false
函数用户指南
- 概述
- JavaScript 函数
- Python 函数
- 部署并运行
重要 :
请注意,此内容已使用机器翻译进行了本地化。
新发布内容的本地化可能需要 1-2 周的时间才能完成。
入门指南
安装 CLI,用框架构建 JavaScript 或 TypeScript 函数项目,并在本地运行您的第一个函数。
此页面将引导您完成安装 CLI、构建项目以及在本地运行第一个 JavaScript 函数。
先决条件
- Node.js 20 或更高版本。
- 带有函数工具的
uipCLI:
npm install -g @uipath/cli
uip tools install @uipath/function-tool
npm install -g @uipath/cli
uip tools install @uipath/function-tool
- 有权访问 Orchestrator 的 UiPath 帐户,用于发布和调用。
构建项目
uip function new my-functions -l ts
cd my-functions
uip function new my-functions -l ts
cd my-functions
TypeScript 为默认设置;对于 JavaScript,传递 -l js;对于跳过示例函数,传递 --empty。
框架将创建:
| 文件 | 用途 |
|---|---|
functions/hello.ts | 函数逻辑 — 每个文件一个函数。作为可运行的示例启动。 |
functions/_*.ts | 共享帮助程序。前导下划线将文件标记为帮助程序,因此它没有入口点,也没有触发器。 |
uipath.json | 将函数名称映射到其入口点。 |
package.json, tsconfig.json | 标准项目文件。 |
编写您的第一个函数
import { defineFunction, defineSchema } from "@uipath/coded-functions-js-sdk";
interface HelloInput {
name: string;
}
interface HelloOutput {
message: string;
}
export default defineFunction({
name: "hello",
method: "POST",
path: "/hello",
input: defineSchema<HelloInput>(),
output: defineSchema<HelloOutput>(),
handler: async (input) => ({ message: `Hello, ${input.name}!` }),
});
import { defineFunction, defineSchema } from "@uipath/coded-functions-js-sdk";
interface HelloInput {
name: string;
}
interface HelloOutput {
message: string;
}
export default defineFunction({
name: "hello",
method: "POST",
path: "/hello",
input: defineSchema<HelloInput>(),
output: defineSchema<HelloOutput>(),
handler: async (input) => ({ message: `Hello, ${input.name}!` }),
});
defineSchema<T>() 从您的 TypeScript 接口派生 JSON 架构,因此可以使用相同的类型声明合约并设置处理程序类型。在 JavaScript 项目中,请改为传递 JSON 架构对象。
结合 method 和 path 使此函数可通过 HTTP 调用。省略两者,它仅作为作业运行 — 请参阅构建 JavaScript 函数。
在本地运行
uip function serve
uip function serve
这将在 http://localhost:7070 上启动热重载服务器。调用函数:
curl -X POST http://localhost:7070/hello \
-H "Content-Type: application/json" \
-d '{"name":"Alice"}'
# {"message":"Hello, Alice!"}
curl -X POST http://localhost:7070/hello \
-H "Content-Type: application/json" \
-d '{"name":"Alice"}'
# {"message":"Hello, Alice!"}
要从本地运行访问 Orchestrator,请将 UIPATH_ACCESS_TOKEN、UIPATH_BASE_URL、UIPATH_ORG_ID 和 UIPATH_TENANT_ID 放入项目根目录的 .env 文件中。serve 在受支持的两个运行时上加载。
后续步骤
- 构建 JavaScript 函数— 合同、错误和两种函数形状。
- HTTP 触发器和路由— 路由、路径参数和调用已部署的函数。
- 打包和发布— 部署到 Orchestrator。
- CLI 参考— 每个命令和标志。