UiPath Documentation
functions
latest
false
函数用户指南
  • 概述
    • 关于函数
  • JavaScript 函数
    • 入门指南
    • 构建 JavaScript 函数
    • HTTP 触发器和路由
    • 函数上下文
    • 访问平台服务
    • 测试和调试
  • Python 函数
  • 部署并运行
重要 :
请注意,此内容已使用机器翻译进行了本地化。 新发布内容的本地化可能需要 1-2 周的时间才能完成。

入门指南

安装 CLI,用框架构建 JavaScript 或 TypeScript 函数项目,并在本地运行您的第一个函数。

此页面将引导您完成安装 CLI、构建项目以及在本地运行第一个 JavaScript 函数。

先决条件​

  • Node.js 20 或更高版本。
  • 带有函数工具的 uip CLI:
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 在受支持的两个运行时上加载。

后续步骤​

此页面有帮助吗?

连接

需要帮助? 支持

想要了解详细内容? UiPath Academy

有问题? UiPath 论坛

保持更新