functions
2.2610
true
函数用户指南
- 概述
- JavaScript 函数
- Python 函数
- 部署并运行
入门指南
安装 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 --kind http
cd my-functions
uip function new my-functions --kind http
cd my-functions
--kind http将创建一个 HTTP 函数项目 ( targetRuntime javascript-server )。--kind job (默认值),用于创建作业功能 ( javascript ) 的项目。TypeScript 是默认语言;对于 JavaScript,传递-l js对于跳过示例函数,传递--empty 。
该项目包含:
| 文件 | 用途 |
|---|---|
functions/hello.ts | 您的函数。functions/中直接包含的每个文件都是一个函数。 |
functions/_*.ts | 共享帮助程序。名称以下划线开头的文件不是函数。 |
uipath.json | 项目的targetRuntime和函数列表。CLI 列表与functions/保持同步。 |
project.uiproj | 项目名称及其主要函数。 |
package.json, package-lock.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 和作业功能、合同和错误。
- HTTP 触发器和路由— 路由、路径参数和调用已部署的函数。
- 打包和发布— 部署到 Orchestrator。
- CLI 参考— 每个命令和标志。